]> git.cworth.org Git - sup/blob - lib/sup/modes/reply-mode.rb
Added UndoManager class
[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 doing.
57     if hook_reply_from && !(hook_reply_from.is_a? Person)
58         Redwood::log "reply-from returned non-Person, using default from."
59         hook_reply_from = nil
60     end
61
62     from =
63       if hook_reply_from
64         hook_reply_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 })
68         b
69       else
70         AccountManager.default_account
71       end
72
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)
77
78     ## next, cc:
79     cc = (@m.to + @m.cc - [from, to]).uniq
80
81     ## one potential reply type is "reply to recipient". this only happens
82     ## in certain cases:
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?)
87     
88     @headers = {}
89     @headers[:recipient] = {
90       "To" => cc.map { |p| p.full_address },
91     } if useful_recipient
92
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
97
98     @headers[:user] = {}
99
100     not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
101     @headers[:all] = {
102       "To" => [to.full_address],
103       "Cc" => not_me_ccs.map { |p| p.full_address },
104     } unless not_me_ccs.empty?
105
106     @headers[:list] = {
107       "To" => [@m.list_address.full_address],
108     } if @m.is_list_message?
109
110     refs = gen_references
111
112     @headers.each do |k, v|
113       @headers[k] = {
114                "From" => from.full_address,
115                "To" => [],
116                "Cc" => [],
117                "Bcc" => [],
118                "In-Reply-To" => "<#{@m.id}>",
119                "Subject" => Message.reify_subj(@m.subj),
120                "References" => refs,
121              }.merge v
122     end
123
124     types = REPLY_TYPES.select { |t| @headers.member?(t) }
125     @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
126
127     hook_reply = HookManager.run "reply-to", :modes => types
128
129     @type_selector.set_to(
130       if types.include? hook_reply
131         hook_reply
132       elsif @m.is_list_message?
133         :list
134       elsif @headers.member? :sender
135         :sender
136       else
137         :recipient
138       end)
139
140     @headers.each do |k, v|
141       HookManager.run "before-edit", :header => v, :body => body
142     end
143
144     super :header => @headers[@type_selector.val], :body => body, :twiddles => false
145     add_selector @type_selector
146   end
147
148 protected
149
150   def move_cursor_right
151     super
152     if @headers[@type_selector.val] != self.header
153       self.header = @headers[@type_selector.val]
154       update
155     end
156   end
157
158   def move_cursor_left
159     super
160     if @headers[@type_selector.val] != self.header
161       self.header = @headers[@type_selector.val]
162       update
163     end
164   end
165
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*$/
170     lines
171   end
172
173   def default_attribution m
174     "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
175   end
176
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
182       update
183     end
184   end
185
186   def gen_references
187     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
188   end
189
190   def edit_field field
191     edited_field = super
192     if edited_field && edited_field != "Subject"
193       @type_selector.set_to :user
194       update
195     end
196   end
197 end
198
199 end