+require 'open3'
module Redwood
class ThreadViewMode < LineCursorMode
DATE_FORMAT = "%B %e %Y %l:%M%P"
INDENT_SPACES = 2 # how many spaces to indent child messages
+ HookManager.register "detailed-headers", <<EOS
+Add or remove headers from the detailed header display of a message.
+Variables:
+ message: The message whose headers are to be formatted.
+ headers: A hash of header (name, value) pairs, initialized to the default
+ headers.
+Return value:
+ None. The variable 'headers' should be modified in place.
+EOS
+
register_keymap do |k|
k.add :toggle_detailed_header, "Toggle detailed header", 'h'
k.add :show_header, "Show full message header", 'H'
- k.add :toggle_expanded, "Expand/collapse item", :enter
+ k.add :activate_chunk, "Expand/collapse or activate item", :enter
k.add :expand_all_messages, "Expand/collapse all messages", 'E'
k.add :edit_draft, "Edit draft", 'e'
k.add :edit_labels, "Edit or add labels for a thread", 'l'
k.add :jump_to_prev_open, "Jump to previous open message", 'p'
k.add :toggle_starred, "Star or unstar message", '*'
k.add :toggle_new, "Toggle new/read status of message", 'N'
-# k.add :collapse_non_new_messages, "Collapse all but new messages", 'N'
+# k.add :collapse_non_new_messages, "Collapse all but unread messages", 'N'
k.add :reply, "Reply to a message", 'r'
- k.add :forward, "Forward a message", 'f'
+ k.add :forward, "Forward a message or attachment", 'f'
k.add :alias, "Edit alias/nickname for a person", 'i'
k.add :edit_as_new, "Edit message as new", 'D'
k.add :save_to_disk, "Save message/attachment to disk", 's'
k.add :compose, "Compose message to person", 'm'
k.add :archive_and_kill, "Archive thread and kill buffer", 'a'
k.add :delete_and_kill, "Delete thread and kill buffer", 'd'
+ k.add :subscribe_to_list, "Subscribe to/unsubscribe from mailing list", "("
+ k.add :unsubscribe_from_list, "Subscribe to/unsubscribe from mailing list", ")"
+ k.add :pipe_message, "Pipe message or attachment to a shell command", '|'
end
## there are a couple important instance variables we hold to format
BufferManager.spawn "Reply to #{m.subj}", mode
end
- def forward
+ def subscribe_to_list
m = @message_lines[curpos] or return
- mode = ForwardMode.new m
- BufferManager.spawn "Forward of #{m.subj}", mode
- mode.edit_message
+ if m.list_subscribe && m.list_subscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
+ ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [PersonManager.person_for($1)], :subj => $3
+ else
+ BufferManager.flash "Can't find List-Subscribe header for this message."
+ end
+ end
+
+ def unsubscribe_from_list
+ m = @message_lines[curpos] or return
+ if m.list_unsubscribe && m.list_unsubscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
+ ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [PersonManager.person_for($1)], :subj => $3
+ else
+ BufferManager.flash "Can't find List-Unsubscribe header for this message."
+ end
+ end
+
+ def forward
+ if(chunk = @chunk_lines[curpos]) && chunk.is_a?(Chunk::Attachment)
+ ForwardMode.spawn_nicely :attachments => [chunk]
+ elsif(m = @message_lines[curpos])
+ ForwardMode.spawn_nicely :message => m
+ end
end
include CanAliasContacts
def compose
p = @person_lines[curpos]
- mode =
- if p
- ComposeMode.new :to => [p]
- else
- ComposeMode.new
- end
- BufferManager.spawn "Compose message", mode
- mode.edit_message
+ if p
+ ComposeMode.spawn_nicely :to => [p]
+ else
+ ComposeMode.spawn_nicely
+ end
end
def edit_labels
- m = @message_lines[curpos] or return
- new_labels = BufferManager.ask_for_labels :label, "Labels for message: ", m.labels
+ reserved_labels = @thread.labels.select { |l| LabelManager::RESERVED_LABELS.include? l }
+ new_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", @thread.labels
return unless new_labels
- m.labels = new_labels
+ @thread.labels = (reserved_labels + new_labels).uniq
new_labels.each { |l| LabelManager << l }
- ## TODO: don't recalculate EVERYTHING
update
- UpdateManager.relay self, :label, m
+ UpdateManager.relay self, :labeled, @thread.first
end
def toggle_starred
## TODO: don't recalculate EVERYTHING just to add a stupid little
## star to the display
update
- UpdateManager.relay self, :label, m
+ UpdateManager.relay self, :single_message_labeled, m
end
- ## a little overly complicated. for quotes and signatures, if it's
- ## one line, we just display it and don't allow for
- ## collapsing/expanding. for crypto notices, we allow
- ## expanding/collapsing iff the # of notice lines is > 0.
- def toggle_expanded
+ ## called when someone presses enter when the cursor is highlighting
+ ## a chunk. for expandable chunks (including messages) we toggle
+ ## open/closed state; for viewable chunks (like attachments) we
+ ## view.
+ def activate_chunk
chunk = @chunk_lines[curpos] or return
- case chunk
- when Message
- l = @layout[chunk]
- l.state = (l.state != :closed ? :closed : :open)
- cursor_down if l.state == :closed
- when CryptoSignature, CryptoDecryptedNotice
- return if chunk.lines.empty?
- toggle_chunk_expansion chunk
- when Message::Quote, Message::Signature
- return if chunk.lines.length <= 1
- toggle_chunk_expansion chunk
- when Message::Attachment
- if chunk.inlineable?
- toggle_chunk_expansion chunk
- else
- view_attachment chunk
+ layout =
+ if chunk.is_a?(Message)
+ @layout[chunk]
+ elsif chunk.expandable?
+ @chunk_layout[chunk]
end
+ if layout
+ layout.state = (layout.state != :closed ? :closed : :open)
+ #cursor_down if layout.state == :closed # too annoying
+ update
+ elsif chunk.viewable?
+ view chunk
end
- update
end
def edit_as_new
m = @message_lines[curpos] or return
- mode = ComposeMode.new(:body => m.basic_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc)
+ mode = ComposeMode.new(:body => m.quotable_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc)
BufferManager.spawn "edit as new", mode
mode.edit_message
end
def save_to_disk
chunk = @chunk_lines[curpos] or return
case chunk
- when Message::Attachment
+ when Chunk::Attachment
fn = BufferManager.ask_for_filename :filename, "Save attachment to file: ", chunk.filename
save_to_file(fn) { |f| f.print chunk.raw_content } if fn
else
end
def jump_to_next_open
+ return continue_search_in_buffer if in_search? # hack: allow 'n' to apply to both operations
m = @message_lines[curpos] or return
while nextm = @layout[m].next
break if @layout[nextm].state != :closed
def expand_all_quotes
if(m = @message_lines[curpos])
- quotes = m.chunks.select { |c| (c.is_a?(Message::Quote) || c.is_a?(Message::Signature)) && c.lines.length > 1 }
+ quotes = m.chunks.select { |c| (c.is_a?(Chunk::Quote) || c.is_a?(Chunk::Signature)) && c.lines.length > 1 }
numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
newstate = numopen > quotes.length / 2 ? :closed : :open
quotes.each { |c| @chunk_layout[c].state = newstate }
def archive_and_kill
@thread.remove_label :inbox
- UpdateManager.relay self, :archived, @thread
+ UpdateManager.relay self, :archived, @thread.first
BufferManager.kill_buffer_safely buffer
end
def delete_and_kill
@thread.apply_label :deleted
- UpdateManager.relay self, :deleted, @thread
+ UpdateManager.relay self, :deleted, @thread.first
BufferManager.kill_buffer_safely buffer
end
-private
+ def pipe_message
+ chunk = @chunk_lines[curpos]
+ chunk = nil unless chunk.is_a?(Chunk::Attachment)
+ message = @message_lines[curpos] unless chunk
+
+ return unless chunk || message
+
+ command = BufferManager.ask(:shell, "pipe command: ")
+ return if command.nil? || command.empty?
+
+ output = pipe_to_process(command) do |stream|
+ if chunk
+ stream.print chunk.raw_content
+ else
+ message.each_raw_message_line { |l| stream.print l }
+ end
+ end
- def toggle_chunk_expansion chunk
- l = @chunk_layout[chunk]
- l.state = (l.state != :closed ? :closed : :open)
- cursor_down if l.state == :closed
+ if output
+ BufferManager.spawn "Output of '#{command}'", TextMode.new(output)
+ else
+ BufferManager.flash "'#{command}' done!"
+ end
end
+private
+
def initial_state_for m
if m.has_label?(:starred) || m.has_label?(:unread)
:open
## set the default state for chunks
cl.state ||=
- if c.is_a?(Message::Attachment) && c.inlineable?
- :open
+ if c.expandable? && c.respond_to?(:initial_state)
+ c.initial_state
else
:closed
end
when :detailed
@person_lines[start] = m.from
- from = [[prefix_widget, open_widget, new_widget, starred_widget,
+ from_line = [[prefix_widget, open_widget, new_widget, starred_widget,
[color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
- rest = []
+ addressee_lines = []
unless m.to.empty?
- m.to.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
- rest += format_person_list " To: ", m.to
+ m.to.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
+ addressee_lines += format_person_list " To: ", m.to
end
unless m.cc.empty?
- m.cc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
- rest += format_person_list " Cc: ", m.cc
+ m.cc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
+ addressee_lines += format_person_list " Cc: ", m.cc
end
unless m.bcc.empty?
- m.bcc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
- rest += format_person_list " Bcc: ", m.bcc
+ m.bcc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
+ addressee_lines += format_person_list " Bcc: ", m.bcc
+ end
+
+ headers = OrderedHash.new
+ headers["Date"] = "#{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})"
+ headers["Subject"] = m.subj
+
+ show_labels = @thread.labels - LabelManager::HIDDEN_RESERVED_LABELS
+ unless show_labels.empty?
+ headers["Labels"] = show_labels.map { |x| x.to_s }.sort.join(', ')
+ end
+ if parent
+ headers["In reply to"] = "#{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}"
end
- rest += [
- " Date: #{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})",
- " Subject: #{m.subj}",
- (parent ? " In reply to: #{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}" : nil),
- m.labels.empty? ? nil : " Labels: #{m.labels.join(', ')}",
- ].compact
+ HookManager.run "detailed-headers", :message => m, :headers => headers
- from + rest.map { |l| [[color, prefix + " " + l]] }
+ from_line + (addressee_lines + headers.map { |k, v| " #{k}: #{v}" }).map { |l| [[color, prefix + " " + l]] }
end
end
end
def format_person p
- p.longname + (ContactManager.is_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
+ p.longname + (ContactManager.is_aliased_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
end
+ ## todo: check arguments on this overly complex function
def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil, star_color=nil
prefix = " " * INDENT_SPACES * depth
case chunk
when Message
message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
(chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. To edit, hit 'e'. <<<"]]] : [])
- when Message::Attachment
- return [[[:attachment_color, "#{prefix}x Attachment: #{chunk.filename} (#{chunk.content_type})"]]] unless chunk.inlineable?
- case state
- when :closed
- [[[:attachment_color, "#{prefix}+ Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]]
- when :open
- [[[:attachment_color, "#{prefix}- Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]] + chunk.lines.map { |line| [[:none, "#{prefix}#{line}"]] }
- end
- when Message::Text
- t = chunk.lines
- if t.last =~ /^\s*$/ && t.length > 1
- t.pop while t[-2] =~ /^\s*$/ # pop until only one file empty line
- end
- t.map { |line| [[:none, "#{prefix}#{line}"]] }
- when Message::Quote
- return [[[:quote_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
- case state
- when :closed
- [[[:quote_patina_color, "#{prefix}+ (#{chunk.lines.length} quoted lines)"]]]
- when :open
- [[[:quote_patina_color, "#{prefix}- (#{chunk.lines.length} quoted lines)"]]] + chunk.lines.map { |line| [[:quote_color, "#{prefix}#{line}"]] }
- end
- when Message::Signature
- return [[[:sig_patina_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
- case state
- when :closed
- [[[:sig_patina_color, "#{prefix}+ (#{chunk.lines.length}-line signature)"]]]
- when :open
- [[[:sig_patina_color, "#{prefix}- (#{chunk.lines.length}-line signature)"]]] + chunk.lines.map { |line| [[:sig_color, "#{prefix}#{line}"]] }
- end
- when CryptoSignature, CryptoDecryptedNotice
- color =
- case chunk.status
- when :valid: :cryptosig_valid_color
- when :invalid: :cryptosig_invalid_color
- else :cryptosig_unknown_color
- end
- widget = chunk.lines.empty? ? "x" : (state == :closed ? "+" : "-")
- case state
- when :closed
- [[[color, "#{prefix}#{widget} #{chunk.description}"]]]
- when :open
- [[[color, "#{prefix}#{widget} #{chunk.description}"]]] +
- chunk.lines.map { |line| [[color, "#{prefix}#{line}"]] }
- end
+
else
- raise "unknown chunk type #{chunk.class.name}"
+ raise "Bad chunk: #{chunk.inspect}" unless chunk.respond_to?(:inlineable?) ## debugging
+ if chunk.inlineable?
+ chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
+ elsif chunk.expandable?
+ case state
+ when :closed
+ [[[chunk.patina_color, "#{prefix}+ #{chunk.patina_text}"]]]
+ when :open
+ [[[chunk.patina_color, "#{prefix}- #{chunk.patina_text}"]]] + chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
+ end
+ else
+ [[[chunk.patina_color, "#{prefix}x #{chunk.patina_text}"]]]
+ end
end
end
- def view_attachment a
- BufferManager.flash "viewing #{a.content_type} attachment..."
- success = a.view!
+ def view chunk
+ BufferManager.flash "viewing #{chunk.content_type} attachment..."
+ success = chunk.view!
BufferManager.erase_flash
BufferManager.completely_redraw_screen
unless success
- BufferManager.spawn "Attachment: #{a.filename}", TextMode.new(a.to_s)
+ BufferManager.spawn "Attachment: #{chunk.filename}", TextMode.new(chunk.to_s)
BufferManager.flash "Couldn't execute view command, viewing as text."
end
end