]> git.cworth.org Git - sup/blob - lib/sup/modes/forward-mode.rb
Merge branch 'index-locking' into next
[sup] / lib / sup / modes / forward-mode.rb
1 module Redwood
2
3 class ForwardMode < EditMessageMode
4   ## TODO: share some of this with reply-mode
5   def initialize opts={}
6     header = {
7       "From" => AccountManager.default_account.full_address,
8     }
9
10     header["Subject"] = 
11       if opts[:message]
12         "Fwd: " + opts[:message].subj
13       elsif opts[:attachments]
14         "Fwd: " + opts[:attachments].keys.join(", ")
15       end
16
17     header["To"] = opts[:to].map { |p| p.full_address }.join(", ") if opts[:to]
18     header["Cc"] = opts[:cc].map { |p| p.full_address }.join(", ") if opts[:cc]
19     header["Bcc"] = opts[:bcc].map { |p| p.full_address }.join(", ") if opts[:bcc]
20
21     body =
22       if opts[:message]
23         forward_body_lines(opts[:message]) 
24       elsif opts[:attachments]
25         ["Note: #{opts[:attachments].size.pluralize 'attachment'}."]
26       end
27
28     super :header => header, :body => body, :attachments => opts[:attachments]
29   end
30
31   def self.spawn_nicely opts={}
32     to = opts[:to] || BufferManager.ask_for_contacts(:people, "To: ") or return
33     cc = opts[:cc] || BufferManager.ask_for_contacts(:people, "Cc: ") or return if $config[:ask_for_cc]
34     bcc = opts[:bcc] || BufferManager.ask_for_contacts(:people, "Bcc: ") or return if $config[:ask_for_bcc]
35     
36     attachment_hash = {}
37     attachments = opts[:attachments] || []
38
39     if(m = opts[:message])
40       m.load_from_source! # read the full message in. you know, maybe i should just make Message#chunks do this....
41       attachments += m.chunks.select { |c| c.is_a?(Chunk::Attachment) && !c.quotable? }
42     end
43
44     attachments.each do |c|
45       mime_type = MIME::Types[c.content_type].first || MIME::Types["application/octet-stream"].first
46       attachment_hash[c.filename] = RMail::Message.make_attachment c.raw_content, mime_type.content_type, mime_type.encoding, c.filename
47     end
48
49     mode = ForwardMode.new :message => opts[:message], :to => to, :cc => cc, :bcc => bcc, :attachments => attachment_hash
50
51     title = "Forwarding " +
52       if opts[:message]
53         opts[:message].subj
54       elsif attachments
55         attachment_hash.keys.join(", ")
56       else
57         "something"
58       end
59
60     BufferManager.spawn title, mode
61     mode.edit_message
62   end
63
64 protected
65
66   def forward_body_lines m
67     ["--- Begin forwarded message from #{m.from.mediumname} ---"] + 
68       m.quotable_header_lines + [""] + m.quotable_body_lines +
69       ["--- End forwarded message ---"]
70   end
71 end
72
73 end