trabajo-afectivo/app/models/channel/imap.rb

79 lines
2.2 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2012-04-10 14:06:46 +00:00
require 'net/imap'
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 = '')
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
2014-06-22 07:00:09 +00:00
@imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false )
2013-01-03 19:01:26 +00:00
# try LOGIN, if not - try plain
begin
2014-06-22 07:00:09 +00:00
@imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] )
2013-01-03 19:01:26 +00:00
rescue Exception => e
if e.to_s !~ /unsupported\sauthentication\smechanism/i
raise e
end
2014-06-22 07:00:09 +00:00
@imap.login( channel[:options][:user], channel[:options][:password] )
2013-01-03 19:01:26 +00:00
end
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'
puts "check only mode, fetch no emails"
disconnect
return
elsif check_type == 'verify'
puts "verify mode, fetch no emails #{verify_string}"
end
2012-04-13 17:06:09 +00:00
count = 0
2014-06-22 07:00:09 +00:00
count_all = @imap.search(['ALL']).count
@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}"
# puts 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'
subject = @imap.fetch(message_id,'ENVELOPE')[0].attr['ENVELOPE'].subject
if subject && subject =~ /#{verify_string}/
puts " - verify email #{verify_string} found"
@imap.store(message_id, "+FLAGS", [:Deleted])
@imap.expunge()
disconnect
return 'verify ok'
end
else
# delete email from server after article was created
msg = @imap.fetch(message_id,'RFC822')[0].attr['RFC822']
if process(channel, msg)
@imap.store(message_id, "+FLAGS", [:Deleted])
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
puts " - no message"
end
puts "done"
2012-04-10 14:06:46 +00:00
end
2014-06-22 07:00:09 +00:00
def disconnect
if @imap
@imap.disconnect()
end
end
2014-10-22 21:00:11 +00:00
end