(if (or (and done-ok (not done-bad))
                  ;; prevent mitm that fake success msg after failure msg.
                  (and done-ok done-bad (< done-ok done-bad)))
-             info))))))
+             info
+           (message "STARTTLS negotiation failed: %s" info)
+           nil))))))
 
 (defun open-starttls-stream (name buffer host service)
   "Open a TLS connection for a service to a host.
 BUFFER is the buffer (or buffer-name) to associate with the process.
  Process output goes at end of that buffer, unless you specify
  an output stream or filter function to handle the output.
- BUFFER may be also nil, meaning that this process is not associated
- with any buffer
 Third arg is name of the host to connect to, or its IP address.
 Fourth arg SERVICE is name of the service desired, or an integer
 specifying a port number to connect to."
-  (let ((cmds starttls-programs) cmd done)
+  (let ((cmds starttls-programs) cmd done old-max)
     (message "Opening STARTTLS connection to `%s'..." host)
+    (with-current-buffer buffer
+      (setq old-max (point-max)))
     (while (and (not done) (setq cmd (pop cmds)))
       (message "Opening STARTTLS connection with `%s'..." cmd)
       (let* ((process-connection-type starttls-process-connection-type)
        (message "Opening STARTTLS connection with `%s'...%s" cmd
                 (if done "done" "failed"))
        (if done
-           (setq done process)
+           (progn
+             (with-current-buffer buffer
+               (delete-region old-max (point-max)))
+             (setq done process))
          (delete-process process))))
     (message "Opening STARTTLS connection to `%s'...%s"
             host (if done "done" "failed"))
 
+2003-09-20  Simon Josefsson  <jas@extundo.com>
+
+       * imap.el (imap-starttls-open): Erase buffer and disable process
+       filter before invoking STARTTLS negotiation, to support new
+       starttls.el that uses GNUTLS.
+
 2003-09-18  Katsumi Yamaoka  <yamaoka@jpl.org>
 
        * gnus-art.el (gnus-treat-display-x-face): Use set-default instead
 
        (unwind-protect
            (progn
              (set-process-filter imap-process 'imap-arrival-filter)
+             (erase-buffer)
              (when (and (eq imap-stream 'starttls)
                         (imap-ok-p (imap-send-command-wait "STARTTLS")))
+               (set-process-filter imap-process nil)
                (starttls-negotiate imap-process)))
          (set-process-filter imap-process nil)))
       (when (memq (process-status process) '(open run))