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))))
48 (require 'proto-stream)
50 (autoload 'auth-source-forget-user-or-password "auth-source")
51 (autoload 'auth-source-user-or-password "auth-source")
55 (defvoo nnimap-address nil
56 "The address of the IMAP server.")
58 (defvoo nnimap-server-port nil
60 If nnimap-stream is `ssl', this will default to `imaps'. If not,
61 it will default to `imap'.")
63 (defvoo nnimap-stream 'undecided
64 "How nnimap will talk to the IMAP server.
65 Values are `ssl', `network', `starttls' or `shell'.
66 The default is to try `ssl' first, and then `network'.")
68 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
69 (if (listp imap-shell-program)
70 (car imap-shell-program)
74 (defvoo nnimap-inbox nil
75 "The mail box where incoming mail arrives and should be split out of.")
77 (defvoo nnimap-split-methods nil
79 Uses the same syntax as nnmail-split-methods")
81 (defvoo nnimap-split-fancy nil
82 "Uses the same syntax as nnmail-split-fancy.")
84 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
85 "Articles with the flags in the list will not be considered when splitting.")
87 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
90 (defvoo nnimap-authenticator nil
91 "How nnimap authenticate itself to the server.
92 Possible choices are nil (use default methods) or `anonymous'.")
94 (defvoo nnimap-expunge t
95 "If non-nil, expunge articles after deleting them.
96 This is always done if the server supports UID EXPUNGE, but it's
97 not done by default on servers that doesn't support that command.")
99 (defvoo nnimap-streaming t
100 "If non-nil, try to use streaming commands with IMAP servers.
101 Switching this off will make nnimap slower, but it helps with
104 (defvoo nnimap-connection-alist nil)
106 (defvoo nnimap-current-infos nil)
108 (defvoo nnimap-fetch-partial-articles nil
109 "If non-nil, Gnus will fetch partial articles.
110 If t, nnimap will fetch only the first part. If a string, it
111 will fetch all parts that have types that match that string. A
112 likely value would be \"text/\" to automatically fetch all
115 (defvar nnimap-process nil)
117 (defvar nnimap-status-string "")
119 (defvar nnimap-split-download-body-default nil
120 "Internal variable with default value for `nnimap-split-download-body'.")
122 (defvar nnimap-keepalive-timer nil)
123 (defvar nnimap-process-buffers nil)
126 group process commands capabilities select-result newlinep server
127 last-command-time greeting examined stream-type)
129 (defvar nnimap-object nil)
131 (defvar nnimap-mark-alist
132 '((read "\\Seen" %Seen)
133 (tick "\\Flagged" %Flagged)
134 (reply "\\Answered" %Answered)
135 (expire "gnus-expire")
136 (dormant "gnus-dormant")
139 (download "gnus-download")
140 (forward "gnus-forward")))
142 (defvar nnimap-quirks
143 '(("QRESYNC" "Zimbra" "QRESYNC ")))
145 (defun nnimap-buffer ()
146 (nnimap-find-process-buffer nntp-server-buffer))
148 (defun nnimap-header-parameters ()
149 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
152 "BODY.PEEK[HEADER.FIELDS %s]"
153 "RFC822.HEADER.LINES %s")
154 (append '(Subject From Date Message-Id
155 References In-Reply-To Xref)
156 nnmail-extra-headers))))
158 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
159 (with-current-buffer nntp-server-buffer
161 (when (nnimap-possibly-change-group group server)
162 (with-current-buffer (nnimap-buffer)
164 (nnimap-wait-for-response
167 (nnimap-article-ranges (gnus-compress-sequence articles))
168 (nnimap-header-parameters))
170 (nnimap-transform-headers)
171 (nnheader-remove-cr-followed-by-lf))
172 (insert-buffer-substring
173 (nnimap-find-process-buffer (current-buffer))))
176 (defun nnimap-transform-headers ()
177 (goto-char (point-min))
178 (let (article bytes lines size string)
181 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
182 (delete-region (point) (progn (forward-line 1) (point)))
185 (setq article (match-string 1))
186 ;; Unfold quoted {number} strings.
187 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
188 (1+ (line-end-position)) t)
189 (setq size (string-to-number (match-string 1)))
190 (delete-region (+ (match-beginning 0) 2) (point))
191 (setq string (buffer-substring (point) (+ (point) size)))
192 (delete-region (point) (+ (point) size))
193 (insert (format "%S" string)))
194 (setq bytes (nnimap-get-length)
198 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
203 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
204 (let ((structure (ignore-errors
205 (read (current-buffer)))))
206 (while (and (consp structure)
207 (not (stringp (car structure))))
208 (setq structure (car structure)))
209 (setq lines (nth 7 structure))))
210 (delete-region (line-beginning-position) (line-end-position))
211 (insert (format "211 %s Article retrieved." article))
214 (insert (format "Chars: %s\n" size)))
216 (insert (format "Lines: %s\n" lines)))
217 (unless (re-search-forward "^\r$" nil t)
218 (goto-char (point-max)))
219 (delete-region (line-beginning-position) (line-end-position))
223 (defun nnimap-unfold-quoted-lines ()
224 ;; Unfold quoted {number} strings.
226 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
227 (setq size (string-to-number (match-string 1)))
228 (delete-region (1+ (match-beginning 0)) (point))
229 (setq string (buffer-substring (point) (+ (point) size)))
230 (delete-region (point) (+ (point) size))
231 (insert (format "%S" string)))))
233 (defun nnimap-get-length ()
234 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
235 (string-to-number (match-string 1))))
237 (defun nnimap-article-ranges (ranges)
241 (number-to-string ranges))
242 ((numberp (cdr ranges))
243 (format "%d:%d" (car ranges) (cdr ranges)))
245 (dolist (elem ranges)
248 (format "%d:%d" (car elem) (cdr elem))
249 (number-to-string elem))
251 (mapconcat #'identity (nreverse result) ",")))))
253 (deffoo nnimap-open-server (server &optional defs)
254 (if (nnimap-server-opened server)
256 (unless (assq 'nnimap-address defs)
257 (setq defs (append defs (list (list 'nnimap-address server)))))
258 (nnoo-change-server 'nnimap server defs)
259 (or (nnimap-find-connection nntp-server-buffer)
260 (nnimap-open-connection nntp-server-buffer))))
262 (defun nnimap-make-process-buffer (buffer)
264 (generate-new-buffer (format "*nnimap %s %s %s*"
265 nnimap-address nnimap-server-port
266 (gnus-buffer-exists-p buffer)))
267 (mm-disable-multibyte)
268 (buffer-disable-undo)
270 (set (make-local-variable 'after-change-functions) nil)
271 (set (make-local-variable 'nnimap-object)
272 (make-nnimap :server (nnoo-current-server 'nnimap)))
273 (push (list buffer (current-buffer)) nnimap-connection-alist)
274 (push (current-buffer) nnimap-process-buffers)
277 (defun nnimap-credentials (address ports &optional inhibit-create)
278 (let (port credentials)
279 ;; Request the credentials from all ports, but only query on the
280 ;; last port if all the previous ones have failed.
281 (while (and (null credentials)
282 (setq port (pop ports)))
284 (auth-source-user-or-password
285 '("login" "password") address port nil
291 (defun nnimap-keepalive ()
292 (let ((now (current-time)))
293 (dolist (buffer nnimap-process-buffers)
294 (when (buffer-name buffer)
295 (with-current-buffer buffer
296 (when (and nnimap-object
297 (nnimap-last-command-time nnimap-object)
301 (nnimap-last-command-time nnimap-object)))
302 ;; More than five minutes since the last command.
304 (nnimap-send-command "NOOP")))))))
306 (defun nnimap-open-connection (buffer)
307 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
308 ;; `ssl' when nnimap-server-port was nil. Sort of.
309 (when (and nnimap-server-port
310 (eq nnimap-stream 'undecided))
311 (setq nnimap-stream 'ssl))
313 (if (eq nnimap-stream 'undecided)
314 (loop for type in '(ssl network)
315 for stream = (let ((nnimap-stream type))
316 (nnimap-open-connection-1 buffer))
317 while (eq stream 'no-connect)
318 finally (return stream))
319 (nnimap-open-connection-1 buffer))))
320 (if (eq stream 'no-connect)
324 (defun nnimap-open-connection-1 (buffer)
325 (unless nnimap-keepalive-timer
326 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
328 (with-current-buffer (nnimap-make-process-buffer buffer)
329 (let* ((coding-system-for-read 'binary)
330 (coding-system-for-write 'binary)
334 ((or (eq nnimap-stream 'network)
335 (eq nnimap-stream 'starttls))
336 (nnheader-message 7 "Opening connection to %s..."
339 ((eq nnimap-stream 'shell)
340 (nnheader-message 7 "Opening connection to %s via shell..."
343 ((memq nnimap-stream '(ssl tls))
344 (nnheader-message 7 "Opening connection to %s via tls..."
346 '("143" "993" "imap" "imaps"))
348 (error "Unknown stream type: %s" nnimap-stream))))
349 (proto-stream-always-use-starttls t)
350 login-result credentials)
351 (when nnimap-server-port
352 (setq ports (append ports (list nnimap-server-port))))
353 (destructuring-bind (stream greeting capabilities stream-type)
354 (open-protocol-stream
355 "*nnimap*" (current-buffer) nnimap-address (car (last ports))
357 :shell-command nnimap-shell-program
358 :capability-command "1 CAPABILITY\r\n"
361 (lambda (capabilities)
362 (when (gnus-string-match-p "STARTTLS" capabilities)
364 (setf (nnimap-process nnimap-object) stream)
365 (setf (nnimap-stream-type nnimap-object) stream-type)
368 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
369 nnimap-address port nnimap-stream)
371 (gnus-set-process-query-on-exit-flag stream nil)
372 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
373 (nnheader-report 'nnimap "%s" greeting)
374 ;; Store the greeting (for debugging purposes).
375 (setf (nnimap-greeting nnimap-object) greeting)
376 (setf (nnimap-capabilities nnimap-object)
378 (split-string capabilities)))
379 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
380 (if (not (setq credentials
381 (if (eq nnimap-authenticator 'anonymous)
383 (message-make-address))
385 ;; First look for the credentials based
386 ;; on the virtual server name.
388 (nnoo-current-server 'nnimap) ports t)
389 ;; Then look them up based on the
391 (nnimap-credentials nnimap-address ports)))))
392 (setq nnimap-object nil)
394 (nnimap-login (car credentials) (cadr credentials)))
395 (unless (car login-result)
396 ;; If the login failed, then forget the credentials
397 ;; that are now possibly cached.
398 (dolist (host (list (nnoo-current-server 'nnimap)
401 (dolist (element '("login" "password"))
402 (auth-source-forget-user-or-password
403 element host port))))
404 (delete-process (nnimap-process nnimap-object))
405 (setq nnimap-object nil))))
407 (when (nnimap-capability "QRESYNC")
408 (nnimap-command "ENABLE QRESYNC"))
409 (nnimap-process nnimap-object))))))))
411 (autoload 'rfc2104-hash "rfc2104")
413 (defun nnimap-login (user password)
415 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
416 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
417 ;; and we're using an encrypted connection.
418 ((and (not (nnimap-capability "LOGINDISABLED"))
419 (eq (nnimap-stream-type nnimap-object) 'tls))
420 (nnimap-command "LOGIN %S %S" user password))
421 ((nnimap-capability "AUTH=CRAM-MD5")
423 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
424 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
426 (get-buffer-process (current-buffer))
428 (base64-encode-string
430 (rfc2104-hash 'md5 64 16 password
431 (base64-decode-string challenge))))
433 (nnimap-wait-for-response sequence)))
434 ((not (nnimap-capability "LOGINDISABLED"))
435 (nnimap-command "LOGIN %S %S" user password))
436 ((nnimap-capability "AUTH=PLAIN")
438 "AUTHENTICATE PLAIN %s"
439 (base64-encode-string
440 (format "\000%s\000%s"
441 (nnimap-quote-specials user)
442 (nnimap-quote-specials password)))))))
444 (defun nnimap-quote-specials (string)
447 (goto-char (point-min))
448 (while (re-search-forward "[\\\"]" nil t)
454 (defun nnimap-find-parameter (parameter elems)
458 ((equal (car elem) parameter)
459 (setq result (cdr elem)))
460 ((and (equal (car elem) "OK")
462 (equal (caadr elem) parameter))
463 (setq result (cdr (cadr elem))))))
466 (deffoo nnimap-close-server (&optional server)
467 (when (nnoo-change-server 'nnimap server nil)
469 (delete-process (get-buffer-process (nnimap-buffer))))
470 (nnoo-close-server 'nnimap server)
473 (deffoo nnimap-request-close ()
476 (deffoo nnimap-server-opened (&optional server)
477 (and (nnoo-current-server-p 'nnimap server)
479 (gnus-buffer-live-p nntp-server-buffer)
480 (nnimap-find-connection nntp-server-buffer)))
482 (deffoo nnimap-status-message (&optional server)
483 nnimap-status-string)
485 (deffoo nnimap-request-article (article &optional group server to-buffer)
486 (with-current-buffer nntp-server-buffer
487 (let ((result (nnimap-possibly-change-group group server))
489 (when (stringp article)
490 (setq article (nnimap-find-article-by-message-id group article)))
494 (with-current-buffer (nnimap-buffer)
496 (when nnimap-fetch-partial-articles
497 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
498 (goto-char (point-min))
499 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
500 (setq structure (ignore-errors
501 (let ((start (point)))
503 (downcase-region start (point))
505 (read (current-buffer))))
506 parts (nnimap-find-wanted-parts structure))))
508 (nnimap-get-partial-article article parts structure)
509 (nnimap-get-whole-article article))
510 (let ((buffer (current-buffer)))
511 (with-current-buffer (or to-buffer nntp-server-buffer)
513 (insert-buffer-substring buffer)
514 (nnheader-ms-strip-cr)
515 (cons group article)))))))))
517 (deffoo nnimap-request-head (article &optional group server to-buffer)
518 (when (nnimap-possibly-change-group group server)
519 (with-current-buffer (nnimap-buffer)
520 (when (stringp article)
521 (setq article (nnimap-find-article-by-message-id group article)))
524 (nnimap-get-whole-article
525 article (format "UID FETCH %%d %s"
526 (nnimap-header-parameters)))
527 (let ((buffer (current-buffer)))
528 (with-current-buffer (or to-buffer nntp-server-buffer)
530 (insert-buffer-substring buffer)
531 (nnheader-ms-strip-cr)
532 (cons group article)))))))
534 (defun nnimap-get-whole-article (article &optional command)
539 "UID FETCH %d BODY.PEEK[]"
540 "UID FETCH %d RFC822.PEEK"))
542 ;; Check that we really got an article.
543 (goto-char (point-min))
544 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
547 ;; Remove any data that may have arrived before the FETCH data.
550 (delete-region (point-min) (point)))
551 (let ((bytes (nnimap-get-length)))
552 (delete-region (line-beginning-position)
553 (progn (forward-line 1) (point)))
554 (goto-char (+ (point) bytes))
555 (delete-region (point) (point-max)))
558 (defun nnimap-capability (capability)
559 (member capability (nnimap-capabilities nnimap-object)))
561 (defun nnimap-ver4-p ()
562 (nnimap-capability "IMAP4REV1"))
564 (defun nnimap-get-partial-article (article parts structure)
567 "UID FETCH %d (%s %s)"
573 (mapconcat (lambda (part)
574 (format "BODY.PEEK[%s]" part))
576 (mapconcat (lambda (part)
577 (format "RFC822.PEEK[%s]" part))
580 (nnimap-convert-partial-article structure))))
582 (defun nnimap-convert-partial-article (structure)
583 ;; First just skip past the headers.
584 (goto-char (point-min))
585 (let ((bytes (nnimap-get-length))
587 ;; Delete "FETCH" line.
588 (delete-region (line-beginning-position)
589 (progn (forward-line 1) (point)))
590 (goto-char (+ (point) bytes))
591 ;; Collect all the body parts.
592 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
593 (setq id (match-string 1)
594 bytes (or (nnimap-get-length) 0))
596 (delete-region (point) (progn (forward-line 1) (point)))
597 (push (list id (buffer-substring (point) (+ (point) bytes)))
599 (delete-region (point) (+ (point) bytes)))
600 ;; Delete trailing junk.
601 (delete-region (point) (point-max))
602 ;; Now insert all the parts again where they fit in the structure.
603 (nnimap-insert-partial-structure structure parts)
606 (defun nnimap-insert-partial-structure (structure parts &optional subp)
608 (let ((bstruc structure))
609 (while (consp (car bstruc))
611 (setq type (car bstruc))
612 (setq bstruc (car (cdr bstruc)))
613 (let ((has-boundary (member "boundary" bstruc)))
615 (setq boundary (cadr has-boundary)))))
617 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
618 (downcase type) boundary)))
619 (while (not (stringp (car structure)))
620 (insert "\n--" boundary "\n")
621 (if (consp (caar structure))
622 (nnimap-insert-partial-structure (pop structure) parts t)
623 (let ((bit (pop structure)))
624 (insert (format "Content-type: %s/%s"
625 (downcase (nth 0 bit))
626 (downcase (nth 1 bit))))
627 (if (member "CHARSET" (nth 2 bit))
629 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
631 (insert (format "Content-transfer-encoding: %s\n"
634 (when (assoc (nth 9 bit) parts)
635 (insert (cadr (assoc (nth 9 bit) parts)))))))
636 (insert "\n--" boundary "--\n")))
638 (defun nnimap-find-wanted-parts (structure)
639 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
641 (defun nnimap-find-wanted-parts-1 (structure prefix)
644 (while (consp (car structure))
645 (let ((sub (pop structure)))
646 (if (consp (car sub))
647 (push (nnimap-find-wanted-parts-1
648 sub (if (string= prefix "")
649 (number-to-string num)
650 (format "%s.%s" prefix num)))
652 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
653 (id (if (string= prefix "")
654 (number-to-string num)
655 (format "%s.%s" prefix num))))
656 (setcar (nthcdr 9 sub) id)
657 (when (if (eq nnimap-fetch-partial-articles t)
659 (string-match nnimap-fetch-partial-articles type))
664 (deffoo nnimap-request-group (group &optional server dont-check info)
665 (let ((result (nnimap-possibly-change-group
666 ;; Don't SELECT the group if we're going to select it
668 (if (and (not dont-check)
669 (assoc group nnimap-current-infos))
673 articles active marks high low)
674 (with-current-buffer nntp-server-buffer
677 (setq active (nth 2 (assoc group nnimap-current-infos))))
678 (insert (format "211 %d %d %d %S\n"
679 (- (cdr active) (car active))
683 (with-current-buffer (nnimap-buffer)
685 (let ((group-sequence
686 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
688 (nnimap-send-command "UID FETCH 1:* FLAGS")))
689 (setf (nnimap-group nnimap-object) group)
690 (nnimap-wait-for-response flag-sequence)
692 (nnimap-flags-to-marks
694 (list (list group-sequence flag-sequence
695 1 group "SELECT")))))
698 (nnimap-update-infos marks (list info))
699 (nnimap-store-info info (gnus-active (gnus-info-group info))))
700 (goto-char (point-max))
701 (let ((uidnext (nth 5 (car marks))))
702 (setq high (or (if uidnext
706 low (or (nth 4 (car marks)) uidnext 1)))))
710 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
713 (deffoo nnimap-request-create-group (group &optional server args)
714 (when (nnimap-possibly-change-group nil server)
715 (with-current-buffer (nnimap-buffer)
716 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
718 (deffoo nnimap-request-delete-group (group &optional force server)
719 (when (nnimap-possibly-change-group nil server)
720 (with-current-buffer (nnimap-buffer)
721 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
723 (deffoo nnimap-request-rename-group (group new-name &optional server)
724 (when (nnimap-possibly-change-group nil server)
725 (with-current-buffer (nnimap-buffer)
726 (nnimap-unselect-group)
727 (car (nnimap-command "RENAME %S %S"
728 (utf7-encode group t) (utf7-encode new-name t))))))
730 (defun nnimap-unselect-group ()
731 ;; Make sure we don't have this group open read/write by asking
732 ;; to examine a mailbox that doesn't exist. This seems to be
733 ;; the only way that allows us to reliably go back to unselected
735 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
737 (deffoo nnimap-request-expunge-group (group &optional server)
738 (when (nnimap-possibly-change-group group server)
739 (with-current-buffer (nnimap-buffer)
740 (car (nnimap-command "EXPUNGE")))))
742 (defun nnimap-get-flags (spec)
745 (with-current-buffer (nnimap-buffer)
747 (nnimap-wait-for-response (nnimap-send-command
748 "UID FETCH %s FLAGS" spec))
750 (subst-char-in-region (point-min) (point-max)
752 (goto-char (point-min))
753 (while (search-forward " FETCH " end t)
754 (setq elems (read (current-buffer)))
755 (push (cons (cadr (memq 'UID elems))
756 (cadr (memq 'FLAGS elems)))
758 (nreverse articles)))
760 (deffoo nnimap-close-group (group &optional server)
763 (deffoo nnimap-request-move-article (article group server accept-form
764 &optional last internal-move-group)
766 (mm-disable-multibyte)
767 (when (funcall (if internal-move-group
769 'nnimap-request-article)
770 article group server (current-buffer))
771 ;; If the move is internal (on the same server), just do it the easy
773 (let ((message-id (message-field-value "message-id")))
774 (if internal-move-group
776 (with-current-buffer (nnimap-buffer)
777 (nnimap-command "UID COPY %d %S"
779 (utf7-encode internal-move-group t)))))
781 (nnimap-delete-article article)
782 (cons internal-move-group
783 (or (nnimap-find-uid-response "COPYUID" (cadr result))
784 (nnimap-find-article-by-message-id
785 internal-move-group message-id)))))
786 ;; Move the article to a different method.
787 (let ((result (eval accept-form)))
789 (nnimap-delete-article article)
792 (deffoo nnimap-request-expire-articles (articles group &optional server force)
796 ((not (nnimap-possibly-change-group group server))
799 (eq nnmail-expiry-target 'delete))
800 (unless (nnimap-delete-article (gnus-compress-sequence articles))
801 (nnheader-message 7 "Article marked for deletion, but not expunged."))
804 (let ((deletable-articles
806 (eq nnmail-expiry-wait 'immediate))
808 (gnus-sorted-intersection
810 (nnimap-find-expired-articles group)))))
811 (if (null deletable-articles)
813 (if (eq nnmail-expiry-target 'delete)
814 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
815 (setq deletable-articles
816 (nnimap-process-expiry-targets
817 deletable-articles group server)))
818 ;; Return the articles we didn't delete.
819 (gnus-sorted-complement articles deletable-articles))))))
821 (defun nnimap-process-expiry-targets (articles group server)
822 (let ((deleted-articles nil))
824 ;; shortcut further processing if we're going to delete the articles
825 ((eq nnmail-expiry-target 'delete)
826 (setq deleted-articles articles)
828 ;; or just move them to another folder on the same IMAP server
829 ((and (not (functionp nnmail-expiry-target))
830 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
831 (gnus-server-to-method
832 (format "nnimap:%s" server))))
833 (and (nnimap-possibly-change-group group server)
834 (with-current-buffer (nnimap-buffer)
835 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
838 (nnimap-article-ranges (gnus-compress-sequence articles))
839 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
840 (setq deleted-articles articles)))
843 (dolist (article articles)
844 (let ((target nnmail-expiry-target))
846 (mm-disable-multibyte)
847 (when (nnimap-request-article article group server (current-buffer))
848 (nnheader-message 7 "Expiring article %s:%d" group article)
849 (when (functionp target)
850 (setq target (funcall target group)))
852 (not (eq target 'delete)))
853 (if (or (gnus-request-group target t)
854 (gnus-request-create-group target))
855 (nnmail-expiry-target-group target group)
858 (push article deleted-articles))))))))
859 ;; Change back to the current group again.
860 (nnimap-possibly-change-group group server)
861 (setq deleted-articles (nreverse deleted-articles))
862 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
865 (defun nnimap-find-expired-articles (group)
866 (let ((cutoff (nnmail-expired-article-p group nil nil)))
867 (with-current-buffer (nnimap-buffer)
870 "UID SEARCH SENTBEFORE %s"
874 (car (rassoc (nth 4 (decode-time cutoff))
875 parse-time-months))))
878 (delete 0 (mapcar #'string-to-number
879 (cdr (assoc "SEARCH" (cdr result))))))))))
882 (defun nnimap-find-article-by-message-id (group message-id)
883 (with-current-buffer (nnimap-buffer)
885 (unless (equal group (nnimap-group nnimap-object))
886 (setf (nnimap-group nnimap-object) nil)
887 (setf (nnimap-examined nnimap-object) group)
888 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
890 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
892 (setq result (nnimap-wait-for-response sequence))
894 (car (setq result (nnimap-parse-response))))
895 ;; Select the last instance of the message in the group.
897 (car (last (assoc "SEARCH" (cdr result)))))
898 (string-to-number article))))))
900 (defun nnimap-delete-article (articles)
901 (with-current-buffer (nnimap-buffer)
902 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
903 (nnimap-article-ranges articles))
905 ((nnimap-capability "UIDPLUS")
906 (nnimap-command "UID EXPUNGE %s"
907 (nnimap-article-ranges articles))
910 (nnimap-command "EXPUNGE")
912 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
913 "server doesn't support UIDPLUS, so we won't "
914 "delete this article now"))))))
916 (deffoo nnimap-request-scan (&optional group server)
917 (when (and (nnimap-possibly-change-group nil server)
919 nnimap-split-methods)
920 (nnheader-message 7 "nnimap %s splitting mail..." server)
921 (nnimap-split-incoming-mail)))
923 (defun nnimap-marks-to-flags (marks)
926 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
930 (deffoo nnimap-request-update-group-status (group status &optional server)
931 (when (nnimap-possibly-change-group nil server)
932 (let ((command (assoc
934 '((subscribe "SUBSCRIBE")
935 (unsubscribe "UNSUBSCRIBE")))))
937 (with-current-buffer (nnimap-buffer)
938 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
940 (deffoo nnimap-request-set-mark (group actions &optional server)
941 (when (nnimap-possibly-change-group group server)
943 (with-current-buffer (nnimap-buffer)
945 ;; Just send all the STORE commands without waiting for
946 ;; response. If they're successful, they're successful.
947 (dolist (action actions)
948 (destructuring-bind (range action marks) action
949 (let ((flags (nnimap-marks-to-flags marks)))
951 (setq sequence (nnimap-send-command
952 "UID STORE %s %sFLAGS.SILENT (%s)"
953 (nnimap-article-ranges range)
955 ((eq action 'del) "-")
956 ((eq action 'add) "+")
957 ((eq action 'set) ""))
958 (mapconcat #'identity flags " ")))))))
959 ;; Wait for the last command to complete to avoid later
960 ;; syncronisation problems with the stream.
962 (nnimap-wait-for-response sequence))))))
964 (deffoo nnimap-request-accept-article (group &optional server last)
965 (when (nnimap-possibly-change-group nil server)
966 (nnmail-check-syntax)
967 (let ((message-id (message-field-value "message-id"))
970 (setq message (buffer-substring-no-properties (point-min) (point-max)))
971 (with-current-buffer (nnimap-buffer)
972 ;; If we have this group open read-only, then unselect it
973 ;; before appending to it.
974 (when (equal (nnimap-examined nnimap-object) group)
975 (nnimap-unselect-group))
977 (setq sequence (nnimap-send-command
978 "APPEND %S {%d}" (utf7-encode group t)
980 (unless nnimap-streaming
981 (nnimap-wait-for-connection "^[+]"))
982 (process-send-string (get-buffer-process (current-buffer)) message)
983 (process-send-string (get-buffer-process (current-buffer))
984 (if (nnimap-newlinep nnimap-object)
987 (let ((result (nnimap-get-response sequence)))
988 (if (not (nnimap-ok-p result))
990 (nnheader-report 'nnimap "%s" result)
993 (or (nnimap-find-uid-response "APPENDUID" (car result))
994 (nnimap-find-article-by-message-id
995 group message-id)))))))))
997 (defun nnimap-ok-p (value)
1000 (equal (caar value) "OK")))
1002 (defun nnimap-find-uid-response (name list)
1003 (let ((result (car (last (nnimap-find-response-element name list)))))
1005 (string-to-number result))))
1007 (defun nnimap-find-response-element (name list)
1010 (when (and (consp elem)
1011 (equal name (car elem)))
1012 (setq result elem)))
1015 (deffoo nnimap-request-replace-article (article group buffer)
1017 (when (and (nnimap-possibly-change-group group nil)
1018 ;; Put the article into the group.
1019 (with-current-buffer buffer
1021 (nnimap-request-accept-article group nil t))))
1022 (nnimap-delete-article (list article))
1023 ;; Return the new article number.
1026 (defun nnimap-add-cr ()
1027 (goto-char (point-min))
1028 (while (re-search-forward "\r?\n" nil t)
1029 (replace-match "\r\n" t t)))
1031 (defun nnimap-get-groups ()
1033 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1035 (nnimap-wait-for-response sequence)
1036 (subst-char-in-region (point-min) (point-max)
1038 (goto-char (point-min))
1039 (nnimap-unfold-quoted-lines)
1040 (goto-char (point-min))
1041 (while (search-forward "* LIST " nil t)
1042 (let ((flags (read (current-buffer)))
1043 (separator (read (current-buffer)))
1044 (group (read (current-buffer))))
1045 (unless (member '%NoSelect flags)
1046 (push (if (stringp group)
1048 (format "%s" group))
1052 (deffoo nnimap-request-list (&optional server)
1053 (nnimap-possibly-change-group nil server)
1054 (with-current-buffer nntp-server-buffer
1057 (with-current-buffer (nnimap-buffer)
1058 (nnimap-get-groups)))
1059 sequences responses)
1061 (with-current-buffer (nnimap-buffer)
1062 (setf (nnimap-group nnimap-object) nil)
1063 (dolist (group groups)
1064 (setf (nnimap-examined nnimap-object) group)
1065 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
1068 (nnimap-wait-for-response (caar sequences))
1070 (nnimap-get-responses (mapcar #'car sequences))))
1071 (dolist (response responses)
1072 (let* ((sequence (car response))
1073 (response (cadr response))
1074 (group (cadr (assoc sequence sequences))))
1076 (equal (caar response) "OK"))
1077 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1079 (dolist (elem response)
1080 (when (equal (cadr elem) "EXISTS")
1081 (setq exists (string-to-number (car elem)))))
1083 (setq highest (1- (string-to-number (car uidnext)))))
1086 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1089 (insert (format "%S %d %d y\n"
1090 (utf7-decode group t) highest (1+ highest))))
1092 ;; Return the widest possible range.
1093 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1094 (or highest exists)))))))))
1097 (deffoo nnimap-request-newgroups (date &optional server)
1098 (nnimap-possibly-change-group nil server)
1099 (with-current-buffer nntp-server-buffer
1101 (dolist (group (with-current-buffer (nnimap-buffer)
1102 (nnimap-get-groups)))
1103 (unless (assoc group nnimap-current-infos)
1104 ;; Insert dummy numbers here -- they don't matter.
1105 (insert (format "%S 0 1 y\n" group))))
1108 (deffoo nnimap-retrieve-group-data-early (server infos)
1109 (when (nnimap-possibly-change-group nil server)
1110 (with-current-buffer (nnimap-buffer)
1112 (setf (nnimap-group nnimap-object) nil)
1113 (let ((qresyncp (nnimap-capability "QRESYNC"))
1114 params groups sequences active uidvalidity modseq group)
1115 ;; Go through the infos and gather the data needed to know
1116 ;; what and how to request the data.
1117 (dolist (info infos)
1118 (setq params (gnus-info-params info)
1119 group (gnus-group-real-name (gnus-info-group info))
1120 active (cdr (assq 'active params))
1121 uidvalidity (cdr (assq 'uidvalidity params))
1122 modseq (cdr (assq 'modseq params)))
1123 (setf (nnimap-examined nnimap-object) group)
1128 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1129 (utf7-encode group t)
1130 (nnimap-quirk "QRESYNC")
1136 (if (and active uidvalidity)
1137 ;; Fetch the last 100 flags.
1138 (max 1 (- (cdr active) 100))
1143 ;; If we don't have a UIDVALIDITY, then this is
1144 ;; the first time we've seen the group, so we
1145 ;; have to do a SELECT (which is slower than an
1146 ;; examine), but will tell us whether the group
1147 ;; is read-only or not.
1149 (push (list (nnimap-send-command "%s %S" command
1150 (utf7-encode group t))
1151 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1152 start group command)
1156 (defun nnimap-quirk (command)
1157 (let ((quirk (assoc command nnimap-quirks)))
1158 ;; If this server is of a type that matches a quirk, then return
1159 ;; the "quirked" command instead of the proper one.
1160 (if (or (null quirk)
1161 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1165 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1166 (when (and sequences
1167 (nnimap-possibly-change-group nil server))
1168 (with-current-buffer (nnimap-buffer)
1169 ;; Wait for the final data to trickle in.
1170 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1174 ;; Now we should have most of the data we need, no matter
1175 ;; whether we're QRESYNCING, fetching all the flags from
1176 ;; scratch, or just fetching the last 100 flags per group.
1177 (nnimap-update-infos (nnimap-flags-to-marks
1179 (nreverse sequences)))
1181 ;; Finally, just return something resembling an active file in
1182 ;; the nntp buffer, so that the agent can save the info, too.
1183 (with-current-buffer nntp-server-buffer
1185 (dolist (info infos)
1186 (let* ((group (gnus-info-group info))
1187 (active (gnus-active group)))
1189 (insert (format "%S %d %d y\n"
1190 (gnus-group-real-name group)
1192 (car active)))))))))))
1194 (defun nnimap-update-infos (flags infos)
1195 (dolist (info infos)
1196 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1197 (marks (cdr (assoc group flags))))
1199 (nnimap-update-info info marks)))))
1201 (defun nnimap-update-info (info marks)
1202 (destructuring-bind (existing flags high low uidnext start-article
1203 permanent-flags uidvalidity
1204 vanished highestmodseq) marks
1206 ;; Ignore groups with no UIDNEXT/marks. This happens for
1207 ;; completely empty groups.
1208 ((and (not existing)
1210 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1212 (gnus-set-active (gnus-info-group info) active))))
1213 ;; We have a mismatch between the old and new UIDVALIDITY
1214 ;; identifiers, so we have to re-request the group info (the next
1215 ;; time). This virtually never happens.
1216 ((let ((old-uidvalidity
1217 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1218 (and old-uidvalidity
1219 (not (equal old-uidvalidity uidvalidity))
1220 (> start-article 1)))
1221 (gnus-group-remove-parameter info 'uidvalidity)
1222 (gnus-group-remove-parameter info 'modseq))
1223 ;; We have the data needed to update.
1225 (let* ((group (gnus-info-group info))
1226 (completep (and start-article
1227 (= start-article 1)))
1228 (active (or (gnus-active group)
1229 (cdr (assq 'active (gnus-info-params info))))))
1231 (setq high (1- uidnext)))
1232 ;; First set the active ranges based on high/low.
1234 (not (gnus-active group)))
1235 (gnus-set-active group
1238 (cons (min (or low (car active))
1240 (max (or high (cdr active))
1245 ;; No articles in this group.
1246 (cons uidnext (1- uidnext)))
1248 (cons start-article (1- start-article)))
1250 ;; No articles and no uidnext.
1255 (or high (1- uidnext)))))
1256 ;; See whether this is a read-only group.
1257 (unless (eq permanent-flags 'not-scanned)
1258 (gnus-group-set-parameter
1259 info 'permanent-flags
1260 (and (or (memq '%* permanent-flags)
1261 (memq '%Seen permanent-flags))
1263 ;; Update marks and read articles if this isn't a
1264 ;; read-only IMAP group.
1265 (when (setq permanent-flags
1266 (cdr (assq 'permanent-flags (gnus-info-params info))))
1267 (if (and highestmodseq
1268 (not start-article))
1269 ;; We've gotten the data by QRESYNCing.
1270 (nnimap-update-qresync-info
1271 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1272 ;; Do normal non-QRESYNC flag updates.
1273 ;; Update the list of read articles.
1275 (gnus-compress-sequence
1276 (gnus-set-difference
1277 (gnus-set-difference
1279 (cdr (assoc '%Seen flags)))
1280 (cdr (assoc '%Flagged flags)))))
1281 (read (gnus-range-difference
1282 (cons start-article high) unread)))
1283 (when (> start-article 1)
1286 (if (> start-article 1)
1287 (gnus-sorted-range-intersection
1288 (cons 1 (1- start-article))
1289 (gnus-info-read info))
1290 (gnus-info-read info))
1292 (when (or (not (listp permanent-flags))
1293 (memq '%Seen permanent-flags))
1294 (gnus-info-set-read info read))
1295 ;; Update the marks.
1296 (setq marks (gnus-info-marks info))
1297 (dolist (type (cdr nnimap-mark-alist))
1298 (when (or (not (listp permanent-flags))
1299 (memq (car (assoc (caddr type) flags))
1301 (memq '%* permanent-flags))
1302 (let ((old-marks (assoc (car type) marks))
1304 (gnus-compress-sequence
1305 (cdr (or (assoc (caddr type) flags) ; %Flagged
1306 (assoc (intern (cadr type) obarray) flags)
1307 (assoc (cadr type) flags)))))) ; "\Flagged"
1308 (setq marks (delq old-marks marks))
1310 (when (and old-marks
1311 (> start-article 1))
1312 (setq old-marks (gnus-range-difference
1314 (cons start-article high)))
1315 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1317 (push (cons (car type) new-marks) marks)))))
1318 (gnus-info-set-marks info marks t))))
1319 ;; Note the active level for the next run-through.
1320 (gnus-group-set-parameter info 'active (gnus-active group))
1321 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1322 (gnus-group-set-parameter info 'modseq highestmodseq)
1323 (nnimap-store-info info (gnus-active group)))))))
1325 (defun nnimap-update-qresync-info (info existing vanished flags)
1326 ;; Add all the vanished articles to the list of read articles.
1331 (gnus-range-add (gnus-info-read info)
1333 (cdr (assq '%Flagged flags)))
1334 (cdr (assq '%Seen flags))))
1335 (let ((marks (gnus-info-marks info)))
1336 (dolist (type (cdr nnimap-mark-alist))
1337 (let ((ticks (assoc (car type) marks))
1339 (cdr (or (assoc (caddr type) flags) ; %Flagged
1340 (assoc (intern (cadr type) obarray) flags)
1341 (assoc (cadr type) flags))))) ; "\Flagged"
1342 (setq marks (delq ticks marks))
1344 ;; Add the new marks we got.
1345 (setq ticks (gnus-add-to-range ticks new-marks))
1346 ;; Remove the marks from messages that don't have them.
1347 (setq ticks (gnus-remove-from-range
1349 (gnus-compress-sequence
1350 (gnus-sorted-complement existing new-marks))))
1352 (push (cons (car type) ticks) marks)))
1353 (gnus-info-set-marks info marks t))))
1355 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1356 (if (zerop (length irange))
1359 (dolist (elem (split-string irange ","))
1361 (if (string-match ":" elem)
1362 (let ((numbers (split-string elem ":")))
1363 (cons (string-to-number (car numbers))
1364 (string-to-number (cadr numbers))))
1365 (string-to-number elem))
1367 (nreverse result))))
1369 (defun nnimap-store-info (info active)
1370 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1371 (entry (assoc group nnimap-current-infos)))
1373 (setcdr entry (list info active))
1374 (push (list group info active) nnimap-current-infos))))
1376 (defun nnimap-flags-to-marks (groups)
1377 (let (data group totalp uidnext articles start-article mark permanent-flags
1378 uidvalidity vanished highestmodseq)
1379 (dolist (elem groups)
1380 (setq group (car elem)
1381 uidnext (nth 1 elem)
1382 start-article (nth 2 elem)
1383 permanent-flags (nth 3 elem)
1384 uidvalidity (nth 4 elem)
1385 vanished (nth 5 elem)
1386 highestmodseq (nth 6 elem)
1387 articles (nthcdr 7 elem))
1388 (let ((high (caar articles))
1390 (dolist (article articles)
1391 (setq low (car article))
1392 (push (car article) existing)
1393 (dolist (flag (cdr article))
1394 (setq mark (assoc flag marks))
1396 (push (list flag (car article)) marks)
1397 (setcdr mark (cons (car article) (cdr mark))))))
1398 (push (list group existing marks high low uidnext start-article
1399 permanent-flags uidvalidity vanished highestmodseq)
1403 (defun nnimap-parse-flags (sequences)
1404 (goto-char (point-min))
1405 ;; Change \Delete etc to %Delete, so that the reader can read it.
1406 (subst-char-in-region (point-min) (point-max)
1408 (let (start end articles groups uidnext elems permanent-flags
1409 uidvalidity vanished highestmodseq)
1410 (dolist (elem sequences)
1411 (destructuring-bind (group-sequence flag-sequence totalp group command)
1413 (setq start (point))
1415 ;; The EXAMINE was successful.
1416 (search-forward (format "\n%d OK " group-sequence) nil t)
1421 (setq permanent-flags
1422 (if (equal command "SELECT")
1423 (and (search-forward "PERMANENTFLAGS "
1424 (or end (point-min)) t)
1425 (read (current-buffer)))
1429 (and (search-forward "UIDNEXT "
1430 (or end (point-min)) t)
1431 (read (current-buffer))))
1434 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1435 (or end (point-min)) t)
1436 ;; Store UIDVALIDITY as a string, as it's
1437 ;; too big for 32-bit Emacsen, usually.
1441 (and (eq flag-sequence 'qresync)
1442 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1443 (or end (point-min)) t)
1447 (and (search-forward "HIGHESTMODSEQ "
1448 (or end (point-min)) t)
1449 (read (current-buffer))))
1452 ;; The UID FETCH FLAGS was successful.
1453 (or (eq flag-sequence 'qresync)
1454 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1455 (if (eq flag-sequence 'qresync)
1459 (setq start (point))
1461 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1462 (setq elems (read (current-buffer)))
1463 (push (cons (cadr (memq 'UID elems))
1464 (cadr (memq 'FLAGS elems)))
1466 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1467 vanished highestmodseq)
1471 (setq articles nil))))
1474 (defun nnimap-find-process-buffer (buffer)
1475 (cadr (assoc buffer nnimap-connection-alist)))
1477 (deffoo nnimap-request-post (&optional server)
1478 (setq nnimap-status-string "Read-only server")
1481 (declare-function gnus-fetch-headers "gnus-sum"
1482 (articles &optional limit force-new dependencies))
1484 (deffoo nnimap-request-thread (header)
1485 (let* ((id (mail-header-id header))
1487 (or (mail-header-references header)
1491 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1493 (dolist (refid refs value)
1495 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1496 refid refid value)))))
1497 (result (with-current-buffer (nnimap-buffer)
1498 (nnimap-command "UID SEARCH %s" cmd))))
1500 (and (car result) (delete 0 (mapcar #'string-to-number
1501 (cdr (assoc "SEARCH" (cdr result))))))
1504 (defun nnimap-possibly-change-group (group server)
1505 (let ((open-result t))
1507 (not (nnimap-server-opened server)))
1508 (setq open-result (nnimap-open-server server)))
1515 (with-current-buffer (nnimap-buffer)
1516 (if (equal group (nnimap-group nnimap-object))
1518 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1520 (setf (nnimap-group nnimap-object) group
1521 (nnimap-select-result nnimap-object) result)
1524 (defun nnimap-find-connection (buffer)
1525 "Find the connection delivering to BUFFER."
1526 (let ((entry (assoc buffer nnimap-connection-alist)))
1528 (if (and (buffer-name (cadr entry))
1529 (get-buffer-process (cadr entry))
1530 (memq (process-status (get-buffer-process (cadr entry)))
1532 (get-buffer-process (cadr entry))
1533 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1536 (defvar nnimap-sequence 0)
1538 (defun nnimap-send-command (&rest args)
1539 (process-send-string
1540 (get-buffer-process (current-buffer))
1543 (incf nnimap-sequence)
1544 (apply #'format args)
1545 (if (nnimap-newlinep nnimap-object)
1548 ;; Some servers apparently can't have many outstanding
1549 ;; commands, so throttle them.
1550 (unless nnimap-streaming
1551 (nnimap-wait-for-response nnimap-sequence))
1554 (defun nnimap-log-command (command)
1555 (with-current-buffer (get-buffer-create "*imap log*")
1556 (goto-char (point-max))
1557 (insert (format-time-string "%H:%M:%S") " " command))
1560 (defun nnimap-command (&rest args)
1562 (setf (nnimap-last-command-time nnimap-object) (current-time))
1563 (let* ((sequence (apply #'nnimap-send-command args))
1564 (response (nnimap-get-response sequence)))
1565 (if (equal (caar response) "OK")
1567 (nnheader-report 'nnimap "%s"
1568 (mapconcat (lambda (a)
1570 (car response) " "))
1573 (defun nnimap-get-response (sequence)
1574 (nnimap-wait-for-response sequence)
1575 (nnimap-parse-response))
1577 (defun nnimap-wait-for-connection (&optional regexp)
1578 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1580 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1581 (let ((process (get-buffer-process (current-buffer))))
1582 (goto-char (point-min))
1583 (while (and (memq (process-status process)
1585 (not (re-search-forward regexp nil t)))
1586 (nnheader-accept-process-output process)
1587 (goto-char (point-min)))
1589 (and (looking-at (or response-regexp regexp))
1592 (defun nnimap-wait-for-response (sequence &optional messagep)
1593 (let ((process (get-buffer-process (current-buffer)))
1597 (goto-char (point-max))
1598 (while (and (setq openp (memq (process-status process)
1601 ;; Skip past any "*" lines that the server has
1603 (while (and (not (bobp))
1606 (looking-at "\\*"))))
1607 (not (looking-at (format "%d " sequence)))))
1609 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1610 (nnheader-accept-process-output process)
1611 (goto-char (point-max)))
1614 ;; The user hit C-g while we were waiting: kill the process, in case
1615 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1617 (delete-process process)
1620 (defun nnimap-parse-response ()
1621 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1623 (dolist (line lines)
1624 (push (cdr (nnimap-parse-line line)) result))
1625 ;; Return the OK/error code first, and then all the "continuation
1626 ;; lines" afterwards.
1628 (nreverse result))))
1630 ;; Parse an IMAP response line lightly. They look like
1631 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1632 ;; the lines into a list of strings and lists of string.
1633 (defun nnimap-parse-line (line)
1636 (mm-disable-multibyte)
1638 (goto-char (point-min))
1640 (if (eql (setq char (following-char)) ? )
1648 (if (search-forward "]" (line-end-position) 'move)
1655 (if (search-forward ")" (line-end-position) 'move)
1662 (1- (or (search-forward "\"" (line-end-position) 'move)
1665 (buffer-substring (point) (if (search-forward " " nil t)
1667 (goto-char (point-max))))))
1669 (nreverse result))))
1671 (defun nnimap-last-response-string ()
1674 (let ((end (point)))
1678 (while (and (not (bobp))
1679 (eql (following-char) ?*))
1681 (unless (eql (following-char) ?*)
1683 (buffer-substring (point) end))))
1685 (defun nnimap-get-responses (sequences)
1687 (dolist (sequence sequences)
1688 (goto-char (point-min))
1689 (when (re-search-forward (format "^%d " sequence) nil t)
1690 (push (list sequence (nnimap-parse-response))
1694 (defvar nnimap-incoming-split-list nil)
1696 (defun nnimap-fetch-inbox (articles)
1698 (nnimap-wait-for-response
1699 (nnimap-send-command
1701 (nnimap-article-ranges articles)
1702 (format "(UID %s%s)"
1705 "BODY.PEEK[HEADER] BODY.PEEK"
1707 (if nnimap-split-download-body-default
1712 (defun nnimap-split-incoming-mail ()
1713 (with-current-buffer (nnimap-buffer)
1714 (let ((nnimap-incoming-split-list nil)
1715 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1716 nnmail-split-methods
1717 nnimap-split-methods))
1718 (nnmail-split-fancy (or nnimap-split-fancy
1719 nnmail-split-fancy))
1720 (nnmail-inhibit-default-split-group t)
1721 (groups (nnimap-get-groups))
1724 (nnimap-command "SELECT %S" nnimap-inbox)
1725 (setf (nnimap-group nnimap-object) nnimap-inbox)
1726 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1728 (nnimap-fetch-inbox new-articles)
1729 (nnimap-transform-split-mail)
1730 (nnheader-ms-strip-cr)
1732 (nnmail-split-incoming (current-buffer)
1733 #'nnimap-save-mail-spec
1735 #'nnimap-dummy-active-number
1736 #'nnimap-save-mail-spec)
1737 (when nnimap-incoming-split-list
1738 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1739 sequences junk-articles)
1740 ;; Create any groups that doesn't already exist on the
1742 (dolist (spec specs)
1743 (when (and (not (member (car spec) groups))
1744 (not (eq (car spec) 'junk)))
1745 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1746 ;; Then copy over all the messages.
1748 (dolist (spec specs)
1749 (let ((group (car spec))
1750 (ranges (cdr spec)))
1751 (if (eq group 'junk)
1752 (setq junk-articles ranges)
1753 (push (list (nnimap-send-command
1755 (nnimap-article-ranges ranges)
1756 (utf7-encode group t))
1759 ;; Wait for the last COPY response...
1761 (nnimap-wait-for-response (caar sequences))
1762 ;; And then mark the successful copy actions as deleted,
1763 ;; and possibly expunge them.
1764 (nnimap-mark-and-expunge-incoming
1765 (nnimap-parse-copied-articles sequences)))
1766 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1768 (defun nnimap-mark-and-expunge-incoming (range)
1770 (setq range (nnimap-article-ranges range))
1773 (nnimap-send-command
1774 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1776 ;; If the server supports it, we now delete the message we have
1777 ;; just copied over.
1778 ((nnimap-capability "UIDPLUS")
1779 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1780 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1781 ;; user has configured it.
1783 (setq sequence (nnimap-send-command "EXPUNGE"))))
1784 (nnimap-wait-for-response sequence))))
1786 (defun nnimap-parse-copied-articles (sequences)
1787 (let (sequence copied range)
1788 (goto-char (point-min))
1789 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1790 (setq sequence (string-to-number (match-string 1)))
1791 (when (setq range (cadr (assq sequence sequences)))
1792 (push (gnus-uncompress-range range) copied)))
1793 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1795 (defun nnimap-new-articles (flags)
1797 (dolist (elem flags)
1798 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1800 (push (car elem) new)))
1801 (gnus-compress-sequence (nreverse new))))
1803 (defun nnimap-make-split-specs (list)
1807 (destructuring-bind (article spec) elem
1808 (dolist (group (delete nil (mapcar #'car spec)))
1809 (unless (setq entry (assoc group specs))
1810 (push (setq entry (list group)) specs))
1811 (setcdr entry (cons article (cdr entry))))))
1812 (dolist (entry specs)
1813 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1816 (defun nnimap-transform-split-mail ()
1817 (goto-char (point-min))
1818 (let (article bytes)
1821 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1822 (delete-region (point) (progn (forward-line 1) (point)))
1825 (setq article (match-string 1)
1826 bytes (nnimap-get-length))
1827 (delete-region (line-beginning-position) (line-end-position))
1828 ;; Insert MMDF separator, and a way to remember what this
1830 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1831 (forward-char (1+ bytes))
1832 (setq bytes (nnimap-get-length))
1833 (delete-region (line-beginning-position) (line-end-position))
1834 ;; There's a body; skip past that.
1836 (forward-char (1+ bytes))
1837 (delete-region (line-beginning-position) (line-end-position)))))))
1839 (defun nnimap-dummy-active-number (group &optional server)
1842 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1844 (goto-char (point-min))
1845 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1846 (error "Invalid nnimap mail")
1847 (setq article (string-to-number (match-string 1))))
1849 (if (eq group-art 'junk)
1850 (list (cons 'junk 1))
1852 nnimap-incoming-split-list)))
1856 ;;; nnimap.el ends here