check_validity.rb 902 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Common::User::Email::CheckValidity < Sequencer::Unit::Base
  3. prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
  4. skip_action :skipped, :failed
  5. uses :mapped
  6. def process
  7. return if mapped[:email].blank?
  8. # TODO: This should be done totally somewhere central
  9. mapped[:email] = ensure_valid_email(mapped[:email])
  10. end
  11. private
  12. def ensure_valid_email(source)
  13. # TODO: should get unified with User#check_email
  14. email = extract_email(source)
  15. return if !email
  16. email.downcase
  17. end
  18. def extract_email(source)
  19. # Support format like "Bob Smith (bob@example.com)"
  20. if source =~ %r{\((.+@.+)\)}
  21. source = $1
  22. end
  23. Mail::Address.new(source).address
  24. rescue
  25. return source if source !~ %r{<\s*([^>]+)}
  26. $1.strip
  27. end
  28. end