lib/sup/modes/forward_mode.rb (3238B) - raw
1 module Redwood
2
3 class ForwardMode < EditMessageMode
4
5 HookManager.register "forward-attribution", <<EOS
6 Generates the attribution for the forwarded message
7 (["--- Begin forwarded message from John Doe ---",
8 "--- End forwarded message ---"])
9 Variables:
10 message: a message object representing the message being replied to
11 (useful values include message.from.mediumname and message.date)
12 Return value:
13 A list containing two strings: the text of the begin line and the text of the end line
14 EOS
15 ## TODO: share some of this with reply-mode
16 def initialize opts={}
17 header = {
18 "From" => AccountManager.default_account.full_address,
19 }
20
21 @m = opts[:message]
22 header["Subject"] =
23 if @m
24 "Fwd: " + @m.subj
25 elsif opts[:attachments]
26 "Fwd: " + opts[:attachments].keys.join(", ")
27 end
28
29 header["To"] = opts[:to].map { |p| p.full_address }.join(", ") if opts[:to]
30 header["Cc"] = opts[:cc].map { |p| p.full_address }.join(", ") if opts[:cc]
31 header["Bcc"] = opts[:bcc].map { |p| p.full_address }.join(", ") if opts[:bcc]
32
33 body =
34 if @m
35 forward_body_lines @m
36 elsif opts[:attachments]
37 ["Note: #{opts[:attachments].size.pluralize 'attachment'}."]
38 end
39
40 super :header => header, :body => body, :attachments => opts[:attachments]
41 end
42
43 def self.spawn_nicely opts={}
44 to = opts[:to] || (BufferManager.ask_for_contacts(:people, "To: ") or return if ($config[:ask_for_to] != false))
45 cc = opts[:cc] || (BufferManager.ask_for_contacts(:people, "Cc: ") or return if $config[:ask_for_cc])
46 bcc = opts[:bcc] || (BufferManager.ask_for_contacts(:people, "Bcc: ") or return if $config[:ask_for_bcc])
47
48 attachment_hash = {}
49 attachments = opts[:attachments] || []
50
51 if(m = opts[:message])
52 m.load_from_source! # read the full message in. you know, maybe i should just make Message#chunks do this....
53 attachments += m.chunks.select { |c| c.is_a?(Chunk::Attachment) && !c.quotable? }
54 end
55
56 attachments.each do |c|
57 mime_type = MIME::Types[c.content_type].first || MIME::Types["application/octet-stream"].first
58 attachment_hash[c.filename] = RMail::Message.make_attachment c.raw_content, mime_type.content_type, mime_type.encoding, c.filename
59 end
60
61 mode = ForwardMode.new :message => opts[:message], :to => to, :cc => cc, :bcc => bcc, :attachments => attachment_hash
62
63 title = "Forwarding " +
64 if opts[:message]
65 opts[:message].subj
66 elsif attachments
67 attachment_hash.keys.join(", ")
68 else
69 "something"
70 end
71
72 BufferManager.spawn title, mode
73 mode.default_edit_message
74 end
75
76 protected
77
78 def forward_body_lines m
79 attribution = HookManager.run("forward-attribution", :message => m) || default_attribution(m)
80 attribution[0,1] +
81 m.quotable_header_lines +
82 [""] +
83 m.quotable_body_lines +
84 attribution[1,1]
85 end
86
87 def default_attribution m
88 ["--- Begin forwarded message from #{m.from.mediumname} ---",
89 "--- End forwarded message ---"]
90 end
91
92 def send_message
93 return unless super # super returns true if the mail has been sent
94 if @m
95 @m.add_label :forwarded
96 Index.save_message @m
97 end
98 end
99 end
100
101 end