Skip to content

Instantly share code, notes, and snippets.

@lorenzoplanas
Created June 23, 2010 12:01
Show Gist options
  • Save lorenzoplanas/449839 to your computer and use it in GitHub Desktop.
Save lorenzoplanas/449839 to your computer and use it in GitHub Desktop.
require 'eventmachine'
require 'net/pop'
class Popper
attr_accessor :pop, :read_messages
def initialize(hostname, port, user, password)
@pop = Net::POP3.start(hostname, port, user, password)
@read_messages = []
load_message_ids
end
def load_message_ids
histfile = File.open("popper.ids", "r")
histfile.each {|line| @read_messages << line.chomp }
end
def save_message_ids
histfile = File.open("popper.ids", "w")
@read_messages.each {|id| histfile.print id + "\n" }
histfile.close
end
def unread?(id)
!(@read_messages.include? id)
end
def inject_message(data)
email = EM::Protocols::SmtpClient.send(
:domain => "foo.com",
:host => 'localhost',
:port => 2525, # optional, defaults 25
:starttls => false, # use ssl
:from => "foo@bar.com",
:to => ["bar@foo.com"],
:header => {"Subject" => ""},
:body => data
)
email.callback { puts 'Email sent!' }
email.errback{ |e| puts 'Email failed!' }
end
def process
@pop.mails.select { |m| unread?(m.unique_id) }.each do |m|
inject_message(m.pop)
@read_messages << m.unique_id
end
@pop.finish
save_message_ids
@read_messages
end
end
EM.run do
EM::add_periodic_timer(60) do
EM.defer(proc { Popper.new("mail.foo.com", 110, "user", "pass").process }, proc {|result| p result})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment