]> git.notmuchmail.org Git - sup/blobdiff - lib/sup/util.rb
Merge branch 'master' into next
[sup] / lib / sup / util.rb
index cc15803b1ddb4ed9a6b4d91d7c6ceb935e26a9e5..c54a2c0db2b47efdc547000cc0ed9f8974536960 100644 (file)
@@ -1,3 +1,4 @@
+require 'thread'
 require 'lockfile'
 require 'mime/types'
 require 'pathname'
@@ -37,16 +38,7 @@ class Pathname
       rescue SystemCallError
         return "?"
       end
-
-    if s < 1024
-      s.to_s + "b"
-    elsif s < (1024 * 1024)
-      (s / 1024).to_s + "k"
-    elsif s < (1024 * 1024 * 1024)
-      (s / 1024 / 1024).to_s + "m"
-    else
-      (s / 1024 / 1024 / 1024).to_s + "g"
-    end
+    s.to_human_size
   end
 
   def human_time
@@ -63,33 +55,35 @@ module RMail
   class EncodingUnsupportedError < StandardError; end
 
   class Message
-    def add_file_attachment fn
+    def self.make_file_attachment fn
       bfn = File.basename fn
-      a = Message.new
       t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first
+      make_attachment IO.read(fn), t.content_type, t.encoding, bfn.to_s
+    end
+
+    def charset
+      if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/i
+        $1
+      end
+    end
 
-      a.header.add "Content-Disposition", "attachment; filename=#{bfn.to_s.inspect}"
-      a.header.add "Content-Type", "#{t.content_type}; name=#{bfn.to_s.inspect}"
-      a.header.add "Content-Transfer-Encoding", t.encoding
+    def self.make_attachment payload, mime_type, encoding, filename
+      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 if encoding
       a.body =
-        case t.encoding
+        case encoding
         when "base64"
-          [IO.read(fn)].pack "m"
+          [payload].pack "m"
         when "quoted-printable"
-          [IO.read(fn)].pack "M"
-        when "7bit", "8bit"
-          IO.read(fn)
+          [payload].pack "M"
+        when "7bit", "8bit", nil
+          payload
         else
-          raise EncodingUnsupportedError, t.encoding
+          raise EncodingUnsupportedError, encoding.inspect
         end
-
-      add_part a
-    end
-
-    def charset
-      if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/
-        $1
-      end
+      a
     end
   end
 end
@@ -114,7 +108,9 @@ class Module
   def defer_all_other_method_calls_to obj
     class_eval %{
       def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end
-      def respond_to? meth; @#{obj}.respond_to?(meth); end
+      def respond_to?(m, include_private = false)
+        @#{obj}.respond_to?(m, include_private)
+      end
     }
   end
 end
@@ -192,11 +188,6 @@ class String
     ret
   end
 
-  ## one of the few things i miss from perl
-  def ucfirst
-    self[0 .. 0].upcase + self[1 .. -1]
-  end
-
   ## a very complicated regex found on teh internets to split on
   ## commas, unless they occurr within double quotes.
   def split_on_commas
@@ -280,6 +271,11 @@ class String
   def normalize_whitespace
     gsub(/\t/, "    ").gsub(/\r/, "")
   end
+
+  ## takes a space-separated list of words, and returns an array of symbols.
+  ## typically used in Sup for translating Ferret's representation of a list
+  ## of labels (a string) to an array of label symbols.
+  def symbolistize; split.map { |x| x.intern } end
 end
 
 class Numeric
@@ -294,14 +290,21 @@ class Numeric
   end
 
   def in? range; range.member? self; end
+
+  def to_human_size
+    if self < 1024
+      to_s + "b"
+    elsif self < (1024 * 1024)
+      (self / 1024).to_s + "k"
+    elsif self < (1024 * 1024 * 1024)
+      (self / 1024 / 1024).to_s + "m"
+    else
+      (self / 1024 / 1024 / 1024).to_s + "g"
+    end
+  end
 end
 
 class Fixnum
-  def num_digits base=10
-    return 1 if self == 0
-    1 + (Math.log(self) / Math.log(10)).floor
-  end
-  
   def to_character
     if self < 128 && self >= 0
       chr
@@ -526,7 +529,9 @@ class Recoverable
   def to_yaml x; __pass :to_yaml, x; end
   def is_a? c; @o.is_a? c; end
 
-  def respond_to? m; @o.respond_to? m end
+  def respond_to?(m, include_private=false)
+    @o.respond_to?(m, include_private)
+  end
 
   def __pass m, *a, &b
     begin
@@ -577,23 +582,56 @@ end
 class OrderedHash < Hash
   alias_method :store, :[]=
   alias_method :each_pair, :each
+  attr_reader :keys
 
-  def initialize
+  def initialize *a
     @keys = []
+    a.each { |k, v| self[k] = v }
   end
 
-  def []=(key, val)
+  def []= key, val
     @keys << key unless member?(key)
     super
   end
 
-  def delete(key)
-    @keys.delete(key)
+  def values; keys.map { |k| self[k] } end
+  def index key; @keys.index key end
+
+  def delete key
+    @keys.delete key
     super
   end
 
-  def each
-    @keys.each { |k| yield k, self[k] }
+  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
 
+class Iconv
+  def self.easy_decode target, charset, text
+    return text if charset =~ /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i
+    charset = case charset
+                when /UTF[-_ ]?8/i: "utf-8"
+                when /(iso[-_ ])?latin[-_ ]?1$/i: "ISO-8859-1"
+                when /iso[-_ ]?8859[-_ ]?15/i: 'ISO-8859-15'
+                when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i: "utf-7"
+                else charset
+              end
+
+    # Convert:
+    #
+    # Remember - Iconv.open(to, from)!
+    Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
+  end
+end