class Mournmail::MessageMode

Constants

MAILTO_REGEXP

See nihongo.jp/support/mail_guide/dev_guide.txt

MESSAGE_MODE_MAP
MIME_REGEXP
URI_OR_MIME_REGEXP
URI_REGEXP

Public Class Methods

new(buffer) click to toggle source
Calls superclass method
# File lib/mournmail/message_mode.rb, line 24
def initialize(buffer)
  super(buffer)
  buffer.keymap = MESSAGE_MODE_MAP
  @attached_file = nil
end

Private Instance Methods

current_part() click to toggle source
# File lib/mournmail/message_mode.rb, line 68
def current_part
  @buffer.save_excursion do
    @buffer.beginning_of_line
    if @buffer.looking_at?(/\[([0-9.]+) .*\]/)
      index = match_string(1)
      indices = index.split(".").map(&:to_i)
      @buffer[:mournmail_mail].dig_part(*indices)
    else
      nil
    end
  end
end
current_uri() click to toggle source
# File lib/mournmail/message_mode.rb, line 114
def current_uri
  @buffer.save_excursion do
    pos = @buffer.point
    @buffer.beginning_of_line
    pos2 = @buffer.re_search_forward(URI_REGEXP, raise_error: false)
    if pos2 && match_beginning(0) <= pos && pos < match_end(0)
      match_string(0)
    else
      nil
    end
  end
end
open_part(part) click to toggle source
# File lib/mournmail/message_mode.rb, line 127
def open_part(part)
  if part.multipart?
    raise EditorError, "Can't open a multipart entity."
  end
  ext = part_file_name(part).slice(/\.([^.]+)\z/, 1)
  if ext
    file_name = ["mournmail", "." + ext]
  else
    file_name = "mournmail"
  end
  @attached_file = Tempfile.open(file_name, binmode: true)
  s = part.decoded
  if part.content_type == "text/html"
    s = s.sub(/<meta http-equiv="content-type".*?>/i, "")
  elsif part.charset
    s = s.encode(part.charset)
  end
  @attached_file.write(s)
  @attached_file.close
  if part.main_type == "text" && part.sub_type != "html"
    find_file(@attached_file.path)
  else
    background do
      system(*CONFIG[:mournmail_file_open_comamnd], @attached_file.path,
             out: File::NULL, err: File::NULL)
    end
  end
end
open_uri(uri) click to toggle source
# File lib/mournmail/message_mode.rb, line 156
def open_uri(uri)
  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
part_default_file_name(part) click to toggle source
# File lib/mournmail/message_mode.rb, line 94
def part_default_file_name(part)
  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
  end
end
part_extension(part) click to toggle source
# File lib/mournmail/message_mode.rb, line 109
def part_extension(part)
  mime_type = part["content-type"].string
  MIME::Types[mime_type]&.first&.preferred_extension
end
part_file_name(part) click to toggle source
# File lib/mournmail/message_mode.rb, line 81
def part_file_name(part)
  file_name =
    (part["content-disposition"]&.parameters&.[]("filename") rescue nil) ||
    (part["content-type"]&.parameters&.[]("name") rescue nil) ||
    part_default_file_name(part)
  decoded_file_name = Mail::Encodings.decode_encode(file_name, :decode)
  if /\A([A-Za-z0-9_\-]+)'(?:[A-Za-z0-9_\-])*'(.*)/ =~ decoded_file_name
    $2.encode("utf-8", $1)
  else
    decoded_file_name
  end
end