rfc2047 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +15 -0
  2. data/lib/rfc2047.rb +54 -0
  3. metadata +63 -0
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ The rfc2047 ruby gem decodes email header fields that have been encoded according the the rules layed forth by [RFC 2047][1].
2
+
3
+ Rfc2047.decode("=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=")
4
+ # => "If you can read this yo u understand the example."
5
+
6
+ It is released under the same [terms][2] as Ruby itself.
7
+
8
+ It was originally written by Sam Roberts in 2003, and the current version was taken from the [Joyent Connector][3], who cite the original [mailing list post][4] as the source. It was packaged as a gem in 2011, and now resides on [Rubygems][5], with the source at [GitHub][6].
9
+
10
+ [1]: http://www.ietf.org/rfc/rfc2047.txt
11
+ [2]: http://www.ruby-lang.org/en/LICENSE.txt
12
+ [3]: http://dev.joyent.com/projects/connector/browse/trunk/vendor/rfc2047.rb
13
+ [4]: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/69323
14
+ [5]: https://rubygems.org/gems/rfc2047
15
+ [6]: https://www.github.com/ConradIrwin/rfc2047-ruby/
data/lib/rfc2047.rb ADDED
@@ -0,0 +1,54 @@
1
+ # from http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/69323
2
+ # via http://dev.joyent.com/projects/connector/browse/trunk/vendor/rfc2047.rb
3
+ #
4
+ # An implementation of RFC 2047 decoding.
5
+ #
6
+ # This module depends on the iconv library by Nobuyoshi Nakada, which I've
7
+ # heard may be distributed as a standard part of Ruby 1.8.
8
+ #
9
+ # Copyright (c) Sam Roberts <sroberts / uniserve.com> 2003 (with modifications).
10
+ #
11
+ # This file is distributed under the same terms as Ruby.
12
+
13
+ require 'iconv'
14
+
15
+ module Rfc2047
16
+
17
+ WORD = /=\?([!#$\%&'*+-\/0-9A-Z\\^\`a-z{|}~]+)\?([BbQq])\?([!->@-~]+)\?=/ # :nodoc:
18
+
19
+ # Decodes a string, +from+, containing RFC 2047 encoded words into a target
20
+ # character set, +target+ defaulting to utf-8. See iconv_open(3) for information on the
21
+ # supported target encodings. If one of the encoded words cannot be
22
+ # converted to the target encoding, it is left in its encoded form.
23
+ def self.decode(from, target='utf-8')
24
+ from.gsub(WORD) do |word|
25
+ cs = $1
26
+ encoding = $2
27
+ text = $3
28
+ # B64 or QP decode, as necessary:
29
+ case encoding.downcase
30
+ when 'b'
31
+ text = text.unpack('m*')[0]
32
+ when 'q'
33
+ # RFC 2047 has a variant of quoted printable where a ' ' character
34
+ # can be represented as an '_', rather than =32, so convert
35
+ # any of these that we find before doing the QP decoding.
36
+ text = text.tr("_", " ")
37
+ text = text.unpack('M*')[0]
38
+ else
39
+ raise Unparseable, from
40
+ end
41
+ # Convert
42
+ #
43
+ # Remember: Iconv.open(to, from)
44
+ begin
45
+ text = Iconv.open(target, cs) {|i| i.iconv(text)}
46
+ rescue
47
+ raise Unparseable, from
48
+ end
49
+ end
50
+ end
51
+
52
+ class Unparseable < RuntimeError; end
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfc2047
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Sam Roberts
12
+ - Conrad Irwin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-09 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Provides a method Rfc2047.decode that can decode individual headers encoded with this format.
22
+ email: conrad.irwin@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/rfc2047.rb
31
+ - README.md
32
+ has_rdoc: true
33
+ homepage: http://github.com/ConradIrwin/rfc2047-ruby/
34
+ licenses:
35
+ - Ruby
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.6
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Decodes email headers according to RFC2047
62
+ test_files: []
63
+