lib/mournmail/message_mode.rb in mournmail-0.2.0 vs lib/mournmail/message_mode.rb in mournmail-0.3.0

- old
+ new

@@ -1,22 +1,26 @@ -# frozen_string_literal: true +require "uri" +require "mime/types" using Mournmail::MessageRendering module Mournmail class MessageMode < Textbringer::Mode MESSAGE_MODE_MAP = Keymap.new MESSAGE_MODE_MAP.define_key("\C-m", :message_open_link_or_part_command) MESSAGE_MODE_MAP.define_key("s", :message_save_part_command) + MESSAGE_MODE_MAP.define_key("\t", :message_next_link_or_part_command) # See http://nihongo.jp/support/mail_guide/dev_guide.txt - URI_REGEXP = /(https?|ftp):\/\/[^  \t\n>)"]*[^]  \t\n>.,:)"]+/ + MAILTO_REGEXP = URI.regexp("mailto") + URI_REGEXP = /(https?|ftp):\/\/[^  \t\n>)"]*[^]  \t\n>.,:)"]+|#{MAILTO_REGEXP}/ + MIME_REGEXP = /^\[(([\-0-9.]+) [A-Za-z._\-]+\/[A-Za-z._\-]+.*|PGP\/MIME .*)\]$/ + URI_OR_MIME_REGEXP = /#{URI_REGEXP}|#{MIME_REGEXP}/ define_syntax :field_name, /^[A-Za-z\-]+: / define_syntax :quotation, /^>.*/ - define_syntax :mime_part, - /^\[(([0-9.]+) [A-Za-z._\-]+\/[A-Za-z._\-]+.*|PGP\/MIME .*)\]$/ + define_syntax :mime_part, MIME_REGEXP define_syntax :link, URI_REGEXP def initialize(buffer) super(buffer) buffer.keymap = MESSAGE_MODE_MAP @@ -44,16 +48,25 @@ if !File.exist?(path) || yes_or_no?("File exists; overwrite?") File.write(path, part.decoded) end end + define_local_command(:message_next_link_or_part, + doc: "Go to the next link or MIME part.") do + if @buffer.re_search_forward(URI_OR_MIME_REGEXP, raise_error: false) + goto_char(@buffer.match_beginning(0)) + else + @buffer.beginning_of_buffer + end + end + private def current_part @buffer.save_excursion do @buffer.beginning_of_line - if @buffer.looking_at?(/\[([0-9.]+) .*\]/) + if @buffer.looking_at?(/\[([\-0-9.]+) .*\]/) index = match_string(1) indices = index.split(".").map(&:to_i) @buffer[:mournmail_mail].dig_part(*indices) else nil @@ -73,11 +86,16 @@ decoded_file_name end end def part_default_file_name(part) - base_name = part.cid.gsub(/[^A-Za-z0-9_\-]/, "_") + base_name = + begin + part.cid.gsub(/[^A-Za-z0-9_\-]/, "_") + rescue NoMethodError + "mournmail" + end ext = part_extension(part) if ext base_name + "." + ext else base_name @@ -124,10 +142,26 @@ end end end def open_uri(uri) - system(*CONFIG[:mournmail_link_open_comamnd], uri, - out: File::NULL, err: File::NULL) + case uri + when /\Amailto:/ + u = URI.parse(uri) + if u.headers.assoc("subject") + re = /^To:\s*\nSubject:\s*\n/ + else + re = /^To:\s*\n/ + end + Commands.mail + beginning_of_buffer + re_search_forward(re) + replace_match("") + insert u.to_mailtext.sub(/\n\n\z/, "") + end_of_buffer + else + system(*CONFIG[:mournmail_link_open_comamnd], uri, + out: File::NULL, err: File::NULL) + end end end end