japetheape-tmail 1.2.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. data/CHANGES +74 -0
  2. data/LICENSE +21 -0
  3. data/NOTES +7 -0
  4. data/README +169 -0
  5. data/Rakefile +2 -0
  6. data/ext/Makefile +20 -0
  7. data/ext/tmailscanner/tmail/MANIFEST +4 -0
  8. data/ext/tmailscanner/tmail/depend +1 -0
  9. data/ext/tmailscanner/tmail/extconf.rb +33 -0
  10. data/ext/tmailscanner/tmail/tmailscanner.c +607 -0
  11. data/lib/tmail.rb +5 -0
  12. data/lib/tmail/Makefile +18 -0
  13. data/lib/tmail/address.rb +392 -0
  14. data/lib/tmail/attachments.rb +46 -0
  15. data/lib/tmail/base64.rb +46 -0
  16. data/lib/tmail/compat.rb +41 -0
  17. data/lib/tmail/config.rb +67 -0
  18. data/lib/tmail/core_extensions.rb +63 -0
  19. data/lib/tmail/encode.rb +591 -0
  20. data/lib/tmail/header.rb +960 -0
  21. data/lib/tmail/index.rb +9 -0
  22. data/lib/tmail/interface.rb +1152 -0
  23. data/lib/tmail/loader.rb +3 -0
  24. data/lib/tmail/mail.rb +591 -0
  25. data/lib/tmail/mailbox.rb +496 -0
  26. data/lib/tmail/main.rb +6 -0
  27. data/lib/tmail/mbox.rb +3 -0
  28. data/lib/tmail/net.rb +250 -0
  29. data/lib/tmail/obsolete.rb +132 -0
  30. data/lib/tmail/parser.rb +1060 -0
  31. data/lib/tmail/parser.y +416 -0
  32. data/lib/tmail/port.rb +379 -0
  33. data/lib/tmail/quoting.rb +125 -0
  34. data/lib/tmail/require_arch.rb +58 -0
  35. data/lib/tmail/scanner.rb +49 -0
  36. data/lib/tmail/scanner_r.rb +261 -0
  37. data/lib/tmail/stringio.rb +280 -0
  38. data/lib/tmail/utils.rb +337 -0
  39. data/lib/tmail/version.rb +39 -0
  40. data/log/BugTrackingLog.txt +1245 -0
  41. data/log/Changelog.txt +534 -0
  42. data/log/Testlog.txt +2340 -0
  43. data/log/Todo.txt +30 -0
  44. data/meta/MANIFEST +128 -0
  45. data/meta/VERSION +1 -0
  46. data/meta/project.yaml +30 -0
  47. data/meta/unixname +1 -0
  48. data/sample/bench_base64.rb +48 -0
  49. data/sample/data/multipart +23 -0
  50. data/sample/data/normal +29 -0
  51. data/sample/data/sendtest +5 -0
  52. data/sample/data/simple +14 -0
  53. data/sample/data/test +27 -0
  54. data/sample/extract-attachements.rb +33 -0
  55. data/sample/from-check.rb +26 -0
  56. data/sample/multipart.rb +26 -0
  57. data/sample/parse-bench.rb +68 -0
  58. data/sample/parse-test.rb +19 -0
  59. data/sample/sendmail.rb +94 -0
  60. data/setup.rb +1482 -0
  61. data/site/contributing/index.html +183 -0
  62. data/site/css/clean.css +27 -0
  63. data/site/css/layout.css +31 -0
  64. data/site/css/style.css +60 -0
  65. data/site/download/index.html +61 -0
  66. data/site/img/envelope.jpg +0 -0
  67. data/site/img/mailman.gif +0 -0
  68. data/site/img/stamp-sm.jpg +0 -0
  69. data/site/img/stamp.jpg +0 -0
  70. data/site/img/stampborder.jpg +0 -0
  71. data/site/img/tfire.jpg +0 -0
  72. data/site/img/tmail.png +0 -0
  73. data/site/index.html +270 -0
  74. data/site/js/jquery.js +31 -0
  75. data/site/log/Changelog.xsl +33 -0
  76. data/site/log/changelog.xml +1677 -0
  77. data/site/outdated/BUGS +3 -0
  78. data/site/outdated/DEPENDS +1 -0
  79. data/site/outdated/Incompatibilities +89 -0
  80. data/site/outdated/Incompatibilities.ja +102 -0
  81. data/site/outdated/NEWS +9 -0
  82. data/site/outdated/README.ja +73 -0
  83. data/site/outdated/doc.ja/address.html +275 -0
  84. data/site/outdated/doc.ja/basics.html +405 -0
  85. data/site/outdated/doc.ja/config.html +49 -0
  86. data/site/outdated/doc.ja/details.html +146 -0
  87. data/site/outdated/doc.ja/index.html +39 -0
  88. data/site/outdated/doc.ja/mail.html +793 -0
  89. data/site/outdated/doc.ja/mailbox.html +265 -0
  90. data/site/outdated/doc.ja/port.html +95 -0
  91. data/site/outdated/doc.ja/tmail.html +58 -0
  92. data/site/outdated/doc.ja/usage.html +202 -0
  93. data/site/outdated/rdd/address.rrd.m +229 -0
  94. data/site/outdated/rdd/basics.rd.m +275 -0
  95. data/site/outdated/rdd/config.rrd.m +26 -0
  96. data/site/outdated/rdd/details.rd.m +117 -0
  97. data/site/outdated/rdd/index.rhtml.m +54 -0
  98. data/site/outdated/rdd/mail.rrd.m +701 -0
  99. data/site/outdated/rdd/mailbox.rrd.m +228 -0
  100. data/site/outdated/rdd/port.rrd.m +69 -0
  101. data/site/outdated/rdd/tmail.rrd.m +33 -0
  102. data/site/outdated/rdd/usage.rd.m +247 -0
  103. data/site/quickstart/index.html +69 -0
  104. data/site/quickstart/quickstart.html +52 -0
  105. data/site/quickstart/usage.html +193 -0
  106. data/site/reference/address.html +247 -0
  107. data/site/reference/config.html +30 -0
  108. data/site/reference/index.html +101 -0
  109. data/site/reference/mail.html +726 -0
  110. data/site/reference/mailbox.html +245 -0
  111. data/site/reference/port.html +75 -0
  112. data/site/reference/tmail.html +35 -0
  113. data/test/extctrl.rb +6 -0
  114. data/test/fixtures/mailbox +414 -0
  115. data/test/fixtures/mailbox_without_any_from_or_sender +10 -0
  116. data/test/fixtures/mailbox_without_from +11 -0
  117. data/test/fixtures/mailbox_without_return_path +12 -0
  118. data/test/fixtures/raw_attack_email_with_zero_length_whitespace +29 -0
  119. data/test/fixtures/raw_base64_decoded_string +0 -0
  120. data/test/fixtures/raw_base64_email +83 -0
  121. data/test/fixtures/raw_base64_encoded_string +1 -0
  122. data/test/fixtures/raw_email +14 -0
  123. data/test/fixtures/raw_email10 +20 -0
  124. data/test/fixtures/raw_email11 +34 -0
  125. data/test/fixtures/raw_email12 +32 -0
  126. data/test/fixtures/raw_email13 +29 -0
  127. data/test/fixtures/raw_email2 +114 -0
  128. data/test/fixtures/raw_email3 +70 -0
  129. data/test/fixtures/raw_email4 +59 -0
  130. data/test/fixtures/raw_email5 +19 -0
  131. data/test/fixtures/raw_email6 +20 -0
  132. data/test/fixtures/raw_email7 +66 -0
  133. data/test/fixtures/raw_email8 +47 -0
  134. data/test/fixtures/raw_email9 +28 -0
  135. data/test/fixtures/raw_email_multiple_from +30 -0
  136. data/test/fixtures/raw_email_quoted_with_0d0a +14 -0
  137. data/test/fixtures/raw_email_reply +32 -0
  138. data/test/fixtures/raw_email_simple +11 -0
  139. data/test/fixtures/raw_email_with_bad_date +48 -0
  140. data/test/fixtures/raw_email_with_double_carriage_return +179 -0
  141. data/test/fixtures/raw_email_with_illegal_boundary +58 -0
  142. data/test/fixtures/raw_email_with_mimepart_without_content_type +94 -0
  143. data/test/fixtures/raw_email_with_multipart_mixed_quoted_boundary +50 -0
  144. data/test/fixtures/raw_email_with_nested_attachment +100 -0
  145. data/test/fixtures/raw_email_with_partially_quoted_subject +14 -0
  146. data/test/fixtures/raw_email_with_quoted_illegal_boundary +58 -0
  147. data/test/kcode.rb +14 -0
  148. data/test/temp_test_one.rb +46 -0
  149. data/test/test_address.rb +1219 -0
  150. data/test/test_attachments.rb +76 -0
  151. data/test/test_base64.rb +64 -0
  152. data/test/test_encode.rb +85 -0
  153. data/test/test_header.rb +1002 -0
  154. data/test/test_helper.rb +9 -0
  155. data/test/test_mail.rb +768 -0
  156. data/test/test_mbox.rb +184 -0
  157. data/test/test_port.rb +436 -0
  158. data/test/test_quote.rb +107 -0
  159. data/test/test_scanner.rb +209 -0
  160. data/test/test_utils.rb +36 -0
  161. data/work/script/make +26 -0
  162. data/work/script/rdoc +39 -0
  163. data/work/script/setup +1616 -0
  164. data/work/script/test +30 -0
  165. metadata +298 -0
