Create a gist now

Instantly share code, notes, and snippets.

#
# biff.rb
# Simple biff(Mail check and notify tool)
#
# This script is example of Ruby/GNTP library
# (http://snaka.github.com/ruby_gntp/)
#
# Setup:
# you need follown gems.
# - ruby_gntp
# - pit
#
# Run:
# Please type following command in command prompt
# > ruby biff.rb
#
# author: snaka.gml@gmail.com
# license: public domain
#
require 'net/pop'
require 'nkf'
require 'yaml'
require 'rubygems'
require 'ruby_gntp'
require 'pit'
config = Pit.get("mail", :require => {
"server" => "pop3 server",
"port" => "pop3 port",
"user" => "user account",
"pass" => "password"
})
serv = config["server"]
port = config["port"].to_i
user = config["user"]
pass = config["pass"]
growl = GNTP.new
growl.register({
:app_name => "Simple Biff",
:app_icon => Dir.pwd + '\Email.png',
:notifies => [{
:name => "Mail",
:enabled => true,
},{
:name => "Info",
:enabled => true,
:icon => Dir.pwd + '\Info.png'
},{
:name => "Error",
:enabled => true,
:icon => Dir.pwd + '\Delete.png'
}]
})
cache_file = Dir.pwd + "/cache.yaml"
recieved = {}
begin
recieved = YAML.load_file(cache_file)
rescue
end
Net::POP3.start(serv, port, user, pass) do |pop|
exit 0 if pop.mails.empty?
mails = []
pop.each_mail do |m|
unless recieved.include?(m.unique_id)
recieved[m.unique_id] = true
mails << m
end
end
exit 0 if mails.size == 0
growl.notify({
:name => "Info",
:title => "Mail recieved",
:text => "#{pop.mails.size} mail(s) in mailbox.",
})
sleep 5
mails.each do |m|
subject = NKF.nkf('-Jw', m.header.match(/^Subject:\s+(.*)/)[1])
from = NKF.nkf('-Jw', m.header.match(/^From:\s+(.*)/)[1])
growl.notify({
:name => "Mail",
:title => "#{from}",
:text => "#{subject}"
})
sleep 1
end
end
YAML.dump(recieved, File.open(cache_file, "w"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment