+require 'thread'
require 'lockfile'
require 'mime/types'
require 'pathname'
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
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
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
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
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 /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