Class: Texmailer::Iview

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

Instance Method Summary collapse

Constructor Details

#initialize(userdir, maxmsg = 100) ⇒ Iview

Returns a new instance of Iview.



13
14
15
16
17
18
19
20
# File 'lib/texmailer/iview.rb', line 13

def initialize userdir, maxmsg = 100
  @userdir = userdir
  @maxmsg = maxmsg
  @msglock = Mutex.new
  @emsg = nil
  @dienow = false
  @tid = nil
end

Instance Method Details

#asyncget(p) ⇒ Object



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

def asyncget p
  while true
    stat = pgets p,30 
    if ! stat
      @emsg = 'IMAP Fetch failed'
      break
    end
    if stat == "EXIT\n"
      break
    end
    if stat != "OK\n"
      @emsg = stat
      break
    end
    @msglock.synchronize {
        p.puts "OK"
        p.flush
        stat = pgets p,5
      }
    if !stat || stat != "OK\n"
      @emsg = 'IMAP Fetch failed'
      break
    end
  end
  p.close
  @emsg ||= 'OK'
  Thread.exit
end

#checkObject



59
60
61
62
63
64
65
# File 'lib/texmailer/iview.rb', line 59

def check
  return nil if running
  rval = @emsg
  @emsg = nil
  return nil if rval == "OK"
  return rval
end

#getcacheObject



42
43
44
45
46
47
48
49
# File 'lib/texmailer/iview.rb', line 42

def getcache
  c = [0,{}]
  cfile = @userdir+'/imapcache.dat'
  return 0,{} if ! File.readable? cfile
  @msglock.synchronize { c = Marshal.load(File.read(cfile)) }
  return 0,{} unless c[0] == CACHEVERSION
  return c[1], c[2]
end

#getsettingsObject



26
27
28
29
30
# File 'lib/texmailer/iview.rb', line 26

def getsettings
  stfile = @userdir+'/imap.cfg'
  return {} if ! File.readable? stfile
  return Marshal.load(File.read(stfile))
end

#pgets(p, tout) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/texmailer/iview.rb', line 67

def pgets p,tout
  s = nil
  for i in (1..tout)
    break if (s = IO.select([p],nil,nil,1)) != nil || @dienow
  end
  if s
    return p.gets
  else
    p.close
    return nil
  end
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/texmailer/iview.rb', line 80

def run
  if ! running && ! @emsg
    worker = File.expand_path(File.dirname(__FILE__)) + "/iworker"
    p = IO.popen(worker+" "+@userdir+" "+String(@maxmsg),"r+")
    Process.detach p.pid
    stat = pgets p,5
    return "Invalid IMAP settings" if ! stat
    return stat if stat != "OK\n"
    @tid = Thread.new(p) {|p| asyncget(p)}
    @tid.join(4)
  end
 
  return check
end

#runningObject



22
23
24
# File 'lib/texmailer/iview.rb', line 22

def running
  return  (@tid && @tid.status) ? true : false
end

#savesettings(iset) ⇒ Object



32
33
34
35
# File 'lib/texmailer/iview.rb', line 32

def savesettings iset
  stdat = Marshal.dump(iset)
  File.open(@userdir+'/imap.cfg','w',0600) {|f| f.write stdat}
end

#stopObject



51
52
53
54
55
56
57
# File 'lib/texmailer/iview.rb', line 51

def stop
  if running then
    @dienow = true
    @tid.join(10)
  end
  @dienow = false
end

#upabookObject



37
38
39
40
# File 'lib/texmailer/iview.rb', line 37

def upabook
  a,b = getcache
  $ab.upenv(b.values.collect {|x| x[0]})
end