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 doing.
57 if hook_reply_from && !(hook_reply_from.is_a? Person)
58 Redwood::log "reply-from returned non-Person, using default from."
65 elsif @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
66 PersonManager.person_for(@m.recipient_email)
67 elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
70 AccountManager.default_account
73 ## now, determine to: and cc: addressess. we ignore reply-to for list
74 ## messages because it's typically set to the list address, which we
75 ## explicitly treat with reply type :list
76 to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
79 cc = (@m.to + @m.cc - [from, to]).uniq
81 ## one potential reply type is "reply to recipient". this only happens
83 ## if there's no cc, then the sender is the person you want to reply
84 ## to. if it's a list message, then the list address is. otherwise,
85 ## the cc contains a recipient.
86 useful_recipient = !(cc.empty? || @m.is_list_message?)
89 @headers[:recipient] = {
90 "To" => cc.map { |p| p.full_address },
93 ## typically we don't want to have a reply-to-sender option if the sender
94 ## is a user account. however, if the cc is empty, it's a message to
95 ## ourselves, so for the lack of any other options, we'll add it.
96 @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
100 not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
102 "To" => [to.full_address],
103 "Cc" => not_me_ccs.map { |p| p.full_address },
104 } unless not_me_ccs.empty?
107 "To" => [@m.list_address.full_address],
108 } if @m.is_list_message?
110 refs = gen_references
112 @headers.each do |k, v|
114 "From" => from.full_address,
118 "In-Reply-To" => "<#{@m.id}>",
119 "Subject" => Message.reify_subj(@m.subj),
120 "References" => refs,
124 types = REPLY_TYPES.select { |t| @headers.member?(t) }
125 @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
127 hook_reply = HookManager.run "reply-to", :modes => types
129 @type_selector.set_to(
130 if types.include? hook_reply
132 elsif @m.is_list_message?
134 elsif @headers.member? :sender
140 @headers.each do |k, v|
141 HookManager.run "before-edit", :header => v, :body => body
144 super :header => @headers[@type_selector.val], :body => body, :twiddles => false
145 add_selector @type_selector
150 def move_cursor_right
152 if @headers[@type_selector.val] != self.header
153 self.header = @headers[@type_selector.val]
160 if @headers[@type_selector.val] != self.header
161 self.header = @headers[@type_selector.val]
166 def reply_body_lines m
167 attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
168 lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
169 lines.pop while lines.last =~ /^\s*$/
173 def default_attribution m
174 "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
177 def handle_new_text new_header, new_body
178 old_header = @headers[@type_selector.val]
179 if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
180 @type_selector.set_to :user
181 self.header = @headers[:user] = new_header
187 (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
192 if edited_field && edited_field != "Subject"
193 @type_selector.set_to :user