3 class ThreadViewMode < LineCursorMode
4 ## this holds all info we need to lay out a message
6 attr_accessor :top, :bot, :prev, :next, :depth, :width, :state, :color, :star_color, :orig_new
13 DATE_FORMAT = "%B %e %Y %l:%M%P"
14 INDENT_SPACES = 2 # how many spaces to indent child messages
16 HookManager.register "detailed-headers", <<EOS
17 Add or remove headers from the detailed header display of a message.
19 message: The message whose headers are to be formatted.
20 headers: A hash of header (name, value) pairs, initialized to the default
23 None. The variable 'headers' should be modified in place.
26 HookManager.register "bounce-command", <<EOS
27 Determines the command used to bounce a message.
29 from: The From header of the message being bounced
30 (eg: likely _not_ your address).
31 to: The addresses you asked the message to be bounced to as an array.
33 A string representing the command to pipe the mail into. This
34 should include the entire command except for the destination addresses,
35 which will be appended by sup.
38 register_keymap do |k|
39 k.add :toggle_detailed_header, "Toggle detailed header", 'h'
40 k.add :show_header, "Show full message header", 'H'
41 k.add :show_message, "Show full message (raw form)", 'V'
42 k.add :activate_chunk, "Expand/collapse or activate item", :enter
43 k.add :expand_all_messages, "Expand/collapse all messages", 'E'
44 k.add :edit_draft, "Edit draft", 'e'
45 k.add :send_draft, "Send draft", 'y'
46 k.add :edit_labels, "Edit or add labels for a thread", 'l'
47 k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
48 k.add :jump_to_next_open, "Jump to next open message", 'n'
49 k.add :jump_to_prev_open, "Jump to previous open message", 'p'
50 k.add :align_current_message, "Align current message in buffer", 'z'
51 k.add :toggle_starred, "Star or unstar message", '*'
52 k.add :toggle_new, "Toggle unread/read status of message", 'N'
53 # k.add :collapse_non_new_messages, "Collapse all but unread messages", 'N'
54 k.add :reply, "Reply to a message", 'r'
55 k.add :forward, "Forward a message or attachment", 'f'
56 k.add :bounce, "Bounce message to other recipient(s)", '!'
57 k.add :alias, "Edit alias/nickname for a person", 'i'
58 k.add :edit_as_new, "Edit message as new", 'D'
59 k.add :save_to_disk, "Save message/attachment to disk", 's'
60 k.add :search, "Search for messages from particular people", 'S'
61 k.add :compose, "Compose message to person", 'm'
62 k.add :subscribe_to_list, "Subscribe to/unsubscribe from mailing list", "("
63 k.add :unsubscribe_from_list, "Subscribe to/unsubscribe from mailing list", ")"
64 k.add :pipe_message, "Pipe message or attachment to a shell command", '|'
66 k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read:", '.' do |kk|
67 kk.add :archive_and_kill, "Archive this thread and kill buffer", 'a'
68 kk.add :delete_and_kill, "Delete this thread and kill buffer", 'd'
69 kk.add :spam_and_kill, "Mark this thread as spam and kill buffer", 's'
70 kk.add :unread_and_kill, "Mark this thread as unread and kill buffer", 'N'
73 k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read/do (n)othing:", ',' do |kk|
74 kk.add :archive_and_next, "Archive this thread, kill buffer, and view next", 'a'
75 kk.add :delete_and_next, "Delete this thread, kill buffer, and view next", 'd'
76 kk.add :spam_and_next, "Mark this thread as spam, kill buffer, and view next", 's'
77 kk.add :unread_and_next, "Mark this thread as unread, kill buffer, and view next", 'N'
78 kk.add :do_nothing_and_next, "Kill buffer, and view next", 'n'
81 k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read/do (n)othing:", ']' do |kk|
82 kk.add :archive_and_prev, "Archive this thread, kill buffer, and view previous", 'a'
83 kk.add :delete_and_prev, "Delete this thread, kill buffer, and view previous", 'd'
84 kk.add :spam_and_prev, "Mark this thread as spam, kill buffer, and view previous", 's'
85 kk.add :unread_and_prev, "Mark this thread as unread, kill buffer, and view previous", 'N'
86 kk.add :do_nothing_and_prev, "Kill buffer, and view previous", 'n'
90 ## there are a couple important instance variables we hold to format
91 ## the thread and to provide line-based functionality. @layout is a
92 ## map from Messages to MessageLayouts, and @chunk_layout from
93 ## Chunks to ChunkLayouts. @message_lines is a map from row #s to
94 ## Message objects. @chunk_lines is a map from row #s to Chunk
95 ## objects. @person_lines is a map from row #s to Person objects.
97 def initialize thread, hidden_labels=[], index_mode=nil
100 @hidden_labels = hidden_labels
102 ## used for dispatch-and-next
103 @index_mode = index_mode
106 @layout = SavingHash.new { MessageLayout.new }
107 @chunk_layout = SavingHash.new { ChunkLayout.new }
108 earliest, latest = nil, nil
112 @thread.each do |m, d, p|
115 @layout[m].state = initial_state_for m
116 @layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
117 @layout[m].star_color = altcolor ? :alternate_starred_patina_color : :starred_patina_color
118 @layout[m].orig_new = m.has_label? :read
120 if latest_date.nil? || m.date > latest_date
126 @layout[latest].state = :open if @layout[latest].state == :closed
127 @layout[earliest].state = :detailed if earliest.has_label?(:unread) || @thread.size == 1
129 @thread.remove_label :unread
133 def draw_line ln, opts={}
135 super ln, :highlight => true
140 def lines; @text.length; end
141 def [] i; @text[i]; end
144 m = @message_lines[curpos] or return
145 BufferManager.spawn_unless_exists("Full header for #{m.id}") do
146 TextMode.new m.raw_header
151 m = @message_lines[curpos] or return
152 BufferManager.spawn_unless_exists("Raw message for #{m.id}") do
153 TextMode.new m.raw_message
157 def toggle_detailed_header
158 m = @message_lines[curpos] or return
159 @layout[m].state = (@layout[m].state == :detailed ? :open : :detailed)
164 m = @message_lines[curpos] or return
165 mode = ReplyMode.new m
166 BufferManager.spawn "Reply to #{m.subj}", mode
169 def subscribe_to_list
170 m = @message_lines[curpos] or return
171 if m.list_subscribe && m.list_subscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
172 ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [Person.from_address($1)], :subj => $3
174 BufferManager.flash "Can't find List-Subscribe header for this message."
178 def unsubscribe_from_list
179 m = @message_lines[curpos] or return
180 if m.list_unsubscribe && m.list_unsubscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
181 ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [Person.from_address($1)], :subj => $3
183 BufferManager.flash "Can't find List-Unsubscribe header for this message."
188 if(chunk = @chunk_lines[curpos]) && chunk.is_a?(Chunk::Attachment)
189 ForwardMode.spawn_nicely :attachments => [chunk]
190 elsif(m = @message_lines[curpos])
191 ForwardMode.spawn_nicely :message => m
196 m = @message_lines[curpos] or return
197 to = BufferManager.ask_for_contacts(:people, "Bounce To: ") or return
199 defcmd = AccountManager.default_account.sendmail.sub(/\s(\-(ti|it|t))\b/) do |match|
206 cmd = case (hookcmd = HookManager.run "bounce-command", :from => m.from, :to => to)
207 when nil, /^$/ then defcmd
209 end + ' ' + to.map { |t| t.email }.join(' ')
211 bt = to.size > 1 ? "#{to.size} recipients" : to.to_s
213 if BufferManager.ask_yes_or_no "Really bounce to #{bt}?"
214 debug "bounce command: #{cmd}"
216 IO.popen(cmd, 'w') do |sm|
217 sm.puts m.raw_message
219 raise SendmailCommandFailed, "Couldn't execute #{cmd}" unless $? == 0
220 rescue SystemCallError, SendmailCommandFailed => e
221 warn "problem sending mail: #{e.message}"
222 BufferManager.flash "Problem sending mail: #{e.message}"
227 include CanAliasContacts
229 p = @person_lines[curpos] or return
235 p = @person_lines[curpos] or return
236 mode = PersonSearchResultsMode.new [p]
237 BufferManager.spawn "Search for #{p.name}", mode
238 mode.load_threads :num => mode.buffer.content_height
242 p = @person_lines[curpos]
244 ComposeMode.spawn_nicely :to_default => p
246 ComposeMode.spawn_nicely
251 reserved_labels = @thread.labels.select { |l| LabelManager::RESERVED_LABELS.include? l }
252 new_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", @thread.labels
254 return unless new_labels
255 @thread.labels = Set.new(reserved_labels) + new_labels
256 new_labels.each { |l| LabelManager << l }
258 UpdateManager.relay self, :labeled, @thread.first
262 m = @message_lines[curpos] or return
263 toggle_label m, :starred
267 m = @message_lines[curpos] or return
268 toggle_label m, :unread
271 def toggle_label m, label
272 if m.has_label? label
277 ## TODO: don't recalculate EVERYTHING just to add a stupid little
278 ## star to the display
280 UpdateManager.relay self, :single_message_labeled, m
283 ## called when someone presses enter when the cursor is highlighting
284 ## a chunk. for expandable chunks (including messages) we toggle
285 ## open/closed state; for viewable chunks (like attachments) we
288 chunk = @chunk_lines[curpos] or return
289 if chunk.is_a? Chunk::Text
290 ## if the cursor is over a text region, expand/collapse the
292 chunk = @message_lines[curpos]
294 layout = if chunk.is_a?(Message)
296 elsif chunk.expandable?
300 layout.state = (layout.state != :closed ? :closed : :open)
301 #cursor_down if layout.state == :closed # too annoying
303 elsif chunk.viewable?
306 if chunk.is_a?(Message)
307 jump_to_message chunk
308 jump_to_next_open if layout.state == :closed
313 m = @message_lines[curpos] or return
314 mode = ComposeMode.new(:body => m.quotable_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc, :refs => m.refs, :replytos => m.replytos)
315 BufferManager.spawn "edit as new", mode
320 chunk = @chunk_lines[curpos] or return
322 when Chunk::Attachment
323 default_dir = File.join(($config[:default_attachment_save_dir] || "."), chunk.filename)
324 fn = BufferManager.ask_for_filename :filename, "Save attachment to file: ", default_dir
325 save_to_file(fn) { |f| f.print chunk.raw_content } if fn
327 m = @message_lines[curpos]
328 fn = BufferManager.ask_for_filename :filename, "Save message to file: "
330 save_to_file(fn) do |f|
331 m.each_raw_message_line { |l| f.print l }
337 m = @message_lines[curpos] or return
339 mode = ResumeMode.new m
340 BufferManager.spawn "Edit message", mode
341 BufferManager.kill_buffer self.buffer
344 BufferManager.flash "Not a draft message!"
349 m = @message_lines[curpos] or return
351 mode = ResumeMode.new m
352 BufferManager.spawn "Send message", mode
353 BufferManager.kill_buffer self.buffer
356 BufferManager.flash "Not a draft message!"
360 def jump_to_first_open
361 m = @message_lines[0] or return
362 if @layout[m].state != :closed
363 jump_to_message m#, true
365 jump_to_next_open #true
369 def jump_to_next_open force_alignment=nil
370 return continue_search_in_buffer if in_search? # hack: allow 'n' to apply to both operations
371 m = (curpos ... @message_lines.length).argfind { |i| @message_lines[i] }
373 while nextm = @layout[m].next
374 break if @layout[nextm].state != :closed
377 jump_to_message nextm, force_alignment if nextm
380 def align_current_message
381 m = @message_lines[curpos] or return
382 jump_to_message m, true
385 def jump_to_prev_open
386 m = (0 .. curpos).to_a.reverse.argfind { |i| @message_lines[i] } # bah, .to_a
388 ## jump to the top of the current message if we're in the body;
389 ## otherwise, to the previous message
393 while(prevm = @layout[m].prev)
394 break if @layout[prevm].state != :closed
397 jump_to_message prevm if prevm
403 def jump_to_message m, force_alignment=false
406 ## boundaries of the message
407 message_left = l.depth * INDENT_SPACES
408 message_right = message_left + l.width
410 ## calculate leftmost colum
411 left = if force_alignment # force mode: align exactly
413 else # regular: minimize cursor movement
414 ## leftmost and rightmost are boundaries of all valid left-column
416 leftmost = [message_left, message_right - buffer.content_width + 1].min
417 rightmost = message_left
418 leftcol.clamp(leftmost, rightmost)
421 jump_to_line l.top # move vertically
422 jump_to_col left # move horizontally
423 set_cursor_pos l.top # set cursor pos
426 def expand_all_messages
427 @global_message_state ||= :closed
428 @global_message_state = (@global_message_state == :closed ? :open : :closed)
429 @layout.each { |m, l| l.state = @global_message_state }
433 def collapse_non_new_messages
434 @layout.each { |m, l| l.state = l.orig_new ? :open : :closed }
438 def expand_all_quotes
439 if(m = @message_lines[curpos])
440 quotes = m.chunks.select { |c| (c.is_a?(Chunk::Quote) || c.is_a?(Chunk::Signature)) && c.lines.length > 1 }
441 numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
442 newstate = numopen > quotes.length / 2 ? :closed : :open
443 quotes.each { |c| @chunk_layout[c].state = newstate }
449 @layout = @chunk_layout = @text = nil # for good luck
452 def archive_and_kill; archive_and_then :kill end
453 def spam_and_kill; spam_and_then :kill end
454 def delete_and_kill; delete_and_then :kill end
455 def unread_and_kill; unread_and_then :kill end
457 def archive_and_next; archive_and_then :next end
458 def spam_and_next; spam_and_then :next end
459 def delete_and_next; delete_and_then :next end
460 def unread_and_next; unread_and_then :next end
461 def do_nothing_and_next; do_nothing_and_then :next end
463 def archive_and_prev; archive_and_then :prev end
464 def spam_and_prev; spam_and_then :prev end
465 def delete_and_prev; delete_and_then :prev end
466 def unread_and_prev; unread_and_then :prev end
467 def do_nothing_and_prev; do_nothing_and_then :prev end
469 def archive_and_then op
471 @thread.remove_label :inbox
472 UpdateManager.relay self, :archived, @thread.first
478 @thread.apply_label :spam
479 UpdateManager.relay self, :spammed, @thread.first
483 def delete_and_then op
485 @thread.apply_label :deleted
486 UpdateManager.relay self, :deleted, @thread.first
490 def unread_and_then op
492 @thread.apply_label :unread
493 UpdateManager.relay self, :unread, @thread.first
497 def do_nothing_and_then op
506 yield if block_given?
507 BufferManager.kill_buffer_safely buffer
512 @index_mode.launch_next_thread_after @thread, &l
514 @index_mode.launch_prev_thread_before @thread, &l
518 raise ArgumentError, "unknown thread dispatch operation #{op.inspect}"
524 chunk = @chunk_lines[curpos]
525 chunk = nil unless chunk.is_a?(Chunk::Attachment)
526 message = @message_lines[curpos] unless chunk
528 return unless chunk || message
530 command = BufferManager.ask(:shell, "pipe command: ")
531 return if command.nil? || command.empty?
533 output = pipe_to_process(command) do |stream|
535 stream.print chunk.raw_content
537 message.each_raw_message_line { |l| stream.print l }
542 BufferManager.spawn "Output of '#{command}'", TextMode.new(output)
544 BufferManager.flash "'#{command}' done!"
550 def initial_state_for m
551 if m.has_label?(:starred) || m.has_label?(:unread)
560 buffer.mark_dirty if buffer
563 ## here we generate the actual content lines. we accumulate
564 ## everything into @text, and we set @chunk_lines and
565 ## @message_lines, and we update @layout.
573 @thread.each do |m, depth, parent|
574 unless m.is_a? Message # handle nil and :fake_root
575 @text += chunk_to_lines m, nil, @text.length, depth, parent
580 ## is this still necessary?
581 next unless @layout[m].state # skip discarded drafts
584 text = chunk_to_lines m, l.state, @text.length, depth, parent, l.color, l.star_color
587 l.bot = @text.length + text.length # updated below
591 # l.state we preserve
592 l.width = 0 # updated below
593 @layout[l.prev].next = m if l.prev
595 (0 ... text.length).each do |i|
596 @chunk_lines[@text.length + i] = m
597 @message_lines[@text.length + i] = m
598 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.display_length }.sum
603 if l.state != :closed
605 cl = @chunk_layout[c]
607 ## set the default state for chunks
609 if c.expandable? && c.respond_to?(:initial_state)
615 text = chunk_to_lines c, cl.state, @text.length, depth
616 (0 ... text.length).each do |i|
617 @chunk_lines[@text.length + i] = c
618 @message_lines[@text.length + i] = m
619 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.display_length }.sum - (depth * INDENT_SPACES)
620 l.width = lw if lw > l.width
624 @layout[m].bot = @text.length
629 def message_patina_lines m, state, start, parent, prefix, color, star_color
630 prefix_widget = [color, prefix]
632 open_widget = [color, (state == :closed ? "+ " : "- ")]
633 new_widget = [color, (m.has_label?(:unread) ? "N" : " ")]
634 starred_widget = if m.has_label?(:starred)
639 attach_widget = [color, (m.has_label?(:attachment) ? "@" : " ")]
643 @person_lines[start] = m.from
644 [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
646 "#{m.from ? m.from.mediumname : '?'} to #{m.recipients.map { |l| l.shortname }.join(', ')} #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})"]]]
649 @person_lines[start] = m.from
650 [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
652 "#{m.from ? m.from.mediumname : '?'}, #{m.date.to_nice_s} (#{m.date.to_nice_distance_s}) #{m.snippet}"]]]
655 @person_lines[start] = m.from
656 from_line = [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
657 [color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
661 m.to.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
662 addressee_lines += format_person_list " To: ", m.to
665 m.cc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
666 addressee_lines += format_person_list " Cc: ", m.cc
669 m.bcc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
670 addressee_lines += format_person_list " Bcc: ", m.bcc
673 headers = OrderedHash.new
674 headers["Date"] = "#{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})"
675 headers["Subject"] = m.subj
677 show_labels = @thread.labels - LabelManager::HIDDEN_RESERVED_LABELS
678 unless show_labels.empty?
679 headers["Labels"] = show_labels.map { |x| x.to_s }.sort.join(', ')
682 headers["In reply to"] = "#{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}"
685 HookManager.run "detailed-headers", :message => m, :headers => headers
687 from_line + (addressee_lines + headers.map { |k, v| " #{k}: #{v}" }).map { |l| [[color, prefix + " " + l]] }
691 def format_person_list prefix, people
692 ptext = people.map { |p| format_person p }
693 pad = " " * prefix.display_length
694 [prefix + ptext.first + (ptext.length > 1 ? "," : "")] +
695 ptext[1 .. -1].map_with_index do |e, i|
696 pad + e + (i == ptext.length - 1 ? "" : ",")
701 p.longname + (ContactManager.is_aliased_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
704 ## todo: check arguments on this overly complex function
705 def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil, star_color=nil
706 prefix = " " * INDENT_SPACES * depth
709 [[[:missing_message_color, "#{prefix}<one or more unreceived messages>"]]]
711 [[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
713 message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
714 (chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. Hit 'e' to edit, 'y' to send. <<<"]]] : [])
717 raise "Bad chunk: #{chunk.inspect}" unless chunk.respond_to?(:inlineable?) ## debugging
719 chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
720 elsif chunk.expandable?
723 [[[chunk.patina_color, "#{prefix}+ #{chunk.patina_text}"]]]
725 [[[chunk.patina_color, "#{prefix}- #{chunk.patina_text}"]]] + chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
728 [[[chunk.patina_color, "#{prefix}x #{chunk.patina_text}"]]]
734 BufferManager.flash "viewing #{chunk.content_type} attachment..."
735 success = chunk.view!
736 BufferManager.erase_flash
737 BufferManager.completely_redraw_screen
739 BufferManager.spawn "Attachment: #{chunk.filename}", TextMode.new(chunk.to_s, chunk.filename)
740 BufferManager.flash "Couldn't execute view command, viewing as text."