lib/sup/modes/reply-mode.rb in sup-0.3 vs lib/sup/modes/reply-mode.rb in sup-0.4
- old
+ new
@@ -1,51 +1,61 @@
module Redwood
class ReplyMode < EditMessageMode
REPLY_TYPES = [:sender, :recipient, :list, :all, :user]
TYPE_DESCRIPTIONS = {
- :sender => "Reply to sender",
- :recipient => "Reply to recipient",
- :all => "Reply to all",
- :list => "Reply to mailing list",
- :user => "Customized reply"
+ :sender => "Sender",
+ :recipient => "Recipient",
+ :all => "All",
+ :list => "Mailing list",
+ :user => "Customized"
}
- register_keymap do |k|
- k.add :move_cursor_right, "Move cursor to the right", :right
- k.add :move_cursor_left, "Move cursor to the left", :left
- end
+ HookManager.register "attribution", <<EOS
+Generates an attribution ("Excerpts from Joe Bloggs's message of Fri Jan 11 09:54:32 -0500 2008:").
+Variables:
+ message: a message object representing the message being replied to
+ (useful values include message.from.name and message.date)
+Return value:
+ A string containing the text of the quote line (can be multi-line)
+EOS
def initialize message
@m = message
## it's important to put this early because it forces a read of
## the full headers (most importantly the list-post header, if
## any)
body = reply_body_lines message
+ ## first, determine the address at which we received this email. this will
+ ## become our From: address in the reply.
from =
- if @m.recipient_email && (a = AccountManager.account_for(@m.recipient_email))
- a
+ if @m.recipient_email && AccountManager.is_account_email?(@m.recipient_email)
+ PersonManager.person_for(@m.recipient_email)
elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p })
b
else
AccountManager.default_account
end
- ## ignore reply-to for list messages because it's typically set to
- ## the list address, which we explicitly treat with :list
+ ## now, determine to: and cc: addressess. we ignore reply-to for list
+ ## messages because it's typically set to the list address, which we
+ ## explicitly treat with reply type :list
to = @m.is_list_message? ? @m.from : (@m.replyto || @m.from)
+
+ ## next, cc:
cc = (@m.to + @m.cc - [from, to]).uniq
- @headers = {}
-
+ ## one potential reply type is "reply to recipient". this only happens
+ ## in certain cases:
## if there's no cc, then the sender is the person you want to reply
## to. if it's a list message, then the list address is. otherwise,
## the cc contains a recipient.
useful_recipient = !(cc.empty? || @m.is_list_message?)
+ @headers = {}
@headers[:recipient] = {
"To" => cc.map { |p| p.full_address },
} if useful_recipient
## typically we don't want to have a reply-to-sender option if the sender
@@ -53,74 +63,83 @@
## ourselves, so for the lack of any other options, we'll add it.
@headers[:sender] = { "To" => [to.full_address], } if !AccountManager.is_account?(to) || !useful_recipient
@headers[:user] = {}
+ not_me_ccs = cc.select { |p| !AccountManager.is_account?(p) }
@headers[:all] = {
"To" => [to.full_address],
- "Cc" => cc.select { |p| !AccountManager.is_account?(p) }.map { |p| p.full_address },
- } unless cc.empty?
+ "Cc" => not_me_ccs.map { |p| p.full_address },
+ } unless not_me_ccs.empty?
@headers[:list] = {
"To" => [@m.list_address.full_address],
} if @m.is_list_message?
refs = gen_references
@headers.each do |k, v|
@headers[k] = {
- "From" => "#{from.name} <#{from.email}>",
+ "From" => from.full_address,
"To" => [],
"Cc" => [],
"Bcc" => [],
"In-Reply-To" => "<#{@m.id}>",
"Subject" => Message.reify_subj(@m.subj),
"References" => refs,
}.merge v
end
- @type_labels = REPLY_TYPES.select { |t| @headers.member?(t) }
- @selected_type =
+ types = REPLY_TYPES.select { |t| @headers.member?(t) }
+ @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] }
+
+ @type_selector.set_to(
if @m.is_list_message?
:list
elsif @headers.member? :sender
:sender
else
:recipient
- end
+ end)
- super :header => @headers[@selected_type], :body => body,
- :skip_top_rows => 2, :twiddles => false
+ super :header => @headers[@type_selector.val], :body => body, :twiddles => false
+ add_selector @type_selector
end
- def lines; super + 2; end
- def [] i
- case i
- when 0
- @type_labels.inject([]) do |array, t|
- array + [[(t == @selected_type ? :none_highlight : :none),
- "#{TYPE_DESCRIPTIONS[t]}"], [:none, " "]]
- end + [[:none, ""]]
- when 1
- ""
- else
- super(i - 2)
+protected
+
+ def move_cursor_right
+ super
+ if @headers[@type_selector.val] != self.header
+ self.header = @headers[@type_selector.val]
+ update
end
end
-protected
+ def move_cursor_left
+ super
+ if @headers[@type_selector.val] != self.header
+ self.header = @headers[@type_selector.val]
+ update
+ end
+ end
def reply_body_lines m
- lines = ["Excerpts from #{@m.from.name}'s message of #{@m.date}:"] + m.quotable_body_lines.map { |l| "> #{l}" }
+ attribution = HookManager.run("attribution", :message => m) || default_attribution(m)
+ lines = attribution.split("\n") + m.quotable_body_lines.map { |l| "> #{l}" }
lines.pop while lines.last =~ /^\s*$/
lines
end
+ def default_attribution m
+ "Excerpts from #{@m.from.name}'s message of #{@m.date}:"
+ end
+
def handle_new_text new_header, new_body
- old_header = @headers[@selected_type]
+ old_header = @headers[@type_selector.val]
if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v }
- @selected_type = :user
+ @type_selector.set_to :user
self.header = @headers[:user] = new_header
update
end
end
@@ -129,26 +148,12 @@
end
def edit_field field
edited_field = super
if edited_field && edited_field != "Subject"
- @selected_type = :user
+ @type_selector.set_to :user
update
end
- end
-
- def move_cursor_left
- i = @type_labels.index @selected_type
- @selected_type = @type_labels[(i - 1) % @type_labels.length]
- self.header = @headers[@selected_type]
- update
- end
-
- def move_cursor_right
- i = @type_labels.index @selected_type
- @selected_type = @type_labels[(i + 1) % @type_labels.length]
- self.header = @headers[@selected_type]
- update
end
end
end