65147228a12466cd1c6eb8b8d895c79757f59f0b
[feedcatcher.git] / vendor / rails / actionmailer / lib / action_mailer / vendor / tmail-1.2.3 / tmail / net.rb
1 #--
2 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
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.
22 #
23 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
24 # with permission of Minero Aoki.
25 #++
26
27 #:stopdoc:
28 require 'nkf'
29 #:startdoc:
30
31 module TMail
32
33 class Mail
34
35 def send_to( smtp )
36 do_send_to(smtp) do
37 ready_to_send
38 end
39 end
40
41 def send_text_to( smtp )
42 do_send_to(smtp) do
43 ready_to_send
44 mime_encode
45 end
46 end
47
48 def do_send_to( smtp )
49 from = from_address or raise ArgumentError, 'no from address'
50 (dests = destinations).empty? and raise ArgumentError, 'no receipient'
51 yield
52 send_to_0 smtp, from, dests
53 end
54 private :do_send_to
55
56 def send_to_0( smtp, from, to )
57 smtp.ready(from, to) do |f|
58 encoded "\r\n", 'j', f, ''
59 end
60 end
61
62 def ready_to_send
63 delete_no_send_fields
64 add_message_id
65 add_date
66 end
67
68 NOSEND_FIELDS = %w(
69 received
70 bcc
71 )
72
73 def delete_no_send_fields
74 NOSEND_FIELDS.each do |nm|
75 delete nm
76 end
77 delete_if {|n,v| v.empty? }
78 end
79
80 def add_message_id( fqdn = nil )
81 self.message_id = ::TMail::new_message_id(fqdn)
82 end
83
84 def add_date
85 self.date = Time.now
86 end
87
88 def mime_encode
89 if parts.empty?
90 mime_encode_singlepart
91 else
92 mime_encode_multipart true
93 end
94 end
95
96 def mime_encode_singlepart
97 self.mime_version = '1.0'
98 b = body
99 if NKF.guess(b) != NKF::BINARY
100 mime_encode_text b
101 else
102 mime_encode_binary b
103 end
104 end
105
106 def mime_encode_text( body )
107 self.body = NKF.nkf('-j -m0', body)
108 self.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
109 self.encoding = '7bit'
110 end
111
112 def mime_encode_binary( body )
113 self.body = [body].pack('m')
114 self.set_content_type 'application', 'octet-stream'
115 self.encoding = 'Base64'
116 end
117
118 def mime_encode_multipart( top = true )
119 self.mime_version = '1.0' if top
120 self.set_content_type 'multipart', 'mixed'
121 e = encoding(nil)
122 if e and not /\A(?:7bit|8bit|binary)\z/i === e
123 raise ArgumentError,
124 'using C.T.Encoding with multipart mail is not permitted'
125 end
126 end
127
128 end
129
130 #:stopdoc:
131 class DeleteFields
132
133 NOSEND_FIELDS = %w(
134 received
135 bcc
136 )
137
138 def initialize( nosend = nil, delempty = true )
139 @no_send_fields = nosend || NOSEND_FIELDS.dup
140 @delete_empty_fields = delempty
141 end
142
143 attr :no_send_fields
144 attr :delete_empty_fields, true
145
146 def exec( mail )
147 @no_send_fields.each do |nm|
148 delete nm
149 end
150 delete_if {|n,v| v.empty? } if @delete_empty_fields
151 end
152
153 end
154 #:startdoc:
155
156 #:stopdoc:
157 class AddMessageId
158
159 def initialize( fqdn = nil )
160 @fqdn = fqdn
161 end
162
163 attr :fqdn, true
164
165 def exec( mail )
166 mail.message_id = ::TMail::new_msgid(@fqdn)
167 end
168
169 end
170 #:startdoc:
171
172 #:stopdoc:
173 class AddDate
174
175 def exec( mail )
176 mail.date = Time.now
177 end
178
179 end
180 #:startdoc:
181
182 #:stopdoc:
183 class MimeEncodeAuto
184
185 def initialize( s = nil, m = nil )
186 @singlepart_composer = s || MimeEncodeSingle.new
187 @multipart_composer = m || MimeEncodeMulti.new
188 end
189
190 attr :singlepart_composer
191 attr :multipart_composer
192
193 def exec( mail )
194 if mail._builtin_multipart?
195 then @multipart_composer
196 else @singlepart_composer end.exec mail
197 end
198
199 end
200 #:startdoc:
201
202 #:stopdoc:
203 class MimeEncodeSingle
204
205 def exec( mail )
206 mail.mime_version = '1.0'
207 b = mail.body
208 if NKF.guess(b) != NKF::BINARY
209 on_text b
210 else
211 on_binary b
212 end
213 end
214
215 def on_text( body )
216 mail.body = NKF.nkf('-j -m0', body)
217 mail.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
218 mail.encoding = '7bit'
219 end
220
221 def on_binary( body )
222 mail.body = [body].pack('m')
223 mail.set_content_type 'application', 'octet-stream'
224 mail.encoding = 'Base64'
225 end
226
227 end
228 #:startdoc:
229
230 #:stopdoc:
231 class MimeEncodeMulti
232
233 def exec( mail, top = true )
234 mail.mime_version = '1.0' if top
235 mail.set_content_type 'multipart', 'mixed'
236 e = encoding(nil)
237 if e and not /\A(?:7bit|8bit|binary)\z/i === e
238 raise ArgumentError,
239 'using C.T.Encoding with multipart mail is not permitted'
240 end
241 mail.parts.each do |m|
242 exec m, false if m._builtin_multipart?
243 end
244 end
245
246 end
247 #:startdoc:
248 end # module TMail