retry.rb 2.0 KB

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