Sha256: e147122d7b1f8436785c8d6915ad2b6e658d6baf656fed727956ad2889e0f939

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'net/imap'

module MList
  module EmailServer

    class Imap < Base
      def initialize(settings)
        super(settings)
        @imap = Net::IMAP.new(
          settings[:server],
          settings[:port],
          settings[:ssl]
        )
      end

      def deliver(tmail)
        raise "Mail delivery is not presently supported by the Imap server. Please use the '#{MList::EmailServer::Default.name}' type."
      end

      def execute
        connect { process_folders }
      end

      def archive_message_id(id)
        @imap.copy(id, settings[:archive_folder])
        @imap.store(id, '+FLAGS', [:Deleted])
      end

      def connect
        @imap.login(settings[:username], settings[:password])
        begin
          yield
        ensure
          @imap.close
        end
      end

      def process_folders
        Array(settings[:source_folders]).each do |folder|
          process_folder(folder)
        end
      end

      def process_folder(folder)
        @imap.select(folder)
        @imap.search(['ALL']).each do |message_id|
          process_message_id(message_id)
          archive_message_id(message_id)
        end
      end

      def process_message_id(id)
        content = @imap.fetch(id, 'RFC822')[0].attr['RFC822']
        process_message(content)
      end

      def process_message(content)
        receive(TMail::Mail.parse(content))
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mlist-0.1.13 lib/mlist/email_server/imap.rb