email_parser.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. begin
  100. data[:from_email] = Mail::Address.new(from).address
  101. data[:from_local] = Mail::Address.new(from).local
  102. data[:from_domain] = Mail::Address.new(from).domain
  103. data[:from_display_name] = Mail::Address.new(from).display_name ||
  104. (Mail::Address.new(from).comments && Mail::Address.new(from).comments[0])
  105. rescue
  106. from.strip!
  107. if from =~ /^(.+?)<(.+?)@(.+?)>$/
  108. data[:from_email] = "#{$2}@#{$3}"
  109. data[:from_local] = $2
  110. data[:from_domain] = $3
  111. data[:from_display_name] = $1
  112. else
  113. data[:from_email] = from
  114. data[:from_local] = from
  115. data[:from_domain] = from
  116. end
  117. end
  118. # do extra decoding because we needed to use field.value
  119. data[:from_display_name] = Mail::Field.new('X-From', data[:from_display_name]).to_s
  120. data[:from_display_name].delete!('"')
  121. data[:from_display_name].strip!
  122. data[:from_display_name].gsub!(/^'/, '')
  123. data[:from_display_name].gsub!(/'$/, '')
  124. # compat headers
  125. data[:message_id] = data['message-id'.to_sym]
  126. # body
  127. # plain_part = mail.multipart? ? (mail.text_part ? mail.text_part.body.decoded : nil) : mail.body.decoded
  128. # html_part = message.html_part ? message.html_part.body.decoded : nil
  129. data[:attachments] = []
  130. # multi part email
  131. if mail.multipart?
  132. # html attachment/body may exists and will be converted to strict html
  133. if mail.html_part && mail.html_part.body
  134. data[:body] = mail.html_part.body.to_s
  135. data[:body] = Encode.conv(mail.html_part.charset.to_s, data[:body])
  136. data[:body] = data[:body].html2html_strict.to_s.force_encoding('utf-8')
  137. if !data[:body].force_encoding('UTF-8').valid_encoding?
  138. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  139. end
  140. data[:content_type] = 'text/html'
  141. end
  142. # text attachment/body exists
  143. if data[:body].empty? && mail.text_part
  144. data[:body] = mail.text_part.body.decoded
  145. data[:body] = Encode.conv(mail.text_part.charset, data[:body])
  146. if !data[:body].valid_encoding?
  147. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  148. end
  149. data[:content_type] = 'text/plain'
  150. end
  151. # any other attachments
  152. if data[:body].empty?
  153. data[:body] = 'no visible content'
  154. data[:content_type] = 'text/plain'
  155. end
  156. # add html attachment/body as real attachment
  157. if mail.html_part
  158. filename = 'message.html'
  159. headers_store = {
  160. 'content-alternative' => true,
  161. }
  162. if mail.mime_type
  163. headers_store['Mime-Type'] = mail.html_part.mime_type
  164. end
  165. if mail.charset
  166. headers_store['Charset'] = mail.html_part.charset
  167. end
  168. attachment = {
  169. data: mail.html_part.body.to_s,
  170. filename: mail.html_part.filename || filename,
  171. preferences: headers_store
  172. }
  173. data[:attachments].push attachment
  174. end
  175. # get attachments
  176. if mail.parts
  177. mail.parts.each { |part|
  178. # protect process to work fine with spam emails, see test/fixtures/mail15.box
  179. begin
  180. attachs = _get_attachment(part, data[:attachments], mail)
  181. data[:attachments].concat(attachs)
  182. rescue
  183. attachs = _get_attachment(part, data[:attachments], mail)
  184. data[:attachments].concat(attachs)
  185. end
  186. }
  187. end
  188. # not multipart email
  189. # html part only, convert to text and add it as attachment
  190. elsif mail.mime_type && mail.mime_type.to_s.casecmp('text/html').zero?
  191. filename = 'message.html'
  192. data[:body] = mail.body.decoded
  193. data[:body] = Encode.conv(mail.charset, data[:body])
  194. data[:body] = data[:body].html2html_strict.to_s.force_encoding('utf-8')
  195. if !data[:body].valid_encoding?
  196. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  197. end
  198. data[:content_type] = 'text/html'
  199. # add body as attachment
  200. headers_store = {
  201. 'content-alternative' => true,
  202. }
  203. if mail.mime_type
  204. headers_store['Mime-Type'] = mail.mime_type
  205. end
  206. if mail.charset
  207. headers_store['Charset'] = mail.charset
  208. end
  209. attachment = {
  210. data: mail.body.decoded,
  211. filename: mail.filename || filename,
  212. preferences: headers_store
  213. }
  214. data[:attachments].push attachment
  215. # text part only
  216. elsif !mail.mime_type || mail.mime_type.to_s == '' || mail.mime_type.to_s.casecmp('text/plain').zero?
  217. data[:body] = mail.body.decoded
  218. data[:body] = Encode.conv(mail.charset, data[:body])
  219. if !data[:body].force_encoding('UTF-8').valid_encoding?
  220. data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
  221. end
  222. data[:content_type] = 'text/plain'
  223. else
  224. filename = '-no name-'
  225. data[:body] = 'no visible content'
  226. data[:content_type] = 'text/plain'
  227. # add body as attachment
  228. headers_store = {
  229. 'content-alternative' => true,
  230. }
  231. if mail.mime_type
  232. headers_store['Mime-Type'] = mail.mime_type
  233. end
  234. if mail.charset
  235. headers_store['Charset'] = mail.charset
  236. end
  237. attachment = {
  238. data: mail.body.decoded,
  239. filename: mail.filename || filename,
  240. preferences: headers_store
  241. }
  242. data[:attachments].push attachment
  243. end
  244. # strip not wanted chars
  245. data[:body].gsub!(/\n\r/, "\n")
  246. data[:body].gsub!(/\r\n/, "\n")
  247. data[:body].tr!("\r", "\n")
  248. # get mail date
  249. begin
  250. if mail.date
  251. data[:date] = Time.zone.parse(mail.date.to_s)
  252. end
  253. rescue
  254. data[:date] = nil
  255. end
  256. # remember original mail instance
  257. data[:mail_instance] = mail
  258. data
  259. end
  260. def _get_attachment(file, attachments, mail)
  261. # check if sub parts are available
  262. if !file.parts.empty?
  263. a = []
  264. file.parts.each { |p|
  265. attachment = _get_attachment(p, attachments, mail)
  266. a.concat(attachment)
  267. }
  268. return a
  269. end
  270. # ignore text/plain attachments - already shown in view
  271. return [] if mail.text_part && mail.text_part.body.to_s == file.body.to_s
  272. # ignore text/html - html part, already shown in view
  273. return [] if mail.html_part && mail.html_part.body.to_s == file.body.to_s
  274. # get file preferences
  275. headers_store = {}
  276. file.header.fields.each { |field|
  277. headers_store[field.name.to_s] = field.value.to_s
  278. }
  279. # get filename from content-disposition
  280. filename = nil
  281. # workaround for: NoMethodError: undefined method `filename' for #<Mail::UnstructuredField:0x007ff109e80678>
  282. begin
  283. filename = file.header[:content_disposition].filename
  284. rescue
  285. begin
  286. result = file.header[:content_disposition].to_s.scan( /filename=("|)(.+?)("|);/i )
  287. if result && result[0] && result[0][1]
  288. filename = result[0][1]
  289. end
  290. rescue
  291. Rails.logger.debug 'Unable to get filename'
  292. end
  293. end
  294. # for some broken sm mail clients (X-MimeOLE: Produced By Microsoft Exchange V6.5)
  295. filename ||= file.header[:content_location].to_s
  296. # generate file name
  297. if filename.blank?
  298. attachment_count = 0
  299. (1..1000).each { |count|
  300. filename_exists = false
  301. filename = 'file-' + count.to_s
  302. attachments.each { |attachment|
  303. if attachment[:filename] == filename
  304. filename_exists = true
  305. end
  306. }
  307. break if filename_exists == false
  308. }
  309. end
  310. # get mime type
  311. if file.header[:content_type] && file.header[:content_type].string
  312. headers_store['Mime-Type'] = file.header[:content_type].string
  313. end
  314. # get charset
  315. if file.header && file.header.charset
  316. headers_store['Charset'] = file.header.charset
  317. end
  318. # remove not needed header
  319. headers_store.delete('Content-Transfer-Encoding')
  320. headers_store.delete('Content-Disposition')
  321. # cleanup content id, <> will be added automatically later
  322. if headers_store['Content-ID']
  323. headers_store['Content-ID'].gsub!(/^</, '').gsub!(/>$/, '')
  324. end
  325. # workaround for mail gem
  326. # https://github.com/zammad/zammad/issues/928
  327. filename = Mail::Encodings.value_decode(filename)
  328. attach = {
  329. data: file.body.to_s,
  330. filename: filename,
  331. preferences: headers_store,
  332. }
  333. [attach]
  334. end
  335. =begin
  336. parser = Channel::EmailParser.new
  337. ticket, article, user = parser.process(channel, email_raw_string)
  338. returns
  339. [ticket, article, user]
  340. do not raise an exception - e. g. if used by scheduler
  341. parser = Channel::EmailParser.new
  342. ticket, article, user = parser.process(channel, email_raw_string, false)
  343. returns
  344. [ticket, article, user] || false
  345. =end
  346. def process(channel, msg, exception = true)
  347. _process(channel, msg)
  348. rescue => e
  349. # store unprocessable email for bug reporting
  350. path = "#{Rails.root}/tmp/unprocessable_mail/"
  351. FileUtils.mkpath path
  352. md5 = Digest::MD5.hexdigest(msg)
  353. filename = "#{path}/#{md5}.eml"
  354. 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"
  355. p message # rubocop:disable Rails/Output
  356. p 'ERROR: ' + e.inspect # rubocop:disable Rails/Output
  357. Rails.logger.error message
  358. Rails.logger.error 'ERROR: ' + e.inspect
  359. Rails.logger.error 'ERROR: ' + e.backtrace.inspect
  360. File.open(filename, 'wb') { |file|
  361. file.write msg
  362. }
  363. return false if exception == false
  364. raise e.inspect + e.backtrace.inspect
  365. end
  366. def _process(channel, msg)
  367. # parse email
  368. mail = parse(msg)
  369. # run postmaster pre filter
  370. UserInfo.current_user_id = 1
  371. filters = {}
  372. Setting.where(area: 'Postmaster::PreFilter').order(:name).each { |setting|
  373. filters[setting.name] = Kernel.const_get(Setting.get(setting.name))
  374. }
  375. filters.each { |_prio, backend|
  376. Rails.logger.debug "run postmaster pre filter #{backend}"
  377. begin
  378. backend.run(channel, mail)
  379. rescue => e
  380. Rails.logger.error "can't run postmaster pre filter #{backend}"
  381. Rails.logger.error e.inspect
  382. raise e
  383. end
  384. }
  385. # check ignore header
  386. if mail['x-zammad-ignore'.to_sym] == 'true' || mail['x-zammad-ignore'.to_sym] == true
  387. Rails.logger.info "ignored email with msgid '#{mail[:message_id]}' from '#{mail[:from]}' because of x-zammad-ignore header"
  388. return true
  389. end
  390. # set interface handle
  391. original_interface_handle = ApplicationHandleInfo.current
  392. ticket = nil
  393. article = nil
  394. session_user = nil
  395. # use transaction
  396. Transaction.execute(interface_handle: "#{original_interface_handle}.postmaster") do
  397. # get sender user
  398. session_user_id = mail['x-zammad-session-user-id'.to_sym]
  399. if !session_user_id
  400. raise 'No x-zammad-session-user-id, no sender set!'
  401. end
  402. session_user = User.lookup(id: session_user_id)
  403. if !session_user
  404. raise "No user found for x-zammad-session-user-id: #{session_user_id}!"
  405. end
  406. # set current user
  407. UserInfo.current_user_id = session_user.id
  408. # get ticket# based on email headers
  409. if mail['x-zammad-ticket-id'.to_sym]
  410. ticket = Ticket.find_by(id: mail['x-zammad-ticket-id'.to_sym])
  411. end
  412. if mail['x-zammad-ticket-number'.to_sym]
  413. ticket = Ticket.find_by(number: mail['x-zammad-ticket-number'.to_sym])
  414. end
  415. # set ticket state to open if not new
  416. if ticket
  417. set_attributes_by_x_headers(ticket, 'ticket', mail, 'followup')
  418. # save changes set by x-zammad-ticket-followup-* headers
  419. ticket.save if ticket.changed?
  420. state = Ticket::State.find(ticket.state_id)
  421. state_type = Ticket::StateType.find(state.state_type_id)
  422. # if tickte is merged, find linked ticket
  423. if state_type.name == 'merged'
  424. end
  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. to: mail[:to],
  476. cc: mail[:cc],
  477. subject: mail[:subject],
  478. message_id: mail[:message_id],
  479. internal: false,
  480. )
  481. # x-headers lookup
  482. set_attributes_by_x_headers(article, 'article', mail)
  483. # create article
  484. article.save!
  485. # store mail plain
  486. article.save_as_raw(msg)
  487. # store attachments
  488. if mail[:attachments]
  489. mail[:attachments].each do |attachment|
  490. Store.add(
  491. object: 'Ticket::Article',
  492. o_id: article.id,
  493. data: attachment[:data],
  494. filename: attachment[:filename],
  495. preferences: attachment[:preferences]
  496. )
  497. end
  498. end
  499. end
  500. end
  501. # run postmaster post filter
  502. filters = {}
  503. Setting.where(area: 'Postmaster::PostFilter').order(:name).each { |setting|
  504. filters[setting.name] = Kernel.const_get(Setting.get(setting.name))
  505. }
  506. filters.each { |_prio, backend|
  507. Rails.logger.debug "run postmaster post filter #{backend}"
  508. begin
  509. backend.run(channel, mail, ticket, article, session_user)
  510. rescue => e
  511. Rails.logger.error "can't run postmaster post filter #{backend}"
  512. Rails.logger.error e.inspect
  513. end
  514. }
  515. # return new objects
  516. [ticket, article, session_user, mail]
  517. end
  518. def self.check_attributes_by_x_headers(header_name, value)
  519. class_name = nil
  520. attribute = nil
  521. if header_name =~ /^x-zammad-(.+?)-(followup-|)(.*)$/i
  522. class_name = $1
  523. attribute = $3
  524. end
  525. return true if !class_name
  526. if class_name.downcase == 'article'
  527. class_name = 'Ticket::Article'
  528. end
  529. return true if !attribute
  530. key_short = attribute[ attribute.length - 3, attribute.length ]
  531. return true if key_short != '_id'
  532. class_object = Object.const_get(class_name.to_classname)
  533. return if !class_object
  534. class_instance = class_object.new
  535. return false if !class_instance.association_id_validation(attribute, value)
  536. true
  537. end
  538. def set_attributes_by_x_headers(item_object, header_name, mail, suffix = false)
  539. # loop all x-zammad-hedaer-* headers
  540. item_object.attributes.each { |key, _value|
  541. # ignore read only attributes
  542. next if key == 'updated_by_id'
  543. next if key == 'created_by_id'
  544. # check if id exists
  545. key_short = key[ key.length - 3, key.length ]
  546. if key_short == '_id'
  547. key_short = key[ 0, key.length - 3 ]
  548. header = "x-zammad-#{header_name}-#{key_short}"
  549. if suffix
  550. header = "x-zammad-#{header_name}-#{suffix}-#{key_short}"
  551. end
  552. # only set value on _id if value/reference lookup exists
  553. if mail[ header.to_sym ]
  554. Rails.logger.info "header #{header} found #{mail[header.to_sym]}"
  555. item_object.class.reflect_on_all_associations.map { |assoc|
  556. next if assoc.name.to_s != key_short
  557. Rails.logger.info "ASSOC found #{assoc.class_name} lookup #{mail[header.to_sym]}"
  558. item = assoc.class_name.constantize
  559. assoc_object = nil
  560. assoc_has_object = false
  561. if item.respond_to?(:name)
  562. assoc_has_object = true
  563. if item.lookup(name: mail[header.to_sym])
  564. assoc_object = item.lookup(name: mail[header.to_sym])
  565. end
  566. elsif item.respond_to?(:login)
  567. assoc_has_object = true
  568. if item.lookup(login: mail[header.to_sym])
  569. assoc_object = item.lookup(login: mail[header.to_sym])
  570. end
  571. end
  572. next if assoc_has_object == false
  573. if assoc_object
  574. item_object[key] = assoc_object.id
  575. next
  576. end
  577. # no assoc exists, remove header
  578. mail.delete(header.to_sym)
  579. }
  580. end
  581. end
  582. # check if attribute exists
  583. header = "x-zammad-#{header_name}-#{key}"
  584. if suffix
  585. header = "x-zammad-#{header_name}-#{suffix}-#{key}"
  586. end
  587. if mail[header.to_sym]
  588. Rails.logger.info "header #{header} found #{mail[header.to_sym]}"
  589. item_object[key] = mail[header.to_sym]
  590. end
  591. }
  592. end
  593. end
  594. module Mail
  595. # workaround to get content of no parseable headers - in most cases with non 7 bit ascii signs
  596. class Field
  597. def raw_value
  598. value = Encode.conv('utf8', @raw_value)
  599. value.sub(/^.+?:(\s|)/, '')
  600. end
  601. end
  602. # workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)
  603. module Encodings
  604. def self.value_decode(str)
  605. # Optimization: If there's no encoded-words in the string, just return it
  606. return str unless str.index('=?')
  607. str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  608. # Split on white-space boundaries with capture, so we capture the white-space as well
  609. str.split(/([ \t])/).map do |text|
  610. if text.index('=?') .nil?
  611. text
  612. else
  613. # Join QP encoded-words that are adjacent to avoid decoding partial chars
  614. # text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/
  615. # Search for occurences of quoted strings or plain strings
  616. text.scan(/( # Group around entire regex to include it in matches
  617. \=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method
  618. | # or
  619. .+?(?=\=\?|$) # Plain String
  620. )/xmi).map do |matches|
  621. string, method = *matches
  622. if method == 'b' || method == 'B'
  623. b_value_decode(string)
  624. elsif method == 'q' || method == 'Q'
  625. q_value_decode(string)
  626. else
  627. string
  628. end
  629. end
  630. end
  631. end.join('')
  632. end
  633. end
  634. # issue#348 - IMAP mail fetching stops because of broken spam email (e. g. broken Content-Transfer-Encoding value see test/fixtures/mail43.box)
  635. # https://github.com/zammad/zammad/issues/348
  636. class Body
  637. def decoded
  638. if !Encodings.defined?(encoding)
  639. #raise UnknownEncodingType, "Don't know how to decode #{encoding}, please call #encoded and decode it yourself."
  640. Rails.logger.info "UnknownEncodingType: Don't know how to decode #{encoding}!"
  641. raw_source
  642. else
  643. Encodings.get_encoding(encoding).decode(raw_source)
  644. end
  645. end
  646. end
  647. end