removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionmailer / lib / action_mailer / vendor / tmail / address.rb
blob235ec7618baa95110b228751e69b9cbd0a7cb3a6
2 # address.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/encode'
31 require 'tmail/parser'
34 module TMail
36   class Address
38     include TextUtils
40     def Address.parse( str )
41       Parser.parse :ADDRESS, str
42     end
44     def address_group?
45       false
46     end
48     def initialize( local, domain )
49       if domain
50         domain.each do |s|
51           raise SyntaxError, 'empty word in domain' if s.empty?
52         end
53       end
54       @local = local
55       @domain = domain
56       @name   = nil
57       @routes = []
58     end
60     attr_reader :name
62     def name=( str )
63       @name = str
64       @name = nil if str and str.empty?
65     end
67     alias phrase  name
68     alias phrase= name=
70     attr_reader :routes
72     def inspect
73       "#<#{self.class} #{address()}>"
74     end
76     def local
77       return nil unless @local
78       return '""' if @local.size == 1 and @local[0].empty?
79       @local.map {|i| quote_atom(i) }.join('.')
80     end
82     def domain
83       return nil unless @domain
84       join_domain(@domain)
85     end
87     def spec
88       s = self.local
89       d = self.domain
90       if s and d
91         s + '@' + d
92       else
93         s
94       end
95     end
97     alias address  spec
100     def ==( other )
101       other.respond_to? :spec and self.spec == other.spec
102     end
104     alias eql? ==
106     def hash
107       @local.hash ^ @domain.hash
108     end
110     def dup
111       obj = self.class.new(@local.dup, @domain.dup)
112       obj.name = @name.dup if @name
113       obj.routes.replace @routes
114       obj
115     end
117     include StrategyInterface
119     def accept( strategy, dummy1 = nil, dummy2 = nil )
120       unless @local
121         strategy.meta '<>'   # empty return-path
122         return
123       end
125       spec_p = (not @name and @routes.empty?)
126       if @name
127         strategy.phrase @name
128         strategy.space
129       end
130       tmp = spec_p ? '' : '<'
131       unless @routes.empty?
132         tmp << @routes.map {|i| '@' + i }.join(',') << ':'
133       end
134       tmp << self.spec
135       tmp << '>' unless spec_p
136       strategy.meta tmp
137       strategy.lwsp ''
138     end
140   end
143   class AddressGroup
145     include Enumerable
147     def address_group?
148       true
149     end
151     def initialize( name, addrs )
152       @name = name
153       @addresses = addrs
154     end
156     attr_reader :name
157     
158     def ==( other )
159       other.respond_to? :to_a and @addresses == other.to_a
160     end
162     alias eql? ==
164     def hash
165       map {|i| i.hash }.hash
166     end
168     def []( idx )
169       @addresses[idx]
170     end
172     def size
173       @addresses.size
174     end
176     def empty?
177       @addresses.empty?
178     end
180     def each( &block )
181       @addresses.each(&block)
182     end
184     def to_a
185       @addresses.dup
186     end
188     alias to_ary to_a
190     def include?( a )
191       @addresses.include? a
192     end
194     def flatten
195       set = []
196       @addresses.each do |a|
197         if a.respond_to? :flatten
198           set.concat a.flatten
199         else
200           set.push a
201         end
202       end
203       set
204     end
206     def each_address( &block )
207       flatten.each(&block)
208     end
210     def add( a )
211       @addresses.push a
212     end
214     alias push add
215     
216     def delete( a )
217       @addresses.delete a
218     end
220     include StrategyInterface
222     def accept( strategy, dummy1 = nil, dummy2 = nil )
223       strategy.phrase @name
224       strategy.meta ':'
225       strategy.space
226       first = true
227       each do |mbox|
228         if first
229           first = false
230         else
231           strategy.meta ','
232         end
233         strategy.space
234         mbox.accept strategy
235       end
236       strategy.meta ';'
237       strategy.lwsp ''
238     end
240   end
242 end   # module TMail