removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionmailer / lib / action_mailer / vendor / tmail / facade.rb
blob1ecd64bff89364d0591538913668da4beb03f172
2 # facade.rb
4 #--
5 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
27 # with permission of Minero Aoki.
28 #++
30 require 'tmail/utils'
33 module TMail
35   class Mail
37     def header_string( name, default = nil )
38       h = @header[name.downcase] or return default
39       h.to_s
40     end
42     ###
43     ### attributes
44     ###
46     include TextUtils
48     def set_string_array_attr( key, strs )
49       strs.flatten!
50       if strs.empty?
51         @header.delete key.downcase
52       else
53         store key, strs.join(', ')
54       end
55       strs
56     end
57     private :set_string_array_attr
59     def set_string_attr( key, str )
60       if str
61         store key, str
62       else
63         @header.delete key.downcase
64       end
65       str
66     end
67     private :set_string_attr
69     def set_addrfield( name, arg )
70       if arg
71         h = HeaderField.internal_new(name, @config)
72         h.addrs.replace [arg].flatten
73         @header[name] = h
74       else
75         @header.delete name
76       end
77       arg
78     end
79     private :set_addrfield
81     def addrs2specs( addrs )
82       return nil unless addrs
83       list = addrs.map {|addr|
84           if addr.address_group?
85           then addr.map {|a| a.spec }
86           else addr.spec
87           end
88       }.flatten
89       return nil if list.empty?
90       list
91     end
92     private :addrs2specs
95     #
96     # date time
97     #
99     def date( default = nil )
100       if h = @header['date']
101         h.date
102       else
103         default
104       end
105     end
107     def date=( time )
108       if time
109         store 'Date', time2str(time)
110       else
111         @header.delete 'date'
112       end
113       time
114     end
116     def strftime( fmt, default = nil )
117       if t = date
118         t.strftime(fmt)
119       else
120         default
121       end
122     end
125     #
126     # destination
127     #
129     def to_addrs( default = nil )
130       if h = @header['to']
131         h.addrs
132       else
133         default
134       end
135     end
137     def cc_addrs( default = nil )
138       if h = @header['cc']
139         h.addrs
140       else
141         default
142       end
143     end
145     def bcc_addrs( default = nil )
146       if h = @header['bcc']
147         h.addrs
148       else
149         default
150       end
151     end
153     def to_addrs=( arg )
154       set_addrfield 'to', arg
155     end
157     def cc_addrs=( arg )
158       set_addrfield 'cc', arg
159     end
161     def bcc_addrs=( arg )
162       set_addrfield 'bcc', arg
163     end
165     def to( default = nil )
166       addrs2specs(to_addrs(nil)) || default
167     end
169     def cc( default = nil )
170       addrs2specs(cc_addrs(nil)) || default
171     end
173     def bcc( default = nil )
174       addrs2specs(bcc_addrs(nil)) || default
175     end
177     def to=( *strs )
178       set_string_array_attr 'To', strs
179     end
181     def cc=( *strs )
182       set_string_array_attr 'Cc', strs
183     end
185     def bcc=( *strs )
186       set_string_array_attr 'Bcc', strs
187     end
190     #
191     # originator
192     #
194     def from_addrs( default = nil )
195       if h = @header['from']
196         h.addrs
197       else
198         default
199       end
200     end
202     def from_addrs=( arg )
203       set_addrfield 'from', arg
204     end
206     def from( default = nil )
207       addrs2specs(from_addrs(nil)) || default
208     end
210     def from=( *strs )
211       set_string_array_attr 'From', strs
212     end
214     def friendly_from( default = nil )
215       h = @header['from']
216       a, = h.addrs
217       return default unless a
218       return a.phrase if a.phrase
219       return h.comments.join(' ') unless h.comments.empty?
220       a.spec
221     end
224     def reply_to_addrs( default = nil )
225       if h = @header['reply-to']
226         h.addrs
227       else
228         default
229       end
230     end
232     def reply_to_addrs=( arg )
233       set_addrfield 'reply-to', arg
234     end
236     def reply_to( default = nil )
237       addrs2specs(reply_to_addrs(nil)) || default
238     end
240     def reply_to=( *strs )
241       set_string_array_attr 'Reply-To', strs
242     end
245     def sender_addr( default = nil )
246       f = @header['sender'] or return default
247       f.addr                or return default
248     end
250     def sender_addr=( addr )
251       if addr
252         h = HeaderField.internal_new('sender', @config)
253         h.addr = addr
254         @header['sender'] = h
255       else
256         @header.delete 'sender'
257       end
258       addr
259     end
261     def sender( default )
262       f = @header['sender'] or return default
263       a = f.addr            or return default
264       a.spec
265     end
267     def sender=( str )
268       set_string_attr 'Sender', str
269     end
272     #
273     # subject
274     #
276     def subject( default = nil )
277       if h = @header['subject']
278         h.body
279       else
280         default
281       end
282     end
283     alias quoted_subject subject
285     def subject=( str )
286       set_string_attr 'Subject', str
287     end
290     #
291     # identity & threading
292     #
294     def message_id( default = nil )
295       if h = @header['message-id']
296         h.id || default
297       else
298         default
299       end
300     end
302     def message_id=( str )
303       set_string_attr 'Message-Id', str
304     end
306     def in_reply_to( default = nil )
307       if h = @header['in-reply-to']
308         h.ids
309       else
310         default
311       end
312     end
314     def in_reply_to=( *idstrs )
315       set_string_array_attr 'In-Reply-To', idstrs
316     end
318     def references( default = nil )
319       if h = @header['references']
320         h.refs
321       else
322         default
323       end
324     end
326     def references=( *strs )
327       set_string_array_attr 'References', strs
328     end
331     #
332     # MIME headers
333     #
335     def mime_version( default = nil )
336       if h = @header['mime-version']
337         h.version || default
338       else
339         default
340       end
341     end
343     def mime_version=( m, opt = nil )
344       if opt
345         if h = @header['mime-version']
346           h.major = m
347           h.minor = opt
348         else
349           store 'Mime-Version', "#{m}.#{opt}"
350         end
351       else
352         store 'Mime-Version', m
353       end
354       m
355     end
358     def content_type( default = nil )
359       if h = @header['content-type']
360         h.content_type || default
361       else
362         default
363       end
364     end
366     def main_type( default = nil )
367       if h = @header['content-type']
368         h.main_type || default
369       else
370         default
371       end
372     end
374     def sub_type( default = nil )
375       if h = @header['content-type']
376         h.sub_type || default
377       else
378         default
379       end
380     end
382     def set_content_type( str, sub = nil, param = nil )
383       if sub
384         main, sub = str, sub
385       else
386         main, sub = str.split(%r</>, 2)
387         raise ArgumentError, "sub type missing: #{str.inspect}" unless sub
388       end
389       if h = @header['content-type']
390         h.main_type = main
391         h.sub_type  = sub
392         h.params.clear
393       else
394         store 'Content-Type', "#{main}/#{sub}"
395       end
396       @header['content-type'].params.replace param if param
398       str
399     end
401     alias content_type= set_content_type
402     
403     def type_param( name, default = nil )
404       if h = @header['content-type']
405         h[name] || default
406       else
407         default
408       end
409     end
411     def charset( default = nil )
412       if h = @header['content-type']
413         h['charset'] or default
414       else
415         default
416       end
417     end
419     def charset=( str )
420       if str
421         if h = @header[ 'content-type' ]
422           h['charset'] = str
423         else
424           store 'Content-Type', "text/plain; charset=#{str}"
425         end
426       end
427       str
428     end
431     def transfer_encoding( default = nil )
432       if h = @header['content-transfer-encoding']
433         h.encoding || default
434       else
435         default
436       end
437     end
439     def transfer_encoding=( str )
440       set_string_attr 'Content-Transfer-Encoding', str
441     end
443     alias encoding                   transfer_encoding
444     alias encoding=                  transfer_encoding=
445     alias content_transfer_encoding  transfer_encoding
446     alias content_transfer_encoding= transfer_encoding=
449     def disposition( default = nil )
450       if h = @header['content-disposition']
451         h.disposition || default
452       else
453         default
454       end
455     end
457     alias content_disposition     disposition
459     def set_disposition( str, params = nil )
460       if h = @header['content-disposition']
461         h.disposition = str
462         h.params.clear
463       else
464         store('Content-Disposition', str)
465         h = @header['content-disposition']
466       end
467       h.params.replace params if params
468     end
470     alias disposition=            set_disposition
471     alias set_content_disposition set_disposition
472     alias content_disposition=    set_disposition
473     
474     def disposition_param( name, default = nil )
475       if h = @header['content-disposition']
476         h[name] || default
477       else
478         default
479       end
480     end
482     ###
483     ### utils
484     ###
486     def create_reply
487       mail = TMail::Mail.parse('')
488       mail.subject = 'Re: ' + subject('').sub(/\A(?:\[[^\]]+\])?(?:\s*Re:)*\s*/i, '')
489       mail.to_addrs = reply_addresses([])
490       mail.in_reply_to = [message_id(nil)].compact
491       mail.references = references([]) + [message_id(nil)].compact
492       mail.mime_version = '1.0'
493       mail
494     end
497     def base64_encode
498       store 'Content-Transfer-Encoding', 'Base64'
499       self.body = Base64.folding_encode(self.body)
500     end
502     def base64_decode
503       if /base64/i === self.transfer_encoding('')
504         store 'Content-Transfer-Encoding', '8bit'
505         self.body = Base64.decode(self.body, @config.strict_base64decode?)
506       end
507     end
510     def destinations( default = nil )
511       ret = []
512       %w( to cc bcc ).each do |nm|
513         if h = @header[nm]
514           h.addrs.each {|i| ret.push i.address }
515         end
516       end
517       ret.empty? ? default : ret
518     end
520     def each_destination( &block )
521       destinations([]).each do |i|
522         if Address === i
523           yield i
524         else
525           i.each(&block)
526         end
527       end
528     end
530     alias each_dest each_destination
533     def reply_addresses( default = nil )
534       reply_to_addrs(nil) or from_addrs(nil) or default
535     end
537     def error_reply_addresses( default = nil )
538       if s = sender(nil)
539         [s]
540       else
541         from_addrs(default)
542       end
543     end
546     def multipart?
547       main_type('').downcase == 'multipart'
548     end
550   end   # class Mail
552 end   # module TMail