]> git.cworth.org Git - sup/blobdiff - lib/sup/mbox.rb
add delete key support to textfields
[sup] / lib / sup / mbox.rb
index 04cd2f8871f18f8995a99878a8c51781c8901b8b..0ce52fe24fd9ff8bbba19bea2141dfd8e4d4625d 100644 (file)
@@ -1,10 +1,14 @@
 require "sup/mbox/loader"
 require "sup/mbox/ssh-file"
 require "sup/mbox/ssh-loader"
+require "sup/rfc2047"
 
 module Redwood
 
-## some utility functions
+## some utility functions. actually these are not mbox-specific at all
+## and should be moved somewhere else.
+##
+## TODO: move functionality to somewhere better, like message.rb
 module MBox
   BREAK_RE = /^From \S+/
 
@@ -16,26 +20,48 @@ module MBox
     ## when scanning over large mbox files.
     while(line = f.gets)
       case line
-      when /^From:\s+(.*)$/i: header[last = "From"] = $1
-      when /^To:\s+(.*)$/i: header[last = "To"] = $1
-      when /^Cc:\s+(.*)$/i: header[last = "Cc"] = $1
-      when /^Bcc:\s+(.*)$/i: header[last = "Bcc"] = $1
-      when /^Subject:\s+(.*)$/i: header[last = "Subject"] = $1
-      when /^Date:\s+(.*)$/i: header[last = "Date"] = $1
-      when /^Message-Id:\s+<(.*)>$/i: header[last = "Message-Id"] = $1
-      when /^References:\s+(.*)$/i: header[last = "References"] = $1
-      when /^In-Reply-To:\s+(.*)$/i: header[last = "In-Reply-To"] = $1
-      when /^List-Post:\s+(.*)$/i: header[last = "List-Post"] = $1
-      when /^Reply-To:\s+(.*)$/i: header[last = "Reply-To"] = $1
-      when /^Status:\s+(.*)$/i: header[last = "Status"] = $1
-      when /^Delivered-To:\s+(.*)$/i
-        header[last = "Delivered-To"] = $1 unless header["Delivered-To"]
+      when /^(From):\s+(.*?)\s*$/i,
+        /^(To):\s+(.*?)\s*$/i,
+        /^(Cc):\s+(.*?)\s*$/i,
+        /^(Bcc):\s+(.*?)\s*$/i,
+        /^(Subject):\s+(.*?)\s*$/i,
+        /^(Date):\s+(.*?)\s*$/i,
+        /^(References):\s+(.*?)\s*$/i,
+        /^(In-Reply-To):\s+(.*?)\s*$/i,
+        /^(Reply-To):\s+(.*?)\s*$/i,
+        /^(List-Post):\s+(.*?)\s*$/i,
+        /^(List-Subscribe):\s+(.*?)\s*$/i,
+        /^(List-Unsubscribe):\s+(.*?)\s*$/i,
+        /^(Status):\s+(.*?)\s*$/i: header[last = $1] = $2
+      when /^(Message-Id):\s+(.*?)\s*$/i: header[mid_field = last = $1] = $2
+
+      ## these next three can occur multiple times, and we want the
+      ## first one
+      when /^(Delivered-To):\s+(.*)$/i,
+        /^(X-Original-To):\s+(.*)$/i,
+        /^(Envelope-To):\s+(.*)$/i: header[last = $1] ||= $2
+
       when /^$/: break
-      when /:/: last = nil
+      when /^\S+: /: last = nil # some other header we don't care about
       else
-        header[last] += line.gsub(/^\s+/, "") if last
+        header[last] += " " + line.chomp.gsub(/^\s+/, "") if last
       end
     end
+
+    if mid_field && header[mid_field] && header[mid_field] =~ /<(.*?)>/
+      header[mid_field] = $1
+    end
+
+    header.each do |k, v|
+      next unless Rfc2047.is_encoded? v
+      header[k] =
+        begin
+          Rfc2047.decode_to $encoding, v
+        rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence => e
+          Redwood::log "warning: error decoding RFC 2047 header (#{e.class.name}): #{e.message}"
+          v
+        end
+    end
     header
   end