class IMAP_Notifier
Constants
- VERSION
Public Class Methods
delete_pid(pidFile)
click to toggle source
# File lib/imap_notifier/base.rb, line 26 def self.delete_pid(pidFile) File.delete(pidFile) if File.exists?(pidFile) end
kill_process()
click to toggle source
# File lib/imap_notifier/base.rb, line 2 def self.kill_process matching_pids = Dir['/tmp/imap_notifier*.pid'] if matching_pids.length > 1 prompts = ["[Q] Cancel"] matching_pids.each_with_index do |p_file, i| prompts.push("[#{i}] #{p_file}") end pidIndex = ask("Select PID by number\n" + prompts.join("\n")) { |q| q.validate = /^[0-#{matching_pids.length-1},q,Q]{1}$/ } return if pidIndex.downcase == "q" pidFile = Dir['/tmp/imap_notifier*.pid'][pidIndex.to_i] else pidFile = Dir['/tmp/imap_notifier*.pid'].first end 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(pidFile) end
new(opts={})
click to toggle source
# File lib/imap_notifier/base.rb, line 30 def initialize(opts={}) config opts if File.exists?(@custom_pid) && IO.readlines(@custom_pid).any? puts "#{@custom_pid} already exists -- Exiting..." exit @ignore_exit_code ? 0 : 1 end @notifier = IMAP_Notifier::Alert.new pid = fork do $stderr.reopen(ERRFILE, 'w') at_exit { self.stop } run end File.open(@custom_pid, 'w') { |f| f.write pid } end
Public Instance Methods
_imap()
click to toggle source
# File lib/imap_notifier/base.rb, line 89 def _imap imap = Net::IMAP.new(@imap_server, { :port => 993, :ssl => true } ) imap.login(@user, @password) @notifier.alert("#{@user} connected!", :group => @domain) return imap end
alert_folder(f, ids)
click to toggle source
# File lib/imap_notifier/base.rb, line 62 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={})
click to toggle source
# 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 @ignore_exit_code = opts[:ignore_exit_code].to_s == 'true' @custom_pid = opts[:pid] || PIDFILE end
handle_exception(err)
click to toggle source
# File lib/imap_notifier/base.rb, line 101 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
imap()
click to toggle source
# File lib/imap_notifier/base.rb, line 79 def imap @imap ||= _imap end
run()
click to toggle source
# File lib/imap_notifier/base.rb, line 48 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)
click to toggle source
# File lib/imap_notifier/base.rb, line 96 def say_goodbye(klass) return if !(klass.eql? "Interrupt") @notifier.alert("Goodbye!", :group => @domain) end
stop()
click to toggle source
# File lib/imap_notifier/base.rb, line 83 def stop IMAP_Notifier::Alert.remove self.class.delete_pid exit end
Private Instance Methods
ensure_perms(file)
click to toggle source
# File lib/imap_notifier/config.rb, line 60 def ensure_perms file m = sprintf("%o", File.stat(file).mode).split('').last(3).join().to_i return if m.eql? 600 warn "#{file} permissions should be set to 600 #{m}" exit 1 end
get_password()
click to toggle source
# File lib/imap_notifier/config.rb, line 26 def get_password if $key_name && $key_account key = %x{security find-internet-password -w -a #{$key_account} -s #{$key_name}} elsif $pass key = %x{pass #{$pass}} elsif $onepass if $one_path key = %x{1pass --path "#{$one_path}" #{$onepass}} else key = %x{1pass #{$onepass}} end end if key.nil? || key.empty? key = pass_prompt end return key.chomp end
mbox_conf(ary)
click to toggle source
# File lib/imap_notifier/config.rb, line 21 def mbox_conf ary Hash[ary.map{ |f| [f,Array.new] }] end
pass_prompt(prompt="Enter Password: ")
click to toggle source
# File lib/imap_notifier/config.rb, line 45 def pass_prompt(prompt="Enter Password: ") ask(prompt) { |q| q.echo = false } end
read_conf(opts)
click to toggle source
# File lib/imap_notifier/config.rb, line 49 def read_conf opts config = opts[:config] || "~/.imap_notifier" file = File.expand_path(config) return if ! File.exists? file ensure_perms file YAML.load(File.open(file)).each do |k,v| opts[k.to_sym] ||= v end opts[:user] ||= ENV['USER'] end