aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib/action_mailer/vendor/tmail-1.2.3/tmail/address.rb
blob: fa8e5bcd8cb864a9be5458204b443563f2f6ae72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
=begin rdoc

= Address handling class

=end
#--
# Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Note: Originally licensed under LGPL v2+. Using MIT license for Rails
# with permission of Minero Aoki.
#++

require 'tmail/encode'
require 'tmail/parser'


module TMail

  # = Class Address
  # 
  # Provides a complete handling library for email addresses. Can parse a string of an
  # address directly or take in preformatted addresses themseleves.  Allows you to add
  # and remove phrases from the front of the address and provides a compare function for
  # email addresses.
  # 
  # == Parsing and Handling a Valid Address:
  # 
  # Just pass the email address in as a string to Address.parse:
  # 
  #  email = TMail::Address.parse('Mikel Lindsaar <mikel@lindsaar.net>)
  #  #=> #<TMail::Address mikel@lindsaar.net>
  #  email.address
  #  #=> "mikel@lindsaar.net"
  #  email.local
  #  #=> "mikel"
  #  email.domain
  #  #=> "lindsaar.net"
  #  email.name             # Aliased as phrase as well
  #  #=> "Mikel Lindsaar"
  # 
  # == Detecting an Invalid Address
  # 
  # If you want to check the syntactical validity of an email address, just pass it to
  # Address.parse and catch any SyntaxError:
  # 
  #  begin
  #    TMail::Mail.parse("mikel   2@@@@@ me .com")
  #  rescue TMail::SyntaxError
  #    puts("Invalid Email Address Detected")
  #  else
  #    puts("Address is valid")
  #  end
  #  #=> "Invalid Email Address Detected"
  class Address

    include TextUtils #:nodoc:
    
    # Sometimes you need to parse an address, TMail can do it for you and provide you with
    # a fairly robust method of detecting a valid address.
    # 
    # Takes in a string, returns a TMail::Address object.
    # 
    # Raises a TMail::SyntaxError on invalid email format
    def Address.parse( str )
      Parser.parse :ADDRESS, special_quote_address(str)
    end
    
    def Address.special_quote_address(str) #:nodoc:
      # Takes a string which is an address and adds quotation marks to special
      # edge case methods that the RACC parser can not handle.
      #
      # Right now just handles two edge cases:
      #
      # Full stop as the last character of the display name:
      #   Mikel L. <mikel@me.com>
      # Returns:
      #   "Mikel L." <mikel@me.com>
      #
      # Unquoted @ symbol in the display name:
      #   mikel@me.com <mikel@me.com>
      # Returns:
      #   "mikel@me.com" <mikel@me.com>
      #
      # Any other address not matching these patterns just gets returned as is. 
      case
      # This handles the missing "" in an older version of Apple Mail.app
      # around the display name when the display name contains a '@'
      # like 'mikel@me.com <mikel@me.com>'
      # Just quotes it to: '"mikel@me.com" <mikel@me.com>'
      when str =~ /\A([^"].+@.+[^"])\s(<.*?>)\Z/
        return "\"#{$1}\" #{$2}"
      # This handles cases where 'Mikel A. <mikel@me.com>' which is a trailing
      # full stop before the address section.  Just quotes it to
      # '"Mikel A. <mikel@me.com>"
      when str =~ /\A(.*?\.)\s(<.*?>)\Z/
        return "\"#{$1}\" #{$2}"
      else
        str
      end
    end

    def address_group? #:nodoc:
      false
    end

    # Address.new(local, domain)
    # 
    # Accepts:
    # 
    # * local - Left of the at symbol
    # 
    # * domain - Array of the domain split at the periods.
    # 
    # For example:
    # 
    #  Address.new("mikel", ["lindsaar", "net"])
    #  #=> "#<TMail::Address mikel@lindsaar.net>"
    def initialize( local, domain )
      if domain
        domain.each do |s|
          raise SyntaxError, 'empty word in domain' if s.empty?
        end
      end
      
      # This is to catch an unquoted "@" symbol in the local part of the
      # address.  Handles addresses like <"@"@me.com> and makes sure they
      # stay like <"@"@me.com> (previously were becomming <@@me.com>)
      if local && (local.join == '@' || local.join =~ /\A[^"].*?@.*?[^"]\Z/)
        @local = "\"#{local.join}\""
      else
        @local = local
      end

      @domain = domain
      @name   = nil
      @routes = []
    end

    # Provides the name or 'phrase' of the email address.
    # 
    # For Example:
    # 
    #  email = TMail::Address.parse("Mikel Lindsaar <mikel@lindsaar.net>")
    #  email.name
    #  #=> "Mikel Lindsaar"
    def name
      @name
    end

    # Setter method for the name or phrase of the email
    # 
    # For Example:
    # 
    #  email = TMail::Address.parse("mikel@lindsaar.net")
    #  email.name
    #  #=> nil
    #  email.name = "Mikel Lindsaar"
    #  email.to_s
    #  #=> "Mikel Lindsaar <mikel@me.com>"
    def name=( str )
      @name = str
      @name = nil if str and str.empty?
    end

    #:stopdoc:
    alias phrase  name
    alias phrase= name=
    #:startdoc:
    
    # This is still here from RFC 822, and is now obsolete per RFC2822 Section 4.
    # 
    # "When interpreting addresses, the route portion SHOULD be ignored."
    # 
    # It is still here, so you can access it.
    # 
    # Routes return the route portion at the front of the email address, if any.
    # 
    # For Example:
    #  email = TMail::Address.parse( "<@sa,@another:Mikel@me.com>")
    #  => #<TMail::Address Mikel@me.com>
    #  email.to_s
    #  => "<@sa,@another:Mikel@me.com>"
    #  email.routes
    #  => ["sa", "another"]
    def routes
      @routes
    end
    
    def inspect #:nodoc:
      "#<#{self.class} #{address()}>"
    end

    # Returns the local part of the email address
    # 
    # For Example:
    # 
    #  email = TMail::Address.parse("mikel@lindsaar.net")
    #  email.local
    #  #=> "mikel"
    def local
      return nil unless @local
      return '""' if @local.size == 1 and @local[0].empty?
      # Check to see if it is an array before trying to map it
      if @local.respond_to?(:map)
        @local.map {|i| quote_atom(i) }.join('.')
      else
        quote_atom(@local)
      end
    end

    # Returns the domain part of the email address
    # 
    # For Example:
    # 
    #  email = TMail::Address.parse("mikel@lindsaar.net")
    #  email.local
    #  #=> "lindsaar.net"
    def domain
      return nil unless @domain
      join_domain(@domain)
    end

    # Returns the full specific address itself
    # 
    # For Example:
    # 
    #  email = TMail::Address.parse("mikel@lindsaar.net")
    #  email.address
    #  #=> "mikel@lindsaar.net"
    def spec
      s = self.local
      d = self.domain
      if s and d
        s + '@' + d
      else
        s
      end
    end

    alias address spec

    # Provides == function to the email.  Only checks the actual address
    # and ignores the name/phrase component
    # 
    # For Example
    # 
    #  addr1 = TMail::Address.parse("My Address <mikel@lindsaar.net>")
    #  #=> "#<TMail::Address mikel@lindsaar.net>"
    #  addr2 = TMail::Address.parse("Another <mikel@lindsaar.net>")
    #  #=> "#<TMail::Address mikel@lindsaar.net>"
    #  addr1 == addr2
    #  #=> true
    def ==( other )
      other.respond_to? :spec and self.spec == other.spec
    end

    alias eql? ==

    # Provides a unique hash value for this record against the local and domain
    # parts, ignores the name/phrase value
    # 
    #  email = TMail::Address.parse("mikel@lindsaar.net")
    #  email.hash
    #  #=> 18767598
    def hash
      @local.hash ^ @domain.hash
    end

    # Duplicates a TMail::Address object returning the duplicate
    # 
    #  addr1 = TMail::Address.parse("mikel@lindsaar.net")
    #  addr2 = addr1.dup
    #  addr1.id == addr2.id
    #  #=> false
    def dup
      obj = self.class.new(@local.dup, @domain.dup)
      obj.name = @name.dup if @name
      obj.routes.replace @routes
      obj
    end

    include StrategyInterface #:nodoc:

    def accept( strategy, dummy1 = nil, dummy2 = nil ) #:nodoc:
      unless @local
        strategy.meta '<>'   # empty return-path
        return
      end

      spec_p = (not @name and @routes.empty?)
      if @name
        strategy.phrase @name
        strategy.space
      end
      tmp = spec_p ? '' : '<'
      unless @routes.empty?
        tmp << @routes.map {|i| '@' + i }.join(',') << ':'
      end
      tmp << self.spec
      tmp << '>' unless spec_p
      strategy.meta tmp
      strategy.lwsp ''
    end

  end


  class AddressGroup

    include Enumerable

    def address_group?
      true
    end

    def initialize( name, addrs )
      @name = name
      @addresses = addrs
    end

    attr_reader :name
    
    def ==( other )
      other.respond_to? :to_a and @addresses == other.to_a
    end

    alias eql? ==

    def hash
      map {|i| i.hash }.hash
    end

    def []( idx )
      @addresses[idx]
    end

    def size
      @addresses.size
    end

    def empty?
      @addresses.empty?
    end

    def each( &block )
      @addresses.each(&block)
    end

    def to_a
      @addresses.dup
    end

    alias to_ary to_a

    def include?( a )
      @addresses.include? a
    end

    def flatten
      set = []
      @addresses.each do |a|
        if a.respond_to? :flatten
          set.concat a.flatten
        else
          set.push a
        end
      end
      set
    end

    def each_address( &block )
      flatten.each(&block)
    end

    def add( a )
      @addresses.push a
    end

    alias push add
    
    def delete( a )
      @addresses.delete a
    end

    include StrategyInterface

    def accept( strategy, dummy1 = nil, dummy2 = nil )
      strategy.phrase @name
      strategy.meta ':'
      strategy.space
      first = true
      each do |mbox|
        if first
          first = false
        else
          strategy.meta ','
        end
        strategy.space
        mbox.accept strategy
      end
      strategy.meta ';'
      strategy.lwsp ''
    end

  end

end   # module TMail