1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
52 (autoload 'auth-source-forget-user-or-password "auth-source")
53 (autoload 'auth-source-user-or-password "auth-source")
57 (defvoo nnimap-address nil
58 "The address of the IMAP server.")
60 (defvoo nnimap-server-port nil
62 If nnimap-stream is `ssl', this will default to `imaps'. If not,
63 it will default to `imap'.")
65 (defvoo nnimap-stream 'ssl
66 "How nnimap will talk to the IMAP server.
67 Values are `ssl', `network', `starttls' or `shell'.")
69 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
70 (if (listp imap-shell-program)
71 (car imap-shell-program)
75 (defvoo nnimap-inbox nil
76 "The mail box where incoming mail arrives and should be split out of.")
78 (defvoo nnimap-split-methods nil
80 Uses the same syntax as nnmail-split-methods")
82 (defvoo nnimap-split-fancy nil
83 "Uses the same syntax as nnmail-split-fancy.")
85 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
86 "Articles with the flags in the list will not be considered when splitting.")
88 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
91 (defvoo nnimap-authenticator nil
92 "How nnimap authenticate itself to the server.
93 Possible choices are nil (use default methods) or `anonymous'.")
95 (defvoo nnimap-expunge t
96 "If non-nil, expunge articles after deleting them.
97 This is always done if the server supports UID EXPUNGE, but it's
98 not done by default on servers that doesn't support that command.")
100 (defvoo nnimap-streaming t
101 "If non-nil, try to use streaming commands with IMAP servers.
102 Switching this off will make nnimap slower, but it helps with
105 (defvoo nnimap-connection-alist nil)
107 (defvoo nnimap-current-infos nil)
109 (defvoo nnimap-fetch-partial-articles nil
110 "If non-nil, Gnus will fetch partial articles.
111 If t, nnimap will fetch only the first part. If a string, it
112 will fetch all parts that have types that match that string. A
113 likely value would be \"text/\" to automatically fetch all
116 (defvar nnimap-process nil)
118 (defvar nnimap-status-string "")
120 (defvar nnimap-split-download-body-default nil
121 "Internal variable with default value for `nnimap-split-download-body'.")
123 (defvar nnimap-keepalive-timer nil)
124 (defvar nnimap-process-buffers nil)
127 group process commands capabilities select-result newlinep server
128 last-command-time greeting examined)
130 (defvar nnimap-object nil)
132 (defvar nnimap-mark-alist
133 '((read "\\Seen" %Seen)
134 (tick "\\Flagged" %Flagged)
135 (reply "\\Answered" %Answered)
136 (expire "gnus-expire")
137 (dormant "gnus-dormant")
140 (download "gnus-download")
141 (forward "gnus-forward")))
143 (defun nnimap-buffer ()
144 (nnimap-find-process-buffer nntp-server-buffer))
146 (defun nnimap-header-parameters ()
147 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
150 "BODY.PEEK[HEADER.FIELDS %s]"
151 "RFC822.HEADER.LINES %s")
152 (append '(Subject From Date Message-Id
153 References In-Reply-To Xref)
154 nnmail-extra-headers))))
156 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
157 (with-current-buffer nntp-server-buffer
159 (when (nnimap-possibly-change-group group server)
160 (with-current-buffer (nnimap-buffer)
162 (nnimap-wait-for-response
165 (nnimap-article-ranges (gnus-compress-sequence articles))
166 (nnimap-header-parameters))
168 (nnimap-transform-headers))
169 (insert-buffer-substring
170 (nnimap-find-process-buffer (current-buffer))))
173 (defun nnimap-transform-headers ()
174 (goto-char (point-min))
175 (let (article bytes lines size string)
178 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
179 (delete-region (point) (progn (forward-line 1) (point)))
182 (setq article (match-string 1))
183 ;; Unfold quoted {number} strings.
184 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r\n"
185 (1+ (line-end-position)) t)
186 (setq size (string-to-number (match-string 1)))
187 (delete-region (+ (match-beginning 0) 2) (point))
188 (setq string (delete-region (point) (+ (point) size)))
189 (insert (format "%S" string)))
190 (setq bytes (nnimap-get-length)
194 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
199 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
200 (let ((structure (ignore-errors
201 (read (current-buffer)))))
202 (while (and (consp structure)
203 (not (stringp (car structure))))
204 (setq structure (car structure)))
205 (setq lines (nth 7 structure))))
206 (delete-region (line-beginning-position) (line-end-position))
207 (insert (format "211 %s Article retrieved." article))
210 (insert (format "Chars: %s\n" size)))
212 (insert (format "Lines: %s\n" lines)))
213 (unless (re-search-forward "^\r$" nil t)
214 (goto-char (point-max)))
215 (delete-region (line-beginning-position) (line-end-position))
219 (defun nnimap-get-length ()
220 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
221 (string-to-number (match-string 1))))
223 (defun nnimap-article-ranges (ranges)
227 (number-to-string ranges))
228 ((numberp (cdr ranges))
229 (format "%d:%d" (car ranges) (cdr ranges)))
231 (dolist (elem ranges)
234 (format "%d:%d" (car elem) (cdr elem))
235 (number-to-string elem))
237 (mapconcat #'identity (nreverse result) ",")))))
239 (deffoo nnimap-open-server (server &optional defs)
240 (if (nnimap-server-opened server)
242 (unless (assq 'nnimap-address defs)
243 (setq defs (append defs (list (list 'nnimap-address server)))))
244 (nnoo-change-server 'nnimap server defs)
245 (or (nnimap-find-connection nntp-server-buffer)
246 (nnimap-open-connection nntp-server-buffer))))
248 (defun nnimap-make-process-buffer (buffer)
250 (generate-new-buffer (format "*nnimap %s %s %s*"
251 nnimap-address nnimap-server-port
252 (gnus-buffer-exists-p buffer)))
253 (mm-disable-multibyte)
254 (buffer-disable-undo)
256 (set (make-local-variable 'after-change-functions) nil)
257 (set (make-local-variable 'nnimap-object)
258 (make-nnimap :server (nnoo-current-server 'nnimap)))
259 (push (list buffer (current-buffer)) nnimap-connection-alist)
260 (push (current-buffer) nnimap-process-buffers)
263 (defun nnimap-open-shell-stream (name buffer host port)
264 (let ((process-connection-type nil))
265 (start-process name buffer shell-file-name
273 (defun nnimap-credentials (address ports &optional inhibit-create)
274 (let (port credentials)
275 ;; Request the credentials from all ports, but only query on the
276 ;; last port if all the previous ones have failed.
277 (while (and (null credentials)
278 (setq port (pop ports)))
280 (auth-source-user-or-password
281 '("login" "password") address port nil
287 (defun nnimap-keepalive ()
288 (let ((now (current-time)))
289 (dolist (buffer nnimap-process-buffers)
290 (when (buffer-name buffer)
291 (with-current-buffer buffer
292 (when (and nnimap-object
293 (nnimap-last-command-time nnimap-object)
297 (nnimap-last-command-time nnimap-object)))
298 ;; More than five minutes since the last command.
300 (nnimap-send-command "NOOP")))))))
302 (declare-function gnutls-negotiate "gnutls"
303 (proc type &optional priority-string trustfiles keyfiles))
305 (defun nnimap-open-connection (buffer)
306 (unless nnimap-keepalive-timer
307 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
310 (with-current-buffer (nnimap-make-process-buffer buffer)
311 (let* ((coding-system-for-read 'binary)
312 (coding-system-for-write 'binary)
316 ((or (eq nnimap-stream 'network)
317 (and (eq nnimap-stream 'starttls)
318 (fboundp 'open-gnutls-stream)))
319 (nnheader-message 7 "Opening connection to %s..."
322 "*nnimap*" (current-buffer) nnimap-address
324 (or nnimap-server-port
325 (if (netrc-find-service-number "imap")
329 ((eq nnimap-stream 'shell)
330 (nnheader-message 7 "Opening connection to %s via shell..."
332 (nnimap-open-shell-stream
333 "*nnimap*" (current-buffer) nnimap-address
334 (setq port (or nnimap-server-port "imap")))
336 ((eq nnimap-stream 'starttls)
337 (nnheader-message 7 "Opening connection to %s via starttls..."
340 '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -starttls imap")))
342 "*nnimap*" (current-buffer) nnimap-address
343 (setq port (or nnimap-server-port "imap"))))
345 ((memq nnimap-stream '(ssl tls))
346 (nnheader-message 7 "Opening connection to %s via tls..."
348 (funcall (if (fboundp 'open-gnutls-stream)
351 "*nnimap*" (current-buffer) nnimap-address
353 (or nnimap-server-port
354 (if (netrc-find-service-number "imaps")
357 '("143" "993" "imap" "imaps"))
359 (error "Unknown stream type: %s" nnimap-stream))))
360 connection-result login-result credentials)
361 (setf (nnimap-process nnimap-object)
362 (get-buffer-process (current-buffer)))
363 (if (not (and (nnimap-process nnimap-object)
364 (memq (process-status (nnimap-process nnimap-object))
366 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
367 nnimap-address port nnimap-stream)
368 (gnus-set-process-query-on-exit-flag
369 (nnimap-process nnimap-object) nil)
370 (if (not (setq connection-result (nnimap-wait-for-connection)))
371 (nnheader-report 'nnimap
372 "%s" (buffer-substring
373 (point) (line-end-position)))
374 ;; Store the greeting (for debugging purposes).
375 (setf (nnimap-greeting nnimap-object)
376 (buffer-substring (line-beginning-position)
377 (line-end-position)))
378 (nnimap-get-capabilities)
379 (when nnimap-server-port
380 (push (format "%s" nnimap-server-port) ports))
381 ;; If this is a STARTTLS-capable server, then sever the
382 ;; connection and start a STARTTLS connection instead.
384 ((and (or (and (eq nnimap-stream 'network)
385 (nnimap-capability "STARTTLS"))
386 (eq nnimap-stream 'starttls))
387 (fboundp 'open-gnutls-stream))
388 (nnimap-command "STARTTLS")
389 (gnutls-negotiate (nnimap-process nnimap-object) nil)
390 ;; Get the capabilities again -- they may have changed
391 ;; after doing STARTTLS.
392 (nnimap-get-capabilities))
393 ((and (eq nnimap-stream 'network)
394 (nnimap-capability "STARTTLS"))
395 (let ((nnimap-stream 'starttls))
397 (nnimap-open-connection buffer)))
398 ;; If the STARTTLS connection was successful, we
399 ;; kill our first non-encrypted connection. If it
400 ;; wasn't successful, we just use our unencrypted
402 (when (memq (process-status tls-process) '(open run))
403 (delete-process (nnimap-process nnimap-object))
404 (kill-buffer (current-buffer))
405 (return tls-process))))))
406 (unless (equal connection-result "PREAUTH")
407 (if (not (setq credentials
408 (if (eq nnimap-authenticator 'anonymous)
410 (message-make-address))
412 ;; First look for the credentials based
413 ;; on the virtual server name.
415 (nnoo-current-server 'nnimap) ports t)
416 ;; Then look them up based on the
418 (nnimap-credentials nnimap-address ports)))))
419 (setq nnimap-object nil)
421 (if (and (nnimap-capability "AUTH=PLAIN")
422 (nnimap-capability "LOGINDISABLED"))
424 "AUTHENTICATE PLAIN %s"
425 (base64-encode-string
426 (format "\000%s\000%s"
427 (nnimap-quote-specials (car credentials))
428 (nnimap-quote-specials (cadr credentials)))))
429 (nnimap-command "LOGIN %S %S"
431 (cadr credentials))))
432 (unless (car login-result)
433 ;; If the login failed, then forget the credentials
434 ;; that are now possibly cached.
435 (dolist (host (list (nnoo-current-server 'nnimap)
438 (dolist (element '("login" "password"))
439 (auth-source-forget-user-or-password
440 element host port))))
441 (delete-process (nnimap-process nnimap-object))
442 (setq nnimap-object nil))))
444 (when (nnimap-capability "QRESYNC")
445 (nnimap-command "ENABLE QRESYNC"))
446 (nnimap-process nnimap-object))))))))
448 (defun nnimap-get-capabilities ()
449 (setf (nnimap-capabilities nnimap-object)
452 (nnimap-find-parameter
453 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
455 (defun nnimap-quote-specials (string)
458 (goto-char (point-min))
459 (while (re-search-forward "[\\\"]" nil t)
465 (defun nnimap-find-parameter (parameter elems)
469 ((equal (car elem) parameter)
470 (setq result (cdr elem)))
471 ((and (equal (car elem) "OK")
473 (equal (caadr elem) parameter))
474 (setq result (cdr (cadr elem))))))
477 (deffoo nnimap-close-server (&optional server)
478 (when (nnoo-change-server 'nnimap server nil)
480 (delete-process (get-buffer-process (nnimap-buffer))))
481 (nnoo-close-server 'nnimap server)
484 (deffoo nnimap-request-close ()
487 (deffoo nnimap-server-opened (&optional server)
488 (and (nnoo-current-server-p 'nnimap server)
490 (gnus-buffer-live-p nntp-server-buffer)
491 (nnimap-find-connection nntp-server-buffer)))
493 (deffoo nnimap-status-message (&optional server)
494 nnimap-status-string)
496 (deffoo nnimap-request-article (article &optional group server to-buffer)
497 (with-current-buffer nntp-server-buffer
498 (let ((result (nnimap-possibly-change-group group server))
500 (when (stringp article)
501 (setq article (nnimap-find-article-by-message-id group article)))
505 (with-current-buffer (nnimap-buffer)
507 (when nnimap-fetch-partial-articles
508 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
509 (goto-char (point-min))
510 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
511 (setq structure (ignore-errors
512 (let ((start (point)))
514 (downcase-region start (point))
516 (read (current-buffer))))
517 parts (nnimap-find-wanted-parts structure))))
519 (nnimap-get-partial-article article parts structure)
520 (nnimap-get-whole-article article))
521 (let ((buffer (current-buffer)))
522 (with-current-buffer (or to-buffer nntp-server-buffer)
524 (insert-buffer-substring buffer)
525 (nnheader-ms-strip-cr)
526 (cons group article)))))))))
528 (deffoo nnimap-request-head (article &optional group server to-buffer)
529 (when (nnimap-possibly-change-group group server)
530 (with-current-buffer (nnimap-buffer)
531 (when (stringp article)
532 (setq article (nnimap-find-article-by-message-id group article)))
533 (nnimap-get-whole-article
534 article (format "UID FETCH %%d %s"
535 (nnimap-header-parameters)))
536 (let ((buffer (current-buffer)))
537 (with-current-buffer (or to-buffer nntp-server-buffer)
539 (insert-buffer-substring buffer)
540 (nnheader-ms-strip-cr)
541 (cons group article))))))
543 (defun nnimap-get-whole-article (article &optional command)
548 "UID FETCH %d BODY.PEEK[]"
549 "UID FETCH %d RFC822.PEEK"))
551 ;; Check that we really got an article.
552 (goto-char (point-min))
553 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
556 ;; Remove any data that may have arrived before the FETCH data.
559 (delete-region (point-min) (point)))
560 (let ((bytes (nnimap-get-length)))
561 (delete-region (line-beginning-position)
562 (progn (forward-line 1) (point)))
563 (goto-char (+ (point) bytes))
564 (delete-region (point) (point-max)))
567 (defun nnimap-capability (capability)
568 (member capability (nnimap-capabilities nnimap-object)))
570 (defun nnimap-ver4-p ()
571 (nnimap-capability "IMAP4REV1"))
573 (defun nnimap-get-partial-article (article parts structure)
576 "UID FETCH %d (%s %s)"
582 (mapconcat (lambda (part)
583 (format "BODY.PEEK[%s]" part))
585 (mapconcat (lambda (part)
586 (format "RFC822.PEEK[%s]" part))
589 (nnimap-convert-partial-article structure))))
591 (defun nnimap-convert-partial-article (structure)
592 ;; First just skip past the headers.
593 (goto-char (point-min))
594 (let ((bytes (nnimap-get-length))
596 ;; Delete "FETCH" line.
597 (delete-region (line-beginning-position)
598 (progn (forward-line 1) (point)))
599 (goto-char (+ (point) bytes))
600 ;; Collect all the body parts.
601 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
602 (setq id (match-string 1)
603 bytes (nnimap-get-length))
605 (delete-region (point) (progn (forward-line 1) (point)))
606 (push (list id (buffer-substring (point) (+ (point) bytes)))
608 (delete-region (point) (+ (point) bytes)))
609 ;; Delete trailing junk.
610 (delete-region (point) (point-max))
611 ;; Now insert all the parts again where they fit in the structure.
612 (nnimap-insert-partial-structure structure parts)
615 (defun nnimap-insert-partial-structure (structure parts &optional subp)
617 (let ((bstruc structure))
618 (while (consp (car bstruc))
620 (setq type (car bstruc))
621 (setq bstruc (car (cdr bstruc)))
622 (let ((has-boundary (member "boundary" bstruc)))
624 (setq boundary (cadr has-boundary)))))
626 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
627 (downcase type) boundary)))
628 (while (not (stringp (car structure)))
629 (insert "\n--" boundary "\n")
630 (if (consp (caar structure))
631 (nnimap-insert-partial-structure (pop structure) parts t)
632 (let ((bit (pop structure)))
633 (insert (format "Content-type: %s/%s"
634 (downcase (nth 0 bit))
635 (downcase (nth 1 bit))))
636 (if (member "CHARSET" (nth 2 bit))
638 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
640 (insert (format "Content-transfer-encoding: %s\n"
643 (when (assoc (nth 9 bit) parts)
644 (insert (cadr (assoc (nth 9 bit) parts)))))))
645 (insert "\n--" boundary "--\n")))
647 (defun nnimap-find-wanted-parts (structure)
648 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
650 (defun nnimap-find-wanted-parts-1 (structure prefix)
653 (while (consp (car structure))
654 (let ((sub (pop structure)))
655 (if (consp (car sub))
656 (push (nnimap-find-wanted-parts-1
657 sub (if (string= prefix "")
658 (number-to-string num)
659 (format "%s.%s" prefix num)))
661 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
662 (id (if (string= prefix "")
663 (number-to-string num)
664 (format "%s.%s" prefix num))))
665 (setcar (nthcdr 9 sub) id)
666 (when (if (eq nnimap-fetch-partial-articles t)
668 (string-match nnimap-fetch-partial-articles type))
673 (deffoo nnimap-request-group (group &optional server dont-check info)
674 (let ((result (nnimap-possibly-change-group
675 ;; Don't SELECT the group if we're going to select it
678 (assoc group nnimap-current-infos))
682 articles active marks high low)
683 (with-current-buffer nntp-server-buffer
686 (setq active (nth 2 (assoc group nnimap-current-infos))))
687 (insert (format "211 %d %d %d %S\n"
688 (- (cdr active) (car active))
692 (with-current-buffer (nnimap-buffer)
694 (let ((group-sequence
695 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
697 (nnimap-send-command "UID FETCH 1:* FLAGS")))
698 (setf (nnimap-group nnimap-object) group)
699 (nnimap-wait-for-response flag-sequence)
701 (nnimap-flags-to-marks
703 (list (list group-sequence flag-sequence
704 1 group "SELECT")))))
707 (nnimap-update-infos marks (list info))
708 (nnimap-store-info info (gnus-active (gnus-info-group info))))
709 (goto-char (point-max))
710 (let ((uidnext (nth 5 (car marks))))
711 (setq high (or (if uidnext
715 low (or (nth 4 (car marks)) uidnext 1)))))
719 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
722 (deffoo nnimap-request-create-group (group &optional server args)
723 (when (nnimap-possibly-change-group nil server)
724 (with-current-buffer (nnimap-buffer)
725 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
727 (deffoo nnimap-request-delete-group (group &optional force server)
728 (when (nnimap-possibly-change-group nil server)
729 (with-current-buffer (nnimap-buffer)
730 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
732 (deffoo nnimap-request-rename-group (group new-name &optional server)
733 (when (nnimap-possibly-change-group nil server)
734 (with-current-buffer (nnimap-buffer)
735 (nnimap-unselect-group)
736 (car (nnimap-command "RENAME %S %S"
737 (utf7-encode group t) (utf7-encode new-name t))))))
739 (defun nnimap-unselect-group ()
740 ;; Make sure we don't have this group open read/write by asking
741 ;; to examine a mailbox that doesn't exist. This seems to be
742 ;; the only way that allows us to reliably go back to unselected
744 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
746 (deffoo nnimap-request-expunge-group (group &optional server)
747 (when (nnimap-possibly-change-group group server)
748 (with-current-buffer (nnimap-buffer)
749 (car (nnimap-command "EXPUNGE")))))
751 (defun nnimap-get-flags (spec)
754 (with-current-buffer (nnimap-buffer)
756 (nnimap-wait-for-response (nnimap-send-command
757 "UID FETCH %s FLAGS" spec))
759 (subst-char-in-region (point-min) (point-max)
761 (goto-char (point-min))
762 (while (search-forward " FETCH " end t)
763 (setq elems (read (current-buffer)))
764 (push (cons (cadr (memq 'UID elems))
765 (cadr (memq 'FLAGS elems)))
767 (nreverse articles)))
769 (deffoo nnimap-close-group (group &optional server)
772 (deffoo nnimap-request-move-article (article group server accept-form
773 &optional last internal-move-group)
775 (mm-disable-multibyte)
776 (when (funcall (if internal-move-group
778 'nnimap-request-article)
779 article group server (current-buffer))
780 ;; If the move is internal (on the same server), just do it the easy
782 (let ((message-id (message-field-value "message-id")))
783 (if internal-move-group
785 (with-current-buffer (nnimap-buffer)
786 ;; Clear all flags before moving.
787 (nnimap-send-command "UID STORE %d FLAGS.SILENT ()"
789 (nnimap-command "UID COPY %d %S"
791 (utf7-encode internal-move-group t)))))
793 (nnimap-delete-article article)
794 (cons internal-move-group
795 (nnimap-find-article-by-message-id
796 internal-move-group message-id))))
797 ;; Move the article to a different method.
798 (let ((result (eval accept-form)))
800 (nnimap-delete-article article)
803 (deffoo nnimap-request-expire-articles (articles group &optional server force)
807 ((not (nnimap-possibly-change-group group server))
810 (eq nnmail-expiry-target 'delete))
811 (unless (nnimap-delete-article (gnus-compress-sequence articles))
812 (nnheader-message 7 "Article marked for deletion, but not expunged."))
815 (let ((deletable-articles
817 (eq nnmail-expiry-wait 'immediate))
819 (gnus-sorted-intersection
821 (nnimap-find-expired-articles group)))))
822 (if (null deletable-articles)
824 (if (eq nnmail-expiry-target 'delete)
825 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
826 (setq deletable-articles
827 (nnimap-process-expiry-targets
828 deletable-articles group server)))
829 ;; Return the articles we didn't delete.
830 (gnus-sorted-complement articles deletable-articles))))))
832 (defun nnimap-process-expiry-targets (articles group server)
833 (let ((deleted-articles nil))
834 (dolist (article articles)
835 (let ((target nnmail-expiry-target))
837 (mm-disable-multibyte)
838 (when (nnimap-request-article article group server (current-buffer))
839 (nnheader-message 7 "Expiring article %s:%d" group article)
840 (when (functionp target)
841 (setq target (funcall target group)))
843 (not (eq target 'delete)))
844 (if (or (gnus-request-group target t)
845 (gnus-request-create-group target))
846 (nnmail-expiry-target-group target group)
849 (push article deleted-articles))))))
850 ;; Change back to the current group again.
851 (nnimap-possibly-change-group group server)
852 (setq deleted-articles (nreverse deleted-articles))
853 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
856 (defun nnimap-find-expired-articles (group)
857 (let ((cutoff (nnmail-expired-article-p group nil nil)))
858 (with-current-buffer (nnimap-buffer)
861 "UID SEARCH SENTBEFORE %s"
865 (car (rassoc (nth 4 (decode-time cutoff))
866 parse-time-months))))
869 (delete 0 (mapcar #'string-to-number
870 (cdr (assoc "SEARCH" (cdr result))))))))))
873 (defun nnimap-find-article-by-message-id (group message-id)
874 (with-current-buffer (nnimap-buffer)
876 (unless (equal group (nnimap-group nnimap-object))
877 (setf (nnimap-group nnimap-object) nil)
878 (setf (nnimap-examined nnimap-object) group)
879 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
881 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
883 (setq result (nnimap-wait-for-response sequence))
885 (car (setq result (nnimap-parse-response))))
886 ;; Select the last instance of the message in the group.
888 (car (last (assoc "SEARCH" (cdr result)))))
889 (string-to-number article))))))
891 (defun nnimap-delete-article (articles)
892 (with-current-buffer (nnimap-buffer)
893 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
894 (nnimap-article-ranges articles))
896 ((nnimap-capability "UIDPLUS")
897 (nnimap-command "UID EXPUNGE %s"
898 (nnimap-article-ranges articles))
901 (nnimap-command "EXPUNGE")
903 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
904 "server doesn't support UIDPLUS, so we won't "
905 "delete this article now"))))))
907 (deffoo nnimap-request-scan (&optional group server)
908 (when (and (nnimap-possibly-change-group nil server)
910 nnimap-split-methods)
911 (nnheader-message 7 "nnimap %s splitting mail..." server)
912 (nnimap-split-incoming-mail)))
914 (defun nnimap-marks-to-flags (marks)
917 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
921 (deffoo nnimap-request-set-mark (group actions &optional server)
922 (when (nnimap-possibly-change-group group server)
924 (with-current-buffer (nnimap-buffer)
926 ;; Just send all the STORE commands without waiting for
927 ;; response. If they're successful, they're successful.
928 (dolist (action actions)
929 (destructuring-bind (range action marks) action
930 (let ((flags (nnimap-marks-to-flags marks)))
932 (setq sequence (nnimap-send-command
933 "UID STORE %s %sFLAGS.SILENT (%s)"
934 (nnimap-article-ranges range)
938 (mapconcat #'identity flags " ")))))))
939 ;; Wait for the last command to complete to avoid later
940 ;; syncronisation problems with the stream.
942 (nnimap-wait-for-response sequence))))))
944 (deffoo nnimap-request-accept-article (group &optional server last)
945 (when (nnimap-possibly-change-group nil server)
946 (nnmail-check-syntax)
947 (let ((message-id (message-field-value "message-id"))
950 (setq message (buffer-substring-no-properties (point-min) (point-max)))
951 (with-current-buffer (nnimap-buffer)
952 ;; If we have this group open read-only, then unselect it
953 ;; before appending to it.
954 (when (equal (nnimap-examined nnimap-object) group)
955 (nnimap-unselect-group))
957 (setq sequence (nnimap-send-command
958 "APPEND %S {%d}" (utf7-encode group t)
960 (unless nnimap-streaming
961 (nnimap-wait-for-connection "^[+]"))
962 (process-send-string (get-buffer-process (current-buffer)) message)
963 (process-send-string (get-buffer-process (current-buffer))
964 (if (nnimap-newlinep nnimap-object)
967 (let ((result (nnimap-get-response sequence)))
968 (if (not (car result))
970 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
973 (nnimap-find-article-by-message-id group message-id))))))))
975 (deffoo nnimap-request-replace-article (article group buffer)
977 (when (and (nnimap-possibly-change-group group nil)
978 ;; Put the article into the group.
979 (with-current-buffer buffer
981 (nnimap-request-accept-article group nil t))))
982 (nnimap-delete-article (list article))
983 ;; Return the new article number.
986 (defun nnimap-add-cr ()
987 (goto-char (point-min))
988 (while (re-search-forward "\r?\n" nil t)
989 (replace-match "\r\n" t t)))
991 (defun nnimap-get-groups ()
992 (let ((result (nnimap-command "LIST \"\" \"*\""))
995 (dolist (line (cdr result))
996 (when (and (equal (car line) "LIST")
997 (not (and (caadr line)
998 (string-match "noselect" (caadr line)))))
999 (push (car (last line)) groups)))
1000 (nreverse groups))))
1002 (deffoo nnimap-request-list (&optional server)
1003 (nnimap-possibly-change-group nil server)
1004 (with-current-buffer nntp-server-buffer
1007 (with-current-buffer (nnimap-buffer)
1008 (nnimap-get-groups)))
1009 sequences responses)
1011 (with-current-buffer (nnimap-buffer)
1012 (setf (nnimap-group nnimap-object) nil)
1013 (dolist (group groups)
1014 (setf (nnimap-examined nnimap-object) group)
1015 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
1018 (nnimap-wait-for-response (caar sequences))
1020 (nnimap-get-responses (mapcar #'car sequences))))
1021 (dolist (response responses)
1022 (let* ((sequence (car response))
1023 (response (cadr response))
1024 (group (cadr (assoc sequence sequences))))
1026 (equal (caar response) "OK"))
1027 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1029 (dolist (elem response)
1030 (when (equal (cadr elem) "EXISTS")
1031 (setq exists (string-to-number (car elem)))))
1033 (setq highest (1- (string-to-number (car uidnext)))))
1036 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1039 (insert (format "%S %d %d y\n"
1040 (utf7-decode group t) highest (1+ highest))))
1042 ;; Return the widest possible range.
1043 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1044 (or highest exists)))))))))
1047 (deffoo nnimap-request-newgroups (date &optional server)
1048 (nnimap-possibly-change-group nil server)
1049 (with-current-buffer nntp-server-buffer
1051 (dolist (group (with-current-buffer (nnimap-buffer)
1052 (nnimap-get-groups)))
1053 (unless (assoc group nnimap-current-infos)
1054 ;; Insert dummy numbers here -- they don't matter.
1055 (insert (format "%S 0 1 y\n" group))))
1058 (deffoo nnimap-retrieve-group-data-early (server infos)
1059 (when (nnimap-possibly-change-group nil server)
1060 (with-current-buffer (nnimap-buffer)
1062 (setf (nnimap-group nnimap-object) nil)
1063 (let ((qresyncp (nnimap-capability "QRESYNC"))
1064 params groups sequences active uidvalidity modseq group)
1065 ;; Go through the infos and gather the data needed to know
1066 ;; what and how to request the data.
1067 (dolist (info infos)
1068 (setq params (gnus-info-params info)
1069 group (gnus-group-real-name (gnus-info-group info))
1070 active (cdr (assq 'active params))
1071 uidvalidity (cdr (assq 'uidvalidity params))
1072 modseq (cdr (assq 'modseq params)))
1073 (setf (nnimap-examined nnimap-object) group)
1078 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1079 (utf7-encode group t)
1085 (if (and active uidvalidity)
1086 ;; Fetch the last 100 flags.
1087 (max 1 (- (cdr active) 100))
1092 ;; If we don't have a UIDVALIDITY, then this is
1093 ;; the first time we've seen the group, so we
1094 ;; have to do a SELECT (which is slower than an
1095 ;; examine), but will tell us whether the group
1096 ;; is read-only or not.
1098 (push (list (nnimap-send-command "%s %S" command
1099 (utf7-encode group t))
1100 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1101 start group command)
1105 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1106 (when (and sequences
1107 (nnimap-possibly-change-group nil server))
1108 (with-current-buffer (nnimap-buffer)
1109 ;; Wait for the final data to trickle in.
1110 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1114 ;; Now we should have most of the data we need, no matter
1115 ;; whether we're QRESYNCING, fetching all the flags from
1116 ;; scratch, or just fetching the last 100 flags per group.
1117 (nnimap-update-infos (nnimap-flags-to-marks
1119 (nreverse sequences)))
1121 ;; Finally, just return something resembling an active file in
1122 ;; the nntp buffer, so that the agent can save the info, too.
1123 (with-current-buffer nntp-server-buffer
1125 (dolist (info infos)
1126 (let* ((group (gnus-info-group info))
1127 (active (gnus-active group)))
1129 (insert (format "%S %d %d y\n"
1130 (gnus-group-real-name group)
1132 (car active)))))))))))
1134 (defun nnimap-update-infos (flags infos)
1135 (dolist (info infos)
1136 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1137 (marks (cdr (assoc group flags))))
1139 (nnimap-update-info info marks)))))
1141 (defun nnimap-update-info (info marks)
1142 (destructuring-bind (existing flags high low uidnext start-article
1143 permanent-flags uidvalidity
1144 vanished highestmodseq) marks
1146 ;; Ignore groups with no UIDNEXT/marks. This happens for
1147 ;; completely empty groups.
1148 ((and (not existing)
1150 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1152 (gnus-set-active (gnus-info-group info) active))))
1153 ;; We have a mismatch between the old and new UIDVALIDITY
1154 ;; identifiers, so we have to re-request the group info (the next
1155 ;; time). This virtually never happens.
1156 ((let ((old-uidvalidity
1157 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1158 (and old-uidvalidity
1159 (not (equal old-uidvalidity uidvalidity))
1160 (> start-article 1)))
1161 (gnus-group-remove-parameter info 'uidvalidity)
1162 (gnus-group-remove-parameter info 'modseq))
1163 ;; We have the data needed to update.
1165 (let* ((group (gnus-info-group info))
1166 (completep (and start-article
1167 (= start-article 1)))
1168 (active (or (gnus-active group)
1169 (cdr (assq 'active (gnus-info-params info))))))
1171 (setq high (1- uidnext)))
1172 ;; First set the active ranges based on high/low.
1174 (not (gnus-active group)))
1175 (gnus-set-active group
1178 (cons (min (or low (car active))
1180 (max (or high (cdr active))
1185 ;; No articles in this group.
1186 (cons uidnext (1- uidnext)))
1188 (cons start-article (1- start-article)))
1190 ;; No articles and no uidnext.
1195 (or high (1- uidnext)))))
1196 ;; See whether this is a read-only group.
1197 (unless (eq permanent-flags 'not-scanned)
1198 (gnus-group-set-parameter
1199 info 'permanent-flags
1200 (and (or (memq '%* permanent-flags)
1201 (memq '%Seen permanent-flags))
1203 ;; Update marks and read articles if this isn't a
1204 ;; read-only IMAP group.
1205 (when (setq permanent-flags
1206 (cdr (assq 'permanent-flags (gnus-info-params info))))
1207 (if (and highestmodseq
1208 (not start-article))
1209 ;; We've gotten the data by QRESYNCing.
1210 (nnimap-update-qresync-info
1211 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1212 ;; Do normal non-QRESYNC flag updates.
1213 ;; Update the list of read articles.
1215 (gnus-compress-sequence
1216 (gnus-set-difference
1217 (gnus-set-difference
1219 (cdr (assoc '%Seen flags)))
1220 (cdr (assoc '%Flagged flags)))))
1221 (read (gnus-range-difference
1222 (cons start-article high) unread)))
1223 (when (> start-article 1)
1226 (if (> start-article 1)
1227 (gnus-sorted-range-intersection
1228 (cons 1 (1- start-article))
1229 (gnus-info-read info))
1230 (gnus-info-read info))
1232 (when (or (not (listp permanent-flags))
1233 (memq '%Seen permanent-flags))
1234 (gnus-info-set-read info read))
1235 ;; Update the marks.
1236 (setq marks (gnus-info-marks info))
1237 (dolist (type (cdr nnimap-mark-alist))
1238 (when (or (not (listp permanent-flags))
1239 (memq (car (assoc (caddr type) flags))
1241 (memq '%* permanent-flags))
1242 (let ((old-marks (assoc (car type) marks))
1244 (gnus-compress-sequence
1245 (cdr (or (assoc (caddr type) flags) ; %Flagged
1246 (assoc (intern (cadr type) obarray) flags)
1247 (assoc (cadr type) flags)))))) ; "\Flagged"
1248 (setq marks (delq old-marks marks))
1250 (when (and old-marks
1251 (> start-article 1))
1252 (setq old-marks (gnus-range-difference
1254 (cons start-article high)))
1255 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1257 (push (cons (car type) new-marks) marks)))))
1258 (gnus-info-set-marks info marks t))))
1259 ;; Note the active level for the next run-through.
1260 (gnus-group-set-parameter info 'active (gnus-active group))
1261 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1262 (gnus-group-set-parameter info 'modseq highestmodseq)
1263 (nnimap-store-info info (gnus-active group)))))))
1265 (defun nnimap-update-qresync-info (info existing vanished flags)
1266 ;; Add all the vanished articles to the list of read articles.
1271 (gnus-range-add (gnus-info-read info)
1273 (cdr (assq '%Flagged flags)))
1274 (cdr (assq '%Seen flags))))
1275 (let ((marks (gnus-info-marks info)))
1276 (dolist (type (cdr nnimap-mark-alist))
1277 (let ((ticks (assoc (car type) marks))
1279 (cdr (or (assoc (caddr type) flags) ; %Flagged
1280 (assoc (intern (cadr type) obarray) flags)
1281 (assoc (cadr type) flags))))) ; "\Flagged"
1282 (setq marks (delq ticks marks))
1284 ;; Add the new marks we got.
1285 (setq ticks (gnus-add-to-range ticks new-marks))
1286 ;; Remove the marks from messages that don't have them.
1287 (setq ticks (gnus-remove-from-range
1289 (gnus-compress-sequence
1290 (gnus-sorted-complement existing new-marks))))
1292 (push (cons (car type) ticks) marks)))
1293 (gnus-info-set-marks info marks t))))
1295 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1296 (if (zerop (length irange))
1299 (dolist (elem (split-string irange ","))
1301 (if (string-match ":" elem)
1302 (let ((numbers (split-string elem ":")))
1303 (cons (string-to-number (car numbers))
1304 (string-to-number (cadr numbers))))
1305 (string-to-number elem))
1307 (nreverse result))))
1309 (defun nnimap-store-info (info active)
1310 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1311 (entry (assoc group nnimap-current-infos)))
1313 (setcdr entry (list info active))
1314 (push (list group info active) nnimap-current-infos))))
1316 (defun nnimap-flags-to-marks (groups)
1317 (let (data group totalp uidnext articles start-article mark permanent-flags
1318 uidvalidity vanished highestmodseq)
1319 (dolist (elem groups)
1320 (setq group (car elem)
1321 uidnext (nth 1 elem)
1322 start-article (nth 2 elem)
1323 permanent-flags (nth 3 elem)
1324 uidvalidity (nth 4 elem)
1325 vanished (nth 5 elem)
1326 highestmodseq (nth 6 elem)
1327 articles (nthcdr 7 elem))
1328 (let ((high (caar articles))
1330 (dolist (article articles)
1331 (setq low (car article))
1332 (push (car article) existing)
1333 (dolist (flag (cdr article))
1334 (setq mark (assoc flag marks))
1336 (push (list flag (car article)) marks)
1337 (setcdr mark (cons (car article) (cdr mark))))))
1338 (push (list group existing marks high low uidnext start-article
1339 permanent-flags uidvalidity vanished highestmodseq)
1343 (defun nnimap-parse-flags (sequences)
1344 (goto-char (point-min))
1345 ;; Change \Delete etc to %Delete, so that the reader can read it.
1346 (subst-char-in-region (point-min) (point-max)
1348 (let (start end articles groups uidnext elems permanent-flags
1349 uidvalidity vanished highestmodseq)
1350 (dolist (elem sequences)
1351 (destructuring-bind (group-sequence flag-sequence totalp group command)
1353 (setq start (point))
1355 ;; The EXAMINE was successful.
1356 (search-forward (format "\n%d OK " group-sequence) nil t)
1361 (setq permanent-flags
1362 (if (equal command "SELECT")
1363 (and (search-forward "PERMANENTFLAGS "
1364 (or end (point-min)) t)
1365 (read (current-buffer)))
1369 (and (search-forward "UIDNEXT "
1370 (or end (point-min)) t)
1371 (read (current-buffer))))
1374 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1375 (or end (point-min)) t)
1376 ;; Store UIDVALIDITY as a string, as it's
1377 ;; too big for 32-bit Emacsen, usually.
1381 (and (eq flag-sequence 'qresync)
1382 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1383 (or end (point-min)) t)
1387 (and (search-forward "HIGHESTMODSEQ "
1388 (or end (point-min)) t)
1389 (read (current-buffer))))
1392 ;; The UID FETCH FLAGS was successful.
1393 (or (eq flag-sequence 'qresync)
1394 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1395 (if (eq flag-sequence 'qresync)
1399 (setq start (point))
1401 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1402 (setq elems (read (current-buffer)))
1403 (push (cons (cadr (memq 'UID elems))
1404 (cadr (memq 'FLAGS elems)))
1406 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1407 vanished highestmodseq)
1411 (setq articles nil))))
1414 (defun nnimap-find-process-buffer (buffer)
1415 (cadr (assoc buffer nnimap-connection-alist)))
1417 (deffoo nnimap-request-post (&optional server)
1418 (setq nnimap-status-string "Read-only server")
1421 (deffoo nnimap-request-thread (id)
1422 (let* ((refs (split-string
1423 (or (mail-header-references (gnus-summary-article-header))
1427 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1429 (dolist (refid refs value)
1431 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1432 refid refid value)))))
1433 (result (with-current-buffer (nnimap-buffer)
1434 (nnimap-command "UID SEARCH %s" cmd))))
1436 (and (car result) (delete 0 (mapcar #'string-to-number
1437 (cdr (assoc "SEARCH" (cdr result))))))
1440 (defun nnimap-possibly-change-group (group server)
1441 (let ((open-result t))
1443 (not (nnimap-server-opened server)))
1444 (setq open-result (nnimap-open-server server)))
1451 (with-current-buffer (nnimap-buffer)
1452 (if (equal group (nnimap-group nnimap-object))
1454 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1456 (setf (nnimap-group nnimap-object) group
1457 (nnimap-select-result nnimap-object) result)
1460 (defun nnimap-find-connection (buffer)
1461 "Find the connection delivering to BUFFER."
1462 (let ((entry (assoc buffer nnimap-connection-alist)))
1464 (if (and (buffer-name (cadr entry))
1465 (get-buffer-process (cadr entry))
1466 (memq (process-status (get-buffer-process (cadr entry)))
1468 (get-buffer-process (cadr entry))
1469 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1472 (defvar nnimap-sequence 0)
1474 (defun nnimap-send-command (&rest args)
1475 (process-send-string
1476 (get-buffer-process (current-buffer))
1479 (incf nnimap-sequence)
1480 (apply #'format args)
1481 (if (nnimap-newlinep nnimap-object)
1484 ;; Some servers apparently can't have many outstanding
1485 ;; commands, so throttle them.
1486 (unless nnimap-streaming
1487 (nnimap-wait-for-response nnimap-sequence))
1490 (defun nnimap-log-command (command)
1491 (with-current-buffer (get-buffer-create "*imap log*")
1492 (goto-char (point-max))
1493 (insert (format-time-string "%H:%M:%S") " " command))
1496 (defun nnimap-command (&rest args)
1498 (setf (nnimap-last-command-time nnimap-object) (current-time))
1499 (let* ((sequence (apply #'nnimap-send-command args))
1500 (response (nnimap-get-response sequence)))
1501 (if (equal (caar response) "OK")
1503 (nnheader-report 'nnimap "%s"
1504 (mapconcat (lambda (a)
1506 (car response) " "))
1509 (defun nnimap-get-response (sequence)
1510 (nnimap-wait-for-response sequence)
1511 (nnimap-parse-response))
1513 (defun nnimap-wait-for-connection (&optional regexp)
1515 (setq regexp "^[*.] .*\n"))
1516 (let ((process (get-buffer-process (current-buffer))))
1517 (goto-char (point-min))
1518 (while (and (memq (process-status process)
1520 (not (re-search-forward regexp nil t)))
1521 (nnheader-accept-process-output process)
1522 (goto-char (point-min)))
1524 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1527 (defun nnimap-wait-for-response (sequence &optional messagep)
1528 (let ((process (get-buffer-process (current-buffer)))
1532 (goto-char (point-max))
1533 (while (and (setq openp (memq (process-status process)
1535 (not (re-search-backward
1536 (format "^%d .*\n" sequence)
1537 (if nnimap-streaming
1538 (max (point-min) (- (point) 500))
1542 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1543 (nnheader-accept-process-output process)
1544 (goto-char (point-max)))
1547 ;; The user hit C-g while we were waiting: kill the process, in case
1548 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1550 (delete-process process)
1553 (defun nnimap-parse-response ()
1554 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1556 (dolist (line lines)
1557 (push (cdr (nnimap-parse-line line)) result))
1558 ;; Return the OK/error code first, and then all the "continuation
1559 ;; lines" afterwards.
1561 (nreverse result))))
1563 ;; Parse an IMAP response line lightly. They look like
1564 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1565 ;; the lines into a list of strings and lists of string.
1566 (defun nnimap-parse-line (line)
1569 (mm-disable-multibyte)
1571 (goto-char (point-min))
1573 (if (eql (setq char (following-char)) ? )
1581 (if (search-forward "]" (line-end-position) 'move)
1588 (if (search-forward ")" (line-end-position) 'move)
1595 (1- (or (search-forward "\"" (line-end-position) 'move)
1598 (buffer-substring (point) (if (search-forward " " nil t)
1600 (goto-char (point-max))))))
1602 (nreverse result))))
1604 (defun nnimap-last-response-string ()
1607 (let ((end (point)))
1611 (while (and (not (bobp))
1612 (eql (following-char) ?*))
1614 (unless (eql (following-char) ?*)
1616 (buffer-substring (point) end))))
1618 (defun nnimap-get-responses (sequences)
1620 (dolist (sequence sequences)
1621 (goto-char (point-min))
1622 (when (re-search-forward (format "^%d " sequence) nil t)
1623 (push (list sequence (nnimap-parse-response))
1627 (defvar nnimap-incoming-split-list nil)
1629 (defun nnimap-fetch-inbox (articles)
1631 (nnimap-wait-for-response
1632 (nnimap-send-command
1634 (nnimap-article-ranges articles)
1635 (format "(UID %s%s)"
1638 "BODY.PEEK[HEADER] BODY.PEEK"
1640 (if nnimap-split-download-body-default
1645 (defun nnimap-split-incoming-mail ()
1646 (with-current-buffer (nnimap-buffer)
1647 (let ((nnimap-incoming-split-list nil)
1648 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1649 nnmail-split-methods
1650 nnimap-split-methods))
1651 (nnmail-split-fancy (or nnimap-split-fancy
1652 nnmail-split-fancy))
1653 (nnmail-inhibit-default-split-group t)
1654 (groups (nnimap-get-groups))
1657 (nnimap-command "SELECT %S" nnimap-inbox)
1658 (setf (nnimap-group nnimap-object) nnimap-inbox)
1659 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1661 (nnimap-fetch-inbox new-articles)
1662 (nnimap-transform-split-mail)
1663 (nnheader-ms-strip-cr)
1665 (nnmail-split-incoming (current-buffer)
1666 #'nnimap-save-mail-spec
1668 #'nnimap-dummy-active-number
1669 #'nnimap-save-mail-spec)
1670 (when nnimap-incoming-split-list
1671 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1672 sequences junk-articles)
1673 ;; Create any groups that doesn't already exist on the
1675 (dolist (spec specs)
1676 (when (and (not (member (car spec) groups))
1677 (not (eq (car spec) 'junk)))
1678 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1679 ;; Then copy over all the messages.
1681 (dolist (spec specs)
1682 (let ((group (car spec))
1683 (ranges (cdr spec)))
1684 (if (eq group 'junk)
1685 (setq junk-articles ranges)
1686 (push (list (nnimap-send-command
1688 (nnimap-article-ranges ranges)
1689 (utf7-encode group t))
1692 ;; Wait for the last COPY response...
1694 (nnimap-wait-for-response (caar sequences))
1695 ;; And then mark the successful copy actions as deleted,
1696 ;; and possibly expunge them.
1697 (nnimap-mark-and-expunge-incoming
1698 (nnimap-parse-copied-articles sequences)))
1699 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1701 (defun nnimap-mark-and-expunge-incoming (range)
1703 (setq range (nnimap-article-ranges range))
1706 (nnimap-send-command
1707 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1709 ;; If the server supports it, we now delete the message we have
1710 ;; just copied over.
1711 ((nnimap-capability "UIDPLUS")
1712 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1713 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1714 ;; user has configured it.
1716 (setq sequence (nnimap-send-command "EXPUNGE"))))
1717 (nnimap-wait-for-response sequence))))
1719 (defun nnimap-parse-copied-articles (sequences)
1720 (let (sequence copied range)
1721 (goto-char (point-min))
1722 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1723 (setq sequence (string-to-number (match-string 1)))
1724 (when (setq range (cadr (assq sequence sequences)))
1725 (push (gnus-uncompress-range range) copied)))
1726 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1728 (defun nnimap-new-articles (flags)
1730 (dolist (elem flags)
1731 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1733 (push (car elem) new)))
1734 (gnus-compress-sequence (nreverse new))))
1736 (defun nnimap-make-split-specs (list)
1740 (destructuring-bind (article spec) elem
1741 (dolist (group (delete nil (mapcar #'car spec)))
1742 (unless (setq entry (assoc group specs))
1743 (push (setq entry (list group)) specs))
1744 (setcdr entry (cons article (cdr entry))))))
1745 (dolist (entry specs)
1746 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1749 (defun nnimap-transform-split-mail ()
1750 (goto-char (point-min))
1751 (let (article bytes)
1754 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1755 (delete-region (point) (progn (forward-line 1) (point)))
1758 (setq article (match-string 1)
1759 bytes (nnimap-get-length))
1760 (delete-region (line-beginning-position) (line-end-position))
1761 ;; Insert MMDF separator, and a way to remember what this
1763 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1764 (forward-char (1+ bytes))
1765 (setq bytes (nnimap-get-length))
1766 (delete-region (line-beginning-position) (line-end-position))
1767 ;; There's a body; skip past that.
1769 (forward-char (1+ bytes))
1770 (delete-region (line-beginning-position) (line-end-position)))))))
1772 (defun nnimap-dummy-active-number (group &optional server)
1775 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1777 (goto-char (point-min))
1778 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1779 (error "Invalid nnimap mail")
1780 (setq article (string-to-number (match-string 1))))
1782 (if (eq group-art 'junk)
1783 (list (cons 'junk 1))
1785 nnimap-incoming-split-list)))
1789 ;;; nnimap.el ends here