commit 1879aa760b88e0085a9dc7eba4398a3925b2b6d6
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Thu Jan 2 03:15:44 2014 +0100

    [evol] switched from (abandonned upstream) TMail to Mail

diff --git a/bin/postman b/bin/postman
index 1c91dad..d83856d 100755
--- a/bin/postman
+++ b/bin/postman
@@ -121,7 +121,7 @@ module CyborgHood
 
       def process_message(msg)
         mail = Mail.new(msg.content)
-        logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_pretty} -> #{mail.to_pretty} (#{mail.subject_pretty})"
+        logger.info "Received mail with ID '#{mail.message_id}': #{mail[:from].decoded} -> #{mail[:to].decoded} (#{mail.subject})"
 
         # ignore mails not signed or encrypted
         unless mail.is_pgp_signed? or mail.is_pgp_encrypted?
@@ -209,7 +209,7 @@ module CyborgHood
         if reply_attachments.empty?
           transcript_part = mail_reply
         else
-          mail_reply.to_multipart!
+          mail_reply.parts.clear
 
           p = CyborgHood::Mail.blank
           transcript_part = p
diff --git a/lib/cyborghood/mail.rb b/lib/cyborghood/mail.rb
index fe585ea..09078ac 100644
--- a/lib/cyborghood/mail.rb
+++ b/lib/cyborghood/mail.rb
@@ -17,10 +17,9 @@
 #++
 
 require 'delegate'
-require 'tmail'
-require 'tmail_gpg'
-require 'tmail_extra'
-require 'net/smtp'
+require 'mail'
+require 'mail/gpg'
+require 'cyborghood/objects'
 require 'fileutils'
 require 'digest/md5'
 
@@ -41,7 +40,7 @@ module CyborgHood
     end
 
     def ok?
-      @error.nil? and @user and @message
+      !!(@error.nil? and @user and @message)
     end
 
     def warn_sender
@@ -60,12 +59,12 @@ module CyborgHood
       @config = Config.instance
 
       if msg.nil?
-        @mail = TMail::Mail.new
+        @mail = ::Mail.new
         set_custom_headers
-      elsif msg.is_a? TMail::Mail
+      elsif msg.is_a? ::Mail::Message
         @mail = msg
       else
-        @mail = TMail::Mail.parse(msg)
+        @mail = ::Mail.new(msg)
       end
     end
 
@@ -75,6 +74,10 @@ module CyborgHood
       m
     end
 
+    def self.read(path)
+      self.new(::Mail.read(path))
+    end
+
     def __getobj__
       @mail
     end
@@ -94,45 +97,27 @@ module CyborgHood
       MailReport.new(:error => _("Mail not RFC3156 compliant."))
     end
 
-    def create_reply
-      tmail_reply = @mail.create_reply
-      tmail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
-      tmail_reply.to_addrs = (@mail.reply_to_addrs_pretty || @mail.from_addrs_pretty).collect do |a|
-        TMail::Address.parse(@mail.class.header_value_smtp(a))
-      end
-      reply = self.class.new(tmail_reply.to_s)
-      reply.set_custom_headers
-      reply.user = @user
-      reply
+    def reply
+      mail_reply = self.class.new(@mail.reply)
+      mail_reply.from = @config.mail.from_address if @config.mail.from_address
+      mail_reply.set_custom_headers
+      mail_reply.user = @user
+      mail_reply
     end
 
     def create_simple_reject_reply(msg)
-      reply = create_reply()
-      reply.set_content_type("text", "plain", {'charset' => "utf-8"})
-      reply.set_disposition("inline")
-      reply.quoted_printable_body = msg + self.default_body_signature
-      reply.sign
-      reply.crypt(reply.user.keyFingerPrint) unless reply.user.nil?
-      reply
-    end
-
-    def to_multipart!
-      self.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
-      self.transfer_encoding = "7bit"
-      self.quoted_body = "This mail is in MIME format.\n"
-      self.parts.clear
+      mail_reply = reply()
+      mail_reply.charset = "utf-8"
+      mail_reply.body = msg + self.default_body_signature
+      mail_reply.sign
+      mail_reply.crypt(mail_reply.user.keyFingerPrint) unless mail_reply.user.nil?
+      mail_reply
     end
 
     def set_custom_headers
       @mail['Organization'] = @config.mail.organization
     end
 
