guess_configuration.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Channel::Email::GuessConfiguration < Channel::Email::BaseConfiguration
  4. description 'Try to guess email channel configuration from user credentials'
  5. argument :email_address, String, description: 'User email address to guess configuration for'
  6. argument :password, String, description: 'User password'
  7. field :result, Gql::Types::Channel::Email::GuessConfigurationResult, null: false, description: 'Holds the guessed configurations.'
  8. def resolve(email_address:, password:)
  9. internal_result = EmailHelper::Probe.full(email: email_address, password:)
  10. return { result: {} } if !internal_result || internal_result[:result] != 'ok'
  11. {
  12. result: {
  13. inbound_configuration: map_config_to_type(internal_result.dig(:setting, :inbound)),
  14. outbound_configuration: map_config_to_type(internal_result.dig(:setting, :outbound)),
  15. }.tap do |result|
  16. result[:mailbox_stats] = internal_result.slice(:content_messages, :archive_possible, :archive_possible_is_fallback, :archive_week_range) if internal_result[:content_messages]
  17. end
  18. }
  19. end
  20. private
  21. def map_config_to_type(hash)
  22. return nil if !hash.is_a?(Hash)
  23. hash
  24. .slice(:adapter)
  25. .merge(hash[:options])
  26. .tap { _1[:ssl] = map_ssl_value(_1) }
  27. end
  28. def map_ssl_value(hash)
  29. if hash[:start_tls]
  30. 'starttls'
  31. elsif hash[:ssl]
  32. 'ssl'
  33. else
  34. 'off'
  35. end
  36. end
  37. end
  38. end