3 class ReplyMode < EditMessageMode
4 REPLY_TYPES = [:sender, :recipient, :list, :all, :user]
7 :recipient => "Recipient",
9 :list => "Mailing list",
13 HookManager.register "attribution", <<EOS
14 Generates an attribution ("Excerpts from Joe Bloggs's message of Fri Jan 11 09:54:32 -0500 2008:").
16 message: a message object representing the message being replied to
17 (useful values include message.from.name and message.date)
19 A string containing the text of the quote line (can be multi-line)
22 def initialize message
25 ## it's important to put this early because it forces a read of
26 ## the full headers (most importantly the list-post header, if
28 body = reply_body_lines message
30 ## first, determine the address at which we received this email. this will
31 ## become our From: address in the reply.
33 if @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
34 PersonManager.person_for(@m.recipient_email)
35 elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
38 AccountManager.default_account
41 ## now, determine to: and cc: addressess. we ignore reply-to for list
42 ## messages because it's typically set to the list address, which we
43 ## explicitly treat with reply type :list
44 to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
47 cc = (@m.to + @m.cc - [from, to]).uniq
49 ## one potential reply type is "reply to recipient". this only happens
51 ## if there's no cc, then the sender is the person you want to reply
52 ## to. if it's a list message, then the list address is. otherwise,
53 ## the cc contains a recipient.
54 useful_recipient = !(cc.empty? || @m.is_list_message?)
57 @headers[:recipient] = {
58 "To" => cc.map { |p| p.full_address },
61 ## typically we don't want to have a reply-to-sender option if the sender
62 ## is a user account. however, if the cc is empty, it's a message to
63 ## ourselves, so for the lack of any other options, we'll add it.
64 @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
68 not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
70 "To" => [to.full_address],
71 "Cc" => not_me_ccs.map { |p| p.full_address },
72 } unless not_me_ccs.empty?
75 "To" => [@m.list_address.full_address],
76 } if @m.is_list_message?
80 @headers.each do |k, v|
82 "From" => from.full_address,
86 "In-Reply-To" => "<#{@m.id}>",
87 "Subject" => Message.reify_subj(@m.subj),
92 types = REPLY_TYPES.select { |t| @headers.member?(t) }
93 @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
95 @type_selector.set_to(
96 if @m.is_list_message?
98 elsif @headers.member? :sender
104 @headers.each do |k, v|
105 HookManager.run "before-edit", :header => v, :body => body
108 super :header => @headers[@type_selector.val], :body => body, :twiddles => false
109 add_selector @type_selector
114 def move_cursor_right
116 if @headers[@type_selector.val] != self.header
117 self.header = @headers[@type_selector.val]
124 if @headers[@type_selector.val] != self.header
125 self.header = @headers[@type_selector.val]
130 def reply_body_lines m
131 attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
132 lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
133 lines.pop while lines.last =~ /^\s*$/
137 def default_attribution m
138 "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
141 def handle_new_text new_header, new_body
142 old_header = @headers[@type_selector.val]
143 if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
144 @type_selector.set_to :user
145 self.header = @headers[:user] = new_header
151 (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
156 if edited_field && edited_field != "Subject"
157 @type_selector.set_to :user