]> git.notmuchmail.org Git - sup/blobdiff - lib/sup/modes/edit-message-mode.rb
Merge branch 'edit-message-mode-longname' into next
[sup] / lib / sup / modes / edit-message-mode.rb
index f4d933387c84be3758fd64575f911a39fc7a1c7d..bbf956bcb1d340e8863f145cf38527bb63faa749 100644 (file)
@@ -23,7 +23,7 @@ Variables:
   from_email: the email part of the From: line, or nil if empty
 Return value:
   A string (multi-line ok) containing the text of the signature, or nil to
-  use the default signature.
+  use the default signature, or :none for no signature.
 EOS
 
   HookManager.register "before-edit", <<EOS
@@ -122,7 +122,7 @@ EOS
     @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
     @file.puts format_headers(@header - NON_EDITABLE_HEADERS).first
     @file.puts
-    @file.puts @body
+    @file.puts @body.join("\n")
     @file.close
 
     editor = $config[:editor] || ENV['EDITOR'] || "/usr/bin/vi"
@@ -145,6 +145,8 @@ EOS
     !edited? || BufferManager.ask_yes_or_no("Discard message?")
   end
 
+  def unsaved?; edited? end
+
   def attach_file
     fn = BufferManager.ask_for_filename :attachment, "File name (enter for browser): "
     return unless fn
@@ -213,7 +215,7 @@ protected
   def parse_file fn
     File.open(fn) do |f|
       header = MBox::read_header f
-      body = f.readlines
+      body = f.readlines.map { |l| l.chomp }
 
       header.delete_if { |k, v| NON_EDITABLE_HEADERS.member? k }
       header.each { |k, v| header[k] = parse_header k, v }
@@ -304,9 +306,10 @@ protected
   def build_message date
     m = RMail::Message.new
     m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
-    m.body = @body.join
-    m.body = m.body
+    m.body = @body.join("\n")
     m.body += sig_lines.join("\n") unless $config[:edit_signature]
+    ## body must end in a newline or GPG signatures will be WRONG!
+    m.body += "\n" unless m.body =~ /\n\Z/
 
     ## there are attachments, so wrap body in an attachment of its own
     unless @attachments.empty?
@@ -320,8 +323,8 @@ protected
 
     ## do whatever crypto transformation is necessary
     if @crypto_selector && @crypto_selector.val != :none
-      from_email = PersonManager.person_for(@header["From"]).email
-      to_email = (@header["To"] + @header["Cc"] + @header["Bcc"]).map { |p| PersonManager.person_for(p).email }
+      from_email = Person.from_address(@header["From"]).email
+      to_email = [@header["To"], @header["Cc"], @header["Bcc"]].flatten.compact.map { |p| Person.from_address(p).email }
 
       m = CryptoManager.send @crypto_selector.val, from_email, to_email, m
     end
@@ -364,7 +367,7 @@ EOS
     end
 
     f.puts
-    f.puts sanitize_body(@body.join)
+    f.puts sanitize_body(@body.join("\n"))
     f.puts sig_lines if full unless $config[:edit_signature]
   end  
 
@@ -377,12 +380,11 @@ protected
        if text
          @header[field] = parse_header field, text
          update
-         field
        end
     else
-      default =
-        case field
+      default = case field
         when *MULTI_HEADERS
+          @header[field] ||= []
           @header[field].join(", ")
         else
           @header[field]
@@ -390,10 +392,9 @@ protected
 
       contacts = BufferManager.ask_for_contacts :people, "#{field}: ", default
       if contacts
-        text = contacts.map { |s| s.longname }.join(", ")
+        text = contacts.map { |s| s.full_address }.join(", ")
         @header[field] = parse_header field, text
         update
-        field
       end
     end
   end
@@ -409,15 +410,17 @@ private
   end
 
   def top_posting?
-    @body.join =~ /(\S+)\s*Excerpts from.*\n(>.*\n)+\s*\Z/
+    @body.join("\n") =~ /(\S+)\s*Excerpts from.*\n(>.*\n)+\s*\Z/
   end
 
   def sig_lines
-    p = PersonManager.person_for(@header["From"])
+    p = Person.from_address(@header["From"])
     from_email = p && p.email
 
     ## first run the hook
     hook_sig = HookManager.run "signature", :header => @header, :from_email => from_email
+
+    return [] if hook_sig == :none
     return ["", "-- "] + hook_sig.split("\n") if hook_sig
 
     ## no hook, do default signature generation based on config.yaml