imap.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2014 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. disconnect
  41. if count == 0
  42. puts " - no message"
  43. end
  44. puts "done"
  45. end
  46. def disconnect
  47. if @imap
  48. @imap.disconnect()
  49. end
  50. end
  51. def send(attr, notification = false)
  52. channel = Channel.where( :area => 'Email::Outbound', :active => true ).first
  53. begin
  54. c = eval 'Channel::' + channel[:adapter] + '.new'
  55. c.send(attr, channel, notification)
  56. rescue Exception => e
  57. puts "can't use " + 'Channel::' + channel[:adapter]
  58. puts e.inspect
  59. end
  60. end
  61. end