Improved email processing and cleaning up firstname/lastname detection.

This commit is contained in:
Martin Edenhofer 2018-07-16 17:07:23 +02:00
parent 673b7c3d65
commit ef179bf67b
4 changed files with 339 additions and 61 deletions

View file

@ -365,7 +365,7 @@ returns
data[:from_local] = mail_address.local data[:from_local] = mail_address.local
data[:from_domain] = mail_address.domain data[:from_domain] = mail_address.domain
data[:from_display_name] = mail_address.display_name || mail_address.comments&.first data[:from_display_name] = mail_address.display_name || mail_address.comments&.first
elsif from =~ /^(.+?)<((.+?)@(.+?))>$/ elsif from =~ /^(.+?)<((.+?)@(.+?))>/
data[:from_email] = $2 data[:from_email] = $2
data[:from_local] = $3 data[:from_local] = $3
data[:from_domain] = $4 data[:from_domain] = $4

View file

@ -145,8 +145,9 @@ module Channel::Filter::IdentifySender
def self.user_create(data, role_ids = nil) def self.user_create(data, role_ids = nil)
data[:email] += '@local' if !data[:email].match?(/@/) data[:email] += '@local' if !data[:email].match?(/@/)
user = User.find_by(email: data[:email].downcase) || data[:email] = cleanup_email(data[:email])
User.find_by(login: data[:email].downcase) user = User.find_by(email: data[:email]) ||
User.find_by(login: data[:email])
# check if firstname or lastname need to be updated # check if firstname or lastname need to be updated
if user if user
@ -194,4 +195,11 @@ module Channel::Filter::IdentifySender
string string
end end
def self.cleanup_email(string)
string = string.downcase
string.strip!
string.delete!('"')
string
end
end end

View file

@ -360,19 +360,24 @@ returns
next if local_url.blank? next if local_url.blank?
url = local_url url = local_url
end end
create!( begin
login: hash['info']['nickname'] || hash['uid'], create!(
firstname: hash['info']['name'], login: hash['info']['nickname'] || hash['uid'],
email: hash['info']['email'], firstname: hash['info']['name'],
image_source: hash['info']['image'], email: hash['info']['email'],
web: url, image_source: hash['info']['image'],
address: hash['info']['location'], web: url,
note: hash['info']['description'], address: hash['info']['location'],
source: hash['provider'], note: hash['info']['description'],
role_ids: role_ids, source: hash['provider'],
updated_by_id: 1, role_ids: role_ids,
created_by_id: 1, updated_by_id: 1,
) created_by_id: 1,
)
rescue => e
logger.error e
raise Exceptions::UnprocessableEntity, e.message
end
end end
=begin =begin
@ -532,10 +537,10 @@ returns
return if username.blank? return if username.blank?
# try to find user based on login # try to find user based on login
user = User.find_by(login: username.downcase, active: true) user = User.find_by(login: username.downcase.strip, active: true)
# try second lookup with email # try second lookup with email
user ||= User.find_by(email: username.downcase, active: true) user ||= User.find_by(email: username.downcase.strip, active: true)
# check if email address exists # check if email address exists
return if !user return if !user
@ -829,6 +834,62 @@ returns
Cache.delete(key) Cache.delete(key)
end end
=begin
try to find correct name
[firstname, lastname] = User.name_guess('Some Name', 'some.name@example.com')
=end
def self.name_guess(string, email = nil)
return if string.blank? && email.blank?
string.strip!
firstname = ''
lastname = ''
# "Lastname, Firstname"
if string.match?(',')
name = string.split(', ', 2)
if name.count == 2
if name[0].present?
lastname = name[0].strip
end
if name[1].present?
firstname = name[1].strip
end
return [firstname, lastname] if firstname.present? || lastname.present?
end
end
# "Firstname Lastname"
if string =~ /^(((Dr\.|Prof\.)[[:space:]]|).+?)[[:space:]](.+?)$/i
if $1.present?
firstname = $1.strip
end
if $4.present?
lastname = $4.strip
end
return [firstname, lastname] if firstname.present? || lastname.present?
end
# -no name- "firstname.lastname@example.com"
if string.blank? && email.present?
scan = email.scan(/^(.+?)\.(.+?)\@.+?$/)
if scan[0].present?
if scan[0][0].present?
firstname = scan[0][0].strip
end
if scan[0][1].present?
lastname = scan[0][1].strip
end
return [firstname, lastname] if firstname.present? || lastname.present?
end
end
nil
end
private private
def check_name def check_name
@ -842,43 +903,21 @@ returns
return true if firstname.present? && lastname.present? return true if firstname.present? && lastname.present?
if (firstname.blank? && lastname.present?) || (firstname.present? && lastname.blank?) if (firstname.blank? && lastname.present?) || (firstname.present? && lastname.blank?)
# "Lastname, Firstname"
used_name = firstname.presence || lastname used_name = firstname.presence || lastname
name = used_name.split(', ', 2) (local_firstname, local_lastname) = User.name_guess(used_name, email)
if name.count == 2
if name[0].present?
self.lastname = name[0]
end
if name[1].present?
self.firstname = name[1]
end
return true
end
# "Firstname Lastname"
name = used_name.split(' ', 2)
if name.count == 2
if name[0].present?
self.firstname = name[0]
end
if name[1].present?
self.lastname = name[1]
end
return true
end
# -no name- "firstname.lastname@example.com"
elsif firstname.blank? && lastname.blank? && email.present? elsif firstname.blank? && lastname.blank? && email.present?
scan = email.scan(/^(.+?)\.(.+?)\@.+?$/) (local_firstname, local_lastname) = User.name_guess('', email)
if scan[0] end
if scan[0][0].present?
self.firstname = scan[0][0].capitalize self.firstname = local_firstname if local_firstname.present?
end self.lastname = local_lastname if local_lastname.present?
if scan[0][1].present?
self.lastname = scan[0][1].capitalize if firstname.present? && firstname.match(/^[A-z]+$/) && (firstname.downcase == firstname || firstname.upcase == firstname)
end firstname.capitalize!
end end
if lastname.present? && lastname.match(/^[A-z]+$/) && (lastname.downcase == lastname || lastname.upcase == lastname)
lastname.capitalize!
end end
true true
end end

