#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

require 'net/imap'

module CyborgHood
  class IMAP
    def initialize(params)
      @params = params
    end

    class IMAPMessage
      def initialize(imap, message_id)
        @imap = imap
        @message_id = message_id

        @config = Config.instance
        @content = nil
      end

      def content
        return @content unless @content.nil?

        data = @imap.fetch(@message_id, ["RFC822"])[0]
        @content = data.attr["RFC822"]
      end

      def delete
	if @config.debug.nil? or @config.debug.flags.nil? or not @config.debug.flags.include?(:debug_nomaildeletion)
          @imap.store(@message_id, "+FLAGS", [:Deleted])
	end
      end
    end

    def check_mail
      # using SSL because TLS does not work in the NET::IMAP library
      ssl = (not @params.ca_cert.nil?)
      port = @params.port || (ssl ? 993 : 143)
      check_ca = ssl
      logger.debug "Connecting to IMAP server..."
      imap = Net::IMAP.new(@params.host, port, ssl, @params.ca_cert, check_ca)
      logger.debug "Connected (IMAP Capabilities: " + imap.capability.join(", ") + ")"

      imap.authenticate('LOGIN', @params.login, @params.passwd)
      logger.debug "Authenticated as '#{@params.login}'"

      #p imap.getquotaroot("INBOX")

      imap.select('INBOX')
      logger.debug "Examining INBOX"

      imap.search(["ALL"], "UTF-8").each do |message_id|
        logger.debug "*** Fetched mail ##{message_id}"
        yield IMAPMessage.new(imap, message_id)
      end

      imap.expunge
      imap.logout
      imap.disconnect
      logger.debug "Disconnected from IMAP server"
    rescue SocketError, Net::IMAP::Error => e
      raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
    end
  end
end
