;;;; Use CL-POP over SSL.

(defpackage cl-pop+ssl
  (:documentation "Use CL-POP over SSL.")
  (:use :cl :cl-pop :usocket :cl+ssl)
  (:export :with-pop+ssl-connection))

(in-package :cl-pop+ssl)

(defmacro with-pop+ssl-connection ((var &key host
                                             (port 995)
                                             username
                                             password
                                             (external-format :utf-8))
                                   &body body)
  "Invoke WITH-POP-CONNECTION using SSL."
  (let ((g!socket (gensym "socket")))
    `(let ((,g!socket (socket-connect ,host ,port)))
       (unwind-protect
            (with-pop-connection
                (,var :stream (make-ssl-client-stream
                               (socket-stream ,g!socket)
                               :external-format ,external-format)
                      :username ,username
                      :password ,password)
              ,@body)
         (socket-close ,g!socket)))))
