trabajo-afectivo/app/models/channel/driver/pop3.rb

171 lines
3.9 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
require 'net/pop'
2012-04-10 14:06:46 +00:00
2015-08-28 00:53:14 +00:00
class Channel::Driver::Pop3 < Channel::EmailParser
2012-04-10 14:06:46 +00:00
=begin
fetch emails from Pop3 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 Pop3 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 Pop3 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'
}
=end
2015-08-28 00:53:14 +00:00
def fetch (options, channel, check_type = '', verify_string = '')
2014-11-18 21:09:01 +00:00
ssl = true
port = 995
2015-08-28 00:53:14 +00:00
if options.key?(:ssl) && options[:ssl].to_s == 'false'
2014-11-18 21:09:01 +00:00
ssl = false
port = 110
2013-01-04 23:14:08 +00:00
end
2015-08-28 00:53:14 +00:00
Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
2015-08-28 00:53:14 +00:00
@pop = Net::POP3.new( options[:host], port )
#@pop.set_debug_output $stderr
2014-11-16 22:45:57 +00:00
# on check, reduce open_timeout to have faster probing
@pop.open_timeout = 8
@pop.read_timeout = 12
2014-11-16 22:45:57 +00:00
if check_type == 'check'
2014-11-18 11:51:35 +00:00
@pop.open_timeout = 4
2014-11-16 22:45:57 +00:00
@pop.read_timeout = 6
end
2013-01-04 23:14:08 +00:00
if ssl
2014-11-16 22:45:57 +00:00
@pop.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
2013-01-04 23:14:08 +00:00
end
@pop.start(options[:user], options[:password])
mails = @pop.mails
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'
content_max_check = 2
content_messages = 0
# check messages
mails.each do |m|
mail = m.pop
next if !mail
# check how many content messages we have, for notice used
if mail !~ /x-zammad-ignore/i
content_messages += 1
break if content_max_check < content_messages
end
end
if content_messages >= content_max_check
content_messages = mails.count
end
2014-10-22 21:00:11 +00:00
disconnect
return {
result: 'ok',
content_messages: content_messages,
}
2014-10-22 21:00:11 +00:00
end
2014-11-09 20:18:13 +00:00
# reverse message order to increase performance
if check_type == 'verify'
Rails.logger.info 'verify mode, fetch no emails'
2014-11-09 20:18:13 +00:00
mails.reverse!
2012-04-13 17:06:09 +00:00
2014-10-22 21:00:11 +00:00
# check for verify message
mails.each do |m|
2014-10-22 21:00:11 +00:00
mail = m.pop
next if !mail
# check if verify message exists
next if mail !~ /#{verify_string}/
Rails.logger.info " - verify email #{verify_string} found"
m.delete
disconnect
return {
result: 'ok',
}
end
2014-10-22 21:00:11 +00:00
return {
result: 'verify not ok',
}
end
# fetch regular messages
count_all = mails.size
count = 0
count_fetched = 0
notice = ''
mails.each do |m|
count += 1
Rails.logger.info " - message #{count}/#{count_all}"
mail = m.pop
next if !mail
# ignore to big messages
max_message_size = Setting.get('postmaster_max_size').to_f
real_message_size = mail.size.to_f / 1024 / 1024
if real_message_size > max_message_size
info = " - ignore message #{count}/#{count_all} - because message is to big (is:#{real_message_size} MB/max:#{max_message_size} MB)"
Rails.logger.info info
notice += "#{info}\n"
next
end
# delete email from server after article was created
if process(channel, m.pop)
m.delete
count_fetched += 1
end
end
2014-06-22 07:00:09 +00:00
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'
{
result: 'ok',
fetched: count_fetched,
notice: notice,
}
end
2014-06-22 07:00:09 +00:00
def disconnect
return if !@pop
@pop.finish
2014-06-22 07:00:09 +00:00
end
end