mailparser 0.4.22a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ # Copyright (C) 2006-2010 TOMITA Masahiro
2
+ # mailto:tommy@tmtm.org
3
+
4
+ require "mailparser/rfc2045"
5
+
6
+ unless ARGV.empty?
7
+ ARGV.each do |fname|
8
+ File.open(fname) do |f|
9
+ header = []
10
+ f.each do |line|
11
+ break if line.chomp.empty?
12
+ if line =~ /^\s/ then
13
+ header[-1] << line
14
+ else
15
+ header << line
16
+ end
17
+ end
18
+ header.each do |h|
19
+ begin
20
+ MailParser::RFC2045.parse(*h.split(/\s*:\s*/, 2))
21
+ rescue MailParser::ParseError => e
22
+ puts fname
23
+ p e
24
+ p h
25
+ end
26
+ end
27
+ end
28
+ end
29
+ exit
30
+ end
31
+
32
+ require "test/unit"
33
+
34
+ class TC_RFC2045 < Test::Unit::TestCase
35
+ def test_content_type_noparams()
36
+ c = MailParser::RFC2045.parse("content-type", "text/plain")
37
+ assert_equal("text", c.type)
38
+ assert_equal("plain", c.subtype)
39
+ assert_equal({}, c.params)
40
+ end
41
+
42
+ def test_content_type_charset()
43
+ c = MailParser::RFC2045.parse("content-type", "text/plain; charset=euc-jp")
44
+ assert_equal("text", c.type)
45
+ assert_equal("plain", c.subtype)
46
+ assert_equal(1, c.params.size)
47
+ assert_equal("euc-jp", c.params["charset"])
48
+ end
49
+
50
+ def test_content_type_charset_upcase()
51
+ c = MailParser::RFC2045.parse("content-type", "TEXT/PLAIN; CHARSET=euc-jp")
52
+ assert_equal("text", c.type)
53
+ assert_equal("plain", c.subtype)
54
+ assert_equal(1, c.params.size)
55
+ assert_equal("euc-jp", c.params["charset"])
56
+ end
57
+
58
+ def test_content_type_charset_quote()
59
+ c = MailParser::RFC2045.parse("content-type", "text/plain; charset=\"euc-jp\"")
60
+ assert_equal("text", c.type)
61
+ assert_equal("plain", c.subtype)
62
+ assert_equal(1, c.params.size)
63
+ assert_equal("euc-jp", c.params["charset"])
64
+ end
65
+
66
+ def test_content_type_invalid()
67
+ assert_raises(MailParser::ParseError){MailParser::RFC2045.parse("content-type", "text/plain; name=\"hosts\"hoge")}
68
+ end
69
+
70
+ def test_content_description()
71
+ c = MailParser::RFC2045.parse("content-description", "(hoge fuga>")
72
+ assert_equal("(hoge fuga>", c)
73
+ end
74
+
75
+ def test_content_transfer_encoding()
76
+ c = MailParser::RFC2045.parse("content-transfer-encoding", "7bit")
77
+ assert_equal("7bit", c.mechanism)
78
+ end
79
+
80
+ def test_content_id()
81
+ c = MailParser::RFC2045.parse("content-id", "<a@b.c>")
82
+ assert_equal("a@b.c", c.msg_id)
83
+ end
84
+
85
+ def test_mime_version()
86
+ v = MailParser::RFC2045.parse("mime-version", "1.0")
87
+ assert_equal("1.0", v)
88
+ assert_equal([], v.comments)
89
+ end
90
+
91
+ def test_mime_version_with_comment()
92
+ v = MailParser::RFC2045.parse("mime-version", "1.0 (hoge)")
93
+ assert_equal("1.0", v)
94
+ assert_equal(["(hoge)"], v.comments)
95
+ end
96
+
97
+ def test_mime_version_with_comment2()
98
+ v = MailParser::RFC2045.parse("mime-version", "1.(hoge)0")
99
+ assert_equal("1.0", v)
100
+ assert_equal(["(hoge)"], v.comments)
101
+ end
102
+
103
+ def test_qp_decode()
104
+ assert_equal("IJKL", MailParser::RFC2045.qp_decode("=49=4A=4B=4C"))
105
+ assert_equal("IJKL", MailParser::RFC2045.qp_decode("=49=4a=4b=4c"))
106
+ assert_equal("abcdefg", MailParser::RFC2045.qp_decode("abcd=\r\nefg"))
107
+ assert_equal("abcdefg", MailParser::RFC2045.qp_decode("abcd= \r\nefg"))
108
+ assert_equal("abcdefg", MailParser::RFC2045.qp_decode("abcd=\nefg"))
109
+ assert_equal("abcdefg", MailParser::RFC2045.qp_decode("abcd= \nefg"))
110
+ assert_equal("abcd\r\nefg", MailParser::RFC2045.qp_decode("abcd=\r\n\r\nefg"))
111
+ assert_equal("abcd\r\nefg", MailParser::RFC2045.qp_decode("abcd= \r\n\r\nefg"))
112
+ end
113
+
114
+ def test_b64_decode()
115
+ assert_equal("abcdefg", MailParser::RFC2045.b64_decode("YWJjZGVmZw=="))
116
+ assert_equal("01234567890123456789", MailParser::RFC2045.b64_decode("MDEyMzQ1Njc4OTAxMjM0NTY3ODk="))
117
+ assert_equal("01234567890123456789", MailParser::RFC2045.b64_decode("MDEyMzQ1Njc4\nOTAxMjM0NTY3ODk="))
118
+ assert_equal("01234567890123456789", MailParser::RFC2045.b64_decode("MDEyMzQ1Nj\nc4OTAxMjM0NTY3ODk="))
119
+ end
120
+ end
121
+
@@ -0,0 +1,118 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (C) 2006-2010 TOMITA Masahiro
3
+ # mailto:tommy@tmtm.org
4
+
5
+ require "mailparser/rfc2047"
6
+ require "test/unit"
7
+
8
+ class TC_RFC2047 < Test::Unit::TestCase
9
+ def test_q_decode_ascii()
10
+ assert_equal("hoge", MailParser::RFC2047.q_decode("hoge"))
11
+ end
12
+
13
+ def test_q_decode_space()
14
+ assert_equal("a b", MailParser::RFC2047.q_decode("a_b"))
15
+ end
16
+
17
+ def test_q_decode_utf8()
18
+ assert_equal("とみた", MailParser::RFC2047.q_decode("=E3=81=A8=E3=81=BF=E3=81=9F"))
19
+ end
20
+
21
+ def test_q_decode_utf8_ascii()
22
+ assert_equal("とaみbた", MailParser::RFC2047.q_decode("=E3=81=A8a=E3=81=BFb=E3=81=9F"))
23
+ end
24
+
25
+ def test_q_decode_end_equal()
26
+ assert_equal("abc", MailParser::RFC2047.q_decode("abc="))
27
+ end
28
+
29
+ def test_b_decode_ascii()
30
+ assert_equal("hoge", MailParser::RFC2047.b_decode("aG9nZQ=="))
31
+ end
32
+
33
+ def test_b_decode_utf8()
34
+ assert_equal("とみた", MailParser::RFC2047.b_decode("44Go44G/44Gf"))
35
+ end
36
+
37
+ def test_b_decode_invalid_space()
38
+ assert_equal("とみた", MailParser::RFC2047.b_decode("44Go 44 G/4 4Gf"))
39
+ end
40
+
41
+ def test_split_decode_q_ascii()
42
+ s = MailParser::RFC2047.split_decode("=?us-ascii?q?hoge?=")
43
+ assert_equal(1, s.size)
44
+ assert_equal("hoge", s[0])
45
+ assert_equal("us-ascii", s[0].charset)
46
+ assert_equal("=?us-ascii?q?hoge?=", s[0].raw)
47
+ end
48
+
49
+ def test_split_decode_q_ascii_upcase()
50
+ s = MailParser::RFC2047.split_decode("=?US-ASCII?Q?hoge?=")
51
+ assert_equal(1, s.size)
52
+ assert_equal("hoge", s[0])
53
+ assert_equal("us-ascii", s[0].charset)
54
+ assert_equal("=?US-ASCII?Q?hoge?=", s[0].raw)
55
+ end
56
+
57
+ def test_decode_q_ascii()
58
+ s = MailParser::RFC2047.decode("=?us-ascii?q?hoge?=")
59
+ assert_equal("hoge", s)
60
+ end
61
+
62
+ def test_decode_q_b()
63
+ s = MailParser::RFC2047.decode("=?us-ascii?q?hoge?= =?us-ascii?b?aG9nZQ==?=")
64
+ assert_equal("hogehoge", s)
65
+ end
66
+
67
+ def test_decode_plain()
68
+ s = MailParser::RFC2047.decode("abcdefg")
69
+ assert_equal("abcdefg", s)
70
+ end
71
+
72
+ def test_decode_encode_plain()
73
+ s = MailParser::RFC2047.decode("012345 =?us-ascii?q?hoge?= abcdefg")
74
+ assert_equal("012345 hoge abcdefg", s)
75
+ end
76
+
77
+ def test_decode_encode_plain2()
78
+ s = MailParser::RFC2047.decode("=?us-ascii?q?hoge?= abcdefg =?us-ascii?q?fuga?=")
79
+ assert_equal("hoge abcdefg fuga", s)
80
+ end
81
+
82
+ def test_decode_unknown_charset()
83
+ s = MailParser::RFC2047.decode("=?hoge?q?hoge?=")
84
+ assert_equal("hoge", s)
85
+ end
86
+
87
+ def test_decode_unknown_charset2()
88
+ s = MailParser::RFC2047.decode("=?hoge?q?hoge?=", "UTF-8")
89
+ assert_equal("=?hoge?q?hoge?=", s)
90
+ end
91
+
92
+ def test_decode_unknown_charset3()
93
+ s = MailParser::RFC2047.decode("abcdefg =?hoge?q?hoge?= =?us-ascii?q?fuga?=")
94
+ assert_equal("abcdefg hogefuga", s)
95
+ end
96
+
97
+ def test_decode_unknown_charset4()
98
+ s = MailParser::RFC2047.decode("abcdefg =?hoge?q?hoge?= =?us-ascii?q?fuga?=", "UTF-8")
99
+ assert_equal("abcdefg =?hoge?q?hoge?= fuga", s)
100
+ end
101
+
102
+ def test_decode_sjis()
103
+ s = MailParser::RFC2047.decode("=?sjis?b?h0A=?=", "UTF-8")
104
+ assert_equal("\xe2\x91\xa0", s)
105
+ end
106
+
107
+ def test_decode_iso2022jp()
108
+ s = MailParser::RFC2047.decode("=?iso-2022-jp?b?GyRCLSEbKEI=?=", "UTF-8")
109
+ assert_equal("\xe2\x91\xa0", s)
110
+ end
111
+
112
+ def test_decode_charset_converter()
113
+ proc = Proc.new{|f,t,s| s.gsub(/o/, "X")}
114
+ s = MailParser::RFC2047.decode("=?us-ascii?q?hoge?=", :output_charset=>"utf-8", :charset_converter=>proc)
115
+ assert_equal("hXge", s)
116
+ end
117
+
118
+ end
@@ -0,0 +1,60 @@
1
+ # Copyright (C) 2006-2010 TOMITA Masahiro
2
+ # mailto:tommy@tmtm.org
3
+
4
+ require "mailparser/rfc2183"
5
+
6
+ unless ARGV.empty?
7
+ ARGV.each do |fname|
8
+ File.open(fname) do |f|
9
+ header = []
10
+ f.each do |line|
11
+ break if line.chomp.empty?
12
+ if line =~ /^\s/ then
13
+ header[-1] << line
14
+ else
15
+ header << line
16
+ end
17
+ end
18
+ header.each do |h|
19
+ begin
20
+ MailParser::RFC2183.parse(*h.split(/\s*:\s*/, 2))
21
+ rescue MailParser::ParseError => e
22
+ puts fname
23
+ p e
24
+ p h
25
+ end
26
+ end
27
+ end
28
+ end
29
+ exit
30
+ end
31
+
32
+ require "test/unit"
33
+
34
+ class TC_RFC2183 < Test::Unit::TestCase
35
+ def test_content_disposition_noparams()
36
+ c = MailParser::RFC2183.parse("content-disposition", "inline")
37
+ assert_equal("inline", c.type)
38
+ assert_equal({}, c.params)
39
+ end
40
+
41
+ def test_content_disposition_charset()
42
+ c = MailParser::RFC2183.parse("content-disposition", "inline; filename=hoge")
43
+ assert_equal("inline", c.type)
44
+ assert_equal(1, c.params.size)
45
+ assert_equal("hoge", c.params["filename"])
46
+ end
47
+
48
+ def test_content_disposition_charset_upcase()
49
+ c = MailParser::RFC2183.parse("content-disposition", "INLINE; FILENAME=HOGE")
50
+ assert_equal("inline", c.type)
51
+ assert_equal("HOGE", c.params["filename"])
52
+ end
53
+
54
+ def test_content_disposition_charset_quote()
55
+ c = MailParser::RFC2183.parse("content-disposition", "inline; filename=\"hoge\"")
56
+ assert_equal("inline", c.type)
57
+ assert_equal(1, c.params.size)
58
+ assert_equal("hoge", c.params["filename"])
59
+ end
60
+ end
@@ -0,0 +1,167 @@
1
+ # Copyright (C) 2006-2010 TOMITA Masahiro
2
+ # mailto:tommy@tmtm.org
3
+
4
+ require "mailparser/rfc2231"
5
+ require "test/unit"
6
+
7
+ class TC_RFC2231 < Test::Unit::TestCase
8
+ def test_parse_param()
9
+ params = {
10
+ "hoge" => "fuga",
11
+ "foo" => "bar",
12
+ }
13
+ h = MailParser::RFC2231.parse_param(params)
14
+ assert_equal("fuga", h["hoge"])
15
+ assert_equal(nil, h["hoge"].charset)
16
+ assert_equal(nil, h["hoge"].language)
17
+ assert_equal("bar", h["foo"])
18
+ assert_equal(nil, h["foo"].charset)
19
+ assert_equal(nil, h["foo"].language)
20
+ end
21
+
22
+ def test_parse_param2()
23
+ params = {
24
+ "hoge*0" => "fuga",
25
+ "hoge*1" => "bar",
26
+ "foo" => "bar",
27
+ }
28
+ h = MailParser::RFC2231.parse_param(params)
29
+ assert_equal("fugabar", h["hoge"])
30
+ assert_equal(nil, h["hoge"].charset)
31
+ assert_equal(nil, h["hoge"].language)
32
+ assert_equal("bar", h["foo"])
33
+ assert_equal(nil, h["foo"].charset)
34
+ assert_equal(nil, h["foo"].language)
35
+ end
36
+
37
+ def test_parse_param3()
38
+ params = {
39
+ "hoge*0*" => "''fuga",
40
+ "hoge*1" => "bar",
41
+ "foo" => "bar",
42
+ }
43
+ h = MailParser::RFC2231.parse_param(params)
44
+ assert_equal("fugabar", h["hoge"])
45
+ assert_equal("", h["hoge"].charset)
46
+ assert_equal("", h["hoge"].language)
47
+ assert_equal("bar", h["foo"])
48
+ assert_equal(nil, h["foo"].charset)
49
+ assert_equal(nil, h["foo"].language)
50
+ end
51
+
52
+ def test_parse_param4()
53
+ params = {
54
+ "hoge*0*" => "euc-jp'ja'fuga",
55
+ "hoge*1" => "bar",
56
+ "foo" => "bar",
57
+ }
58
+ h = MailParser::RFC2231.parse_param(params)
59
+ assert_equal("fugabar", h["hoge"])
60
+ assert_equal("euc-jp", h["hoge"].charset)
61
+ assert_equal("ja", h["hoge"].language)
62
+ assert_equal("bar", h["foo"])
63
+ assert_equal(nil, h["foo"].charset)
64
+ assert_equal(nil, h["foo"].language)
65
+ end
66
+
67
+ def test_parse_param5()
68
+ params = {
69
+ "hoge*0*" => "''%30%31%32%33",
70
+ "hoge*1" => "%34%35%36",
71
+ "foo" => "bar",
72
+ }
73
+ h = MailParser::RFC2231.parse_param(params)
74
+ assert_equal("0123%34%35%36", h["hoge"])
75
+ assert_equal("", h["hoge"].charset)
76
+ assert_equal("", h["hoge"].language)
77
+ assert_equal("bar", h["foo"])
78
+ assert_equal(nil, h["foo"].charset)
79
+ assert_equal(nil, h["foo"].language)
80
+ end
81
+
82
+ def test_parse_param6()
83
+ params = {
84
+ "hoge*" => "''fuga",
85
+ "foo" => "bar",
86
+ }
87
+ h = MailParser::RFC2231.parse_param(params)
88
+ assert_equal("fuga", h["hoge"])
89
+ assert_equal("", h["hoge"].charset)
90
+ assert_equal("", h["hoge"].language)
91
+ assert_equal("bar", h["foo"])
92
+ assert_equal(nil, h["foo"].charset)
93
+ assert_equal(nil, h["foo"].language)
94
+ end
95
+
96
+ def test_parse_param7()
97
+ params = {
98
+ "hoge*" => "fuga",
99
+ }
100
+ assert_raises(MailParser::ParseError){MailParser::RFC2231.parse_param(params)}
101
+ end
102
+
103
+ def test_parse_param7_1()
104
+ params = {
105
+ "hoge*" => "fuga",
106
+ }
107
+ h = MailParser::RFC2231.parse_param(params, false)
108
+ assert_equal("fuga", h["hoge"])
109
+ end
110
+
111
+ def test_parse_param8()
112
+ params = {
113
+ "hoge*0*" => "fuga",
114
+ }
115
+ assert_raises(MailParser::ParseError){MailParser::RFC2231.parse_param(params)}
116
+ end
117
+
118
+ def test_parse_param8_1()
119
+ params = {
120
+ "hoge*0*" => "fuga",
121
+ }
122
+ h = MailParser::RFC2231.parse_param(params, false)
123
+ assert_equal("fuga", h["hoge"])
124
+ end
125
+
126
+ def test_parse_param_charset_converter
127
+ params = {
128
+ "hoge*" => "xcharset'xlang'%41%42%43",
129
+ }
130
+ h = MailParser::RFC2231.parse_param(params, :output_charset=>"xx", :charset_converter=>Proc.new{|f,t,s| s.downcase})
131
+ assert_equal "abc", h["hoge"]
132
+ end
133
+
134
+ def test_rfc_example()
135
+ params = {
136
+ "URL*0" => "ftp://",
137
+ "URL*1" => "cs.utk.edu.pub/moore/bulk-mailer/buik-mailer.tar"
138
+ }
139
+ h = MailParser::RFC2231.parse_param(params)
140
+ assert_equal("ftp://cs.utk.edu.pub/moore/bulk-mailer/buik-mailer.tar", h["URL"])
141
+ assert_equal(nil, h["URL"].charset)
142
+ assert_equal(nil, h["URL"].language)
143
+ end
144
+
145
+ def test_rfc_example2()
146
+ params = {
147
+ "title*" => "us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A"
148
+ }
149
+ h = MailParser::RFC2231.parse_param(params)
150
+ assert_equal("This is ***fun***", h["title"])
151
+ assert_equal("us-ascii", h["title"].charset)
152
+ assert_equal("en-us", h["title"].language)
153
+ end
154
+
155
+ def test_rfc_example3()
156
+ params = {
157
+ "title*0*" => "us-ascii'en'This%20is%20even%20more%20",
158
+ "title*1*" => "%2A%2A%2Afun%2A%2A%2A%20",
159
+ "title*2" => "isn't it!"
160
+ }
161
+ h = MailParser::RFC2231.parse_param(params)
162
+ assert_equal("This is even more ***fun*** isn't it!", h["title"])
163
+ assert_equal("us-ascii", h["title"].charset)
164
+ assert_equal("en", h["title"].language)
165
+ end
166
+
167
+ end