2012-06-15 11:10:23 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
require 'mail'
|
2012-12-05 00:28:04 +00:00
|
|
|
#require 'iconv'
|
2012-04-13 16:42:25 +00:00
|
|
|
class Channel::EmailParser
|
2012-05-04 11:33:05 +00:00
|
|
|
def conv (charset, string)
|
2012-12-05 00:28:04 +00:00
|
|
|
|
|
|
|
# if no charset is given, use LATIN1 as default
|
2012-05-07 22:37:07 +00:00
|
|
|
if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
|
2012-05-04 11:33:05 +00:00
|
|
|
charset = 'LATIN1'
|
|
|
|
end
|
2012-12-05 00:28:04 +00:00
|
|
|
|
|
|
|
# return if string is false
|
|
|
|
return string if !string
|
|
|
|
|
|
|
|
# validate already existing utf8 strings
|
|
|
|
if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
|
|
|
|
begin
|
|
|
|
|
|
|
|
# return if encoding is valid
|
|
|
|
utf8 = string.force_encoding('UTF-8')
|
|
|
|
return utf8 if utf8.valid_encoding?
|
|
|
|
|
|
|
|
# try to encode from Windows-1252 to utf8
|
|
|
|
string.encode!( 'UTF-8', 'Windows-1252' )
|
|
|
|
|
|
|
|
rescue EncodingError => e
|
|
|
|
puts "Bad encoding: #{new_value.inspect}"
|
|
|
|
string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' )
|
|
|
|
end
|
|
|
|
return string
|
|
|
|
end
|
2012-05-04 19:30:22 +00:00
|
|
|
# puts '-------' + charset
|
|
|
|
# puts string
|
2012-12-05 00:28:04 +00:00
|
|
|
|
|
|
|
# convert string
|
|
|
|
string.encode!( 'UTF-8', charset.upcase )
|
|
|
|
# Iconv.conv( 'UTF8', charset, string )
|
2012-05-04 11:33:05 +00:00
|
|
|
end
|
2012-10-04 06:54:21 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
mail = parse( msg_as_string )
|
|
|
|
|
|
|
|
mail = {
|
|
|
|
: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',
|
2012-10-04 07:09:27 +00:00
|
|
|
:to => 'Some System <system@example.com>',
|
|
|
|
:cc => 'Somebody <somebody@example.com>',
|
|
|
|
:subject => 'some message subject',
|
|
|
|
:body => 'some message body',
|
2012-10-04 06:54:21 +00:00
|
|
|
:attachments => [
|
2012-10-04 07:09:27 +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 06:54:21 +00:00
|
|
|
],
|
2012-10-04 07:09:27 +00:00
|
|
|
|
|
|
|
# ignore email header
|
|
|
|
:x-zammad-ignore => 'false',
|
|
|
|
|
|
|
|
# customer headers
|
|
|
|
:x-zammad-customer-login => '',
|
|
|
|
:x-zammad-customer-email => '',
|
|
|
|
:x-zammad-customer-firstname => '',
|
|
|
|
:x-zammad-customer-lastname => '',
|
|
|
|
|
|
|
|
# ticket headers
|
|
|
|
:x-zammad-group => 'some_group',
|
|
|
|
:x-zammad-state => 'some_state',
|
|
|
|
:x-zammad-priority => 'some_priority',
|
|
|
|
:x-zammad-owner => 'some_owner_login',
|
|
|
|
|
|
|
|
# article headers
|
2012-10-05 06:42:12 +00:00
|
|
|
:x-zammad-article-visability => 'internal',
|
2012-10-04 07:09:27 +00:00
|
|
|
:x-zammad-article-type => 'agent',
|
|
|
|
:x-zammad-article-sender => 'customer',
|
|
|
|
|
|
|
|
# all other email headers
|
|
|
|
:some-header => 'some_value',
|
2012-10-04 06:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
def parse (msg)
|
|
|
|
data = {}
|
2012-04-13 16:42:25 +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|
|
2012-12-05 00:28:04 +00:00
|
|
|
data[field.name.downcase.to_sym] = conv( 'utf8', field.to_s )
|
2012-05-05 09:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# set extra headers
|
2012-05-04 11:33:05 +00:00
|
|
|
data[:from_email] = Mail::Address.new( mail[:from].value ).address
|
2012-05-05 09:24:05 +00:00
|
|
|
data[:from_local] = Mail::Address.new( mail[:from].value ).local
|
|
|
|
data[:from_domain] = Mail::Address.new( mail[:from].value ).domain
|
2012-05-07 22:37:07 +00:00
|
|
|
data[:from_display_name] = Mail::Address.new( mail[:from].value ).display_name ||
|
|
|
|
( Mail::Address.new( mail[:from].value ).comments && Mail::Address.new( mail[:from].value ).comments[0] )
|
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
|
|
|
|
data[:from_display_name] = Mail::Field.new( 'X-From', data[:from_display_name] ).to_s
|
|
|
|
|
|
|
|
# compat headers
|
|
|
|
data[:message_id] = data['message-id'.to_sym]
|
2012-05-04 11:33:05 +00:00
|
|
|
|
|
|
|
# body
|
2012-05-04 19:30:22 +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
|
|
|
|
data[:body] = conv( mail.text_part.charset, data[:body] )
|
2012-10-05 06:42:12 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# html attachment/body may exists and will be converted to text
|
2012-06-15 11:10:23 +00:00
|
|
|
else
|
|
|
|
filename = '-no name-'
|
|
|
|
if mail.html_part.body
|
|
|
|
filename = 'html-email'
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.html_part.body.to_s
|
|
|
|
data[:body] = conv( mail.html_part.charset.to_s, data[:body] )
|
|
|
|
data[:body] = html2ascii( data[:body] )
|
2012-06-15 11:10:23 +00:00
|
|
|
|
|
|
|
# any other attachments
|
|
|
|
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 = {
|
2012-06-15 11:27:52 +00:00
|
|
|
:data => mail.html_part.body.to_s,
|
2012-06-15 11:10:23 +00:00
|
|
|
:filename => mail.html_part.filename || filename,
|
2012-10-04 06:54:21 +00:00
|
|
|
: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
|
|
|
|
if mail.has_attachments?
|
|
|
|
mail.attachments.each { |file|
|
|
|
|
|
|
|
|
# get file preferences
|
|
|
|
headers_store = {}
|
|
|
|
file.header.fields.each { |field|
|
|
|
|
headers_store[field.name.to_s] = field.value.to_s
|
|
|
|
}
|
2012-10-24 18:23:04 +00:00
|
|
|
|
|
|
|
# get filename from content-disposition
|
2012-07-02 18:52:27 +00:00
|
|
|
filename = nil
|
2012-12-04 22:08:03 +00:00
|
|
|
|
|
|
|
# workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
|
|
|
|
begin
|
2012-07-02 18:52:27 +00:00
|
|
|
filename = file.header[:content_disposition].filename
|
2012-12-04 22:08:03 +00:00
|
|
|
rescue
|
|
|
|
result = file.header[:content_disposition].to_s.scan( /filename=("|)(.+?)("|);/i )
|
|
|
|
if result && result[0] && result[0][1]
|
|
|
|
filename = result[0][1]
|
|
|
|
end
|
2012-07-02 18:52:27 +00:00
|
|
|
end
|
2012-10-24 18:23:04 +00:00
|
|
|
|
|
|
|
# for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
|
|
|
|
if !filename
|
|
|
|
filename = file.header[:content_location].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# get mime type
|
|
|
|
if file.header[:content_type] && file.header[:content_type].string
|
2012-07-02 18:52:27 +00:00
|
|
|
headers_store['Mime-Type'] = file.header[:content_type].string
|
|
|
|
end
|
2012-10-24 18:23:04 +00:00
|
|
|
|
|
|
|
# get charset
|
|
|
|
if file.header && file.header.charset
|
2012-07-02 18:52:27 +00:00
|
|
|
headers_store['Charset'] = file.header.charset
|
|
|
|
end
|
2012-10-04 06:54:21 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# remove not needed header
|
|
|
|
headers_store.delete('Content-Transfer-Encoding')
|
|
|
|
headers_store.delete('Content-Disposition')
|
|
|
|
|
|
|
|
attach = {
|
|
|
|
:data => file.body.to_s,
|
|
|
|
:filename => filename,
|
2012-10-04 06:54:21 +00:00
|
|
|
:preferences => headers_store,
|
2012-07-02 18:52:27 +00:00
|
|
|
}
|
2012-10-24 18:23:04 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
data[:attachments].push attach
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# not multipart email
|
2012-05-04 19:30:22 +00:00
|
|
|
else
|
2012-05-07 22:37:07 +00:00
|
|
|
|
|
|
|
# text part
|
|
|
|
if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain'
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.body.decoded
|
|
|
|
data[:body] = conv( mail.charset, data[:body] )
|
2012-05-07 22:37:07 +00:00
|
|
|
|
2012-07-02 18:52:27 +00:00
|
|
|
# html part
|
|
|
|
else
|
2012-05-07 22:37:07 +00:00
|
|
|
filename = '-no name-'
|
|
|
|
if mail.mime_type.to_s.downcase == 'text/html'
|
|
|
|
filename = 'html-email'
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = mail.body.decoded
|
|
|
|
data[:body] = conv( mail.charset, data[:body] )
|
|
|
|
data[:body] = html2ascii( data[:body] )
|
2012-05-07 22:37:07 +00:00
|
|
|
|
|
|
|
# any other attachments
|
|
|
|
else
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body] = 'no visible content'
|
2012-05-07 22:37:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# add body as attachment
|
2012-07-02 18:52:27 +00:00
|
|
|
headers_store = {
|
|
|
|
'content-alternative' => true,
|
|
|
|
}
|
2012-05-07 22:37:07 +00:00
|
|
|
if mail.mime_type
|
|
|
|
headers_store['Mime-Type'] = mail.mime_type
|
|
|
|
end
|
|
|
|
if mail.charset
|
|
|
|
headers_store['Charset'] = mail.charset
|
|
|
|
end
|
|
|
|
attachment = {
|
|
|
|
:data => mail.body.decoded,
|
|
|
|
:filename => mail.filename || filename,
|
2012-10-24 18:23:04 +00:00
|
|
|
:preferences => headers_store
|
2012-05-07 22:37:07 +00:00
|
|
|
}
|
|
|
|
data[:attachments].push attachment
|
|
|
|
end
|
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
|
2012-10-04 06:54:21 +00:00
|
|
|
data[:body].gsub!( /\r\n/, "\n" )
|
|
|
|
data[:body].gsub!( /\r/, "\n" )
|
2012-07-02 18:52:27 +00:00
|
|
|
|
2012-05-04 11:33:05 +00:00
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
def process(channel, msg)
|
|
|
|
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,
|
|
|
|
'1000' => Channel::Filter::Database,
|
|
|
|
}
|
2012-10-04 07:09:27 +00:00
|
|
|
|
2012-10-05 06:42:12 +00:00
|
|
|
# filter( channel, mail )
|
|
|
|
filters.each {|prio, backend|
|
|
|
|
begin
|
|
|
|
backend.run( channel, mail )
|
|
|
|
rescue Exception => e
|
|
|
|
puts "can't run postmaster pre filter #{backend}"
|
|
|
|
puts e.inspect
|
|
|
|
return false
|
|
|
|
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
|
|
|
|
|
2012-05-06 20:48:23 +00:00
|
|
|
if mail[ 'x-zammad-customer-login'.to_sym ]
|
|
|
|
user = User.where( :login => mail[ 'x-zammad-customer-login'.to_sym ] ).first
|
|
|
|
end
|
|
|
|
if !user
|
|
|
|
user = User.where( :email => mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email] ).first
|
|
|
|
end
|
|
|
|
if !user
|
2012-04-13 16:42:25 +00:00
|
|
|
puts 'create user...'
|
|
|
|
roles = Role.where( :name => 'Customer' )
|
|
|
|
user = User.create(
|
2012-05-06 20:48:23 +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
|
|
|
:password => '',
|
|
|
|
:active => true,
|
|
|
|
:roles => roles,
|
2012-11-07 12:57:13 +00:00
|
|
|
:updated_by_id => 1,
|
|
|
|
:created_by_id => 1,
|
2012-04-13 16:42:25 +00:00
|
|
|
)
|
|
|
|
end
|
2012-10-04 07:09:27 +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
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# get ticket# from subject
|
2012-05-04 11:33:05 +00:00
|
|
|
ticket = Ticket.number_check( mail[:subject] )
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
# set ticket state to open if not new
|
|
|
|
if ticket
|
|
|
|
ticket_state = Ticket::State.find( ticket.ticket_state_id )
|
|
|
|
ticket_state_type = Ticket::StateType.find( ticket_state.ticket_state_type_id )
|
2012-08-28 05:21:45 +00:00
|
|
|
|
|
|
|
# if tickte is merged, find linked ticket
|
|
|
|
if ticket_state_type.name == 'merged'
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-04-13 16:42:25 +00:00
|
|
|
if ticket_state_type.name != 'new'
|
|
|
|
ticket.ticket_state = Ticket::State.where( :name => 'open' ).first
|
|
|
|
ticket.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# create new ticket
|
2012-05-06 20:48:23 +00:00
|
|
|
if !ticket
|
|
|
|
|
|
|
|
# set attributes
|
|
|
|
ticket_attributes = {
|
2012-05-04 19:30:22 +00:00
|
|
|
:group_id => channel[:group_id] || 1,
|
2012-04-13 16:42:25 +00:00
|
|
|
:customer_id => user.id,
|
2012-08-28 05:21:45 +00:00
|
|
|
:title => mail[:subject] || '',
|
2012-05-06 20:48:23 +00:00
|
|
|
:ticket_state_id => Ticket::State.where( :name => 'new' ).first.id,
|
|
|
|
:ticket_priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id,
|
2012-11-07 12:57:13 +00:00
|
|
|
:updated_by_id => user.id,
|
2012-05-06 20:48:23 +00:00
|
|
|
:created_by_id => user.id,
|
|
|
|
}
|
|
|
|
|
|
|
|
# x-headers lookup
|
|
|
|
map = [
|
|
|
|
[ 'x-zammad-group', Group, 'group_id', 'name' ],
|
|
|
|
[ 'x-zammad-state', Ticket::State, 'ticket_state_id', 'name' ],
|
|
|
|
[ 'x-zammad-priority', Ticket::Priority, 'ticket_priority_id', 'name' ],
|
|
|
|
[ 'x-zammad-owner', User, 'owner_id', 'login' ],
|
|
|
|
]
|
2012-10-05 06:42:12 +00:00
|
|
|
object_lookup( ticket_attributes, map, mail )
|
2012-05-06 20:48:23 +00:00
|
|
|
|
|
|
|
# create ticket
|
|
|
|
ticket = Ticket.create( ticket_attributes )
|
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
|
|
|
|
internal = false
|
|
|
|
if mail[ 'X-Zammad-Article-Visability'.to_sym ] && mail[ 'X-Zammad-Article-Visability'.to_sym ] == 'internal'
|
|
|
|
internal = true
|
|
|
|
end
|
|
|
|
article_attributes = {
|
2012-04-13 16:42:25 +00:00
|
|
|
:created_by_id => user.id,
|
2012-11-07 12:57:13 +00:00
|
|
|
:updated_by_id => user.id,
|
2012-04-13 16:42:25 +00:00
|
|
|
:ticket_id => ticket.id,
|
2012-05-06 20:48:23 +00:00
|
|
|
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id,
|
|
|
|
:ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Customer' ).first.id,
|
2012-10-04 06:54:21 +00:00
|
|
|
:body => mail[:body],
|
2012-05-04 11:33:05 +00:00
|
|
|
:from => mail[:from],
|
|
|
|
:to => mail[:to],
|
|
|
|
:cc => mail[:cc],
|
|
|
|
:subject => mail[:subject],
|
|
|
|
:message_id => mail[:message_id],
|
2012-05-06 20:48:23 +00:00
|
|
|
:internal => internal,
|
|
|
|
}
|
|
|
|
|
|
|
|
# x-headers lookup
|
|
|
|
map = [
|
|
|
|
[ 'x-zammad-article-type', Ticket::Article::Type, 'ticket_article_type_id', 'name' ],
|
|
|
|
[ 'x-zammad-article-sender', Ticket::Article::Sender, 'ticket_article_sender_id', 'name' ],
|
|
|
|
]
|
2012-10-05 06:42:12 +00:00
|
|
|
object_lookup( article_attributes, map, mail )
|
2012-05-06 20:48:23 +00:00
|
|
|
|
|
|
|
# create article
|
|
|
|
article = Ticket::Article.create(article_attributes)
|
2012-04-13 16:42:25 +00:00
|
|
|
|
|
|
|
# store mail plain
|
|
|
|
Store.add(
|
|
|
|
:object => 'Ticket::Article::Mail',
|
|
|
|
:o_id => article.id,
|
|
|
|
:data => msg,
|
2012-07-02 12:47:09 +00:00
|
|
|
:filename => "ticket-#{ticket.number}-#{article.id}.eml",
|
2012-04-13 16:42:25 +00:00
|
|
|
:preferences => {}
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
:object => 'Ticket::Article',
|
|
|
|
:o_id => article.id,
|
2012-05-04 11:33:05 +00:00
|
|
|
: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 = {
|
|
|
|
# '0010' => Channel::Filter::Trusted,
|
|
|
|
}
|
|
|
|
|
|
|
|
# filter( channel, mail )
|
|
|
|
filters.each {|prio, backend|
|
|
|
|
begin
|
|
|
|
backend.run( channel, mail, ticket, article, user )
|
|
|
|
rescue Exception => e
|
|
|
|
puts "can't run postmaster post filter #{backend}"
|
|
|
|
puts e.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-05-06 20:48:23 +00:00
|
|
|
# return new objects
|
|
|
|
return ticket, article, user
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|
2012-11-07 16:38:09 +00:00
|
|
|
|
2012-10-05 06:42:12 +00:00
|
|
|
def object_lookup( attributes, map, mail )
|
|
|
|
map.each { |item|
|
|
|
|
if mail[ item[0].to_sym ]
|
2012-11-07 16:38:09 +00:00
|
|
|
new_object = item[1].where( "lower(#{item[3]}) = ?", mail[ item[0].to_sym ].downcase ).first
|
|
|
|
if new_object
|
|
|
|
attributes[ item[2].to_sym ] = new_object.id
|
2012-10-05 06:42:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-05-07 22:37:07 +00:00
|
|
|
def html2ascii(string)
|
|
|
|
|
|
|
|
# find <a href=....> and replace it with [x]
|
|
|
|
link_list = ''
|
|
|
|
counter = 0
|
|
|
|
string.gsub!( /<a\s.*?href=("|')(.+?)("|').*?>/ix ) { |item|
|
|
|
|
link = $2
|
|
|
|
counter = counter + 1
|
|
|
|
link_list += "[#{counter}] #{link}\n"
|
|
|
|
"[#{counter}]"
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove empty lines
|
|
|
|
string.gsub!( /^\s*/m, '' )
|
|
|
|
|
|
|
|
# fix some bad stuff from opera and others
|
2012-06-15 11:10:23 +00:00
|
|
|
string.gsub!( /(\n\r|\r\r\n|\r\n)/, "\n" )
|
|
|
|
|
|
|
|
# strip all other tags
|
|
|
|
string.gsub!( /\<(br|br\/|br\s\/)\>/, "\n" )
|
2012-05-07 22:37:07 +00:00
|
|
|
|
|
|
|
# strip all other tags
|
2012-06-15 11:10:23 +00:00
|
|
|
string.gsub!( /\<.+?\>/, '' )
|
2012-05-07 22:37:07 +00:00
|
|
|
|
|
|
|
# encode html entities like "–"
|
|
|
|
string.gsub!( /(&\#(\d+);?)/x ) { |item|
|
|
|
|
$2.chr
|
|
|
|
}
|
|
|
|
|
|
|
|
# encode html entities like "d;"
|
|
|
|
string.gsub!( /(&\#[xX]([0-9a-fA-F]+);?)/x ) { |item|
|
|
|
|
chr_orig = $1
|
|
|
|
hex = $2.hex
|
|
|
|
if hex
|
|
|
|
chr = hex.chr
|
|
|
|
if chr
|
|
|
|
chr
|
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove empty lines
|
|
|
|
string.gsub!( /^\s*\n\s*\n/m, "\n" )
|
|
|
|
|
|
|
|
# add extracted links
|
|
|
|
if link_list
|
|
|
|
string += "\n\n" + link_list
|
|
|
|
end
|
|
|
|
|
|
|
|
return string
|
|
|
|
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
|
|
|
|
def Encodings.value_decode(str)
|
|
|
|
# Optimization: If there's no encoded-words in the string, just return it
|
|
|
|
return str unless str.index("=?")
|
|
|
|
|
|
|
|
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
|
|
|
|
# text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
|
|
|
|
|
|
|
|
# Search for occurences of quoted strings or plain strings
|
|
|
|
text.scan(/( # Group around entire regex to include it in matches
|
|
|
|
\=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
|
|
|
|
| # or
|
|
|
|
.+?(?=\=\?|$) # Plain String
|
|
|
|
)/xmi).map do |matches|
|
|
|
|
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
|
|
|
|
end.join("")
|
|
|
|
end
|
|
|
|
end
|
2012-04-13 16:42:25 +00:00
|
|
|
end
|