View file

@ -172,6 +172,237 @@ Some Textäöü".encode('ISO-8859-1'),
}, },
}, },
}, },
{
data: 'From: Bob Smith <customer_with_blank_at_end@example.com> >
To: zammad@example.com
Subject: abc some subject
Some Text with stange from line',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'abc some subject',
},
1 => {
body: 'Some Text with stange from line',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'Bob',
lastname: 'Smith',
fullname: 'Bob Smith',
email: 'customer_with_blank_at_end@example.com',
},
],
},
},
{
data: 'From: dr. daniel rodriguez <info_dr_daniel"@example.de>
To: zammad@example.com
Subject: abc some subject
Some Text with stange from line',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'abc some subject',
},
1 => {
body: 'Some Text with stange from line',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'dr. daniel',
lastname: 'Rodriguez',
fullname: 'dr. daniel Rodriguez',
email: 'info_dr_daniel@example.de',
},
],
},
},
{
data: 'From: Dr. Daniel Rodriguez <info_dr_daniel_r"@example.de>
To: zammad@example.com
Subject: abc some subject
Some Text with stange from line',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'abc some subject',
},
1 => {
body: 'Some Text with stange from line',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'Dr. Daniel',
lastname: 'Rodriguez',
fullname: 'Dr. Daniel Rodriguez',
email: 'info_dr_daniel_r@example.de',
},
],
},
},
{
data: 'From: akademie@example.com> <ilka.mueller@example.com>
To: zammad@example.com
Subject: abc some subject
Some Text with stange from line',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'abc some subject',
},
1 => {
body: 'Some Text with stange from line',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'akademie@example.com>',
lastname: '',
fullname: 'akademie@example.com>',
email: 'ilka.mueller@example.com',
},
],
},
},
{
data: 'From: yann degran <yann@example.com>
To: zammad@example.com
Subject: test name in downcase #1
test name in downcase #1',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'test name in downcase #1',
},
1 => {
body: 'test name in downcase #1',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'Yann',
lastname: 'Degran',
fullname: 'Yann Degran',
email: 'yann@example.com',
},
],
},
},
{
data: 'From: yann Degran <yann2@example.com>
To: zammad@example.com
Subject: test name in downcase #2
test name in downcase #2',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'test name in downcase #2',
},
1 => {
body: 'test name in downcase #2',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'Yann',
lastname: 'Degran',
fullname: 'Yann Degran',
email: 'yann2@example.com',
},
],
},
},
{
data: 'From: YANN DEGRAN <yann3@example.com>
To: zammad@example.com
Subject: test name in downcase #3
test name in downcase #3',
channel: {
trusted: false,
},
success: true,
result: {
0 => {
priority: '2 normal',
title: 'test name in downcase #3',
},
1 => {
body: 'test name in downcase #3',
sender: 'Customer',
type: 'email',
internal: false,
},
},
verify: {
users: [
{
firstname: 'Yann',
lastname: 'Degran',
fullname: 'Yann Degran',
email: 'yann3@example.com',
},
],
},
},
{ {
data: "From: Realname data: "From: Realname
To: customer@example.com To: customer@example.com
@ -226,9 +457,9 @@ Some Text",
verify: { verify: {
users: [ users: [
{ {
firstname: 'max', firstname: 'Max',
lastname: '', lastname: '',
fullname: 'max', fullname: 'Max',
email: 'somebody_else@example.com', email: 'somebody_else@example.com',
}, },
{ {
@ -2354,9 +2585,9 @@ Some Text',
verify: { verify: {
users: [ users: [
{ {
firstname: 'bertha mou', firstname: 'Bertha',
lastname: '', lastname: 'Mou',
fullname: 'bertha mou', fullname: 'Bertha Mou',
email: 'zhengkang@ha.chinamobile.com', email: 'zhengkang@ha.chinamobile.com',
}, },
], ],
@ -2404,8 +2635,8 @@ Some Text',
{ {
firstname: '', firstname: '',
lastname: '', lastname: '',
fullname: '"finances8@firstfinanceloanfirm.example.com"', fullname: 'finances8@firstfinanceloanfirm.example.com',
email: '"finances8@firstfinanceloanfirm.example.com"', email: 'finances8@firstfinanceloanfirm.example.com',
}, },
], ],
} }
@ -2560,9 +2791,9 @@ Some Text',
verify: { verify: {
users: [ users: [
{ {
firstname: 'EXAMPLE', firstname: 'Example',
lastname: 'HotPriceMail', lastname: 'HotPriceMail',
fullname: 'EXAMPLE HotPriceMail', fullname: 'Example HotPriceMail',
email: 'anja.weber@example.de', email: 'anja.weber@example.de',
}, },
], ],