email_parser.rb 28 KB

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