handler_incoming.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SecureMailing::Backend::HandlerIncoming < SecureMailing::Backend::Handler
  3. attr_accessor :mail, :content_type
  4. def initialize(mail)
  5. super()
  6. @mail = mail
  7. @content_type = mail[:mail_instance].content_type
  8. end
  9. def process
  10. return if !process?
  11. initialize_article_preferences
  12. decrypt
  13. verify_signature
  14. log
  15. end
  16. def process?
  17. signed? || encrypted?
  18. end
  19. def initialize_article_preferences
  20. article_preferences[:security] = {
  21. type: type,
  22. sign: {
  23. success: false,
  24. comment: nil,
  25. },
  26. encryption: {
  27. success: false,
  28. comment: nil,
  29. }
  30. }
  31. end
  32. def article_preferences
  33. @article_preferences ||= begin
  34. key = :'x-zammad-article-preferences'
  35. mail[ key ] ||= {}
  36. mail[ key ]
  37. end
  38. end
  39. def signed?
  40. raise NotImplementedError
  41. end
  42. def encrypted?
  43. raise NotImplementedError
  44. end
  45. def decrypt
  46. raise NotImplementedError
  47. end
  48. def verify_signature
  49. raise NotImplementedError
  50. end
  51. def set_article_preferences(operation:, comment:, success: false)
  52. article_preferences[:security][operation] = {
  53. success: success,
  54. comment: comment,
  55. }
  56. end
  57. private
  58. def parse_decrypted_mail(decrypted_body)
  59. %w[Content-Type Content-Disposition Content-Transfer-Encoding Content-Description].each do |header|
  60. mail[:mail_instance].header[header] = nil
  61. end
  62. Channel::EmailParser.new.parse("#{mail[:mail_instance].header}#{decrypted_body}").each do |key, value|
  63. mail[key] = value
  64. end
  65. update_content_type
  66. end
  67. def update_content_type
  68. # By parsing the decrypted body, the content type might have changed.
  69. @content_type = mail[:mail_instance].content_type
  70. end
  71. def log
  72. %i[sign encryption].each do |action|
  73. result = log_result(action)
  74. next if result.blank?
  75. HttpLog.create(log_result(action))
  76. end
  77. end
  78. def log_result(action)
  79. result = article_preferences[:security][action]
  80. return if result.blank?
  81. if result[:success]
  82. status = 'success'
  83. elsif result[:comment].blank?
  84. # means not performed
  85. return
  86. else
  87. status = 'failed'
  88. end
  89. {
  90. direction: 'in',
  91. facility: type,
  92. url: "#{mail[:from]} -> #{mail[:to]}",
  93. status: status,
  94. ip: nil,
  95. request: {
  96. message_id: mail[:message_id],
  97. },
  98. response: article_preferences[:security],
  99. method: action,
  100. created_by_id: 1,
  101. updated_by_id: 1,
  102. }
  103. end
  104. end