data/CHANGES ADDED
@@ -0,0 +1,74 @@
1
+ === 1.2.3.1 / 2008-04-11
2
+
3
+ * Closed #19429 - Installing TMail on Windows with the gem
4
+
5
+ === 1.2.3 / 2008-04-11
6
+
7
+ * Closed #18881 - TMail goes into an endless loop if sent an crafted email
8
+ * Closed #19203 - TMail errors in Ruby 1.9.1 on invalid multibyte chars.
9
+ * Closed #18814 - Fixed attchment.rb failing on mail part that had a nil content-type
10
+ * Closed #18516 - Fix TMail::Mail#preamble, and add #preamble= (Charles Lowe) (233)
11
+ * Applied patch #18515 to remove ftools from test case (Charles Lowe) (232)
12
+ * A lot of documentation patches to the mail and utils and net files. Also added "log/BugTrackingLog.txt" for myself as my bug log got clobbered by the new changelog.txt format. (231)
13
+
14
+ === 1.2.2 / 2008-03-07
15
+
16
+ * Fixed install bug with gem pacakge (1.2.1 was not compiling on gem install)
17
+ * A _LOT_ more documentation...!
18
+ * More documentation - (Mikel)
19
+ * Applied Ruby 1.9 patches to the library - All tests passing now - (Mikel)
20
+ * Closed #17719 - Fixed UNIXMbox code - readonly was not working and raising an exception. Now works.
21
+ * Closed #18038 - Multiple froms not being parsed correctly, added a test case to cover this and show the correct handling - (Mikel)
22
+
23
+ === 1.2.1 / 2008-01-11
24
+
25
+ * More documentation (Mikel)
26
+ * Added 15 test cases from the Rails ActionMailer to TMail
27
+ * Changed mailscanner to tmailscanner (mailscanner is copyrighted)
28
+ * Closed Bug - Handled quote boundary being gready on content-type header (M. Mondragon)
29
+ * Closed #16025 - Fixed scanner.rb so it passes same tests as scanner.c
30
+ * Closed #16283 - Handled incorrect decoding of attachments (M. Aoki - garyo)
31
+ * Closed #16899 - HeaderField.new_from_port and added test cases to cover this code (Maarten O.)
32
+ * Closed #16900 - UNIXMbox.fromaddr missing port param and does not return Envelope Sender (Maarten O.)
33
+
34
+ === 1.2.0 / 2007-11-29
35
+
36
+ * 5 major enhancements:
37
+ * Extensive documentation work. (mikel)
38
+ * Renamed scanner_c.c to mailscanner.c. (trans)
39
+ * Removed base64 c extension. It's speed benefit was negligable (only 0.2 sec over 10000 sizable encode/decode runs) (trans)
40
+ * Closed 15445 - TMail::Mail#create_forward now returns a new Mail object that has the original mail as an encoded 7 bit multipart attachment. Also moved create_forward and create_reply from tmail/net into tmail/interface as it makes more sense to have it there. (mikel)
41
+ * Closed 15643 - TMail::Mail#reply_addresses was returning an empty array if reply_to was set to nil (ie, the header field existed but was empty) instead of returning the from address or default. (mikel)
42
+ * Closed 16025 - TMail scanner.rb would not parse ATOM chars correctly making it fail tests where the C version passed them. Fixed this by updating the Scanner.rb version to be in step with the C version (there was an extra @ symbol in the ATOM CHARS definition that was not in the C version.) (mikel)
43
+ * Fixed scanner.rb so that it would pass the same tests that the C version does - had a sundry @ symbol inside of the ATOM CHARS
44
+ * 3 minor enhancements:
45
+ * Renamed scanner_c.c to tmailscanner.c (trans)
46
+ * Changed TMail::Mail#sender to have a default "default" value of nil to be in alignment with all the other interface methods (mikel)
47
+ * Made base64_decode and base64_encode into ! versions as they are destructive on the mail body (encodes the body to base64 or decodes it), made aliases for base64_decode and base64_encode to point back to the bang versions. Doing this with a view to change base64_encode to a non destructive version (returns the encoded body) in future versions. (mikel)
48
+
49
+ === 1.1.1 / 2007-11-05
50
+
51
+ * 3 major enhancement:
52
+ * Created unified package, for installation in any platform.
53
+ * Added require_arch.rb to facilitate multi-platform support.
54
+ * If compilation fails, set NORUBYEXT="true" and reinstall.
55
+ * 3 minor enhancement:
56
+ * Fixed line wrapping of long header fields so that they wrap at the correct whitespace points.
57
+ * Fixed bug where re-assigning the mail.body to existing mail object that already had a parsed body would not re-parse the body.
58
+ * Started documenting the source code... lots more to do.
59
+
60
+ === 1.1.0 / 2007-10-28
61
+
62
+ * 1 minor enhancements:
63
+ * Changed the quoting of paramaters in the header fields to wrap double quotes around fields that are needed to be quoted.
64
+ * Removed keeping double quotes around a filename that does not need double quotes per RFC 1521
65
+ * More clean up and getting tests passing. Now standing at 2 failures out of 3366 assertions. One is the incorrect handling of "@"@test.com (returns @@test.com) and the other is a japanese encoding issue.
66
+
67
+ === 1.0.0 / 2007-10-28
68
+
69
+ * 1 major enhancement:
70
+ * TMail is now released as a GEM!
71
+ * 2 minor enhancements:
72
+ * Fixed bug 15077 - TMail now recognizes attachments as soon as they are added to the body.
73
+ * Refactored handling of quotations in header fields - now cleaner
74
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2001-2007 Minero Aoki
2
+ Changes Copyright (c) 2007 Mikel Lindsaar
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/NOTES ADDED
@@ -0,0 +1,7 @@
1
+ [ANN] TMail 1.2.3.1 - Minor Fix to Installer
2
+
3
+ This release just handles a bug with installing the gem on a Windows platform.
4
+
5
+ It is otherwise identical to 1.2.3
6
+
7
+ Mikel
data/README ADDED
@@ -0,0 +1,169 @@
1
+ = TMail
2
+
3
+ http://tmail.rubyforge.org/
4
+
5
+ Mikel Lindsaar maintainer
6
+ Trans assitant developer
7
+ Minero Aoki original developer
8
+
9
+ == DESCRIPTION:
10
+
11
+ TMail is a mail handling library for Ruby. It abstracts a mail message into a usable object allowing you to read, set, add and delete headers and the mail body.
12
+
13
+ TMail is used by the Ruby on Rails web framework as the Email abstraction layer for their ActionMailer module. It is also used by the Nitro framework and many other applications on and off the web.
14
+
15
+ The goal of the TMail handling library is to be able to parse and handle raw Email sources and produce RFC compliant Emails as a result. If you find something that TMail does that violates an RFC, we want to know and we'll get it fixed fast.
16
+
17
+ == DOCUMENTATION:
18
+
19
+ The place you will want to look first is the TMail::Mail class. This has the vast majority of methods you will be using to talk to your TMail object.
20
+
21
+ == FEATURES/PROBLEMS:
22
+
23
+ TMail is fairly RFC compliant on the handling of emails.
24
+
25
+ There are also some problems in the header handling, but for 99.9% of email, you will be fine. Usually, the problems revolve around parsing incomming emails and making sense of them.
26
+
27
+ I really welcome any examples of Emails that "didn't work" with TMail so I can use them as test cases.
28
+
29
+ == SYNOPSIS:
30
+
31
+ TMail is very easy to use. You simply require the library and then pass a raw email text message into the TMail::Mail.parse method. This returns a TMail::Mail object which you can now query and run methods against to modify, inspect or add to the Email.
32
+
33
+ You can find almost all of the methods that you will use to talk to and update a TMail instance in the TMail::Mail class. I am constantly updating this code, with comments, added a fair bit and have a lot more to go!.
34
+
35
+ === Short Version:
36
+
37
+ irb(main):001:0> require 'tmail'
38
+ irb(main):002:0> raw_email = File.open("my_raw_email", 'r') { |f| @mail = f.read }
39
+ irb(main):003:0> email = TMail::Mail.parse(raw_email)
40
+ irb(main):004:0> puts email['to']
41
+ mikel@example.com
42
+ => nil
43
+ irb(main):005:0> email['to'] = 'mikel@somewhere.else.com'
44
+ => "mikel@somewhere.else.com"
45
+ irb(main):006:0> puts email['to']
46
+ mikel@somewhere.else.com
47
+ => nil
48
+
49
+ === Longer Version:
50
+
51
+ Assuming you have a single raw email in the variable my_message, you can do the following:
52
+
53
+ require 'tmail'
54
+ email = TMail::Mail.parse(my_message)
55
+
56
+ This will give you a TMail::Mail class containing your parsed message. There are other methods of opening emails through Ports.
57
+
58
+ You can view this email by a simple puts:
59
+
60
+ puts email
61
+
62
+ Return-Path: <mikel@nowhere.com>
63
+ Date: Sun, 21 Oct 2007 19:38:13 +1000
64
+ From: Mikel Lindsaar <mikel@nowhere.com>
65
+ To: mikel@somewhere.com
66
+ Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
67
+ Subject: Testing Email
68
+
69
+ Hello Mikel
70
+
71
+ Easy right?
72
+
73
+ === Adding a header to the EMail:
74
+
75
+ Say now that you have opened your message, you want to put in a Reply-To field. You do this like so:
76
+
77
+ email['reply-to'] = "My Email Address <my_address@anotherplace.com>"
78
+
79
+ Is it really there? Well, find out with a puts:
80
+
81
+ puts email
82
+
83
+ Return-Path: <mikel@nowhere.com>
84
+ Date: Sun, 21 Oct 2007 19:38:13 +1000
85
+ From: Mikel Lindsaar <mikel@nowhere.com>
86
+ Reply-To: My Email Address <my_address@anotherplace.com>
87
+ To: mikel@somewhere.com
88
+ Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
89
+ Subject: Testing Email
90
+
91
+ Hello Mikel
92
+
93
+ Yup looks good.
94
+
95
+ === Inspecting a header:
96
+
97
+ You can then inspect your added header by doing:
98
+
99
+ email['reply-to'] # => #<TMail::AddressHeader "My Email Address <my_address@anotherplace.com>">
100
+
101
+ If you just want to the actual value, not the AddressHeader object, pass to_s to this.
102
+
103
+ email['reply-to'].to_s # => "My Email Address <my_address@anotherplace.com>"
104
+
105
+ === Deleting a header:
106
+
107
+ One way of deleting a header from an Email is just assigning it nil like so:
108
+
109
+ email['reply-to'] = nil # => nil
110
+
111
+ If you now puts the email again, it will not be included:
112
+
113
+ puts email
114
+
115
+ Return-Path: <mikel@nowhere.com>
116
+ Date: Sun, 21 Oct 2007 19:38:13 +1000
117
+ From: Mikel Lindsaar <mikel@nowhere.com>
118
+ To: mikel@somewhere.com
119
+ Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
120
+ Subject: Testing Email
121
+
122
+ Hello Mikel
123
+
124
+ === Writing out an Email:
125
+
126
+ You can just call to_s on any email to have it serialized out as a single string with the right number of line breaks and encodings.
127
+
128
+ == CONTRIBUTING:
129
+
130
+ You can visit the {Contributing to TMail}[link:http://tmail.rubyforge.org/contributing/] to find out how to contribute to TMail, developers are welcome and wanted!
131
+
132
+ == REQUIREMENTS:
133
+
134
+ * C compiler if you want the Ruby extension for Scanner
135
+ * Ruby 1.8 or later
136
+
137
+ == INSTALLATION:
138
+
139
+ * sudo gem install tmail
140
+
141
+ Or manually,
142
+
143
+ * sudo script/setup
144
+
145
+ == LICENSE:
146
+
147
+ (The MIT License)
148
+
149
+ Copyright (c) 2007 FIX
150
+
151
+ Permission is hereby granted, free of charge, to any person obtaining
152
+ a copy of this software and associated documentation files (the
153
+ 'Software'), to deal in the Software without restriction, including
154
+ without limitation the rights to use, copy, modify, merge, publish,
155
+ distribute, sublicense, and/or sell copies of the Software, and to
156
+ permit persons to whom the Software is furnished to do so, subject to
157
+ the following conditions:
158
+
159
+ The above copyright notice and this permission notice shall be
160
+ included in all copies or substantial portions of the Software.
161
+
162
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
163
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
164
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
165
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
166
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
167
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
168
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
169
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), "setup.rb")
2
+
data/ext/Makefile ADDED
@@ -0,0 +1,20 @@
1
+ #
2
+ # ext/tmail/Makefile
3
+ #
4
+
5
+ .PHONY: tmailscanner clean distclean
6
+
7
+ all: tmailscanner
8
+
9
+ tmailscanner: tmailscanner/tmail/Makefile
10
+ cd tmailscanner/tmail; $(MAKE)
11
+
12
+ tmailscanner/tmail/Makefile: tmailscanner/tmail/extconf.rb
13
+ cd tmailscanner/tmail; ruby extconf.rb
14
+
15
+ clean:
16
+ cd tmailscanner/tmail; $(MAKE) clean
17
+
18
+ distclean:
19
+ cd tmailscanner/tmail; $(MAKE) distclean
20
+
@@ -0,0 +1,4 @@
1
+ MANIFEST
2
+ tmailscanner.c
3
+ extconf.rb
4
+ depend
@@ -0,0 +1 @@
1
+ tmailscanner.o: tmailscanner.c $(hdrdir)/ruby.h $(topdir)/config.h $(hdrdir)/defines.h Makefile
@@ -0,0 +1,33 @@
1
+ require 'mkmf'
2
+ require 'rbconfig'
3
+
4
+ extension_name = 'tmailscanner'
5
+
6
+ windows = (/djgpp|(cyg|ms|bcc)win|mingw/ =~ RUBY_PLATFORM)
7
+
8
+ # For now use pure Ruby tmailscanner if on Windows, since
9
+ # most Window's users don't have developer tools needed.
10
+
11
+ if (ENV['NORUBYEXT'] == 'true' || windows)
12
+ if windows
13
+ File.open('make.bat', 'w') do |f|
14
+ f << 'echo Native extension will be omitted.'
15
+ end
16
+ File.open('nmake.bat', 'w') do |f|
17
+ f << 'echo Native extension will be omitted.'
18
+ end
19
+ end
20
+ File.open('Makefile', 'w') do |f|
21
+ f << "all:\n"
22
+ f << "install:\n"
23
+ end
24
+ else
25
+ #dir_config(extension_name)
26
+ if windows && ENV['make'].nil?
27
+ $LIBS += " msvcprt.lib"
28
+ else
29
+ $CFLAGS += " -D_FILE_OFFSET_BITS=64" #???
30
+ end
31
+ create_makefile(extension_name)
32
+ end
33
+
@@ -0,0 +1,607 @@
1
+ /*
2
+
3
+ tmailscanner.c
4
+
5
+ Copyright (c) 1998-2007 Minero Aoki
6
+
7
+ This program is free software.
8
+ You can distribute/modify this program under the terms of
9
+ the GNU Lesser General Public License version 2.1.
10
+
11
+ */
12
+
13
+ #include <stdio.h>
14
+ #ifdef __STDC__
15
+ # include <stdlib.h>
16
+ #endif
17
+
18
+ #include "ruby.h"
19
+
20
+ #include "ruby/encoding.h"
21
+
22
+ #define TMAIL_VERSION "1.2.3"
23
+
24
+ static VALUE TMailScanner;
25
+ static VALUE ScanError;
26
+
27
+ struct scanner
28
+ {
29
+ char *pbeg;
30
+ char *p;
31
+ char *pend;
32
+ unsigned int flags;
33
+ VALUE comments;
34
+ };
35
+
36
+ #define MODE_MIME (1 << 0)
37
+ #define MODE_RECV (1 << 1)
38
+ #define MODE_ISO2022 (1 << 2)
39
+ #define MODE_DEBUG (1 << 4)
40
+
41
+ #define MIME_MODE_P(s) ((s)->flags & MODE_MIME)
42
+ #define RECV_MODE_P(s) ((s)->flags & MODE_RECV)
43
+ #define ISO2022_MODE_P(s) ((s)->flags & MODE_ISO2022)
44
+
45
+ #define GET_SCANNER(val, s) Data_Get_Struct(val, struct scanner, s)
46
+
47
+
48
+
49
+
50
+ /*
51
+ // begin @japetheape
52
+ int
53
+ mbclen(const char p)
54
+ {
55
+ return rb_enc_mbclen(&p);
56
+ }
57
+
58
+ int
59
+ ismbchar(const char p)
60
+ {
61
+ return mbclen((p)) != 1;
62
+ }
63
+ // end @japethape
64
+ */
65
+
66
+
67
+ static void
68
+ mails_free(sc)
69
+ struct scanner *sc;
70
+ {
71
+ free(sc);
72
+ }
73
+
74
+ #ifndef StringValue
75
+ # define StringValue(s) Check_Type(str, T_STRING);
76
+ #endif
77
+
78
+ /*
79
+ * Document-method: mails_s_new
80
+ *
81
+ * Creates a new mail
82
+ *
83
+ */
84
+ static VALUE
85
+ mails_s_new(klass, str, ident, cmt)
86
+ VALUE klass, str, ident, cmt;
87
+ {
88
+ struct scanner *sc;
89
+ const char *tmp;
90
+
91
+ sc = ALLOC_N(struct scanner, 1);
92
+
93
+ StringValue(str);
94
+ sc->pbeg = RSTRING_PTR(str);
95
+ sc->p = sc->pbeg;
96
+ sc->pend = sc->p + RSTRING_LEN(str);
97
+
98
+ sc->flags = 0;
99
+ Check_Type(ident, T_SYMBOL);
100
+ tmp = rb_id2name(SYM2ID(ident));
101
+ if (strcmp(tmp, "RECEIVED") == 0) sc->flags |= MODE_RECV;
102
+ else if (strcmp(tmp, "CTYPE") == 0) sc->flags |= MODE_MIME;
103
+ else if (strcmp(tmp, "CENCODING") == 0) sc->flags |= MODE_MIME;
104
+ else if (strcmp(tmp, "CDISPOSITION") == 0) sc->flags |= MODE_MIME;
105
+
106
+ tmp = rb_get_kcode();
107
+ if (strcmp(tmp, "EUC") == 0 || strcmp(tmp, "SJIS") == 0) {
108
+ sc->flags |= MODE_ISO2022;
109
+ }
110
+
111
+ sc->comments = Qnil;
112
+ if (! NIL_P(cmt)) {
113
+ Check_Type(cmt, T_ARRAY);
114
+ sc->comments = cmt;
115
+ }
116
+
117
+ return Data_Wrap_Struct(TMailScanner, 0, mails_free, sc);
118
+ }
119
+
120
+ /*
121
+ * Document-method: mails_debug_get
122
+ *
123
+ * TODO: Documentation needed
124
+ *
125
+ */
126
+ static VALUE
127
+ mails_debug_get(self)
128
+ VALUE self;
129
+ {
130
+ struct scanner *sc;
131
+
132
+ GET_SCANNER(self, sc);
133
+ if (sc->flags & MODE_DEBUG)
134
+ return Qtrue;
135
+ else
136
+ return Qfalse;
137
+ }
138
+
139
+ /*
140
+ * Document-method: mails_debug_set
141
+ *
142
+ * TODO: Documentation needed
143
+ *
144
+ */
145
+ static VALUE
146
+ mails_debug_set(self, flag)
147
+ VALUE self, flag;
148
+ {
149
+ struct scanner *sc;
150
+
151
+ GET_SCANNER(self, sc);
152
+ if (RTEST(flag))
153
+ sc->flags |= MODE_DEBUG;
154
+ else
155
+ sc->flags &= ~MODE_DEBUG;
156
+ return Qnil;
157
+ }
158
+
159
+
160
+ /*
161
+ ----------------------------------------------------------------------
162
+ scanning routines
163
+ ----------------------------------------------------------------------
164
+ */
165
+
166
+ #define ESC '\033'
167
+ #define ATOM_SYMBOLS "_#!$%&'`*+-{|}~^/=?"
168
+ #define TOKEN_SYMBOLS "_#!$%&'`*+-{|}~^."
169
+ #define ATOM_SPECIAL "()<>[]@,;:\"\\."
170
+ #define TOKEN_SPECIAL "()<>[]@,;:\"\\/?="
171
+ #define LWSP " \t\r\n"
172
+
173
+ #define IS_ALPHA(ch) (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'))
174
+ #define IS_UPPER(ch) ('A' <= ch && ch <= 'Z')
175
+ #define TO_LOWER(ch) (IS_UPPER(ch) ? ch + 32 : ch)
176
+ #define IS_LWSP(ch) (strchr(LWSP, ch))
177
+ #define IS_DIGIT(ch) ('0' <= ch && ch <= '9')
178
+ #define IS_WORDCHAR(ch, symlist) \
179
+ (IS_ALPHA(ch) || IS_DIGIT(ch) || strchr(symlist, ch))
180
+ #define IS_ATOMCHAR(ch) IS_WORDCHAR(ch, ATOM_SYMBOLS)
181
+ #define IS_TOKENCHAR(ch) IS_WORDCHAR(ch, TOKEN_SYMBOLS)
182
+ #define IS_JCHAR(ch) MBCLEN_CHARFOUND_LEN(ch) != 1
183
+
184
+
185
+
186
+
187
+
188
+ /* I know this implement is ugly, but usually useful. */
189
+
190
+ /* skip until "\e(B" (us-ascii) */
191
+ static void
192
+ skip_iso2022jp_string(sc)
193
+ struct scanner *sc;
194
+ {
195
+ for (; sc->p < sc->pend; sc->p++) {
196
+ if (*sc->p == ESC) {
197
+ if (strncmp(sc->p, "\033(B", 3) == 0) {
198
+ sc->p += 3;
199
+ return;
200
+ }
201
+ }
202
+ }
203
+ }
204
+
205
+ static void
206
+ skip_japanese_string(sc)
207
+ struct scanner *sc;
208
+ {
209
+ /*
210
+ while (sc->p < sc->pend) {
211
+ if (! ismbchar(*sc->p)) return;
212
+ sc->p += mbclen(*sc->p);
213
+ }
214
+ */
215
+ }
216
+
217
+
218
+ #define scan_atom(sc) scan_word(sc, ATOM_SYMBOLS)
219
+ #define scan_token(sc) scan_word(sc, TOKEN_SYMBOLS)
220
+
221
+ static VALUE
222
+ scan_word(sc, syms)
223
+ struct scanner *sc;
224
+ char *syms;
225
+ {
226
+ char *beg = sc->p;
227
+
228
+ while (sc->p < sc->pend) {
229
+ if (ISO2022_MODE_P(sc) && *sc->p == ESC) {
230
+ skip_iso2022jp_string(sc);
231
+ }
232
+ else if (IS_JCHAR(*sc->p)) {
233
+ skip_japanese_string(sc);
234
+ }
235
+ else if (IS_WORDCHAR(*sc->p, syms)) {
236
+ sc->p++;
237
+ }
238
+ else {
239
+ break;
240
+ }
241
+ }
242
+
243
+ return rb_str_new(beg, sc->p - beg);
244
+ }
245
+
246
+
247
+ #define BUFSIZE 256
248
+
249
+ static VALUE
250
+ scan_quoted_word(sc)
251
+ struct scanner *sc;
252
+ {
253
+ char buf[BUFSIZE];
254
+ char *p;
255
+ char *save;
256
+ VALUE result = rb_str_new("", 0);
257
+
258
+ sc->p++; /* discard first dquote */
259
+ p = buf;
260
+ while (sc->p < sc->pend) {
261
+ if (*sc->p == '"') {
262
+ sc->p++; /* discard terminal dquote */
263
+ rb_str_cat(result, buf, p - buf);
264
+ return result;
265
+ }
266
+ if (ISO2022_MODE_P(sc) && *sc->p == ESC) {
267
+ save = sc->p;
268
+ skip_iso2022jp_string(sc);
269
+ while (save < sc->p) {
270
+ *p++ = *save++;
271
+ if (p >= buf + BUFSIZE) {
272
+ /* flush buffer */
273
+ rb_str_cat(result, buf, BUFSIZE);
274
+ p = buf;
275
+ }
276
+ }
277
+ continue;
278
+ }
279
+
280
+ if (*sc->p == '\\')
281
+ sc->p++; /* discard quoting backslash */
282
+ *p++ = *sc->p++;
283
+ if (p >= buf + BUFSIZE) {
284
+ /* flush buffer */
285
+ rb_str_cat(result, buf, BUFSIZE);
286
+ p = buf;
287
+ }
288
+ }
289
+
290
+ rb_raise(ScanError, "unterminated quoted-word");
291
+ return Qnil;
292
+ }
293
+
294
+ static VALUE
295
+ scan_domain_literal(sc)
296
+ struct scanner *sc;
297
+ {
298
+ char buf[BUFSIZE];
299
+ char *p;
300
+ VALUE result = rb_str_new("", 0);
301
+
302
+ p = buf;
303
+ while (sc->p < sc->pend) {
304
+ if (*sc->p == ']') {
305
+ *p++ = *sc->p++;
306
+ rb_str_cat(result, buf, p - buf);
307
+ return result;
308
+ }
309
+
310
+ if (*sc->p == '\\')
311
+ sc->p++; /* discard backslash */
312
+ *p++ = *sc->p++;
313
+ if (p >= buf + BUFSIZE) {
314
+ /* flush buffer */
315
+ rb_str_cat(result, buf, BUFSIZE);
316
+ p = buf;
317
+ }
318
+ }
319
+
320
+ rb_raise(ScanError, "unterminated domain literal");
321
+ return Qnil;
322
+ }
323
+
324
+
325
+ static VALUE
326
+ scan_comment(sc)
327
+ struct scanner *sc;
328
+ {
329
+ int nest = 1;
330
+ char *p;
331
+ VALUE ret = rb_str_new("", 0);
332
+
333
+ sc->p++;
334
+ p = sc->p;
335
+ while (sc->p < sc->pend) {
336
+ if (ISO2022_MODE_P(sc) && *sc->p == ESC) {
337
+ skip_iso2022jp_string(sc);
338
+ }
339
+ else if (IS_JCHAR(*sc->p)) {
340
+ skip_japanese_string(sc);
341
+ }
342
+ else {
343
+ switch (*sc->p) {
344
+ case '(':
345
+ nest++;
346
+ break;
347
+ case ')':
348
+ nest--;
349
+ if (nest == 0) {
350
+ rb_str_cat(ret, p, sc->p - p);
351
+ sc->p++;
352
+ return ret;
353
+ }
354
+ break;
355
+ case '\\':
356
+ rb_str_cat(ret, p, sc->p - p);
357
+ sc->p++;
358
+ if (sc->p == sc->pend)
359
+ rb_raise(ScanError, "incomplete char quote");
360
+ p = sc->p;
361
+ break;
362
+ default:
363
+ break;
364
+ }
365
+ sc->p++;
366
+ }
367
+ }
368
+
369
+ rb_raise(ScanError, "unterminated comment");
370
+ return Qnil;
371
+ }
372
+
373
+
374
+ static void
375
+ skip_lwsp(sc)
376
+ struct scanner *sc;
377
+ {
378
+ while (sc->p < sc->pend) {
379
+ if (IS_LWSP(*sc->p)) sc->p++;
380
+ else break;
381
+ }
382
+ }
383
+
384
+ static int
385
+ nccmp(a, b)
386
+ char *a, *b;
387
+ {
388
+ while (*a && *b) {
389
+ if ((*a != *b) && (TO_LOWER(*a) != TO_LOWER(*b)))
390
+ return 0;
391
+ a++; b++;
392
+ }
393
+ return (*a == *b);
394
+ }
395
+
396
+ static int
397
+ digit_p(str)
398
+ VALUE str;
399
+ {
400
+ char *p;
401
+ int i;
402
+
403
+ p = RSTRING_PTR(str);
404
+ for (i = 0; i < RSTRING_LEN(str); i++) {
405
+ if (! IS_DIGIT(RSTRING_PTR(str)[i]))
406
+ return 0;
407
+ }
408
+ return 1;
409
+ }
410
+
411
+ static VALUE tok_atom, tok_digit, tok_token, tok_quoted, tok_domlit;
412
+ static VALUE tok_from, tok_by, tok_via, tok_with, tok_id, tok_for;
413
+
414
+ static VALUE
415
+ atomsym(sc, str)
416
+ struct scanner *sc;
417
+ VALUE str;
418
+ {
419
+ if (digit_p(str)) {
420
+ return tok_digit;
421
+ }
422
+ else if (RECV_MODE_P(sc)) {
423
+ char *p = RSTRING_PTR(str);
424
+ if (nccmp(p, "from")) return tok_from;
425
+ else if (nccmp(p, "by")) return tok_by;
426
+ else if (nccmp(p, "via")) return tok_via;
427
+ else if (nccmp(p, "with")) return tok_with;
428
+ else if (nccmp(p, "id")) return tok_id;
429
+ else if (nccmp(p, "for")) return tok_for;
430
+ }
431
+ return tok_atom;
432
+ }
433
+
434
+ static void
435
+ debug_print(sc, sym, val)
436
+ struct scanner *sc;
437
+ VALUE sym, val;
438
+ {
439
+ VALUE s;
440
+
441
+ s = rb_funcall(sym, rb_intern("inspect"), 0),
442
+ printf("%7ld %-10s token=<%s>\n",
443
+ (unsigned long)(sc->pend - sc->p),
444
+ RSTRING_PTR(s),
445
+ RSTRING_PTR(val));
446
+ }
447
+
448
+ #define D(expr) do {\
449
+ if (sc->flags & MODE_DEBUG) {expr;}\
450
+ } while (0)
451
+
452
+ static void
453
+ pass_token(sc, sym, tok, arr)
454
+ struct scanner *sc;
455
+ VALUE sym, tok, arr;
456
+ {
457
+ D(debug_print(sc, sym, tok));
458
+ rb_ary_store(arr, 0, sym);
459
+ rb_ary_store(arr, 1, tok);
460
+ rb_yield(arr);
461
+ }
462
+
463
+ /*
464
+ * Document-method: mails_scan
465
+ *
466
+ * TODO: Documentation needed
467
+ *
468
+ */
469
+ static VALUE
470
+ mails_scan(self)
471
+ VALUE self;
472
+ {
473
+ struct scanner *sc;
474
+ VALUE arr;
475
+
476
+ #define PASS(s,v) pass_token(sc,s,v,arr)
477
+ GET_SCANNER(self, sc);
478
+ if (!sc->p) {
479
+ rb_raise(ScanError, "Mails#scan called before reset");
480
+ }
481
+ arr = rb_assoc_new(Qnil, Qnil);
482
+
483
+ while (sc->p < sc->pend) {
484
+ D(puts("new loop"));
485
+ D(printf("char='%c'\n", *sc->p));
486
+ if (IS_LWSP(*sc->p)) {
487
+ D(puts("lwsp"));
488
+ skip_lwsp(sc);
489
+ if (sc->p >= sc->pend)
490
+ break;
491
+ }
492
+
493
+ if (MIME_MODE_P(sc)) {
494
+ if (IS_TOKENCHAR(*sc->p) ||
495
+ (ISO2022_MODE_P(sc) && (*sc->p == ESC)) ||
496
+ IS_JCHAR(*sc->p)) {
497
+ D(puts("token"));
498
+ PASS(tok_token, scan_token(sc));
499
+ continue;
500
+ }
501
+ }
502
+ else {
503
+ if (IS_ATOMCHAR(*sc->p) ||
504
+ (ISO2022_MODE_P(sc) && (*sc->p == ESC)) ||
505
+ IS_JCHAR(*sc->p)) {
506
+ VALUE tmp;
507
+ D(puts("atom"));
508
+ tmp = scan_atom(sc);
509
+ PASS(atomsym(sc, tmp), tmp);
510
+ continue;
511
+ }
512
+ }
513
+
514
+ if (*sc->p == '"') {
515
+ D(puts("quoted"));
516
+ PASS(tok_quoted, scan_quoted_word(sc));
517
+ D(puts("quoted"));
518
+ }
519
+ else if (*sc->p == '(') {
520
+ VALUE c;
521
+ D(puts("comment"));
522
+ c = scan_comment(sc);
523
+ if (! NIL_P(sc->comments))
524
+ rb_ary_push(sc->comments, c);
525
+ }
526
+ else if (*sc->p == '[') {
527
+ D(puts("domlit"));
528
+ PASS(tok_domlit, scan_domain_literal(sc));
529
+ }
530
+ else {
531
+ VALUE ch;
532
+ D(puts("char"));
533
+ ch = rb_str_new(sc->p, 1);
534
+ sc->p++;
535
+ PASS(ch, ch);
536
+ }
537
+ }
538
+
539
+ PASS(Qfalse, rb_str_new("$", 1));
540
+ return Qnil;
541
+ }
542
+
543
+
544
+ /*
545
+ ------------------------------------------------------------------
546
+ ruby interface
547
+ ------------------------------------------------------------------
548
+ */
549
+
550
+ static VALUE
551
+ cstr2symbol(str)
552
+ char *str;
553
+ {
554
+ ID tmp;
555
+
556
+ tmp = rb_intern(str);
557
+ #ifdef ID2SYM
558
+ return ID2SYM(tmp);
559
+ #else
560
+ return INT2FIX(tmp);
561
+ #endif
562
+ }
563
+
564
+ void
565
+ Init_tmailscanner()
566
+ {
567
+ VALUE TMail;
568
+ VALUE tmp;
569
+
570
+ if (rb_const_defined(rb_cObject, rb_intern("TMail"))) {
571
+ TMail = rb_const_get(rb_cObject, rb_intern("TMail"));
572
+ }
573
+ else {
574
+ TMail = rb_define_module("TMail");
575
+ }
576
+ TMailScanner = rb_define_class_under(TMail, "TMailScanner", rb_cObject);
577
+
578
+ tmp = rb_str_new2(TMAIL_VERSION);
579
+ rb_obj_freeze(tmp);
580
+ rb_define_const(TMailScanner, "Version", tmp);
581
+
582
+ rb_define_singleton_method(TMailScanner, "new", mails_s_new, 3);
583
+ rb_define_method(TMailScanner, "scan", mails_scan, 0);
584
+ rb_define_method(TMailScanner, "debug", mails_debug_get, 0);
585
+ rb_define_method(TMailScanner, "debug=", mails_debug_set, 1);
586
+
587
+ if (rb_const_defined(TMail, rb_intern("SyntaxError"))) {
588
+ ScanError = rb_const_get(rb_cObject, rb_intern("SyntaxError"));
589
+ }
590
+ else {
591
+ ScanError = rb_define_class_under(TMail, "SyntaxError", rb_eStandardError);
592
+ }
593
+
594
+ tok_atom = cstr2symbol("ATOM");
595
+ tok_digit = cstr2symbol("DIGIT");
596
+ tok_token = cstr2symbol("TOKEN");
597
+ tok_quoted = cstr2symbol("QUOTED");
598
+ tok_domlit = cstr2symbol("DOMLIT");
599
+
600
+ tok_from = cstr2symbol("FROM");
601
+ tok_by = cstr2symbol("BY");
602
+ tok_via = cstr2symbol("VIA");
603
+ tok_with = cstr2symbol("WITH");
604
+ tok_id = cstr2symbol("ID");
605
+ tok_for = cstr2symbol("FOR");
606
+ }
607
+