1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2012 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 This can be a string or a list of strings
86 For example, \"INBOX\" or (\"INBOX\" \"SENT\").")
88 (defvoo nnimap-split-methods nil
90 Uses the same syntax as `nnmail-split-methods'.")
92 (defvoo nnimap-split-fancy nil
93 "Uses the same syntax as `nnmail-split-fancy'.")
95 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
96 "Articles with the flags in the list will not be considered when splitting.")
98 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
101 (defvoo nnimap-authenticator nil
102 "How nnimap authenticate itself to the server.
103 Possible choices are nil (use default methods), `anonymous',
104 `login', `plain' and `cram-md5'.")
106 (defvoo nnimap-expunge t
107 "If non-nil, expunge articles after deleting them.
108 This is always done if the server supports UID EXPUNGE, but it's
109 not done by default on servers that doesn't support that command.")
111 (defvoo nnimap-streaming t
112 "If non-nil, try to use streaming commands with IMAP servers.
113 Switching this off will make nnimap slower, but it helps with
116 (defvoo nnimap-connection-alist nil)
118 (defvoo nnimap-current-infos nil)
120 (defvoo nnimap-fetch-partial-articles nil
121 "If non-nil, Gnus will fetch partial articles.
122 If t, Gnus will fetch only the first part. If a string, it
123 will fetch all parts that have types that match that string. A
124 likely value would be \"text/\" to automatically fetch all
131 (defcustom nnimap-request-articles-find-limit nil
132 "Limit the number of articles to look for after moving an article."
137 (defvar nnimap-process nil)
139 (defvar nnimap-status-string "")
141 (defvar nnimap-split-download-body-default nil
142 "Internal variable with default value for `nnimap-split-download-body'.")
144 (defvar nnimap-keepalive-timer nil)
145 (defvar nnimap-process-buffers nil)
148 group process commands capabilities select-result newlinep server
149 last-command-time greeting examined stream-type initial-resync)
151 (defvar nnimap-object nil)
153 (defvar nnimap-mark-alist
154 '((read "\\Seen" %Seen)
155 (tick "\\Flagged" %Flagged)
156 (reply "\\Answered" %Answered)
157 (expire "gnus-expire")
158 (dormant "gnus-dormant")
161 (download "gnus-download")
162 (forward "gnus-forward")))
164 (defvar nnimap-quirks
165 '(("QRESYNC" "Zimbra" "QRESYNC ")))
167 (defvar nnimap-inhibit-logging nil)
169 (defun nnimap-buffer ()
170 (nnimap-find-process-buffer nntp-server-buffer))
172 (defun nnimap-header-parameters ()
173 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
176 "BODY.PEEK[HEADER.FIELDS %s]"
177 "RFC822.HEADER.LINES %s")
178 (append '(Subject From Date Message-Id
179 References In-Reply-To Xref)
180 nnmail-extra-headers))))
182 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
184 (setq group (nnimap-decode-gnus-group group)))
185 (with-current-buffer nntp-server-buffer
187 (when (nnimap-change-group group server)
188 (with-current-buffer (nnimap-buffer)
190 (nnimap-wait-for-response
193 (nnimap-article-ranges (gnus-compress-sequence articles))
194 (nnimap-header-parameters))
196 (nnimap-transform-headers)
197 (nnheader-remove-cr-followed-by-lf))
198 (insert-buffer-substring
199 (nnimap-find-process-buffer (current-buffer))))
202 (defun nnimap-transform-headers ()
203 (goto-char (point-min))
204 (let (article lines size string)
207 (while (not (looking-at "\\* [0-9]+ FETCH"))
208 (delete-region (point) (progn (forward-line 1) (point)))
211 (goto-char (match-end 0))
212 ;; Unfold quoted {number} strings.
213 (while (re-search-forward
214 "[^]][ (]{\\([0-9]+\\)}\r?\n"
216 ;; Start of the header section.
217 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
218 ;; Start of the next FETCH.
219 (re-search-forward "\\* [0-9]+ FETCH" nil t)
222 (setq size (string-to-number (match-string 1)))
223 (delete-region (+ (match-beginning 0) 2) (point))
224 (setq string (buffer-substring (point) (+ (point) size)))
225 (delete-region (point) (+ (point) size))
226 (insert (format "%S" (mm-subst-char-in-string ?\n ?\s string))))
229 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
234 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
239 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
240 (let ((structure (ignore-errors
241 (read (current-buffer)))))
242 (while (and (consp structure)
243 (not (atom (car structure))))
244 (setq structure (car structure)))
246 (stringp (car structure))
247 (equal (upcase (nth 0 structure)) "MESSAGE")
248 (equal (upcase (nth 1 structure)) "RFC822"))
250 (nth 7 structure)))))
251 (delete-region (line-beginning-position) (line-end-position))
252 (insert (format "211 %s Article retrieved." article))
255 (insert (format "Chars: %s\n" size)))
257 (insert (format "Lines: %s\n" lines)))
258 (unless (re-search-forward "^\r$" nil t)
259 (goto-char (point-max)))
260 (delete-region (line-beginning-position) (line-end-position))
264 (defun nnimap-unfold-quoted-lines ()
265 ;; Unfold quoted {number} strings.
267 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
268 (setq size (string-to-number (match-string 1)))
269 (delete-region (1+ (match-beginning 0)) (point))
270 (setq string (buffer-substring (point) (+ (point) size)))
271 (delete-region (point) (+ (point) size))
272 (insert (format "%S" string)))))
274 (defun nnimap-get-length ()
275 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
276 (string-to-number (match-string 1))))
278 (defun nnimap-article-ranges (ranges)
282 (number-to-string ranges))
283 ((numberp (cdr ranges))
284 (format "%d:%d" (car ranges) (cdr ranges)))
286 (dolist (elem ranges)
289 (format "%d:%d" (car elem) (cdr elem))
290 (number-to-string elem))
292 (mapconcat #'identity (nreverse result) ",")))))
294 (deffoo nnimap-open-server (server &optional defs no-reconnect)
295 (if (nnimap-server-opened server)
297 (unless (assq 'nnimap-address defs)
298 (setq defs (append defs (list (list 'nnimap-address server)))))
299 (nnoo-change-server 'nnimap server defs)
301 (nnimap-find-connection nntp-server-buffer)
302 (or (nnimap-find-connection nntp-server-buffer)
303 (nnimap-open-connection nntp-server-buffer)))))
305 (defun nnimap-make-process-buffer (buffer)
307 (generate-new-buffer (format " *nnimap %s %s %s*"
308 nnimap-address nnimap-server-port
309 (gnus-buffer-exists-p buffer)))
310 (mm-disable-multibyte)
311 (buffer-disable-undo)
313 (set (make-local-variable 'after-change-functions) nil)
314 (set (make-local-variable 'nnimap-object)
315 (make-nnimap :server (nnoo-current-server 'nnimap)
317 (push (list buffer (current-buffer)) nnimap-connection-alist)
318 (push (current-buffer) nnimap-process-buffers)
321 (defun nnimap-credentials (address ports user)
322 (let* ((auth-source-creation-prompts
323 '((user . "IMAP user at %h: ")
324 (secret . "IMAP password for %u@%h: ")))
325 (found (nth 0 (auth-source-search :max 1
329 :require '(:user :secret)
332 (list (plist-get found :user)
333 (let ((secret (plist-get found :secret)))
334 (if (functionp secret)
337 (plist-get found :save-function))
340 (defun nnimap-keepalive ()
341 (let ((now (current-time)))
342 (dolist (buffer nnimap-process-buffers)
343 (when (buffer-name buffer)
344 (with-current-buffer buffer
345 (when (and nnimap-object
346 (nnimap-last-command-time nnimap-object)
350 (nnimap-last-command-time nnimap-object)))
351 ;; More than five minutes since the last command.
353 (nnimap-send-command "NOOP")))))))
355 (defun nnimap-open-connection (buffer)
356 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
357 ;; `ssl' when nnimap-server-port was nil. Sort of.
358 (when (and nnimap-server-port
359 (eq nnimap-stream 'undecided))
360 (setq nnimap-stream 'ssl))
362 (if (eq nnimap-stream 'undecided)
363 (loop for type in '(ssl network)
364 for stream = (let ((nnimap-stream type))
365 (nnimap-open-connection-1 buffer))
366 while (eq stream 'no-connect)
367 finally (return stream))
368 (nnimap-open-connection-1 buffer))))
369 (if (eq stream 'no-connect)
373 (defun nnimap-map-port (port)
374 (if (equal port "imaps")
378 (defun nnimap-open-connection-1 (buffer)
379 (unless nnimap-keepalive-timer
380 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
382 (with-current-buffer (nnimap-make-process-buffer buffer)
383 (let* ((coding-system-for-read 'binary)
384 (coding-system-for-write 'binary)
387 ((memq nnimap-stream '(network plain starttls))
388 (nnheader-message 7 "Opening connection to %s..."
391 ((eq nnimap-stream 'shell)
392 (nnheader-message 7 "Opening connection to %s via shell..."
395 ((memq nnimap-stream '(ssl tls))
396 (nnheader-message 7 "Opening connection to %s via tls..."
398 '("imaps" "imap" "993" "143"))
400 (error "Unknown stream type: %s" nnimap-stream))))
401 login-result credentials)
402 (when nnimap-server-port
403 (push nnimap-server-port ports))
405 (open-protocol-stream
406 "*nnimap*" (current-buffer) nnimap-address
407 (nnimap-map-port (car ports))
410 :shell-command nnimap-shell-program
411 :capability-command "1 CAPABILITY\r\n"
412 :end-of-command "\r\n"
415 (lambda (capabilities)
416 (when (gnus-string-match-p "STARTTLS" capabilities)
418 (stream (car stream-list))
419 (props (cdr stream-list))
420 (greeting (plist-get props :greeting))
421 (capabilities (plist-get props :capabilities))
422 (stream-type (plist-get props :type)))
423 (when (and stream (not (memq (process-status stream) '(open run))))
426 (when (and (fboundp 'set-network-process-option) ;; Not in XEmacs.
427 (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
428 (eq (process-type stream) 'network))
429 ;; Use TCP-keepalive so that connections that pass through a NAT
430 ;; router don't hang when left idle.
431 (set-network-process-option stream :keepalive t))
433 (setf (nnimap-process nnimap-object) stream)
434 (setf (nnimap-stream-type nnimap-object) stream-type)
437 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
438 nnimap-address (car ports) nnimap-stream)
440 (gnus-set-process-query-on-exit-flag stream nil)
441 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
442 (nnheader-report 'nnimap "%s" greeting)
443 ;; Store the greeting (for debugging purposes).
444 (setf (nnimap-greeting nnimap-object) greeting)
445 (setf (nnimap-capabilities nnimap-object)
447 (split-string capabilities)))
448 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
449 (if (not (setq credentials
450 (if (eq nnimap-authenticator 'anonymous)
452 (message-make-address))
453 ;; Look for the credentials based on
454 ;; the virtual server name and the address
456 (gnus-delete-duplicates
459 (nnoo-current-server 'nnimap)))
462 (setq nnimap-object nil)
463 (let ((nnimap-inhibit-logging t))
465 (nnimap-login (car credentials) (cadr credentials))))
466 (if (car login-result)
468 ;; Save the credentials if a save function exists
469 ;; (such a function will only be passed if a new
470 ;; token was created).
471 (when (functionp (nth 2 credentials))
472 (funcall (nth 2 credentials)))
473 ;; See if CAPABILITY is set as part of login
475 (dolist (response (cddr login-result))
476 (when (string= "CAPABILITY" (upcase (car response)))
477 (setf (nnimap-capabilities nnimap-object)
478 (mapcar #'upcase (cdr response))))))
479 ;; If the login failed, then forget the credentials
480 ;; that are now possibly cached.
481 (dolist (host (list (nnoo-current-server 'nnimap)
484 (auth-source-forget+ :host host :port port)))
485 (delete-process (nnimap-process nnimap-object))
486 (setq nnimap-object nil))))
488 (when (nnimap-capability "QRESYNC")
489 (nnimap-command "ENABLE QRESYNC"))
490 (nnheader-message 7 "Opening connection to %s...done"
492 (nnimap-process nnimap-object))))))))
494 (autoload 'rfc2104-hash "rfc2104")
496 (defun nnimap-login (user password)
498 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
499 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
500 ;; and we're using an encrypted connection.
501 ((and (not (nnimap-capability "LOGINDISABLED"))
502 (eq (nnimap-stream-type nnimap-object) 'tls)
503 (or (null nnimap-authenticator)
504 (eq nnimap-authenticator 'login)))
505 (nnimap-command "LOGIN %S %S" user password))
506 ((and (nnimap-capability "AUTH=CRAM-MD5")
507 (or (null nnimap-authenticator)
508 (eq nnimap-authenticator 'cram-md5)))
510 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
511 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
513 (get-buffer-process (current-buffer))
515 (base64-encode-string
517 (rfc2104-hash 'md5 64 16 password
518 (base64-decode-string challenge))))
520 (nnimap-wait-for-response sequence)))
521 ((and (not (nnimap-capability "LOGINDISABLED"))
522 (or (null nnimap-authenticator)
523 (eq nnimap-authenticator 'login)))
524 (nnimap-command "LOGIN %S %S" user password))
525 ((and (nnimap-capability "AUTH=PLAIN")
526 (or (null nnimap-authenticator)
527 (eq nnimap-authenticator 'plain)))
529 "AUTHENTICATE PLAIN %s"
530 (base64-encode-string
531 (format "\000%s\000%s"
532 (nnimap-quote-specials user)
533 (nnimap-quote-specials password)))))))
535 (defun nnimap-quote-specials (string)
538 (goto-char (point-min))
539 (while (re-search-forward "[\\\"]" nil t)
545 (defun nnimap-find-parameter (parameter elems)
549 ((equal (car elem) parameter)
550 (setq result (cdr elem)))
551 ((and (equal (car elem) "OK")
553 (equal (caadr elem) parameter))
554 (setq result (cdr (cadr elem))))))
557 (deffoo nnimap-close-server (&optional server)
558 (when (nnoo-change-server 'nnimap server nil)
560 (delete-process (get-buffer-process (nnimap-buffer))))
561 (nnoo-close-server 'nnimap server)
564 (deffoo nnimap-request-close ()
567 (deffoo nnimap-server-opened (&optional server)
568 (and (nnoo-current-server-p 'nnimap server)
570 (gnus-buffer-live-p nntp-server-buffer)
571 (nnimap-find-connection nntp-server-buffer)))
573 (deffoo nnimap-status-message (&optional server)
574 nnimap-status-string)
576 (deffoo nnimap-request-article (article &optional group server to-buffer)
578 (setq group (nnimap-decode-gnus-group group)))
579 (with-current-buffer nntp-server-buffer
580 (let ((result (nnimap-change-group group server))
582 (when (stringp article)
583 (setq article (nnimap-find-article-by-message-id group server article)))
587 (with-current-buffer (nnimap-buffer)
589 (when nnimap-fetch-partial-articles
590 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
591 (goto-char (point-min))
592 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
593 (setq structure (ignore-errors
594 (let ((start (point)))
596 (downcase-region start (point))
598 (read (current-buffer))))
599 parts (nnimap-find-wanted-parts structure))))
601 (nnimap-get-partial-article article parts structure)
602 (nnimap-get-whole-article article))
603 (let ((buffer (current-buffer)))
604 (with-current-buffer (or to-buffer nntp-server-buffer)
605 (nnheader-insert-buffer-substring buffer)
606 (nnheader-ms-strip-cr)))
607 (cons group article)))))))
609 (deffoo nnimap-request-head (article &optional group server to-buffer)
611 (setq group (nnimap-decode-gnus-group group)))
612 (when (nnimap-change-group group server)
613 (with-current-buffer (nnimap-buffer)
614 (when (stringp article)
615 (setq article (nnimap-find-article-by-message-id group server article)))
618 (nnimap-get-whole-article
619 article (format "UID FETCH %%d %s"
620 (nnimap-header-parameters)))
621 (let ((buffer (current-buffer)))
622 (with-current-buffer (or to-buffer nntp-server-buffer)
624 (insert-buffer-substring buffer)
625 (nnheader-ms-strip-cr)
626 (cons group article)))))))
628 (defun nnimap-get-whole-article (article &optional command)
633 "UID FETCH %d BODY.PEEK[]"
634 "UID FETCH %d RFC822.PEEK"))
636 ;; Check that we really got an article.
637 (goto-char (point-min))
638 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
641 ;; Remove any data that may have arrived before the FETCH data.
644 (delete-region (point-min) (point)))
645 (let ((bytes (nnimap-get-length)))
646 (delete-region (line-beginning-position)
647 (progn (forward-line 1) (point)))
648 (goto-char (+ (point) bytes))
649 (delete-region (point) (point-max)))
652 (defun nnimap-capability (capability)
653 (member capability (nnimap-capabilities nnimap-object)))
655 (defun nnimap-ver4-p ()
656 (nnimap-capability "IMAP4REV1"))
658 (defun nnimap-get-partial-article (article parts structure)
661 "UID FETCH %d (%s %s)"
667 (mapconcat (lambda (part)
668 (format "BODY.PEEK[%s]" part))
670 (mapconcat (lambda (part)
671 (format "RFC822.PEEK[%s]" part))
674 (nnimap-convert-partial-article structure))))
676 (defun nnimap-convert-partial-article (structure)
677 ;; First just skip past the headers.
678 (goto-char (point-min))
679 (let ((bytes (nnimap-get-length))
681 ;; Delete "FETCH" line.
682 (delete-region (line-beginning-position)
683 (progn (forward-line 1) (point)))
684 (goto-char (+ (point) bytes))
685 ;; Collect all the body parts.
686 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
687 (setq id (match-string 1)
688 bytes (or (nnimap-get-length) 0))
690 (delete-region (point) (progn (forward-line 1) (point)))
691 (push (list id (buffer-substring (point) (+ (point) bytes)))
693 (delete-region (point) (+ (point) bytes)))
694 ;; Delete trailing junk.
695 (delete-region (point) (point-max))
696 ;; Now insert all the parts again where they fit in the structure.
697 (nnimap-insert-partial-structure structure parts)
700 (defun nnimap-insert-partial-structure (structure parts &optional subp)
702 (let ((bstruc structure))
703 (while (consp (car bstruc))
705 (setq type (car bstruc))
706 (setq bstruc (car (cdr bstruc)))
707 (let ((has-boundary (member "boundary" bstruc)))
709 (setq boundary (cadr has-boundary)))))
711 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
712 (downcase type) boundary)))
713 (while (not (stringp (car structure)))
714 (insert "\n--" boundary "\n")
715 (if (consp (caar structure))
716 (nnimap-insert-partial-structure (pop structure) parts t)
717 (let ((bit (pop structure)))
718 (insert (format "Content-type: %s/%s"
719 (downcase (nth 0 bit))
720 (downcase (nth 1 bit))))
721 (if (member-ignore-case "CHARSET" (nth 2 bit))
724 (cadr (member-ignore-case "CHARSET" (nth 2 bit)))))
726 (insert (format "Content-transfer-encoding: %s\n"
729 (when (assoc (nth 9 bit) parts)
730 (insert (cadr (assoc (nth 9 bit) parts)))))))
731 (insert "\n--" boundary "--\n")))
733 (defun nnimap-find-wanted-parts (structure)
734 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
736 (defun nnimap-find-wanted-parts-1 (structure prefix)
739 (while (consp (car structure))
740 (let ((sub (pop structure)))
741 (if (consp (car sub))
742 (push (nnimap-find-wanted-parts-1
743 sub (if (string= prefix "")
744 (number-to-string num)
745 (format "%s.%s" prefix num)))
747 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
748 (id (if (string= prefix "")
749 (number-to-string num)
750 (format "%s.%s" prefix num))))
751 (setcar (nthcdr 9 sub) id)
752 (when (if (eq nnimap-fetch-partial-articles t)
754 (string-match nnimap-fetch-partial-articles type))
759 (defun nnimap-decode-gnus-group (group)
760 (decode-coding-string group 'utf-8))
762 (deffoo nnimap-request-group (group &optional server dont-check info)
763 (setq group (nnimap-decode-gnus-group group))
764 (let ((result (nnimap-change-group
765 ;; Don't SELECT the group if we're going to select it
767 (if (and (not dont-check)
768 (assoc group nnimap-current-infos))
772 articles active marks high low)
773 (with-current-buffer nntp-server-buffer
776 (setq active (nth 2 (assoc group nnimap-current-infos))))
777 (insert (format "211 %d %d %d %S\n"
778 (- (cdr active) (car active))
782 (with-current-buffer (nnimap-buffer)
784 (let ((group-sequence
785 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
787 (nnimap-send-command "UID FETCH 1:* FLAGS")))
788 (setf (nnimap-group nnimap-object) group)
789 (nnimap-wait-for-response flag-sequence)
791 (nnimap-flags-to-marks
793 (list (list group-sequence flag-sequence
794 1 group "SELECT")))))
797 (nnimap-update-infos marks (list info))
798 (nnimap-store-info info (gnus-active (gnus-info-group info))))
799 (goto-char (point-max))
800 (let ((uidnext (nth 5 (car marks))))
801 (setq high (or (if uidnext
805 low (or (nth 4 (car marks)) uidnext 1)))))
809 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
812 (deffoo nnimap-request-create-group (group &optional server args)
813 (setq group (nnimap-decode-gnus-group group))
814 (when (nnimap-change-group nil server)
815 (with-current-buffer (nnimap-buffer)
816 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
818 (deffoo nnimap-request-delete-group (group &optional force server)
819 (setq group (nnimap-decode-gnus-group group))
820 (when (nnimap-change-group nil server)
821 (with-current-buffer (nnimap-buffer)
822 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
824 (deffoo nnimap-request-rename-group (group new-name &optional server)
825 (setq group (nnimap-decode-gnus-group group))
826 (when (nnimap-change-group nil server)
827 (with-current-buffer (nnimap-buffer)
828 (nnimap-unselect-group)
829 (car (nnimap-command "RENAME %S %S"
830 (utf7-encode group t) (utf7-encode new-name t))))))
832 (defun nnimap-unselect-group ()
833 ;; Make sure we don't have this group open read/write by asking
834 ;; to examine a mailbox that doesn't exist. This seems to be
835 ;; the only way that allows us to reliably go back to unselected
837 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
839 (deffoo nnimap-request-expunge-group (group &optional server)
840 (setq group (nnimap-decode-gnus-group group))
841 (when (nnimap-change-group group server)
842 (with-current-buffer (nnimap-buffer)
843 (car (nnimap-command "EXPUNGE")))))
845 (defun nnimap-get-flags (spec)
848 (with-current-buffer (nnimap-buffer)
850 (nnimap-wait-for-response (nnimap-send-command
851 "UID FETCH %s FLAGS" spec))
853 (subst-char-in-region (point-min) (point-max)
855 (goto-char (point-min))
856 (while (search-forward " FETCH " end t)
857 (setq elems (read (current-buffer)))
858 (push (cons (cadr (memq 'UID elems))
859 (cadr (memq 'FLAGS elems)))
861 (nreverse articles)))
863 (deffoo nnimap-close-group (group &optional server)
866 (deffoo nnimap-request-move-article (article group server accept-form
867 &optional last internal-move-group)
868 (setq group (nnimap-decode-gnus-group group))
869 (when internal-move-group
870 (setq internal-move-group (nnimap-decode-gnus-group internal-move-group)))
872 (mm-disable-multibyte)
873 (when (funcall (if internal-move-group
875 'nnimap-request-article)
876 article group server (current-buffer))
877 ;; If the move is internal (on the same server), just do it the easy
879 (let ((message-id (message-field-value "message-id")))
880 (if internal-move-group
882 (with-current-buffer (nnimap-buffer)
883 (nnimap-command "UID COPY %d %S"
885 (utf7-encode internal-move-group t)))))
887 (nnimap-delete-article article)
888 (cons internal-move-group
889 (or (nnimap-find-uid-response "COPYUID" (cadr result))
890 (nnimap-find-article-by-message-id
891 internal-move-group server message-id
892 nnimap-request-articles-find-limit)))))
893 ;; Move the article to a different method.
894 (let ((result (eval accept-form)))
896 (nnimap-change-group group server)
897 (nnimap-delete-article article)
900 (deffoo nnimap-request-expire-articles (articles group &optional server force)
901 (setq group (nnimap-decode-gnus-group group))
905 ((not (nnimap-change-group group server))
908 (eq nnmail-expiry-target 'delete))
909 (unless (nnimap-delete-article (gnus-compress-sequence articles))
910 (nnheader-message 7 "Article marked for deletion, but not expunged."))
913 (let ((deletable-articles
915 (eq nnmail-expiry-wait 'immediate))
917 (gnus-sorted-intersection
919 (nnimap-find-expired-articles group)))))
920 (if (null deletable-articles)
922 (if (eq nnmail-expiry-target 'delete)
923 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
924 (setq deletable-articles
925 (nnimap-process-expiry-targets
926 deletable-articles group server)))
927 ;; Return the articles we didn't delete.
928 (gnus-sorted-complement articles deletable-articles))))))
930 (defun nnimap-process-expiry-targets (articles group server)
931 (let ((deleted-articles nil))
933 ;; shortcut further processing if we're going to delete the articles
934 ((eq nnmail-expiry-target 'delete)
935 (setq deleted-articles articles)
937 ;; or just move them to another folder on the same IMAP server
938 ((and (not (functionp nnmail-expiry-target))
939 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
940 (gnus-server-to-method
941 (format "nnimap:%s" server))))
942 (and (nnimap-change-group group server)
943 (with-current-buffer (nnimap-buffer)
944 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
947 (nnimap-article-ranges (gnus-compress-sequence articles))
948 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
949 (setq deleted-articles articles)))
952 (dolist (article articles)
953 (let ((target nnmail-expiry-target))
955 (mm-disable-multibyte)
956 (when (nnimap-request-article article group server (current-buffer))
957 (when (functionp target)
958 (setq target (funcall target group)))
960 (not (eq target 'delete)))
961 (if (or (gnus-request-group target t)
962 (gnus-request-create-group target))
964 (nnmail-expiry-target-group target group)
965 (nnheader-message 7 "Expiring article %s:%d to %s"
966 group article target))
968 (nnheader-message 7 "Expiring article %s:%d" group article))
970 (push article deleted-articles))))))))
971 ;; Change back to the current group again.
972 (nnimap-change-group group server)
973 (setq deleted-articles (nreverse deleted-articles))
974 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
977 (defun nnimap-find-expired-articles (group)
978 (let ((cutoff (nnmail-expired-article-p group nil nil)))
979 (with-current-buffer (nnimap-buffer)
982 "UID SEARCH SENTBEFORE %s"
986 (car (rassoc (nth 4 (decode-time cutoff))
987 parse-time-months))))
990 (delete 0 (mapcar #'string-to-number
991 (cdr (assoc "SEARCH" (cdr result))))))))))
994 (defun nnimap-find-article-by-message-id (group server message-id
996 "Search for message with MESSAGE-ID in GROUP from SERVER.
997 If LIMIT, first try to limit the search to the N last articles."
998 (with-current-buffer (nnimap-buffer)
1000 (let* ((change-group-result (nnimap-change-group group server nil t))
1002 (and (listp change-group-result)
1004 (dolist (result (cdr change-group-result))
1005 (when (equal "EXISTS" (cadr result))
1006 (throw 'found (car result)))))))
1008 (nnimap-send-command
1009 "UID SEARCH%s HEADER Message-Id %S"
1010 (if (and limit number-of-article)
1011 ;; The -1 is because IMAP message
1012 ;; numbers are one-based rather than
1014 (format " %s:*" (- (string-to-number number-of-article)
1018 (when (nnimap-wait-for-response sequence)
1019 (let ((article (car (last (cdr (assoc "SEARCH"
1020 (nnimap-parse-response)))))))
1022 (string-to-number article)
1023 (when (and limit number-of-article)
1024 (nnimap-find-article-by-message-id group server message-id))))))))
1026 (defun nnimap-delete-article (articles)
1027 (with-current-buffer (nnimap-buffer)
1028 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
1029 (nnimap-article-ranges articles))
1031 ((nnimap-capability "UIDPLUS")
1032 (nnimap-command "UID EXPUNGE %s"
1033 (nnimap-article-ranges articles))
1036 (nnimap-command "EXPUNGE")
1038 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
1039 "server doesn't support UIDPLUS, so we won't "
1040 "delete this article now"))))))
1042 (deffoo nnimap-request-scan (&optional group server)
1044 (setq group (nnimap-decode-gnus-group group)))
1045 (when (and (nnimap-change-group nil server)
1047 nnimap-split-methods)
1048 (nnheader-message 7 "nnimap %s splitting mail..." server)
1049 (if (listp nnimap-inbox)
1050 (dolist (nnimap-inbox nnimap-inbox)
1051 (nnimap-split-incoming-mail))
1052 (nnimap-split-incoming-mail))
1053 (nnheader-message 7 "nnimap %s splitting mail...done" server)))
1055 (defun nnimap-marks-to-flags (marks)
1057 (dolist (mark marks)
1058 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
1062 (deffoo nnimap-request-update-group-status (group status &optional server)
1063 (setq group (nnimap-decode-gnus-group group))
1064 (when (nnimap-change-group nil server)
1065 (let ((command (assoc
1067 '((subscribe "SUBSCRIBE")
1068 (unsubscribe "UNSUBSCRIBE")))))
1070 (with-current-buffer (nnimap-buffer)
1071 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
1073 (deffoo nnimap-request-set-mark (group actions &optional server)
1074 (setq group (nnimap-decode-gnus-group group))
1075 (when (nnimap-change-group group server)
1077 (with-current-buffer (nnimap-buffer)
1079 ;; Just send all the STORE commands without waiting for
1080 ;; response. If they're successful, they're successful.
1081 (dolist (action actions)
1082 (destructuring-bind (range action marks) action
1083 (let ((flags (nnimap-marks-to-flags marks)))
1085 (setq sequence (nnimap-send-command
1086 "UID STORE %s %sFLAGS.SILENT (%s)"
1087 (nnimap-article-ranges range)
1089 ((eq action 'del) "-")
1090 ((eq action 'add) "+")
1091 ((eq action 'set) ""))
1092 (mapconcat #'identity flags " ")))))))
1093 ;; Wait for the last command to complete to avoid later
1094 ;; synchronization problems with the stream.
1096 (nnimap-wait-for-response sequence))))))
1098 (deffoo nnimap-request-accept-article (group &optional server last)
1099 (setq group (nnimap-decode-gnus-group group))
1100 (when (nnimap-change-group nil server)
1101 (nnmail-check-syntax)
1102 (let ((message-id (message-field-value "message-id"))
1105 (setq message (buffer-substring-no-properties (point-min) (point-max)))
1106 (with-current-buffer (nnimap-buffer)
1107 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
1109 ;; If we have this group open read-only, then unselect it
1110 ;; before appending to it.
1111 (when (equal (nnimap-examined nnimap-object) group)
1112 (nnimap-unselect-group))
1114 (setq sequence (nnimap-send-command
1115 "APPEND %S {%d}" (utf7-encode group t)
1117 (unless nnimap-streaming
1118 (nnimap-wait-for-connection "^[+]"))
1119 (process-send-string (get-buffer-process (current-buffer)) message)
1120 (process-send-string (get-buffer-process (current-buffer))
1121 (if (nnimap-newlinep nnimap-object)
1124 (let ((result (nnimap-get-response sequence)))
1125 (if (not (nnimap-ok-p result))
1127 (nnheader-report 'nnimap "%s" result)
1130 (or (nnimap-find-uid-response "APPENDUID" (car result))
1131 (nnimap-find-article-by-message-id
1132 group server message-id
1133 nnimap-request-articles-find-limit))))))))))
1135 (defun nnimap-process-quirk (greeting-match type data)
1136 (when (and (nnimap-greeting nnimap-object)
1137 (string-match greeting-match (nnimap-greeting nnimap-object))
1139 (string-match "\000" data))
1140 (let ((choice (gnus-multiple-choice
1141 "Message contains NUL characters. Delete, continue, abort? "
1142 '((?d "Delete NUL characters")
1143 (?c "Try to APPEND the message as is")
1147 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1153 (goto-char (point-min))
1154 (while (search-forward "\000" nil t)
1155 (replace-match "" t t))
1156 (buffer-string)))))))
1158 (defun nnimap-ok-p (value)
1161 (equal (caar value) "OK")))
1163 (defun nnimap-find-uid-response (name list)
1164 (let ((result (car (last (nnimap-find-response-element name list)))))
1166 (string-to-number result))))
1168 (defun nnimap-find-response-element (name list)
1171 (when (and (consp elem)
1172 (equal name (car elem)))
1173 (setq result elem)))
1176 (deffoo nnimap-request-replace-article (article group buffer)
1177 (setq group (nnimap-decode-gnus-group group))
1179 (when (and (nnimap-change-group group)
1180 ;; Put the article into the group.
1181 (with-current-buffer buffer
1183 (nnimap-request-accept-article group nil t))))
1184 (nnimap-delete-article (list article))
1185 ;; Return the new article number.
1188 (defun nnimap-add-cr ()
1189 (goto-char (point-min))
1190 (while (re-search-forward "\r?\n" nil t)
1191 (replace-match "\r\n" t t)))
1193 (defun nnimap-get-groups ()
1195 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1197 (nnimap-wait-for-response sequence)
1198 (subst-char-in-region (point-min) (point-max)
1200 (goto-char (point-min))
1201 (nnimap-unfold-quoted-lines)
1202 (goto-char (point-min))
1203 (while (search-forward "* LIST " nil t)
1204 (let ((flags (read (current-buffer)))
1205 (separator (read (current-buffer)))
1206 (group (read (current-buffer))))
1207 (unless (member '%NoSelect flags)
1208 (push (utf7-decode (if (stringp group)
1210 (format "%s" group)) t)
1214 (defun nnimap-get-responses (sequences)
1216 (dolist (sequence sequences)
1217 (goto-char (point-min))
1218 (when (re-search-forward (format "^%d " sequence) nil t)
1219 (push (list sequence (nnimap-parse-response))
1223 (deffoo nnimap-request-list (&optional server)
1224 (when (nnimap-change-group nil server)
1225 (with-current-buffer nntp-server-buffer
1228 (with-current-buffer (nnimap-buffer)
1229 (nnimap-get-groups)))
1230 sequences responses)
1232 (with-current-buffer (nnimap-buffer)
1233 (setf (nnimap-group nnimap-object) nil)
1234 (dolist (group groups)
1235 (setf (nnimap-examined nnimap-object) group)
1236 (push (list (nnimap-send-command "EXAMINE %S"
1237 (utf7-encode group t))
1240 (nnimap-wait-for-response (caar sequences))
1242 (nnimap-get-responses (mapcar #'car sequences))))
1243 (dolist (response responses)
1244 (let* ((sequence (car response))
1245 (response (cadr response))
1246 (group (cadr (assoc sequence sequences)))
1247 (egroup (encode-coding-string group 'utf-8)))
1249 (equal (caar response) "OK"))
1250 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1252 (dolist (elem response)
1253 (when (equal (cadr elem) "EXISTS")
1254 (setq exists (string-to-number (car elem)))))
1256 (setq highest (1- (string-to-number (car uidnext)))))
1259 (insert (format "%S 0 1 y\n" egroup)))
1262 (insert (format "%S %d %d y\n" egroup
1263 highest (1+ highest))))
1265 ;; Return the widest possible range.
1266 (insert (format "%S %d 1 y\n" egroup
1267 (or highest exists)))))))))
1270 (deffoo nnimap-request-newgroups (date &optional server)
1271 (when (nnimap-change-group nil server)
1272 (with-current-buffer nntp-server-buffer
1274 (dolist (group (with-current-buffer (nnimap-buffer)
1275 (nnimap-get-groups)))
1276 (unless (assoc group nnimap-current-infos)
1277 ;; Insert dummy numbers here -- they don't matter.
1278 (insert (format "%S 0 1 y\n" (encode-coding-string group 'utf-8)))))
1281 (deffoo nnimap-retrieve-group-data-early (server infos)
1282 (when (and (nnimap-change-group nil server)
1284 (with-current-buffer (nnimap-buffer)
1286 (setf (nnimap-group nnimap-object) nil)
1287 (setf (nnimap-initial-resync nnimap-object) 0)
1288 (let ((qresyncp (nnimap-capability "QRESYNC"))
1289 params groups sequences active uidvalidity modseq group
1291 ;; Go through the infos and gather the data needed to know
1292 ;; what and how to request the data.
1293 (dolist (info infos)
1294 (setq params (gnus-info-params info)
1295 group (nnimap-decode-gnus-group
1296 (gnus-group-real-name (gnus-info-group info)))
1297 active (cdr (assq 'active params))
1298 unexist (assq 'unexist (gnus-info-marks info))
1299 uidvalidity (cdr (assq 'uidvalidity params))
1300 modseq (cdr (assq 'modseq params)))
1301 (setf (nnimap-examined nnimap-object) group)
1308 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1309 (utf7-encode group t)
1310 (nnimap-quirk "QRESYNC")
1318 ;; If we don't have a UIDVALIDITY, then this is
1319 ;; the first time we've seen the group, so we
1320 ;; have to do a SELECT (which is slower than an
1321 ;; examine), but will tell us whether the group
1322 ;; is read-only or not.
1325 (if (and active uidvalidity unexist)
1326 ;; Fetch the last 100 flags.
1327 (setq start (max 1 (- (cdr active) 100)))
1328 (incf (nnimap-initial-resync nnimap-object))
1330 (push (list (nnimap-send-command "%s %S" command
1331 (utf7-encode group t))
1332 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1333 start group command)
1337 (defun nnimap-quirk (command)
1338 (let ((quirk (assoc command nnimap-quirks)))
1339 ;; If this server is of a type that matches a quirk, then return
1340 ;; the "quirked" command instead of the proper one.
1341 (if (or (null quirk)
1342 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1346 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1347 (when (and sequences
1348 (nnimap-change-group nil server t)
1349 ;; Check that the process is still alive.
1350 (get-buffer-process (nnimap-buffer))
1351 (memq (process-status (get-buffer-process (nnimap-buffer)))
1353 (with-current-buffer (nnimap-buffer)
1354 ;; Wait for the final data to trickle in.
1355 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1359 ;; Now we should have most of the data we need, no matter
1360 ;; whether we're QRESYNCING, fetching all the flags from
1361 ;; scratch, or just fetching the last 100 flags per group.
1362 (nnimap-update-infos (nnimap-flags-to-marks
1364 (nreverse sequences)))
1366 ;; Finally, just return something resembling an active file in
1367 ;; the nntp buffer, so that the agent can save the info, too.
1368 (with-current-buffer nntp-server-buffer
1370 (dolist (info infos)
1371 (let* ((group (gnus-info-group info))
1372 (active (gnus-active group)))
1374 (insert (format "%S %d %d y\n"
1375 (decode-coding-string
1376 (gnus-group-real-name group) 'utf-8)
1378 (car active)))))))))))
1380 (defun nnimap-update-infos (flags infos)
1381 (dolist (info infos)
1382 (let* ((group (nnimap-decode-gnus-group
1383 (gnus-group-real-name (gnus-info-group info))))
1384 (marks (cdr (assoc group flags))))
1386 (nnimap-update-info info marks)))))
1388 (defun nnimap-update-info (info marks)
1389 (destructuring-bind (existing flags high low uidnext start-article
1390 permanent-flags uidvalidity
1391 vanished highestmodseq) marks
1393 ;; Ignore groups with no UIDNEXT/marks. This happens for
1394 ;; completely empty groups.
1395 ((and (not existing)
1397 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1399 (gnus-set-active (gnus-info-group info) active))))
1400 ;; We have a mismatch between the old and new UIDVALIDITY
1401 ;; identifiers, so we have to re-request the group info (the next
1402 ;; time). This virtually never happens.
1403 ((let ((old-uidvalidity
1404 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1405 (and old-uidvalidity
1406 (not (equal old-uidvalidity uidvalidity))
1407 (or (not start-article)
1408 (> start-article 1))))
1409 (gnus-group-remove-parameter info 'uidvalidity)
1410 (gnus-group-remove-parameter info 'modseq))
1411 ;; We have the data needed to update.
1413 (let* ((group (gnus-info-group info))
1414 (completep (and start-article
1415 (= start-article 1)))
1416 (active (or (gnus-active group)
1417 (cdr (assq 'active (gnus-info-params info))))))
1419 (setq high (1- uidnext)))
1420 ;; First set the active ranges based on high/low.
1422 (not (gnus-active group)))
1423 (gnus-set-active group
1426 (cons (min (or low (car active))
1428 (max (or high (cdr active))
1433 ;; No articles in this group.
1434 (cons uidnext (1- uidnext)))
1436 (cons start-article (1- start-article)))
1438 ;; No articles and no uidnext.
1440 (gnus-set-active group
1442 (or high (1- uidnext)))))
1443 ;; See whether this is a read-only group.
1444 (unless (eq permanent-flags 'not-scanned)
1445 (gnus-group-set-parameter
1446 info 'permanent-flags
1447 (and (or (memq '%* permanent-flags)
1448 (memq '%Seen permanent-flags))
1450 ;; Update marks and read articles if this isn't a
1451 ;; read-only IMAP group.
1452 (when (setq permanent-flags
1453 (cdr (assq 'permanent-flags (gnus-info-params info))))
1454 (if (and highestmodseq
1455 (not start-article))
1456 ;; We've gotten the data by QRESYNCing.
1457 (nnimap-update-qresync-info
1458 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1459 ;; Do normal non-QRESYNC flag updates.
1460 ;; Update the list of read articles.
1462 (gnus-compress-sequence
1463 (gnus-set-difference
1464 (gnus-set-difference
1466 (cdr (assoc '%Seen flags)))
1467 (cdr (assoc '%Flagged flags)))))
1468 (read (gnus-range-difference
1469 (cons start-article high) unread)))
1470 (when (> start-article 1)
1473 (if (> start-article 1)
1474 (gnus-sorted-range-intersection
1475 (cons 1 (1- start-article))
1476 (gnus-info-read info))
1477 (gnus-info-read info))
1479 (when (or (not (listp permanent-flags))
1480 (memq '%Seen permanent-flags))
1481 (gnus-info-set-read info read))
1482 ;; Update the marks.
1483 (setq marks (gnus-info-marks info))
1484 (dolist (type (cdr nnimap-mark-alist))
1485 (when (or (not (listp permanent-flags))
1486 (memq (car (assoc (caddr type) flags))
1488 (memq '%* permanent-flags))
1489 (let ((old-marks (assoc (car type) marks))
1491 (gnus-compress-sequence
1492 (cdr (or (assoc (caddr type) flags) ; %Flagged
1493 (assoc (intern (cadr type) obarray) flags)
1494 (assoc (cadr type) flags)))))) ; "\Flagged"
1495 (setq marks (delq old-marks marks))
1497 (when (and old-marks
1498 (> start-article 1))
1499 (setq old-marks (gnus-range-difference
1501 (cons start-article high)))
1502 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1504 (push (cons (car type) new-marks) marks)))))
1505 ;; Keep track of non-existing articles.
1506 (let* ((old-unexists (assq 'unexist marks))
1507 (active (gnus-active group))
1510 (gnus-range-difference
1512 (gnus-compress-sequence existing))
1515 (gnus-list-range-difference
1516 existing (gnus-active group))))))
1517 (when (> (car active) 1)
1518 (setq unexists (gnus-range-add
1519 (cons 1 (1- (car active)))
1522 (setcdr old-unexists unexists)
1523 (push (cons 'unexist unexists) marks)))
1524 (gnus-info-set-marks info marks t))))
1525 ;; Tell Gnus whether there are any \Recent messages in any of
1527 (let ((recent (cdr (assoc '%Recent flags))))
1530 (> (car (last recent)) (cdr active)))
1531 (push (list (cons (gnus-group-real-name group) 0))
1532 nnmail-split-history)))
1533 ;; Note the active level for the next run-through.
1534 (gnus-group-set-parameter info 'active (gnus-active group))
1535 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1536 (gnus-group-set-parameter info 'modseq highestmodseq)
1537 (nnimap-store-info info (gnus-active group)))))))
1539 (defun nnimap-update-qresync-info (info existing vanished flags)
1540 ;; Add all the vanished articles to the list of read articles.
1545 (gnus-range-add (gnus-info-read info)
1547 (cdr (assq '%Flagged flags)))
1548 (cdr (assq '%Seen flags))))
1549 (let ((marks (gnus-info-marks info)))
1550 (dolist (type (cdr nnimap-mark-alist))
1551 (let ((ticks (assoc (car type) marks))
1553 (cdr (or (assoc (caddr type) flags) ; %Flagged
1554 (assoc (intern (cadr type) obarray) flags)
1555 (assoc (cadr type) flags))))) ; "\Flagged"
1556 (setq marks (delq ticks marks))
1558 ;; Add the new marks we got.
1559 (setq ticks (gnus-add-to-range ticks new-marks))
1560 ;; Remove the marks from messages that don't have them.
1561 (setq ticks (gnus-remove-from-range
1563 (gnus-compress-sequence
1564 (gnus-sorted-complement existing new-marks))))
1566 (push (cons (car type) ticks) marks)))
1567 (gnus-info-set-marks info marks t))
1568 ;; Add vanished to the list of unexisting articles.
1570 (let* ((old-unexists (assq 'unexist marks))
1571 (unexists (gnus-range-add (cdr old-unexists) vanished)))
1573 (setcdr old-unexists unexists)
1574 (push (cons 'unexist unexists) marks)))
1575 (gnus-info-set-marks info marks t))))
1577 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1578 (if (zerop (length irange))
1581 (dolist (elem (split-string irange ","))
1583 (if (string-match ":" elem)
1584 (let ((numbers (split-string elem ":")))
1585 (cons (string-to-number (car numbers))
1586 (string-to-number (cadr numbers))))
1587 (string-to-number elem))
1589 (nreverse result))))
1591 (defun nnimap-store-info (info active)
1592 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1593 (entry (assoc group nnimap-current-infos)))
1595 (setcdr entry (list info active))
1596 (push (list group info active) nnimap-current-infos))))
1598 (defun nnimap-flags-to-marks (groups)
1599 (let (data group totalp uidnext articles start-article mark permanent-flags
1600 uidvalidity vanished highestmodseq)
1601 (dolist (elem groups)
1602 (setq group (car elem)
1603 uidnext (nth 1 elem)
1604 start-article (nth 2 elem)
1605 permanent-flags (nth 3 elem)
1606 uidvalidity (nth 4 elem)
1607 vanished (nth 5 elem)
1608 highestmodseq (nth 6 elem)
1609 articles (nthcdr 7 elem))
1610 (let ((high (caar articles))
1612 (dolist (article articles)
1613 (setq low (car article))
1614 (push (car article) existing)
1615 (dolist (flag (cdr article))
1616 (setq mark (assoc flag marks))
1618 (push (list flag (car article)) marks)
1619 (setcdr mark (cons (car article) (cdr mark))))))
1620 (push (list group existing marks high low uidnext start-article
1621 permanent-flags uidvalidity vanished highestmodseq)
1625 (defun nnimap-parse-flags (sequences)
1626 (goto-char (point-min))
1627 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1629 (subst-char-in-region (point-min) (point-max)
1631 ;; Remove any MODSEQ entries in the buffer, because they may contain
1632 ;; numbers that are too large for 32-bit Emacsen.
1633 (while (re-search-forward " MODSEQ ([0-9]+)" nil t)
1634 (replace-match "" t t))
1635 (goto-char (point-min))
1636 (let (start end articles groups uidnext elems permanent-flags
1637 uidvalidity vanished highestmodseq)
1638 (dolist (elem sequences)
1639 (destructuring-bind (group-sequence flag-sequence totalp group command)
1641 (setq start (point))
1643 ;; The EXAMINE was successful.
1644 (search-forward (format "\n%d OK " group-sequence) nil t)
1649 (setq permanent-flags
1650 (if (equal command "SELECT")
1651 (and (search-forward "PERMANENTFLAGS "
1652 (or end (point-min)) t)
1653 (read (current-buffer)))
1657 (and (search-forward "UIDNEXT "
1658 (or end (point-min)) t)
1659 (read (current-buffer))))
1662 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1663 (or end (point-min)) t)
1664 ;; Store UIDVALIDITY as a string, as it's
1665 ;; too big for 32-bit Emacsen, usually.
1669 (and (eq flag-sequence 'qresync)
1670 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1671 (or end (point-min)) t)
1675 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1676 (or end (point-min)) t)
1680 ;; The UID FETCH FLAGS was successful.
1681 (or (eq flag-sequence 'qresync)
1682 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1683 (if (eq flag-sequence 'qresync)
1687 (setq start (point))
1689 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1691 (setq elems (read (current-buffer)))
1692 (push (cons (cadr (memq 'UID elems))
1693 (cadr (memq 'FLAGS elems)))
1695 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1696 vanished highestmodseq)
1699 (if (eq flag-sequence 'qresync)
1702 (setq articles nil))))
1705 (defun nnimap-find-process-buffer (buffer)
1706 (cadr (assoc buffer nnimap-connection-alist)))
1708 (deffoo nnimap-request-post (&optional server)
1709 (setq nnimap-status-string "Read-only server")
1712 (defvar gnus-refer-thread-use-nnir) ;; gnus-sum.el
1713 (declare-function gnus-fetch-headers "gnus-sum"
1714 (articles &optional limit force-new dependencies))
1716 (autoload 'nnir-search-thread "nnir")
1718 (deffoo nnimap-request-thread (header &optional group server)
1720 (setq group (nnimap-decode-gnus-group group)))
1721 (if gnus-refer-thread-use-nnir
1722 (nnir-search-thread header)
1723 (when (nnimap-change-group group server)
1724 (let* ((cmd (nnimap-make-thread-query header))
1725 (result (with-current-buffer (nnimap-buffer)
1726 (nnimap-command "UID SEARCH %s" cmd))))
1730 (delete 0 (mapcar #'string-to-number
1731 (cdr (assoc "SEARCH" (cdr result))))))
1734 (defun nnimap-change-group (group &optional server no-reconnect read-only)
1735 "Change group to GROUP if non-nil.
1736 If SERVER is set, check that server is connected, otherwise retry
1737 to reconnect, unless NO-RECONNECT is set to t. Return nil if
1738 unsuccessful in connecting.
1739 If GROUP is nil, return t.
1740 If READ-ONLY is set, send EXAMINE rather than SELECT to the server.
1741 Return the server's response to the SELECT or EXAMINE command."
1742 (let ((open-result t))
1744 (not (nnimap-server-opened server)))
1745 (setq open-result (nnimap-open-server server nil no-reconnect)))
1752 (with-current-buffer (nnimap-buffer)
1753 (let ((result (nnimap-command "%s %S"
1757 (utf7-encode group t))))
1759 (setf (nnimap-group nnimap-object) group
1760 (nnimap-select-result nnimap-object) result)
1763 (defun nnimap-find-connection (buffer)
1764 "Find the connection delivering to BUFFER."
1765 (let ((entry (assoc buffer nnimap-connection-alist)))
1767 (if (and (buffer-name (cadr entry))
1768 (get-buffer-process (cadr entry))
1769 (memq (process-status (get-buffer-process (cadr entry)))
1771 (get-buffer-process (cadr entry))
1772 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1775 (defvar nnimap-sequence 0)
1777 (defun nnimap-send-command (&rest args)
1778 (setf (nnimap-last-command-time nnimap-object) (current-time))
1779 (process-send-string
1780 (get-buffer-process (current-buffer))
1783 (incf nnimap-sequence)
1784 (apply #'format args)
1785 (if (nnimap-newlinep nnimap-object)
1788 ;; Some servers apparently can't have many outstanding
1789 ;; commands, so throttle them.
1790 (unless nnimap-streaming
1791 (nnimap-wait-for-response nnimap-sequence))
1794 (defvar nnimap-record-commands nil
1795 "If non-nil, log commands to the \"*imap log*\" buffer.")
1797 (defun nnimap-log-buffer ()
1798 (let ((name "*imap log*"))
1799 (or (get-buffer name)
1800 (with-current-buffer (get-buffer-create name)
1801 (when (boundp 'window-point-insertion-type)
1802 (make-local-variable 'window-point-insertion-type)
1803 (setq window-point-insertion-type t))
1804 (current-buffer)))))
1806 (defun nnimap-log-command (command)
1807 (when nnimap-record-commands
1808 (with-current-buffer (nnimap-log-buffer)
1809 (goto-char (point-max))
1810 (insert (format-time-string "%H:%M:%S")
1811 " [" nnimap-address "] "
1812 (if nnimap-inhibit-logging
1817 (defun nnimap-command (&rest args)
1819 (let* ((sequence (apply #'nnimap-send-command args))
1820 (response (nnimap-get-response sequence)))
1821 (if (equal (caar response) "OK")
1823 (nnheader-report 'nnimap "%s"
1824 (mapconcat (lambda (a)
1826 (car response) " "))
1829 (defun nnimap-get-response (sequence)
1830 (nnimap-wait-for-response sequence)
1831 (nnimap-parse-response))
1833 (defun nnimap-wait-for-connection (&optional regexp)
1834 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1836 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1837 (let ((process (get-buffer-process (current-buffer))))
1838 (goto-char (point-min))
1839 (while (and (memq (process-status process)
1841 (not (re-search-forward regexp nil t)))
1842 (nnheader-accept-process-output process)
1843 (goto-char (point-min)))
1845 (and (looking-at (or response-regexp regexp))
1848 (defun nnimap-wait-for-response (sequence &optional messagep)
1849 (let ((process (get-buffer-process (current-buffer)))
1853 (goto-char (point-max))
1854 (while (and (setq openp (memq (process-status process)
1857 ;; Skip past any "*" lines that the server has
1859 (while (and (not (bobp))
1862 (looking-at "\\*"))))
1863 (not (looking-at (format "%d .*\n" sequence)))))
1865 (nnheader-message-maybe
1866 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
1868 (if (not (zerop (nnimap-initial-resync nnimap-object)))
1869 (format " (initial sync of %d group%s; please wait)"
1870 (nnimap-initial-resync nnimap-object)
1871 (if (= (nnimap-initial-resync nnimap-object) 1)
1875 (nnheader-accept-process-output process)
1876 (goto-char (point-max)))
1877 (setf (nnimap-initial-resync nnimap-object) 0)
1882 ;; The user hit C-g while we were waiting: kill the process, in case
1883 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1885 (delete-process process)
1888 (defun nnimap-parse-response ()
1889 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1891 (dolist (line lines)
1892 (push (cdr (nnimap-parse-line line)) result))
1893 ;; Return the OK/error code first, and then all the "continuation
1894 ;; lines" afterwards.
1896 (nreverse result))))
1898 ;; Parse an IMAP response line lightly. They look like
1899 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1900 ;; the lines into a list of strings and lists of string.
1901 (defun nnimap-parse-line (line)
1904 (mm-disable-multibyte)
1906 (goto-char (point-min))
1908 (if (eql (setq char (following-char)) ? )
1916 (if (search-forward "]" (line-end-position) 'move)
1923 (if (search-forward ")" (line-end-position) 'move)
1930 (1- (or (search-forward "\"" (line-end-position) 'move)
1933 (buffer-substring (point) (if (search-forward " " nil t)
1935 (goto-char (point-max))))))
1937 (nreverse result))))
1939 (defun nnimap-last-response-string ()
1942 (let ((end (point)))
1946 (while (and (not (bobp))
1947 (eql (following-char) ?*))
1949 (unless (eql (following-char) ?*)
1951 (buffer-substring (point) end))))
1953 (defvar nnimap-incoming-split-list nil)
1955 (defun nnimap-fetch-inbox (articles)
1957 (nnimap-wait-for-response
1958 (nnimap-send-command
1960 (nnimap-article-ranges articles)
1961 (format "(UID %s%s)"
1967 (nnimap-split-download-body-default
1975 (defun nnimap-split-incoming-mail ()
1976 (with-current-buffer (nnimap-buffer)
1977 (let ((nnimap-incoming-split-list nil)
1978 (nnmail-split-methods
1980 ((eq nnimap-split-methods 'default)
1981 nnmail-split-methods)
1982 (nnimap-split-methods
1983 nnimap-split-methods)
1985 'nnmail-split-fancy)))
1986 (nnmail-split-fancy (or nnimap-split-fancy
1987 nnmail-split-fancy))
1988 (nnmail-inhibit-default-split-group t)
1989 (groups (nnimap-get-groups))
1992 (nnimap-command "SELECT %S" nnimap-inbox)
1993 (setf (nnimap-group nnimap-object) nnimap-inbox)
1994 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1996 (nnimap-fetch-inbox new-articles)
1997 (nnimap-transform-split-mail)
1998 (nnheader-ms-strip-cr)
2000 (nnmail-split-incoming (current-buffer)
2001 #'nnimap-save-mail-spec
2003 #'nnimap-dummy-active-number
2004 #'nnimap-save-mail-spec)
2005 (when nnimap-incoming-split-list
2006 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
2007 sequences junk-articles)
2008 ;; Create any groups that doesn't already exist on the
2010 (dolist (spec specs)
2011 (when (and (not (member (car spec) groups))
2012 (not (eq (car spec) 'junk)))
2013 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
2014 ;; Then copy over all the messages.
2016 (dolist (spec specs)
2017 (let ((group (car spec))
2018 (ranges (cdr spec)))
2019 (if (eq group 'junk)
2020 (setq junk-articles ranges)
2021 (push (list (nnimap-send-command
2023 (nnimap-article-ranges ranges)
2024 (utf7-encode group t))
2027 ;; Wait for the last COPY response...
2029 (nnimap-wait-for-response (caar sequences))
2030 ;; And then mark the successful copy actions as deleted,
2031 ;; and possibly expunge them.
2032 (nnimap-mark-and-expunge-incoming
2033 (nnimap-parse-copied-articles sequences)))
2034 (nnimap-mark-and-expunge-incoming junk-articles)))))))
2036 (defun nnimap-mark-and-expunge-incoming (range)
2038 (setq range (nnimap-article-ranges range))
2041 (nnimap-send-command
2042 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
2044 ;; If the server supports it, we now delete the message we have
2045 ;; just copied over.
2046 ((nnimap-capability "UIDPLUS")
2047 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
2048 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
2049 ;; user has configured it.
2051 (setq sequence (nnimap-send-command "EXPUNGE"))))
2052 (nnimap-wait-for-response sequence))))
2054 (defun nnimap-parse-copied-articles (sequences)
2055 (let (sequence copied range)
2056 (goto-char (point-min))
2057 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
2058 (setq sequence (string-to-number (match-string 1)))
2059 (when (setq range (cadr (assq sequence sequences)))
2060 (push (gnus-uncompress-range range) copied)))
2061 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
2063 (defun nnimap-new-articles (flags)
2065 (dolist (elem flags)
2066 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
2068 (push (car elem) new)))
2069 (gnus-compress-sequence (nreverse new))))
2071 (defun nnimap-make-split-specs (list)
2075 (destructuring-bind (article spec) elem
2076 (dolist (group (delete nil (mapcar #'car spec)))
2077 (unless (setq entry (assoc group specs))
2078 (push (setq entry (list group)) specs))
2079 (setcdr entry (cons article (cdr entry))))))
2080 (dolist (entry specs)
2081 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
2084 (defun nnimap-transform-split-mail ()
2085 (goto-char (point-min))
2086 (let (article bytes)
2089 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2090 (delete-region (point) (progn (forward-line 1) (point)))
2093 (setq article (match-string 1)
2094 bytes (nnimap-get-length))
2095 (delete-region (line-beginning-position) (line-end-position))
2096 ;; Insert MMDF separator, and a way to remember what this
2098 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
2099 (forward-char (1+ bytes))
2100 (setq bytes (nnimap-get-length))
2101 (delete-region (line-beginning-position) (line-end-position))
2102 ;; There's a body; skip past that.
2104 (forward-char (1+ bytes))
2105 (delete-region (line-beginning-position) (line-end-position)))))))
2107 (defun nnimap-dummy-active-number (group &optional server)
2110 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
2112 (goto-char (point-min))
2113 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
2114 (error "Invalid nnimap mail")
2115 (setq article (string-to-number (match-string 1))))
2117 (if (eq group-art 'junk)
2118 (list (cons 'junk 1))
2120 nnimap-incoming-split-list)))
2122 (defun nnimap-make-thread-query (header)
2123 (let* ((id (mail-header-id header))
2125 (or (mail-header-references header)
2129 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2131 (dolist (refid refs value)
2133 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2134 refid refid value)))))
2139 ;;; nnimap.el ends here