Sha256: 7c9a46f6306f6fe4066c2d80b825b8fb009f7ef3afb101f4170ee3149935713c

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'net/imap'

module MList
  module EmailServer

    class Imap < Base
      def initialize(settings)
        super(settings)
      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
        begin
          connect
          process_folders
        ensure
          disconnect
        end
      end

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

      def connect
        @imap = Net::IMAP.new(
          settings[:server],
          settings[:port],
          settings[:ssl]
        )
        @imap.login(settings[:username], settings[:password])
      end

      def disconnect
        @imap.disconnect if @imap && !@imap.disconnected?
      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(['NOT','DELETED']).each do |message_id|
          process_message_id(message_id)
          archive_message_id(message_id)
        end
        @imap.close
      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.16 lib/mlist/email_server/imap.rb