3 class ReplyMode < EditMessageMode
4 REPLY_TYPES = [:sender, :recipient, :list, :all, :user]
7 :recipient => "Recipient",
9 :list => "Mailing list",
13 def initialize message
16 ## it's important to put this early because it forces a read of
17 ## the full headers (most importantly the list-post header, if
19 body = reply_body_lines message
21 ## first, determine the address at which we received this email. this will
22 ## become our From: address in the reply.
24 if @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
25 PersonManager.person_for(@m.recipient_email)
26 elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
29 AccountManager.default_account
32 ## now, determine to: and cc: addressess. we ignore reply-to for list
33 ## messages because it's typically set to the list address, which we
34 ## explicitly treat with reply type :list
35 to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
38 cc = (@m.to + @m.cc - [from, to]).uniq
40 ## one potential reply type is "reply to recipient". this only happens
42 ## if there's no cc, then the sender is the person you want to reply
43 ## to. if it's a list message, then the list address is. otherwise,
44 ## the cc contains a recipient.
45 useful_recipient = !(cc.empty? || @m.is_list_message?)
48 @headers[:recipient] = {
49 "To" => cc.map { |p| p.full_address },
52 ## typically we don't want to have a reply-to-sender option if the sender
53 ## is a user account. however, if the cc is empty, it's a message to
54 ## ourselves, so for the lack of any other options, we'll add it.
55 @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
60 "To" => [to.full_address],
61 "Cc" => cc.select { |p| !AccountManager.is_account?(p) }.map { |p| p.full_address },
65 "To" => [@m.list_address.full_address],
66 } if @m.is_list_message?
70 @headers.each do |k, v|
72 "From" => from.full_address,
76 "In-Reply-To" => "<#{@m.id}>",
77 "Subject" => Message.reify_subj(@m.subj),
82 types = REPLY_TYPES.select { |t| @headers.member?(t) }
83 @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
85 @type_selector.set_to(
86 if @m.is_list_message?
88 elsif @headers.member? :sender
94 super :header => @headers[@type_selector.val], :body => body, :twiddles => false
95 add_selector @type_selector
100 def move_cursor_right
102 if @headers[@type_selector.val] != self.header
103 self.header = @headers[@type_selector.val]
110 if @headers[@type_selector.val] != self.header
111 self.header = @headers[@type_selector.val]
116 def reply_body_lines m
117 lines = ["Excerpts from #{@m.from.name}'s message of #{@m.date}:"] + m.quotable_body_lines.map { |l| "> #{l}" }
118 lines.pop while lines.last =~ /^\s*$/
122 def handle_new_text new_header, new_body
123 old_header = @headers[@type_selector.val]
124 if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
125 @type_selector.set_to :user
126 self.header = @headers[:user] = new_header
132 (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
137 if edited_field && edited_field != "Subject"
138 @type_selector.set_to :user