]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/edit-message-mode.rb
distinguish search types, and blank searches now do nothing
[sup] / lib / sup / modes / edit-message-mode.rb
index 2c0314070c4a1bd43b9d369c85fdb6b438f7541b..52447e8345e979b5a44bdc89f492efe1e31a14d1 100644 (file)
@@ -12,13 +12,28 @@ class EditMessageMode < LineCursorMode
   MULTI_HEADERS = %w(To Cc Bcc)
   NON_EDITABLE_HEADERS = %w(Message-Id Date)
 
+  HookManager.register "signature", <<EOS
+Generates a signature for a message.
+Variables:
+      header: an object that supports string-to-string hashtable-style access
+              to the raw headers for the message. E.g., header["From"],
+              header["To"], etc.
+  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.
+EOS
+
   attr_reader :status
   attr_accessor :body, :header
   bool_reader :edited
 
   register_keymap do |k|
     k.add :send_message, "Send message", 'y'
-    k.add :edit_field, "Edit field", 'e'
+    k.add :edit_message_or_field, "Edit selected field", 'e'
+    k.add :edit_to, "Edit To:", 't'
+    k.add :edit_cc, "Edit Cc:", 'c'
+    k.add :edit_subject, "Edit Subject", 's'
     k.add :edit_message, "Edit message", :enter
     k.add :save_as_draft, "Save as draft", 'P'
     k.add :attach_file, "Attach a file", 'a'
@@ -47,33 +62,18 @@ class EditMessageMode < LineCursorMode
   ## a hook
   def handle_new_text header, body; end
 
-  def edit_field
+  def edit_message_or_field
     if (curpos - @skip_top_rows) >= @header_lines.length
       edit_message
     else
-      case(field = @header_lines[curpos - @skip_top_rows])
-      when "Subject"
-        text = BufferManager.ask :subject, "Subject: ", @header[field]
-        @header[field] = parse_header field, text if text
-      else
-        default =
-          case field
-          when *MULTI_HEADERS
-            @header[field].join(", ")
-          else
-            @header[field]
-          end
-
-        contacts = BufferManager.ask_for_contacts :people, "#{field}: ", default
-        if contacts
-          text = contacts.map { |s| s.longname }.join(", ")
-          @header[field] = parse_header field, text
-        end
-      end
-      update
+      edit_field @header_lines[curpos - @skip_top_rows]
     end
   end
 
+  def edit_to; edit_field "To" end
+  def edit_cc; edit_field "Cc" end
+  def edit_subject; edit_field "Subject" end
+
   def edit_message
     @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
     @file.puts format_headers(@header - NON_EDITABLE_HEADERS).first
@@ -87,12 +87,14 @@ class EditMessageMode < LineCursorMode
     BufferManager.shell_out "#{editor} #{@file.path}"
     @edited = true if File.mtime(@file.path) > mtime
 
-    BufferManager.kill_buffer self.buffer unless @edited
+    return @edited unless @edited
 
     header, @body = parse_file @file.path
     @header = header - NON_EDITABLE_HEADERS
     handle_new_text @header, @body
     update
+
+    @edited
   end
 
   def killable?
@@ -100,8 +102,9 @@ class EditMessageMode < LineCursorMode
   end
 
   def attach_file
-    fn = BufferManager.ask_for_filenames :attachment, "File name (enter for browser): "
-    fn.each { |f| @attachments << Pathname.new(f) }
+    fn = BufferManager.ask_for_filename :attachment, "File name (enter for browser): "
+    return unless fn
+    @attachments << Pathname.new(fn)
     update
   end
 
@@ -124,6 +127,8 @@ protected
     header, @header_lines = format_headers(@header - NON_EDITABLE_HEADERS) + [""]
     @text = header + [""] + @body
     @text += sig_lines unless $config[:edit_signature]
+    
+    @attachment_lines_offset = 0
 
     unless @attachments.empty?
       @text += [""]
@@ -187,9 +192,9 @@ protected
   end
 
   def send_message
-    return if !edited? && !BufferManager.ask_yes_or_no("Message unedited. Really send?")
-    return if $config[:confirm_no_attachments] && mentions_attachments? && @attachments.size == 0 && !BufferManager.ask_yes_or_no("You haven't added any attachments. Really send?")#" stupid ruby-mode
-    return if $config[:confirm_top_posting] && top_posting? && !BufferManager.ask_yes_or_no("You're top posting. That makes you a bad person. Really send?") #" stupid ruby-mode
+    return false if !edited? && !BufferManager.ask_yes_or_no("Message unedited. Really send?")
+    return false if $config[:confirm_no_attachments] && mentions_attachments? && @attachments.size == 0 && !BufferManager.ask_yes_or_no("You haven't added any attachments. Really send?")#" stupid ruby-mode
+    return false if $config[:confirm_top_posting] && top_posting? && !BufferManager.ask_yes_or_no("You're top-posting. That makes you a bad person. Really send?") #" stupid ruby-mode
 
     date = Time.now
     from_email = 
