retry.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class SecureMailing::SMIME::Retry < SecureMailing::Backend::Handler
  3. def initialize(article)
  4. super()
  5. @article = article
  6. end
  7. def process
  8. return existing_result if already_processed?
  9. save_result if retry_succeeded?
  10. retry_result
  11. end
  12. def signature_checked?
  13. @signature_checked ||= existing_result&.dig('sign', 'success') || false
  14. end
  15. def decrypted?
  16. @decrypted ||= existing_result&.dig('encryption', 'success') || false
  17. end
  18. def already_processed?
  19. signature_checked? && decrypted?
  20. end
  21. def existing_result
  22. @article.preferences['security']
  23. end
  24. def mail
  25. @mail ||= begin
  26. raw_mail = @article.as_raw.store_file.content
  27. Channel::EmailParser.new.parse(raw_mail).tap do |parsed|
  28. SecureMailing.incoming(parsed)
  29. end
  30. end
  31. end
  32. def retry_result
  33. @retry_result ||= mail['x-zammad-article-preferences']['security']
  34. end
  35. def signature_found?
  36. return false if signature_checked?
  37. retry_result['sign']['success']
  38. end
  39. def decryption_succeeded?
  40. return false if decrypted?
  41. retry_result['encryption']['success']
  42. end
  43. def retry_succeeded?
  44. return true if signature_found?
  45. decryption_succeeded?
  46. end
  47. def save_result
  48. save_decrypted if decryption_succeeded?
  49. @article.preferences['security'] = retry_result
  50. @article.save!
  51. end
  52. def save_decrypted
  53. @article.content_type = mail['content_type']
  54. @article.body = mail['body']
  55. Store.remove(
  56. object: 'Ticket::Article',
  57. o_id: @article.id,
  58. )
  59. mail[:attachments]&.each do |attachment|
  60. filename = attachment[:filename].force_encoding('utf-8')
  61. if !filename.force_encoding('UTF-8').valid_encoding?
  62. filename = filename.utf8_encode(fallback: :read_as_sanitized_binary)
  63. end
  64. Store.add(
  65. object: 'Ticket::Article',
  66. o_id: @article.id,
  67. data: attachment[:data],
  68. filename: filename,
  69. preferences: attachment[:preferences],
  70. created_by_id: @article.created_by_id,
  71. )
  72. end
  73. end
  74. end