email_parser.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. # encoding: utf-8
  3. require 'mail'
  4. require 'encode'
  5. class Channel::EmailParser
  6. =begin
  7. mail = parse( msg_as_string )
  8. mail = {
  9. :from => 'Some Name <some@example.com>',
  10. :from_email => 'some@example.com',
  11. :from_local => 'some',
  12. :from_domain => 'example.com',
  13. :from_display_name => 'Some Name',
  14. :message_id => 'some_message_id@example.com',
  15. :to => 'Some System <system@example.com>',
  16. :cc => 'Somebody <somebody@example.com>',
  17. :subject => 'some message subject',
  18. :body => 'some message body',
  19. :attachments => [
  20. {
  21. :data => 'binary of attachment',
  22. :filename => 'file_name_of_attachment.txt',
  23. :preferences => {
  24. :content-alternative => true,
  25. :Mime-Type => 'text/plain',
  26. :Charset => 'iso-8859-1',
  27. },
  28. },
  29. ],
  30. # ignore email header
  31. :x-zammad-ignore => 'false',
  32. # customer headers
  33. :x-zammad-customer-login => '',
  34. :x-zammad-customer-email => '',
  35. :x-zammad-customer-firstname => '',
  36. :x-zammad-customer-lastname => '',
  37. # ticket headers
  38. :x-zammad-group => 'some_group',
  39. :x-zammad-state => 'some_state',
  40. :x-zammad-priority => 'some_priority',
  41. :x-zammad-owner => 'some_owner_login',
  42. # article headers
  43. :x-zammad-article-visibility => 'internal',
  44. :x-zammad-article-type => 'agent',
  45. :x-zammad-article-sender => 'customer',
  46. # all other email headers
  47. :some-header => 'some_value',
  48. }
  49. =end
  50. def parse (msg)
  51. data = {}
  52. mail = Mail.new( msg )
  53. # set all headers
  54. mail.header.fields.each { |field|
  55. data[field.name.to_s.downcase.to_sym] = Encode.conv( 'utf8', field.to_s )
  56. }
  57. # get sender
  58. from = nil
  59. ['from', 'reply-to', 'return-path'].each { |item|
  60. if !from
  61. if mail[ item.to_sym ]
  62. from = mail[ item.to_sym ].value
  63. end
  64. end
  65. }
  66. # set extra headers
  67. begin
  68. data[:from_email] = Mail::Address.new( from ).address
  69. data[:from_local] = Mail::Address.new( from ).local
  70. data[:from_domain] = Mail::Address.new( from ).domain
  71. data[:from_display_name] = Mail::Address.new( from ).display_name ||
  72. ( Mail::Address.new( from ).comments && Mail::Address.new( from ).comments[0] )
  73. rescue
  74. data[:from_email] = from
  75. data[:from_local] = from
  76. data[:from_domain] = from
  77. end
  78. # do extra decoding because we needed to use field.value
  79. data[:from_display_name] = Mail::Field.new( 'X-From', data[:from_display_name] ).to_s
  80. # compat headers
  81. data[:message_id] = data['message-id'.to_sym]
  82. # body
  83. # plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
  84. # html_part = message.html_part ? message.html_part.body.decoded : nil
  85. data[:attachments] = []
  86. # multi part email
  87. if mail.multipart?
  88. # text attachment/body exists
  89. if mail.text_part
  90. data[:body] = mail.text_part.body.decoded
  91. data[:body] = Encode.conv( mail.text_part.charset, data[:body] )
  92. # html attachment/body may exists and will be converted to text
  93. else
  94. filename = '-no name-'
  95. if mail.html_part.body
  96. filename = 'html-email'
  97. data[:body] = mail.html_part.body.to_s
  98. data[:body] = Encode.conv( mail.html_part.charset.to_s, data[:body] )
  99. data[:body] = html2ascii( data[:body] )
  100. # any other attachments
  101. else
  102. data[:body] = 'no visible content'
  103. end
  104. end
  105. # add html attachment/body as real attachment
  106. if mail.html_part
  107. filename = 'message.html'
  108. headers_store = {
  109. 'content-alternative' => true,
  110. }
  111. if mail.mime_type
  112. headers_store['Mime-Type'] = mail.html_part.mime_type
  113. end
  114. if mail.charset
  115. headers_store['Charset'] = mail.html_part.charset
  116. end
  117. attachment = {
  118. :data => mail.html_part.body.to_s,
  119. :filename => mail.html_part.filename || filename,
  120. :preferences => headers_store
  121. }
  122. data[:attachments].push attachment
  123. end
  124. # get attachments
  125. if mail.parts
  126. attachment_count_total = 0
  127. mail.parts.each { |part|
  128. attachment_count_total += 1
  129. # protect process to work fine with spam emails, see test/fixtures/mail15.box
  130. begin
  131. if mail.text_part && mail.text_part == part
  132. # ignore text/plain attachments - already shown in view
  133. elsif mail.html_part && mail.html_part == part
  134. # ignore text/html - html part, already shown in view
  135. else
  136. attachs = self._get_attachment( part, data[:attachments] )
  137. data[:attachments].concat( attachs )
  138. end
  139. rescue
  140. attachs = self._get_attachment( part, data[:attachments] )
  141. data[:attachments].concat( attachs )
  142. end
  143. }
  144. end
  145. # not multipart email
  146. else
  147. # text part
  148. if !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.downcase == 'text/plain'
  149. data[:body] = mail.body.decoded
  150. data[:body] = Encode.conv( mail.charset, data[:body] )
  151. # html part
  152. else
  153. filename = '-no name-'
  154. if mail.mime_type.to_s.downcase == 'text/html'
  155. filename = 'html-email'
  156. data[:body] = mail.body.decoded
  157. data[:body] = Encode.conv( mail.charset, data[:body] )
  158. data[:body] = html2ascii( data[:body] )
  159. # any other attachments
  160. else
  161. data[:body] = 'no visible content'
  162. end
  163. # add body as attachment
  164. headers_store = {
  165. 'content-alternative' => true,
  166. }
  167. if mail.mime_type
  168. headers_store['Mime-Type'] = mail.mime_type
  169. end
  170. if mail.charset
  171. headers_store['Charset'] = mail.charset
  172. end
  173. attachment = {
  174. :data => mail.body.decoded,
  175. :filename => mail.filename || filename,
  176. :preferences => headers_store
  177. }
  178. data[:attachments].push attachment
  179. end
  180. end
  181. # strip not wanted chars
  182. data[:body].gsub!( /\r\n/, "\n" )
  183. data[:body].gsub!( /\r/, "\n" )
  184. return data
  185. end
  186. def _get_attachment( file, attachments )
  187. # check if sub parts are available
  188. if !file.parts.empty?
  189. a = []
  190. file.parts.each {|p|
  191. a.concat( self._get_attachment( p, attachments ) )
  192. }
  193. return a
  194. end
  195. # get file preferences
  196. headers_store = {}
  197. file.header.fields.each { |field|
  198. headers_store[field.name.to_s] = field.value.to_s
  199. }
  200. # get filename from content-disposition
  201. filename = nil
  202. # workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
  203. begin
  204. filename = file.header[:content_disposition].filename
  205. rescue
  206. result = file.header[:content_disposition].to_s.scan( /filename=("|)(.+?)("|);/i )
  207. if result && result[0] && result[0][1]
  208. filename = result[0][1]
  209. end
  210. end
  211. # for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
  212. if !filename
  213. filename = file.header[:content_location].to_s
  214. end
  215. # generate file name
  216. if !filename || filename.empty?
  217. attachment_count = 0
  218. (1..1000).each {|count|
  219. filename_exists = false
  220. filename = 'file-' + count.to_s
  221. attachments.each {|attachment|
  222. if attachment[:filename] == filename
  223. filename_exists = true
  224. end
  225. }
  226. break if filename_exists == false
  227. }
  228. end
  229. # get mime type
  230. if file.header[:content_type] && file.header[:content_type].string
  231. headers_store['Mime-Type'] = file.header[:content_type].string
  232. end
  233. # get charset
  234. if file.header && file.header.charset
  235. headers_store['Charset'] = file.header.charset
  236. end
  237. # remove not needed header
  238. headers_store.delete('Content-Transfer-Encoding')
  239. headers_store.delete('Content-Disposition')
  240. attach = {
  241. :data => file.body.to_s,
  242. :filename => filename,
  243. :preferences => headers_store,
  244. }
  245. return [attach]
  246. end
  247. def process(channel, msg)
  248. mail = parse( msg )
  249. # run postmaster pre filter
  250. filters = {
  251. '0010' => Channel::Filter::Trusted,
  252. '1000' => Channel::Filter::Database,
  253. }
  254. # filter( channel, mail )
  255. filters.each {|prio, backend|
  256. begin
  257. backend.run( channel, mail )
  258. rescue Exception => e
  259. puts "can't run postmaster pre filter #{backend}"
  260. puts e.inspect
  261. return false
  262. end
  263. }
  264. # check ignore header
  265. return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
  266. ticket = nil
  267. article = nil
  268. user = nil
  269. # use transaction
  270. ActiveRecord::Base.transaction do
  271. # reset current_user
  272. UserInfo.current_user_id = 1
  273. if mail[ 'x-zammad-customer-login'.to_sym ]
  274. user = User.where( :login => mail[ 'x-zammad-customer-login'.to_sym ] ).first
  275. end
  276. if !user
  277. user = User.where( :email => mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email] ).first
  278. end
  279. if !user
  280. puts 'create user...'
  281. roles = Role.where( :name => 'Customer' )
  282. user = User.create(
  283. :login => mail[ 'x-zammad-customer-login'.to_sym ] || mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
  284. :firstname => mail[ 'x-zammad-customer-firstname'.to_sym ] || mail[:from_display_name],
  285. :lastname => mail[ 'x-zammad-customer-lastname'.to_sym ],
  286. :email => mail[ 'x-zammad-customer-email'.to_sym ] || mail[:from_email],
  287. :password => '',
  288. :active => true,
  289. :roles => roles,
  290. :updated_by_id => 1,
  291. :created_by_id => 1,
  292. )
  293. end
  294. # set current user
  295. UserInfo.current_user_id = user.id
  296. # get ticket# from subject
  297. ticket = Ticket::Number.check( mail[:subject] )
  298. # set ticket state to open if not new
  299. if ticket
  300. ticket_state = Ticket::State.find( ticket.ticket_state_id )
  301. ticket_state_type = Ticket::StateType.find( ticket_state.state_type_id )
  302. # if tickte is merged, find linked ticket
  303. if ticket_state_type.name == 'merged'
  304. end
  305. if ticket_state_type.name != 'new'
  306. ticket.ticket_state = Ticket::State.where( :name => 'open' ).first
  307. ticket.save
  308. end
  309. end
  310. # create new ticket
  311. if !ticket
  312. # set attributes
  313. ticket_attributes = {
  314. :group_id => channel[:group_id] || 1,
  315. :customer_id => user.id,
  316. :title => mail[:subject] || '',
  317. :ticket_state_id => Ticket::State.where( :name => 'new' ).first.id,
  318. :ticket_priority_id => Ticket::Priority.where( :name => '2 normal' ).first.id,
  319. }
  320. # x-headers lookup
  321. map = [
  322. [ 'x-zammad-group', Group, 'group_id', 'name' ],
  323. [ 'x-zammad-state', Ticket::State, 'ticket_state_id', 'name' ],
  324. [ 'x-zammad-priority', Ticket::Priority, 'ticket_priority_id', 'name' ],
  325. [ 'x-zammad-owner', User, 'owner_id', 'login' ],
  326. ]
  327. object_lookup( ticket_attributes, map, mail )
  328. # create ticket
  329. ticket = Ticket.create( ticket_attributes )
  330. end
  331. # import mail
  332. # set attributes
  333. internal = false
  334. if mail[ 'X-Zammad-Article-Visibility'.to_sym ] && mail[ 'X-Zammad-Article-Visibility'.to_sym ] == 'internal'
  335. internal = true
  336. end
  337. article_attributes = {
  338. :ticket_id => ticket.id,
  339. :ticket_article_type_id => Ticket::Article::Type.where( :name => 'email' ).first.id,
  340. :ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Customer' ).first.id,
  341. :body => mail[:body],
  342. :from => mail[:from],
  343. :to => mail[:to],
  344. :cc => mail[:cc],
  345. :subject => mail[:subject],
  346. :message_id => mail[:message_id],
  347. :internal => internal,
  348. }
  349. # x-headers lookup
  350. map = [
  351. [ 'x-zammad-article-type', Ticket::Article::Type, 'ticket_article_type_id', 'name' ],
  352. [ 'x-zammad-article-sender', Ticket::Article::Sender, 'ticket_article_sender_id', 'name' ],
  353. ]
  354. object_lookup( article_attributes, map, mail )
  355. # create article
  356. article = Ticket::Article.create(article_attributes)
  357. # store mail plain
  358. Store.add(
  359. :object => 'Ticket::Article::Mail',
  360. :o_id => article.id,
  361. :data => msg,
  362. :filename => "ticket-#{ticket.number}-#{article.id}.eml",
  363. :preferences => {}
  364. )
  365. # store attachments
  366. if mail[:attachments]
  367. mail[:attachments].each do |attachment|
  368. Store.add(
  369. :object => 'Ticket::Article',
  370. :o_id => article.id,
  371. :data => attachment[:data],
  372. :filename => attachment[:filename],
  373. :preferences => attachment[:preferences]
  374. )
  375. end
  376. end
  377. end
  378. # execute ticket events
  379. Observer::Ticket::Notification.transaction
  380. # run postmaster post filter
  381. filters = {
  382. # '0010' => Channel::Filter::Trusted,
  383. }
  384. # filter( channel, mail )
  385. filters.each {|prio, backend|
  386. begin
  387. backend.run( channel, mail, ticket, article, user )
  388. rescue Exception => e
  389. puts "can't run postmaster post filter #{backend}"
  390. puts e.inspect
  391. end
  392. }
  393. # return new objects
  394. return ticket, article, user
  395. end
  396. def object_lookup( attributes, map, mail )
  397. map.each { |item|
  398. if mail[ item[0].to_sym ]
  399. new_object = item[1].where( "lower(#{item[3]}) = ?", mail[ item[0].to_sym ].downcase ).first
  400. if new_object
  401. attributes[ item[2].to_sym ] = new_object.id
  402. end
  403. end
  404. }
  405. end
  406. def html2ascii(string)
  407. # find <a href=....> and replace it with [x]
  408. link_list = ''
  409. counter = 0
  410. string.gsub!( /<a\s.*?href=("|')(.+?)("|').*?>/ix ) { |item|
  411. link = $2
  412. counter = counter + 1
  413. link_list += "[#{counter}] #{link}\n"
  414. "[#{counter}]"
  415. }
  416. # remove empty lines
  417. string.gsub!( /^\s*/m, '' )
  418. # fix some bad stuff from opera and others
  419. string.gsub!( /(\n\r|\r\r\n|\r\n)/, "\n" )
  420. # strip all other tags
  421. string.gsub!( /\<(br|br\/|br\s\/)\>/, "\n" )
  422. # strip all other tags
  423. string.gsub!( /\<.+?\>/, '' )
  424. # encode html entities like "&#8211;"
  425. string.gsub!( /(&\#(\d+);?)/x ) { |item|
  426. $2.chr
  427. }
  428. # encode html entities like "&#3d;"
  429. string.gsub!( /(&\#[xX]([0-9a-fA-F]+);?)/x ) { |item|
  430. chr_orig = $1
  431. hex = $2.hex
  432. if hex
  433. chr = hex.chr
  434. if chr
  435. chr
  436. else
  437. chr_orig
  438. end
  439. else
  440. chr_orig
  441. end
  442. }
  443. # remove empty lines
  444. string.gsub!( /^\s*\n\s*\n/m, "\n" )
  445. # add extracted links
  446. if link_list
  447. string += "\n\n" + link_list
  448. end
  449. return string
  450. end
  451. end
  452. # workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)
  453. module Mail
  454. module Encodings
  455. def Encodings.value_decode(str)
  456. # Optimization: If there's no encoded-words in the string, just return it
  457. return str unless str.index("=?")
  458. str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  459. # Split on white-space boundaries with capture, so we capture the white-space as well
  460. str.split(/([ \t])/).map do |text|
  461. if text.index('=?') .nil?
  462. text
  463. else
  464. # Join QP encoded-words that are adjacent to avoid decoding partial chars
  465. # text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
  466. # Search for occurences of quoted strings or plain strings
  467. text.scan(/( # Group around entire regex to include it in matches
  468. \=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
  469. | # or
  470. .+?(?=\=\?|$) # Plain String
  471. )/xmi).map do |matches|
  472. string, method = *matches
  473. if method == 'b' || method == 'B'
  474. b_value_decode(string)
  475. elsif method == 'q' || method == 'Q'
  476. q_value_decode(string)
  477. else
  478. string
  479. end
  480. end
  481. end
  482. end.join("")
  483. end
  484. end
  485. end