1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2011 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))))
35 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
36 ;; `make-network-stream'.
37 (unless (fboundp 'open-protocol-stream)
38 (require 'proto-stream)))
53 (autoload 'auth-source-forget+ "auth-source")
54 (autoload 'auth-source-search "auth-source")
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-user nil
62 "Username to use for authentication to the IMAP server.")
64 (defvoo nnimap-server-port nil
66 If nnimap-stream is `ssl', this will default to `imaps'. If not,
67 it will default to `imap'.")
69 (defvoo nnimap-stream 'undecided
70 "How nnimap talks to the IMAP server.
71 The value should be either `undecided', `ssl' or `tls',
72 `network', `starttls', `plain', or `shell'.
74 If the value is `undecided', nnimap tries `ssl' first, then falls
77 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
78 (if (listp imap-shell-program)
79 (car imap-shell-program)
83 (defvoo nnimap-inbox nil
84 "The mail box where incoming mail arrives and should be split out of.
85 For example, \"INBOX\".")
87 (defvoo nnimap-split-methods nil
89 Uses the same syntax as `nnmail-split-methods'.")
91 (defvoo nnimap-split-fancy nil
92 "Uses the same syntax as `nnmail-split-fancy'.")
94 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
95 "Articles with the flags in the list will not be considered when splitting.")
97 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
100 (defvoo nnimap-authenticator nil
101 "How nnimap authenticate itself to the server.
102 Possible choices are nil (use default methods) or `anonymous'.")
104 (defvoo nnimap-expunge t
105 "If non-nil, expunge articles after deleting them.
106 This is always done if the server supports UID EXPUNGE, but it's
107 not done by default on servers that doesn't support that command.")
109 (defvoo nnimap-streaming t
110 "If non-nil, try to use streaming commands with IMAP servers.
111 Switching this off will make nnimap slower, but it helps with
114 (defvoo nnimap-connection-alist nil)
116 (defvoo nnimap-current-infos nil)
118 (defvoo nnimap-fetch-partial-articles nil
119 "If non-nil, Gnus will fetch partial articles.
120 If t, nnimap will fetch only the first part. If a string, it
121 will fetch all parts that have types that match that string. A
122 likely value would be \"text/\" to automatically fetch all
125 (defvar nnimap-process nil)
127 (defvar nnimap-status-string "")
129 (defvar nnimap-split-download-body-default nil
130 "Internal variable with default value for `nnimap-split-download-body'.")
132 (defvar nnimap-keepalive-timer nil)
133 (defvar nnimap-process-buffers nil)
136 group process commands capabilities select-result newlinep server
137 last-command-time greeting examined stream-type)
139 (defvar nnimap-object nil)
141 (defvar nnimap-mark-alist
142 '((read "\\Seen" %Seen)
143 (tick "\\Flagged" %Flagged)
144 (reply "\\Answered" %Answered)
145 (expire "gnus-expire")
146 (dormant "gnus-dormant")
149 (download "gnus-download")
150 (forward "gnus-forward")))
152 (defvar nnimap-quirks
153 '(("QRESYNC" "Zimbra" "QRESYNC ")))
155 (defvar nnimap-inhibit-logging nil)
157 (defun nnimap-buffer ()
158 (nnimap-find-process-buffer nntp-server-buffer))
160 (defun nnimap-header-parameters ()
161 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
164 "BODY.PEEK[HEADER.FIELDS %s]"
165 "RFC822.HEADER.LINES %s")
166 (append '(Subject From Date Message-Id
167 References In-Reply-To Xref)
168 nnmail-extra-headers))))
170 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
171 (with-current-buffer nntp-server-buffer
173 (when (nnimap-possibly-change-group group server)
174 (with-current-buffer (nnimap-buffer)
176 (nnimap-wait-for-response
179 (nnimap-article-ranges (gnus-compress-sequence articles))
180 (nnimap-header-parameters))
182 (nnimap-transform-headers)
183 (nnheader-remove-cr-followed-by-lf))
184 (insert-buffer-substring
185 (nnimap-find-process-buffer (current-buffer))))
188 (defun nnimap-transform-headers ()
189 (goto-char (point-min))
190 (let (article bytes lines size string)
193 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
194 (delete-region (point) (progn (forward-line 1) (point)))
197 (setq article (match-string 1))
198 ;; Unfold quoted {number} strings.
199 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
200 (1+ (line-end-position)) t)
201 (setq size (string-to-number (match-string 1)))
202 (delete-region (+ (match-beginning 0) 2) (point))
203 (setq string (buffer-substring (point) (+ (point) size)))
204 (delete-region (point) (+ (point) size))
205 (insert (format "%S" string)))
206 (setq bytes (nnimap-get-length)
210 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
215 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
216 (let ((structure (ignore-errors
217 (read (current-buffer)))))
218 (while (and (consp structure)
219 (not (atom (car structure))))
220 (setq structure (car structure)))
222 (stringp (car structure))
223 (equal (upcase (nth 0 structure)) "MESSAGE")
224 (equal (upcase (nth 1 structure)) "RFC822"))
226 (nth 7 structure)))))
227 (delete-region (line-beginning-position) (line-end-position))
228 (insert (format "211 %s Article retrieved." article))
231 (insert (format "Chars: %s\n" size)))
233 (insert (format "Lines: %s\n" lines)))
234 (unless (re-search-forward "^\r$" nil t)
235 (goto-char (point-max)))
236 (delete-region (line-beginning-position) (line-end-position))
240 (defun nnimap-unfold-quoted-lines ()
241 ;; Unfold quoted {number} strings.
243 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
244 (setq size (string-to-number (match-string 1)))
245 (delete-region (1+ (match-beginning 0)) (point))
246 (setq string (buffer-substring (point) (+ (point) size)))
247 (delete-region (point) (+ (point) size))
248 (insert (format "%S" string)))))
250 (defun nnimap-get-length ()
251 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
252 (string-to-number (match-string 1))))
254 (defun nnimap-article-ranges (ranges)
258 (number-to-string ranges))
259 ((numberp (cdr ranges))
260 (format "%d:%d" (car ranges) (cdr ranges)))
262 (dolist (elem ranges)
265 (format "%d:%d" (car elem) (cdr elem))
266 (number-to-string elem))
268 (mapconcat #'identity (nreverse result) ",")))))
270 (deffoo nnimap-open-server (server &optional defs)
271 (if (nnimap-server-opened server)
273 (unless (assq 'nnimap-address defs)
274 (setq defs (append defs (list (list 'nnimap-address server)))))
275 (nnoo-change-server 'nnimap server defs)
276 (or (nnimap-find-connection nntp-server-buffer)
277 (nnimap-open-connection nntp-server-buffer))))
279 (defun nnimap-make-process-buffer (buffer)
281 (generate-new-buffer (format "*nnimap %s %s %s*"
282 nnimap-address nnimap-server-port
283 (gnus-buffer-exists-p buffer)))
284 (mm-disable-multibyte)
285 (buffer-disable-undo)
287 (set (make-local-variable 'after-change-functions) nil)
288 (set (make-local-variable 'nnimap-object)
289 (make-nnimap :server (nnoo-current-server 'nnimap)))
290 (push (list buffer (current-buffer)) nnimap-connection-alist)
291 (push (current-buffer) nnimap-process-buffers)
294 (defun nnimap-credentials (address ports user)
295 (let* ((auth-source-creation-prompts
296 '((user . "IMAP user at %h: ")
297 (secret . "IMAP password for %u@%h: ")))
298 (found (nth 0 (auth-source-search :max 1
302 :require '(:user :secret)
305 (list (plist-get found :user)
306 (let ((secret (plist-get found :secret)))
307 (if (functionp secret)
310 (plist-get found :save-function))
313 (defun nnimap-keepalive ()
314 (let ((now (current-time)))
315 (dolist (buffer nnimap-process-buffers)
316 (when (buffer-name buffer)
317 (with-current-buffer buffer
318 (when (and nnimap-object
319 (nnimap-last-command-time nnimap-object)
323 (nnimap-last-command-time nnimap-object)))
324 ;; More than five minutes since the last command.
326 (nnimap-send-command "NOOP")))))))
328 (defun nnimap-open-connection (buffer)
329 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
330 ;; `ssl' when nnimap-server-port was nil. Sort of.
331 (when (and nnimap-server-port
332 (eq nnimap-stream 'undecided))
333 (setq nnimap-stream 'ssl))
335 (if (eq nnimap-stream 'undecided)
336 (loop for type in '(ssl network)
337 for stream = (let ((nnimap-stream type))
338 (nnimap-open-connection-1 buffer))
339 while (eq stream 'no-connect)
340 finally (return stream))
341 (nnimap-open-connection-1 buffer))))
342 (if (eq stream 'no-connect)
346 (defun nnimap-open-connection-1 (buffer)
347 (unless nnimap-keepalive-timer
348 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
350 (with-current-buffer (nnimap-make-process-buffer buffer)
351 (let* ((coding-system-for-read 'binary)
352 (coding-system-for-write 'binary)
356 ((memq nnimap-stream '(network plain starttls))
357 (nnheader-message 7 "Opening connection to %s..."
360 ((eq nnimap-stream 'shell)
361 (nnheader-message 7 "Opening connection to %s via shell..."
364 ((memq nnimap-stream '(ssl tls))
365 (nnheader-message 7 "Opening connection to %s via tls..."
367 '("imaps" "imap" "993" "143"))
369 (error "Unknown stream type: %s" nnimap-stream))))
370 login-result credentials)
371 (when nnimap-server-port
372 (push nnimap-server-port ports))
374 (open-protocol-stream
375 "*nnimap*" (current-buffer) nnimap-address (car ports)
378 :shell-command nnimap-shell-program
379 :capability-command "1 CAPABILITY\r\n"
380 :end-of-command "\r\n"
383 (lambda (capabilities)
384 (when (gnus-string-match-p "STARTTLS" capabilities)
386 (stream (car stream-list))
387 (props (cdr stream-list))
388 (greeting (plist-get props :greeting))
389 (capabilities (plist-get props :capabilities))
390 (stream-type (plist-get props :type)))
391 (when (and stream (not (memq (process-status stream) '(open run))))
393 (setf (nnimap-process nnimap-object) stream)
394 (setf (nnimap-stream-type nnimap-object) stream-type)
397 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
398 nnimap-address port nnimap-stream)
400 (gnus-set-process-query-on-exit-flag stream nil)
401 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
402 (nnheader-report 'nnimap "%s" greeting)
403 ;; Store the greeting (for debugging purposes).
404 (setf (nnimap-greeting nnimap-object) greeting)
405 (setf (nnimap-capabilities nnimap-object)
407 (split-string capabilities)))
408 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
409 (if (not (setq credentials
410 (if (eq nnimap-authenticator 'anonymous)
412 (message-make-address))
413 ;; Look for the credentials based on
414 ;; the virtual server name and the address
416 (gnus-delete-duplicates
419 (nnoo-current-server 'nnimap)))
422 (setq nnimap-object nil)
423 (let ((nnimap-inhibit-logging t))
425 (nnimap-login (car credentials) (cadr credentials))))
426 (if (car login-result)
428 ;; Save the credentials if a save function exists
429 ;; (such a function will only be passed if a new
430 ;; token was created).
431 (when (functionp (nth 2 credentials))
432 (funcall (nth 2 credentials)))
433 ;; See if CAPABILITY is set as part of login
435 (dolist (response (cddr login-result))
436 (when (string= "CAPABILITY" (upcase (car response)))
437 (setf (nnimap-capabilities nnimap-object)
438 (mapcar #'upcase (cdr response))))))
439 ;; If the login failed, then forget the credentials
440 ;; that are now possibly cached.
441 (dolist (host (list (nnoo-current-server 'nnimap)
444 (auth-source-forget+ :host host :port port)))
445 (delete-process (nnimap-process nnimap-object))
446 (setq nnimap-object nil))))
448 (when (nnimap-capability "QRESYNC")
449 (nnimap-command "ENABLE QRESYNC"))
450 (nnimap-process nnimap-object))))))))
452 (autoload 'rfc2104-hash "rfc2104")
454 (defun nnimap-login (user password)
456 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
457 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
458 ;; and we're using an encrypted connection.
459 ((and (not (nnimap-capability "LOGINDISABLED"))
460 (eq (nnimap-stream-type nnimap-object) 'tls))
461 (nnimap-command "LOGIN %S %S" user password))
462 ((nnimap-capability "AUTH=CRAM-MD5")
464 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
465 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
467 (get-buffer-process (current-buffer))
469 (base64-encode-string
471 (rfc2104-hash 'md5 64 16 password
472 (base64-decode-string challenge))))
474 (nnimap-wait-for-response sequence)))
475 ((not (nnimap-capability "LOGINDISABLED"))
476 (nnimap-command "LOGIN %S %S" user password))
477 ((nnimap-capability "AUTH=PLAIN")
479 "AUTHENTICATE PLAIN %s"
480 (base64-encode-string
481 (format "\000%s\000%s"
482 (nnimap-quote-specials user)
483 (nnimap-quote-specials password)))))))
485 (defun nnimap-quote-specials (string)
488 (goto-char (point-min))
489 (while (re-search-forward "[\\\"]" nil t)
495 (defun nnimap-find-parameter (parameter elems)
499 ((equal (car elem) parameter)
500 (setq result (cdr elem)))
501 ((and (equal (car elem) "OK")
503 (equal (caadr elem) parameter))
504 (setq result (cdr (cadr elem))))))
507 (deffoo nnimap-close-server (&optional server)
508 (when (nnoo-change-server 'nnimap server nil)
510 (delete-process (get-buffer-process (nnimap-buffer))))
511 (nnoo-close-server 'nnimap server)
514 (deffoo nnimap-request-close ()
517 (deffoo nnimap-server-opened (&optional server)
518 (and (nnoo-current-server-p 'nnimap server)
520 (gnus-buffer-live-p nntp-server-buffer)
521 (nnimap-find-connection nntp-server-buffer)))
523 (deffoo nnimap-status-message (&optional server)
524 nnimap-status-string)
526 (deffoo nnimap-request-article (article &optional group server to-buffer)
527 (with-current-buffer nntp-server-buffer
528 (let ((result (nnimap-possibly-change-group group server))
530 (when (stringp article)
531 (setq article (nnimap-find-article-by-message-id group article)))
535 (with-current-buffer (nnimap-buffer)
537 (when nnimap-fetch-partial-articles
538 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
539 (goto-char (point-min))
540 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
541 (setq structure (ignore-errors
542 (let ((start (point)))
544 (downcase-region start (point))
546 (read (current-buffer))))
547 parts (nnimap-find-wanted-parts structure))))
549 (nnimap-get-partial-article article parts structure)
550 (nnimap-get-whole-article article))
551 (let ((buffer (current-buffer)))
552 (with-current-buffer (or to-buffer nntp-server-buffer)
553 (nnheader-insert-buffer-substring buffer)
554 (nnheader-ms-strip-cr)))
555 (cons group article)))))))
557 (deffoo nnimap-request-head (article &optional group server to-buffer)
558 (when (nnimap-possibly-change-group group server)
559 (with-current-buffer (nnimap-buffer)
560 (when (stringp article)
561 (setq article (nnimap-find-article-by-message-id group article)))
564 (nnimap-get-whole-article
565 article (format "UID FETCH %%d %s"
566 (nnimap-header-parameters)))
567 (let ((buffer (current-buffer)))
568 (with-current-buffer (or to-buffer nntp-server-buffer)
570 (insert-buffer-substring buffer)
571 (nnheader-ms-strip-cr)
572 (cons group article)))))))
574 (defun nnimap-get-whole-article (article &optional command)
579 "UID FETCH %d BODY.PEEK[]"
580 "UID FETCH %d RFC822.PEEK"))
582 ;; Check that we really got an article.
583 (goto-char (point-min))
584 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
587 ;; Remove any data that may have arrived before the FETCH data.
590 (delete-region (point-min) (point)))
591 (let ((bytes (nnimap-get-length)))
592 (delete-region (line-beginning-position)
593 (progn (forward-line 1) (point)))
594 (goto-char (+ (point) bytes))
595 (delete-region (point) (point-max)))
598 (defun nnimap-capability (capability)
599 (member capability (nnimap-capabilities nnimap-object)))
601 (defun nnimap-ver4-p ()
602 (nnimap-capability "IMAP4REV1"))
604 (defun nnimap-get-partial-article (article parts structure)
607 "UID FETCH %d (%s %s)"
613 (mapconcat (lambda (part)
614 (format "BODY.PEEK[%s]" part))
616 (mapconcat (lambda (part)
617 (format "RFC822.PEEK[%s]" part))
620 (nnimap-convert-partial-article structure))))
622 (defun nnimap-convert-partial-article (structure)
623 ;; First just skip past the headers.
624 (goto-char (point-min))
625 (let ((bytes (nnimap-get-length))
627 ;; Delete "FETCH" line.
628 (delete-region (line-beginning-position)
629 (progn (forward-line 1) (point)))
630 (goto-char (+ (point) bytes))
631 ;; Collect all the body parts.
632 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
633 (setq id (match-string 1)
634 bytes (or (nnimap-get-length) 0))
636 (delete-region (point) (progn (forward-line 1) (point)))
637 (push (list id (buffer-substring (point) (+ (point) bytes)))
639 (delete-region (point) (+ (point) bytes)))
640 ;; Delete trailing junk.
641 (delete-region (point) (point-max))
642 ;; Now insert all the parts again where they fit in the structure.
643 (nnimap-insert-partial-structure structure parts)
646 (defun nnimap-insert-partial-structure (structure parts &optional subp)
648 (let ((bstruc structure))
649 (while (consp (car bstruc))
651 (setq type (car bstruc))
652 (setq bstruc (car (cdr bstruc)))
653 (let ((has-boundary (member "boundary" bstruc)))
655 (setq boundary (cadr has-boundary)))))
657 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
658 (downcase type) boundary)))
659 (while (not (stringp (car structure)))
660 (insert "\n--" boundary "\n")
661 (if (consp (caar structure))
662 (nnimap-insert-partial-structure (pop structure) parts t)
663 (let ((bit (pop structure)))
664 (insert (format "Content-type: %s/%s"
665 (downcase (nth 0 bit))
666 (downcase (nth 1 bit))))
667 (if (member "CHARSET" (nth 2 bit))
669 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
671 (insert (format "Content-transfer-encoding: %s\n"
674 (when (assoc (nth 9 bit) parts)
675 (insert (cadr (assoc (nth 9 bit) parts)))))))
676 (insert "\n--" boundary "--\n")))
678 (defun nnimap-find-wanted-parts (structure)
679 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
681 (defun nnimap-find-wanted-parts-1 (structure prefix)
684 (while (consp (car structure))
685 (let ((sub (pop structure)))
686 (if (consp (car sub))
687 (push (nnimap-find-wanted-parts-1
688 sub (if (string= prefix "")
689 (number-to-string num)
690 (format "%s.%s" prefix num)))
692 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
693 (id (if (string= prefix "")
694 (number-to-string num)
695 (format "%s.%s" prefix num))))
696 (setcar (nthcdr 9 sub) id)
697 (when (if (eq nnimap-fetch-partial-articles t)
699 (string-match nnimap-fetch-partial-articles type))
704 (deffoo nnimap-request-group (group &optional server dont-check info)
705 (let ((result (nnimap-possibly-change-group
706 ;; Don't SELECT the group if we're going to select it
708 (if (and (not dont-check)
709 (assoc group nnimap-current-infos))
713 articles active marks high low)
714 (with-current-buffer nntp-server-buffer
717 (setq active (nth 2 (assoc group nnimap-current-infos))))
718 (insert (format "211 %d %d %d %S\n"
719 (- (cdr active) (car active))
723 (with-current-buffer (nnimap-buffer)
725 (let ((group-sequence
726 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
728 (nnimap-send-command "UID FETCH 1:* FLAGS")))
729 (setf (nnimap-group nnimap-object) group)
730 (nnimap-wait-for-response flag-sequence)
732 (nnimap-flags-to-marks
734 (list (list group-sequence flag-sequence
735 1 group "SELECT")))))
738 (nnimap-update-infos marks (list info))
739 (nnimap-store-info info (gnus-active (gnus-info-group info))))
740 (goto-char (point-max))
741 (let ((uidnext (nth 5 (car marks))))
742 (setq high (or (if uidnext
746 low (or (nth 4 (car marks)) uidnext 1)))))
750 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
753 (deffoo nnimap-request-create-group (group &optional server args)
754 (when (nnimap-possibly-change-group nil server)
755 (with-current-buffer (nnimap-buffer)
756 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
758 (deffoo nnimap-request-delete-group (group &optional force server)
759 (when (nnimap-possibly-change-group nil server)
760 (with-current-buffer (nnimap-buffer)
761 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
763 (deffoo nnimap-request-rename-group (group new-name &optional server)
764 (when (nnimap-possibly-change-group nil server)
765 (with-current-buffer (nnimap-buffer)
766 (nnimap-unselect-group)
767 (car (nnimap-command "RENAME %S %S"
768 (utf7-encode group t) (utf7-encode new-name t))))))
770 (defun nnimap-unselect-group ()
771 ;; Make sure we don't have this group open read/write by asking
772 ;; to examine a mailbox that doesn't exist. This seems to be
773 ;; the only way that allows us to reliably go back to unselected
775 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
777 (deffoo nnimap-request-expunge-group (group &optional server)
778 (when (nnimap-possibly-change-group group server)
779 (with-current-buffer (nnimap-buffer)
780 (car (nnimap-command "EXPUNGE")))))
782 (defun nnimap-get-flags (spec)
785 (with-current-buffer (nnimap-buffer)
787 (nnimap-wait-for-response (nnimap-send-command
788 "UID FETCH %s FLAGS" spec))
790 (subst-char-in-region (point-min) (point-max)
792 (goto-char (point-min))
793 (while (search-forward " FETCH " end t)
794 (setq elems (read (current-buffer)))
795 (push (cons (cadr (memq 'UID elems))
796 (cadr (memq 'FLAGS elems)))
798 (nreverse articles)))
800 (deffoo nnimap-close-group (group &optional server)
803 (deffoo nnimap-request-move-article (article group server accept-form
804 &optional last internal-move-group)
806 (mm-disable-multibyte)
807 (when (funcall (if internal-move-group
809 'nnimap-request-article)
810 article group server (current-buffer))
811 ;; If the move is internal (on the same server), just do it the easy
813 (let ((message-id (message-field-value "message-id")))
814 (if internal-move-group
816 (with-current-buffer (nnimap-buffer)
817 (nnimap-command "UID COPY %d %S"
819 (utf7-encode internal-move-group t)))))
821 (nnimap-delete-article article)
822 (cons internal-move-group
823 (or (nnimap-find-uid-response "COPYUID" (cadr result))
824 (nnimap-find-article-by-message-id
825 internal-move-group message-id)))))
826 ;; Move the article to a different method.
827 (let ((result (eval accept-form)))
829 (nnimap-delete-article article)
832 (deffoo nnimap-request-expire-articles (articles group &optional server force)
836 ((not (nnimap-possibly-change-group group server))
839 (eq nnmail-expiry-target 'delete))
840 (unless (nnimap-delete-article (gnus-compress-sequence articles))
841 (nnheader-message 7 "Article marked for deletion, but not expunged."))
844 (let ((deletable-articles
846 (eq nnmail-expiry-wait 'immediate))
848 (gnus-sorted-intersection
850 (nnimap-find-expired-articles group)))))
851 (if (null deletable-articles)
853 (if (eq nnmail-expiry-target 'delete)
854 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
855 (setq deletable-articles
856 (nnimap-process-expiry-targets
857 deletable-articles group server)))
858 ;; Return the articles we didn't delete.
859 (gnus-sorted-complement articles deletable-articles))))))
861 (defun nnimap-process-expiry-targets (articles group server)
862 (let ((deleted-articles nil))
864 ;; shortcut further processing if we're going to delete the articles
865 ((eq nnmail-expiry-target 'delete)
866 (setq deleted-articles articles)
868 ;; or just move them to another folder on the same IMAP server
869 ((and (not (functionp nnmail-expiry-target))
870 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
871 (gnus-server-to-method
872 (format "nnimap:%s" server))))
873 (and (nnimap-possibly-change-group group server)
874 (with-current-buffer (nnimap-buffer)
875 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
878 (nnimap-article-ranges (gnus-compress-sequence articles))
879 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
880 (setq deleted-articles articles)))
883 (dolist (article articles)
884 (let ((target nnmail-expiry-target))
886 (mm-disable-multibyte)
887 (when (nnimap-request-article article group server (current-buffer))
888 (when (functionp target)
889 (setq target (funcall target group)))
891 (not (eq target 'delete)))
892 (if (or (gnus-request-group target t)
893 (gnus-request-create-group target))
895 (nnmail-expiry-target-group target group)
896 (nnheader-message 7 "Expiring article %s:%d to %s"
897 group article target))
899 (nnheader-message 7 "Expiring article %s:%d" group article))
901 (push article deleted-articles))))))))
902 ;; Change back to the current group again.
903 (nnimap-possibly-change-group group server)
904 (setq deleted-articles (nreverse deleted-articles))
905 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
908 (defun nnimap-find-expired-articles (group)
909 (let ((cutoff (nnmail-expired-article-p group nil nil)))
910 (with-current-buffer (nnimap-buffer)
913 "UID SEARCH SENTBEFORE %s"
917 (car (rassoc (nth 4 (decode-time cutoff))
918 parse-time-months))))
921 (delete 0 (mapcar #'string-to-number
922 (cdr (assoc "SEARCH" (cdr result))))))))))
925 (defun nnimap-find-article-by-message-id (group message-id)
926 (with-current-buffer (nnimap-buffer)
928 (unless (equal group (nnimap-group nnimap-object))
929 (setf (nnimap-group nnimap-object) nil)
930 (setf (nnimap-examined nnimap-object) group)
931 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
933 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
935 (setq result (nnimap-wait-for-response sequence))
937 (car (setq result (nnimap-parse-response))))
938 ;; Select the last instance of the message in the group.
940 (car (last (cdr (assoc "SEARCH" (cdr result))))))
941 (string-to-number article))))))
943 (defun nnimap-delete-article (articles)
944 (with-current-buffer (nnimap-buffer)
945 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
946 (nnimap-article-ranges articles))
948 ((nnimap-capability "UIDPLUS")
949 (nnimap-command "UID EXPUNGE %s"
950 (nnimap-article-ranges articles))
953 (nnimap-command "EXPUNGE")
955 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
956 "server doesn't support UIDPLUS, so we won't "
957 "delete this article now"))))))
959 (deffoo nnimap-request-scan (&optional group server)
960 (when (and (nnimap-possibly-change-group nil server)
962 nnimap-split-methods)
963 (nnheader-message 7 "nnimap %s splitting mail..." server)
964 (nnimap-split-incoming-mail)
965 (nnheader-message 7 "nnimap %s splitting mail...done" server)))
967 (defun nnimap-marks-to-flags (marks)
970 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
974 (deffoo nnimap-request-update-group-status (group status &optional server)
975 (when (nnimap-possibly-change-group nil server)
976 (let ((command (assoc
978 '((subscribe "SUBSCRIBE")
979 (unsubscribe "UNSUBSCRIBE")))))
981 (with-current-buffer (nnimap-buffer)
982 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
984 (deffoo nnimap-request-set-mark (group actions &optional server)
985 (when (nnimap-possibly-change-group group server)
987 (with-current-buffer (nnimap-buffer)
989 ;; Just send all the STORE commands without waiting for
990 ;; response. If they're successful, they're successful.
991 (dolist (action actions)
992 (destructuring-bind (range action marks) action
993 (let ((flags (nnimap-marks-to-flags marks)))
995 (setq sequence (nnimap-send-command
996 "UID STORE %s %sFLAGS.SILENT (%s)"
997 (nnimap-article-ranges range)
999 ((eq action 'del) "-")
1000 ((eq action 'add) "+")
1001 ((eq action 'set) ""))
1002 (mapconcat #'identity flags " ")))))))
1003 ;; Wait for the last command to complete to avoid later
1004 ;; syncronisation problems with the stream.
1006 (nnimap-wait-for-response sequence))))))
1008 (deffoo nnimap-request-accept-article (group &optional server last)
1009 (when (nnimap-possibly-change-group nil server)
1010 (nnmail-check-syntax)
1011 (let ((message-id (message-field-value "message-id"))
1014 (setq message (buffer-substring-no-properties (point-min) (point-max)))
1015 (with-current-buffer (nnimap-buffer)
1016 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
1018 ;; If we have this group open read-only, then unselect it
1019 ;; before appending to it.
1020 (when (equal (nnimap-examined nnimap-object) group)
1021 (nnimap-unselect-group))
1023 (setq sequence (nnimap-send-command
1024 "APPEND %S {%d}" (utf7-encode group t)
1026 (unless nnimap-streaming
1027 (nnimap-wait-for-connection "^[+]"))
1028 (process-send-string (get-buffer-process (current-buffer)) message)
1029 (process-send-string (get-buffer-process (current-buffer))
1030 (if (nnimap-newlinep nnimap-object)
1033 (let ((result (nnimap-get-response sequence)))
1034 (if (not (nnimap-ok-p result))
1036 (nnheader-report 'nnimap "%s" result)
1039 (or (nnimap-find-uid-response "APPENDUID" (car result))
1040 (nnimap-find-article-by-message-id
1041 group message-id))))))))))
1043 (defun nnimap-process-quirk (greeting-match type data)
1044 (when (and (nnimap-greeting nnimap-object)
1045 (string-match greeting-match (nnimap-greeting nnimap-object))
1047 (string-match "\000" data))
1048 (let ((choice (gnus-multiple-choice
1049 "Message contains NUL characters. Delete, continue, abort? "
1050 '((?d "Delete NUL characters")
1051 (?c "Try to APPEND the message as is")
1055 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1061 (goto-char (point-min))
1062 (while (search-forward "\000" nil t)
1063 (replace-match "" t t))
1064 (buffer-string)))))))
1066 (defun nnimap-ok-p (value)
1069 (equal (caar value) "OK")))
1071 (defun nnimap-find-uid-response (name list)
1072 (let ((result (car (last (nnimap-find-response-element name list)))))
1074 (string-to-number result))))
1076 (defun nnimap-find-response-element (name list)
1079 (when (and (consp elem)
1080 (equal name (car elem)))
1081 (setq result elem)))
1084 (deffoo nnimap-request-replace-article (article group buffer)
1086 (when (and (nnimap-possibly-change-group group nil)
1087 ;; Put the article into the group.
1088 (with-current-buffer buffer
1090 (nnimap-request-accept-article group nil t))))
1091 (nnimap-delete-article (list article))
1092 ;; Return the new article number.
1095 (defun nnimap-add-cr ()
1096 (goto-char (point-min))
1097 (while (re-search-forward "\r?\n" nil t)
1098 (replace-match "\r\n" t t)))
1100 (defun nnimap-get-groups ()
1102 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1104 (nnimap-wait-for-response sequence)
1105 (subst-char-in-region (point-min) (point-max)
1107 (goto-char (point-min))
1108 (nnimap-unfold-quoted-lines)
1109 (goto-char (point-min))
1110 (while (search-forward "* LIST " nil t)
1111 (let ((flags (read (current-buffer)))
1112 (separator (read (current-buffer)))
1113 (group (read (current-buffer))))
1114 (unless (member '%NoSelect flags)
1115 (push (utf7-decode (if (stringp group)
1117 (format "%s" group)) t)
1121 (deffoo nnimap-request-list (&optional server)
1122 (when (nnimap-possibly-change-group nil server)
1123 (with-current-buffer nntp-server-buffer
1126 (with-current-buffer (nnimap-buffer)
1127 (nnimap-get-groups)))
1128 sequences responses)
1130 (with-current-buffer (nnimap-buffer)
1131 (setf (nnimap-group nnimap-object) nil)
1132 (dolist (group groups)
1133 (setf (nnimap-examined nnimap-object) group)
1134 (push (list (nnimap-send-command "EXAMINE %S"
1135 (utf7-encode group t))
1138 (nnimap-wait-for-response (caar sequences))
1140 (nnimap-get-responses (mapcar #'car sequences))))
1141 (dolist (response responses)
1142 (let* ((sequence (car response))
1143 (response (cadr response))
1144 (group (cadr (assoc sequence sequences))))
1146 (equal (caar response) "OK"))
1147 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1149 (dolist (elem response)
1150 (when (equal (cadr elem) "EXISTS")
1151 (setq exists (string-to-number (car elem)))))
1153 (setq highest (1- (string-to-number (car uidnext)))))
1156 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1159 (insert (format "%S %d %d y\n"
1160 (utf7-decode group t)
1161 highest (1+ highest))))
1163 ;; Return the widest possible range.
1164 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1165 (or highest exists)))))))))
1168 (deffoo nnimap-request-newgroups (date &optional server)
1169 (when (nnimap-possibly-change-group nil server)
1170 (with-current-buffer nntp-server-buffer
1172 (dolist (group (with-current-buffer (nnimap-buffer)
1173 (nnimap-get-groups)))
1174 (unless (assoc group nnimap-current-infos)
1175 ;; Insert dummy numbers here -- they don't matter.
1176 (insert (format "%S 0 1 y\n" (utf7-encode group)))))
1179 (deffoo nnimap-retrieve-group-data-early (server infos)
1180 (when (nnimap-possibly-change-group nil server)
1181 (with-current-buffer (nnimap-buffer)
1183 (setf (nnimap-group nnimap-object) nil)
1184 (let ((qresyncp (nnimap-capability "QRESYNC"))
1185 params groups sequences active uidvalidity modseq group)
1186 ;; Go through the infos and gather the data needed to know
1187 ;; what and how to request the data.
1188 (dolist (info infos)
1189 (setq params (gnus-info-params info)
1190 group (gnus-group-real-name (gnus-info-group info))
1191 active (cdr (assq 'active params))
1192 uidvalidity (cdr (assq 'uidvalidity params))
1193 modseq (cdr (assq 'modseq params)))
1194 (setf (nnimap-examined nnimap-object) group)
1200 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1201 (utf7-encode group t)
1202 (nnimap-quirk "QRESYNC")
1208 (if (and active uidvalidity)
1209 ;; Fetch the last 100 flags.
1210 (max 1 (- (cdr active) 100))
1215 ;; If we don't have a UIDVALIDITY, then this is
1216 ;; the first time we've seen the group, so we
1217 ;; have to do a SELECT (which is slower than an
1218 ;; examine), but will tell us whether the group
1219 ;; is read-only or not.
1221 (push (list (nnimap-send-command "%s %S" command
1222 (utf7-encode group t))
1223 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1224 start group command)
1228 (defun nnimap-quirk (command)
1229 (let ((quirk (assoc command nnimap-quirks)))
1230 ;; If this server is of a type that matches a quirk, then return
1231 ;; the "quirked" command instead of the proper one.
1232 (if (or (null quirk)
1233 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1237 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1238 (when (and sequences
1239 ;; Check that the process is still alive.
1240 (get-buffer-process (nnimap-buffer))
1241 (memq (process-status (get-buffer-process (nnimap-buffer)))
1243 (nnimap-possibly-change-group nil server))
1244 (with-current-buffer (nnimap-buffer)
1245 ;; Wait for the final data to trickle in.
1246 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1250 ;; Now we should have most of the data we need, no matter
1251 ;; whether we're QRESYNCING, fetching all the flags from
1252 ;; scratch, or just fetching the last 100 flags per group.
1253 (nnimap-update-infos (nnimap-flags-to-marks
1255 (nreverse sequences)))
1257 ;; Finally, just return something resembling an active file in
1258 ;; the nntp buffer, so that the agent can save the info, too.
1259 (with-current-buffer nntp-server-buffer
1261 (dolist (info infos)
1262 (let* ((group (gnus-info-group info))
1263 (active (gnus-active group)))
1265 (insert (format "%S %d %d y\n"
1266 (gnus-group-real-name group)
1268 (car active)))))))))))
1270 (defun nnimap-update-infos (flags infos)
1271 (dolist (info infos)
1272 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1273 (marks (cdr (assoc group flags))))
1275 (nnimap-update-info info marks)))))
1277 (defun nnimap-update-info (info marks)
1278 (destructuring-bind (existing flags high low uidnext start-article
1279 permanent-flags uidvalidity
1280 vanished highestmodseq) marks
1282 ;; Ignore groups with no UIDNEXT/marks. This happens for
1283 ;; completely empty groups.
1284 ((and (not existing)
1286 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1288 (gnus-set-active (gnus-info-group info) active))))
1289 ;; We have a mismatch between the old and new UIDVALIDITY
1290 ;; identifiers, so we have to re-request the group info (the next
1291 ;; time). This virtually never happens.
1292 ((let ((old-uidvalidity
1293 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1294 (and old-uidvalidity
1295 (not (equal old-uidvalidity uidvalidity))
1296 (> start-article 1)))
1297 (gnus-group-remove-parameter info 'uidvalidity)
1298 (gnus-group-remove-parameter info 'modseq))
1299 ;; We have the data needed to update.
1301 (let* ((group (gnus-info-group info))
1302 (completep (and start-article
1303 (= start-article 1)))
1304 (active (or (gnus-active group)
1305 (cdr (assq 'active (gnus-info-params info))))))
1307 (setq high (1- uidnext)))
1308 ;; First set the active ranges based on high/low.
1310 (not (gnus-active group)))
1311 (gnus-set-active group
1314 (cons (min (or low (car active))
1316 (max (or high (cdr active))
1321 ;; No articles in this group.
1322 (cons uidnext (1- uidnext)))
1324 (cons start-article (1- start-article)))
1326 ;; No articles and no uidnext.
1328 (gnus-set-active group
1330 (or high (1- uidnext)))))
1331 ;; See whether this is a read-only group.
1332 (unless (eq permanent-flags 'not-scanned)
1333 (gnus-group-set-parameter
1334 info 'permanent-flags
1335 (and (or (memq '%* permanent-flags)
1336 (memq '%Seen permanent-flags))
1338 ;; Update marks and read articles if this isn't a
1339 ;; read-only IMAP group.
1340 (when (setq permanent-flags
1341 (cdr (assq 'permanent-flags (gnus-info-params info))))
1342 (if (and highestmodseq
1343 (not start-article))
1344 ;; We've gotten the data by QRESYNCing.
1345 (nnimap-update-qresync-info
1346 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1347 ;; Do normal non-QRESYNC flag updates.
1348 ;; Update the list of read articles.
1350 (gnus-compress-sequence
1351 (gnus-set-difference
1352 (gnus-set-difference
1354 (cdr (assoc '%Seen flags)))
1355 (cdr (assoc '%Flagged flags)))))
1356 (read (gnus-range-difference
1357 (cons start-article high) unread)))
1358 (when (> start-article 1)
1361 (if (> start-article 1)
1362 (gnus-sorted-range-intersection
1363 (cons 1 (1- start-article))
1364 (gnus-info-read info))
1365 (gnus-info-read info))
1367 (when (or (not (listp permanent-flags))
1368 (memq '%Seen permanent-flags))
1369 (gnus-info-set-read info read))
1370 ;; Update the marks.
1371 (setq marks (gnus-info-marks info))
1372 (dolist (type (cdr nnimap-mark-alist))
1373 (when (or (not (listp permanent-flags))
1374 (memq (car (assoc (caddr type) flags))
1376 (memq '%* permanent-flags))
1377 (let ((old-marks (assoc (car type) marks))
1379 (gnus-compress-sequence
1380 (cdr (or (assoc (caddr type) flags) ; %Flagged
1381 (assoc (intern (cadr type) obarray) flags)
1382 (assoc (cadr type) flags)))))) ; "\Flagged"
1383 (setq marks (delq old-marks marks))
1385 (when (and old-marks
1386 (> start-article 1))
1387 (setq old-marks (gnus-range-difference
1389 (cons start-article high)))
1390 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1392 (push (cons (car type) new-marks) marks)))))
1393 (gnus-info-set-marks info marks t))))
1394 ;; Tell Gnus whether there are any \Recent messages in any of
1396 (let ((recent (cdr (assoc '%Recent flags))))
1399 (> (car (last recent)) (cdr active)))
1400 (push (list (cons (gnus-group-real-name group) 0))
1401 nnmail-split-history)))
1402 ;; Note the active level for the next run-through.
1403 (gnus-group-set-parameter info 'active (gnus-active group))
1404 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1405 (gnus-group-set-parameter info 'modseq highestmodseq)
1406 (nnimap-store-info info (gnus-active group)))))))
1408 (defun nnimap-update-qresync-info (info existing vanished flags)
1409 ;; Add all the vanished articles to the list of read articles.
1414 (gnus-range-add (gnus-info-read info)
1416 (cdr (assq '%Flagged flags)))
1417 (cdr (assq '%Seen flags))))
1418 (let ((marks (gnus-info-marks info)))
1419 (dolist (type (cdr nnimap-mark-alist))
1420 (let ((ticks (assoc (car type) marks))
1422 (cdr (or (assoc (caddr type) flags) ; %Flagged
1423 (assoc (intern (cadr type) obarray) flags)
1424 (assoc (cadr type) flags))))) ; "\Flagged"
1425 (setq marks (delq ticks marks))
1427 ;; Add the new marks we got.
1428 (setq ticks (gnus-add-to-range ticks new-marks))
1429 ;; Remove the marks from messages that don't have them.
1430 (setq ticks (gnus-remove-from-range
1432 (gnus-compress-sequence
1433 (gnus-sorted-complement existing new-marks))))
1435 (push (cons (car type) ticks) marks)))
1436 (gnus-info-set-marks info marks t))))
1438 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1439 (if (zerop (length irange))
1442 (dolist (elem (split-string irange ","))
1444 (if (string-match ":" elem)
1445 (let ((numbers (split-string elem ":")))
1446 (cons (string-to-number (car numbers))
1447 (string-to-number (cadr numbers))))
1448 (string-to-number elem))
1450 (nreverse result))))
1452 (defun nnimap-store-info (info active)
1453 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1454 (entry (assoc group nnimap-current-infos)))
1456 (setcdr entry (list info active))
1457 (push (list group info active) nnimap-current-infos))))
1459 (defun nnimap-flags-to-marks (groups)
1460 (let (data group totalp uidnext articles start-article mark permanent-flags
1461 uidvalidity vanished highestmodseq)
1462 (dolist (elem groups)
1463 (setq group (car elem)
1464 uidnext (nth 1 elem)
1465 start-article (nth 2 elem)
1466 permanent-flags (nth 3 elem)
1467 uidvalidity (nth 4 elem)
1468 vanished (nth 5 elem)
1469 highestmodseq (nth 6 elem)
1470 articles (nthcdr 7 elem))
1471 (let ((high (caar articles))
1473 (dolist (article articles)
1474 (setq low (car article))
1475 (push (car article) existing)
1476 (dolist (flag (cdr article))
1477 (setq mark (assoc flag marks))
1479 (push (list flag (car article)) marks)
1480 (setcdr mark (cons (car article) (cdr mark))))))
1481 (push (list group existing marks high low uidnext start-article
1482 permanent-flags uidvalidity vanished highestmodseq)
1486 (defun nnimap-parse-flags (sequences)
1487 (goto-char (point-min))
1488 ;; Change \Delete etc to %Delete, so that the reader can read it.
1489 (subst-char-in-region (point-min) (point-max)
1491 ;; Remove any MODSEQ entries in the buffer, because they may contain
1492 ;; numbers that are too large for 32-bit Emacsen.
1493 (while (re-search-forward " MODSEQ ([0-9]+)" nil t)
1494 (replace-match "" t t))
1495 (goto-char (point-min))
1496 (let (start end articles groups uidnext elems permanent-flags
1497 uidvalidity vanished highestmodseq)
1498 (dolist (elem sequences)
1499 (destructuring-bind (group-sequence flag-sequence totalp group command)
1501 (setq start (point))
1503 ;; The EXAMINE was successful.
1504 (search-forward (format "\n%d OK " group-sequence) nil t)
1509 (setq permanent-flags
1510 (if (equal command "SELECT")
1511 (and (search-forward "PERMANENTFLAGS "
1512 (or end (point-min)) t)
1513 (read (current-buffer)))
1517 (and (search-forward "UIDNEXT "
1518 (or end (point-min)) t)
1519 (read (current-buffer))))
1522 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1523 (or end (point-min)) t)
1524 ;; Store UIDVALIDITY as a string, as it's
1525 ;; too big for 32-bit Emacsen, usually.
1529 (and (eq flag-sequence 'qresync)
1530 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1531 (or end (point-min)) t)
1535 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1536 (or end (point-min)) t)
1540 ;; The UID FETCH FLAGS was successful.
1541 (or (eq flag-sequence 'qresync)
1542 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1543 (if (eq flag-sequence 'qresync)
1547 (setq start (point))
1549 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1551 (setq elems (read (current-buffer)))
1552 (push (cons (cadr (memq 'UID elems))
1553 (cadr (memq 'FLAGS elems)))
1555 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1556 vanished highestmodseq)
1560 (setq articles nil))))
1563 (defun nnimap-find-process-buffer (buffer)
1564 (cadr (assoc buffer nnimap-connection-alist)))
1566 (deffoo nnimap-request-post (&optional server)
1567 (setq nnimap-status-string "Read-only server")
1570 (declare-function gnus-fetch-headers "gnus-sum"
1571 (articles &optional limit force-new dependencies))
1573 (deffoo nnimap-request-thread (header &optional group server)
1574 (if gnus-refer-thread-use-nnir
1575 (nnir-search-thread header)
1576 (when (nnimap-possibly-change-group group server)
1577 (let* ((cmd (nnimap-make-thread-query header))
1578 (result (with-current-buffer (nnimap-buffer)
1579 (nnimap-command "UID SEARCH %s" cmd))))
1582 (and (car result) (delete 0 (mapcar #'string-to-number
1583 (cdr (assoc "SEARCH" (cdr result))))))
1586 (defun nnimap-possibly-change-group (group server)
1587 (let ((open-result t))
1589 (not (nnimap-server-opened server)))
1590 (setq open-result (nnimap-open-server server)))
1597 (with-current-buffer (nnimap-buffer)
1598 (if (equal group (nnimap-group nnimap-object))
1600 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1602 (setf (nnimap-group nnimap-object) group
1603 (nnimap-select-result nnimap-object) result)
1606 (defun nnimap-find-connection (buffer)
1607 "Find the connection delivering to BUFFER."
1608 (let ((entry (assoc buffer nnimap-connection-alist)))
1610 (if (and (buffer-name (cadr entry))
1611 (get-buffer-process (cadr entry))
1612 (memq (process-status (get-buffer-process (cadr entry)))
1614 (get-buffer-process (cadr entry))
1615 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1618 (defvar nnimap-sequence 0)
1620 (defun nnimap-send-command (&rest args)
1621 (setf (nnimap-last-command-time nnimap-object) (current-time))
1622 (process-send-string
1623 (get-buffer-process (current-buffer))
1626 (incf nnimap-sequence)
1627 (apply #'format args)
1628 (if (nnimap-newlinep nnimap-object)
1631 ;; Some servers apparently can't have many outstanding
1632 ;; commands, so throttle them.
1633 (unless nnimap-streaming
1634 (nnimap-wait-for-response nnimap-sequence))
1637 (defun nnimap-log-command (command)
1638 (with-current-buffer (get-buffer-create "*imap log*")
1639 (goto-char (point-max))
1640 (insert (format-time-string "%H:%M:%S") " "
1641 (if nnimap-inhibit-logging
1646 (defun nnimap-command (&rest args)
1648 (let* ((sequence (apply #'nnimap-send-command args))
1649 (response (nnimap-get-response sequence)))
1650 (if (equal (caar response) "OK")
1652 (nnheader-report 'nnimap "%s"
1653 (mapconcat (lambda (a)
1655 (car response) " "))
1658 (defun nnimap-get-response (sequence)
1659 (nnimap-wait-for-response sequence)
1660 (nnimap-parse-response))
1662 (defun nnimap-wait-for-connection (&optional regexp)
1663 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1665 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1666 (let ((process (get-buffer-process (current-buffer))))
1667 (goto-char (point-min))
1668 (while (and (memq (process-status process)
1670 (not (re-search-forward regexp nil t)))
1671 (nnheader-accept-process-output process)
1672 (goto-char (point-min)))
1674 (and (looking-at (or response-regexp regexp))
1677 (defun nnimap-wait-for-response (sequence &optional messagep)
1678 (let ((process (get-buffer-process (current-buffer)))
1682 (goto-char (point-max))
1683 (while (and (setq openp (memq (process-status process)
1686 ;; Skip past any "*" lines that the server has
1688 (while (and (not (bobp))
1691 (looking-at "\\*"))))
1692 (not (looking-at (format "%d .*\n" sequence)))))
1694 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1695 (nnheader-accept-process-output process)
1696 (goto-char (point-max)))
1701 ;; The user hit C-g while we were waiting: kill the process, in case
1702 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1704 (delete-process process)
1707 (defun nnimap-parse-response ()
1708 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1710 (dolist (line lines)
1711 (push (cdr (nnimap-parse-line line)) result))
1712 ;; Return the OK/error code first, and then all the "continuation
1713 ;; lines" afterwards.
1715 (nreverse result))))
1717 ;; Parse an IMAP response line lightly. They look like
1718 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1719 ;; the lines into a list of strings and lists of string.
1720 (defun nnimap-parse-line (line)
1723 (mm-disable-multibyte)
1725 (goto-char (point-min))
1727 (if (eql (setq char (following-char)) ? )
1735 (if (search-forward "]" (line-end-position) 'move)
1742 (if (search-forward ")" (line-end-position) 'move)
1749 (1- (or (search-forward "\"" (line-end-position) 'move)
1752 (buffer-substring (point) (if (search-forward " " nil t)
1754 (goto-char (point-max))))))
1756 (nreverse result))))
1758 (defun nnimap-last-response-string ()
1761 (let ((end (point)))
1765 (while (and (not (bobp))
1766 (eql (following-char) ?*))
1768 (unless (eql (following-char) ?*)
1770 (buffer-substring (point) end))))
1772 (defun nnimap-get-responses (sequences)
1774 (dolist (sequence sequences)
1775 (goto-char (point-min))
1776 (when (re-search-forward (format "^%d " sequence) nil t)
1777 (push (list sequence (nnimap-parse-response))
1781 (defvar nnimap-incoming-split-list nil)
1783 (defun nnimap-fetch-inbox (articles)
1785 (nnimap-wait-for-response
1786 (nnimap-send-command
1788 (nnimap-article-ranges articles)
1789 (format "(UID %s%s)"
1795 (nnimap-split-download-body-default
1803 (defun nnimap-split-incoming-mail ()
1804 (with-current-buffer (nnimap-buffer)
1805 (let ((nnimap-incoming-split-list nil)
1806 (nnmail-split-methods
1808 ((eq nnimap-split-methods 'default)
1809 nnmail-split-methods)
1810 (nnimap-split-methods
1811 nnimap-split-methods)
1813 'nnmail-split-fancy)))
1814 (nnmail-split-fancy (or nnimap-split-fancy
1815 nnmail-split-fancy))
1816 (nnmail-inhibit-default-split-group t)
1817 (groups (nnimap-get-groups))
1820 (nnimap-command "SELECT %S" nnimap-inbox)
1821 (setf (nnimap-group nnimap-object) nnimap-inbox)
1822 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1824 (nnimap-fetch-inbox new-articles)
1825 (nnimap-transform-split-mail)
1826 (nnheader-ms-strip-cr)
1828 (nnmail-split-incoming (current-buffer)
1829 #'nnimap-save-mail-spec
1831 #'nnimap-dummy-active-number
1832 #'nnimap-save-mail-spec)
1833 (when nnimap-incoming-split-list
1834 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1835 sequences junk-articles)
1836 ;; Create any groups that doesn't already exist on the
1838 (dolist (spec specs)
1839 (when (and (not (member (car spec) groups))
1840 (not (eq (car spec) 'junk)))
1841 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1842 ;; Then copy over all the messages.
1844 (dolist (spec specs)
1845 (let ((group (car spec))
1846 (ranges (cdr spec)))
1847 (if (eq group 'junk)
1848 (setq junk-articles ranges)
1849 (push (list (nnimap-send-command
1851 (nnimap-article-ranges ranges)
1852 (utf7-encode group t))
1855 ;; Wait for the last COPY response...
1857 (nnimap-wait-for-response (caar sequences))
1858 ;; And then mark the successful copy actions as deleted,
1859 ;; and possibly expunge them.
1860 (nnimap-mark-and-expunge-incoming
1861 (nnimap-parse-copied-articles sequences)))
1862 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1864 (defun nnimap-mark-and-expunge-incoming (range)
1866 (setq range (nnimap-article-ranges range))
1869 (nnimap-send-command
1870 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1872 ;; If the server supports it, we now delete the message we have
1873 ;; just copied over.
1874 ((nnimap-capability "UIDPLUS")
1875 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1876 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1877 ;; user has configured it.
1879 (setq sequence (nnimap-send-command "EXPUNGE"))))
1880 (nnimap-wait-for-response sequence))))
1882 (defun nnimap-parse-copied-articles (sequences)
1883 (let (sequence copied range)
1884 (goto-char (point-min))
1885 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
1886 (setq sequence (string-to-number (match-string 1)))
1887 (when (setq range (cadr (assq sequence sequences)))
1888 (push (gnus-uncompress-range range) copied)))
1889 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1891 (defun nnimap-new-articles (flags)
1893 (dolist (elem flags)
1894 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1896 (push (car elem) new)))
1897 (gnus-compress-sequence (nreverse new))))
1899 (defun nnimap-make-split-specs (list)
1903 (destructuring-bind (article spec) elem
1904 (dolist (group (delete nil (mapcar #'car spec)))
1905 (unless (setq entry (assoc group specs))
1906 (push (setq entry (list group)) specs))
1907 (setcdr entry (cons article (cdr entry))))))
1908 (dolist (entry specs)
1909 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1912 (defun nnimap-transform-split-mail ()
1913 (goto-char (point-min))
1914 (let (article bytes)
1917 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
1918 (delete-region (point) (progn (forward-line 1) (point)))
1921 (setq article (match-string 1)
1922 bytes (nnimap-get-length))
1923 (delete-region (line-beginning-position) (line-end-position))
1924 ;; Insert MMDF separator, and a way to remember what this
1926 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1927 (forward-char (1+ bytes))
1928 (setq bytes (nnimap-get-length))
1929 (delete-region (line-beginning-position) (line-end-position))
1930 ;; There's a body; skip past that.
1932 (forward-char (1+ bytes))
1933 (delete-region (line-beginning-position) (line-end-position)))))))
1935 (defun nnimap-dummy-active-number (group &optional server)
1938 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1940 (goto-char (point-min))
1941 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1942 (error "Invalid nnimap mail")
1943 (setq article (string-to-number (match-string 1))))
1945 (if (eq group-art 'junk)
1946 (list (cons 'junk 1))
1948 nnimap-incoming-split-list)))
1950 (defun nnimap-make-thread-query (header)
1951 (let* ((id (mail-header-id header))
1953 (or (mail-header-references header)
1957 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
1959 (dolist (refid refs value)
1961 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
1962 refid refid value)))))
1967 ;;; nnimap.el ends here