imap.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. require 'net/imap'
  3. class Channel::IMAP < Channel::EmailParser
  4. def fetch (channel)
  5. ssl = false
  6. port = 143
  7. if channel[:options][:ssl].to_s == 'true'
  8. ssl = true
  9. port = 993
  10. end
  11. puts "fetching imap (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})"
  12. imap = Net::IMAP.new( channel[:options][:host], port, ssl, nil, false )
  13. # try LOGIN, if not - try plain
  14. begin
  15. imap.authenticate( 'LOGIN', channel[:options][:user], channel[:options][:password] )
  16. rescue Exception => e
  17. if e.to_s !~ /unsupported\sauthentication\smechanism/i
  18. raise e
  19. end
  20. imap.login( channel[:options][:user], channel[:options][:password] )
  21. end
  22. if !channel[:options][:folder] || channel[:options][:folder].empty?
  23. imap.select('INBOX')
  24. else
  25. imap.select( channel[:options][:folder] )
  26. end
  27. count = 0
  28. count_all = imap.search(['ALL']).count
  29. imap.search(['ALL']).each do |message_id|
  30. count += 1
  31. puts " - message #{count.to_s}/#{count_all.to_s}"
  32. msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
  33. # puts msg.to_s
  34. # delete email from server after article was created
  35. if process(channel, msg)
  36. imap.store(message_id, "+FLAGS", [:Deleted])
  37. end
  38. end
  39. imap.expunge()
  40. imap.disconnect()
  41. if count == 0
  42. puts " - no message"
  43. end
  44. puts "done"
  45. end
  46. def send(attr, notification = false)
  47. channel = Channel.where( :area => 'Email::Outbound', :active => true ).first
  48. begin
  49. c = eval 'Channel::' + channel[:adapter] + '.new'
  50. c.send(attr, channel, notification)
  51. rescue Exception => e
  52. puts "can't use " + 'Channel::' + channel[:adapter]
  53. puts e.inspect
  54. end
  55. end
  56. end