]> git.cworth.org Git - sup/blobdiff - lib/sup/util.rb
fixed off-by-one error in imap.rb and maildir.rb
[sup] / lib / sup / util.rb
index bd150b484da89dfb238efe0e2dd9920846cced62..ceaf0b8dabc8a83ea451005e9b27bc109823caf1 100644 (file)
@@ -1,3 +1,4 @@
+require 'thread'
 require 'lockfile'
 require 'mime/types'
 require 'pathname'
@@ -61,7 +62,7 @@ module RMail
     end
 
     def charset
-      if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/
+      if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/i
         $1
       end
     end
@@ -70,17 +71,17 @@ module RMail
       a = Message.new
       a.header.add "Content-Disposition", "attachment; filename=#{filename.inspect}"
       a.header.add "Content-Type", "#{mime_type}; name=#{filename.inspect}"
-      a.header.add "Content-Transfer-Encoding", encoding
+      a.header.add "Content-Transfer-Encoding", encoding if encoding
       a.body =
         case encoding
         when "base64"
           [payload].pack "m"
         when "quoted-printable"
           [payload].pack "M"
-        when "7bit", "8bit"
+        when "7bit", "8bit", nil
           payload
         else
-          raise EncodingUnsupportedError, t.encoding
+          raise EncodingUnsupportedError, encoding.inspect
         end
       a
     end
@@ -600,3 +601,15 @@ class OrderedHash < Hash
   def each; @keys.each { |k| yield k, self[k] } end
 end
 
+## easy thread-safe class for determining who's the "winner" in a race (i.e.
+## first person to hit the finish line
+class FinishLine
+  def initialize
+    @m = Mutex.new
+    @over = false
+  end
+
+  def winner?
+    @m.synchronize { !@over && @over = true }
+  end
+end