X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fmodes%2Freply-mode.rb;h=6b9f55933a6ef967fc221f8b31a6f209e48bbe84;hb=eabc808e1f79297aff65bb9e3adacf59a72b90ba;hp=b22f6ae6b2f1c6fc2b859b732cfa15bee0c59f60;hpb=6f5306d0c8a6ad21d918480a6541d3475384a0ef;p=sup diff --git a/lib/sup/modes/reply-mode.rb b/lib/sup/modes/reply-mode.rb index b22f6ae..6b9f559 100644 --- a/lib/sup/modes/reply-mode.rb +++ b/lib/sup/modes/reply-mode.rb @@ -3,154 +3,207 @@ 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", < false @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) - - from = - if @m.recipient_email - AccountManager.account_for(@m.recipient_email) - else - (@m.to + @m.cc).find { |p| AccountManager.is_account? p } - end || AccountManager.default_account + body = reply_body_lines message + + ## first, determine the address at which we received this email. this will + ## become our From: address in the reply. + hook_reply_from = HookManager.run "reply-from", :message => @m + + ## sanity check that selection is a Person (or we'll fail below) + ## don't check that it's an Account, though; assume they know what they're + ## doing. + if hook_reply_from && !(hook_reply_from.is_a? Person) + Redwood::log "reply-from returned non-Person, using default from." + hook_reply_from = nil + end - #from_email = @m.recipient_email || from.email - from_email = from.email + ## determine the from address of a reply. + ## if we have a value from a hook, use it. + from = if hook_reply_from + hook_reply_from + ## otherwise, if the original email had an envelope-to header, try and use + ## it, and look up the corresponding name form the list of accounts. + ## + ## this is for the case where mail is received from a mailing lists (so the + ## To: is the list id itself). if the user subscribes via a particular + ## alias, we want to use that alias in the reply. + elsif @m.recipient_email && (a = AccountManager.account_for(@m.recipient_email)) + Person.new a.name, @m.recipient_email + ## otherwise, try and find an account somewhere in the list of to's + ## and cc's. + elsif(b = (@m.to + @m.cc).find { |p| AccountManager.is_account? p }) + b + ## if all else fails, use the default + else + AccountManager.default_account + end - ## ignore reply-to for list messages because it's typically set to - ## the list address anyways + ## 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 + ## 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[:sender] = { - "From" => "#{from.name} <#{from_email}>", - "To" => [to.full_address], - } unless AccountManager.is_account? to - @headers[:recipient] = { - "From" => "#{from.name} <#{from_email}>", "To" => cc.map { |p| p.full_address }, - } unless cc.empty? || @m.is_list_message? + } if useful_recipient - @headers[:user] = { - "From" => "#{from.name} <#{from_email}>", - } + ## typically we don't want to have a reply-to-sender option if the sender + ## is a user account. however, if the cc is empty, it's a message to + ## 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] = { - "From" => "#{from.name} <#{from_email}>", "To" => [to.full_address], - "Cc" => cc.map { |p| p.full_address }, - } unless cc.empty? + "Cc" => not_me_ccs.map { |p| p.full_address }, + } unless not_me_ccs.empty? @headers[:list] = { - "From" => "#{from.name} <#{from_email}>", "To" => [@m.list_address.full_address], } if @m.is_list_message? refs = gen_references - mid = gen_message_id + @headers.each do |k, v| @headers[k] = { - "To" => "", - "Cc" => "", - "Bcc" => "", + "From" => from.full_address, + "To" => [], + "Cc" => [], + "Bcc" => [], "In-Reply-To" => "<#{@m.id}>", "Subject" => Message.reify_subj(@m.subj), - "Message-Id" => mid, "References" => refs, }.merge v end - @type_labels = REPLY_TYPES.select { |t| @headers.member?(t) } - @selected_type = - if @m.is_list_message? + types = REPLY_TYPES.select { |t| @headers.member?(t) } + @type_selector = HorizontalSelector.new "Reply to:", types, types.map { |x| TYPE_DESCRIPTIONS[x] } + + hook_reply = HookManager.run "reply-to", :modes => types + + @type_selector.set_to( + if types.include? hook_reply + hook_reply + elsif @m.is_list_message? :list elsif @headers.member? :sender :sender else :recipient - end + end) - regen_text - end - - def lines; @text.length + 2; end - def [] i - case i - when 0 - lame = [] - @type_labels.each do |t| - lame << [(t == @selected_type ? :none_highlight : :none), - "#{TYPE_DESCRIPTIONS[t]}"] - lame << [:none, " "] - end - lame + [[:none, ""]] - when 1 - "" - else - @text[i - 2] + @headers.each do |k, v| + HookManager.run "before-edit", :header => v, :body => body end + + super :header => @headers[@type_selector.val], :body => body, :twiddles => false + add_selector @type_selector end protected - def body; @body + sig_lines; end - def header; @headers[@selected_type]; end + def move_cursor_right + super + if @headers[@type_selector.val] != self.header + self.header = @headers[@type_selector.val] + update + end + end + + 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.basic_body_lines.map { |l| "> #{l}" } - lines.pop while lines.last !~ /[:alpha:]/ + 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 handle_new_text new_header, new_body - @body = new_body - - if new_header.size != header.size || - header.any? { |k, v| new_header[k] != v } - #raise "nhs: #{new_header.size} hs: #{header.size} new: #{new_header.inspect} old: #{header.inspect}" - @selected_type = :user - @headers[:user] = new_header - end + def default_attribution m + "Excerpts from #{@m.from.name}'s message of #{@m.date}:" end - def regen_text - @text = header_lines(header - NON_EDITABLE_HEADERS) + [""] + body + def handle_new_text new_header, new_body + old_header = @headers[@type_selector.val] + if new_header.size != old_header.size || old_header.any? { |k, v| new_header[k] != v } + @type_selector.set_to :user + self.header = @headers[:user] = new_header + update + end end def gen_references (@m.refs + [@m.id]).map { |x| "<#{x}>" }.join(" ") end - - def move_cursor_left - i = @type_labels.index @selected_type - @selected_type = @type_labels[(i - 1) % @type_labels.length] - update - end - def move_cursor_right - i = @type_labels.index @selected_type - @selected_type = @type_labels[(i + 1) % @type_labels.length] - update + def edit_field field + edited_field = super + if edited_field && edited_field != "Subject" + @type_selector.set_to :user + update + end end end