trabajo-afectivo/app/models/channel/imap.rb

42 lines
1.2 KiB
Ruby
Raw Normal View History

2012-04-10 14:06:46 +00:00
require 'net/imap'
class Channel::IMAP < Channel::EmailParser
2012-04-10 14:06:46 +00:00
include UserInfo
def fetch (channel)
2012-04-13 17:06:09 +00:00
puts "fetching imap (#{channel[:options][:host]}/#{channel[:options][:user]})"
2012-04-10 14:06:46 +00:00
imap = Net::IMAP.new(channel[:options][:host], 993, true, nil, false )
imap.authenticate('LOGIN', channel[:options][:user], channel[:options][:password])
2012-04-10 14:06:46 +00:00
imap.select('INBOX')
2012-04-13 17:06:09 +00:00
count = 0
count_all = imap.search(['ALL']).count
2012-04-10 14:06:46 +00:00
imap.search(['ALL']).each do |message_id|
2012-04-13 17:06:09 +00:00
count += 1
puts " - message #{count.to_s}/#{count_all.to_s}"
2012-04-10 14:06:46 +00:00
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
# puts msg.to_s
# delete email from server after article was created
if process(channel, msg)
imap.store(message_id, "+FLAGS", [:Deleted])
2012-04-10 14:06:46 +00:00
end
end
imap.expunge()
imap.disconnect()
2012-04-13 17:06:09 +00:00
if count == 0
puts " - no message"
end
puts "done"
2012-04-10 14:06:46 +00:00
end
def send(attr, notification = false)
channel = Channel.where( :area => 'Email::Outbound', :active => true ).first
begin
c = eval 'Channel::' + channel[:adapter] + '.new'
c.send(attr, channel, notification)
rescue Exception => e
puts "can't use " + 'Channel::' + channel[:adapter]
puts e.inspect
2012-04-10 14:06:46 +00:00
end
end
end