channel.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Channel < ApplicationModel
  3. store :options
  4. store :preferences
  5. after_create :email_address_check
  6. after_update :email_address_check
  7. after_destroy :email_address_check
  8. =begin
  9. fetch all accounts
  10. Channel.fetch
  11. =end
  12. def self.fetch
  13. channels = Channel.where('active = ? AND area LIKE ?', true, '%::Account')
  14. channels.each(&:fetch)
  15. end
  16. =begin
  17. fetch one account
  18. channel = Channel.where(area: 'Email::Account').first
  19. channel.fetch
  20. =end
  21. def fetch
  22. adapter = options[:adapter]
  23. adapter_options = options
  24. if options[:options]
  25. adapter_options = options[:options]
  26. elsif options[:inbound] && options[:inbound][:adapter]
  27. adapter = options[:inbound][:adapter]
  28. adapter_options = options[:inbound][:options]
  29. end
  30. begin
  31. # we need to require each channel backend individually otherwise we get a
  32. # 'warning: toplevel constant Twitter referenced by Channel::Driver::Twitter' error e.g.
  33. # so we have to convert the channel name to the filename via Rails String.underscore
  34. # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
  35. require "channel/driver/#{adapter.to_filename}"
  36. driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
  37. driver_instance = driver_class.new
  38. driver_instance.fetch(adapter_options, self)
  39. self.status_in = 'ok'
  40. self.last_log_in = ''
  41. save
  42. rescue => e
  43. error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
  44. logger.error error
  45. logger.error e.backtrace
  46. self.status_in = 'error'
  47. self.last_log_in = error
  48. save
  49. end
  50. end
  51. =begin
  52. send via account
  53. channel = Channel.where(area: 'Email::Account').first
  54. channel.deliver(mail_params, notification)
  55. =end
  56. def deliver(mail_params, notification = false)
  57. adapter = options[:adapter]
  58. adapter_options = options
  59. if options[:options]
  60. adapter_options = options[:options]
  61. elsif options[:outbound] && options[:outbound][:adapter]
  62. adapter = options[:outbound][:adapter]
  63. adapter_options = options[:outbound][:options]
  64. end
  65. result = nil
  66. begin
  67. # we need to require each channel backend individually otherwise we get a
  68. # 'warning: toplevel constant Twitter referenced by Channel::Driver::Twitter' error e.g.
  69. # so we have to convert the channel name to the filename via Rails String.underscore
  70. # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
  71. require "channel/driver/#{adapter.to_filename}"
  72. driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
  73. driver_instance = driver_class.new
  74. result = driver_instance.send(adapter_options, mail_params, notification)
  75. self.status_out = 'ok'
  76. self.last_log_out = ''
  77. save
  78. rescue => e
  79. error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
  80. logger.error error
  81. logger.error e.backtrace
  82. self.status_out = 'error'
  83. self.last_log_out = error
  84. save
  85. end
  86. result
  87. end
  88. private
  89. def email_address_check
  90. # reset non existing channel_ids
  91. EmailAddress.channel_cleanup
  92. end
  93. end