class RIMS::RFC822::Message

Attributes

raw_source[R]

Public Class Methods

new(msg_txt, charset_aliases: DEFAULT_CHARSET_ALIASES) click to toggle source
# File lib/rims/rfc822.rb, line 582
def initialize(msg_txt, charset_aliases: DEFAULT_CHARSET_ALIASES)
  @raw_source = msg_txt.dup.freeze
  @charset_aliases = charset_aliases
  @header = nil
  @body = nil
  @content_type = nil
  @content_disposition = nil
  @content_language = nil
  @parts = nil
  @message = nil
  @date = nil
  @from = nil
  @sender = nil
  @reply_to = nil
  @to = nil
  @cc = nil
  @bcc = nil
  @mime_decoded_header_cache = nil
  @mime_decoded_header_field_value_list_cache = nil
  @mime_decoded_header_text_cache = nil
  @mime_charset_body_text_cache = nil
end

Public Instance Methods

bcc() click to toggle source
# File lib/rims/rfc822.rb, line 836
def bcc
  mail_address_header_field('bcc')
end
body() click to toggle source
# File lib/rims/rfc822.rb, line 623
def body
  setup_message
  @body
end
boundary() click to toggle source
# File lib/rims/rfc822.rb, line 685
def boundary
  content_type_parameter('boundary')
end
cc() click to toggle source
# File lib/rims/rfc822.rb, line 832
def cc
  mail_address_header_field('cc')
end
charset() click to toggle source
# File lib/rims/rfc822.rb, line 681
def charset
  content_type_parameter('charset')
end
content_disposition() click to toggle source
# File lib/rims/rfc822.rb, line 700
def content_disposition
  setup_content_disposition
  @content_disposition && @content_disposition[0]
end
content_disposition_parameter(name) click to toggle source
# File lib/rims/rfc822.rb, line 709
def content_disposition_parameter(name)
  setup_content_disposition
  if (@content_disposition) then
    if (name_value_pair = @content_disposition[1][name.downcase]) then
      name_value_pair[1]
    end
  end
end
content_disposition_parameter_list() click to toggle source
# File lib/rims/rfc822.rb, line 718
def content_disposition_parameter_list
  setup_content_type
  @content_disposition && @content_disposition[1].values
end
content_disposition_parameters()
content_disposition_upcase() click to toggle source
# File lib/rims/rfc822.rb, line 705
def content_disposition_upcase
  content_disposition&.upcase
end
content_language() click to toggle source
# File lib/rims/rfc822.rb, line 738
def content_language
  setup_content_language
  @content_language
end
content_language_upcase() click to toggle source
# File lib/rims/rfc822.rb, line 743
def content_language_upcase
  content_language&.map{|tag| tag.upcase }
end
content_type() click to toggle source
# File lib/rims/rfc822.rb, line 646
def content_type
  "#{media_main_type}/#{media_sub_type}"
end
content_type_parameter(name) click to toggle source
# File lib/rims/rfc822.rb, line 667
def content_type_parameter(name)
  setup_content_type
  if (name_value_pair = @content_type[2][name.downcase]) then
    name_value_pair[1]
  end
end
content_type_parameter_list() click to toggle source
# File lib/rims/rfc822.rb, line 674
def content_type_parameter_list
  setup_content_type
  @content_type[2].values
end
Also aliased as: content_type_parameters
content_type_parameters()
content_type_upcase() click to toggle source
# File lib/rims/rfc822.rb, line 662
def content_type_upcase
  # not return `nil'
  content_type.upcase
end
date() click to toggle source
# File lib/rims/rfc822.rb, line 785
def date
  if (@date.nil?) then
    if (header.key? 'Date') then
      begin
        @date = Time.parse(header['Date'])
      rescue ArgumentError
        @date = Time.at(0)
      end
      @date.freeze
    end
  end

  @date
