]> git.cworth.org Git - sup/blob - lib/sup/modes/reply-mode.rb
reply all keybindings
[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, type_arg=nil
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       info "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 had an envelope-to header, try and use
68     ## it, and look up the corresponding name form the list of accounts.
69     ##
70     ## this is for the case where mail is received from a mailing lists (so the
71     ## To: is the list id itself). if the user subscribes via a particular
72     ## alias, we want to use that alias in the reply.
73     elsif @m.recipient_email && (a = AccountManager.account_for(@m.recipient_email))
74       Person.new a.name, @m.recipient_email
75     ## otherwise, try and find an account somewhere in the list of to's
76     ## and cc's.
77     elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
78       b
79     ## if all else fails, use the default
80     else
81       AccountManager.default_account
82     end
83
84     ## now, determine to: and cc: addressess. we ignore reply-to for list
85     ## messages because it's typically set to the list address, which we
86     ## explicitly treat with reply type :list
87     to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
88
89     ## next, cc:
90     cc = (@m.to + @m.cc - [from, to]).uniq
91
92     ## one potential reply type is "reply to recipient". this only happens
93     ## in certain cases:
94     ## if there's no cc, then the sender is the person you want to reply
95     ## to. if it's a list message, then the list address is. otherwise,
96     ## the cc contains a recipient.
97     useful_recipient = !(cc.empty? || @m.is_list_message?)
98     
99     @headers = {}
100     @headers[:recipient] = {
101       "To" => cc.map { |p| p.full_address },
102     } if useful_recipient
103
104     ## typically we don't want to have a reply-to-sender option if the sender
105     ## is a user account. however, if the cc is empty, it's a message to
106     ## ourselves, so for the lack of any other options, we'll add it.
107     @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
108
109     @headers[:user] = {}
110
111     not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
112     @headers[:all] = {
113       "To" => [to.full_address],
114       "Cc" => not_me_ccs.map { |p| p.full_address },
115     } unless not_me_ccs.empty?
116
117     @headers[:list] = {
118       "To" => [@m.list_address.full_address],
119     } if @m.is_list_message?
120
121     refs = gen_references
122
123     @headers.each do |k, v|
124       @headers[k] = {
125                "From" => from.full_address,
126                "To" => [],
127                "Cc" => [],
128                "Bcc" => [],
129                "In-reply-to" => "<#{@m.id}>",
130                "Subject" => Message.reify_subj(@m.subj),
131                "References" => refs,
132              }.merge v
133     end
134
135     types = REPLY_TYPES.select { |t| @headers.member?(t) }
136     @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
137
138     hook_reply = HookManager.run "reply-to", :modes => types
139
140     @type_selector.set_to(
141       if types.include? type_arg
142         type_arg
143       elsif types.include? hook_reply
144         hook_reply
145       elsif @m.is_list_message?
146         :list
147       elsif @headers.member? :sender
148         :sender
149       else
150         :recipient
151       end)
152
153     @headers.each do |k, v|
154       HookManager.run "before-edit", :header => v, :body => body
155     end
156
157     super :header => @headers[@type_selector.val], :body => body, :twiddles => false
158     add_selector @type_selector
159   end
160
161 protected
162
163   def move_cursor_right
164     super
165     if @headers[@type_selector.val] != self.header
166       self.header = @headers[@type_selector.val]
167       update
168     end
169   end
170
171   def move_cursor_left
172     super
173     if @headers[@type_selector.val] != self.header
174       self.header = @headers[@type_selector.val]
175       update
176     end
177   end
178
179   def reply_body_lines m
180     attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
181     lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
182     lines.pop while lines.last =~ /^\s*$/
183     lines
184   end
185
186   def default_attribution m
187     "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
188   end
189
190   def handle_new_text new_header, new_body
191     old_header = @headers[@type_selector.val]
192     if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
193       @type_selector.set_to :user
194       self.header = @headers[:user] = new_header
195       update
196     end
197   end
198
199   def gen_references
200     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
201   end
202
203   def edit_field field
204     edited_field = super
205     if edited_field && edited_field != "Subject"
206       @type_selector.set_to :user
207       update
208     end
209   end
210 end
211
212 end