Function: imap-open

imap-open is a byte-compiled function defined in imap.el.gz.

Signature

(imap-open SERVER &optional PORT STREAM AUTH BUFFER)

Documentation

Open an IMAP connection to host SERVER at PORT returning a buffer.

If PORT is unspecified, a default value is used (143 except for SSL which use 993). STREAM indicates the stream to use, see imap-streams for available streams. If nil, it choices the best stream the server is capable of. AUTH indicates authenticator to use, see imap-authenticators for available authenticators. If nil, it choices the best stream the server is capable of. BUFFER can be a buffer or a name of a buffer, which is created if necessary. If nil, the buffer name is generated.

Probably introduced at or before Emacs version 31.1.

Source Code

;; Defined in /usr/src/emacs/lisp/net/imap.el.gz
(defun imap-open (server &optional port stream auth buffer)
  "Open an IMAP connection to host SERVER at PORT returning a buffer.
If PORT is unspecified, a default value is used (143 except
for SSL which use 993).
STREAM indicates the stream to use, see `imap-streams' for available
streams.  If nil, it choices the best stream the server is capable of.
AUTH indicates authenticator to use, see `imap-authenticators' for
available authenticators.  If nil, it choices the best stream the
server is capable of.
BUFFER can be a buffer or a name of a buffer, which is created if
necessary.  If nil, the buffer name is generated."
  (setq buffer (or buffer (format " *imap* %s:%d" server (or port 0))))
  (with-current-buffer (get-buffer-create buffer)
    (if (imap-opened buffer)
	(imap-close buffer))
    (mapc #'make-local-variable imap-local-variables)
    (set-buffer-multibyte nil)
    (buffer-disable-undo)
    (setq imap-server (or server imap-server))
    (setq imap-port (or port imap-port))
    (setq imap-auth (or auth imap-auth))
    (setq imap-stream (or stream imap-stream))
    (message "imap: Connecting to %s..." imap-server)
    (if (null (let ((imap-stream (or imap-stream imap-default-stream)))
		(imap-open-1 buffer)))
	(progn
	  (message "imap: Connecting to %s...failed" imap-server)
	  nil)
      (when (null imap-stream)
	;; Need to choose stream.
	(let ((streams imap-streams))
	  (while (setq stream (pop streams))
	    ;; OK to use this stream?
	    (when (funcall (nth 1 (assq stream imap-stream-alist)) buffer)
	      ;; Stream changed?
	      (if (not (eq imap-default-stream stream))
                  (with-current-buffer (generate-new-buffer " *temp*")
		    (mapc #'make-local-variable imap-local-variables)
		    (set-buffer-multibyte nil)
		    (buffer-disable-undo)
		    (setq imap-server (or server imap-server))
		    (setq imap-port (or port imap-port))
		    (setq imap-auth (or auth imap-auth))
		    (message "imap: Reconnecting with stream `%s'..." stream)
		    (if (null (let ((imap-stream stream))
				(imap-open-1 (current-buffer))))
			(progn
			  (kill-buffer (current-buffer))
			  (message
			   "imap: Reconnecting with stream `%s'...failed"
			   stream))
		      ;; We're done, kill the first connection
		      (imap-close buffer)
		      (let ((name (if (stringp buffer)
				      buffer
				    (buffer-name buffer))))
			(kill-buffer buffer)
			(rename-buffer name)
			;; set the passed buffer to the current one,
			;; so that (imap-opened buffer) later will work
			(setq buffer (current-buffer)))
		      (message "imap: Reconnecting with stream `%s'...done"
			       stream)
		      (setq imap-stream stream)
		      (setq imap-capability nil)
		      (setq streams nil)))
		;; We're done
		(message "imap: Connecting to %s...done" imap-server)
		(setq imap-stream stream)
		(setq imap-capability nil)
		(setq streams nil))))))
      (when (imap-opened buffer)
	(setq imap-mailbox-data (obarray-make imap-mailbox-prime)))
      ;; (debug "opened+state+auth+buffer" (imap-opened buffer) imap-state imap-auth buffer)
      (when imap-stream
	buffer))))