end
from() click to toggle source
# File lib/rims/rfc822.rb, line 816
def from
  mail_address_header_field('from')
end
header() click to toggle source
# File lib/rims/rfc822.rb, line 618
def header
  setup_message
  @header
end
media_main_type() click to toggle source
# File lib/rims/rfc822.rb, line 634
def media_main_type
  setup_content_type
  @content_type[0]
end
media_main_type_upcase() click to toggle source
# File lib/rims/rfc822.rb, line 650
def media_main_type_upcase
  # not return `nil'
  media_main_type.upcase
end
media_sub_type() click to toggle source
# File lib/rims/rfc822.rb, line 639
def media_sub_type
  setup_content_type
  @content_type[1]
end
Also aliased as: media_subtype
media_sub_type_upcase() click to toggle source
# File lib/rims/rfc822.rb, line 655
def media_sub_type_upcase
  # not return `nil'
  media_sub_type.upcase
end
Also aliased as: media_subtype_upcase
media_subtype()
Alias for: media_sub_type
media_subtype_upcase()
message() click to toggle source
# File lib/rims/rfc822.rb, line 775
def message
  if (@message.nil?) then
    if (message?) then
      @message = Message.new(body.raw_source)
    end
  end

  @message
end
message?() click to toggle source
# File lib/rims/rfc822.rb, line 771
def message?
  media_main_type_upcase == 'MESSAGE'
end
mime_binary_body_string() click to toggle source
# File lib/rims/rfc822.rb, line 904
def mime_binary_body_string
  mime_charset_body_text(Encoding::ASCII_8BIT)
end
mime_charset_body_text(charset=nil) click to toggle source
# File lib/rims/rfc822.rb, line 884
def mime_charset_body_text(charset=nil)
  @mime_charset_body_text_cache ||= {}
  unless (charset) then
    unless (@mime_charset_body_text_cache.key? :default) then
      charset = (text?) ? self.charset : Encoding::ASCII_8BIT
      @mime_charset_body_text_cache[:default] = CharsetText.get_mime_charset_text(body.raw_source,
                                                                                  charset,
                                                                                  header['Content-Transfer-Encoding'],
                                                                                  charset_aliases: @charset_aliases)
    end
    @mime_charset_body_text_cache[:default]
  else
    cache_key = make_charset_key(charset)
    @mime_charset_body_text_cache[cache_key] ||= CharsetText.get_mime_charset_text(body.raw_source,
                                                                                   charset,
                                                                                   header['Content-Transfer-Encoding'],
                                                                                   charset_aliases: @charset_aliases)
  end
end
mime_decoded_header(name, decode_charset=nil, charset_convert_options: {}) click to toggle source
# File lib/rims/rfc822.rb, line 849
def mime_decoded_header(name, decode_charset=nil, charset_convert_options: {})
  cache_key = [
    name.downcase.freeze,
    (decode_charset) ? make_charset_key(decode_charset) : :default
  ].freeze
  @mime_decoded_header_cache ||= {}
  @mime_decoded_header_cache[cache_key] ||= CharsetText.decode_mime_encoded_words(header[name],
                                                                                  decode_charset,
                                                                                  charset_aliases: @charset_aliases,
                                                                                  charset_convert_options: charset_convert_options)
end
mime_decoded_header_field_value_list(name, decode_charset=nil, charset_convert_options: {}) click to toggle source
# File lib/rims/rfc822.rb, line 861
def mime_decoded_header_field_value_list(name, decode_charset=nil, charset_convert_options: {})
  cache_key = [
    name.downcase.freeze,
    (decode_charset) ? make_charset_key(decode_charset) : :default
  ].freeze
  @mime_decoded_header_field_value_list_cache ||= {}
  @mime_decoded_header_field_value_list_cache[cache_key] ||= header.field_value_list(name).map{|field_value|
    CharsetText.decode_mime_encoded_words(field_value,
                                          decode_charset,
                                          charset_aliases: @charset_aliases,
                                          charset_convert_options: charset_convert_options)
  }.freeze
