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

37 lines
1 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-10 14:06:46 +00:00
puts 'fetching imap'
imap = Net::IMAP.new(channel[:options][:host], 993, true )
imap.authenticate('LOGIN', channel[:options][:user], channel[:options][:password])
2012-04-10 14:06:46 +00:00
imap.select('INBOX')
count = 0
2012-04-10 14:06:46 +00:00
imap.search(['ALL']).each do |message_id|
msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
# puts msg.to_s
# delete email from server after article was created
if parse(channel, msg)
imap.store(message_id, "+FLAGS", [:Deleted])
2012-04-10 14:06:46 +00:00
end
count += 1
2012-04-10 14:06:46 +00:00
end
imap.expunge()
imap.disconnect()
puts "#{count.to_s} mails fetched. 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