# File /home/matt/rubymail/mail/address.rb, line 216 def Address.parse(string) require "English" puts "\nparse #{string.inspect}" if $DEBUG token_list = tokenize(string) puts "token_list #{token_list.inspect}" if $DEBUG # Split up each address in the mailbox list mailboxes = [] commas = [] in_angle = false token_list.each_with_index { |token, i| if token[0] == :special case token[1] when "<" in_angle = true when ">" in_angle = false when "," commas.push(i) unless in_angle end end } puts "commas #{commas.inspect}" if $DEBUG offset = 0 commas.each { |i| mailboxes.push(token_list.slice(0, i - offset)) token_list.slice!(0, i - offset + 1) offset += i - offset + 1 } mailboxes.push(token_list) puts "mailboxes #{mailboxes.inspect}" if $DEBUG results = [] mailboxes.each { |tokens| puts "## tokens #{tokens.inspect}" if $DEBUG # Find any ":" -- a group name if colon = tokens.index([:special, ':']) angle = tokens.index([:special, '<']) if angle.nil? || angle > colon tokens.slice!(0 .. tokens.index([:special, ':'])) end end # FIXME: perhaps move this into the above if statement? if tokens.index([:special, ";"]) tokens.slice!(tokens.index([:special, ";"])) end # Strip comments comments = [] tokens.delete_if { |token| if token[0] == :comment comments.push(token[1]) true end } comments = nil if comments.empty? puts "comments #{comments.inspect}" if $DEBUG puts "tokens #{tokens.inspect}" if $DEBUG # Find any "<" angle_index = tokens.index([:special, "<"]) unless angle_index.nil? # new style name-addr = [display-name] angle-addr if angle_index > 0 display_name = join_tokens_whitespace(tokens[0, angle_index]) else display_name = nil end tokens = tokens[angle_index..-1] puts "display_name #{display_name.inspect}" if $DEBUG puts "tokens #{tokens.inspect}" if $DEBUG # Find the ">" angle_index = tokens.index([:special, ">"]) || tokens.length - 1 address = tokens[1, angle_index - 1] tokens = tokens[angle_index + 1 .. -1] unless address.nil? if colon = address.rindex([:special, ':']) puts "found obs-domain #{address.inspect}" if $DEBUG address.slice!(0..colon) end end local, domain = split_address(address) else # New style addr-spec = local-part @ domain local, domain = split_address(tokens) end unless local.nil? local = join_tokens(local) unless local == '' puts "new address #{local.inspect} #{domain.inspect} #{comments.inspect}" if $DEBUG ret = Mail::Address.new ret.local = local ret.domain = join_tokens(domain) ret.display_name = display_name ret.comments = comments results.push(ret) end end } puts "parsed #{results.inspect}" if $DEBUG return results end