]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/reply-mode.rb
fix from email bug
[sup] / lib / sup / modes / reply-mode.rb
index 5488a0d5db352a7022040bd7be1e2b7db814f727..180ae4e7f8c70e6330594ffff62765426072eead 100644 (file)
@@ -1,9 +1,10 @@
 module Redwood
 
 class ReplyMode < EditMessageMode
-  REPLY_TYPES = [:sender, :list, :all, :user]
+  REPLY_TYPES = [:sender, :recipient, :list, :all, :user]
   TYPE_DESCRIPTIONS = {
     :sender => "Reply to sender",
+    :recipient => "Reply to recipient",
     :all => "Reply to all",
     :list => "Reply to mailing list",
     :user => "Customized reply"
@@ -15,123 +16,138 @@ class ReplyMode < EditMessageMode
   end
 
   def initialize message
-    super 2, :twiddles => false
     @m = message
 
-    from =
-      if @m.recipient_email
-        AccountManager.account_for(@m.recipient_email)
-      else
-        (@m.to + @m.cc).find { |p| AccountManager.is_account? p }
-      end || AccountManager.default_account
+    ## it's important to put this early because it forces a read of
+    ## the full headers (most importantly the list-post header, if
+    ## any)
+    body = reply_body_lines message
 
-    from_email = @m.recipient_email || from.email
+    from_email, from_acct =
+      if @m.recipient_email && (a = AccountManager.account_for(@m.recipient_email))
+        [@m.recipient_email, a]
+      elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
+        [nil, b]
+      else
+        c = AccountManager.default_account
+        [nil, c]
+      end
 
     ## ignore reply-to for list messages because it's typically set to
-    ## the list address anyways
+    ## the list address, which we explicitly treat with :list
     to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
-    cc = (@m.to + @m.cc - [from, to]).uniq
+    cc = (@m.to + @m.cc - [from_acct, to]).uniq
 
     @headers = {}
-    @headers[:sender] = {
-      "From" => "#{from.name} <#{from_email}>",
-      "To" => [to.full_address],
-    }
 
-    @headers[:user] = {
-      "From" => "#{from.name} <#{from_email}>",
-    }
+    ## if there's no cc, then the sender is the person you want to reply
+    ## to. if it's a list message, then the list address is. otherwise,
+    ## the cc contains a recipient.
+    useful_recipient = !(cc.empty? || @m.is_list_message?)
+    
+    @headers[:recipient] = {
+      "To" => cc.map { |p| p.full_address },
+    } if useful_recipient
+
+    ## typically we don't want to have a reply-to-sender option if the sender
+    ## is a user account. however, if the cc is empty, it's a message to
+    ## ourselves, so for the lack of any other options, we'll add it.
+    @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
+
+    @headers[:user] = {}
 
     @headers[:all] = {
-      "From" => "#{from.name} <#{from_email}>",
       "To" => [to.full_address],
-      "Cc" => cc.map { |p| p.full_address },
+      "Cc" => cc.select { |p| !AccountManager.is_account?(p) }.map { |p| p.full_address },
     } unless cc.empty?
 
     @headers[:list] = {
-      "From" => "#{from.name} <#{from_email}>",
       "To" => [@m.list_address.full_address],
     } if @m.is_list_message?
 
     refs = gen_references
-    mid = gen_message_id
+
     @headers.each do |k, v|
       @headers[k] = {
-               "To" => "",
-               "Cc" => "",
-               "Bcc" => "",
+               "From" => from_acct.full_address(from_email),
+               "To" => [],
+               "Cc" => [],
+               "Bcc" => [],
                "In-Reply-To" => "<#{@m.id}>",
                "Subject" => Message.reify_subj(@m.subj),
-               "Message-Id" => mid,
                "References" => refs,
              }.merge v
     end
 
     @type_labels = REPLY_TYPES.select { |t| @headers.member?(t) }
-    @selected_type = @m.is_list_message? ? :list : :sender
+    @selected_type = 
+      if @m.is_list_message?
+        :list
+      elsif @headers.member? :sender
+        :sender
+      else
+        :recipient
+      end
 
-    @body = reply_body_lines(message) + sig_lines
-    regen_text
+    super :header => @headers[@selected_type], :body => body,
+          :skip_top_rows => 2, :twiddles => false
   end
 
-  def lines; @text.length + 2; end
+  def lines; super + 2; end
   def [] i
     case i
     when 0
-      lame = []
-      @type_labels.each do |t|
-        lame << [(t == @selected_type ? :none_highlight : :none), 
-          "#{TYPE_DESCRIPTIONS[t]}"]
-        lame << [:none, "  "]
-      end
-      lame + [[:none, ""]]
+      @type_labels.inject([]) do |array, t|
+        array + [[(t == @selected_type ? :none_highlight : :none), 
+          "#{TYPE_DESCRIPTIONS[t]}"], [:none, "  "]]
+      end + [[:none, ""]]
     when 1
       ""
     else
-      @text[i - 2]
+      super(i - 2)
     end
   end
 
 protected
 
-  def body; @body; end
-  def header; @headers[@selected_type]; end
-
   def reply_body_lines m
-    lines = ["Excerpts from #{@m.from.name}'s message of #{@m.date}:"] + 
-      m.basic_body_lines.map { |l| "> #{l}" }
-    lines.pop while lines.last !~ /[:alpha:]/
+    lines = ["Excerpts from #{@m.from.name}'s message of #{@m.date}:"] + m.quotable_body_lines.map { |l| "> #{l}" }
+    lines.pop while lines.last =~ /^\s*$/
     lines
   end
 
   def handle_new_text new_header, new_body
-    @body = new_body
-
-    if new_header.size != header.size ||
-        header.any? { |k, v| new_header[k] != v }
-      #raise "nhs: #{new_header.size} hs: #{header.size} new: #{new_header.inspect} old: #{header.inspect}"
+    old_header = @headers[@selected_type]
+    if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
       @selected_type = :user
-      @headers[:user] = new_header
+      self.header = @headers[:user] = new_header
+      update
     end
   end
 
-  def regen_text
-    @text = header_lines(@headers[@selected_type] - NON_EDITABLE_HEADERS) + [""] + @body
-  end
-
   def gen_references
     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
   end
+
+  def edit_field field
+    edited_field = super
+    if edited_field && edited_field != "Subject"
+      @selected_type = :user
+      update
+    end
+  end
   
   def move_cursor_left
     i = @type_labels.index @selected_type
     @selected_type = @type_labels[(i - 1) % @type_labels.length]
+    self.header = @headers[@selected_type]
     update
   end
 
   def move_cursor_right
     i = @type_labels.index @selected_type
     @selected_type = @type_labels[(i + 1) % @type_labels.length]
+    self.header = @headers[@selected_type]
     update
   end
 end