<commit_before># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

require 'net/imap'

class Channel::Driver::Imap < Channel::EmailParser

  def fetchable?(_channel)
    true
  end

=begin

fetch emails from IMAP account

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)

returns

  {
    result: 'ok',
    fetched: 123,
    notice: 'e. g. message about to big emails in mailbox',
  }

check if connect to IMAP account is possible, return count of mails in mailbox

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'check')

returns

  {
    result: 'ok',
    content_messages: 123,
  }

verify IMAP account, check if search email is in there

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)

returns

  {
    result: 'ok', # 'verify not ok'
  }

=end

  def fetch (options, channel, check_type = '', verify_string = '')
    ssl  = true
    port = 993
    if options.key?(:ssl) && options[:ssl].to_s == 'false'
      ssl  = false
      port = 143
    end

    Rails.logger.info "fetching imap (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"

    # on check, reduce open_timeout to have faster probing
    timeout = 12
    if check_type == 'check'
      timeout = 6
    end

    Timeout.timeout(timeout) do
      @imap = Net::IMAP.new(options[:host], port, ssl, nil, false)
    end

    # try LOGIN, if not - try plain
    begin
      @imap.authenticate('LOGIN', options[:user], options[:password])
    rescue => e
      if e.to_s !~ /(unsupported\s(authenticate|authentication)\smechanism|not\ssupported)/i
        raise e
      end
      @imap.login(options[:user], options[:password])
    end

    # select folder
    if !options[:folder] || options[:folder].empty?
      @imap.select('INBOX')
    else
      @imap.select(options[:folder])
    end

    message_ids = @imap.search(['ALL'])

    # check mode only
    if check_type == 'check'
      Rails.logger.info 'check only mode, fetch no emails'
      content_max_check = 2
      content_messages  = 0

      # check messages
      message_ids.each do |message_id|

        message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0].attr<commit_msg>Fetch older emails first (to keep possibility for references header follow up detection).<commit_after># Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/

require 'net/imap'

class Channel::Driver::Imap < Channel::EmailParser

  def fetchable?(_channel)
    true
  end

=begin

fetch emails from IMAP account

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)

returns

  {
    result: 'ok',
    fetched: 123,
    notice: 'e. g. message about to big emails in mailbox',
  }

check if connect to IMAP account is possible, return count of mails in mailbox

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'check')

returns

  {
    result: 'ok',
    content_messages: 123,
  }

verify IMAP account, check if search email is in there

  instance = Channel::Driver::Imap.new
  result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)

returns

  {
    result: 'ok', # 'verify not ok'
  }

=end

  def fetch (options, channel, check_type = '', verify_string = '')
    ssl  = true
    port = 993
    if options.key?(:ssl) && options[:ssl].to_s == 'false'
      ssl  = false
      port = 143
    end

    Rails.logger.info "fetching imap (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"

    # on check, reduce open_timeout to have faster probing
    timeout = 12
    if check_type == 'check'
      timeout = 6
    end

    Timeout.timeout(timeout) do
      @imap = Net::IMAP.new(options[:host], port, ssl, nil, false)
    end

    # try LOGIN, if not - try plain
    begin
      @imap.authenticate('LOGIN', options[:user], options[:password])
    rescue => e
      if e.to_s !~ /(unsupported\s(authenticate|authentication)\smechanism|not\ssupported)/i
        raise e
      end
      @imap.login(options[:user], options[:password])
    end

    # select folder
    if !options[:folder] || options[:folder].empty?
      @imap.select('INBOX')
    else
      @imap.select(options[:folder])
    end

    begin
      message_ids = @imap.sort(['DATE'], ['ALL'], 'US-ASCII')
    rescue => e
      Rails.logger.error "Unable to use imap sort: #{e.inspect}, use imap search now"
      message_ids = @imap.search(['ALL'])
    end

    # check mode only
    if check_type == 'check'
      Rails.logger.info 'check only mode, fetch no emails'
      content_max_check = 2
      content_messages  = 0

      # check messages
      message_ids.each do |message_id|

        message_meta = @imap.fetch(message_id, ['RFC822.HEADER'])[0].attr
{'fraction_non_alphanumeric': 0.13270616493194556, 'fraction_numerical': 0.011609287429943955, 'mean_word_length': 3.1921140939597317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794016', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1589, 'n_words': 428}