Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionmailer / lib / action_mailer / vendor / tmail-1.1.0 / tmail / address.rb
blob224ed7090e5077a699e06029422f06a3130e0944
1 =begin rdoc
3 = Address handling class
5 =end
7 #--
8 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be
19 # included in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
30 # with permission of Minero Aoki.
31 #++
33 require 'tmail/encode'
34 require 'tmail/parser'
37 module TMail
39   class Address
41     include TextUtils
43     def Address.parse( str )
44       Parser.parse :ADDRESS, str
45     end
47     def address_group?
48       false
49     end
51     def initialize( local, domain )
52       if domain
53         domain.each do |s|
54           raise SyntaxError, 'empty word in domain' if s.empty?
55         end
56       end
58       @local = local
59       @domain = domain
60       @name   = nil
61       @routes = []
62     end
64     attr_reader :name
66     def name=( str )
67       @name = str
68       @name = nil if str and str.empty?
69     end
71     alias phrase  name
72     alias phrase= name=
74     attr_reader :routes
76     def inspect
77       "#<#{self.class} #{address()}>"
78     end
80     def local
81       return nil unless @local
82       return '""' if @local.size == 1 and @local[0].empty?
83       @local.map {|i| quote_atom(i) }.join('.')
84     end
86     def domain
87       return nil unless @domain
88       join_domain(@domain)
89     end
91     def spec
92       s = self.local
93       d = self.domain
94       if s and d
95         s + '@' + d
96       else
97         s
98       end
99     end
101     alias address  spec
103     def ==( other )
104       other.respond_to? :spec and self.spec == other.spec
105     end
107     alias eql? ==
109     def hash
110       @local.hash ^ @domain.hash
111     end
113     def dup
114       obj = self.class.new(@local.dup, @domain.dup)
115       obj.name = @name.dup if @name
116       obj.routes.replace @routes
117       obj
118     end
120     include StrategyInterface
122     def accept( strategy, dummy1 = nil, dummy2 = nil )
123       unless @local
124         strategy.meta '<>'   # empty return-path
125         return
126       end
128       spec_p = (not @name and @routes.empty?)
129       if @name
130         strategy.phrase @name
131         strategy.space
132       end
133       tmp = spec_p ? '' : '<'
134       unless @routes.empty?
135         tmp << @routes.map {|i| '@' + i }.join(',') << ':'
136       end
137       tmp << self.spec
138       tmp << '>' unless spec_p
139       strategy.meta tmp
140       strategy.lwsp ''
141     end
143   end
146   class AddressGroup
148     include Enumerable
150     def address_group?
151       true
152     end
154     def initialize( name, addrs )
155       @name = name
156       @addresses = addrs
157     end
159     attr_reader :name
160     
161     def ==( other )
162       other.respond_to? :to_a and @addresses == other.to_a
163     end
165     alias eql? ==
167     def hash
168       map {|i| i.hash }.hash
169     end
171     def []( idx )
172       @addresses[idx]
173     end
175     def size
176       @addresses.size
177     end
179     def empty?
180       @addresses.empty?
181     end
183     def each( &block )
184       @addresses.each(&block)
185     end
187     def to_a
188       @addresses.dup
189     end
191     alias to_ary to_a
193     def include?( a )
194       @addresses.include? a
195     end
197     def flatten
198       set = []
199       @addresses.each do |a|
200         if a.respond_to? :flatten
201           set.concat a.flatten
202         else
203           set.push a
204         end
205       end
206       set
207     end
209     def each_address( &block )
210       flatten.each(&block)
211     end
213     def add( a )
214       @addresses.push a
215     end
217     alias push add
218     
219     def delete( a )
220       @addresses.delete a
221     end
223     include StrategyInterface
225     def accept( strategy, dummy1 = nil, dummy2 = nil )
226       strategy.phrase @name
227       strategy.meta ':'
228       strategy.space
229       first = true
230       each do |mbox|
231         if first
232           first = false
233         else
234           strategy.meta ','
235         end
236         strategy.space
237         mbox.accept strategy
238       end
239       strategy.meta ';'
240       strategy.lwsp ''
241     end
243   end
245 end   # module TMail