Class: IMAP_Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/imap_notifier/base.rb,
lib/imap_notifier/config.rb,
lib/imap_notifier/version.rb

Defined Under Namespace

Classes: Alert

Constant Summary collapse

VERSION =
'0.2.8'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ IMAP_Notifier

Returns a new instance of IMAP_Notifier.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/imap_notifier/base.rb', line 16

def initialize(opts={})
  config opts
  @notifier = IMAP_Notifier::Alert.new

  pid = fork do
    $stderr.reopen(ERRFILE, 'w')
    at_exit {  self.stop }
    run
  end
  File.open(PIDFILE, 'w') { |f| f.write pid }
end

Class Method Details

.delete_pidObject



12
13
14
# File 'lib/imap_notifier/base.rb', line 12

def self.delete_pid
  File.delete(PIDFILE) if File.exists?(PIDFILE)
end

.kill_processObject



2
3
4
5
6
7
8
9
10
# File 'lib/imap_notifier/base.rb', line 2

def self.kill_process
  pid = IO.readlines(PIDFILE).first.to_i
  puts "Killing PID: #{pid}"
  Process.kill("SIGINT", pid)
rescue Errno::ESRCH, Errno::ENOENT => e
  puts "#{e.message} - Exiting..."
ensure
  self.delete_pid
end

Instance Method Details

#_imapObject



69
70
71
72
73
74
# File 'lib/imap_notifier/base.rb', line 69

def _imap
  imap = Net::IMAP.new(@imap_server, { :port => 993, :ssl => true } )
  imap.(@user, @password)
  @notifier.alert("#{@user} connected!", :group => @domain)
  return imap
end

#alert_folder(f, ids) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/imap_notifier/base.rb', line 42

def alert_folder(f, ids)
  imap.examine(f)
  unseen    = imap.search(["UNSEEN"])
  unalerted = unseen - ids

  if unalerted.length > @max_mail
    @notifier.alert("#{f}: #{unalerted.length} new mails", :group => f)
  else
    unalerted.each do |msg_id|
      msg = imap.fetch(msg_id, "ENVELOPE")[0].attr["ENVELOPE"]
      next if ids.include?(msg.message_id)
      @notifier.alert("#{f} #{msg.subject}", :title => "Mail from #{msg.from[0].mailbox}@#{msg.from[0].host}", :group => msg_id)
    end
  end
  @folders[f] = unseen
end

#config(opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/imap_notifier/config.rb', line 2

def config(opts={})
  read_conf opts
  @imap_server = opts[:server] || IMAP_SERVER
  @domain      = opts[:domain] || @imap_server.split('.').pop(2).join('.')
  @user        = "#{opts[:user]}@#{@domain}"
  $key_name    = opts[:key_name] || false
  $key_account = opts[:key_account] || false
  $pass        = opts[:pass] || false
  $one_path    = opts[:one_path] || false
  $onepass     = opts[:onepass] || false
  @password    = opts[:password] || get_password
  @folders     = mbox_conf opts[:folders] || ['INBOX']
  @debug       = opts[:debug] || false
  @max_mail    = opts[:max]   || MAX_MAIL
end

#handle_exception(err) ⇒ Object



81
82
83
84
85
86
# File 'lib/imap_notifier/base.rb', line 81

def handle_exception(err)
  @notifier.alert(err.message, :title => err.class, :open => "file://#{ERRFILE}")
  warn("#{Time.now} - #{@user}")
  warn("#{err.class}: #{err.message}")
  err.backtrace.map{ |e| warn e }
end

#imapObject



59
60
61
# File 'lib/imap_notifier/base.rb', line 59

def imap
  @imap ||= _imap
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/imap_notifier/base.rb', line 28

def run
  begin
    @folders.each do |f, ids|
      alert_folder(f, ids)
    end
    sleep SLEEP
  rescue EOFError
    @imap = nil
  rescue Exception => err
    say_goodbye(err.class.to_s) || handle_exception(err)
    stop
  end while true
end

#say_goodbye(klass) ⇒ Object



76
77
78
79
# File 'lib/imap_notifier/base.rb', line 76

def say_goodbye(klass)
  return if !(klass.eql? "Interrupt")
  @notifier.alert("Goodbye!", :group => @domain)
end

#stopObject



63
64
65
66
67
# File 'lib/imap_notifier/base.rb', line 63

def stop
  IMAP_Notifier::Alert.remove
  self.class.delete_pid
  exit
end