2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
require 'net/imap'
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
class Channel::IMAP < Channel::EmailParser
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
def fetch (channel)
|
2013-01-03 19:01:26 +00:00
|
|
|
ssl = false
|
|
|
|
port = 143
|
|
|
|
if channel[:options][:ssl].to_s == 'true'
|
|
|
|
ssl = true
|
|
|
|
port = 993
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "fetching imap (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})"
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-01-03 19:01:26 +00:00
|
|
|
imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false )
|
|
|
|
|
|
|
|
# try LOGIN, if not - try plain
|
|
|
|
begin
|
|
|
|
imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] )
|
|
|
|
rescue Exception => e
|
|
|
|
if e.to_s !~ /unsupported\sauthentication\smechanism/i
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
imap.login( channel[:options][:user], channel[:options][:password] )
|
|
|
|
end
|
2013-01-08 21:25:21 +00:00
|
|
|
if !channel[:options][:folder] || channel[:options][:folder].empty?
|
2013-01-04 23:14:08 +00:00
|
|
|
imap.select('INBOX')
|
|
|
|
else
|
|
|
|
imap.select( channel[:options][:folder] )
|
|
|
|
end
|
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']
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts msg.to_s
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# delete email from server after article was created
|
2012-05-04 11:33:05 +00:00
|
|
|
if process(channel, msg)
|
2012-04-13 16:42:25 +00:00
|
|
|
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
|
2012-04-13 13:51:10 +00:00
|
|
|
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
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|