imap.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'net/imap'
  3. class Channel::Driver::Imap < Channel::EmailParser
  4. FETCH_METADATA_TIMEOUT = 2.minutes
  5. FETCH_MSG_TIMEOUT = 4.minutes
  6. EXPUNGE_TIMEOUT = 16.minutes
  7. def fetchable?(_channel)
  8. true
  9. end
  10. =begin
  11. fetch emails from IMAP account
  12. instance = Channel::Driver::Imap.new
  13. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  14. returns
  15. {
  16. result: 'ok',
  17. fetched: 123,
  18. notice: 'e. g. message about to big emails in mailbox',
  19. }
  20. check if connect to IMAP account is possible, return count of mails in mailbox
  21. instance = Channel::Driver::Imap.new
  22. result = instance.fetch(params[:inbound][:options], channel, 'check')
  23. returns
  24. {
  25. result: 'ok',
  26. content_messages: 123,
  27. }
  28. verify IMAP account, check if search email is in there
  29. instance = Channel::Driver::Imap.new
  30. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  31. returns
  32. {
  33. result: 'ok', # 'verify not ok'
  34. }
  35. example
  36. params = {
  37. host: 'outlook.office365.com',
  38. user: 'xxx@znuny.onmicrosoft.com',
  39. password: 'xxx',
  40. keep_on_server: true,
  41. }
  42. OR
  43. params = {
  44. host: 'imap.gmail.com',
  45. user: 'xxx@gmail.com',
  46. password: 'xxx',
  47. keep_on_server: true,
  48. auth_type: 'XOAUTH2'
  49. }
  50. channel = Channel.last
  51. instance = Channel::Driver::Imap.new
  52. result = instance.fetch(params, channel, 'verify')
  53. =end
  54. def fetch(options, channel, check_type = '', verify_string = '')
  55. ssl = true
  56. starttls = false
  57. port = 993
  58. keep_on_server = false
  59. folder = 'INBOX'
  60. if options[:keep_on_server] == true || options[:keep_on_server] == 'true'
  61. keep_on_server = true
  62. end
  63. if options.key?(:ssl) && options[:ssl] == false
  64. ssl = false
  65. port = 143
  66. end
  67. port = if options.key?(:port) && options[:port].present?
  68. options[:port].to_i
  69. elsif ssl == true
  70. 993
  71. else
  72. 143
  73. end
  74. if ssl == true && port != 993
  75. ssl = false
  76. starttls = true
  77. end
  78. if options[:folder].present?
  79. folder = options[:folder]
  80. end
  81. Rails.logger.info "fetching imap (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl},starttls=#{starttls},folder=#{folder},keep_on_server=#{keep_on_server},auth_type=#{options.fetch(:auth_type, 'LOGIN')})"
  82. # on check, reduce open_timeout to have faster probing
  83. check_type_timeout = 45
  84. if check_type == 'check'
  85. check_type_timeout = 6
  86. end
  87. timeout(check_type_timeout) do
  88. @imap = ::Net::IMAP.new(options[:host], port, ssl, nil, false)
  89. if starttls
  90. @imap.starttls()
  91. end
  92. end
  93. timeout(check_type_timeout) do
  94. if options[:auth_type].present?
  95. @imap.authenticate(options[:auth_type], options[:user], options[:password])
  96. else
  97. @imap.login(options[:user], options[:password].dup&.force_encoding('ascii-8bit'))
  98. end
  99. end
  100. timeout(check_type_timeout) do
  101. # select folder
  102. @imap.select(folder)
  103. end
  104. message_ids = timeout(6.minutes) do
  105. if keep_on_server && check_type != 'check' && check_type != 'verify'
  106. fetch_unread_message_ids
  107. else
  108. fetch_all_message_ids
  109. end
  110. end
  111. # check mode only
  112. if check_type == 'check'
  113. Rails.logger.info 'check only mode, fetch no emails'
  114. content_max_check = 2
  115. content_messages = 0
  116. # check messages
  117. message_ids.each do |message_id|
  118. message_meta = nil
  119. timeout(1.minute) do
  120. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  121. end
  122. # check how many content messages we have, for notice used
  123. headers = self.class.extract_rfc822_headers(message_meta)
  124. next if messages_is_verify_message?(headers)
  125. next if messages_is_ignore_message?(headers)
  126. content_messages += 1
  127. break if content_max_check < content_messages
  128. end
  129. if content_messages >= content_max_check
  130. content_messages = message_ids.count
  131. end
  132. archive_possible = false
  133. archive_check = 0
  134. archive_max_check = 500
  135. archive_days_range = 14
  136. archive_week_range = archive_days_range / 7
  137. message_ids.reverse_each do |message_id|
  138. message_meta = nil
  139. timeout(1.minute) do
  140. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  141. end
  142. headers = self.class.extract_rfc822_headers(message_meta)
  143. next if messages_is_verify_message?(headers)
  144. next if messages_is_ignore_message?(headers)
  145. next if headers['Date'].blank?
  146. archive_check += 1
  147. break if archive_check >= archive_max_check
  148. begin
  149. date = Time.zone.parse(headers['Date'])
  150. rescue => e
  151. Rails.logger.error e
  152. next
  153. end
  154. break if date >= Time.zone.now - archive_days_range.days
  155. archive_possible = true
  156. break
  157. end
  158. disconnect
  159. return {
  160. result: 'ok',
  161. content_messages: content_messages,
  162. archive_possible: archive_possible,
  163. archive_week_range: archive_week_range,
  164. }
  165. end
  166. # reverse message order to increase performance
  167. if check_type == 'verify'
  168. Rails.logger.info "verify mode, fetch no emails #{verify_string}"
  169. message_ids.reverse!
  170. # check for verify message
  171. message_ids.each do |message_id|
  172. message_meta = nil
  173. timeout(FETCH_METADATA_TIMEOUT) do
  174. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  175. end
  176. # check if verify message exists
  177. headers = self.class.extract_rfc822_headers(message_meta)
  178. subject = headers['Subject']
  179. next if !subject
  180. next if !subject.match?(/#{verify_string}/)
  181. Rails.logger.info " - verify email #{verify_string} found"
  182. timeout(600) do
  183. @imap.store(message_id, '+FLAGS', [:Deleted])
  184. @imap.expunge()
  185. end
  186. disconnect
  187. return {
  188. result: 'ok',
  189. }
  190. end
  191. disconnect
  192. return {
  193. result: 'verify not ok',
  194. }
  195. end
  196. # fetch regular messages
  197. count_all = message_ids.count
  198. count = 0
  199. count_fetched = 0
  200. count_max = 5000
  201. too_large_messages = []
  202. active_check_interval = 20
  203. result = 'ok'
  204. notice = ''
  205. message_ids.each do |message_id|
  206. count += 1
  207. break if (count % active_check_interval).zero? && channel_has_changed?(channel)
  208. break if max_process_count_has_reached?(channel, count, count_max)
  209. Rails.logger.info " - message #{count}/#{count_all}"
  210. message_meta = nil
  211. timeout(FETCH_METADATA_TIMEOUT) do
  212. message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'FLAGS', 'INTERNALDATE', 'RFC822.HEADER'])[0]
  213. rescue Net::IMAP::ResponseParseError => e
  214. raise if e.message.exclude?('unknown token')
  215. result = 'error'
  216. notice += <<~NOTICE
  217. One of your incoming emails could not be imported (#{e.message}).
  218. Please remove it from your inbox directly
  219. to prevent Zammad from trying to import it again.
  220. NOTICE
  221. Rails.logger.error "Net::IMAP failed to parse message #{message_id}: #{e.message} (#{e.class})"
  222. Rails.logger.error '(See https://github.com/zammad/zammad/issues/2754 for more details)'
  223. end
  224. next if message_meta.nil?
  225. # ignore verify messages
  226. next if !messages_is_too_old_verify?(message_meta, count, count_all)
  227. # ignore deleted messages
  228. next if deleted?(message_meta, count, count_all)
  229. # ignore already imported
  230. next if already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
  231. # delete email from server after article was created
  232. msg = nil
  233. begin
  234. timeout(FETCH_MSG_TIMEOUT) do
  235. msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
  236. end
  237. rescue Timeout::Error => e
  238. Rails.logger.error "Unable to fetch email from #{count}/#{count_all} from server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  239. raise e
  240. end
  241. next if !msg
  242. # do not process too big messages, instead download & send postmaster reply
  243. too_large_info = too_large?(message_meta)
  244. if too_large_info
  245. if Setting.get('postmaster_send_reject_if_mail_too_large') == true
  246. info = " - download message #{count}/#{count_all} - ignore message because it's too large (is:#{too_large_info[0]} MB/max:#{too_large_info[1]} MB)"
  247. Rails.logger.info info
  248. notice += "#{info}\n"
  249. process_oversized_mail(channel, msg)
  250. else
  251. info = " - ignore message #{count}/#{count_all} - because message is too large (is:#{too_large_info[0]} MB/max:#{too_large_info[1]} MB)"
  252. Rails.logger.info info
  253. notice += "#{info}\n"
  254. too_large_messages.push info
  255. next
  256. end
  257. else
  258. process(channel, msg, false)
  259. end
  260. begin
  261. timeout(FETCH_MSG_TIMEOUT) do
  262. if keep_on_server
  263. @imap.store(message_id, '+FLAGS', [:Seen])
  264. else
  265. @imap.store(message_id, '+FLAGS', [:Deleted])
  266. end
  267. end
  268. rescue Timeout::Error => e
  269. Rails.logger.error "Unable to set +FLAGS for email #{count}/#{count_all} on server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  270. raise e
  271. end
  272. count_fetched += 1
  273. end
  274. if !keep_on_server
  275. begin
  276. timeout(EXPUNGE_TIMEOUT) do
  277. @imap.expunge()
  278. end
  279. rescue Timeout::Error => e
  280. Rails.logger.error "Unable to expunge server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  281. raise e
  282. end
  283. end
  284. disconnect
  285. if count.zero?
  286. Rails.logger.info ' - no message'
  287. end
  288. if too_large_messages.present?
  289. raise too_large_messages.join("\n")
  290. end
  291. Rails.logger.info 'done'
  292. {
  293. result: result,
  294. fetched: count_fetched,
  295. notice: notice,
  296. }
  297. end
  298. def fetch_all_message_ids
  299. fetch_message_ids %w[ALL]
  300. end
  301. def fetch_unread_message_ids
  302. fetch_message_ids %w[NOT SEEN]
  303. rescue
  304. fetch_message_ids %w[UNSEEN]
  305. end
  306. def fetch_message_ids(filter)
  307. @imap.sort(['DATE'], filter, 'US-ASCII')
  308. rescue
  309. @imap.search(filter)
  310. end
  311. def disconnect
  312. return if !@imap
  313. timeout(1.minute) do
  314. @imap.disconnect()
  315. end
  316. end
  317. =begin
  318. Channel::Driver::Imap.streamable?
  319. returns
  320. true|false
  321. =end
  322. def self.streamable?
  323. false
  324. end
  325. # Parses RFC822 header
  326. # @param [String] RFC822 header text blob
  327. # @return [Hash<String=>String>]
  328. def self.parse_rfc822_headers(string)
  329. array = string
  330. .gsub("\r\n\t", ' ') # Some servers (e.g. microsoft365) may put attribute value on a separate line and tab it
  331. .lines(chomp: true)
  332. .map { |line| line.split(/:\s*/, 2).map(&:strip) }
  333. array.each { |elem| elem.append(nil) if elem.one? }
  334. Hash[*array.flatten]
  335. end
  336. # Parses RFC822 header
  337. # @param [Net::IMAP::FetchData] fetched message
  338. # @return [Hash<String=>String>]
  339. def self.extract_rfc822_headers(message_meta)
  340. blob = message_meta&.attr&.dig 'RFC822.HEADER'
  341. return if !blob
  342. parse_rfc822_headers blob
  343. end
  344. private
  345. def messages_is_too_old_verify?(message_meta, count, count_all)
  346. headers = self.class.extract_rfc822_headers(message_meta)
  347. return true if !messages_is_verify_message?(headers)
  348. return true if headers['X-Zammad-Verify-Time'].blank?
  349. begin
  350. verify_time = Time.zone.parse(headers['X-Zammad-Verify-Time'])
  351. rescue => e
  352. Rails.logger.error e
  353. return true
  354. end
  355. return true if verify_time < Time.zone.now - 30.minutes
  356. Rails.logger.info " - ignore message #{count}/#{count_all} - because message has a verify message"
  357. false
  358. end
  359. def messages_is_verify_message?(headers)
  360. return true if headers['X-Zammad-Verify'] == 'true'
  361. false
  362. end
  363. def messages_is_ignore_message?(headers)
  364. return true if headers['X-Zammad-Ignore'] == 'true'
  365. false
  366. end
  367. =begin
  368. check if email is already impoted
  369. Channel::Driver::IMAP.already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
  370. returns
  371. true|false
  372. =end
  373. # rubocop:disable Metrics/ParameterLists
  374. def already_imported?(message_id, message_meta, count, count_all, keep_on_server, channel)
  375. # rubocop:enable Metrics/ParameterLists
  376. return false if !keep_on_server
  377. headers = self.class.extract_rfc822_headers(message_meta)
  378. retrurn false if !headers
  379. local_message_id = headers['Message-ID']
  380. return false if local_message_id.blank?
  381. local_message_id_md5 = Digest::MD5.hexdigest(local_message_id)
  382. article = Ticket::Article.where(message_id_md5: local_message_id_md5).order('created_at DESC, id DESC').limit(1).first
  383. return false if !article
  384. # verify if message is already imported via same channel, if not, import it again
  385. ticket = article.ticket
  386. return false if ticket&.preferences && ticket.preferences[:channel_id].present? && channel.present? && ticket.preferences[:channel_id] != channel[:id]
  387. timeout(1.minute) do
  388. @imap.store(message_id, '+FLAGS', [:Seen])
  389. end
  390. Rails.logger.info " - ignore message #{count}/#{count_all} - because message message id already imported"
  391. true
  392. end
  393. =begin
  394. check if email is already marked as deleted
  395. Channel::Driver::IMAP.deleted?(message_meta, count, count_all)
  396. returns
  397. true|false
  398. =end
  399. def deleted?(message_meta, count, count_all)
  400. return false if message_meta.attr['FLAGS'].exclude?(:Deleted)
  401. Rails.logger.info " - ignore message #{count}/#{count_all} - because message has already delete flag"
  402. true
  403. end
  404. =begin
  405. check if email is to big
  406. Channel::Driver::IMAP.too_large?(message_meta, count, count_all)
  407. returns
  408. true|false
  409. =end
  410. def too_large?(message_meta)
  411. max_message_size = Setting.get('postmaster_max_size').to_f
  412. real_message_size = message_meta.attr['RFC822.SIZE'].to_f / 1024 / 1024
  413. if real_message_size > max_message_size
  414. return [real_message_size, max_message_size]
  415. end
  416. false
  417. end
  418. =begin
  419. check if channel config has changed
  420. Channel::Driver::IMAP.channel_has_changed?(channel)
  421. returns
  422. true|false
  423. =end
  424. def channel_has_changed?(channel)
  425. current_channel = Channel.find_by(id: channel.id)
  426. if !current_channel
  427. Rails.logger.info "Channel with id #{channel.id} is deleted in the meantime. Stop fetching."
  428. return true
  429. end
  430. return false if channel.updated_at == current_channel.updated_at
  431. Rails.logger.info "Channel with id #{channel.id} has changed. Stop fetching."
  432. true
  433. end
  434. =begin
  435. check if maximal fetching email count has reached
  436. Channel::Driver::IMAP.max_process_count_has_reached?(channel, count, count_max)
  437. returns
  438. true|false
  439. =end
  440. def max_process_count_has_reached?(channel, count, count_max)
  441. return false if count < count_max
  442. Rails.logger.info "Maximal fetched emails (#{count_max}) reached for this interval for Channel with id #{channel.id}."
  443. true
  444. end
  445. def timeout(seconds, &block)
  446. Timeout.timeout(seconds, &block)
  447. end
  448. end