2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2012-04-10 14:06:46 +00:00
|
|
|
require 'net/imap'
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
class Channel::Driver::Imap < Channel::EmailParser
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-30 13:45:01 +00:00
|
|
|
FETCH_METADATA_TIMEOUT = 2.minutes
|
|
|
|
FETCH_MSG_TIMEOUT = 4.minutes
|
|
|
|
EXPUNGE_TIMEOUT = 16.minutes
|
|
|
|
|
2016-01-10 13:24:54 +00:00
|
|
|
def fetchable?(_channel)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
fetch emails from IMAP account
|
|
|
|
|
|
|
|
instance = Channel::Driver::Imap.new
|
|
|
|
result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'ok',
|
|
|
|
fetched: 123,
|
|
|
|
notice: 'e. g. message about to big emails in mailbox',
|
|
|
|
}
|
|
|
|
|
|
|
|
check if connect to IMAP account is possible, return count of mails in mailbox
|
|
|
|
|
|
|
|
instance = Channel::Driver::Imap.new
|
|
|
|
result = instance.fetch(params[:inbound][:options], channel, 'check')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'ok',
|
|
|
|
content_messages: 123,
|
|
|
|
}
|
|
|
|
|
|
|
|
verify IMAP account, check if search email is in there
|
|
|
|
|
|
|
|
instance = Channel::Driver::Imap.new
|
|
|
|
result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
result: 'ok', # 'verify not ok'
|
|
|
|
}
|
|
|
|
|
2016-11-06 15:39:05 +00:00
|
|
|
example
|
|
|
|
|
|
|
|
params = {
|
|
|
|
host: 'outlook.office365.com',
|
|
|
|
user: 'xxx@znuny.onmicrosoft.com',
|
|
|
|
password: 'xxx',
|
2017-07-20 12:01:14 +00:00
|
|
|
keep_on_server: true,
|
2016-11-06 15:39:05 +00:00
|
|
|
}
|
2020-05-28 13:28:07 +00:00
|
|
|
|
|
|
|
OR
|
|
|
|
|
|
|
|
params = {
|
|
|
|
host: 'imap.gmail.com',
|
|
|
|
user: 'xxx@gmail.com',
|
|
|
|
password: 'xxx',
|
|
|
|
keep_on_server: true,
|
|
|
|
auth_type: 'XOAUTH2'
|
|
|
|
}
|
|
|
|
|
2016-11-06 15:39:05 +00:00
|
|
|
channel = Channel.last
|
|
|
|
instance = Channel::Driver::Imap.new
|
|
|
|
result = instance.fetch(params, channel, 'verify')
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
=end
|
|
|
|
|
2017-10-02 13:10:07 +00:00
|
|
|
def fetch(options, channel, check_type = '', verify_string = '')
|
2017-07-20 12:01:14 +00:00
|
|
|
ssl = true
|
2018-04-20 11:46:09 +00:00
|
|
|
starttls = false
|
2017-07-20 12:01:14 +00:00
|
|
|
port = 993
|
|
|
|
keep_on_server = false
|
|
|
|
folder = 'INBOX'
|
|
|
|
if options[:keep_on_server] == true || options[:keep_on_server] == 'true'
|
|
|
|
keep_on_server = true
|
|
|
|
end
|
2016-12-02 11:24:00 +00:00
|
|
|
|
2018-04-25 07:42:10 +00:00
|
|
|
if options.key?(:ssl) && options[:ssl] == false
|
|
|
|
ssl = false
|
|
|
|
port = 143
|
|
|
|
end
|
2018-04-20 11:46:09 +00:00
|
|
|
|
|
|
|
port = if options.key?(:port) && options[:port].present?
|
2018-04-24 07:39:49 +00:00
|
|
|
options[:port].to_i
|
2018-04-20 11:46:09 +00:00
|
|
|
elsif ssl == true
|
|
|
|
993
|
|
|
|
else
|
|
|
|
143
|
|
|
|
end
|
|
|
|
|
|
|
|
if ssl == true && port != 993
|
|
|
|
ssl = false
|
|
|
|
starttls = true
|
2016-11-10 23:04:10 +00:00
|
|
|
end
|
2018-04-20 11:46:09 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
if options[:folder].present?
|
|
|
|
folder = options[:folder]
|
|
|
|
end
|
2013-01-03 19:01:26 +00:00
|
|
|
|
2020-05-28 13:28:07 +00:00
|
|
|
Rails.logger.info "fetching imap (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl},starttls=#{starttls},folder=#{folder},keep_on_server=#{keep_on_server},auth_type=#{options.fetch(:auth_type, 'LOGIN')})"
|
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
|
2018-11-12 07:47:56 +00:00
|
|
|
check_type_timeout = 45
|
2014-11-18 11:51:35 +00:00
|
|
|
if check_type == 'check'
|
2018-11-12 07:47:56 +00:00
|
|
|
check_type_timeout = 6
|
2013-01-03 19:01:26 +00:00
|
|
|
end
|
2014-11-18 11:51:35 +00:00
|
|
|
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(check_type_timeout) do
|
2018-04-26 08:55:53 +00:00
|
|
|
@imap = ::Net::IMAP.new(options[:host], port, ssl, nil, false)
|
2018-04-20 11:46:09 +00:00
|
|
|
if starttls
|
|
|
|
@imap.starttls()
|
|
|
|
end
|
2014-11-18 11:51:35 +00:00
|
|
|
end
|
|
|
|
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(check_type_timeout) do
|
2020-05-28 13:28:07 +00:00
|
|
|
if options[:auth_type].present?
|
|
|
|
@imap.authenticate(options[:auth_type], options[:user], options[:password])
|
|
|
|
else
|
|
|
|
@imap.login(options[:user], options[:password].dup&.force_encoding('ascii-8bit'))
|
|
|
|
end
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2014-11-18 11:51:35 +00:00
|
|
|
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(check_type_timeout) do
|
2018-11-06 08:43:14 +00:00
|
|
|
# select folder
|
|
|
|
@imap.select(folder)
|
|
|
|
end
|
2015-09-06 07:50:51 +00:00
|
|
|
|
2016-12-24 11:40:08 +00:00
|
|
|
# sort messages by date on server (if not supported), if not fetch messages via search (first in, first out)
|
2017-07-20 12:01:14 +00:00
|
|
|
filter = ['ALL']
|
|
|
|
if keep_on_server && check_type != 'check' && check_type != 'verify'
|
2017-11-23 08:09:44 +00:00
|
|
|
filter = %w[NOT SEEN]
|
2017-07-20 12:01:14 +00:00
|
|
|
end
|
2018-11-06 08:43:14 +00:00
|
|
|
|
|
|
|
message_ids = nil
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(6.minutes) do
|
2019-06-27 18:26:28 +00:00
|
|
|
|
|
|
|
message_ids = @imap.sort(['DATE'], filter, 'US-ASCII')
|
|
|
|
rescue
|
|
|
|
message_ids = @imap.search(filter)
|
|
|
|
|
2016-06-02 14:00:45 +00:00
|
|
|
end
|
2015-09-06 07:50:51 +00:00
|
|
|
|
|
|
|
# check mode only
|
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'
|
2016-03-10 13:41:42 +00:00
|
|
|
content_max_check = 2
|
2015-09-06 07:50:51 +00:00
|
|
|
content_messages = 0
|
|
|
|
|
|
|
|
# check messages
|
|
|
|
message_ids.each do |message_id|
|
|
|
|
|
2018-11-06 08:43:14 +00:00
|
|
|
message_meta = nil
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(1.minute) do
|
2020-08-01 18:19:42 +00:00
|
|
|
message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2015-09-06 07:50:51 +00:00
|
|
|
|
|
|
|
# check how many content messages we have, for notice used
|
2020-08-01 18:19:42 +00:00
|
|
|
headers = self.class.extract_rfc822_headers(message_meta)
|
2019-01-14 14:53:26 +00:00
|
|
|
next if messages_is_verify_message?(headers)
|
|
|
|
next if messages_is_ignore_message?(headers)
|
|
|
|
|
|
|
|
content_messages += 1
|
|
|
|
break if content_max_check < content_messages
|
2015-09-06 07:50:51 +00:00
|
|
|
end
|
2015-09-06 11:39:16 +00:00
|
|
|
if content_messages >= content_max_check
|
2015-09-06 07:50:51 +00:00
|
|
|
content_messages = message_ids.count
|
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
disconnect
|
2015-09-06 07:50:51 +00:00
|
|
|
return {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'ok',
|
2015-09-06 07:50:51 +00:00
|
|
|
content_messages: content_messages,
|
|
|
|
}
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|
2014-11-09 20:10:23 +00:00
|
|
|
|
|
|
|
# reverse message order to increase performance
|
|
|
|
if check_type == 'verify'
|
2015-09-06 07:50:51 +00:00
|
|
|
Rails.logger.info "verify mode, fetch no emails #{verify_string}"
|
2014-11-09 20:10:23 +00:00
|
|
|
message_ids.reverse!
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2014-10-22 21:00:11 +00:00
|
|
|
# check for verify message
|
2015-09-06 07:50:51 +00:00
|
|
|
message_ids.each do |message_id|
|
|
|
|
|
2018-11-06 08:43:14 +00:00
|
|
|
message_meta = nil
|
2019-04-30 13:45:01 +00:00
|
|
|
timeout(FETCH_METADATA_TIMEOUT) do
|
2020-08-01 18:19:42 +00:00
|
|
|
message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2015-09-06 07:50:51 +00:00
|
|
|
|
|
|
|
# check if verify message exists
|
2020-08-01 18:19:42 +00:00
|
|
|
headers = self.class.extract_rfc822_headers(message_meta)
|
|
|
|
subject = headers['Subject']
|
2015-09-06 11:39:16 +00:00
|
|
|
next if !subject
|
2019-09-16 09:21:10 +00:00
|
|
|
next if !subject.match?(/#{verify_string}/)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-09-06 11:39:16 +00:00
|
|
|
Rails.logger.info " - verify email #{verify_string} found"
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(600) do
|
2018-11-06 08:43:14 +00:00
|
|
|
@imap.store(message_id, '+FLAGS', [:Deleted])
|
|
|
|
@imap.expunge()
|
|
|
|
end
|
2015-09-06 11:39:16 +00:00
|
|
|
disconnect
|
|
|
|
return {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
2015-09-06 07:50:51 +00:00
|
|
|
end
|
2014-10-22 21:00:11 +00:00
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
disconnect
|
|
|
|
return {
|
|
|
|
result: 'verify not ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# fetch regular messages
|
2019-04-30 07:27:32 +00:00
|
|
|
count_all = message_ids.count
|
|
|
|
count = 0
|
|
|
|
count_fetched = 0
|
|
|
|
count_max = 5000
|
2019-08-06 15:26:29 +00:00
|
|
|
too_large_messages = []
|
2019-04-30 07:27:32 +00:00
|
|
|
active_check_interval = 20
|
2019-11-06 08:15:22 +00:00
|
|
|
result = 'ok'
|
2019-04-30 07:27:32 +00:00
|
|
|
notice = ''
|
2015-09-06 07:50:51 +00:00
|
|
|
message_ids.each do |message_id|
|
|
|
|
count += 1
|
2019-04-30 07:27:32 +00:00
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
break if (count % active_check_interval).zero? && channel_has_changed?(channel)
|
2019-04-30 07:27:32 +00:00
|
|
|
break if max_process_count_has_reached?(channel, count, count_max)
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
Rails.logger.info " - message #{count}/#{count_all}"
|
|
|
|
|
2018-11-06 08:43:14 +00:00
|
|
|
message_meta = nil
|
2019-04-30 13:45:01 +00:00
|
|
|
timeout(FETCH_METADATA_TIMEOUT) do
|
2020-08-01 18:19:42 +00:00
|
|
|
message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'FLAGS', 'INTERNALDATE', 'RFC822.HEADER'])[0]
|
2019-11-06 08:15:22 +00:00
|
|
|
rescue Net::IMAP::ResponseParseError => e
|
2020-09-30 09:07:01 +00:00
|
|
|
raise if e.message.exclude?('unknown token')
|
2019-11-06 08:15:22 +00:00
|
|
|
|
|
|
|
result = 'error'
|
|
|
|
notice += <<~NOTICE
|
|
|
|
One of your incoming emails could not be imported (#{e.message}).
|
|
|
|
Please remove it from your inbox directly
|
|
|
|
to prevent Zammad from trying to import it again.
|
|
|
|
NOTICE
|
|
|
|
Rails.logger.error "Net::IMAP failed to parse message #{message_id}: #{e.message} (#{e.class})"
|
|
|
|
Rails.logger.error '(See https://github.com/zammad/zammad/issues/2754 for more details)'
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2015-09-06 07:50:51 +00:00
|
|
|
|
2019-12-17 08:47:15 +00:00
|
|
|
next if message_meta.nil?
|
|
|
|
|
2019-01-14 14:53:26 +00:00
|
|
|
# ignore verify messages
|
|
|
|
next if !messages_is_too_old_verify?(message_meta, count, count_all)
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
# ignore deleted messages
|
2017-03-06 11:18:04 +00:00
|
|
|
next if deleted?(message_meta, count, count_all)
|
2015-09-06 07:50:51 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
# ignore already imported
|
2017-10-24 07:00:58 +00:00
|
|
|
next if already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
# delete email from server after article was created
|
2018-11-06 08:43:14 +00:00
|
|
|
msg = nil
|
2019-04-30 13:45:01 +00:00
|
|
|
begin
|
|
|
|
timeout(FETCH_MSG_TIMEOUT) do
|
|
|
|
msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
|
|
|
|
end
|
|
|
|
rescue Timeout::Error => e
|
|
|
|
Rails.logger.error "Unable to fetch email from #{count}/#{count_all} from server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
|
|
|
|
raise e
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2015-09-06 11:39:16 +00:00
|
|
|
next if !msg
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-08-06 15:26:29 +00:00
|
|
|
# do not process too big messages, instead download & send postmaster reply
|
|
|
|
too_large_info = too_large?(message_meta)
|
|
|
|
if too_large_info
|
|
|
|
if Setting.get('postmaster_send_reject_if_mail_too_large') == true
|
|
|
|
info = " - download message #{count}/#{count_all} - ignore message because it's too large (is:#{too_large_info[0]} MB/max:#{too_large_info[1]} MB)"
|
|
|
|
Rails.logger.info info
|
|
|
|
notice += "#{info}\n"
|
|
|
|
process_oversized_mail(channel, msg)
|
|
|
|
else
|
|
|
|
info = " - ignore message #{count}/#{count_all} - because message is too large (is:#{too_large_info[0]} MB/max:#{too_large_info[1]} MB)"
|
|
|
|
Rails.logger.info info
|
|
|
|
notice += "#{info}\n"
|
|
|
|
too_large_messages.push info
|
|
|
|
next
|
|
|
|
end
|
|
|
|
else
|
|
|
|
process(channel, msg, false)
|
|
|
|
end
|
2018-11-06 08:43:14 +00:00
|
|
|
|
2019-04-30 13:45:01 +00:00
|
|
|
begin
|
|
|
|
timeout(FETCH_MSG_TIMEOUT) do
|
|
|
|
if !keep_on_server
|
|
|
|
@imap.store(message_id, '+FLAGS', [:Deleted])
|
|
|
|
else
|
|
|
|
@imap.store(message_id, '+FLAGS', [:Seen])
|
|
|
|
end
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2019-04-30 13:45:01 +00:00
|
|
|
rescue Timeout::Error => e
|
|
|
|
Rails.logger.error "Unable to set +FLAGS for email #{count}/#{count_all} on server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
|
|
|
|
raise e
|
2017-07-20 12:01:14 +00:00
|
|
|
end
|
2016-11-07 22:20:58 +00:00
|
|
|
count_fetched += 1
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2018-11-06 08:43:14 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
if !keep_on_server
|
2019-04-30 13:45:01 +00:00
|
|
|
begin
|
|
|
|
timeout(EXPUNGE_TIMEOUT) do
|
|
|
|
@imap.expunge()
|
|
|
|
end
|
|
|
|
rescue Timeout::Error => e
|
|
|
|
Rails.logger.error "Unable to expunge server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
|
|
|
|
raise e
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
2017-07-20 12:01:14 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
disconnect
|
2016-01-16 10:05:04 +00:00
|
|
|
if count.zero?
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info ' - no message'
|
2012-04-13 17:06:09 +00:00
|
|
|
end
|
2019-08-06 15:26:29 +00:00
|
|
|
|
|
|
|
if too_large_messages.present?
|
|
|
|
raise too_large_messages.join("\n")
|
|
|
|
end
|
|
|
|
|
2015-05-04 19:34:04 +00:00
|
|
|
Rails.logger.info 'done'
|
2015-09-06 07:50:51 +00:00
|
|
|
{
|
2019-11-06 08:15:22 +00:00
|
|
|
result: result,
|
2015-09-06 07:50:51 +00:00
|
|
|
fetched: count_fetched,
|
2018-12-19 17:31:51 +00:00
|
|
|
notice: notice,
|
2015-09-06 07:50:51 +00:00
|
|
|
}
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(1.minute) do
|
2018-11-06 08:43:14 +00:00
|
|
|
@imap.disconnect()
|
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
end
|
2017-03-06 11:18:04 +00:00
|
|
|
|
2017-10-02 13:10:07 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Channel::Driver::Imap.streamable?
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.streamable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-08-01 18:19:42 +00:00
|
|
|
# Parses RFC822 header
|
|
|
|
# @param [String] RFC822 header text blob
|
|
|
|
# @return [Hash<String=>String>]
|
|
|
|
def self.parse_rfc822_headers(string)
|
|
|
|
array = string
|
2020-10-27 14:05:10 +00:00
|
|
|
.gsub("\r\n\t", ' ') # Some servers (e.g. microsoft365) may put attribute value on a separate line and tab it
|
2020-08-01 18:19:42 +00:00
|
|
|
.lines(chomp: true)
|
|
|
|
.map { |line| line.split(/:\s*/, 2).map(&:strip) }
|
|
|
|
|
|
|
|
array.each { |elem| elem.append(nil) if elem.one? }
|
|
|
|
|
|
|
|
Hash[*array.flatten]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parses RFC822 header
|
|
|
|
# @param [Net::IMAP::FetchData] fetched message
|
|
|
|
# @return [Hash<String=>String>]
|
|
|
|
def self.extract_rfc822_headers(message_meta)
|
|
|
|
blob = message_meta&.attr&.dig 'RFC822.HEADER'
|
|
|
|
|
2020-08-18 13:01:18 +00:00
|
|
|
return if !blob
|
2020-08-01 18:19:42 +00:00
|
|
|
|
|
|
|
parse_rfc822_headers blob
|
|
|
|
end
|
|
|
|
|
2017-03-06 11:18:04 +00:00
|
|
|
private
|
|
|
|
|
2019-01-14 14:53:26 +00:00
|
|
|
def messages_is_too_old_verify?(message_meta, count, count_all)
|
2020-08-01 18:19:42 +00:00
|
|
|
headers = self.class.extract_rfc822_headers(message_meta)
|
2019-01-14 14:53:26 +00:00
|
|
|
return true if !messages_is_verify_message?(headers)
|
|
|
|
return true if headers['X-Zammad-Verify-Time'].blank?
|
|
|
|
|
|
|
|
begin
|
|
|
|
verify_time = Time.zone.parse(headers['X-Zammad-Verify-Time'])
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return true if verify_time < Time.zone.now - 30.minutes
|
|
|
|
|
|
|
|
Rails.logger.info " - ignore message #{count}/#{count_all} - because message has a verify message"
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def messages_is_verify_message?(headers)
|
|
|
|
return true if headers['X-Zammad-Verify'] == 'true'
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def messages_is_ignore_message?(headers)
|
|
|
|
return true if headers['X-Zammad-Ignore'] == 'true'
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-04-30 07:27:32 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if email is already impoted
|
|
|
|
|
|
|
|
Channel::Driver::IMAP.already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-10-24 07:00:58 +00:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
|
|
|
def already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
|
|
|
|
# rubocop:enable Metrics/ParameterLists
|
2017-07-20 12:01:14 +00:00
|
|
|
return false if !keep_on_server
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-08-01 18:19:42 +00:00
|
|
|
headers = self.class.extract_rfc822_headers(message_meta)
|
|
|
|
retrurn false if !headers
|
|
|
|
|
|
|
|
local_message_id = headers['Message-ID']
|
2017-07-20 12:01:14 +00:00
|
|
|
return false if local_message_id.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
local_message_id_md5 = Digest::MD5.hexdigest(local_message_id)
|
|
|
|
article = Ticket::Article.where(message_id_md5: local_message_id_md5).order('created_at DESC, id DESC').limit(1).first
|
|
|
|
return false if !article
|
2017-10-24 07:00:58 +00:00
|
|
|
|
|
|
|
# verify if message is already imported via same channel, if not, import it again
|
|
|
|
ticket = article.ticket
|
2020-09-30 09:07:01 +00:00
|
|
|
return false if ticket&.preferences && ticket.preferences[:channel_id].present? && channel.present? && ticket.preferences[:channel_id] != channel[:id]
|
2017-10-24 07:00:58 +00:00
|
|
|
|
2018-11-12 07:47:56 +00:00
|
|
|
timeout(1.minute) do
|
2018-11-06 08:43:14 +00:00
|
|
|
@imap.store(message_id, '+FLAGS', [:Seen])
|
|
|
|
end
|
2017-07-20 12:01:14 +00:00
|
|
|
Rails.logger.info " - ignore message #{count}/#{count_all} - because message message id already imported"
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-04-30 07:27:32 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if email is already marked as deleted
|
|
|
|
|
|
|
|
Channel::Driver::IMAP.deleted?(message_meta, count, count_all)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-03-06 11:18:04 +00:00
|
|
|
def deleted?(message_meta, count, count_all)
|
2020-09-30 09:07:01 +00:00
|
|
|
return false if message_meta.attr['FLAGS'].exclude?(:Deleted)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-06 11:18:04 +00:00
|
|
|
Rails.logger.info " - ignore message #{count}/#{count_all} - because message has already delete flag"
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-04-30 07:27:32 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if email is to big
|
|
|
|
|
2019-08-06 15:26:29 +00:00
|
|
|
Channel::Driver::IMAP.too_large?(message_meta, count, count_all)
|
2019-04-30 07:27:32 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-08-06 15:26:29 +00:00
|
|
|
def too_large?(message_meta)
|
2017-03-06 11:18:04 +00:00
|
|
|
max_message_size = Setting.get('postmaster_max_size').to_f
|
|
|
|
real_message_size = message_meta.attr['RFC822.SIZE'].to_f / 1024 / 1024
|
|
|
|
if real_message_size > max_message_size
|
2019-08-06 15:26:29 +00:00
|
|
|
return [real_message_size, max_message_size]
|
2017-03-06 11:18:04 +00:00
|
|
|
end
|
2019-08-06 15:26:29 +00:00
|
|
|
|
2017-03-06 11:18:04 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-04-30 07:27:32 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
check if channel config has changed
|
|
|
|
|
|
|
|
Channel::Driver::IMAP.channel_has_changed?(channel)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def channel_has_changed?(channel)
|
|
|
|
current_channel = Channel.find_by(id: channel.id)
|
|
|
|
if !current_channel
|
|
|
|
Rails.logger.info "Channel with id #{channel.id} is deleted in the meantime. Stop fetching."
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false if channel.updated_at == current_channel.updated_at
|
|
|
|
|
|
|
|
Rails.logger.info "Channel with id #{channel.id} has changed. Stop fetching."
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if maximal fetching email count has reached
|
|
|
|
|
|
|
|
Channel::Driver::IMAP.max_process_count_has_reached?(channel, count, count_max)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def max_process_count_has_reached?(channel, count, count_max)
|
|
|
|
return false if count < count_max
|
|
|
|
|
|
|
|
Rails.logger.info "Maximal fetched emails (#{count_max}) reached for this interval for Channel with id #{channel.id}."
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
def timeout(seconds, &block)
|
|
|
|
Timeout.timeout(seconds, &block)
|
2018-11-06 08:43:14 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|