2 require 'socket' # just for gethostname!
8 class EditMessageMode < LineCursorMode
9 FORCE_HEADERS = %w(From To Cc Bcc Subject)
10 MULTI_HEADERS = %w(To Cc Bcc)
11 NON_EDITABLE_HEADERS = %w(Message-Id Date)
14 attr_accessor :body, :header
17 register_keymap do |k|
18 k.add :send_message, "Send message", 'y'
19 k.add :edit, "Edit message", 'e', :enter
20 k.add :save_as_draft, "Save as draft", 'P'
21 k.add :attach_file, "Attach a file", 'a'
22 k.add :delete_attachment, "Delete an attachment", 'd'
25 def initialize opts={}
26 @header = opts.delete(:header) || {}
27 @body = opts.delete(:body) || []
28 @body += sig_lines if $config[:edit_signature]
30 @attachment_lines = {}
31 @message_id = "<#{Time.now.to_i}-sup-#{rand 10000}@#{Socket.gethostname}>"
34 @message_id = "<#{Time.now.to_i}-sup-#{rand 10000}@#{Socket.gethostname}>"
39 def lines; @text.length end
40 def [] i; @text[i] end
43 def handle_new_text header, body; end
46 @file = Tempfile.new "sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}"
47 @file.puts header_lines(@header - NON_EDITABLE_HEADERS)
52 editor = $config[:editor] || ENV['EDITOR'] || "/usr/bin/vi"
54 mtime = File.mtime @file.path
55 BufferManager.shell_out "#{editor} #{@file.path}"
56 @edited = true if File.mtime(@file.path) > mtime
58 header, @body = parse_file @file.path
59 @header = header - NON_EDITABLE_HEADERS
60 handle_new_text @header, @body
65 !edited? || BufferManager.ask_yes_or_no("Discard message?")
69 fn = BufferManager.ask_for_filenames :attachment, "File name (enter for browser): "
70 fn.each { |f| @attachments << Pathname.new(f) }
75 i = curpos - @attachment_lines_offset
76 if i >= 0 && i < @attachments.size && BufferManager.ask_yes_or_no("Delete attachment #{@attachments[i]}?")
77 @attachments.delete_at i
86 buffer.mark_dirty if buffer
90 top = header_lines(@header - NON_EDITABLE_HEADERS) + [""]
92 @text += sig_lines unless $config[:edit_signature]
94 unless @attachments.empty?
96 @attachment_lines_offset = @text.length
97 @text += @attachments.map { |f| [[:attachment_color, "+ Attachment: #{f} (#{f.human_size})"]] }
103 header = MBox::read_header f
106 header.delete_if { |k, v| NON_EDITABLE_HEADERS.member? k }
107 header.each do |k, v|
108 next unless MULTI_HEADERS.include?(k) && !v.empty?
109 header[k] = v.split_on_commas.map do |name|
110 (p = ContactManager.person_with(name)) && p.full_address || name
118 def header_lines header
119 force_headers = FORCE_HEADERS.map { |h| make_lines "#{h}:", header[h] }
120 other_headers = (header.keys - FORCE_HEADERS).map do |h|
121 make_lines "#{h}:", header[h]
124 (force_headers + other_headers).flatten.compact
127 def make_lines header, things
132 [header + " " + things]
137 things.map_with_index do |name, i|
138 raise "an array: #{name.inspect} (things #{things.inspect})" if Array === name
142 (" " * (header.length + 1)) + name
143 end + (i == things.length - 1 ? "" : ",")
150 return unless edited? || BufferManager.ask_yes_or_no("Message unedited. Really send?")
154 if @header["From"] =~ /<?(\S+@(\S+?))>?$/
157 AccountManager.default_account.email
160 acct = AccountManager.account_for(from_email) || AccountManager.default_account
161 BufferManager.flash "Sending..."
164 IO.popen(acct.sendmail, "w") { |p| write_full_message_to p }
165 rescue SystemCallError
168 SentManager.write_sent_message(date, from_email) { |f| write_message f, true, date }
169 BufferManager.kill_buffer buffer
170 BufferManager.flash "Message sent!"
172 Redwood::log "Non-zero return value in running sendmail command for #{acct.longname}: #{acct.sendmail.inspect}"
173 BufferManager.flash "Problem sending mail. See log for details."
178 DraftManager.write_draft { |f| write_message f, false }
179 BufferManager.kill_buffer buffer
180 BufferManager.flash "Saved for later editing."
183 def write_full_message_to f
184 m = RMail::Message.new
185 @header.each { |k, v| m.header[k] = v.to_s unless v.to_s.empty? }
186 m.header["Date"] = Time.now.rfc2822
187 m.header["Message-Id"] = @message_id
188 m.header["User-Agent"] = "Sup/#{Redwood::VERSION}"
189 if @attachments.empty?
190 m.header["Content-Disposition"] = "inline"
191 m.header["Content-Type"] = "text/plain; charset=#{$encoding}"
192 m.body = @body.join "\n"
193 m.body += sig_lines.join("\n") unless $config[:edit_signature]
195 body_m = RMail::Message.new
196 body_m.body = @body.join "\n"
197 body_m.body += sig_lines.join("\n") unless $config[:edit_signature]
200 @attachments.each { |fn| m.add_attachment fn.to_s }
205 ## this is going to change soon: draft messages (currently written
206 ## with full=false) will be output as yaml.
207 def write_message f, full=true, date=Time.now
208 raise ArgumentError, "no pre-defined date: header allowed" if @header["Date"]
209 f.puts header_lines(@header)
211 Date: #{date.rfc2822}
212 Message-Id: #{@message_id}
217 Content-Type: text/plain; charset=us-ascii
218 Content-Disposition: inline
219 User-Agent: Redwood/#{Redwood::VERSION}
224 f.puts @body.map { |l| l =~ /^From / ? ">#{l}" : l }
225 f.puts sig_lines if full unless $config[:edit_signature]
231 p = PersonManager.person_for @header["From"]
232 sigfn = (AccountManager.account_for(p.email) ||
233 AccountManager.default_account).signature
235 if sigfn && File.exists?(sigfn)
236 ["", "-- "] + File.readlines(sigfn).map { |l| l.chomp }