]> git.notmuchmail.org Git - sup/blob - lib/sup/modes/reply-mode.rb
Merge branch 'dont-canonicalize-email-addresses' into next
[sup] / lib / sup / modes / reply-mode.rb
1 module Redwood
2
3 class ReplyMode < EditMessageMode
4   REPLY_TYPES = [:sender, :recipient, :list, :all, :user]
5   TYPE_DESCRIPTIONS = {
6     :sender => "Sender",
7     :recipient => "Recipient",
8     :all => "All",
9     :list => "Mailing list",
10     :user => "Customized"
11   }
12
13   HookManager.register "attribution", <<EOS
14 Generates an attribution ("Excerpts from Joe Bloggs's message of Fri Jan 11 09:54:32 -0500 2008:").
15 Variables:
16   message: a message object representing the message being replied to
17     (useful values include message.from.name and message.date)
18 Return value:
19   A string containing the text of the quote line (can be multi-line)
20 EOS
21
22   HookManager.register "reply-from", <<EOS
23 Selects a default address for the From: header of a new reply.
24 Variables:
25   message: a message object representing the message being replied to
26     (useful values include message.recipient_email, message.to, and message.cc)
27 Return value:
28   A Person to be used as the default for the From: header, or nil to use the
29   default behavior.
30 EOS
31
32   HookManager.register "reply-to", <<EOS
33 Set the default reply-to mode.
34 Variables:
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]
39 Return value:
40   The reply mode you desire, or nil to use the default behavior.
41 EOS
42
43   def initialize message
44     @m = message
45
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
48     ## any)
49     body = reply_body_lines message
50
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
54
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
57     ## doing.
58     if hook_reply_from && !(hook_reply_from.is_a? Person)
59       Redwood::log "reply-from returned non-Person, using default from."
60       hook_reply_from = nil
61     end
62
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
66       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))
70       a
71     ## otherwise, try and find an account somewhere in the list of to's
72     ## and cc's.
73     elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
74       b
75     ## if all else fails, use the default
76     else
77       AccountManager.default_account
78     end
79
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)
84
85     ## next, cc:
86     cc = (@m.to + @m.cc - [from, to]).uniq
87
88     ## one potential reply type is "reply to recipient". this only happens
89     ## in certain cases:
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?)
94     
95     @headers = {}
96     @headers[:recipient] = {
97       "To" => cc.map { |p| p.full_address },
98     } if useful_recipient
99
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
104
105     @headers[:user] = {}
106
107     not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
108     @headers[:all] = {
109       "To" => [to.full_address],
110       "Cc" => not_me_ccs.map { |p| p.full_address },
111     } unless not_me_ccs.empty?
112
113     @headers[:list] = {
114       "To" => [@m.list_address.full_address],
115     } if @m.is_list_message?
116
117     refs = gen_references
118
119     @headers.each do |k, v|
120       @headers[k] = {
121                "From" => from.full_address,
122                "To" => [],
123                "Cc" => [],
124                "Bcc" => [],
125                "In-Reply-To" => "<#{@m.id}>",
126                "Subject" => Message.reify_subj(@m.subj),
127                "References" => refs,
128              }.merge v
129     end
130
131     types = REPLY_TYPES.select { |t| @headers.member?(t) }
132     @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
133
134     hook_reply = HookManager.run "reply-to", :modes => types
135
136     @type_selector.set_to(
137       if types.include? hook_reply
138         hook_reply
139       elsif @m.is_list_message?
140         :list
141       elsif @headers.member? :sender
142         :sender
143       else
144         :recipient
145       end)
146
147     @headers.each do |k, v|
148       HookManager.run "before-edit", :header => v, :body => body
149     end
150
151     super :header => @headers[@type_selector.val], :body => body, :twiddles => false
152     add_selector @type_selector
153   end
154
155 protected
156
157   def move_cursor_right
158     super
159     if @headers[@type_selector.val] != self.header
160       self.header = @headers[@type_selector.val]
161       update
162     end
163   end
164
165   def move_cursor_left
166     super
167     if @headers[@type_selector.val] != self.header
168       self.header = @headers[@type_selector.val]
169       update
170     end
171   end
172
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*$/
177     lines
178   end
179
180   def default_attribution m
181     "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
182   end
183
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
189       update
190     end
191   end
192
193   def gen_references
194     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
195   end
196
197   def edit_field field
198     edited_field = super
199     if edited_field && edited_field != "Subject"
200       @type_selector.set_to :user
201       update
202     end
203   end
204 end
205
206 end