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