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-13 16:42:25 +00:00
|
|
|
require 'net/pop'
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
class Channel::POP3 < Channel::EmailParser
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2014-11-09 20:17:14 +00:00
|
|
|
def fetch (channel, check_type = '', verify_string = '')
|
2014-11-18 21:09:01 +00:00
|
|
|
ssl = true
|
|
|
|
port = 995
|
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 = 110
|
2013-01-04 23:14:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "fetching pop3 (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})"
|
2012-04-13 16:42:25 +00:00
|
|
|
|
2014-06-22 07:00:09 +00:00
|
|
|
@pop = Net::POP3.new( channel[:options][:host], port )
|
2014-11-16 22:45:57 +00:00
|
|
|
|
|
|
|
# on check, reduce open_timeout to have faster probing
|
|
|
|
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
|
2014-06-22 07:00:09 +00:00
|
|
|
@pop.start( channel[:options][:user], channel[:options][:password] )
|
2014-10-22 21:00:11 +00:00
|
|
|
if check_type == 'check'
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'check only mode, fetch no emails'
|
2014-10-22 21:00:11 +00:00
|
|
|
disconnect
|
|
|
|
return
|
|
|
|
elsif check_type == 'verify'
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'verify mode, fetch no emails'
|
2014-10-22 21:00:11 +00:00
|
|
|
end
|
2014-11-09 20:18:13 +00:00
|
|
|
|
|
|
|
mails = @pop.mails
|
2012-04-13 17:06:09 +00:00
|
|
|
count = 0
|
2014-11-09 20:18:13 +00:00
|
|
|
count_all = mails.size
|
|
|
|
|
|
|
|
# reverse message order to increase performance
|
|
|
|
if check_type == 'verify'
|
|
|
|
mails.reverse!
|
|
|
|
end
|
|
|
|
|
|
|
|
mails.each do |m|
|
2012-04-13 17:06:09 +00:00
|
|
|
count += 1
|
|
|
|
puts " - message #{count.to_s}/#{count_all.to_s}"
|
|
|
|
|
2014-10-22 21:00:11 +00:00
|
|
|
# check for verify message
|
|
|
|
if check_type == 'verify'
|
|
|
|
mail = m.pop
|
|
|
|
if mail && mail =~ /#{verify_string}/
|
|
|
|
puts " - verify email #{verify_string} found"
|
|
|
|
m.delete
|
|
|
|
disconnect
|
|
|
|
return 'verify ok'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
|
|
|
|
# delete email from server after article was created
|
|
|
|
if process(channel, m.pop)
|
|
|
|
m.delete
|
|
|
|
end
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
disconnect
|
2012-04-13 17:06:09 +00:00
|
|
|
if count == 0
|
2015-04-27 13:20:16 +00:00
|
|
|
puts ' - no message'
|
2012-04-13 17:06:09 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'done'
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2014-06-22 07:00:09 +00:00
|
|
|
|
|
|
|
def disconnect
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
return if !@pop
|
|
|
|
|
|
|
|
@pop.finish
|
2014-06-22 07:00:09 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|