Class: Texmailer::Adbook

Inherits:
Object
  • Object
show all
Defined in:
lib/texmailer/adbook.rb

Instance Method Summary collapse

Constructor Details

#initialize(userdir) ⇒ Adbook

Returns a new instance of Adbook.



10
11
12
13
14
15
16
17
# File 'lib/texmailer/adbook.rb', line 10

def initialize userdir
  @afile = userdir+'/abook.dat'
  if File.readable? @afile
    data = Marshal.load(File.read(@afile))
    @ab = data[1] if data[0] == ABVERSION
  end
  @ab ||= {}
end

Instance Method Details

#saveObject



19
20
21
22
# File 'lib/texmailer/adbook.rb', line 19

def save
  s = Marshal.dump([ABVERSION,@ab])
  File.open(@afile,'w',0600) {|f| f.write s}
end

#search(q) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/texmailer/adbook.rb', line 25

def search q
  subs = {}
  lg = q.length-1
  query= q.upcase
  @ab.each_pair { |em,nm|
      if em[0..lg].upcase == query
        subs[em] = nm
      else
        nm.split(" ").each { |x|
          if x[0..lg].upcase == query
            subs[em] = nm
            break
          end
        }
      end
    }
  lst = subs.to_a.collect{ |x|
      nstr = x[1].strip
      nstr = '"' + nstr + '" ' if nstr.length > 0
      '\''+ nstr + '<'+x[0]+'>\'' 
    }
  return lst.join(",")
end

#upemail(name, email) ⇒ Object



49
50
51
52
# File 'lib/texmailer/adbook.rb', line 49

def upemail name,email
  name = '' if name[0..0] == '='
  @ab[email] = name if !(ename = @ab[email]) || ename.length < name.length
end

#upenv(evs) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/texmailer/adbook.rb', line 84

def upenv evs
  evs.each { |x|
      [x.from, x.to, x.cc].each { |y| 
        y.each { |z|
          upemail z.name||'', z.mailbox+"@"+z.host
        } if y
      }
    }
  save
end

#uptolist(em) ⇒ Object



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
# File 'lib/texmailer/adbook.rb', line 54

def uptolist em
  arr = []
  pfx = ''
  while m = /[\,\"]/.match(em)
    if m[0] == ','
      arr = arr + [(pfx + m.pre_match).strip]
      pfx = ''
      em = m.post_match
    elsif m2 = /[^\\]\"/.match(m.post_match)
      pfx = pfx + m.pre_match + m[0] + m2.pre_match + m2[0]
      em = m2.post_match
    else
      pfx = pfx + em
      em = ''
    end
  end
  if (s = (pfx+em).strip).length > 0
    arr = arr + [s]
  end
  arr.each { |x|
      if m = /.*?\"(.*?[^\\])\"\s*<([^@\s]*?@[^@\s]*?)>/.match(x)
        upemail m[1].strip, m[2].strip
      elsif m = /(.*?)<([^@]*?@[^@]*?)>/.match(x)
        upemail m[1].strip, m[2].strip
      elsif m = /[^\s\"]*?@[^\s\"]*/.match(x)
        upemail '', m[0]
      end
    }
end