2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class ArticleCustomer
|
|
|
|
include Import::Helper
|
|
|
|
|
|
|
|
def initialize(article)
|
2016-12-12 09:34:07 +00:00
|
|
|
import(article)
|
2019-06-28 11:38:49 +00:00
|
|
|
rescue Exceptions::UnprocessableEntity
|
2016-11-25 16:10:37 +00:00
|
|
|
log "ERROR: Can't extract customer from Article #{article[:id]}"
|
|
|
|
end
|
|
|
|
|
2021-09-20 10:47:05 +00:00
|
|
|
def self.mutex
|
|
|
|
@mutex ||= Mutex.new
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
class << self
|
2016-11-25 16:10:37 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def find(article)
|
2017-03-27 10:47:33 +00:00
|
|
|
email = local_email(article['From'])
|
2017-03-28 09:28:07 +00:00
|
|
|
return if !email
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
user = ::User.find_by(email: email)
|
|
|
|
user ||= ::User.find_by(login: email)
|
|
|
|
user
|
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
|
2017-03-27 10:47:33 +00:00
|
|
|
def local_email(from)
|
|
|
|
# TODO: should get unified with User#check_email
|
2017-03-28 09:28:07 +00:00
|
|
|
email = extract_email(from)
|
|
|
|
return if !email
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-28 09:28:07 +00:00
|
|
|
email.downcase
|
2017-03-27 10:47:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def extract_email(from)
|
|
|
|
Mail::Address.new(from).address
|
|
|
|
rescue
|
2021-05-12 11:37:44 +00:00
|
|
|
return from if from !~ %r{<\s*([^>]+)}
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-16 14:06:24 +00:00
|
|
|
$1.strip
|
2016-12-12 09:34:07 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def import(article)
|
|
|
|
find_or_create(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def find_or_create(article)
|
2021-09-20 10:47:05 +00:00
|
|
|
self.class.mutex.synchronize do
|
|
|
|
return if self.class.find(article)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-09-20 10:47:05 +00:00
|
|
|
create(article)
|
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def create(article)
|
2017-03-27 10:47:33 +00:00
|
|
|
email = self.class.local_email(article['From'])
|
2016-11-25 16:10:37 +00:00
|
|
|
::User.create(
|
|
|
|
login: email,
|
2016-12-12 09:34:07 +00:00
|
|
|
firstname: extract_display_name(article['From']),
|
2016-11-25 16:10:37 +00:00
|
|
|
lastname: '',
|
|
|
|
email: email,
|
|
|
|
password: '',
|
|
|
|
active: true,
|
|
|
|
role_ids: roles,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def roles
|
|
|
|
[
|
|
|
|
Role.find_by(name: 'Customer').id
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_display_name(from)
|
|
|
|
# do extra decoding because we needed to use field.value
|
|
|
|
Mail::Field.new('X-From', parsed_display_name(from)).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_display_name(from)
|
|
|
|
parsed_address = Mail::Address.new(from)
|
|
|
|
return parsed_address.display_name if parsed_address.display_name
|
2017-11-23 08:09:44 +00:00
|
|
|
return from if parsed_address.comments.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
parsed_address.comments[0]
|
|
|
|
rescue
|
|
|
|
from
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|