email_parser.rb 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. # encoding: utf-8
  3. class Channel::EmailParser
  4. =begin
  5. parser = Channel::EmailParser.new
  6. mail = parser.parse(msg_as_string)
  7. mail = {
  8. from: 'Some Name <some@example.com>',
  9. from_email: 'some@example.com',
  10. from_local: 'some',
  11. from_domain: 'example.com',
  12. from_display_name: 'Some Name',
  13. message_id: 'some_message_id@example.com',
  14. to: 'Some System <system@example.com>',
  15. cc: 'Somebody <somebody@example.com>',
  16. subject: 'some message subject',
  17. body: 'some message body',
  18. content_type: 'text/html', # text/plain
  19. date: Time.zone.now,
  20. attachments: [
  21. {
  22. data: 'binary of attachment',
  23. filename: 'file_name_of_attachment.txt',
  24. preferences: {
  25. 'content-alternative' => true,
  26. 'Mime-Type' => 'text/plain',
  27. 'Charset: => 'iso-8859-1',
  28. },
  29. },
  30. ],
  31. # ignore email header
  32. x-zammad-ignore: 'false',
  33. # customer headers
  34. x-zammad-customer-login: '',
  35. x-zammad-customer-email: '',
  36. x-zammad-customer-firstname: '',
  37. x-zammad-customer-lastname: '',
  38. # ticket headers (for new tickets)
  39. x-zammad-ticket-group: 'some_group',
  40. x-zammad-ticket-state: 'some_state',
  41. x-zammad-ticket-priority: 'some_priority',
  42. x-zammad-ticket-owner: 'some_owner_login',
  43. # ticket headers (for existing tickets)
  44. x-zammad-ticket-followup-group: 'some_group',
  45. x-zammad-ticket-followup-state: 'some_state',
  46. x-zammad-ticket-followup-priority: 'some_priority',
  47. x-zammad-ticket-followup-owner: 'some_owner_login',
  48. # article headers
  49. x-zammad-article-internal: false,
  50. x-zammad-article-type: 'agent',
  51. x-zammad-article-sender: 'customer',
  52. # all other email headers
  53. some-header: 'some_value',
  54. }
  55. =end
  56. def parse(msg)
  57. data = {}
  58. mail = Mail.new(msg)
  59. # set all headers
  60. mail.header.fields.each do |field|
  61. # full line, encode, ready for storage
  62. begin
  63. value = Encode.conv('utf8', field.to_s)
  64. if value.blank?
  65. value = field.raw_value
  66. end
  67. data[field.name.to_s.downcase.to_sym] = value
  68. rescue => e
  69. data[field.name.to_s.downcase.to_sym] = field.raw_value
  70. end
  71. # if we need to access the lines by objects later again
  72. data["raw-#{field.name.downcase}".to_sym] = field
  73. end
  74. # verify content, ignore recipients with non email address
  75. ['to', 'cc', 'delivered-to', 'x-original-to', 'envelope-to'].each do |field|
  76. next if data[field.to_sym].blank?
  77. next if data[field.to_sym].match?(/@/)
  78. data[field.to_sym] = ''
  79. end
  80. # get sender with @ / email address
  81. from = nil
  82. ['from', 'reply-to', 'return-path'].each do |item|
  83. next if data[item.to_sym].blank?
  84. next if data[item.to_sym] !~ /@/
  85. from = data[item.to_sym]
  86. break if from
  87. end
  88. # in case of no sender with email address - get sender
  89. if !from
  90. ['from', 'reply-to', 'return-path'].each do |item|
  91. next if data[item.to_sym].blank?
  92. from = data[item.to_sym]
  93. break if from
  94. end
  95. end
  96. # set x-any-recipient
  97. data['x-any-recipient'.to_sym] = ''
  98. ['to', 'cc', 'delivered-to', 'x-original-to', 'envelope-to'].each do |item|
  99. next if data[item.to_sym].blank?
  100. if data['x-any-recipient'.to_sym] != ''
  101. data['x-any-recipient'.to_sym] += ', '
  102. end
  103. data['x-any-recipient'.to_sym] += mail[item.to_sym].to_s
  104. end
  105. # set extra headers
  106. data = data.merge(Channel::EmailParser.sender_properties(from))
  107. # do extra encoding (see issue#1045)
  108. if data[:subject].present?
  109. data[:subject].sub!(/^=\?us-ascii\?Q\?(.+)\?=$/, '\1')
  110. end
  111. # compat headers
  112. data[:message_id] = data['message-id'.to_sym]
  113. # body
  114. # plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
  115. # html_part = message.html_part ? message.html_part.body.decoded : nil
  116. data[:attachments] = []
  117. # multi part email
  118. if mail.multipart?
  119. # html attachment/body may exists and will be converted to strict html
  120. if mail.html_part&.body
  121. data[:body] = mail.html_part.body.to_s
  122. data[:body] = Encode.conv(mail.html_part.charset.to_s, data[:body])
  123. data[:body] = data[:body].html2html_strict.to_s.force_encoding('utf-8')
  124. if !data[:body].force_encoding('UTF-8').valid_encoding?
  125. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  126. end
  127. data[:content_type] = 'text/html'
  128. end
  129. # text attachment/body exists
  130. if data[:body].blank? && mail.text_part
  131. data[:body] = mail.text_part.body.decoded
  132. data[:body] = Encode.conv(mail.text_part.charset, data[:body])
  133. data[:body] = data[:body].to_s.force_encoding('utf-8')
  134. if !data[:body].valid_encoding?
  135. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  136. end
  137. data[:content_type] = 'text/plain'
  138. end
  139. # any other attachments
  140. if data[:body].blank?
  141. data[:body] = 'no visible content'
  142. data[:content_type] = 'text/plain'
  143. end
  144. # add html attachment/body as real attachment
  145. if mail.html_part
  146. filename = 'message.html'
  147. headers_store = {
  148. 'content-alternative' => true,
  149. 'original-format' => true,
  150. }
  151. if mail.mime_type
  152. headers_store['Mime-Type'] = mail.html_part.mime_type
  153. end
  154. if mail.charset
  155. headers_store['Charset'] = mail.html_part.charset
  156. end
  157. attachment = {
  158. data: mail.html_part.body.to_s,
  159. filename: mail.html_part.filename || filename,
  160. preferences: headers_store
  161. }
  162. data[:attachments].push attachment
  163. end
  164. # get attachments
  165. mail.parts&.each do |part|
  166. # protect process to work fine with spam emails, see test/fixtures/mail15.box
  167. begin
  168. attachs = _get_attachment(part, data[:attachments], mail)
  169. data[:attachments].concat(attachs)
  170. rescue
  171. attachs = _get_attachment(part, data[:attachments], mail)
  172. data[:attachments].concat(attachs)
  173. end
  174. end
  175. # not multipart email
  176. # html part only, convert to text and add it as attachment
  177. elsif mail.mime_type && mail.mime_type.to_s.casecmp('text/html').zero?
  178. filename = 'message.html'
  179. data[:body] = mail.body.decoded
  180. data[:body] = Encode.conv(mail.charset, data[:body])
  181. data[:body] = data[:body].html2html_strict.to_s.force_encoding('utf-8')
  182. if !data[:body].valid_encoding?
  183. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  184. end
  185. data[:content_type] = 'text/html'
  186. # add body as attachment
  187. headers_store = {
  188. 'content-alternative' => true,
  189. 'original-format' => true,
  190. }
  191. if mail.mime_type
  192. headers_store['Mime-Type'] = mail.mime_type
  193. end
  194. if mail.charset
  195. headers_store['Charset'] = mail.charset
  196. end
  197. attachment = {
  198. data: mail.body.decoded,
  199. filename: mail.filename || filename,
  200. preferences: headers_store
  201. }
  202. data[:attachments].push attachment
  203. # text part only
  204. elsif !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.casecmp('text/plain').zero?
  205. data[:body] = mail.body.decoded
  206. data[:body] = Encode.conv(mail.charset, data[:body])
  207. if !data[:body].force_encoding('UTF-8').valid_encoding?
  208. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  209. end
  210. data[:content_type] = 'text/plain'
  211. else
  212. filename = '-no name-'
  213. data[:body] = 'no visible content'
  214. data[:content_type] = 'text/plain'
  215. # add body as attachment
  216. headers_store = {
  217. 'content-alternative' => true,
  218. }
  219. if mail.mime_type
  220. headers_store['Mime-Type'] = mail.mime_type
  221. end
  222. if mail.charset
  223. headers_store['Charset'] = mail.charset
  224. end
  225. attachment = {
  226. data: mail.body.decoded,
  227. filename: mail.filename || filename,
  228. preferences: headers_store
  229. }
  230. data[:attachments].push attachment
  231. end
  232. # strip not wanted chars
  233. data[:body].gsub!(/\n\r/, "\n")
  234. data[:body].gsub!(/\r\n/, "\n")
  235. data[:body].tr!("\r", "\n")
  236. # get mail date
  237. begin
  238. if mail.date
  239. data[:date] = Time.zone.parse(mail.date.to_s)
  240. end
  241. rescue
  242. data[:date] = nil
  243. end
  244. # remember original mail instance
  245. data[:mail_instance] = mail
  246. data
  247. end
  248. def _get_attachment(file, attachments, mail)
  249. # check if sub parts are available
  250. if file.parts.present?
  251. list = []
  252. file.parts.each do |p|
  253. attachment = _get_attachment(p, attachments, mail)
  254. list.concat(attachment)
  255. end
  256. return list
  257. end
  258. # ignore text/plain attachments - already shown in view
  259. return [] if mail.text_part&.body.to_s == file.body.to_s
  260. # ignore text/html - html part, already shown in view
  261. return [] if mail.html_part&.body.to_s == file.body.to_s
  262. # get file preferences
  263. headers_store = {}
  264. file.header.fields.each do |field|
  265. # full line, encode, ready for storage
  266. begin
  267. value = Encode.conv('utf8', field.to_s)
  268. if value.blank?
  269. value = field.raw_value
  270. end
  271. headers_store[field.name.to_s] = value
  272. rescue => e
  273. headers_store[field.name.to_s] = field.raw_value
  274. end
  275. end
  276. # cleanup content id, <> will be added automatically later
  277. if headers_store['Content-ID']
  278. headers_store['Content-ID'].gsub!(/^</, '')
  279. headers_store['Content-ID'].gsub!(/>$/, '')
  280. end
  281. # get filename from content-disposition
  282. filename = nil
  283. # workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
  284. begin
  285. filename = file.header[:content_disposition].filename
  286. rescue
  287. begin
  288. if file.header[:content_disposition].to_s =~ /filename="(.+?)"/i
  289. filename = $1
  290. elsif file.header[:content_disposition].to_s =~ /filename='(.+?)'/i
  291. filename = $1
  292. elsif file.header[:content_disposition].to_s =~ /filename=(.+?);/i
  293. filename = $1
  294. end
  295. rescue
  296. Rails.logger.debug { 'Unable to get filename' }
  297. end
  298. end
  299. # as fallback, use raw values
  300. if filename.blank?
  301. if headers_store['Content-Disposition'].to_s =~ /filename="(.+?)"/i
  302. filename = $1
  303. elsif headers_store['Content-Disposition'].to_s =~ /filename='(.+?)'/i
  304. filename = $1
  305. elsif headers_store['Content-Disposition'].to_s =~ /filename=(.+?);/i
  306. filename = $1
  307. end
  308. end
  309. # for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
  310. filename ||= file.header[:content_location].to_s
  311. # generate file name based on content-id
  312. if filename.blank? && headers_store['Content-ID'].present?
  313. if headers_store['Content-ID'] =~ /(.+?)@.+?/i
  314. filename = $1
  315. end
  316. end
  317. # generate file name based on content type
  318. if filename.blank? && headers_store['Content-Type'].present?
  319. if headers_store['Content-Type'].match?(%r{^message/rfc822}i)
  320. begin
  321. parser = Channel::EmailParser.new
  322. mail_local = parser.parse(file.body.to_s)
  323. filename = if mail_local[:subject].present?
  324. "#{mail_local[:subject]}.eml"
  325. elsif headers_store['Content-Description'].present?
  326. "#{headers_store['Content-Description']}.eml".to_s.force_encoding('utf-8')
  327. else
  328. 'Mail.eml'
  329. end
  330. rescue
  331. filename = 'Mail.eml'
  332. end
  333. end
  334. # e. g. Content-Type: video/quicktime; name="Video.MOV";
  335. if filename.blank?
  336. ['name="(.+?)"(;|$)', "name='(.+?)'(;|$)", 'name=(.+?)(;|$)'].each do |regexp|
  337. if headers_store['Content-Type'] =~ /#{regexp}/i
  338. filename = $1
  339. break
  340. end
  341. end
  342. end
  343. # e. g. Content-Type: video/quicktime
  344. if filename.blank?
  345. map = {
  346. 'message/delivery-status': ['txt', 'delivery-status'],
  347. 'text/plain': %w[txt document],
  348. 'text/html': %w[html document],
  349. 'video/quicktime': %w[mov video],
  350. 'image/jpeg': %w[jpg image],
  351. 'image/jpg': %w[jpg image],
  352. 'image/png': %w[png image],
  353. 'image/gif': %w[gif image],
  354. }
  355. map.each do |type, ext|
  356. next if headers_store['Content-Type'] !~ /^#{Regexp.quote(type)}/i
  357. filename = if headers_store['Content-Description'].present?
  358. "#{headers_store['Content-Description']}.#{ext[0]}".to_s.force_encoding('utf-8')
  359. else
  360. "#{ext[1]}.#{ext[0]}"
  361. end
  362. break
  363. end
  364. end
  365. end
  366. if filename.blank?
  367. filename = 'file'
  368. end
  369. attachment_count = 0
  370. local_filename = ''
  371. local_extention = ''
  372. if filename =~ /^(.*?)\.(.+?)$/
  373. local_filename = $1
  374. local_extention = $2
  375. end
  376. (1..1000).each do |count|
  377. filename_exists = false
  378. attachments.each do |attachment|
  379. if attachment[:filename] == filename
  380. filename_exists = true
  381. end
  382. end
  383. break if filename_exists == false
  384. filename = if local_extention.present?
  385. "#{local_filename}#{count}.#{local_extention}"
  386. else
  387. "#{local_filename}#{count}"
  388. end
  389. end
  390. # get mime type
  391. if file.header[:content_type]&.string
  392. headers_store['Mime-Type'] = file.header[:content_type].string
  393. end
  394. # get charset
  395. if file.header&.charset
  396. headers_store['Charset'] = file.header.charset
  397. end
  398. # remove not needed header
  399. headers_store.delete('Content-Transfer-Encoding')
  400. headers_store.delete('Content-Disposition')
  401. # workaround for mail gem
  402. # https://github.com/zammad/zammad/issues/928
  403. filename = Mail::Encodings.value_decode(filename)
  404. attach = {
  405. data: file.body.to_s,
  406. filename: filename,
  407. preferences: headers_store,
  408. }
  409. [attach]
  410. end
  411. =begin
  412. parser = Channel::EmailParser.new
  413. ticket, article, user, mail = parser.process(channel, email_raw_string)
  414. returns
  415. [ticket, article, user, mail]
  416. do not raise an exception - e. g. if used by scheduler
  417. parser = Channel::EmailParser.new
  418. ticket, article, user, mail = parser.process(channel, email_raw_string, false)
  419. returns
  420. [ticket, article, user, mail] || false
  421. =end
  422. def process(channel, msg, exception = true)
  423. _process(channel, msg)
  424. rescue => e
  425. # store unprocessable email for bug reporting
  426. path = Rails.root.join('tmp', 'unprocessable_mail')
  427. FileUtils.mkpath path
  428. md5 = Digest::MD5.hexdigest(msg)
  429. filename = "#{path}/#{md5}.eml"
  430. message = "ERROR: Can't process email, you will find it for bug reporting under #{filename}, please create an issue at https://github.com/zammad/zammad/issues"
  431. p message # rubocop:disable Rails/Output
  432. p 'ERROR: ' + e.inspect # rubocop:disable Rails/Output
  433. Rails.logger.error message
  434. Rails.logger.error e
  435. File.open(filename, 'wb') do |file|
  436. file.write msg
  437. end
  438. return false if exception == false
  439. raise e.inspect + "\n" + e.backtrace.join("\n")
  440. end
  441. def _process(channel, msg)
  442. # parse email
  443. mail = parse(msg)
  444. Rails.logger.info "Process email with msgid '#{mail[:message_id]}'"
  445. # run postmaster pre filter
  446. UserInfo.current_user_id = 1
  447. filters = {}
  448. Setting.where(area: 'Postmaster::PreFilter').order(:name).each do |setting|
  449. filters[setting.name] = Kernel.const_get(Setting.get(setting.name))
  450. end
  451. filters.each do |key, backend|
  452. Rails.logger.debug { "run postmaster pre filter #{key}: #{backend}" }
  453. begin
  454. backend.run(channel, mail)
  455. rescue => e
  456. Rails.logger.error "can't run postmaster pre filter #{key}: #{backend}"
  457. Rails.logger.error e.inspect
  458. raise e
  459. end
  460. end
  461. # check ignore header
  462. if mail['x-zammad-ignore'.to_sym] == 'true' || mail['x-zammad-ignore'.to_sym] == true
  463. Rails.logger.info "ignored email with msgid '#{mail[:message_id]}' from '#{mail[:from]}' because of x-zammad-ignore header"
  464. return
  465. end
  466. # set interface handle
  467. original_interface_handle = ApplicationHandleInfo.current
  468. ticket = nil
  469. article = nil
  470. session_user = nil
  471. # use transaction
  472. Transaction.execute(interface_handle: "#{original_interface_handle}.postmaster") do
  473. # get sender user
  474. session_user_id = mail['x-zammad-session-user-id'.to_sym]
  475. if !session_user_id
  476. raise 'No x-zammad-session-user-id, no sender set!'
  477. end
  478. session_user = User.lookup(id: session_user_id)
  479. if !session_user
  480. raise "No user found for x-zammad-session-user-id: #{session_user_id}!"
  481. end
  482. # set current user
  483. UserInfo.current_user_id = session_user.id
  484. # get ticket# based on email headers
  485. if mail['x-zammad-ticket-id'.to_sym]
  486. ticket = Ticket.find_by(id: mail['x-zammad-ticket-id'.to_sym])
  487. end
  488. if mail['x-zammad-ticket-number'.to_sym]
  489. ticket = Ticket.find_by(number: mail['x-zammad-ticket-number'.to_sym])
  490. end
  491. # set ticket state to open if not new
  492. if ticket
  493. set_attributes_by_x_headers(ticket, 'ticket', mail, 'followup')
  494. # save changes set by x-zammad-ticket-followup-* headers
  495. ticket.save! if ticket.has_changes_to_save?
  496. state = Ticket::State.find(ticket.state_id)
  497. state_type = Ticket::StateType.find(state.state_type_id)
  498. # set ticket to open again or keep create state
  499. if !mail['x-zammad-ticket-followup-state'.to_sym] && !mail['x-zammad-ticket-followup-state_id'.to_sym]
  500. new_state = Ticket::State.find_by(default_create: true)
  501. if ticket.state_id != new_state.id && !mail['x-zammad-out-of-office'.to_sym]
  502. ticket.state = Ticket::State.find_by(default_follow_up: true)
  503. ticket.save!
  504. end
  505. end
  506. end
  507. # create new ticket
  508. if !ticket
  509. preferences = {}
  510. if channel[:id]
  511. preferences = {
  512. channel_id: channel[:id]
  513. }
  514. end
  515. # get default group where ticket is created
  516. group = nil
  517. if channel[:group_id]
  518. group = Group.lookup(id: channel[:group_id])
  519. end
  520. if group.blank? || group.active == false
  521. group = Group.where(active: true).order('id ASC').first
  522. end
  523. if group.blank?
  524. group = Group.first
  525. end
  526. title = mail[:subject]
  527. if title.blank?
  528. title = '-'
  529. end
  530. ticket = Ticket.new(
  531. group_id: group.id,
  532. title: title,
  533. preferences: preferences,
  534. )
  535. set_attributes_by_x_headers(ticket, 'ticket', mail)
  536. # create ticket
  537. ticket.save!
  538. end
  539. # set attributes
  540. ticket.with_lock do
  541. article = Ticket::Article.new(
  542. ticket_id: ticket.id,
  543. type_id: Ticket::Article::Type.find_by(name: 'email').id,
  544. sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
  545. content_type: mail[:content_type],
  546. body: mail[:body],
  547. from: mail[:from],
  548. reply_to: mail[:"reply-to"],
  549. to: mail[:to],
  550. cc: mail[:cc],
  551. subject: mail[:subject],
  552. message_id: mail[:message_id],
  553. internal: false,
  554. )
  555. # x-headers lookup
  556. set_attributes_by_x_headers(article, 'article', mail)
  557. # create article
  558. article.save!
  559. # store mail plain
  560. article.save_as_raw(msg)
  561. # store attachments
  562. mail[:attachments]&.each do |attachment|
  563. filename = attachment[:filename].force_encoding('utf-8')
  564. if !filename.force_encoding('UTF-8').valid_encoding?
  565. filename = filename.encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  566. end
  567. Store.add(
  568. object: 'Ticket::Article',
  569. o_id: article.id,
  570. data: attachment[:data],
  571. filename: filename,
  572. preferences: attachment[:preferences]
  573. )
  574. end
  575. end
  576. end
  577. ticket.reload
  578. article.reload
  579. session_user.reload
  580. # run postmaster post filter
  581. filters = {}
  582. Setting.where(area: 'Postmaster::PostFilter').order(:name).each do |setting|
  583. filters[setting.name] = Kernel.const_get(Setting.get(setting.name))
  584. end
  585. filters.each_value do |backend|
  586. Rails.logger.debug { "run postmaster post filter #{backend}" }
  587. begin
  588. backend.run(channel, mail, ticket, article, session_user)
  589. rescue => e
  590. Rails.logger.error "can't run postmaster post filter #{backend}"
  591. Rails.logger.error e.inspect
  592. end
  593. end
  594. # return new objects
  595. [ticket, article, session_user, mail]
  596. end
  597. def self.check_attributes_by_x_headers(header_name, value)
  598. class_name = nil
  599. attribute = nil
  600. if header_name =~ /^x-zammad-(.+?)-(followup-|)(.*)$/i
  601. class_name = $1
  602. attribute = $3
  603. end
  604. return true if !class_name
  605. if class_name.downcase == 'article'
  606. class_name = 'Ticket::Article'
  607. end
  608. return true if !attribute
  609. key_short = attribute[ attribute.length - 3, attribute.length ]
  610. return true if key_short != '_id'
  611. class_object = Object.const_get(class_name.to_classname)
  612. return if !class_object
  613. class_instance = class_object.new
  614. return false if !class_instance.association_id_validation(attribute, value)
  615. true
  616. end
  617. def self.sender_properties(from)
  618. data = {}
  619. return data if from.blank?
  620. begin
  621. list = Mail::AddressList.new(from)
  622. list.addresses.each do |address|
  623. data[:from_email] = address.address
  624. data[:from_local] = address.local
  625. data[:from_domain] = address.domain
  626. data[:from_display_name] = address.display_name ||
  627. (address.comments && address.comments[0])
  628. break if data[:from_email].present? && data[:from_email] =~ /@/
  629. end
  630. rescue => e
  631. if from =~ /<>/ && from =~ /<.+?>/
  632. data = sender_properties(from.gsub(/<>/, ''))
  633. end
  634. end
  635. if data.blank? || data[:from_email].blank?
  636. from.strip!
  637. if from =~ /^(.+?)<(.+?)@(.+?)>$/
  638. data[:from_email] = "#{$2}@#{$3}"
  639. data[:from_local] = $2
  640. data[:from_domain] = $3
  641. data[:from_display_name] = $1
  642. else
  643. data[:from_email] = from
  644. data[:from_local] = from
  645. data[:from_domain] = from
  646. end
  647. end
  648. # do extra decoding because we needed to use field.value
  649. data[:from_display_name] = Mail::Field.new('X-From', Encode.conv('utf8', data[:from_display_name])).to_s
  650. data[:from_display_name].delete!('"')
  651. data[:from_display_name].strip!
  652. data[:from_display_name].gsub!(/^'/, '')
  653. data[:from_display_name].gsub!(/'$/, '')
  654. data
  655. end
  656. def set_attributes_by_x_headers(item_object, header_name, mail, suffix = false)
  657. # loop all x-zammad-header-* headers
  658. item_object.attributes.each_key do |key|
  659. # ignore read only attributes
  660. next if key == 'updated_by_id'
  661. next if key == 'created_by_id'
  662. # check if id exists
  663. key_short = key[ key.length - 3, key.length ]
  664. if key_short == '_id'
  665. key_short = key[ 0, key.length - 3 ]
  666. header = "x-zammad-#{header_name}-#{key_short}"
  667. if suffix
  668. header = "x-zammad-#{header_name}-#{suffix}-#{key_short}"
  669. end
  670. # only set value on _id if value/reference lookup exists
  671. if mail[ header.to_sym ]
  672. Rails.logger.info "set_attributes_by_x_headers header #{header} found #{mail[header.to_sym]}"
  673. item_object.class.reflect_on_all_associations.map do |assoc|
  674. next if assoc.name.to_s != key_short
  675. Rails.logger.info "set_attributes_by_x_headers found #{assoc.class_name} lookup for '#{mail[header.to_sym]}'"
  676. item = assoc.class_name.constantize
  677. assoc_object = nil
  678. if item.respond_to?(:name)
  679. assoc_object = item.lookup(name: mail[header.to_sym])
  680. end
  681. if !assoc_object && item.respond_to?(:login)
  682. assoc_object = item.lookup(login: mail[header.to_sym])
  683. end
  684. if assoc_object.blank?
  685. # no assoc exists, remove header
  686. mail.delete(header.to_sym)
  687. next
  688. end
  689. Rails.logger.info "set_attributes_by_x_headers assign #{item_object.class} #{key}=#{assoc_object.id}"
  690. item_object[key] = assoc_object.id
  691. end
  692. end
  693. end
  694. # check if attribute exists
  695. header = "x-zammad-#{header_name}-#{key}"
  696. if suffix
  697. header = "x-zammad-#{header_name}-#{suffix}-#{key}"
  698. end
  699. if mail[header.to_sym]
  700. Rails.logger.info "set_attributes_by_x_headers header #{header} found. Assign #{key}=#{mail[header.to_sym]}"
  701. item_object[key] = mail[header.to_sym]
  702. end
  703. end
  704. end
  705. =begin
  706. process unprocessable_mails (tmp/unprocessable_mail/*.eml) again
  707. Channel::EmailParser.process_unprocessable_mails
  708. =end
  709. def self.process_unprocessable_mails(params = {})
  710. path = Rails.root.join('tmp', 'unprocessable_mail')
  711. files = []
  712. Dir.glob("#{path}/*.eml") do |entry|
  713. ticket, article, user, mail = Channel::EmailParser.new.process(params, IO.binread(entry))
  714. next if ticket.blank?
  715. files.push entry
  716. File.delete(entry)
  717. end
  718. files
  719. end
  720. end
  721. module Mail
  722. # workaround to get content of no parseable headers - in most cases with non 7 bit ascii signs
  723. class Field
  724. def raw_value
  725. value = Encode.conv('utf8', @raw_value)
  726. return value if value.blank?
  727. value.sub(/^.+?:(\s|)/, '')
  728. end
  729. end
  730. # workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)
  731. module Encodings
  732. def self.value_decode(str)
  733. # Optimization: If there's no encoded-words in the string, just return it
  734. return str if !str.index('=?')
  735. str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  736. # Split on white-space boundaries with capture, so we capture the white-space as well
  737. str.split(/([ \t])/).map do |text|
  738. if text.index('=?') .nil?
  739. text
  740. else
  741. # Join QP encoded-words that are adjacent to avoid decoding partial chars
  742. # text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
  743. # Search for occurences of quoted strings or plain strings
  744. text.scan(/( # Group around entire regex to include it in matches
  745. \=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
  746. | # or
  747. .+?(?=\=\?|$) # Plain String
  748. )/xmi).map do |matches|
  749. string, method = *matches
  750. if method == 'b' || method == 'B' # rubocop:disable Style/MultipleComparison
  751. b_value_decode(string)
  752. elsif method == 'q' || method == 'Q' # rubocop:disable Style/MultipleComparison
  753. q_value_decode(string)
  754. else
  755. string
  756. end
  757. end
  758. end
  759. end.join('')
  760. end
  761. end
  762. # issue#348 - IMAP mail fetching stops because of broken spam email (e. g. broken Content-Transfer-Encoding value see test/fixtures/mail43.box)
  763. # https://github.com/zammad/zammad/issues/348
  764. class Body
  765. def decoded
  766. if !Encodings.defined?(encoding)
  767. #raise UnknownEncodingType, "Don't know how to decode #{encoding}, please call #encoded and decode it yourself."
  768. Rails.logger.info "UnknownEncodingType: Don't know how to decode #{encoding}!"
  769. raw_source
  770. else
  771. Encodings.get_encoding(encoding).decode(raw_source)
  772. end
  773. end
  774. end
  775. end