Module: RIMS::RFC822::Parse
- Included in:
- RIMS::RFC822
- Defined in:
- lib/rims/rfc822.rb
Defined Under Namespace
Classes: Address
Class Method Summary collapse
- .parse_content_disposition(disposition_txt) ⇒ Object
- .parse_content_language(language_tags_txt) ⇒ Object
- .parse_content_type(type_txt) ⇒ Object
- .parse_header(header_txt) ⇒ Object
- .parse_mail_address_list(address_list_txt) ⇒ Object
- .parse_multipart_body(boundary, body_txt) ⇒ Object
- .parse_parameters(parameters_txt) ⇒ Object
- .split_message(msg_txt) ⇒ Object
- .split_parameters(type_params_txt) ⇒ Object
- .unquote_phrase(phrase_txt) ⇒ Object
Class Method Details
.parse_content_disposition(disposition_txt) ⇒ Object
172 173 174 |
# File 'lib/rims/rfc822.rb', line 172 def parse_content_disposition(disposition_txt) split_parameters(disposition_txt) end |
.parse_content_language(language_tags_txt) ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rims/rfc822.rb', line 177 def parse_content_language() tag_list = .split(',') for tag in tag_list tag.strip! tag.freeze end tag_list.reject!(&:empty?) tag_list.freeze end |
.parse_content_type(type_txt) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rims/rfc822.rb', line 139 def parse_content_type(type_txt) media_type_txt, params = split_parameters(type_txt) if (media_type_txt) then main_type, sub_type = media_type_txt.split('/', 2) if (main_type) then main_type.strip! main_type.freeze if (sub_type) then sub_type.strip! sub_type.freeze if (! main_type.empty? && ! sub_type.empty?) then return [ main_type, sub_type, params ].freeze end end end end # See RFC2045 / 5.2. Content-Type Defaults # <https://tools.ietf.org/html/rfc2045#section-5.2> # # Default RFC 822 messages without a MIME Content-Type header are taken # by this protocol to be plain text in the US-ASCII character set, # which can be explicitly specified as: # # Content-type: text/plain; charset=us-ascii # [ 'text'.dup.force_encoding(type_txt.encoding).freeze, 'plain'.dup.force_encoding(type_txt.encoding).freeze, params # default is no charset, it will be `ASCII-8BIT'. ].freeze end |
.parse_header(header_txt) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rims/rfc822.rb', line 23 def parse_header(header_txt) field_pair_list = header_txt.scan(%r{ ((?#name) \S+? ) \s* : \s* ( (?#value) .*? (?: \R|\z) (?: \s .*? (?: \R|\z) )* ) }x) for name, value in field_pair_list value.strip! name.freeze value.freeze end field_pair_list.freeze end |
.parse_mail_address_list(address_list_txt) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rims/rfc822.rb', line 219 def parse_mail_address_list(address_list_txt) addr_list = [] src_txt = address_list_txt.dup while (true) if (src_txt.sub!(%r{ \A \s* (?<display_name> \S.*? ) \s* : (?<group_list> .*? ) ; \s* ,? }x, '')) then display_name = $~[:display_name] group_list = $~[:group_list] addr_list << Address.new(nil, nil, unquote_phrase(display_name), nil).freeze addr_list.concat(parse_mail_address_list(group_list)) addr_list << Address.new(nil, nil, nil, nil).freeze elsif (src_txt.sub!(%r{ \A \s* (?<local_part> [^<>@",\s]+ ) \s* @ \s* (?<domain> [^<>@",\s]+ ) \s* ,? }x, '')) then addr_list << Address.new(nil, nil, $~[:local_part].freeze, $~[:domain].freeze).freeze elsif (src_txt.sub!(%r{ \A \s* (?<display_name> \S.*? ) \s* < \s* (?: (?<route> @[^<>@",]* (?: , \s* @[^<>@",]* )* ) \s* : )? \s* (?<local_part> [^<>@",\s]+ ) \s* @ \s* (?<domain> [^<>@",\s]+ ) \s* > \s* ,? }x, '')) then display_name = $~[:display_name] route = $~[:route] local_part = $~[:local_part] domain = $~[:domain] addr_list << Address.new(unquote_phrase(display_name), route.freeze, local_part.freeze, domain.freeze).freeze else break end end addr_list.freeze end |
.parse_multipart_body(boundary, body_txt) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rims/rfc822.rb', line 189 def parse_multipart_body(boundary, body_txt) delim = '--' + boundary term = delim + '--' body_txt2, _body_epilogue_txt = body_txt.split(term, 2) if (body_txt2) then _body_preamble_txt, body_parts_txt = body_txt2.split(delim, 2) if (body_parts_txt) then part_list = body_parts_txt.split(delim, -1) for part_txt in part_list part_txt.lstrip! part_txt.chomp!("\n") part_txt.chomp!("\r") part_txt.freeze end return part_list.freeze end end [].freeze end |
.parse_parameters(parameters_txt) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rims/rfc822.rb', line 96 def parse_parameters(parameters_txt) params = {} parameters_txt.scan(%r{ (?<name> \S+? ) \s* = \s* (?: (?<quoted_string> ".*?" ) | (?<token> \S+? ) ) \s* (?: ; | \Z ) }x) do name = $~[:name] if ($~[:quoted_string]) then quoted_value = $~[:quoted_string] value = unquote_phrase(quoted_value) else value = $~[:token] end params[name.downcase.freeze] = [ name.freeze, value.freeze ].freeze end params.freeze end |
.split_message(msg_txt) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rims/rfc822.rb', line 10 def (msg_txt) header_txt, body_txt = msg_txt.lstrip.split(/\R\R/, 2) if ($&) then header_txt << $& else body_txt = header_txt header_txt = nil end [ header_txt.freeze, body_txt.freeze ].freeze end |
.split_parameters(type_params_txt) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rims/rfc822.rb', line 122 def split_parameters(type_params_txt) type, params_txt = type_params_txt.split(';', 2) if (type) then type.strip! type.freeze if (params_txt) then params = parse_parameters(params_txt) else params = {}.freeze end [ type, params ].freeze else [ nil, {}.freeze ].freeze end end |
.unquote_phrase(phrase_txt) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rims/rfc822.rb', line 44 def unquote_phrase(phrase_txt) state = :raw src_txt = phrase_txt.dup dst_txt = ''.encode(phrase_txt.encoding) while (src_txt.sub!(/\A (?: " | \( | \) | \\ | [^"\(\)\\]+ )/x, '')) match_txt = $& case (state) when :raw case (match_txt) when '"' state = :quote when '(' state = :comment when "\\" unless (src_txt.empty?) then dst_txt << src_txt[0] src_txt[0] = '' end else dst_txt << match_txt end when :quote case (match_txt) when '"' state = :raw when "\\" unless (src_txt.empty?) then dst_txt << src_txt[0] src_txt[0] = '' end else dst_txt << match_txt end when :comment case (match_txt) when ')' state = :raw when "\\" src_txt[0] = '' else # ignore comment text. end else raise "internal error - unknown state: #{state}" end end dst_txt.freeze end |