10 ## read in stored people
11 IO.readlines(fn).map do |l|
12 l =~ /^(.*)?:\s+(\d+)\s+(.*)$/ or raise "can't parse: #{l}"
13 email, time, name = $1, $2, $3
14 @@people[email] = Person.new name, email, time, false
15 end if File.exists? fn
17 self.class.i_am_the_instance self
21 File.open(@fn, "w") do |f|
22 @@people.each do |email, p|
23 f.puts "#{p.email}: #{p.timestamp} #{p.name}"
28 def self.people_for s, opts={}
30 s.split_on_commas.map { |ss| self.person_for ss, opts }
33 def self.person_for s, opts={}
34 p = Person.from_address(s) or return nil
35 p.definitive = true if opts[:definitive]
40 oldp = @@people[p.email]
42 if oldp.nil? || p.better_than?(oldp)
46 @@people[p.email].touch!
51 ## don't create these by hand. rather, go through personmanager, to
52 ## ensure uniqueness and overriding.
54 attr_accessor :name, :email, :timestamp
55 bool_accessor :definitive
57 def initialize name, email, timestamp=0, definitive=false
58 raise ArgumentError, "email can't be nil" unless email
61 @name = name.gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ")
62 if @name =~ /^(['"]\s*)(.*?)(\s*["'])$/
67 @email = email.gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ").downcase
68 @definitive = definitive
69 @timestamp = timestamp
72 ## heuristic: whether the name attached to this email is "real", i.e.
73 ## we should bother to store it.
75 @email =~ /no\-?reply/
79 return false if o.definitive? || generic?
80 return true if definitive?
81 o.name.nil? || (name && name.length > o.name.length && name =~ /[a-z]/)
84 def to_s; "#@name <#@email>" end
86 def touch!; @timestamp = Time.now.to_i end
88 # def == o; o && o.email == email; end
90 # def hash; [name, email].hash; end
113 def mediumname; @name || @email; end
118 "#{@name.inspect} <#@email>" # escape quotes
127 ## when sorting addresses, sort by this
132 when /^\S+ \S+ (\S+)/
143 def self.from_address s
146 ## try and parse an email address and name
149 when /["'](.*?)["'] <(.*?)>/, /([^,]+) <(.*?)>/
151 [a.gsub('\"', '"'), b]
152 when /<((\S+?)@\S+?)>/
160 Person.new name, email
163 def eql? o; email.eql? o.email end
164 def hash; email.hash end