Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionmailer / lib / action_mailer / vendor / tmail-1.1.0 / tmail / net.rb
blob50b1dd95bef3735da98a9ee66c1488eaff8a3d7b
1 =begin rdoc
3 = Net provides SMTP wrapping
5 =end
6 #--
7 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
29 # with permission of Minero Aoki.
30 #++
32 require 'nkf'
35 module TMail
37   class Mail
39     def send_to( smtp )
40       do_send_to(smtp) do
41         ready_to_send
42       end
43     end
45     def send_text_to( smtp )
46       do_send_to(smtp) do
47         ready_to_send
48         mime_encode
49       end
50     end
52     def do_send_to( smtp )
53       from = from_address or raise ArgumentError, 'no from address'
54       (dests = destinations).empty? and raise ArgumentError, 'no receipient'
55       yield
56       send_to_0 smtp, from, dests
57     end
58     private :do_send_to
60     def send_to_0( smtp, from, to )
61       smtp.ready(from, to) do |f|
62         encoded "\r\n", 'j', f, ''
63       end
64     end
66     def ready_to_send
67       delete_no_send_fields
68       add_message_id
69       add_date
70     end
72     NOSEND_FIELDS = %w(
73       received
74       bcc
75     )
77     def delete_no_send_fields
78       NOSEND_FIELDS.each do |nm|
79         delete nm
80       end
81       delete_if {|n,v| v.empty? }
82     end
84     def add_message_id( fqdn = nil )
85       self.message_id = ::TMail::new_message_id(fqdn)
86     end
88     def add_date
89       self.date = Time.now
90     end
92     def mime_encode
93       if parts.empty?
94         mime_encode_singlepart
95       else
96         mime_encode_multipart true
97       end
98     end
100     def mime_encode_singlepart
101       self.mime_version = '1.0'
102       b = body
103       if NKF.guess(b) != NKF::BINARY
104         mime_encode_text b
105       else
106         mime_encode_binary b
107       end
108     end
110     def mime_encode_text( body )
111       self.body = NKF.nkf('-j -m0', body)
112       self.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
113       self.encoding = '7bit'
114     end
116     def mime_encode_binary( body )
117       self.body = [body].pack('m')
118       self.set_content_type 'application', 'octet-stream'
119       self.encoding = 'Base64'
120     end
122     def mime_encode_multipart( top = true )
123       self.mime_version = '1.0' if top
124       self.set_content_type 'multipart', 'mixed'
125       e = encoding(nil)
126       if e and not /\A(?:7bit|8bit|binary)\z/i === e
127         raise ArgumentError,
128               'using C.T.Encoding with multipart mail is not permitted'
129       end
130     end
132     def create_empty_mail
133       self.class.new(StringPort.new(''), @config)
134     end
136     def create_reply
137       setup_reply create_empty_mail()
138     end
140     def setup_reply( m )
141       if tmp = reply_addresses(nil)
142         m.to_addrs = tmp
143       end
145       mid = message_id(nil)
146       tmp = references(nil) || []
147       tmp.push mid if mid
148       m.in_reply_to = [mid] if mid
149       m.references = tmp unless tmp.empty?
150       m.subject = 'Re: ' + subject('').sub(/\A(?:\s*re:)+/i, '')
152       m
153     end
155     def create_forward
156       setup_forward create_empty_mail()
157     end
159     def setup_forward( mail )
160       m = Mail.new(StringPort.new(''))
161       m.body = decoded
162       m.set_content_type 'message', 'rfc822'
163       m.encoding = encoding('7bit')
164       mail.parts.push m
165     end
166   
167   end
170   class DeleteFields
172     NOSEND_FIELDS = %w(
173       received
174       bcc
175     )
177     def initialize( nosend = nil, delempty = true )
178       @no_send_fields = nosend || NOSEND_FIELDS.dup
179       @delete_empty_fields = delempty
180     end
182     attr :no_send_fields
183     attr :delete_empty_fields, true
185     def exec( mail )
186       @no_send_fields.each do |nm|
187         delete nm
188       end
189       delete_if {|n,v| v.empty? } if @delete_empty_fields
190     end
191   
192   end
195   class AddMessageId
197     def initialize( fqdn = nil )
198       @fqdn = fqdn
199     end
201     attr :fqdn, true
203     def exec( mail )
204       mail.message_id = ::TMail::new_msgid(@fqdn)
205     end
206   
207   end
210   class AddDate
212     def exec( mail )
213       mail.date = Time.now
214     end
215   
216   end
219   class MimeEncodeAuto
221     def initialize( s = nil, m = nil )
222       @singlepart_composer = s || MimeEncodeSingle.new
223       @multipart_composer  = m || MimeEncodeMulti.new
224     end
226     attr :singlepart_composer
227     attr :multipart_composer
229     def exec( mail )
230       if mail._builtin_multipart?
231       then @multipart_composer
232       else @singlepart_composer end.exec mail
233     end
234   
235   end
237   
238   class MimeEncodeSingle
240     def exec( mail )
241       mail.mime_version = '1.0'
242       b = mail.body
243       if NKF.guess(b) != NKF::BINARY
244         on_text b
245       else
246         on_binary b
247       end
248     end
250     def on_text( body )
251       mail.body = NKF.nkf('-j -m0', body)
252       mail.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
253       mail.encoding = '7bit'
254     end
256     def on_binary( body )
257       mail.body = [body].pack('m')
258       mail.set_content_type 'application', 'octet-stream'
259       mail.encoding = 'Base64'
260     end
261   
262   end
265   class MimeEncodeMulti
267     def exec( mail, top = true )
268       mail.mime_version = '1.0' if top
269       mail.set_content_type 'multipart', 'mixed'
270       e = encoding(nil)
271       if e and not /\A(?:7bit|8bit|binary)\z/i === e
272         raise ArgumentError,
273               'using C.T.Encoding with multipart mail is not permitted'
274       end
275       mail.parts.each do |m|
276         exec m, false if m._builtin_multipart?
277       end
278     end
280   end
282 end   # module TMail