Class: Redwood::EditMessageMode
Constant Summary
collapse
- DECORATION_LINES =
1
%w(From To Cc Bcc Subject)
%w(To Cc Bcc)
%w(Message-id Date)
Instance Attribute Summary collapse
#curpos
Attributes inherited from ScrollMode
#botline, #leftcol, #topline
Attributes inherited from Mode
#buffer
Instance Method Summary
collapse
#cleanup, #draw
Methods inherited from ScrollMode
#at_bottom?, #at_top?, #cancel_search!, #col_jump, #col_left, #col_right, #continue_search_in_buffer, #draw, #ensure_mode_validity, #half_page_down, #half_page_up, #in_search?, #jump_to_col, #jump_to_end, #jump_to_left, #jump_to_line, #jump_to_start, #line_down, #line_up, #page_down, #page_up, #resize, #rightcol, #search_goto_line, #search_goto_pos, #search_in_buffer, #search_start_line
Methods inherited from Mode
#blur, #cancel_search!, #cleanup, #draw, #focus, #handle_input, #help_text, #in_search?, keymap, keymaps, load_all_modes, make_name, #name, #pipe_to_process, register_keymap, #resize, #resolve_input, #save_to_file
Constructor Details
Returns a new instance of EditMessageMode.
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/sup/modes/edit_message_mode.rb', line 91
def initialize opts={}
@header = opts.delete(:header) || {}
@header_lines = []
@body = opts.delete(:body) || []
if opts[:attachments]
@attachments = opts[:attachments].values
@attachment_names = opts[:attachments].keys
else
@attachments = []
@attachment_names = []
end
begin
hostname = File.open("/etc/mailname", "r").gets.chomp
rescue
nil
end
hostname = Socket.gethostname if hostname.nil? or hostname.empty?
@message_id = "<#{Time.now.to_i}-sup-#{rand 10000}@#{hostname}>"
@edited = false
@sig_edited = false
@selectors = []
@selector_label_width = 0
@async_mode = nil
HookManager.run "before-edit", :header => @header, :body => @body
@account_selector = nil
if $config[:account_selector] && AccountManager.user_emails.length > 1
user_emails_copy = []
AccountManager.user_emails.each { |e| user_emails_copy.push e.dup }
@account_selector =
HorizontalSelector.new "Account:", AccountManager.user_emails + [nil], user_emails_copy + ["Customized"]
if @header["From"] =~ /<?(\S+@(\S+?))>?$/
account_from = @account_selector.can_set_to?($1) ? $1 : nil
@account_selector.set_to account_from
else
@account_selector.set_to nil
end
@account_user = @header["From"]
add_selector @account_selector
end
@crypto_selector =
if CryptoManager.have_crypto?
HorizontalSelector.new "Crypto:", [:none] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.keys, ["None"] + CryptoManager::OUTGOING_MESSAGE_OPERATIONS.values
end
add_selector @crypto_selector if @crypto_selector
if @crypto_selector
HookManager.run "crypto-mode", :header => @header, :body => @body, :crypto_selector => @crypto_selector
end
super opts
regen_text
end
|
Instance Attribute Details
Returns the value of attribute body.
73
74
75
|
# File 'lib/sup/modes/edit_message_mode.rb', line 73
def body
@body
end
|
Returns the value of attribute header.
73
74
75
|
# File 'lib/sup/modes/edit_message_mode.rb', line 73
def
@header
end
|
Returns the value of attribute status.
72
73
74
|
# File 'lib/sup/modes/edit_message_mode.rb', line 72
def status
@status
end
|
Instance Method Details
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/sup/modes/edit_message_mode.rb', line 165
def [] i
if @selectors.empty?
@text[i]
elsif i < @selectors.length
@selectors[i].line @selector_label_width
elsif i == @selectors.length
""
else
@text[i - @selectors.length - DECORATION_LINES]
end
end
|
#alternate_edit_message ⇒ Object
237
238
239
240
241
242
243
|
# File 'lib/sup/modes/edit_message_mode.rb', line 237
def alternate_edit_message
if $config[:always_edit_async]
return edit_message
else
return edit_message_async
end
end
|
#attach_file ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/sup/modes/edit_message_mode.rb', line 331
def attach_file
fn = BufferManager.ask_for_filename :attachment, "File name (enter for browser): "
return unless fn
begin
Dir[fn].each do |f|
@attachments << RMail::Message.make_file_attachment(f)
@attachment_names << f
end
update
rescue SystemCallError => e
BufferManager.flash "Can't read #{fn}: #{e.message}"
end
end
|
#default_edit_message ⇒ Object
229
230
231
232
233
234
235
|
# File 'lib/sup/modes/edit_message_mode.rb', line 229
def default_edit_message
if $config[:always_edit_async]
return edit_message_async
else
return edit_message
end
end
|
#delete_attachment ⇒ Object
345
346
347
348
349
350
351
352
|
# File 'lib/sup/modes/edit_message_mode.rb', line 345
def delete_attachment
i = curpos - @attachment_lines_offset - (@selectors.empty? ? 0 : DECORATION_LINES) - @selectors.size
if i >= 0 && i < @attachments.size && BufferManager.ask_yes_or_no("Delete attachment #{@attachment_names[i]}?")
@attachments.delete_at i
@attachment_names.delete_at i
update
end
end
|
192
|
# File 'lib/sup/modes/edit_message_mode.rb', line 192
def edit_cc; edit_field "Cc" end
|
#edit_message ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/sup/modes/edit_message_mode.rb', line 245
def edit_message
old_from = @header["From"] if @account_selector
begin
save_message_to_file
rescue SystemCallError => e
BufferManager.flash "Can't save message to file: #{e.message}"
return
end
editor = $config[:editor] || ENV['EDITOR'] || "/usr/bin/vi"
mtime = File.mtime @file.path
BufferManager.shell_out "#{editor} #{@file.path}"
@edited = true if File.mtime(@file.path) > mtime
return @edited unless @edited
, @body = parse_file @file.path
@header = - NON_EDITABLE_HEADERS
set_sig_edit_flag
if @account_selector and @header["From"] != old_from
@account_user = @header["From"]
@account_selector.set_to nil
end
handle_new_text @header, @body
rerun_crypto_selector_hook
update
@edited
end
|
#edit_message_async ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/sup/modes/edit_message_mode.rb', line 279
def edit_message_async
begin
save_message_to_file
rescue SystemCallError => e
BufferManager.flash "Can't save message to file: #{e.message}"
return
end
@mtime = File.mtime @file.path
subject = @header["Subject"] || ""
@async_mode = EditMessageAsyncMode.new self, @file.path, subject
BufferManager.spawn "Waiting for message \"#{subject}\" to be finished", @async_mode
buffer.hidden = true
end
|
#edit_message_async_resume(being_killed = false) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/sup/modes/edit_message_mode.rb', line 299
def edit_message_async_resume being_killed=false
buffer.hidden = false
@async_mode = nil
BufferManager.raise_to_front buffer if !being_killed
@edited = true if File.mtime(@file.path) > @mtime
, @body = parse_file @file.path
@header = - NON_EDITABLE_HEADERS
set_sig_edit_flag
handle_new_text @header, @body
update
true
end
|
#edit_message_or_field ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/sup/modes/edit_message_mode.rb', line 180
def edit_message_or_field
lines = (@selectors.empty? ? 0 : DECORATION_LINES) + @selectors.size
if lines > curpos
return
elsif (curpos - lines) >= @header_lines.length
default_edit_message
else
edit_field @header_lines[curpos - lines]
end
end
|
#edit_subject ⇒ Object
193
|
# File 'lib/sup/modes/edit_message_mode.rb', line 193
def edit_subject; edit_field "Subject" end
|
191
|
# File 'lib/sup/modes/edit_message_mode.rb', line 191
def edit_to; edit_field "To" end
|
#handle_new_text(header, body) ⇒ Object
hook for subclasses. i hate this style of programming.
178
|
# File 'lib/sup/modes/edit_message_mode.rb', line 178
def handle_new_text , body; end
|
#killable? ⇒ Boolean
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/sup/modes/edit_message_mode.rb', line 315
def killable?
if !@async_mode.nil?
return false if !@async_mode.killable?
if File.mtime(@file.path) > @mtime
@edited = true
, @body = parse_file @file.path
@header = - NON_EDITABLE_HEADERS
handle_new_text @header, @body
update
end
end
!edited? || BufferManager.ask_yes_or_no("Discard message?")
end
|
163
|
# File 'lib/sup/modes/edit_message_mode.rb', line 163
def lines; @text.length + (@selectors.empty? ? 0 : (@selectors.length + DECORATION_LINES)) end
|
#save_message_to_file ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/sup/modes/edit_message_mode.rb', line 195
def save_message_to_file
sig = sig_lines.join("\n")
@file = Tempfile.new ["sup.#{self.class.name.gsub(/.*::/, '').camel_to_hyphy}", ".eml"]
@file.puts (@header - NON_EDITABLE_HEADERS).first
@file.puts
begin
text = @body.join("\n")
rescue Encoding::CompatibilityError
text = @body.map { |x| x.fix_encoding! }.join("\n")
debug "encoding problem while writing message, trying to rescue, but expect errors: #{text}"
end
@file.puts text
@file.puts sig if ($config[:edit_signature] and !@sig_edited)
@file.close
end
|
#set_sig_edit_flag ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/sup/modes/edit_message_mode.rb', line 213
def set_sig_edit_flag
sig = sig_lines.join("\n")
if $config[:edit_signature]
pbody = @body.map { |x| x.fix_encoding! }.join("\n").fix_encoding!
blen = pbody.length
slen = sig.length
if blen > slen and pbody[blen-slen..blen] == sig
@sig_edited = false
@body = pbody[0..blen-slen].fix_encoding!.split("\n")
else
@sig_edited = true
end
end
end
|
#unsaved? ⇒ Boolean
329
|
# File 'lib/sup/modes/edit_message_mode.rb', line 329
def unsaved?; edited? end
|