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 HookManager.register "reply-to", <<EOS
23 Set the default reply-to mode.
25 modes: array of valid modes to choose from, which will be a subset of
26 [:#{REPLY_TYPES * ', :'}]
27 The default behavior is equivalent to
28 ([:list, :sender, :recipent] & modes)[0]
30 The reply mode you desire, or nil to use the default behavior.
33 def initialize message
36 ## it's important to put this early because it forces a read of
37 ## the full headers (most importantly the list-post header, if
39 body = reply_body_lines message
41 ## first, determine the address at which we received this email. this will
42 ## become our From: address in the reply.
44 if @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
45 PersonManager.person_for(@m.recipient_email)
46 elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
49 AccountManager.default_account
52 ## now, determine to: and cc: addressess. we ignore reply-to for list
53 ## messages because it's typically set to the list address, which we
54 ## explicitly treat with reply type :list
55 to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
58 cc = (@m.to + @m.cc - [from, to]).uniq
60 ## one potential reply type is "reply to recipient". this only happens
62 ## if there's no cc, then the sender is the person you want to reply
63 ## to. if it's a list message, then the list address is. otherwise,
64 ## the cc contains a recipient.
65 useful_recipient = !(cc.empty? || @m.is_list_message?)
68 @headers[:recipient] = {
69 "To" => cc.map { |p| p.full_address },
72 ## typically we don't want to have a reply-to-sender option if the sender
73 ## is a user account. however, if the cc is empty, it's a message to
74 ## ourselves, so for the lack of any other options, we'll add it.
75 @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
79 not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
81 "To" => [to.full_address],
82 "Cc" => not_me_ccs.map { |p| p.full_address },
83 } unless not_me_ccs.empty?
86 "To" => [@m.list_address.full_address],
87 } if @m.is_list_message?
91 @headers.each do |k, v|
93 "From" => from.full_address,
97 "In-Reply-To" => "<#{@m.id}>",
98 "Subject" => Message.reify_subj(@m.subj),
103 types = REPLY_TYPES.select { |t| @headers.member?(t) }
104 @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
106 hook_reply = HookManager.run "reply-to", :modes => types
108 @type_selector.set_to(
109 if types.include? hook_reply
111 elsif @m.is_list_message?
113 elsif @headers.member? :sender
119 @headers.each do |k, v|
120 HookManager.run "before-edit", :header => v, :body => body
123 super :header => @headers[@type_selector.val], :body => body, :twiddles => false
124 add_selector @type_selector
129 def move_cursor_right
131 if @headers[@type_selector.val] != self.header
132 self.header = @headers[@type_selector.val]
139 if @headers[@type_selector.val] != self.header
140 self.header = @headers[@type_selector.val]
145 def reply_body_lines m
146 attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
147 lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
148 lines.pop while lines.last =~ /^\s*$/
152 def default_attribution m
153 "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
156 def handle_new_text new_header, new_body
157 old_header = @headers[@type_selector.val]
158 if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
159 @type_selector.set_to :user
160 self.header = @headers[:user] = new_header
166 (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
171 if edited_field && edited_field != "Subject"
172 @type_selector.set_to :user