]> git.notmuchmail.org Git - sup/blob - lib/sup/modes/reply-mode.rb
adding a reply-to hook for setting the default reply-to mode
[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-to", <<EOS
23 Set the default reply-to mode.
24 Variables:
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]
29 Return value:
30   The reply mode you desire, or nil to use the default behavior.
31 EOS
32
33   def initialize message
34     @m = message
35
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
38     ## any)
39     body = reply_body_lines message
40
41     ## first, determine the address at which we received this email. this will
42     ## become our From: address in the reply.
43     from =
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 })
47         b
48       else
49         AccountManager.default_account
50       end
51
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)
56
57     ## next, cc:
58     cc = (@m.to + @m.cc - [from, to]).uniq
59
60     ## one potential reply type is "reply to recipient". this only happens
61     ## in certain cases:
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?)
66     
67     @headers = {}
68     @headers[:recipient] = {
69       "To" => cc.map { |p| p.full_address },
70     } if useful_recipient
71
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
76
77     @headers[:user] = {}
78
79     not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
80     @headers[:all] = {
81       "To" => [to.full_address],
82       "Cc" => not_me_ccs.map { |p| p.full_address },
83     } unless not_me_ccs.empty?
84
85     @headers[:list] = {
86       "To" => [@m.list_address.full_address],
87     } if @m.is_list_message?
88
89     refs = gen_references
90
91     @headers.each do |k, v|
92       @headers[k] = {
93                "From" => from.full_address,
94                "To" => [],
95                "Cc" => [],
96                "Bcc" => [],
97                "In-Reply-To" => "<#{@m.id}>",
98                "Subject" => Message.reify_subj(@m.subj),
99                "References" => refs,
100              }.merge v
101     end
102
103     types = REPLY_TYPES.select { |t| @headers.member?(t) }
104     @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
105
106     hook_reply = HookManager.run "reply-to", :modes => types
107
108     @type_selector.set_to(
109       if types.include? hook_reply
110         hook_reply
111       elsif @m.is_list_message?
112         :list
113       elsif @headers.member? :sender
114         :sender
115       else
116         :recipient
117       end)
118
119     @headers.each do |k, v|
120       HookManager.run "before-edit", :header => v, :body => body
121     end
122
123     super :header => @headers[@type_selector.val], :body => body, :twiddles => false
124     add_selector @type_selector
125   end
126
127 protected
128
129   def move_cursor_right
130     super
131     if @headers[@type_selector.val] != self.header
132       self.header = @headers[@type_selector.val]
133       update
134     end
135   end
136
137   def move_cursor_left
138     super
139     if @headers[@type_selector.val] != self.header
140       self.header = @headers[@type_selector.val]
141       update
142     end
143   end
144
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*$/
149     lines
150   end
151
152   def default_attribution m
153     "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
154   end
155
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
161       update
162     end
163   end
164
165   def gen_references
166     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
167   end
168
169   def edit_field field
170     edited_field = super
171     if edited_field && edited_field != "Subject"
172       @type_selector.set_to :user
173       update
174     end
175   end
176 end
177
178 end