-    def check_headers
-      @mail.header.keys.each do |h|
-        @mail[h] = @mail.class.quote_address_if_necessary(@mail[h].to_s, "utf-8")
-      end
-    end
-
     def default_body_signature
       s = "\n" +
         "-- \n" +
@@ -143,15 +128,18 @@ module CyborgHood
     end
 
     def deliver
-      check_headers
-
-      smtp_server = @config.mail.smtp_server
-      smtp_port = @config.mail.smtp_port
-      smtp_from = @mail.from_addrs.collect{|a| a.address}.join(", ")
-      smtp_to = @mail.to_addrs.collect{|a| a.address}
-      Net::SMTP.start(smtp_server, smtp_port) do |smtp|
-        smtp.send_message(@mail.to_s, smtp_from, smtp_to)
+      unless @@delivery_initialized
+        Mail.defaults do
+          delivery_method :smtp, {
+            :address => @config.mail.smtp_server,
+            :port => @config.mail.smtp_port
+          }
+        end
+
+        @@delivery_initialized = true
       end
+
+      @mail.deliver
     end
 
     def to_s
@@ -171,7 +159,7 @@ module CyborgHood
 
     def sign_and_crypt(fingerprint = nil)
       # not using sign_and_crypt(), to avoid repeating code
-      sign()
+      sign
       crypt(fingerprint || @user.keyFingerPrint)
     end
 
@@ -181,24 +169,28 @@ module CyborgHood
       sigs_check = verify_pgp_signature()
       return MailReport.new(:error => _("Mail not formatted correctly (signed part).")) if sigs_check.nil? or sigs_check.size != 1
 
-      sig_check = sigs_check.first
-      return MailReport.new(:error => _("Mail content tampered or badly signed: %{sig_err}", :sig_err => sig_check.to_s)) unless sig_check.status == 0
+      sig = sigs_check.first
+      return MailReport.new(:error => _("Mail content tampered or badly signed: %{sig_err}", :sig_err => sig.to_s)) if sig.bad?
 
-      logger.info "Mail content was properly signed by key #{sig_check.fingerprint}"
-      user = Person.find_by_fingerprint(sig_check.fingerprint)
+      logger.info "Mail content was properly signed by key #{sig.fingerprint}"
+      begin
+        user = Person.find_by_fingerprint(sig.key.fingerprint)
+      rescue EOFError => error
+        return MailReport.new(:error => _("Could not get public key for '%{fpr}'", :fpr => check.fingerprint))
+      end
       return MailReport.new(:error => _("Mail is from an unknown person."), :warn_sender => true) if user.nil?
 
       logger.info "Mail is from user #{user.uid} (#{user.cn})"
       @user = user
 
-      drift = Time.new.to_i - sig_check.timestamp.to_i
+      drift = Time.new.to_i - sig.timestamp.to_i
       logger.debug "Signature drift time: #{drift}"
       unless drift.abs < @config.mail.max_drift_time
         if drift > 0
           return MailReport.new(:error => _("The signature was made too long ago (check your system clock). Rejected message to avoid replay attacks."), :user => user)
         else
           # mark message to prevent later replay of the message
-          mark_processed(sig_check.timestamp)
+          mark_processed(sig.timestamp)
           return MailReport.new(:error => _("The signature was made in the future (check your system clock). Rejected message to avoid replay attacks."), :user => user)
         end
       end
@@ -209,9 +201,9 @@ module CyborgHood
       signed_content = pgp_signed_part()
 
       # create a fake mail and chain parsing operations
-      plain_mail = self.class.new(signed_content)
+      plain_mail = self.class.new(signed_content.encoded)
       plain_mail.user = @user
-      plain_mail.signature_timestamp = sig_check.timestamp
+      plain_mail.signature_timestamp = sig.timestamp
       # propagate message_id to be able to mark messages (replay protection)
       plain_mail.message_id = @mail.message_id
 
