imap.rb 15 KB

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