email_parser.rb 25 KB

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