Moved to backend modules for filters.
This commit is contained in:
parent
2cd8315d67
commit
e9e156b074
3 changed files with 104 additions and 28 deletions
|
@ -57,7 +57,7 @@ class Channel::EmailParser
|
||||||
:x-zammad-owner => 'some_owner_login',
|
:x-zammad-owner => 'some_owner_login',
|
||||||
|
|
||||||
# article headers
|
# article headers
|
||||||
:x-zammad-article-visability => 'true',
|
:x-zammad-article-visability => 'internal',
|
||||||
:x-zammad-article-type => 'agent',
|
:x-zammad-article-type => 'agent',
|
||||||
:x-zammad-article-sender => 'customer',
|
:x-zammad-article-sender => 'customer',
|
||||||
|
|
||||||
|
@ -93,15 +93,15 @@ class Channel::EmailParser
|
||||||
# plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
|
# 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
|
# html_part = message.html_part ? message.html_part.body.decoded : nil
|
||||||
data[:attachments] = []
|
data[:attachments] = []
|
||||||
|
|
||||||
# multi part email
|
# multi part email
|
||||||
if mail.multipart?
|
if mail.multipart?
|
||||||
|
|
||||||
# text attachment/body exists
|
# text attachment/body exists
|
||||||
if mail.text_part
|
if mail.text_part
|
||||||
data[:body] = mail.text_part.body.decoded
|
data[:body] = mail.text_part.body.decoded
|
||||||
data[:body] = conv( mail.text_part.charset, data[:body] )
|
data[:body] = conv( mail.text_part.charset, data[:body] )
|
||||||
|
|
||||||
# html attachment/body may exists and will be converted to text
|
# html attachment/body may exists and will be converted to text
|
||||||
else
|
else
|
||||||
filename = '-no name-'
|
filename = '-no name-'
|
||||||
|
@ -221,17 +221,22 @@ class Channel::EmailParser
|
||||||
def process(channel, msg)
|
def process(channel, msg)
|
||||||
mail = parse( msg )
|
mail = parse( msg )
|
||||||
|
|
||||||
# check if trust x-headers
|
# run postmaster pre filter
|
||||||
if !channel[:trusted]
|
filters = {
|
||||||
mail.each {|key, value|
|
'0010' => Channel::Filter::Trusted,
|
||||||
if key =~ /^x-zammad/i
|
'1000' => Channel::Filter::Database,
|
||||||
mail.delete(key)
|
}
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# process postmaster filter
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
# check ignore header
|
# check ignore header
|
||||||
return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
|
return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
|
||||||
|
@ -306,13 +311,7 @@ class Channel::EmailParser
|
||||||
[ 'x-zammad-priority', Ticket::Priority, 'ticket_priority_id', 'name' ],
|
[ 'x-zammad-priority', Ticket::Priority, 'ticket_priority_id', 'name' ],
|
||||||
[ 'x-zammad-owner', User, 'owner_id', 'login' ],
|
[ 'x-zammad-owner', User, 'owner_id', 'login' ],
|
||||||
]
|
]
|
||||||
map.each { |item|
|
object_lookup( ticket_attributes, map, mail )
|
||||||
if mail[ item[0].to_sym ]
|
|
||||||
if item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first
|
|
||||||
ticket_attributes[ item[2].to_sym ] = item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first.id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
# create ticket
|
# create ticket
|
||||||
ticket = Ticket.create( ticket_attributes )
|
ticket = Ticket.create( ticket_attributes )
|
||||||
|
@ -344,13 +343,7 @@ class Channel::EmailParser
|
||||||
[ 'x-zammad-article-type', Ticket::Article::Type, 'ticket_article_type_id', 'name' ],
|
[ 'x-zammad-article-type', Ticket::Article::Type, 'ticket_article_type_id', 'name' ],
|
||||||
[ 'x-zammad-article-sender', Ticket::Article::Sender, 'ticket_article_sender_id', 'name' ],
|
[ 'x-zammad-article-sender', Ticket::Article::Sender, 'ticket_article_sender_id', 'name' ],
|
||||||
]
|
]
|
||||||
map.each { |item|
|
object_lookup( article_attributes, map, mail )
|
||||||
if mail[ item[0].to_sym ]
|
|
||||||
if item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first
|
|
||||||
article_attributes[ item[2].to_sym ] = item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first.id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
# create article
|
# create article
|
||||||
article = Ticket::Article.create(article_attributes)
|
article = Ticket::Article.create(article_attributes)
|
||||||
|
@ -381,10 +374,35 @@ class Channel::EmailParser
|
||||||
# execute ticket events
|
# execute ticket events
|
||||||
Ticket::Observer::Notification.transaction
|
Ticket::Observer::Notification.transaction
|
||||||
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
||||||
# return new objects
|
# return new objects
|
||||||
return ticket, article, user
|
return ticket, article, user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def object_lookup( attributes, map, mail )
|
||||||
|
map.each { |item|
|
||||||
|
if mail[ item[0].to_sym ]
|
||||||
|
if item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first
|
||||||
|
attributes[ item[2].to_sym ] = item[1].where( item[3].to_sym => mail[ item[0].to_sym ] ).first.id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def html2ascii(string)
|
def html2ascii(string)
|
||||||
|
|
||||||
# find <a href=....> and replace it with [x]
|
# find <a href=....> and replace it with [x]
|
||||||
|
|
42
app/models/channel/filter/database.rb
Normal file
42
app/models/channel/filter/database.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
# process all database filter
|
||||||
|
module Channel::Filter::Database
|
||||||
|
|
||||||
|
def self.run( channel, mail )
|
||||||
|
|
||||||
|
# process postmaster filter
|
||||||
|
filters = [
|
||||||
|
{
|
||||||
|
:name => 'some name',
|
||||||
|
:match => {
|
||||||
|
'from' => 'martin',
|
||||||
|
},
|
||||||
|
:set => {
|
||||||
|
'x-zammad-priority' => '3 high',
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
filters.each {|filter|
|
||||||
|
match = true
|
||||||
|
filter[:match].each {|key, value|
|
||||||
|
begin
|
||||||
|
if match && mail[ key.to_sym ].scan(/#{value}/i)
|
||||||
|
match = true
|
||||||
|
else
|
||||||
|
match = false
|
||||||
|
end
|
||||||
|
rescue Exception => e
|
||||||
|
match = false
|
||||||
|
puts "can't use match rule #{value} on #{mail[ key.to_sym ]}"
|
||||||
|
puts e.inspect
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if match
|
||||||
|
filter[:set].each {|key, value|
|
||||||
|
mail[ key.to_sym ] = value
|
||||||
|
}
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
16
app/models/channel/filter/trusted.rb
Normal file
16
app/models/channel/filter/trusted.rb
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# delete all X-Zammad header if channel is not trusted
|
||||||
|
module Channel::Filter::Trusted
|
||||||
|
|
||||||
|
def self.run( channel, mail )
|
||||||
|
|
||||||
|
# check if trust x-headers
|
||||||
|
if !channel[:trusted]
|
||||||
|
mail.each {|key, value|
|
||||||
|
if key =~ /^x-zammad/i
|
||||||
|
mail.delete(key)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue