2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-06-15 11:10:23 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
require 'mail'
|
2012-12-05 01:38:30 +00:00
|
|
|
require 'encode'
|
2012-12-05 00:28:04 +00:00
|
|
|
|
2012-12-05 01:27:56 +00:00
|
|
|
class Channel::EmailParser
|
2012-10-04 06:54:21 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
mail = parse(msg_as_string)
|
2012-10-04 06:54:21 +00:00
|
|
|
|
|
|
|
mail = {
|
2015-08-30 18:16:29 +00:00
|
|
|
from: 'Some Name <some@example.com>',
|
|
|
|
from_email: 'some@example.com',
|
|
|
|
from_local: 'some',
|
|
|
|
from_domain: 'example.com',
|
|
|
|
from_display_name: 'Some Name',
|
|
|
|
message_id: 'some_message_id@example.com',
|
|
|
|
to: 'Some System <system@example.com>',
|
|
|
|
cc: 'Somebody <somebody@example.com>',
|
|
|
|
subject: 'some message subject',
|
|
|
|
body: 'some message body',
|
|
|
|
attachments: [
|
2012-10-04 07:09:27 +00:00
|
|
|
{
|
2015-08-30 18:16:29 +00:00
|
|
|
data: 'binary of attachment',
|
|
|
|
filename: 'file_name_of_attachment.txt',
|
|
|
|
preferences: {
|
|
|
|
content-alternative: true,
|
|
|
|
Mime-Type: 'text/plain',
|
|
|
|
Charset: 'iso-8859-1',
|
2012-10-04 07:09:27 +00:00
|
|
|
},
|
|
|
|
},
|
2012-10-04 06:54:21 +00:00
|
|
|
],
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# ignore email header
|
2015-08-30 18:16:29 +00:00
|
|
|
x-zammad-ignore: 'false',
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# customer headers
|
2015-08-30 18:16:29 +00:00
|
|
|
x-zammad-customer-login: '',
|
|
|
|
x-zammad-customer-email: '',
|
|
|
|
x-zammad-customer-firstname: '',
|
|
|
|
x-zammad-customer-lastname: '',
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# ticket headers
|
2015-08-30 18:16:29 +00:00
|
|
|
x-zammad-ticket-group: 'some_group',
|
|
|
|
x-zammad-ticket-state: 'some_state',
|
|
|
|
x-zammad-ticket-priority: 'some_priority',
|
|
|
|
x-zammad-ticket-owner: 'some_owner_login',
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# article headers
|
2015-08-30 18:16:29 +00:00
|
|
|
x-zammad-article-internal: false,
|
|
|
|
x-zammad-article-type: 'agent',
|
|
|
|
x-zammad-article-sender: 'customer',
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# all other email headers
|
2015-08-30 18:16:29 +00:00
|
|
|
some-header: 'some_value',
|
2012-10-04 06:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
def parse(msg)
|
2012-05-04 11:33:05 +00:00
|
|
|
data = {}
|
2015-12-14 09:23:14 +00:00
|
|
|
mail = Mail.new(msg)
|
2012-05-04 11:33:05 +00:00
|
|
|
|
2012-05-05 09:24:05 +00:00
|
|
|
# set all headers
|
|
|
|
mail.header.fields.each { |field|
|
2015-01-12 10:55:13 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
next if !field.name
|
2015-01-12 10:55:13 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
# full line, encode, ready for storage
|
2015-12-14 09:23:14 +00:00
|
|
|
data[field.name.to_s.downcase.to_sym] = Encode.conv('utf8', field.to_s)
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
# if we need to access the lines by objects later again
|
|
|
|
data[ "raw-#{field.name.downcase}".to_sym ] = field
|
2012-05-05 09:24:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 22:56:08 +00:00
|
|
|
# get sender
|
|
|
|
from = nil
|
|
|
|
['from', 'reply-to', 'return-path'].each { |item|
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if !mail[ item.to_sym ]
|
|
|
|
|
|
|
|
from = mail[ item.to_sym ].value
|
|
|
|
|
|
|
|
break if from
|
2013-10-31 22:56:08 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 15:14:51 +00:00
|
|
|
# set x-any-recipient
|
|
|
|
data['x-any-recipient'.to_sym] = ''
|
|
|
|
['to', 'cc', 'delivered-to', 'x-original-to', 'envelope-to'].each { |item|
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if !mail[item.to_sym]
|
|
|
|
|
|
|
|
if data['x-any-recipient'.to_sym] != ''
|
|
|
|
data['x-any-recipient'.to_sym] += ', '
|
2014-06-08 15:14:51 +00:00
|
|
|
end
|
2015-05-07 09:04:40 +00:00
|
|
|
data['x-any-recipient'.to_sym] += mail[item.to_sym].to_s
|
2014-06-08 15:14:51 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 09:24:05 +00:00
|
|
|
# set extra headers
|
2013-12-01 17:51:35 +00:00
|
|
|
begin
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:from_email] = Mail::Address.new(from).address
|
|
|
|
data[:from_local] = Mail::Address.new(from).local
|
|
|
|
data[:from_domain] = Mail::Address.new(from).domain
|
|
|
|
data[:from_display_name] = Mail::Address.new(from).display_name ||
|
|
|
|
(Mail::Address.new(from).comments && Mail::Address.new(from).comments[0])
|
2013-12-01 17:51:35 +00:00
|
|
|
rescue
|
2015-03-17 06:59:33 +00:00
|
|
|
data[:from_email] = from
|
|
|
|
data[:from_local] = from
|
|
|
|
data[:from_domain] = from
|
2013-12-01 17:51:35 +00:00
|
|
|
end
|
2012-05-04 11:33:05 +00:00
|
|
|
|
2012-05-05 09:24:05 +00:00
|
|
|
# do extra decoding because we needed to use field.value
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:from_display_name] = Mail::Field.new('X-From', data[:from_display_name]).to_s
|
2012-05-05 09:24:05 +00:00
|
|
|
|
|
|
|
# compat headers
|
|
|
|
data[:message_id] = data['message-id'.to_sym]
|
2012-05-04 11:33:05 +00:00
|
|
|
|
|
|
|
# body
|
2013-06-12 15:59:58 +00:00
|
|
|
# plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
|
|
|
|
# html_part = message.html_part ? message.html_part.body.decoded : nil
|
2012-05-07 22:37:07 +00:00
|
|
|
data[:attachments] = []
|
2012-10-05 06:42:12 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# multi part email
|
2012-05-04 19:30:22 +00:00
|
|
|
if mail.multipart?
|
2012-10-05 06:42:12 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# text attachment/body exists
|
2012-06-15 11:10:23 +00:00
|
|
|
if mail.text_part
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.text_part.body.decoded
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:body] = Encode.conv(mail.text_part.charset, data[:body])
|
2012-10-05 06:42:12 +00:00
|
|
|
|
2014-08-06 11:41:10 +00:00
|
|
|
if !data[:body].valid_encoding?
|
2015-04-27 13:42:53 +00:00
|
|
|
data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
|
2014-08-06 11:41:10 +00:00
|
|
|
end
|
2015-11-26 08:29:58 +00:00
|
|
|
end
|
2014-08-06 11:41:10 +00:00
|
|
|
|
2014-12-29 10:27:59 +00:00
|
|
|
# html attachment/body may exists and will be converted to text
|
2015-11-26 08:29:58 +00:00
|
|
|
if !mail.text_part || !data[:body] || data[:body] == ''
|
2012-06-15 11:10:23 +00:00
|
|
|
filename = '-no name-'
|
2015-01-08 08:26:40 +00:00
|
|
|
if mail.html_part && mail.html_part.body
|
2014-12-29 10:27:59 +00:00
|
|
|
filename = 'message.html'
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.html_part.body.to_s
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:body] = Encode.conv(mail.html_part.charset.to_s, data[:body])
|
2015-01-08 14:27:44 +00:00
|
|
|
data[:body] = data[:body].html2text.to_s.force_encoding('utf-8')
|
2014-08-06 11:41:10 +00:00
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
if !data[:body].force_encoding('UTF-8').valid_encoding?
|
2015-04-27 13:42:53 +00:00
|
|
|
data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
|
2014-08-06 11:41:10 +00:00
|
|
|
end
|
2012-06-15 11:10:23 +00:00
|
|
|
|
2014-12-29 10:27:59 +00:00
|
|
|
# any other attachments
|
2012-06-15 11:10:23 +00:00
|
|
|
else
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = 'no visible content'
|
2012-06-15 11:10:23 +00:00
|
|
|
end
|
2012-07-02 18:52:27 +00:00
|
|
|
end
|
2012-06-15 11:10:23 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# add html attachment/body as real attachment
|
|
|
|
if mail.html_part
|
|
|
|
filename = 'message.html'
|
|
|
|
headers_store = {
|
|
|
|
'content-alternative' => true,
|
|
|
|
}
|
2012-06-15 11:10:23 +00:00
|
|
|
if mail.mime_type
|
|
|
|
headers_store['Mime-Type'] = mail.html_part.mime_type
|
|
|
|
end
|
|
|
|
if mail.charset
|
|
|
|
headers_store['Charset'] = mail.html_part.charset
|
|
|
|
end
|
|
|
|
attachment = {
|
2015-04-27 13:42:53 +00:00
|
|
|
data: mail.html_part.body.to_s,
|
|
|
|
filename: mail.html_part.filename || filename,
|
|
|
|
preferences: headers_store
|
2012-06-15 11:10:23 +00:00
|
|
|
}
|
|
|
|
data[:attachments].push attachment
|
|
|
|
end
|
2012-10-04 06:54:21 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# get attachments
|
2013-01-23 13:47:57 +00:00
|
|
|
if mail.parts
|
|
|
|
mail.parts.each { |part|
|
2013-07-22 08:13:38 +00:00
|
|
|
|
|
|
|
# protect process to work fine with spam emails, see test/fixtures/mail15.box
|
|
|
|
begin
|
2015-12-14 09:23:14 +00:00
|
|
|
attachs = _get_attachment(part, data[:attachments], mail)
|
|
|
|
data[:attachments].concat(attachs)
|
2013-07-22 08:13:38 +00:00
|
|
|
rescue
|
2015-12-14 09:23:14 +00:00
|
|
|
attachs = _get_attachment(part, data[:attachments], mail)
|
|
|
|
data[:attachments].concat(attachs)
|
2012-07-02 18:52:27 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-12-28 23:11:37 +00:00
|
|
|
# not multipart email
|
2012-05-07 22:37:07 +00:00
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
# text part only
|
2016-01-16 10:05:04 +00:00
|
|
|
elsif !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.casecmp('text/plain').zero?
|
2016-01-15 17:22:57 +00:00
|
|
|
data[:body] = mail.body.decoded
|
|
|
|
data[:body] = Encode.conv(mail.charset, data[:body])
|
|
|
|
|
|
|
|
if !data[:body].force_encoding('UTF-8').valid_encoding?
|
|
|
|
data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
|
|
|
|
end
|
|
|
|
|
2016-01-15 20:20:48 +00:00
|
|
|
# html part only, convert to text and add it as attachment
|
2016-01-15 17:22:57 +00:00
|
|
|
else
|
|
|
|
filename = '-no name-'
|
2016-01-16 10:05:04 +00:00
|
|
|
if mail.mime_type.to_s.casecmp('text/html').zero?
|
2016-01-15 17:22:57 +00:00
|
|
|
filename = 'message.html'
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.body.decoded
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:body] = Encode.conv(mail.charset, data[:body])
|
2016-01-15 17:22:57 +00:00
|
|
|
data[:body] = data[:body].html2text.to_s.force_encoding('utf-8')
|
2012-05-07 22:37:07 +00:00
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
if !data[:body].valid_encoding?
|
2015-04-27 13:42:53 +00:00
|
|
|
data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
|
2014-08-06 11:41:10 +00:00
|
|
|
end
|
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
# any other attachments
|
2012-07-02 18:52:27 +00:00
|
|
|
else
|
2016-01-15 17:22:57 +00:00
|
|
|
data[:body] = 'no visible content'
|
|
|
|
end
|
2012-05-07 22:37:07 +00:00
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
# add body as attachment
|
|
|
|
headers_store = {
|
|
|
|
'content-alternative' => true,
|
|
|
|
}
|
|
|
|
if mail.mime_type
|
|
|
|
headers_store['Mime-Type'] = mail.mime_type
|
|
|
|
end
|
|
|
|
if mail.charset
|
|
|
|
headers_store['Charset'] = mail.charset
|
2012-05-07 22:37:07 +00:00
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
attachment = {
|
|
|
|
data: mail.body.decoded,
|
|
|
|
filename: mail.filename || filename,
|
|
|
|
preferences: headers_store
|
|
|
|
}
|
|
|
|
data[:attachments].push attachment
|
2012-05-04 19:30:22 +00:00
|
|
|
end
|
2012-05-04 11:33:05 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# strip not wanted chars
|
2015-12-14 09:23:14 +00:00
|
|
|
data[:body].gsub!(/\n\r/, "\n")
|
|
|
|
data[:body].gsub!(/\r\n/, "\n")
|
2016-01-15 17:22:57 +00:00
|
|
|
data[:body].tr!("\r", "\n")
|
2012-07-02 18:52:27 +00:00
|
|
|
|
2015-08-30 18:16:29 +00:00
|
|
|
# remember original mail instance
|
|
|
|
data[:mail_instance] = mail
|
|
|
|
|
2014-12-29 10:27:59 +00:00
|
|
|
data
|
2012-05-04 11:33:05 +00:00
|
|
|
end
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
def _get_attachment(file, attachments, mail)
|
2013-01-23 13:47:57 +00:00
|
|
|
|
|
|
|
# check if sub parts are available
|
|
|
|
if !file.parts.empty?
|
|
|
|
a = []
|
|
|
|
file.parts.each {|p|
|
2015-12-14 09:23:14 +00:00
|
|
|
attachment = _get_attachment(p, attachments, mail)
|
|
|
|
a.concat(attachment)
|
2013-01-23 13:47:57 +00:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
2014-12-28 23:11:37 +00:00
|
|
|
# ignore text/plain attachments - already shown in view
|
|
|
|
return [] if mail.text_part && mail.text_part.body.to_s == file.body.to_s
|
|
|
|
|
|
|
|
# ignore text/html - html part, already shown in view
|
|
|
|
return [] if mail.html_part && mail.html_part.body.to_s == file.body.to_s
|
|
|
|
|
2013-01-23 13:47:57 +00:00
|
|
|
# get file preferences
|
|
|
|
headers_store = {}
|
|
|
|
file.header.fields.each { |field|
|
|
|
|
headers_store[field.name.to_s] = field.value.to_s
|
|
|
|
}
|
|
|
|
|
|
|
|
# get filename from content-disposition
|
|
|
|
filename = nil
|
|
|
|
|
|
|
|
# workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
|
|
|
|
begin
|
|
|
|
filename = file.header[:content_disposition].filename
|
|
|
|
rescue
|
2016-01-28 18:55:15 +00:00
|
|
|
begin
|
|
|
|
result = file.header[:content_disposition].to_s.scan( /filename=("|)(.+?)("|);/i )
|
|
|
|
if result && result[0] && result[0][1]
|
|
|
|
filename = result[0][1]
|
|
|
|
end
|
2016-01-28 19:08:13 +00:00
|
|
|
rescue
|
|
|
|
Rails.logger.debug 'Unable to get filename'
|
2013-01-23 13:47:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
|
|
|
|
if !filename
|
|
|
|
filename = file.header[:content_location].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# generate file name
|
|
|
|
if !filename || filename.empty?
|
|
|
|
attachment_count = 0
|
|
|
|
(1..1000).each {|count|
|
|
|
|
filename_exists = false
|
|
|
|
filename = 'file-' + count.to_s
|
|
|
|
attachments.each {|attachment|
|
|
|
|
if attachment[:filename] == filename
|
|
|
|
filename_exists = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
break if filename_exists == false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# get mime type
|
|
|
|
if file.header[:content_type] && file.header[:content_type].string
|
|
|
|
headers_store['Mime-Type'] = file.header[:content_type].string
|
|
|
|
end
|
|
|
|
|
|
|
|
# get charset
|
|
|
|
if file.header && file.header.charset
|
|
|
|
headers_store['Charset'] = file.header.charset
|
|
|
|
end
|
|
|
|
|
|
|
|
# remove not needed header
|
|
|
|
headers_store.delete('Content-Transfer-Encoding')
|
|
|
|
headers_store.delete('Content-Disposition')
|
|
|
|
|
|
|
|
attach = {
|
2015-04-27 13:42:53 +00:00
|
|
|
data: file.body.to_s,
|
|
|
|
filename: filename,
|
|
|
|
preferences: headers_store,
|
2013-01-23 13:47:57 +00:00
|
|
|
}
|
2014-12-28 23:11:37 +00:00
|
|
|
[attach]
|
2013-01-23 13:47:57 +00:00
|
|
|
end
|
|
|
|
|
2015-08-30 18:16:29 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
parser = Channel::EmailParser.new
|
|
|
|
ticket, article, user = parser.process(channel, email_raw_string)
|
|
|
|
|
|
|
|
retrns
|
|
|
|
|
|
|
|
[ticket, article, user]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
def process(channel, msg)
|
2015-12-14 09:23:14 +00:00
|
|
|
mail = parse(msg)
|
2012-04-13 16:42:25 +00:00
|
|
|
|
2012-10-05 06:42:12 +00:00
|
|
|
# run postmaster pre filter
|
|
|
|
filters = {
|
|
|
|
'0010' => Channel::Filter::Trusted,
|
2015-09-20 00:16:22 +00:00
|
|
|
'0020' => Channel::Filter::AutoResponseCheck,
|
|
|
|
'0030' => Channel::Filter::OutOfOfficeCheck,
|
2015-08-30 18:16:29 +00:00
|
|
|
'0100' => Channel::Filter::FollowUpCheck,
|
|
|
|
'0900' => Channel::Filter::BounceCheck,
|
2012-10-05 06:42:12 +00:00
|
|
|
'1000' => Channel::Filter::Database,
|
|
|
|
}
|
2012-10-04 07:09:27 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
# filter(channel, mail)
|
2015-05-07 09:49:46 +00:00
|
|
|
filters.each {|_prio, backend|
|
2012-10-05 06:42:12 +00:00
|
|
|
begin
|
2015-08-30 18:16:29 +00:00
|
|
|
backend.run(channel, mail)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.error "can't run postmaster pre filter #{backend}"
|
|
|
|
Rails.logger.error e.inspect
|
2015-08-31 09:27:12 +00:00
|
|
|
raise e
|
2012-10-05 06:42:12 +00:00
|
|
|
end
|
|
|
|
}
|
2012-10-04 07:09:27 +00:00
|
|
|
|
2012-05-06 20:48:23 +00:00
|
|
|
# check ignore header
|
|
|
|
return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
|
|
|
|
|
|
|
|
ticket = nil
|
|
|
|
article = nil
|
|
|
|
user = nil
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# use transaction
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
2013-04-20 10:14:27 +00:00
|
|
|
# reset current_user
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# create sender
|
2012-05-06 20:48:23 +00:00
|
|
|
if mail[ 'x-zammad-customer-login'.to_sym ]
|
2015-12-14 09:23:14 +00:00
|
|
|
user = User.find_by(login: mail[ 'x-zammad-customer-login'.to_sym ])
|
2012-05-06 20:48:23 +00:00
|
|
|
end
|
|
|
|
if !user
|
2015-12-14 09:23:14 +00:00
|
|
|
user = User.find_by(email: mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email])
|
2012-05-06 20:48:23 +00:00
|
|
|
end
|
|
|
|
if !user
|
2015-01-08 14:27:44 +00:00
|
|
|
user = user_create(
|
2015-04-27 13:42:53 +00:00
|
|
|
login: mail[ 'x-zammad-customer-login'.to_sym ] || mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
|
|
|
|
firstname: mail[ 'x-zammad-customer-firstname'.to_sym ] || mail[:from_display_name],
|
|
|
|
lastname: mail[ 'x-zammad-customer-lastname'.to_sym ],
|
|
|
|
email: mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
|
2012-04-13 16:42:25 +00:00
|
|
|
)
|
|
|
|
end
|
2012-10-04 07:09:27 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# create to and cc user
|
2015-01-12 10:55:13 +00:00
|
|
|
['raw-to', 'raw-cc'].each { |item|
|
2015-05-07 09:04:40 +00:00
|
|
|
next if !mail[item.to_sym]
|
2016-01-28 18:55:15 +00:00
|
|
|
next if !mail[item.to_sym].addrs
|
|
|
|
items = mail[item.to_sym].addrs
|
|
|
|
items.each {|address_data|
|
2015-05-07 09:04:40 +00:00
|
|
|
user_create(
|
2015-05-08 10:33:08 +00:00
|
|
|
firstname: address_data.display_name,
|
2015-05-07 09:04:40 +00:00
|
|
|
lastname: '',
|
2015-05-08 10:33:08 +00:00
|
|
|
email: address_data.address,
|
2015-05-07 09:04:40 +00:00
|
|
|
)
|
|
|
|
}
|
2015-01-08 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# set current user
|
|
|
|
UserInfo.current_user_id = user.id
|
2012-10-04 07:09:27 +00:00
|
|
|
|
2015-08-30 18:16:29 +00:00
|
|
|
# get ticket# based on email headers
|
|
|
|
if mail[ 'x-zammad-ticket-id'.to_sym ]
|
|
|
|
ticket = Ticket.find_by( id: mail[ 'x-zammad-ticket-id'.to_sym ] )
|
|
|
|
end
|
|
|
|
if mail[ 'x-zammad-ticket-number'.to_sym ]
|
|
|
|
ticket = Ticket.find_by( number: mail[ 'x-zammad-ticket-number'.to_sym ] )
|
|
|
|
end
|
2012-05-04 11:33:05 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# set ticket state to open if not new
|
|
|
|
if ticket
|
2015-12-14 09:23:14 +00:00
|
|
|
state = Ticket::State.find(ticket.state_id)
|
|
|
|
state_type = Ticket::StateType.find(state.state_type_id)
|
2012-08-28 05:21:45 +00:00
|
|
|
|
|
|
|
# if tickte is merged, find linked ticket
|
2014-06-08 22:01:20 +00:00
|
|
|
if state_type.name == 'merged'
|
2012-08-28 05:21:45 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-09-20 00:16:22 +00:00
|
|
|
# set ticket to open again
|
|
|
|
if state_type.name != 'new' && !mail[ 'x-zammad-out-of-office'.to_sym ]
|
|
|
|
ticket.state = Ticket::State.find_by(name: 'open')
|
2012-04-13 16:42:25 +00:00
|
|
|
ticket.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# create new ticket
|
2012-05-06 20:48:23 +00:00
|
|
|
if !ticket
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
preferences = {}
|
|
|
|
if channel[:id]
|
|
|
|
preferences = {
|
|
|
|
channel_id: channel[:id]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-06-08 03:11:21 +00:00
|
|
|
ticket = Ticket.new(
|
2015-04-27 13:42:53 +00:00
|
|
|
group_id: channel[:group_id] || 1,
|
|
|
|
customer_id: user.id,
|
|
|
|
title: mail[:subject] || '',
|
2015-12-14 09:23:14 +00:00
|
|
|
state_id: Ticket::State.find_by(name: 'new').id,
|
|
|
|
priority_id: Ticket::Priority.find_by(name: '2 normal').id,
|
|
|
|
preferences: preferences,
|
2014-06-08 03:11:21 +00:00
|
|
|
)
|
2012-05-06 20:48:23 +00:00
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
set_attributes_by_x_headers(ticket, 'ticket', mail)
|
2012-05-06 20:48:23 +00:00
|
|
|
|
|
|
|
# create ticket
|
2014-06-08 03:11:21 +00:00
|
|
|
ticket.save
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2012-10-04 06:54:21 +00:00
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# import mail
|
2012-10-04 06:54:21 +00:00
|
|
|
|
2012-05-06 20:48:23 +00:00
|
|
|
# set attributes
|
2014-06-08 03:11:21 +00:00
|
|
|
article = Ticket::Article.new(
|
2015-04-27 13:42:53 +00:00
|
|
|
ticket_id: ticket.id,
|
2015-12-14 09:23:14 +00:00
|
|
|
type_id: Ticket::Article::Type.find_by(name: 'email').id,
|
|
|
|
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
2015-04-27 13:42:53 +00:00
|
|
|
body: mail[:body],
|
|
|
|
from: mail[:from],
|
|
|
|
to: mail[:to],
|
|
|
|
cc: mail[:cc],
|
|
|
|
subject: mail[:subject],
|
|
|
|
message_id: mail[:message_id],
|
|
|
|
internal: false,
|
2014-06-08 03:11:21 +00:00
|
|
|
)
|
2012-05-06 20:48:23 +00:00
|
|
|
|
|
|
|
# x-headers lookup
|
2015-12-14 09:23:14 +00:00
|
|
|
set_attributes_by_x_headers(article, 'article', mail)
|
2012-05-06 20:48:23 +00:00
|
|
|
|
|
|
|
# create article
|
2014-06-08 03:11:21 +00:00
|
|
|
article.save
|
2012-04-13 16:42:25 +00:00
|
|
|
|
|
|
|
# store mail plain
|
|
|
|
Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Ticket::Article::Mail',
|
|
|
|
o_id: article.id,
|
|
|
|
data: msg,
|
|
|
|
filename: "ticket-#{ticket.number}-#{article.id}.eml",
|
|
|
|
preferences: {}
|
2012-04-13 16:42:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# store attachments
|
2012-05-04 11:33:05 +00:00
|
|
|
if mail[:attachments]
|
|
|
|
mail[:attachments].each do |attachment|
|
2012-04-13 16:42:25 +00:00
|
|
|
Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'Ticket::Article',
|
|
|
|
o_id: article.id,
|
|
|
|
data: attachment[:data],
|
|
|
|
filename: attachment[:filename],
|
|
|
|
preferences: attachment[:preferences]
|
2012-04-13 16:42:25 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-07 16:38:09 +00:00
|
|
|
# execute ticket events
|
2012-11-18 10:56:48 +00:00
|
|
|
Observer::Ticket::Notification.transaction
|
2012-05-06 20:48:23 +00:00
|
|
|
|
2012-10-05 06:42:12 +00:00
|
|
|
# run postmaster post filter
|
|
|
|
filters = {
|
2015-12-14 09:23:14 +00:00
|
|
|
# '0010' => Channel::Filter::Trusted,
|
2012-10-05 06:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# filter( channel, mail )
|
2015-05-07 09:49:46 +00:00
|
|
|
filters.each {|_prio, backend|
|
2012-10-05 06:42:12 +00:00
|
|
|
begin
|
2015-12-14 09:23:14 +00:00
|
|
|
backend.run(channel, mail, ticket, article, user)
|
2015-05-08 14:09:24 +00:00
|
|
|
rescue => e
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.error "can't run postmaster post filter #{backend}"
|
|
|
|
Rails.logger.error e.inspect
|
2012-10-05 06:42:12 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-05-06 20:48:23 +00:00
|
|
|
# return new objects
|
2015-09-20 00:16:22 +00:00
|
|
|
[ticket, article, user, mail]
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2012-11-07 16:38:09 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
def user_create(data)
|
|
|
|
|
|
|
|
# return existing
|
2015-12-14 09:23:14 +00:00
|
|
|
user = User.find_by(login: data[:email].downcase)
|
2015-01-08 14:27:44 +00:00
|
|
|
return user if user
|
|
|
|
|
|
|
|
# create new user
|
2015-12-14 09:23:14 +00:00
|
|
|
roles = Role.where(name: 'Customer')
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# fillup
|
2015-05-07 07:23:16 +00:00
|
|
|
%w(firstname lastname).each { |item|
|
2015-05-07 10:11:45 +00:00
|
|
|
if data[item.to_sym].nil?
|
2015-01-08 14:27:44 +00:00
|
|
|
data[item.to_sym] = ''
|
|
|
|
end
|
|
|
|
}
|
|
|
|
data[:password] = ''
|
|
|
|
data[:active] = true
|
|
|
|
data[:roles] = roles
|
|
|
|
data[:updated_by_id] = 1
|
|
|
|
data[:created_by_id] = 1
|
|
|
|
|
|
|
|
user = User.create(data)
|
|
|
|
user.update_attributes(
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_by_id: user.id,
|
|
|
|
created_by_id: user.id,
|
2015-01-08 14:27:44 +00:00
|
|
|
)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2015-12-14 09:23:14 +00:00
|
|
|
def set_attributes_by_x_headers(item_object, header_name, mail)
|
2014-06-08 03:11:21 +00:00
|
|
|
|
|
|
|
# loop all x-zammad-hedaer-* headers
|
2015-05-07 09:49:46 +00:00
|
|
|
item_object.attributes.each {|key, _value|
|
2014-06-08 03:11:21 +00:00
|
|
|
|
|
|
|
# ignore read only attributes
|
|
|
|
next if key == 'updated_at'
|
|
|
|
next if key == 'created_at'
|
|
|
|
next if key == 'updated_by_id'
|
|
|
|
next if key == 'created_by_id'
|
|
|
|
|
|
|
|
# check if id exists
|
2015-04-27 14:42:53 +00:00
|
|
|
key_short = key[ key.length - 3, key.length ]
|
2014-06-08 03:11:21 +00:00
|
|
|
if key_short == '_id'
|
2015-04-27 14:41:03 +00:00
|
|
|
key_short = key[ 0, key.length - 3 ]
|
2014-06-08 03:11:21 +00:00
|
|
|
header = "x-zammad-#{header_name}-#{key_short}"
|
|
|
|
if mail[ header.to_sym ]
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.info "header #{header} found #{mail[ header.to_sym ]}"
|
2014-06-08 03:11:21 +00:00
|
|
|
item_object.class.reflect_on_all_associations.map { |assoc|
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if assoc.name.to_s != key_short
|
|
|
|
|
|
|
|
Rails.logger.info "ASSOC found #{assoc.class_name} lookup #{mail[ header.to_sym ]}"
|
|
|
|
item = assoc.class_name.constantize
|
|
|
|
|
|
|
|
if item.respond_to?(:name)
|
2015-12-14 09:23:14 +00:00
|
|
|
if item.lookup(name: mail[ header.to_sym ])
|
|
|
|
item_object[key] = item.lookup(name: mail[ header.to_sym ]).id
|
2015-05-07 09:04:40 +00:00
|
|
|
end
|
|
|
|
elsif item.respond_to?(:login)
|
2015-12-14 09:23:14 +00:00
|
|
|
if item.lookup(login: mail[ header.to_sym ])
|
|
|
|
item_object[key] = item.lookup(login: mail[ header.to_sym ]).id
|
2014-06-08 03:11:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2012-10-05 06:42:12 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-08 03:11:21 +00:00
|
|
|
|
|
|
|
# check if attribute exists
|
|
|
|
header = "x-zammad-#{header_name}-#{key}"
|
|
|
|
if mail[ header.to_sym ]
|
2015-05-04 18:58:28 +00:00
|
|
|
Rails.logger.info "header #{header} found #{mail[ header.to_sym ]}"
|
2014-06-08 03:11:21 +00:00
|
|
|
item_object[key] = mail[ header.to_sym ]
|
|
|
|
end
|
2012-10-05 06:42:12 +00:00
|
|
|
}
|
2014-06-08 03:11:21 +00:00
|
|
|
|
2012-10-05 06:42:12 +00:00
|
|
|
end
|
2012-07-25 13:58:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)
|
|
|
|
module Mail
|
|
|
|
module Encodings
|
2015-05-05 11:01:16 +00:00
|
|
|
def self.value_decode(str)
|
2012-07-25 13:58:08 +00:00
|
|
|
# Optimization: If there's no encoded-words in the string, just return it
|
2015-04-27 13:20:16 +00:00
|
|
|
return str unless str.index('=?')
|
2012-07-25 13:58:08 +00:00
|
|
|
|
|
|
|
str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
|
|
|
|
|
|
|
|
# Split on white-space boundaries with capture, so we capture the white-space as well
|
|
|
|
str.split(/([ \t])/).map do |text|
|
|
|
|
if text.index('=?') .nil?
|
|
|
|
text
|
|
|
|
else
|
|
|
|
# Join QP encoded-words that are adjacent to avoid decoding partial chars
|
2013-06-12 15:59:58 +00:00
|
|
|
# text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
|
2012-07-25 13:58:08 +00:00
|
|
|
|
|
|
|
# Search for occurences of quoted strings or plain strings
|
|
|
|
text.scan(/( # Group around entire regex to include it in matches
|
2013-06-13 07:01:06 +00:00
|
|
|
\=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
|
|
|
|
| # or
|
|
|
|
.+?(?=\=\?|$) # Plain String
|
2013-06-12 15:59:58 +00:00
|
|
|
)/xmi).map do |matches|
|
2012-07-25 13:58:08 +00:00
|
|
|
string, method = *matches
|
|
|
|
if method == 'b' || method == 'B'
|
|
|
|
b_value_decode(string)
|
|
|
|
elsif method == 'q' || method == 'Q'
|
|
|
|
q_value_decode(string)
|
|
|
|
else
|
|
|
|
string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
end.join('')
|
2012-07-25 13:58:08 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|