Skip to content

Instantly share code, notes, and snippets.

@snaka
Created May 7, 2009 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snaka/108168 to your computer and use it in GitHub Desktop.
Save snaka/108168 to your computer and use it in GitHub Desktop.
#
# 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