Class: RIMS::RFC822::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rims/rfc822.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg_txt, charset_aliases: DEFAULT_CHARSET_ALIASES) ⇒ Message

Returns a new instance of Message.



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/rims/rfc822.rb', line 583

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

Instance Attribute Details

#raw_sourceObject (readonly)

Returns the value of attribute raw_source.



606
607
608
# File 'lib/rims/rfc822.rb', line 606

def raw_source
  @raw_source
end

Instance Method Details

#bccObject



837
838
839
# File 'lib/rims/rfc822.rb', line 837

def bcc
  mail_address_header_field('bcc')
end

#bodyObject



624
625
626
627
# File 'lib/rims/rfc822.rb', line 624

def body
  setup_message
  @body
end

#boundaryObject



686
687
688
# File 'lib/rims/rfc822.rb', line 686

def boundary
  content_type_parameter('boundary')
end

#ccObject



833
834
835
# File 'lib/rims/rfc822.rb', line 833

def cc
  mail_address_header_field('cc')
end

#charsetObject



682
683
684
# File 'lib/rims/rfc822.rb', line 682

def charset
  content_type_parameter('charset')
end

#content_dispositionObject



701
702
703
704
# File 'lib/rims/rfc822.rb', line 701

def content_disposition
  setup_content_disposition
  @content_disposition && @content_disposition[0]
end

#content_disposition_parameter(name) ⇒ Object



710
711
712
713
714
715
716
717
# File 'lib/rims/rfc822.rb', line 710

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_listObject Also known as: content_disposition_parameters



719
720
721
722
# File 'lib/rims/rfc822.rb', line 719

def content_disposition_parameter_list
  setup_content_type
  @content_disposition && @content_disposition[1].values
end

#content_disposition_upcaseObject



706
707
708
# File 'lib/rims/rfc822.rb', line 706

def content_disposition_upcase
  content_disposition&.upcase
end

#content_languageObject



739
740
741
742
# File 'lib/rims/rfc822.rb', line 739

def content_language
  setup_content_language
  @content_language
end

#content_language_upcaseObject



744
745
746
# File 'lib/rims/rfc822.rb', line 744

def content_language_upcase
  content_language&.map{|tag| tag.upcase }
end

#content_typeObject



647
648
649
# File 'lib/rims/rfc822.rb', line 647

def content_type
  "#{media_main_type}/#{media_sub_type}"
end

#content_type_parameter(name) ⇒ Object



668
669
670
671
672
673
# File 'lib/rims/rfc822.rb', line 668

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_listObject Also known as: content_type_parameters



675
676
677
678
# File 'lib/rims/rfc822.rb', line 675

def content_type_parameter_list
  setup_content_type
  @content_type[2].values
end

#content_type_upcaseObject



663
664
665
666
# File 'lib/rims/rfc822.rb', line 663

def content_type_upcase
  # not return `nil'
  content_type.upcase
end

#dateObject



786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/rims/rfc822.rb', line 786

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

#fromObject



817
818
819
# File 'lib/rims/rfc822.rb', line 817

def from
  mail_address_header_field('from')
end

#headerObject



619
620
621
622
# File 'lib/rims/rfc822.rb', line 619

def header
  setup_message
  @header
end

#media_main_typeObject



635
636
637
638
# File 'lib/rims/rfc822.rb', line 635

def media_main_type
  setup_content_type
  @content_type[0]
end

#media_main_type_upcaseObject



651
652
653
654
# File 'lib/rims/rfc822.rb', line 651

def media_main_type_upcase
  # not return `nil'
  media_main_type.upcase
end

#media_sub_typeObject Also known as: media_subtype



640
641
642
643
# File 'lib/rims/rfc822.rb', line 640

def media_sub_type
  setup_content_type
  @content_type[1]
end

#media_sub_type_upcaseObject Also known as: media_subtype_upcase



656
657
658
659
# File 'lib/rims/rfc822.rb', line 656

def media_sub_type_upcase
  # not return `nil'
  media_sub_type.upcase
end

#messageObject



776
777
778
779
780
781
782
783
784
# File 'lib/rims/rfc822.rb', line 776

def message
  if (@message.nil?) then
    if (message?) then
      @message = Message.new(body.raw_source)
    end
  end

  @message
end

#message?Boolean

Returns:

  • (Boolean)


772
773
774
# File 'lib/rims/rfc822.rb', line 772

def message?
  media_main_type_upcase == 'MESSAGE'
end

#mime_binary_body_stringObject



905
906
907
# File 'lib/rims/rfc822.rb', line 905

def mime_binary_body_string
  mime_charset_body_text(Encoding::ASCII_8BIT)
end

#mime_charset_body_text(charset = nil) ⇒ Object



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/rims/rfc822.rb', line 885

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: {}) ⇒ Object



850
851
852
853
854
855
856
857
858
859
860
# File 'lib/rims/rfc822.rb', line 850

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: {}) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/rims/rfc822.rb', line 862

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: {}) ⇒ Object



876
877
878
879
880
881
882
883
# File 'lib/rims/rfc822.rb', line 876

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?Boolean

Returns:

  • (Boolean)


752
753
754
# File 'lib/rims/rfc822.rb', line 752

def multipart?
  media_main_type_upcase == 'MULTIPART'
end

#partsObject



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/rims/rfc822.rb', line 756

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_toObject



825
826
827
# File 'lib/rims/rfc822.rb', line 825

def reply_to
  mail_address_header_field('reply-to')
end

#senderObject



821
822
823
# File 'lib/rims/rfc822.rb', line 821

def sender
  mail_address_header_field('sender')
end

#setup_content_dispositionObject



690
691
692
693
694
695
696
697
698
# File 'lib/rims/rfc822.rb', line 690

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?Boolean

Returns:

  • (Boolean)


748
749
750
# File 'lib/rims/rfc822.rb', line 748

def text?
  media_main_type_upcase == 'TEXT'
end

#toObject



829
830
831
# File 'lib/rims/rfc822.rb', line 829

def to
  mail_address_header_field('to')
end