Module: Rfc2047
- Defined in:
- lib/rfc2047.rb
Overview
from blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/69323 via dev.joyent.com/projects/connector/browse/trunk/vendor/rfc2047.rb
An implementation of RFC 2047 decoding.
This module depends on the iconv library by Nobuyoshi Nakada, which I’ve heard may be distributed as a standard part of Ruby 1.8.
Copyright © Sam Roberts <sroberts / uniserve.com> 2003 (with modifications).
This file is distributed under the same terms as Ruby.
Defined Under Namespace
Classes: Unparseable
Constant Summary collapse
- WORD =
:nodoc:
/=\?([!#$\%&'*+-\/0-9A-Z\\^\`a-z{|}~]+)\?([BbQq])\?([!->@-~]+)\?=/
- ADJACENT_WORDS =
Look for two adjacent words in the same encoding.
/(#{WORD})[\s\r\n]+(?==\?(\2)\?([BbQq])\?)/
Class Method Summary collapse
-
.decode(from, target = 'utf-8') ⇒ Object
Decodes a string,
from
, containing RFC 2047 encoded words into a target character set,target
defaulting to utf-8.
Class Method Details
.decode(from, target = 'utf-8') ⇒ Object
Decodes a string, from
, containing RFC 2047 encoded words into a target character set, target
defaulting to utf-8. See iconv_open(3) for information on the supported target encodings. If one of the encoded words cannot be converted to the target encoding, it is left in its encoded form.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rfc2047.rb', line 24 def self.decode(from, target='utf-8') from.gsub(ADJACENT_WORDS, "\\1").gsub(WORD) do |word| cs = $1 encoding = $2 text = $3 # B64 or QP decode, as necessary: case encoding.downcase when 'b' text = text.unpack('m*')[0] when 'q' # RFC 2047 has a variant of quoted printable where a ' ' character # can be represented as an '_', rather than =32, so convert # any of these that we find before doing the QP decoding. text = text.tr("_", " ") text = text.unpack('M*')[0] else raise Unparseable, from end # Convert # # Remember: Iconv.open(to, from) begin text = text.encode(target, cs) rescue raise Unparseable, from end end end |