From e9e156b0742c19c9e843b16382c6d74aa4c66e0a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 5 Oct 2012 08:42:12 +0200 Subject: [PATCH] Moved to backend modules for filters. --- app/models/channel/email_parser.rb | 74 +++++++++++++++++---------- app/models/channel/filter/database.rb | 42 +++++++++++++++ app/models/channel/filter/trusted.rb | 16 ++++++ 3 files changed, 104 insertions(+), 28 deletions(-) create mode 100644 app/models/channel/filter/database.rb create mode 100644 app/models/channel/filter/trusted.rb diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 4946afc59..31e801ad4 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -57,7 +57,7 @@ class Channel::EmailParser :x-zammad-owner => 'some_owner_login', # article headers - :x-zammad-article-visability => 'true', + :x-zammad-article-visability => 'internal', :x-zammad-article-type => 'agent', :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 # html_part = message.html_part ? message.html_part.body.decoded : nil data[:attachments] = [] - + # multi part email if mail.multipart? - + # text attachment/body exists if mail.text_part data[:body] = mail.text_part.body.decoded data[:body] = conv( mail.text_part.charset, data[:body] ) - + # html attachment/body may exists and will be converted to text else filename = '-no name-' @@ -221,17 +221,22 @@ class Channel::EmailParser def process(channel, msg) mail = parse( msg ) - # check if trust x-headers - if !channel[:trusted] - mail.each {|key, value| - if key =~ /^x-zammad/i - mail.delete(key) - end - } - end - - # process postmaster filter + # run postmaster pre filter + filters = { + '0010' => Channel::Filter::Trusted, + '1000' => Channel::Filter::Database, + } + # 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 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-owner', User, 'owner_id', 'login' ], ] - map.each { |item| - 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 - } + object_lookup( ticket_attributes, map, mail ) # create ticket 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-sender', Ticket::Article::Sender, 'ticket_article_sender_id', 'name' ], ] - map.each { |item| - 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 - } + object_lookup( article_attributes, map, mail ) # create article article = Ticket::Article.create(article_attributes) @@ -381,10 +374,35 @@ class Channel::EmailParser # execute ticket events 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 ticket, article, user 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) # find and replace it with [x] diff --git a/app/models/channel/filter/database.rb b/app/models/channel/filter/database.rb new file mode 100644 index 000000000..c41631383 --- /dev/null +++ b/app/models/channel/filter/database.rb @@ -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 \ No newline at end of file diff --git a/app/models/channel/filter/trusted.rb b/app/models/channel/filter/trusted.rb new file mode 100644 index 000000000..c0e5114cc --- /dev/null +++ b/app/models/channel/filter/trusted.rb @@ -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 \ No newline at end of file