405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/rims/rfc822.rb', line 405
def decode_mime_encoded_words(encoded_string, decode_charset=nil, charset_aliases: DEFAULT_CHARSET_ALIASES, charset_convert_options: {})
src = encoded_string
dst = ''.dup
charset_convert_options ||= {}
if (decode_charset) then
if (decode_charset.is_a? Encoding) then
decode_charset_encoding = decode_charset
else
decode_charset_encoding = charset_aliases[decode_charset] ||
Encoding.find(decode_charset) end
dst.force_encoding(decode_charset_encoding)
else
dst.force_encoding(encoded_string.encoding)
end
while (src =~ %r{
=\? [^\s?]+ \? [BQ] \? [^\s?]+ \?=
(?:
\s+
=\? [^\s?]+ \? [BQ] \? [^\s?]+ \?=
)*
}ix)
src = $'
foreword = $`
encoded_word_list = $&.split(/\s+/, -1)
unless (foreword.empty?) then
if (Encoding.compatible? dst, foreword) then
foreword.encode!(dst.encoding, **charset_convert_options)
end
dst << foreword
end
for encoded_word in encoded_word_list
_, charset, encoding, encoded_text, _ = encoded_word.split('?', 5)
encoding.upcase!
encoded_text.tr!('_', ' ') if (encoding == 'Q')
transfer_encoding = ENCODED_WORD_TRANSFER_ENCODING_TABLE[encoding] or raise "internal error - unknown encoding: #{encoding}"
decoded_text = get_mime_charset_text(encoded_text, charset, transfer_encoding, charset_aliases: charset_aliases)
if (decode_charset_encoding) then
if (decoded_text.encoding != decode_charset_encoding) then
decoded_text = decoded_text.encode(decode_charset_encoding, **charset_convert_options)
end
end
unless (Encoding.compatible? dst, decoded_text) then
if (dst.ascii_only?) then
dst.encode!(decoded_text.encoding, **charset_convert_options)
else
decoded_text = decoded_text.encode(dst.encoding, **charset_convert_options)
end
end
dst << decoded_text
end
end
unless (src.empty?) then
unless (Encoding.compatible? dst, src) then
src = src.encode(dst.encoding, **charset_convert_options) end
dst << src
end
dst.freeze
end
|