end
mime_decoded_header_text(decode_charset=nil, charset_convert_options: {}) click to toggle source
# File lib/rims/rfc822.rb, line 875
def mime_decoded_header_text(decode_charset=nil, charset_convert_options: {})
  cache_key = (decode_charset) ? make_charset_key(decode_charset) : :default
  @mime_decoded_header_text_cache ||= {}
  @mime_decoded_header_text_cache[cache_key] ||= CharsetText.decode_mime_encoded_words(header.raw_source,
                                                                                       decode_charset,
                                                                                       charset_aliases: @charset_aliases,
                                                                                       charset_convert_options: charset_convert_options)
end
multipart?() click to toggle source
# File lib/rims/rfc822.rb, line 751
def multipart?
  media_main_type_upcase == 'MULTIPART'
end
parts() click to toggle source
# File lib/rims/rfc822.rb, line 755
def parts
  if (@parts.nil?) then
    if (multipart?) then
      if (boundary = self.boundary) then
        part_list = Parse.parse_multipart_body(boundary, body.raw_source)
        @parts = part_list.map{|msg_txt| Message.new(msg_txt) }
      else
        @parts = []
      end
      @parts.freeze
    end
  end

  @parts
end
reply_to() click to toggle source
# File lib/rims/rfc822.rb, line 824
def reply_to
  mail_address_header_field('reply-to')
end
sender() click to toggle source
# File lib/rims/rfc822.rb, line 820
def sender
  mail_address_header_field('sender')
end
setup_content_disposition() click to toggle source
# File lib/rims/rfc822.rb, line 689
def setup_content_disposition
  if (@content_disposition.nil?) then
    if (header.key? 'Content-Disposition') then
      @content_disposition = Parse.parse_content_disposition(header['Content-Disposition'])
    end
  end

  nil
end
text?() click to toggle source
# File lib/rims/rfc822.rb, line 747
def text?
  media_main_type_upcase == 'TEXT'
end
to() click to toggle source
# File lib/rims/rfc822.rb, line 828
def to
  mail_address_header_field('to')
end

Private Instance Methods

mail_address_header_field(field_name) click to toggle source
# File lib/rims/rfc822.rb, line 800
def mail_address_header_field(field_name)
  if (header.key? field_name) then
    ivar_name = '@' + field_name.downcase.tr('-', '_')
    addr_list = instance_variable_get(ivar_name)
    if (addr_list.nil?) then
      addr_list = header.field_value_list(field_name).map{|addr_list_txt| Parse.parse_mail_address_list(addr_list_txt) }
      addr_list.flatten!
      addr_list.freeze
      instance_variable_set(ivar_name, addr_list)
    end

    addr_list
  end
end
make_charset_key(charset) click to toggle source
# File lib/rims/rfc822.rb, line 840
def make_charset_key(charset)
  if (charset.is_a? Encoding) then
    charset
  else
    charset.downcase.freeze
  end
end
setup_content_language() click to toggle source
# File lib/rims/rfc822.rb, line 725
def setup_content_language
  if (@content_language.nil?) then
    if (header.key? 'Content-Language') then
      @content_language = header.field_value_list('Content-Language').map{|tags_txt| Parse.parse_content_language(tags_txt) }
      @content_language.flatten!
      @content_language.freeze
    end
  end

  nil
end
setup_content_type() click to toggle source
# File lib/rims/rfc822.rb, line 628
def setup_content_type
  @content_type ||= Parse.parse_content_type(header['Content-Type'] || '')
  nil
end
setup_message() click to toggle source
# File lib/rims/rfc822.rb, line 607
def setup_message
  if (@header.nil? || @body.nil?) then
    header_txt, body_txt = Parse.split_message(@raw_source)
    @header = Header.new(header_txt || '')
    @body = Body.new(body_txt || '')
  end

  nil
end