]> git.cworth.org Git - sup/blob - lib/sup/modes/reply-mode.rb
Merge branch 'load-all-threads'
[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   def initialize message
23     @m = message
24
25     ## it's important to put this early because it forces a read of
26     ## the full headers (most importantly the list-post header, if
27     ## any)
28     body = reply_body_lines message
29
30     ## first, determine the address at which we received this email. this will
31     ## become our From: address in the reply.
32     from =
33       if @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
34         PersonManager.person_for(@m.recipient_email)
35       elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
36         b
37       else
38         AccountManager.default_account
39       end
40
41     ## now, determine to: and cc: addressess. we ignore reply-to for list
42     ## messages because it's typically set to the list address, which we
43     ## explicitly treat with reply type :list
44     to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
45
46     ## next, cc:
47     cc = (@m.to + @m.cc - [from, to]).uniq
48
49     ## one potential reply type is "reply to recipient". this only happens
50     ## in certain cases:
51     ## if there's no cc, then the sender is the person you want to reply
52     ## to. if it's a list message, then the list address is. otherwise,
53     ## the cc contains a recipient.
54     useful_recipient = !(cc.empty? || @m.is_list_message?)
55     
56     @headers = {}
57     @headers[:recipient] = {
58       "To" => cc.map { |p| p.full_address },
59     } if useful_recipient
60
61     ## typically we don't want to have a reply-to-sender option if the sender
62     ## is a user account. however, if the cc is empty, it's a message to
63     ## ourselves, so for the lack of any other options, we'll add it.
64     @headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
65
66     @headers[:user] = {}
67
68     not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
69     @headers[:all] = {
70       "To" => [to.full_address],
71       "Cc" => not_me_ccs.map { |p| p.full_address },
72     } unless not_me_ccs.empty?
73
74     @headers[:list] = {
75       "To" => [@m.list_address.full_address],
76     } if @m.is_list_message?
77
78     refs = gen_references
79
80     @headers.each do |k, v|
81       @headers[k] = {
82                "From" => from.full_address,
83                "To" => [],
84                "Cc" => [],
85                "Bcc" => [],
86                "In-Reply-To" => "<#{@m.id}>",
87                "Subject" => Message.reify_subj(@m.subj),
88                "References" => refs,
89              }.merge v
90     end
91
92     types = REPLY_TYPES.select { |t| @headers.member?(t) }
93     @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
94
95     @type_selector.set_to(
96       if @m.is_list_message?
97         :list
98       elsif @headers.member? :sender
99         :sender
100       else
101         :recipient
102       end)
103
104     @headers.each do |k, v|
105       HookManager.run "before-edit", :header => v, :body => body
106     end
107
108     super :header => @headers[@type_selector.val], :body => body, :twiddles => false
109     add_selector @type_selector
110   end
111
112 protected
113
114   def move_cursor_right
115     super
116     if @headers[@type_selector.val] != self.header
117       self.header = @headers[@type_selector.val]
118       update
119     end
120   end
121
122   def move_cursor_left
123     super
124     if @headers[@type_selector.val] != self.header
125       self.header = @headers[@type_selector.val]
126       update
127     end
128   end
129
130   def reply_body_lines m
131     attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
132     lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
133     lines.pop while lines.last =~ /^\s*$/
134     lines
135   end
136
137   def default_attribution m
138     "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
139   end
140
141   def handle_new_text new_header, new_body
142     old_header = @headers[@type_selector.val]
143     if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
144       @type_selector.set_to :user
145       self.header = @headers[:user] = new_header
146       update
147     end
148   end
149
150   def gen_references
151     (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ")
152   end
153
154   def edit_field field
155     edited_field = super
156     if edited_field && edited_field != "Subject"
157       @type_selector.set_to :user
158       update
159     end
160   end
161 end
162
163 end