1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
52 (autoload 'auth-source-forget-user-or-password "auth-source")
53 (autoload 'auth-source-user-or-password "auth-source")
57 (defvoo nnimap-address nil
58 "The address of the IMAP server.")
60 (defvoo nnimap-server-port nil
62 If nnimap-stream is `ssl', this will default to `imaps'. If not,
63 it will default to `imap'.")
65 (defvoo nnimap-stream 'ssl
66 "How nnimap will talk to the IMAP server.
67 Values are `ssl', `network', `starttls' or `shell'.")
69 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
70 (if (listp imap-shell-program)
71 (car imap-shell-program)
75 (defvoo nnimap-inbox nil
76 "The mail box where incoming mail arrives and should be split out of.")
78 (defvoo nnimap-split-methods nil
80 Uses the same syntax as nnmail-split-methods")
82 (defvoo nnimap-split-fancy nil
83 "Uses the same syntax as nnmail-split-fancy.")
85 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
86 "Articles with the flags in the list will not be considered when splitting.")
88 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
91 (defvoo nnimap-authenticator nil
92 "How nnimap authenticate itself to the server.
93 Possible choices are nil (use default methods) or `anonymous'.")
95 (defvoo nnimap-expunge t
96 "If non-nil, expunge articles after deleting them.
97 This is always done if the server supports UID EXPUNGE, but it's
98 not done by default on servers that doesn't support that command.")
100 (defvoo nnimap-streaming t
101 "If non-nil, try to use streaming commands with IMAP servers.
102 Switching this off will make nnimap slower, but it helps with
105 (defvoo nnimap-connection-alist nil)
107 (defvoo nnimap-current-infos nil)
109 (defvoo nnimap-fetch-partial-articles nil
110 "If non-nil, Gnus will fetch partial articles.
111 If t, nnimap will fetch only the first part. If a string, it
112 will fetch all parts that have types that match that string. A
113 likely value would be \"text/\" to automatically fetch all
116 (defvar nnimap-process nil)
118 (defvar nnimap-status-string "")
120 (defvar nnimap-split-download-body-default nil
121 "Internal variable with default value for `nnimap-split-download-body'.")
123 (defvar nnimap-keepalive-timer nil)
124 (defvar nnimap-process-buffers nil)
127 group process commands capabilities select-result newlinep server
128 last-command-time greeting examined)
130 (defvar nnimap-object nil)
132 (defvar nnimap-mark-alist
133 '((read "\\Seen" %Seen)
134 (tick "\\Flagged" %Flagged)
135 (reply "\\Answered" %Answered)
136 (expire "gnus-expire")
137 (dormant "gnus-dormant")
140 (download "gnus-download")
141 (forward "gnus-forward")))
143 (defun nnimap-buffer ()
144 (nnimap-find-process-buffer nntp-server-buffer))
146 (defun nnimap-header-parameters ()
147 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
150 "BODY.PEEK[HEADER.FIELDS %s]"
151 "RFC822.HEADER.LINES %s")
152 (append '(Subject From Date Message-Id
153 References In-Reply-To Xref)
154 nnmail-extra-headers))))
156 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
157 (with-current-buffer nntp-server-buffer
159 (when (nnimap-possibly-change-group group server)
160 (with-current-buffer (nnimap-buffer)
162 (nnimap-wait-for-response
165 (nnimap-article-ranges (gnus-compress-sequence articles))
166 (nnimap-header-parameters))
168 (nnimap-transform-headers))
169 (insert-buffer-substring
170 (nnimap-find-process-buffer (current-buffer))))
173 (defun nnimap-transform-headers ()
174 (goto-char (point-min))
175 (let (article bytes lines size string)
178 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
179 (delete-region (point) (progn (forward-line 1) (point)))
182 (setq article (match-string 1))
183 ;; Unfold quoted {number} strings.
184 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
185 (1+ (line-end-position)) t)
186 (setq size (string-to-number (match-string 1)))
187 (delete-region (+ (match-beginning 0) 2) (point))
188 (setq string (buffer-substring (point) (+ (point) size)))
189 (delete-region (point) (+ (point) size))
190 (insert (format "%S" string)))
191 (setq bytes (nnimap-get-length)
195 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
200 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
201 (let ((structure (ignore-errors
202 (read (current-buffer)))))
203 (while (and (consp structure)
204 (not (stringp (car structure))))
205 (setq structure (car structure)))
206 (setq lines (nth 7 structure))))
207 (delete-region (line-beginning-position) (line-end-position))
208 (insert (format "211 %s Article retrieved." article))
211 (insert (format "Chars: %s\n" size)))
213 (insert (format "Lines: %s\n" lines)))
214 (unless (re-search-forward "^\r$" nil t)
215 (goto-char (point-max)))
216 (delete-region (line-beginning-position) (line-end-position))
220 (defun nnimap-unfold-quoted-lines ()
221 ;; Unfold quoted {number} strings.
223 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
224 (setq size (string-to-number (match-string 1)))
225 (delete-region (1+ (match-beginning 0)) (point))
226 (setq string (buffer-substring (point) (+ (point) size)))
227 (delete-region (point) (+ (point) size))
228 (insert (format "%S" string)))))
230 (defun nnimap-get-length ()
231 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
232 (string-to-number (match-string 1))))
234 (defun nnimap-article-ranges (ranges)
238 (number-to-string ranges))
239 ((numberp (cdr ranges))
240 (format "%d:%d" (car ranges) (cdr ranges)))
242 (dolist (elem ranges)
245 (format "%d:%d" (car elem) (cdr elem))
246 (number-to-string elem))
248 (mapconcat #'identity (nreverse result) ",")))))
250 (deffoo nnimap-open-server (server &optional defs)
251 (if (nnimap-server-opened server)
253 (unless (assq 'nnimap-address defs)
254 (setq defs (append defs (list (list 'nnimap-address server)))))
255 (nnoo-change-server 'nnimap server defs)
256 (or (nnimap-find-connection nntp-server-buffer)
257 (nnimap-open-connection nntp-server-buffer))))
259 (defun nnimap-make-process-buffer (buffer)
261 (generate-new-buffer (format "*nnimap %s %s %s*"
262 nnimap-address nnimap-server-port
263 (gnus-buffer-exists-p buffer)))
264 (mm-disable-multibyte)
265 (buffer-disable-undo)
267 (set (make-local-variable 'after-change-functions) nil)
268 (set (make-local-variable 'nnimap-object)
269 (make-nnimap :server (nnoo-current-server 'nnimap)))
270 (push (list buffer (current-buffer)) nnimap-connection-alist)
271 (push (current-buffer) nnimap-process-buffers)
274 (defun nnimap-open-shell-stream (name buffer host port)
275 (let ((process-connection-type nil))
276 (start-process name buffer shell-file-name
284 (defun nnimap-credentials (address ports &optional inhibit-create)
285 (let (port credentials)
286 ;; Request the credentials from all ports, but only query on the
287 ;; last port if all the previous ones have failed.
288 (while (and (null credentials)
289 (setq port (pop ports)))
291 (auth-source-user-or-password
292 '("login" "password") address port nil
298 (defun nnimap-keepalive ()
299 (let ((now (current-time)))
300 (dolist (buffer nnimap-process-buffers)
301 (when (buffer-name buffer)
302 (with-current-buffer buffer
303 (when (and nnimap-object
304 (nnimap-last-command-time nnimap-object)
308 (nnimap-last-command-time nnimap-object)))
309 ;; More than five minutes since the last command.
311 (nnimap-send-command "NOOP")))))))
313 (declare-function gnutls-negotiate "gnutls"
314 (proc type &optional priority-string trustfiles keyfiles))
316 (defun nnimap-open-connection (buffer)
317 (unless nnimap-keepalive-timer
318 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
321 (with-current-buffer (nnimap-make-process-buffer buffer)
322 (let* ((coding-system-for-read 'binary)
323 (coding-system-for-write 'binary)
327 ((or (eq nnimap-stream 'network)
328 (and (eq nnimap-stream 'starttls)
329 (fboundp 'open-gnutls-stream)))
330 (nnheader-message 7 "Opening connection to %s..."
333 "*nnimap*" (current-buffer) nnimap-address
335 (or nnimap-server-port
336 (if (netrc-find-service-number "imap")
340 ((eq nnimap-stream 'shell)
341 (nnheader-message 7 "Opening connection to %s via shell..."
343 (nnimap-open-shell-stream
344 "*nnimap*" (current-buffer) nnimap-address
345 (setq port (or nnimap-server-port "imap")))
347 ((eq nnimap-stream 'starttls)
348 (nnheader-message 7 "Opening connection to %s via starttls..."
351 '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -starttls imap")))
353 "*nnimap*" (current-buffer) nnimap-address
354 (setq port (or nnimap-server-port "imap"))))
356 ((memq nnimap-stream '(ssl tls))
357 (nnheader-message 7 "Opening connection to %s via tls..."
359 (funcall (if (fboundp 'open-gnutls-stream)
362 "*nnimap*" (current-buffer) nnimap-address
364 (or nnimap-server-port
365 (if (netrc-find-service-number "imaps")
368 '("143" "993" "imap" "imaps"))
370 (error "Unknown stream type: %s" nnimap-stream))))
371 connection-result login-result credentials)
372 (setf (nnimap-process nnimap-object)
373 (get-buffer-process (current-buffer)))
374 (if (not (and (nnimap-process nnimap-object)
375 (memq (process-status (nnimap-process nnimap-object))
377 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
378 nnimap-address port nnimap-stream)
379 (gnus-set-process-query-on-exit-flag
380 (nnimap-process nnimap-object) nil)
381 (if (not (setq connection-result (nnimap-wait-for-connection)))
382 (nnheader-report 'nnimap
383 "%s" (buffer-substring
384 (point) (line-end-position)))
385 ;; Store the greeting (for debugging purposes).
386 (setf (nnimap-greeting nnimap-object)
387 (buffer-substring (line-beginning-position)
388 (line-end-position)))
389 (nnimap-get-capabilities)
390 (when nnimap-server-port
391 (push (format "%s" nnimap-server-port) ports))
392 ;; If this is a STARTTLS-capable server, then sever the
393 ;; connection and start a STARTTLS connection instead.
395 ((and (or (and (eq nnimap-stream 'network)
396 (nnimap-capability "STARTTLS"))
397 (eq nnimap-stream 'starttls))
398 (fboundp 'open-gnutls-stream))
399 (nnimap-command "STARTTLS")
400 (gnutls-negotiate (nnimap-process nnimap-object) nil)
401 ;; Get the capabilities again -- they may have changed
402 ;; after doing STARTTLS.
403 (nnimap-get-capabilities))
404 ((and (eq nnimap-stream 'network)
405 (nnimap-capability "STARTTLS"))
406 (let ((nnimap-stream 'starttls))
408 (nnimap-open-connection buffer)))
409 ;; If the STARTTLS connection was successful, we
410 ;; kill our first non-encrypted connection. If it
411 ;; wasn't successful, we just use our unencrypted
413 (when (memq (process-status tls-process) '(open run))
414 (delete-process (nnimap-process nnimap-object))
415 (kill-buffer (current-buffer))
416 (return tls-process))))))
417 (unless (equal connection-result "PREAUTH")
418 (if (not (setq credentials
419 (if (eq nnimap-authenticator 'anonymous)
421 (message-make-address))
423 ;; First look for the credentials based
424 ;; on the virtual server name.
426 (nnoo-current-server 'nnimap) ports t)
427 ;; Then look them up based on the
429 (nnimap-credentials nnimap-address ports)))))
430 (setq nnimap-object nil)
432 (if (and (nnimap-capability "AUTH=PLAIN")
433 (nnimap-capability "LOGINDISABLED"))
435 "AUTHENTICATE PLAIN %s"
436 (base64-encode-string
437 (format "\000%s\000%s"
438 (nnimap-quote-specials (car credentials))
439 (nnimap-quote-specials (cadr credentials)))))
440 (nnimap-command "LOGIN %S %S"
442 (cadr credentials))))
443 (unless (car login-result)
444 ;; If the login failed, then forget the credentials
445 ;; that are now possibly cached.
446 (dolist (host (list (nnoo-current-server 'nnimap)
449 (dolist (element '("login" "password"))
450 (auth-source-forget-user-or-password
451 element host port))))
452 (delete-process (nnimap-process nnimap-object))
453 (setq nnimap-object nil))))
455 (when (nnimap-capability "QRESYNC")
456 (nnimap-command "ENABLE QRESYNC"))
457 (nnimap-process nnimap-object))))))))
459 (defun nnimap-get-capabilities ()
460 (setf (nnimap-capabilities nnimap-object)
463 (nnimap-find-parameter
464 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
466 (defun nnimap-quote-specials (string)
469 (goto-char (point-min))
470 (while (re-search-forward "[\\\"]" nil t)
476 (defun nnimap-find-parameter (parameter elems)
480 ((equal (car elem) parameter)
481 (setq result (cdr elem)))
482 ((and (equal (car elem) "OK")
484 (equal (caadr elem) parameter))
485 (setq result (cdr (cadr elem))))))
488 (deffoo nnimap-close-server (&optional server)
489 (when (nnoo-change-server 'nnimap server nil)
491 (delete-process (get-buffer-process (nnimap-buffer))))
492 (nnoo-close-server 'nnimap server)
495 (deffoo nnimap-request-close ()
498 (deffoo nnimap-server-opened (&optional server)
499 (and (nnoo-current-server-p 'nnimap server)
501 (gnus-buffer-live-p nntp-server-buffer)
502 (nnimap-find-connection nntp-server-buffer)))
504 (deffoo nnimap-status-message (&optional server)
505 nnimap-status-string)
507 (deffoo nnimap-request-article (article &optional group server to-buffer)
508 (with-current-buffer nntp-server-buffer
509 (let ((result (nnimap-possibly-change-group group server))
511 (when (stringp article)
512 (setq article (nnimap-find-article-by-message-id group article)))
516 (with-current-buffer (nnimap-buffer)
518 (when nnimap-fetch-partial-articles
519 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
520 (goto-char (point-min))
521 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
522 (setq structure (ignore-errors
523 (let ((start (point)))
525 (downcase-region start (point))
527 (read (current-buffer))))
528 parts (nnimap-find-wanted-parts structure))))
530 (nnimap-get-partial-article article parts structure)
531 (nnimap-get-whole-article article))
532 (let ((buffer (current-buffer)))
533 (with-current-buffer (or to-buffer nntp-server-buffer)
535 (insert-buffer-substring buffer)
536 (nnheader-ms-strip-cr)
537 (cons group article)))))))))
539 (deffoo nnimap-request-head (article &optional group server to-buffer)
540 (when (nnimap-possibly-change-group group server)
541 (with-current-buffer (nnimap-buffer)
542 (when (stringp article)
543 (setq article (nnimap-find-article-by-message-id group article)))
544 (nnimap-get-whole-article
545 article (format "UID FETCH %%d %s"
546 (nnimap-header-parameters)))
547 (let ((buffer (current-buffer)))
548 (with-current-buffer (or to-buffer nntp-server-buffer)
550 (insert-buffer-substring buffer)
551 (nnheader-ms-strip-cr)
552 (cons group article))))))
554 (defun nnimap-get-whole-article (article &optional command)
559 "UID FETCH %d BODY.PEEK[]"
560 "UID FETCH %d RFC822.PEEK"))
562 ;; Check that we really got an article.
563 (goto-char (point-min))
564 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
567 ;; Remove any data that may have arrived before the FETCH data.
570 (delete-region (point-min) (point)))
571 (let ((bytes (nnimap-get-length)))
572 (delete-region (line-beginning-position)
573 (progn (forward-line 1) (point)))
574 (goto-char (+ (point) bytes))
575 (delete-region (point) (point-max)))
578 (defun nnimap-capability (capability)
579 (member capability (nnimap-capabilities nnimap-object)))
581 (defun nnimap-ver4-p ()
582 (nnimap-capability "IMAP4REV1"))
584 (defun nnimap-get-partial-article (article parts structure)
587 "UID FETCH %d (%s %s)"
593 (mapconcat (lambda (part)
594 (format "BODY.PEEK[%s]" part))
596 (mapconcat (lambda (part)
597 (format "RFC822.PEEK[%s]" part))
600 (nnimap-convert-partial-article structure))))
602 (defun nnimap-convert-partial-article (structure)
603 ;; First just skip past the headers.
604 (goto-char (point-min))
605 (let ((bytes (nnimap-get-length))
607 ;; Delete "FETCH" line.
608 (delete-region (line-beginning-position)
609 (progn (forward-line 1) (point)))
610 (goto-char (+ (point) bytes))
611 ;; Collect all the body parts.
612 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
613 (setq id (match-string 1)
614 bytes (nnimap-get-length))
616 (delete-region (point) (progn (forward-line 1) (point)))
617 (push (list id (buffer-substring (point) (+ (point) bytes)))
619 (delete-region (point) (+ (point) bytes)))
620 ;; Delete trailing junk.
621 (delete-region (point) (point-max))
622 ;; Now insert all the parts again where they fit in the structure.
623 (nnimap-insert-partial-structure structure parts)
626 (defun nnimap-insert-partial-structure (structure parts &optional subp)
628 (let ((bstruc structure))
629 (while (consp (car bstruc))
631 (setq type (car bstruc))
632 (setq bstruc (car (cdr bstruc)))
633 (let ((has-boundary (member "boundary" bstruc)))
635 (setq boundary (cadr has-boundary)))))
637 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
638 (downcase type) boundary)))
639 (while (not (stringp (car structure)))
640 (insert "\n--" boundary "\n")
641 (if (consp (caar structure))
642 (nnimap-insert-partial-structure (pop structure) parts t)
643 (let ((bit (pop structure)))
644 (insert (format "Content-type: %s/%s"
645 (downcase (nth 0 bit))
646 (downcase (nth 1 bit))))
647 (if (member "CHARSET" (nth 2 bit))
649 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
651 (insert (format "Content-transfer-encoding: %s\n"
654 (when (assoc (nth 9 bit) parts)
655 (insert (cadr (assoc (nth 9 bit) parts)))))))
656 (insert "\n--" boundary "--\n")))
658 (defun nnimap-find-wanted-parts (structure)
659 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
661 (defun nnimap-find-wanted-parts-1 (structure prefix)
664 (while (consp (car structure))
665 (let ((sub (pop structure)))
666 (if (consp (car sub))
667 (push (nnimap-find-wanted-parts-1
668 sub (if (string= prefix "")
669 (number-to-string num)
670 (format "%s.%s" prefix num)))
672 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
673 (id (if (string= prefix "")
674 (number-to-string num)
675 (format "%s.%s" prefix num))))
676 (setcar (nthcdr 9 sub) id)
677 (when (if (eq nnimap-fetch-partial-articles t)
679 (string-match nnimap-fetch-partial-articles type))
684 (deffoo nnimap-request-group (group &optional server dont-check info)
685 (let ((result (nnimap-possibly-change-group
686 ;; Don't SELECT the group if we're going to select it
689 (assoc group nnimap-current-infos))
693 articles active marks high low)
694 (with-current-buffer nntp-server-buffer
697 (setq active (nth 2 (assoc group nnimap-current-infos))))
698 (insert (format "211 %d %d %d %S\n"
699 (- (cdr active) (car active))
703 (with-current-buffer (nnimap-buffer)
705 (let ((group-sequence
706 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
708 (nnimap-send-command "UID FETCH 1:* FLAGS")))
709 (setf (nnimap-group nnimap-object) group)
710 (nnimap-wait-for-response flag-sequence)
712 (nnimap-flags-to-marks
714 (list (list group-sequence flag-sequence
715 1 group "SELECT")))))
718 (nnimap-update-infos marks (list info))
719 (nnimap-store-info info (gnus-active (gnus-info-group info))))
720 (goto-char (point-max))
721 (let ((uidnext (nth 5 (car marks))))
722 (setq high (or (if uidnext
726 low (or (nth 4 (car marks)) uidnext 1)))))
730 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
733 (deffoo nnimap-request-create-group (group &optional server args)
734 (when (nnimap-possibly-change-group nil server)
735 (with-current-buffer (nnimap-buffer)
736 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
738 (deffoo nnimap-request-delete-group (group &optional force server)
739 (when (nnimap-possibly-change-group nil server)
740 (with-current-buffer (nnimap-buffer)
741 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
743 (deffoo nnimap-request-rename-group (group new-name &optional server)
744 (when (nnimap-possibly-change-group nil server)
745 (with-current-buffer (nnimap-buffer)
746 (nnimap-unselect-group)
747 (car (nnimap-command "RENAME %S %S"
748 (utf7-encode group t) (utf7-encode new-name t))))))
750 (defun nnimap-unselect-group ()
751 ;; Make sure we don't have this group open read/write by asking
752 ;; to examine a mailbox that doesn't exist. This seems to be
753 ;; the only way that allows us to reliably go back to unselected
755 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
757 (deffoo nnimap-request-expunge-group (group &optional server)
758 (when (nnimap-possibly-change-group group server)
759 (with-current-buffer (nnimap-buffer)
760 (car (nnimap-command "EXPUNGE")))))
762 (defun nnimap-get-flags (spec)
765 (with-current-buffer (nnimap-buffer)
767 (nnimap-wait-for-response (nnimap-send-command
768 "UID FETCH %s FLAGS" spec))
770 (subst-char-in-region (point-min) (point-max)
772 (goto-char (point-min))
773 (while (search-forward " FETCH " end t)
774 (setq elems (read (current-buffer)))
775 (push (cons (cadr (memq 'UID elems))
776 (cadr (memq 'FLAGS elems)))
778 (nreverse articles)))
780 (deffoo nnimap-close-group (group &optional server)
783 (deffoo nnimap-request-move-article (article group server accept-form
784 &optional last internal-move-group)
786 (mm-disable-multibyte)
787 (when (funcall (if internal-move-group
789 'nnimap-request-article)
790 article group server (current-buffer))
791 ;; If the move is internal (on the same server), just do it the easy
793 (let ((message-id (message-field-value "message-id")))
794 (if internal-move-group
796 (with-current-buffer (nnimap-buffer)
797 (nnimap-command "UID COPY %d %S"
799 (utf7-encode internal-move-group t)))))
801 (nnimap-delete-article article)
802 (cons internal-move-group
803 (or (nnimap-find-uid-response "COPYUID" (cadr result))
804 (nnimap-find-article-by-message-id
805 internal-move-group message-id)))))
806 ;; Move the article to a different method.
807 (let ((result (eval accept-form)))
809 (nnimap-delete-article article)
812 (deffoo nnimap-request-expire-articles (articles group &optional server force)
816 ((not (nnimap-possibly-change-group group server))
819 (eq nnmail-expiry-target 'delete))
820 (unless (nnimap-delete-article (gnus-compress-sequence articles))
821 (nnheader-message 7 "Article marked for deletion, but not expunged."))
824 (let ((deletable-articles
826 (eq nnmail-expiry-wait 'immediate))
828 (gnus-sorted-intersection
830 (nnimap-find-expired-articles group)))))
831 (if (null deletable-articles)
833 (if (eq nnmail-expiry-target 'delete)
834 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
835 (setq deletable-articles
836 (nnimap-process-expiry-targets
837 deletable-articles group server)))
838 ;; Return the articles we didn't delete.
839 (gnus-sorted-complement articles deletable-articles))))))
841 (defun nnimap-process-expiry-targets (articles group server)
842 (let ((deleted-articles nil))
843 (dolist (article articles)
844 (let ((target nnmail-expiry-target))
846 (mm-disable-multibyte)
847 (when (nnimap-request-article article group server (current-buffer))
848 (nnheader-message 7 "Expiring article %s:%d" group article)
849 (when (functionp target)
850 (setq target (funcall target group)))
852 (not (eq target 'delete)))
853 (if (or (gnus-request-group target t)
854 (gnus-request-create-group target))
855 (nnmail-expiry-target-group target group)
858 (push article deleted-articles))))))
859 ;; Change back to the current group again.
860 (nnimap-possibly-change-group group server)
861 (setq deleted-articles (nreverse deleted-articles))
862 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
865 (defun nnimap-find-expired-articles (group)
866 (let ((cutoff (nnmail-expired-article-p group nil nil)))
867 (with-current-buffer (nnimap-buffer)
870 "UID SEARCH SENTBEFORE %s"
874 (car (rassoc (nth 4 (decode-time cutoff))
875 parse-time-months))))
878 (delete 0 (mapcar #'string-to-number
879 (cdr (assoc "SEARCH" (cdr result))))))))))
882 (defun nnimap-find-article-by-message-id (group message-id)
883 (with-current-buffer (nnimap-buffer)
885 (unless (equal group (nnimap-group nnimap-object))
886 (setf (nnimap-group nnimap-object) nil)
887 (setf (nnimap-examined nnimap-object) group)
888 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
890 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
892 (setq result (nnimap-wait-for-response sequence))
894 (car (setq result (nnimap-parse-response))))
895 ;; Select the last instance of the message in the group.
897 (car (last (assoc "SEARCH" (cdr result)))))
898 (string-to-number article))))))
900 (defun nnimap-delete-article (articles)
901 (with-current-buffer (nnimap-buffer)
902 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
903 (nnimap-article-ranges articles))
905 ((nnimap-capability "UIDPLUS")
906 (nnimap-command "UID EXPUNGE %s"
907 (nnimap-article-ranges articles))
910 (nnimap-command "EXPUNGE")
912 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
913 "server doesn't support UIDPLUS, so we won't "
914 "delete this article now"))))))
916 (deffoo nnimap-request-scan (&optional group server)
917 (when (and (nnimap-possibly-change-group nil server)
919 nnimap-split-methods)
920 (nnheader-message 7 "nnimap %s splitting mail..." server)
921 (nnimap-split-incoming-mail)))
923 (defun nnimap-marks-to-flags (marks)
926 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
930 (deffoo nnimap-request-set-mark (group actions &optional server)
931 (when (nnimap-possibly-change-group group server)
933 (with-current-buffer (nnimap-buffer)
935 ;; Just send all the STORE commands without waiting for
936 ;; response. If they're successful, they're successful.
937 (dolist (action actions)
938 (destructuring-bind (range action marks) action
939 (let ((flags (nnimap-marks-to-flags marks)))
941 (setq sequence (nnimap-send-command
942 "UID STORE %s %sFLAGS.SILENT (%s)"
943 (nnimap-article-ranges range)
945 ((eq action 'del) "-")
946 ((eq action 'add) "+")
947 ((eq action 'set) ""))
948 (mapconcat #'identity flags " ")))))))
949 ;; Wait for the last command to complete to avoid later
950 ;; syncronisation problems with the stream.
952 (nnimap-wait-for-response sequence))))))
954 (deffoo nnimap-request-accept-article (group &optional server last)
955 (when (nnimap-possibly-change-group nil server)
956 (nnmail-check-syntax)
957 (let ((message-id (message-field-value "message-id"))
960 (setq message (buffer-substring-no-properties (point-min) (point-max)))
961 (with-current-buffer (nnimap-buffer)
962 ;; If we have this group open read-only, then unselect it
963 ;; before appending to it.
964 (when (equal (nnimap-examined nnimap-object) group)
965 (nnimap-unselect-group))
967 (setq sequence (nnimap-send-command
968 "APPEND %S {%d}" (utf7-encode group t)
970 (unless nnimap-streaming
971 (nnimap-wait-for-connection "^[+]"))
972 (process-send-string (get-buffer-process (current-buffer)) message)
973 (process-send-string (get-buffer-process (current-buffer))
974 (if (nnimap-newlinep nnimap-object)
977 (let ((result (nnimap-get-response sequence)))
978 (if (not (car result))
980 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
983 (or (nnimap-find-uid-response "APPENDUID" (car result))
984 (nnimap-find-article-by-message-id
985 group message-id)))))))))
987 (defun nnimap-find-uid-response (name list)
988 (let ((result (car (last (nnimap-find-response-element name list)))))
990 (string-to-number result))))
992 (defun nnimap-find-response-element (name list)
995 (when (and (consp elem)
996 (equal name (car elem)))
1000 (deffoo nnimap-request-replace-article (article group buffer)
1002 (when (and (nnimap-possibly-change-group group nil)
1003 ;; Put the article into the group.
1004 (with-current-buffer buffer
1006 (nnimap-request-accept-article group nil t))))
1007 (nnimap-delete-article (list article))
1008 ;; Return the new article number.
1011 (defun nnimap-add-cr ()
1012 (goto-char (point-min))
1013 (while (re-search-forward "\r?\n" nil t)
1014 (replace-match "\r\n" t t)))
1016 (defun nnimap-get-groups ()
1018 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1020 (nnimap-wait-for-response sequence)
1021 (subst-char-in-region (point-min) (point-max)
1023 (goto-char (point-min))
1024 (nnimap-unfold-quoted-lines)
1025 (goto-char (point-min))
1026 (while (search-forward "* LIST " nil t)
1027 (let ((flags (read (current-buffer)))
1028 (separator (read (current-buffer)))
1029 (group (read (current-buffer))))
1030 (unless (member '%NoSelect flags)
1031 (push (if (stringp group)
1033 (format "%s" group))
1037 (deffoo nnimap-request-list (&optional server)
1038 (nnimap-possibly-change-group nil server)
1039 (with-current-buffer nntp-server-buffer
1042 (with-current-buffer (nnimap-buffer)
1043 (nnimap-get-groups)))
1044 sequences responses)
1046 (with-current-buffer (nnimap-buffer)
1047 (setf (nnimap-group nnimap-object) nil)
1048 (dolist (group groups)
1049 (setf (nnimap-examined nnimap-object) group)
1050 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
1053 (nnimap-wait-for-response (caar sequences))
1055 (nnimap-get-responses (mapcar #'car sequences))))
1056 (dolist (response responses)
1057 (let* ((sequence (car response))
1058 (response (cadr response))
1059 (group (cadr (assoc sequence sequences))))
1061 (equal (caar response) "OK"))
1062 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1064 (dolist (elem response)
1065 (when (equal (cadr elem) "EXISTS")
1066 (setq exists (string-to-number (car elem)))))
1068 (setq highest (1- (string-to-number (car uidnext)))))
1071 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1074 (insert (format "%S %d %d y\n"
1075 (utf7-decode group t) highest (1+ highest))))
1077 ;; Return the widest possible range.
1078 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1079 (or highest exists)))))))))
1082 (deffoo nnimap-request-newgroups (date &optional server)
1083 (nnimap-possibly-change-group nil server)
1084 (with-current-buffer nntp-server-buffer
1086 (dolist (group (with-current-buffer (nnimap-buffer)
1087 (nnimap-get-groups)))
1088 (unless (assoc group nnimap-current-infos)
1089 ;; Insert dummy numbers here -- they don't matter.
1090 (insert (format "%S 0 1 y\n" group))))
1093 (deffoo nnimap-retrieve-group-data-early (server infos)
1094 (when (nnimap-possibly-change-group nil server)
1095 (with-current-buffer (nnimap-buffer)
1097 (setf (nnimap-group nnimap-object) nil)
1098 (let ((qresyncp (nnimap-capability "QRESYNC"))
1099 params groups sequences active uidvalidity modseq group)
1100 ;; Go through the infos and gather the data needed to know
1101 ;; what and how to request the data.
1102 (dolist (info infos)
1103 (setq params (gnus-info-params info)
1104 group (gnus-group-real-name (gnus-info-group info))
1105 active (cdr (assq 'active params))
1106 uidvalidity (cdr (assq 'uidvalidity params))
1107 modseq (cdr (assq 'modseq params)))
1108 (setf (nnimap-examined nnimap-object) group)
1113 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1114 (utf7-encode group t)
1120 (if (and active uidvalidity)
1121 ;; Fetch the last 100 flags.
1122 (max 1 (- (cdr active) 100))
1127 ;; If we don't have a UIDVALIDITY, then this is
1128 ;; the first time we've seen the group, so we
1129 ;; have to do a SELECT (which is slower than an
1130 ;; examine), but will tell us whether the group
1131 ;; is read-only or not.
1133 (push (list (nnimap-send-command "%s %S" command
1134 (utf7-encode group t))
1135 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1136 start group command)
1140 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1141 (when (and sequences
1142 (nnimap-possibly-change-group nil server))
1143 (with-current-buffer (nnimap-buffer)
1144 ;; Wait for the final data to trickle in.
1145 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1149 ;; Now we should have most of the data we need, no matter
1150 ;; whether we're QRESYNCING, fetching all the flags from
1151 ;; scratch, or just fetching the last 100 flags per group.
1152 (nnimap-update-infos (nnimap-flags-to-marks
1154 (nreverse sequences)))
1156 ;; Finally, just return something resembling an active file in
1157 ;; the nntp buffer, so that the agent can save the info, too.
1158 (with-current-buffer nntp-server-buffer
1160 (dolist (info infos)
1161 (let* ((group (gnus-info-group info))
1162 (active (gnus-active group)))
1164 (insert (format "%S %d %d y\n"
1165 (gnus-group-real-name group)
1167 (car active)))))))))))
1169 (defun nnimap-update-infos (flags infos)
1170 (dolist (info infos)
1171 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1172 (marks (cdr (assoc group flags))))
1174 (nnimap-update-info info marks)))))
1176 (defun nnimap-update-info (info marks)
1177 (destructuring-bind (existing flags high low uidnext start-article
1178 permanent-flags uidvalidity
1179 vanished highestmodseq) marks
1181 ;; Ignore groups with no UIDNEXT/marks. This happens for
1182 ;; completely empty groups.
1183 ((and (not existing)
1185 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1187 (gnus-set-active (gnus-info-group info) active))))
1188 ;; We have a mismatch between the old and new UIDVALIDITY
1189 ;; identifiers, so we have to re-request the group info (the next
1190 ;; time). This virtually never happens.
1191 ((let ((old-uidvalidity
1192 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1193 (and old-uidvalidity
1194 (not (equal old-uidvalidity uidvalidity))
1195 (> start-article 1)))
1196 (gnus-group-remove-parameter info 'uidvalidity)
1197 (gnus-group-remove-parameter info 'modseq))
1198 ;; We have the data needed to update.
1200 (let* ((group (gnus-info-group info))
1201 (completep (and start-article
1202 (= start-article 1)))
1203 (active (or (gnus-active group)
1204 (cdr (assq 'active (gnus-info-params info))))))
1206 (setq high (1- uidnext)))
1207 ;; First set the active ranges based on high/low.
1209 (not (gnus-active group)))
1210 (gnus-set-active group
1213 (cons (min (or low (car active))
1215 (max (or high (cdr active))
1220 ;; No articles in this group.
1221 (cons uidnext (1- uidnext)))
1223 (cons start-article (1- start-article)))
1225 ;; No articles and no uidnext.
1230 (or high (1- uidnext)))))
1231 ;; See whether this is a read-only group.
1232 (unless (eq permanent-flags 'not-scanned)
1233 (gnus-group-set-parameter
1234 info 'permanent-flags
1235 (and (or (memq '%* permanent-flags)
1236 (memq '%Seen permanent-flags))
1238 ;; Update marks and read articles if this isn't a
1239 ;; read-only IMAP group.
1240 (when (setq permanent-flags
1241 (cdr (assq 'permanent-flags (gnus-info-params info))))
1242 (if (and highestmodseq
1243 (not start-article))
1244 ;; We've gotten the data by QRESYNCing.
1245 (nnimap-update-qresync-info
1246 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1247 ;; Do normal non-QRESYNC flag updates.
1248 ;; Update the list of read articles.
1250 (gnus-compress-sequence
1251 (gnus-set-difference
1252 (gnus-set-difference
1254 (cdr (assoc '%Seen flags)))
1255 (cdr (assoc '%Flagged flags)))))
1256 (read (gnus-range-difference
1257 (cons start-article high) unread)))
1258 (when (> start-article 1)
1261 (if (> start-article 1)
1262 (gnus-sorted-range-intersection
1263 (cons 1 (1- start-article))
1264 (gnus-info-read info))
1265 (gnus-info-read info))
1267 (when (or (not (listp permanent-flags))
1268 (memq '%Seen permanent-flags))
1269 (gnus-info-set-read info read))
1270 ;; Update the marks.
1271 (setq marks (gnus-info-marks info))
1272 (dolist (type (cdr nnimap-mark-alist))
1273 (when (or (not (listp permanent-flags))
1274 (memq (car (assoc (caddr type) flags))
1276 (memq '%* permanent-flags))
1277 (let ((old-marks (assoc (car type) marks))
1279 (gnus-compress-sequence
1280 (cdr (or (assoc (caddr type) flags) ; %Flagged
1281 (assoc (intern (cadr type) obarray) flags)
1282 (assoc (cadr type) flags)))))) ; "\Flagged"
1283 (setq marks (delq old-marks marks))
1285 (when (and old-marks
1286 (> start-article 1))
1287 (setq old-marks (gnus-range-difference
1289 (cons start-article high)))
1290 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1292 (push (cons (car type) new-marks) marks)))))
1293 (gnus-info-set-marks info marks t))))
1294 ;; Note the active level for the next run-through.
1295 (gnus-group-set-parameter info 'active (gnus-active group))
1296 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1297 (gnus-group-set-parameter info 'modseq highestmodseq)
1298 (nnimap-store-info info (gnus-active group)))))))
1300 (defun nnimap-update-qresync-info (info existing vanished flags)
1301 ;; Add all the vanished articles to the list of read articles.
1306 (gnus-range-add (gnus-info-read info)
1308 (cdr (assq '%Flagged flags)))
1309 (cdr (assq '%Seen flags))))
1310 (let ((marks (gnus-info-marks info)))
1311 (dolist (type (cdr nnimap-mark-alist))
1312 (let ((ticks (assoc (car type) marks))
1314 (cdr (or (assoc (caddr type) flags) ; %Flagged
1315 (assoc (intern (cadr type) obarray) flags)
1316 (assoc (cadr type) flags))))) ; "\Flagged"
1317 (setq marks (delq ticks marks))
1319 ;; Add the new marks we got.
1320 (setq ticks (gnus-add-to-range ticks new-marks))
1321 ;; Remove the marks from messages that don't have them.
1322 (setq ticks (gnus-remove-from-range
1324 (gnus-compress-sequence
1325 (gnus-sorted-complement existing new-marks))))
1327 (push (cons (car type) ticks) marks)))
1328 (gnus-info-set-marks info marks t))))
1330 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1331 (if (zerop (length irange))
1334 (dolist (elem (split-string irange ","))
1336 (if (string-match ":" elem)
1337 (let ((numbers (split-string elem ":")))
1338 (cons (string-to-number (car numbers))
1339 (string-to-number (cadr numbers))))
1340 (string-to-number elem))
1342 (nreverse result))))
1344 (defun nnimap-store-info (info active)
1345 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1346 (entry (assoc group nnimap-current-infos)))
1348 (setcdr entry (list info active))
1349 (push (list group info active) nnimap-current-infos))))
1351 (defun nnimap-flags-to-marks (groups)
1352 (let (data group totalp uidnext articles start-article mark permanent-flags
1353 uidvalidity vanished highestmodseq)
1354 (dolist (elem groups)
1355 (setq group (car elem)
1356 uidnext (nth 1 elem)
1357 start-article (nth 2 elem)
1358 permanent-flags (nth 3 elem)
1359 uidvalidity (nth 4 elem)
1360 vanished (nth 5 elem)
1361 highestmodseq (nth 6 elem)
1362 articles (nthcdr 7 elem))
1363 (let ((high (caar articles))
1365 (dolist (article articles)
1366 (setq low (car article))
1367 (push (car article) existing)
1368 (dolist (flag (cdr article))
1369 (setq mark (assoc flag marks))
1371 (push (list flag (car article)) marks)
1372 (setcdr mark (cons (car article) (cdr mark))))))
1373 (push (list group existing marks high low uidnext start-article
1374 permanent-flags uidvalidity vanished highestmodseq)
1378 (defun nnimap-parse-flags (sequences)
1379 (goto-char (point-min))
1380 ;; Change \Delete etc to %Delete, so that the reader can read it.
1381 (subst-char-in-region (point-min) (point-max)
1383 (let (start end articles groups uidnext elems permanent-flags
1384 uidvalidity vanished highestmodseq)
1385 (dolist (elem sequences)
1386 (destructuring-bind (group-sequence flag-sequence totalp group command)
1388 (setq start (point))
1390 ;; The EXAMINE was successful.
1391 (search-forward (format "\n%d OK " group-sequence) nil t)
1396 (setq permanent-flags
1397 (if (equal command "SELECT")
1398 (and (search-forward "PERMANENTFLAGS "
1399 (or end (point-min)) t)
1400 (read (current-buffer)))
1404 (and (search-forward "UIDNEXT "
1405 (or end (point-min)) t)
1406 (read (current-buffer))))
1409 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1410 (or end (point-min)) t)
1411 ;; Store UIDVALIDITY as a string, as it's
1412 ;; too big for 32-bit Emacsen, usually.
1416 (and (eq flag-sequence 'qresync)
1417 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1418 (or end (point-min)) t)
1422 (and (search-forward "HIGHESTMODSEQ "
1423 (or end (point-min)) t)
1424 (read (current-buffer))))
1427 ;; The UID FETCH FLAGS was successful.
1428 (or (eq flag-sequence 'qresync)
1429 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1430 (if (eq flag-sequence 'qresync)
1434 (setq start (point))
1436 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1437 (setq elems (read (current-buffer)))
1438 (push (cons (cadr (memq 'UID elems))
1439 (cadr (memq 'FLAGS elems)))
1441 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1442 vanished highestmodseq)
1446 (setq articles nil))))
1449 (defun nnimap-find-process-buffer (buffer)
1450 (cadr (assoc buffer nnimap-connection-alist)))
1452 (deffoo nnimap-request-post (&optional server)
1453 (setq nnimap-status-string "Read-only server")
1456 (deffoo nnimap-request-thread (id)
1457 (let* ((refs (split-string
1458 (or (mail-header-references (gnus-summary-article-header))
1462 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1464 (dolist (refid refs value)
1466 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1467 refid refid value)))))
1468 (result (with-current-buffer (nnimap-buffer)
1469 (nnimap-command "UID SEARCH %s" cmd))))
1471 (and (car result) (delete 0 (mapcar #'string-to-number
1472 (cdr (assoc "SEARCH" (cdr result))))))
1475 (defun nnimap-possibly-change-group (group server)
1476 (let ((open-result t))
1478 (not (nnimap-server-opened server)))
1479 (setq open-result (nnimap-open-server server)))
1486 (with-current-buffer (nnimap-buffer)
1487 (if (equal group (nnimap-group nnimap-object))
1489 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1491 (setf (nnimap-group nnimap-object) group
1492 (nnimap-select-result nnimap-object) result)
1495 (defun nnimap-find-connection (buffer)
1496 "Find the connection delivering to BUFFER."
1497 (let ((entry (assoc buffer nnimap-connection-alist)))
1499 (if (and (buffer-name (cadr entry))
1500 (get-buffer-process (cadr entry))
1501 (memq (process-status (get-buffer-process (cadr entry)))
1503 (get-buffer-process (cadr entry))
1504 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1507 (defvar nnimap-sequence 0)
1509 (defun nnimap-send-command (&rest args)
1510 (process-send-string
1511 (get-buffer-process (current-buffer))
1514 (incf nnimap-sequence)
1515 (apply #'format args)
1516 (if (nnimap-newlinep nnimap-object)
1519 ;; Some servers apparently can't have many outstanding
1520 ;; commands, so throttle them.
1521 (unless nnimap-streaming
1522 (nnimap-wait-for-response nnimap-sequence))
1525 (defun nnimap-log-command (command)
1526 (with-current-buffer (get-buffer-create "*imap log*")
1527 (goto-char (point-max))
1528 (insert (format-time-string "%H:%M:%S") " " command))
1531 (defun nnimap-command (&rest args)
1533 (setf (nnimap-last-command-time nnimap-object) (current-time))
1534 (let* ((sequence (apply #'nnimap-send-command args))
1535 (response (nnimap-get-response sequence)))
1536 (if (equal (caar response) "OK")
1538 (nnheader-report 'nnimap "%s"
1539 (mapconcat (lambda (a)
1541 (car response) " "))
1544 (defun nnimap-get-response (sequence)
1545 (nnimap-wait-for-response sequence)
1546 (nnimap-parse-response))
1548 (defun nnimap-wait-for-connection (&optional regexp)
1550 (setq regexp "^[*.] .*\n"))
1551 (let ((process (get-buffer-process (current-buffer))))
1552 (goto-char (point-min))
1553 (while (and (memq (process-status process)
1555 (not (re-search-forward regexp nil t)))
1556 (nnheader-accept-process-output process)
1557 (goto-char (point-min)))
1559 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1562 (defun nnimap-wait-for-response (sequence &optional messagep)
1563 (let ((process (get-buffer-process (current-buffer)))
1567 (goto-char (point-max))
1568 (while (and (setq openp (memq (process-status process)
1570 (not (re-search-backward
1571 (format "^%d .*\n" sequence)
1572 (if nnimap-streaming
1573 (max (point-min) (- (point) 500))
1577 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1578 (nnheader-accept-process-output process)
1579 (goto-char (point-max)))
1582 ;; The user hit C-g while we were waiting: kill the process, in case
1583 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1585 (delete-process process)
1588 (defun nnimap-parse-response ()
1589 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1591 (dolist (line lines)
1592 (push (cdr (nnimap-parse-line line)) result))
1593 ;; Return the OK/error code first, and then all the "continuation
1594 ;; lines" afterwards.
1596 (nreverse result))))
1598 ;; Parse an IMAP response line lightly. They look like
1599 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1600 ;; the lines into a list of strings and lists of string.
1601 (defun nnimap-parse-line (line)
1604 (mm-disable-multibyte)
1606 (goto-char (point-min))
1608 (if (eql (setq char (following-char)) ? )
1616 (if (search-forward "]" (line-end-position) 'move)
1623 (if (search-forward ")" (line-end-position) 'move)
1630 (1- (or (search-forward "\"" (line-end-position) 'move)
1633 (buffer-substring (point) (if (search-forward " " nil t)
1635 (goto-char (point-max))))))
1637 (nreverse result))))
1639 (defun nnimap-last-response-string ()
1642 (let ((end (point)))
1646 (while (and (not (bobp))
1647 (eql (following-char) ?*))
1649 (unless (eql (following-char) ?*)
1651 (buffer-substring (point) end))))
1653 (defun nnimap-get-responses (sequences)
1655 (dolist (sequence sequences)
1656 (goto-char (point-min))
1657 (when (re-search-forward (format "^%d " sequence) nil t)
1658 (push (list sequence (nnimap-parse-response))
1662 (defvar nnimap-incoming-split-list nil)
1664 (defun nnimap-fetch-inbox (articles)
1666 (nnimap-wait-for-response
1667 (nnimap-send-command
1669 (nnimap-article-ranges articles)
1670 (format "(UID %s%s)"
1673 "BODY.PEEK[HEADER] BODY.PEEK"
1675 (if nnimap-split-download-body-default
1680 (defun nnimap-split-incoming-mail ()
1681 (with-current-buffer (nnimap-buffer)
1682 (let ((nnimap-incoming-split-list nil)
1683 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1684 nnmail-split-methods
1685 nnimap-split-methods))
1686 (nnmail-split-fancy (or nnimap-split-fancy
1687 nnmail-split-fancy))
1688 (nnmail-inhibit-default-split-group t)
1689 (groups (nnimap-get-groups))
1692 (nnimap-command "SELECT %S" nnimap-inbox)
1693 (setf (nnimap-group nnimap-object) nnimap-inbox)
1694 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1696 (nnimap-fetch-inbox new-articles)
1697 (nnimap-transform-split-mail)
1698 (nnheader-ms-strip-cr)
1700 (nnmail-split-incoming (current-buffer)
1701 #'nnimap-save-mail-spec
1703 #'nnimap-dummy-active-number
1704 #'nnimap-save-mail-spec)
1705 (when nnimap-incoming-split-list
1706 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1707 sequences junk-articles)
1708 ;; Create any groups that doesn't already exist on the
1710 (dolist (spec specs)
1711 (when (and (not (member (car spec) groups))
1712 (not (eq (car spec) 'junk)))
1713 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1714 ;; Then copy over all the messages.
1716 (dolist (spec specs)
1717 (let ((group (car spec))
1718 (ranges (cdr spec)))
1719 (if (eq group 'junk)
1720 (setq junk-articles ranges)
1721 (push (list (nnimap-send-command
1723 (nnimap-article-ranges ranges)
1724 (utf7-encode group t))
1727 ;; Wait for the last COPY response...
1729 (nnimap-wait-for-response (caar sequences))
1730 ;; And then mark the successful copy actions as deleted,
1731 ;; and possibly expunge them.
1732 (nnimap-mark-and-expunge-incoming
1733 (nnimap-parse-copied-articles sequences)))
1734 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1736 (defun nnimap-mark-and-expunge-incoming (range)
1738 (setq range (nnimap-article-ranges range))
1741 (nnimap-send-command
1742 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1744 ;; If the server supports it, we now delete the message we have
1745 ;; just copied over.
1746 ((nnimap-capability "UIDPLUS")
1747 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1748 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1749 ;; user has configured it.
1751 (setq sequence (nnimap-send-command "EXPUNGE"))))
1752 (nnimap-wait-for-response sequence))))
1754 (defun nnimap-parse-copied-articles (sequences)
1755 (let (sequence copied range)
1756 (goto-char (point-min))
1757 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1758 (setq sequence (string-to-number (match-string 1)))
1759 (when (setq range (cadr (assq sequence sequences)))
1760 (push (gnus-uncompress-range range) copied)))
1761 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1763 (defun nnimap-new-articles (flags)
1765 (dolist (elem flags)
1766 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1768 (push (car elem) new)))
1769 (gnus-compress-sequence (nreverse new))))
1771 (defun nnimap-make-split-specs (list)
1775 (destructuring-bind (article spec) elem
1776 (dolist (group (delete nil (mapcar #'car spec)))
1777 (unless (setq entry (assoc group specs))
1778 (push (setq entry (list group)) specs))
1779 (setcdr entry (cons article (cdr entry))))))
1780 (dolist (entry specs)
1781 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1784 (defun nnimap-transform-split-mail ()
1785 (goto-char (point-min))
1786 (let (article bytes)
1789 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1790 (delete-region (point) (progn (forward-line 1) (point)))
1793 (setq article (match-string 1)
1794 bytes (nnimap-get-length))
1795 (delete-region (line-beginning-position) (line-end-position))
1796 ;; Insert MMDF separator, and a way to remember what this
1798 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1799 (forward-char (1+ bytes))
1800 (setq bytes (nnimap-get-length))
1801 (delete-region (line-beginning-position) (line-end-position))
1802 ;; There's a body; skip past that.
1804 (forward-char (1+ bytes))
1805 (delete-region (line-beginning-position) (line-end-position)))))))
1807 (defun nnimap-dummy-active-number (group &optional server)
1810 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1812 (goto-char (point-min))
1813 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1814 (error "Invalid nnimap mail")
1815 (setq article (string-to-number (match-string 1))))
1817 (if (eq group-art 'junk)
1818 (list (cons 'junk 1))
1820 nnimap-incoming-split-list)))
1824 ;;; nnimap.el ends here