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-from", <<EOS
23 Selects a default address for the From: header of a new reply.
25 message: a message object representing the message being replied to
26 (useful values include message.recipient_email, message.to, and message.cc)
28 A Person to be used as the default for the From: header, or nil to use the
32 HookManager.register "reply-to", <<EOS
33 Set the default reply-to mode.
35 modes: array of valid modes to choose from, which will be a subset of
36 [:#{REPLY_TYPES * ', :'}]
37 The default behavior is equivalent to
38 ([:list, :sender, :recipent] & modes)[0]
40 The reply mode you desire, or nil to use the default behavior.
43 def initialize message
46 ## it's important to put this early because it forces a read of
47 ## the full headers (most importantly the list-post header, if
49 body = reply_body_lines message
51 ## first, determine the address at which we received this email. this will
52 ## become our From: address in the reply.
53 hook_reply_from = HookManager.run "reply-from", :message => @m
55 ## sanity check that selection is a Person (or we'll fail below)
56 ## don't check that it's an Account, though; assume they know what they're
58 if hook_reply_from && !(hook_reply_from.is_a? Person)
59 Redwood::log "reply-from returned non-Person, using default from."
63 ## determine the from address of a reply.
64 ## if we have a value from a hook, use it.
65 from = if hook_reply_from
67 ## otherwise, if the original email was addressed to a particular
68 ## address via an envelope-to or whatever, try and use that one.
69 elsif @m.recipient_email && (a = AccountManager.account_for(@m.recipient_email))
71 ## otherwise, try and find an account somewhere in the list of to's
73 elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
75 ## if all else fails, use the default
77 AccountManager.default_account
80 ## now, determine to: and cc: addressess. we ignore reply-to for list
81 ## messages because it's typically set to the list address, which we
82 ## explicitly treat with reply type :list
83 to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
86 cc = (@m.to + @m.cc - [from, to]).uniq
88 ## one potential reply type is "reply to recipient". this only happens
90 ## if there's no cc, then the sender is the person you want to reply
91 ## to. if it's a list message, then the list address is. otherwise,
92 ## the cc contains a recipient.
93 useful_recipient = !(cc.empty? || @m.is_list_message?)
96 @headers[:recipient] = {
97 "To" => cc.map { |p| p.full_address },
100 ## typically we don't want to have a reply-to-sender option if the sender
101 ## is a user account. however, if the cc is empty, it's a message to
102 ## ourselves, so for the lack of any other options, we'll add it.
103 @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
107 not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
109 "To" => [to.full_address],
110 "Cc" => not_me_ccs.map { |p| p.full_address },
111 } unless not_me_ccs.empty?
114 "To" => [@m.list_address.full_address],
115 } if @m.is_list_message?
117 refs = gen_references
119 @headers.each do |k, v|
121 "From" => from.full_address,
125 "In-Reply-To" => "<#{@m.id}>",
126 "Subject" => Message.reify_subj(@m.subj),
127 "References" => refs,
131 types = REPLY_TYPES.select { |t| @headers.member?(t) }
132 @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
134 hook_reply = HookManager.run "reply-to", :modes => types
136 @type_selector.set_to(
137 if types.include? hook_reply
139 elsif @m.is_list_message?
141 elsif @headers.member? :sender
147 @headers.each do |k, v|
148 HookManager.run "before-edit", :header => v, :body => body
151 super :header => @headers[@type_selector.val], :body => body, :twiddles => false
152 add_selector @type_selector
157 def move_cursor_right
159 if @headers[@type_selector.val] != self.header
160 self.header = @headers[@type_selector.val]
167 if @headers[@type_selector.val] != self.header
168 self.header = @headers[@type_selector.val]
173 def reply_body_lines m
174 attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
175 lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
176 lines.pop while lines.last =~ /^\s*$/
180 def default_attribution m
181 "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
184 def handle_new_text new_header, new_body
185 old_header = @headers[@type_selector.val]
186 if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
187 @type_selector.set_to :user
188 self.header = @headers[:user] = new_header
194 (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
199 if edited_field && edited_field != "Subject"
200 @type_selector.set_to :user