@@ -203,14 +208,16 @@ protected
     BufferManager.flash "Sending..."
 
     begin
-      IO.popen(acct.sendmail, "w") { |p| write_full_message_to p, date }
+      IO.popen(acct.sendmail, "w") { |p| write_full_message_to p, date, false }
       raise SendmailCommandFailed, "Couldn't execute #{acct.sendmail}" unless $? == 0
-      SentManager.write_sent_message(date, from_email) { |f| write_full_message_to f, date }
+      SentManager.write_sent_message(date, from_email) { |f| write_full_message_to f, date, true }
       BufferManager.kill_buffer buffer
       BufferManager.flash "Message sent!"
+      true
     rescue SystemCallError, SendmailCommandFailed => e
       Redwood::log "Problem sending mail: #{e.message}"
       BufferManager.flash "Problem sending mail: #{e.message}"
+      false
     end
   end
 
@@ -220,7 +227,7 @@ protected
     BufferManager.flash "Saved for later editing."
   end
 
-  def write_full_message_to f, date=Time.now
+  def write_full_message_to f, date=Time.now, escape=false
     m = RMail::Message.new
     @header.each do |k, v|
       next if v.nil? || v.empty?
@@ -238,21 +245,26 @@ protected
     m.header["User-Agent"] = "Sup/#{Redwood::VERSION}"
 
     if @attachments.empty?
-      m.header["Content-Disposition"] = "inline"
       m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
       m.body = @body.join
+      m.body = sanitize_body m.body if escape
       m.body += sig_lines.join("\n") unless $config[:edit_signature]
     else
       body_m = RMail::Message.new
       body_m.body = @body.join
+      body_m.body = sanitize_body body_m.body if escape
       body_m.body += sig_lines.join("\n") unless $config[:edit_signature]
+      body_m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
+      body_m.header["Content-Disposition"] = "inline"
       
       m.add_part body_m
-      @attachments.each { |fn| m.add_attachment fn.to_s }
+      @attachments.each { |fn| m.add_file_attachment fn.to_s }
     end
     f.puts m.to_s
   end
 
+  ## TODO: remove this. redundant with write_full_message_to.
+  ##
   ## this is going to change soon: draft messages (currently written
   ## with full=false) will be output as yaml.
   def write_message f, full=true, date=Time.now
@@ -272,23 +284,65 @@ EOS
     end
 
     f.puts
-    f.puts @body.map { |l| l =~ /^From / ? ">#{l}" : l }
+    f.puts sanitize_body(@body.join)
     f.puts sig_lines if full unless $config[:edit_signature]
   end  
 
+protected
+
+  def edit_field field
+    case field
+    when "Subject"
+      text = BufferManager.ask :subject, "Subject: ", @header[field]
+       if text
+         @header[field] = parse_header field, text
+         update
+         field
+       end
+    else
+      default =
+        case field
+        when *MULTI_HEADERS
+          @header[field].join(", ")
+        else
+          @header[field]
+        end
+
+      contacts = BufferManager.ask_for_contacts :people, "#{field}: ", default
+      if contacts
+        text = contacts.map { |s| s.longname }.join(", ")
+        @header[field] = parse_header field, text
+        update
+        field
+      end
+    end
+  end
+
 private
 
+  def sanitize_body body
+    body.gsub(/^From /, ">From ")
+  end
+
   def mentions_attachments?
     @body.any? { |l| l =~ /^[^>]/ && l =~ /\battach(ment|ed|ing|)\b/i }
   end
 
   def top_posting?
-    @body.join =~ /(\S+)\s*Excerpts from /
+    @body.join =~ /(\S+)\s*Excerpts from.*\n(>.*\n)+\s*\Z/
   end
 
   def sig_lines
-    p = PersonManager.person_for @header["From"]
-    sigfn = (AccountManager.account_for(p.email) || 
+    p = PersonManager.person_for(@header["From"])
+    from_email = p && p.email
+
+    ## first run the hook
+    hook_sig = HookManager.run "signature", :header => @header, :from_email => from_email
+    return ["", "-- "] + hook_sig.split("\n") if hook_sig
+
+    ## no hook, do default signature generation based on config.yaml
+    return [] unless from_email
+    sigfn = (AccountManager.account_for(from_email) || 
              AccountManager.default_account).signature
 
     if sigfn && File.exists?(sigfn)