imap.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'net/imap'
  3. class Channel::Driver::Imap < Channel::Driver::BaseEmailInbound
  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. =begin
  10. fetch emails from IMAP account
  11. instance = Channel::Driver::Imap.new
  12. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  13. returns
  14. {
  15. result: 'ok',
  16. fetched: 123,
  17. notice: 'e. g. message about to big emails in mailbox',
  18. }
  19. check if connect to IMAP account is possible, return count of mails in mailbox
  20. instance = Channel::Driver::Imap.new
  21. result = instance.fetch(params[:inbound][:options], channel, 'check')
  22. returns
  23. {
  24. result: 'ok',
  25. content_messages: 123,
  26. }
  27. verify IMAP account, check if search email is in there
  28. instance = Channel::Driver::Imap.new
  29. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  30. returns
  31. {
  32. result: 'ok', # 'verify not ok'
  33. }
  34. example
  35. params = {
  36. host: 'outlook.office365.com',
  37. user: 'xxx@zammad.onmicrosoft.com',
  38. password: 'xxx',
  39. keep_on_server: true,
  40. }
  41. OR
  42. params = {
  43. host: 'imap.gmail.com',
  44. user: 'xxx@gmail.com',
  45. password: 'xxx',
  46. keep_on_server: true,
  47. auth_type: 'XOAUTH2'
  48. }
  49. channel = Channel.last
  50. instance = Channel::Driver::Imap.new
  51. result = instance.fetch(params, channel, 'verify')
  52. =end
  53. def fetch(options, channel)
  54. setup_connection(options)
  55. keep_on_server = false
  56. if options[:keep_on_server] == true || options[:keep_on_server] == 'true'
  57. keep_on_server = true
  58. end
  59. message_ids_result = Timeout.timeout(6.minutes) do
  60. if keep_on_server
  61. fetch_unread_message_ids
  62. else
  63. fetch_all_message_ids
  64. end
  65. end
  66. message_ids = message_ids_result[:result]
  67. # fetch regular messages
  68. count_all = message_ids.count
  69. count = 0
  70. count_fetched = 0
  71. count_max = 5000
  72. too_large_messages = []
  73. active_check_interval = 20
  74. result = 'ok'
  75. notice = ''
  76. message_ids.each do |message_id|
  77. count += 1
  78. break if (count % active_check_interval).zero? && channel_has_changed?(channel)
  79. break if max_process_count_was_reached?(channel, count, count_max)
  80. Rails.logger.info " - message #{count}/#{count_all}"
  81. message_meta = nil
  82. Timeout.timeout(FETCH_METADATA_TIMEOUT) do
  83. message_meta = @imap.fetch(message_id, ['RFC822.SIZE', 'FLAGS', 'INTERNALDATE', 'RFC822.HEADER'])[0]
  84. rescue Net::IMAP::ResponseParseError => e
  85. raise if e.message.exclude?('unknown token')
  86. result = 'error'
  87. notice += <<~NOTICE
  88. One of your incoming emails could not be imported (#{e.message}).
  89. Please remove it from your inbox directly
  90. to prevent Zammad from trying to import it again.
  91. NOTICE
  92. Rails.logger.error "Net::IMAP failed to parse message #{message_id}: #{e.message} (#{e.class})"
  93. Rails.logger.error '(See https://github.com/zammad/zammad/issues/2754 for more details)'
  94. end
  95. next if message_meta.nil?
  96. # ignore verify messages
  97. next if !messages_is_too_old_verify?(self.class.extract_rfc822_headers(message_meta), count, count_all)
  98. # ignore deleted messages
  99. next if deleted?(message_meta, count, count_all)
  100. # ignore already imported
  101. if already_imported?(self.class.extract_rfc822_headers(message_meta), keep_on_server, channel)
  102. Timeout.timeout(1.minute) do
  103. @imap.store(message_id, '+FLAGS', [:Seen])
  104. end
  105. Rails.logger.info " - ignore message #{count}/#{count_all} - because message message id already imported"
  106. next
  107. end
  108. # delete email from server after article was created
  109. msg = nil
  110. begin
  111. Timeout.timeout(FETCH_MSG_TIMEOUT) do
  112. key = fetch_message_body_key(options)
  113. msg = @imap.fetch(message_id, key)[0].attr[key]
  114. end
  115. rescue Timeout::Error => e
  116. Rails.logger.error "Unable to fetch email from #{count}/#{count_all} from server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  117. raise e
  118. end
  119. next if !msg
  120. # do not process too big messages, instead download & send postmaster reply
  121. too_large_info = too_large?(message_meta.attr['RFC822.SIZE'])
  122. if too_large_info
  123. if Setting.get('postmaster_send_reject_if_mail_too_large') == true
  124. 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)"
  125. Rails.logger.info info
  126. notice += "#{info}\n"
  127. process_oversized_mail(channel, msg)
  128. else
  129. info = " - ignore message #{count}/#{count_all} - because message is too large (is:#{too_large_info[0]} MB/max:#{too_large_info[1]} MB)"
  130. Rails.logger.info info
  131. notice += "#{info}\n"
  132. too_large_messages.push info
  133. next
  134. end
  135. else
  136. process(channel, msg, false)
  137. end
  138. begin
  139. Timeout.timeout(FETCH_MSG_TIMEOUT) do
  140. if keep_on_server
  141. @imap.store(message_id, '+FLAGS', [:Seen])
  142. else
  143. @imap.store(message_id, '+FLAGS', [:Deleted])
  144. end
  145. end
  146. rescue Timeout::Error => e
  147. Rails.logger.error "Unable to set +FLAGS for email #{count}/#{count_all} on server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  148. raise e
  149. end
  150. count_fetched += 1
  151. end
  152. if !keep_on_server
  153. begin
  154. Timeout.timeout(EXPUNGE_TIMEOUT) do
  155. @imap.expunge
  156. end
  157. rescue Timeout::Error => e
  158. Rails.logger.error "Unable to expunge server (#{options[:host]}/#{options[:user]}): #{e.inspect}"
  159. raise e
  160. end
  161. end
  162. disconnect
  163. if count.zero?
  164. Rails.logger.info ' - no message'
  165. end
  166. # Error is raised if one of the messages was too large AND postmaster_send_reject_if_mail_too_large is turned off.
  167. # This effectivelly marks channels as stuck and gets highlighted for the admin.
  168. # New emails are still processed! But large email is not touched, so error keeps being re-raised on every fetch.
  169. if too_large_messages.present?
  170. raise too_large_messages.join("\n")
  171. end
  172. {
  173. result: result,
  174. fetched: count_fetched,
  175. notice: notice,
  176. }
  177. end
  178. def check(options)
  179. setup_connection(options, check: true)
  180. message_ids_result = Timeout.timeout(6.minutes) do
  181. fetch_all_message_ids
  182. end
  183. message_ids = message_ids_result[:result]
  184. Rails.logger.info 'check only mode, fetch no emails'
  185. content_max_check = 2
  186. content_messages = 0
  187. # check messages
  188. message_ids.each do |message_id|
  189. message_meta = nil
  190. Timeout.timeout(1.minute) do
  191. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  192. end
  193. # check how many content messages we have, for notice used
  194. headers = self.class.extract_rfc822_headers(message_meta)
  195. next if messages_is_verify_message?(headers)
  196. next if messages_is_ignore_message?(headers)
  197. content_messages += 1
  198. break if content_max_check < content_messages
  199. end
  200. if content_messages >= content_max_check
  201. content_messages = message_ids.count
  202. end
  203. archive_possible = false || message_ids_result[:is_fallback]
  204. archive_possible_is_fallback = false || message_ids_result[:is_fallback]
  205. archive_check = 0
  206. archive_max_check = 500
  207. archive_days_range = 14
  208. archive_week_range = archive_days_range / 7
  209. # use .each only if ordered response is ascending (from older to newer)
  210. message_ids_iterator = message_ids.each
  211. # since the correct loop order could improve performance, we should check even for less than 500 available messages
  212. # starting with 5 messages, since we need 2 additional fetch requests to find the used order and it would not make sense with less messages
  213. if !message_ids_result[:is_fallback] && content_messages > 4
  214. message_0_meta = nil
  215. message_1_meta = nil
  216. Timeout.timeout(1.minute) do
  217. message_0_meta = @imap.fetch(message_ids[0], ['RFC822.HEADER'])[0]
  218. message_1_meta = @imap.fetch(message_ids[1], ['RFC822.HEADER'])[0]
  219. end
  220. headers0 = self.class.extract_rfc822_headers(message_0_meta)
  221. headers1 = self.class.extract_rfc822_headers(message_1_meta)
  222. if headers0['Date'].present? && headers1['Date'].present?
  223. begin
  224. date0 = Time.zone.parse(headers0['Date'])
  225. date1 = Time.zone.parse(headers1['Date'])
  226. # change iterator to .reverse_each if order of the 2 probe messages is descending (from newer to older)
  227. message_ids_iterator = message_ids.reverse_each if date0 > date1
  228. rescue => e
  229. # no easy order decision possible due to a date parsing issue, continue with default iterator
  230. end
  231. end
  232. end
  233. message_ids_iterator.each do |message_id|
  234. message_meta = nil
  235. Timeout.timeout(1.minute) do
  236. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  237. end
  238. headers = self.class.extract_rfc822_headers(message_meta)
  239. next if messages_is_verify_message?(headers)
  240. next if messages_is_ignore_message?(headers)
  241. next if headers['Date'].blank?
  242. archive_check += 1
  243. break if archive_check >= archive_max_check
  244. begin
  245. date = Time.zone.parse(headers['Date'])
  246. rescue => e
  247. Rails.logger.error e
  248. next
  249. end
  250. break if date >= Time.zone.now - archive_days_range.days
  251. archive_possible = true
  252. # even if it was fallback before, we just found a real old mail, so it's not fallback anymore
  253. archive_possible_is_fallback = false
  254. break
  255. end
  256. disconnect
  257. {
  258. result: 'ok',
  259. content_messages: content_messages,
  260. archive_possible: archive_possible,
  261. archive_possible_is_fallback: archive_possible_is_fallback,
  262. archive_week_range: archive_week_range,
  263. }
  264. end
  265. # This method is used for custom IMAP only.
  266. # It is not used in conjunction with Micrsofot365 or Gogle OAuth channels.
  267. def verify(options, verify_string)
  268. setup_connection(options)
  269. message_ids_result = Timeout.timeout(6.minutes) do
  270. fetch_all_message_ids
  271. end
  272. message_ids = message_ids_result[:result]
  273. Rails.logger.info "verify mode, fetch no emails #{verify_string}"
  274. # check for verify message
  275. message_ids.reverse_each do |message_id|
  276. message_meta = nil
  277. Timeout.timeout(FETCH_METADATA_TIMEOUT) do
  278. message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0]
  279. end
  280. # check if verify message exists
  281. headers = self.class.extract_rfc822_headers(message_meta)
  282. subject = headers['Subject']
  283. next if !subject
  284. next if !subject.match?(%r{#{verify_string}})
  285. Rails.logger.info " - verify email #{verify_string} found"
  286. Timeout.timeout(600) do
  287. @imap.store(message_id, '+FLAGS', [:Deleted])
  288. @imap.expunge
  289. end
  290. disconnect
  291. return {
  292. result: 'ok',
  293. }
  294. end
  295. disconnect
  296. {
  297. result: 'verify not ok',
  298. }
  299. end
  300. def fetch_all_message_ids
  301. fetch_message_ids %w[ALL]
  302. end
  303. def fetch_unread_message_ids
  304. fetch_message_ids %w[NOT SEEN]
  305. rescue
  306. fetch_message_ids %w[UNSEEN]
  307. end
  308. def fetch_message_ids(filter)
  309. raise if @imap.capabilities.exclude?('SORT')
  310. {
  311. result: @imap.sort(['DATE'], filter, 'US-ASCII'),
  312. is_fallback: false
  313. }
  314. rescue
  315. {
  316. result: @imap.search(filter),
  317. is_fallback: true # indicates that we can not use a result ordered by date
  318. }
  319. end
  320. def fetch_message_body_key(options)
  321. # https://github.com/zammad/zammad/issues/4589
  322. options['host'] == 'imap.mail.me.com' ? 'BODY[]' : 'RFC822'
  323. end
  324. def disconnect
  325. return if !@imap
  326. Timeout.timeout(1.minute) do
  327. @imap.disconnect
  328. end
  329. end
  330. # Parses RFC822 header
  331. # @param [String] RFC822 header text blob
  332. # @return [Hash<String=>String>]
  333. def self.parse_rfc822_headers(string)
  334. array = string
  335. .gsub("\r\n\t", ' ') # Some servers (e.g. microsoft365) may put attribute value on a separate line and tab it
  336. .lines(chomp: true)
  337. .map { |line| line.split(%r{:\s*}, 2).map(&:strip) }
  338. array.each { |elem| elem.append(nil) if elem.one? }
  339. Hash[*array.flatten]
  340. end
  341. # Parses RFC822 header
  342. # @param [Net::IMAP::FetchData] fetched message
  343. # @return [Hash<String=>String>]
  344. def self.extract_rfc822_headers(message_meta)
  345. blob = message_meta&.attr&.dig 'RFC822.HEADER'
  346. return if !blob
  347. parse_rfc822_headers blob
  348. end
  349. private
  350. =begin
  351. check if email is already marked as deleted
  352. Channel::Driver::IMAP.deleted?(message_meta, count, count_all)
  353. returns
  354. true|false
  355. =end
  356. def deleted?(message_meta, count, count_all)
  357. return false if message_meta.attr['FLAGS'].exclude?(:Deleted)
  358. Rails.logger.info " - ignore message #{count}/#{count_all} - because message has already delete flag"
  359. true
  360. end
  361. =begin
  362. check if maximal fetching email count has reached
  363. Channel::Driver::IMAP.max_process_count_was_reached?(channel, count, count_max)
  364. returns
  365. true|false
  366. =end
  367. def max_process_count_was_reached?(channel, count, count_max)
  368. return false if count < count_max
  369. Rails.logger.info "Maximal fetched emails (#{count_max}) reached for this interval for Channel with id #{channel.id}."
  370. true
  371. end
  372. def setup_connection(options, check: false)
  373. ssl = true
  374. ssl_verify = options.fetch(:ssl_verify, true)
  375. starttls = false
  376. keep_on_server = false
  377. folder = 'INBOX'
  378. if options[:keep_on_server] == true || options[:keep_on_server] == 'true'
  379. keep_on_server = true
  380. end
  381. case options[:ssl]
  382. when 'off'
  383. ssl = false
  384. when 'starttls'
  385. ssl = false
  386. starttls = true
  387. end
  388. port = if options.key?(:port) && options[:port].present?
  389. options[:port].to_i
  390. elsif ssl == true
  391. 993
  392. else
  393. 143
  394. end
  395. if options[:folder].present?
  396. folder = options[:folder]
  397. end
  398. 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')})"
  399. # on check, reduce open_timeout to have faster probing
  400. check_type_timeout = check ? CHECK_ONLY_TIMEOUT : DEFAULT_TIMEOUT
  401. Certificate::ApplySSLCertificates.ensure_fresh_ssl_context if ssl || starttls
  402. Timeout.timeout(check_type_timeout) do
  403. ssl_settings = false
  404. ssl_settings = (ssl_verify ? true : { verify_mode: OpenSSL::SSL::VERIFY_NONE }) if ssl
  405. @imap = ::Net::IMAP.new(options[:host], port: port, ssl: ssl_settings)
  406. if starttls
  407. @imap.starttls(verify_mode: ssl_verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE)
  408. end
  409. end
  410. Timeout.timeout(check_type_timeout) do
  411. if options[:auth_type].present?
  412. @imap.authenticate(options[:auth_type], options[:user], options[:password])
  413. else
  414. @imap.login(options[:user], options[:password].dup&.force_encoding('ascii-8bit'))
  415. end
  416. end
  417. Timeout.timeout(check_type_timeout) do
  418. # select folder
  419. @imap.select(folder)
  420. end
  421. end
  422. end