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
|
|
|
|
2014-10-22 21:00:11 +00:00
|
|
|
def fetch (channel, check_type = '', verify_string = '')
|
2014-11-18 21:09:01 +00:00
|
|
|
ssl = true
|
|
|
|
port = 993
|
2015-04-30 17:34:33 +00:00
|
|
|
if channel[:options].key?(:ssl) && channel[:options][:ssl].to_s == 'false'
|
2014-11-18 21:09:01 +00:00
|
|
|
ssl = false
|
|
|
|
port = 143
|
2013-01-03 19:01:26 +00:00
|
|
|
end
|
|
|
|
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info "fetching imap (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})"
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2014-11-18 11:51:35 +00:00
|
|
|
# on check, reduce open_timeout to have faster probing
|
|
|
|
timeout = 12
|
|
|
|
if check_type == 'check'
|
2014-12-11 10:44:23 +00:00
|
|
|
timeout = 6
|
2013-01-03 19:01:26 +00:00
|
|
|
end
|
2014-11-18 11:51:35 +00:00
|
|
|
|
|
|
|
Timeout.timeout(timeout) do
|
|
|
|
|
|
|
|
@imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false )
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-11-18 21:09:01 +00:00
|
|
|
# try LOGIN, if not - try plain
|
|
|
|
begin
|
|
|
|
@imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] )
|
|
|
|
rescue Exception => e
|
2014-12-11 10:44:23 +00:00
|
|
|
if e.to_s !~ /(unsupported\s(authenticate|authentication)\smechanism|not\ssupported)/i
|
2014-11-18 21:09:01 +00:00
|
|
|
raise e
|
2014-11-18 11:51:35 +00:00
|
|
|
end
|
2014-11-18 21:09:01 +00:00
|
|
|
@imap.login( channel[:options][:user], channel[:options][:password] )
|
|
|
|
end
|
2014-11-18 11:51:35 +00:00
|
|
|
|
2013-01-08 21:25:21 +00:00
|
|
|
if !channel[:options][:folder] || channel[:options][:folder].empty?
|
2014-06-22 07:00:09 +00:00
|
|
|
@imap.select('INBOX')
|
2013-01-04 23:14:08 +00:00
|
|
|
else
|
2014-06-22 07:00:09 +00:00
|
|
|
@imap.select( channel[:options][:folder] )
|
2013-01-04 23:14:08 +00:00
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
if check_type == 'check'
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info 'check only mode, fetch no emails'
|
2014-10-22 21:00:11 +00:00
|
|
|
disconnect
|
|
|
|
return
|
|
|
|
elsif check_type == 'verify'
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info "verify mode, fetch no emails #{verify_string}"
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|
2014-11-09 20:10:23 +00:00
|
|
|
|
|
|
|
message_ids = @imap.search(['ALL'])
|
|
|
|
count_all = message_ids.count
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
# reverse message order to increase performance
|
|
|
|
if check_type == 'verify'
|
|
|
|
message_ids.reverse!
|
|
|
|
end
|
|
|
|
|
|
|
|
message_ids.each do |message_id|
|
2012-04-13 17:06:09 +00:00
|
|
|
count += 1
|
2015-05-06 09:30:39 +00:00
|
|
|
Rails.logger.info " - message #{count}/#{count_all}"
|
2015-05-04 19:34:04 +00:00
|
|
|
#Rails.logger.info msg.to_s
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2014-10-22 21:00:11 +00:00
|
|
|
# check for verify message
|
|
|
|
if check_type == 'verify'
|
2015-04-27 14:53:29 +00:00
|
|
|
subject = @imap.fetch(message_id, 'ENVELOPE')[0].attr['ENVELOPE'].subject
|
2014-10-22 21:00:11 +00:00
|
|
|
if subject && subject =~ /#{verify_string}/
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info " - verify email #{verify_string} found"
|
2015-04-27 13:20:16 +00:00
|
|
|
@imap.store(message_id, '+FLAGS', [:Deleted])
|
2014-10-22 21:00:11 +00:00
|
|
|
@imap.expunge()
|
|
|
|
disconnect
|
|
|
|
return 'verify ok'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
|
|
|
|
# delete email from server after article was created
|
2015-04-27 14:53:29 +00:00
|
|
|
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
|
2014-10-22 21:00:11 +00:00
|
|
|
if process(channel, msg)
|
2015-04-27 13:20:16 +00:00
|
|
|
@imap.store(message_id, '+FLAGS', [:Deleted])
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
@imap.expunge()
|
|
|
|
disconnect
|
2012-04-13 17:06:09 +00:00
|
|
|
if count == 0
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info ' - no message'
|
2012-04-13 17:06:09 +00:00
|
|
|
end
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info 'done'
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
|
|
|
def disconnect
|
2015-04-30 15:25:04 +00:00
|
|
|
return if !@imap
|
|
|
|
@imap.disconnect()
|
2014-06-22 07:00:09 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|