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.
44 (defvoo nnimap-address nil
45 "The address of the IMAP server.")
47 (defvoo nnimap-server-port nil
49 If nnimap-stream is `ssl', this will default to `imaps'. If not,
50 it will default to `imap'.")
52 (defvoo nnimap-stream 'ssl
53 "How nnimap will talk to the IMAP server.
54 Values are `ssl', `network', `starttls' or `shell'.")
56 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
57 (if (listp imap-shell-program)
58 (car imap-shell-program)
62 (defvoo nnimap-inbox nil
63 "The mail box where incoming mail arrives and should be split out of.")
65 (defvoo nnimap-authenticator nil
66 "How nnimap authenticate itself to the server.
67 Possible choices are nil (use default methods) or `anonymous'.")
69 (defvoo nnimap-fetch-partial-articles nil
70 "If non-nil, nnimap will fetch partial articles.
71 If t, nnimap will fetch only the first part. If a string, it
72 will fetch all parts that have types that match that string. A
73 likely value would be \"text/\" to automatically fetch all
76 (defvoo nnimap-expunge t
77 "If non-nil, expunge articles after deleting them.
78 This is always done if the server supports UID EXPUNGE, but it's
79 not done by default on servers that doesn't support that command.")
82 (defvoo nnimap-connection-alist nil)
84 (defvoo nnimap-current-infos nil)
86 (defvar nnimap-process nil)
88 (defvar nnimap-status-string "")
90 (defvar nnimap-split-download-body-default nil
91 "Internal variable with default value for `nnimap-split-download-body'.")
93 (defvar nnimap-keepalive-timer nil)
94 (defvar nnimap-process-buffers nil)
97 group process commands capabilities select-result newlinep server
100 (defvar nnimap-object nil)
102 (defvar nnimap-mark-alist
103 '((read "\\Seen" %Seen)
104 (tick "\\Flagged" %Flagged)
105 (reply "\\Answered" %Answered)
106 (expire "gnus-expire")
107 (dormant "gnus-dormant")
110 (download "gnus-download")
111 (forward "gnus-forward")))
113 (defvar nnimap-split-methods nil)
115 (defun nnimap-buffer ()
116 (nnimap-find-process-buffer nntp-server-buffer))
118 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
119 (with-current-buffer nntp-server-buffer
121 (when (nnimap-possibly-change-group group server)
122 (with-current-buffer (nnimap-buffer)
123 (nnimap-send-command "SELECT %S" (utf7-encode group t))
125 (nnimap-wait-for-response
128 (nnimap-article-ranges (gnus-compress-sequence articles))
129 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
132 "BODY.PEEK[HEADER.FIELDS %s]"
133 "RFC822.HEADER.LINES %s")
134 (append '(Subject From Date Message-Id
135 References In-Reply-To Xref)
136 nnmail-extra-headers))))
138 (nnimap-transform-headers))
139 (insert-buffer-substring
140 (nnimap-find-process-buffer (current-buffer))))
143 (defun nnimap-transform-headers ()
144 (goto-char (point-min))
145 (let (article bytes lines size string)
148 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
149 (delete-region (point) (progn (forward-line 1) (point)))
152 (setq article (match-string 1))
153 ;; Unfold quoted {number} strings.
154 (while (re-search-forward "[^]] {\\([0-9]+\\)}\r\n"
155 (1+ (line-end-position)) t)
156 (setq size (string-to-number (match-string 1)))
157 (delete-region (+ (match-beginning 0) 2) (point))
158 (setq string (delete-region (point) (+ (point) size)))
159 (insert (format "%S" string)))
160 (setq bytes (nnimap-get-length)
164 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
169 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
170 (let ((structure (ignore-errors
171 (read (current-buffer)))))
172 (while (and (consp structure)
173 (not (stringp (car structure))))
174 (setq structure (car structure)))
175 (setq lines (nth 7 structure))))
176 (delete-region (line-beginning-position) (line-end-position))
177 (insert (format "211 %s Article retrieved." article))
180 (insert (format "Chars: %s\n" size)))
182 (insert (format "Lines: %s\n" lines)))
183 (re-search-forward "^\r$")
184 (delete-region (line-beginning-position) (line-end-position))
188 (defun nnimap-get-length ()
189 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
190 (string-to-number (match-string 1))))
192 (defun nnimap-article-ranges (ranges)
196 (number-to-string ranges))
197 ((numberp (cdr ranges))
198 (format "%d:%d" (car ranges) (cdr ranges)))
200 (dolist (elem ranges)
203 (format "%d:%d" (car elem) (cdr elem))
204 (number-to-string elem))
206 (mapconcat #'identity (nreverse result) ",")))))
208 (deffoo nnimap-open-server (server &optional defs)
209 (if (nnimap-server-opened server)
211 (unless (assq 'nnimap-address defs)
212 (setq defs (append defs (list (list 'nnimap-address server)))))
213 (nnoo-change-server 'nnimap server defs)
214 (or (nnimap-find-connection nntp-server-buffer)
215 (nnimap-open-connection nntp-server-buffer))))
217 (defun nnimap-make-process-buffer (buffer)
219 (generate-new-buffer (format "*nnimap %s %s %s*"
220 nnimap-address nnimap-server-port
221 (gnus-buffer-exists-p buffer)))
222 (mm-disable-multibyte)
223 (buffer-disable-undo)
225 (set (make-local-variable 'after-change-functions) nil)
226 (set (make-local-variable 'nnimap-object)
227 (make-nnimap :server (nnoo-current-server 'nnimap)))
228 (push (list buffer (current-buffer)) nnimap-connection-alist)
229 (push (current-buffer) nnimap-process-buffers)
232 (defun nnimap-open-shell-stream (name buffer host port)
233 (let ((process-connection-type nil))
234 (start-process name buffer shell-file-name
242 (defun nnimap-credentials (address ports)
243 (let (port credentials)
244 ;; Request the credentials from all ports, but only query on the
245 ;; last port if all the previous ones have failed.
246 (while (and (null credentials)
247 (setq port (pop ports)))
249 (auth-source-user-or-password
250 '("login" "password") address port nil (null ports))))
253 (defun nnimap-keepalive ()
254 (let ((now (current-time)))
255 (dolist (buffer nnimap-process-buffers)
256 (when (buffer-name buffer)
257 (with-current-buffer buffer
258 (when (and nnimap-object
259 (nnimap-last-command-time nnimap-object)
263 (nnimap-last-command-time nnimap-object)))
264 ;; More than five minutes since the last command.
266 (nnimap-send-command "NOOP")))))))
268 (defun nnimap-open-connection (buffer)
269 (unless nnimap-keepalive-timer
270 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
272 (with-current-buffer (nnimap-make-process-buffer buffer)
273 (let* ((coding-system-for-read 'binary)
274 (coding-system-for-write 'binary)
277 ((eq nnimap-stream 'network)
279 "*nnimap*" (current-buffer) nnimap-address
280 (or nnimap-server-port
281 (if (netrc-find-service-number "imap")
285 ((eq nnimap-stream 'shell)
286 (nnimap-open-shell-stream
287 "*nnimap*" (current-buffer) nnimap-address
288 (or nnimap-server-port "imap"))
290 ((eq nnimap-stream 'starttls)
291 (starttls-open-stream
292 "*nnimap*" (current-buffer) nnimap-address
293 (or nnimap-server-port "imap"))
295 ((eq nnimap-stream 'ssl)
297 "*nnimap*" (current-buffer) nnimap-address
298 (or nnimap-server-port
299 (if (netrc-find-service-number "imaps")
302 '("143" "993" "imap" "imaps"))))
303 connection-result login-result credentials)
304 (setf (nnimap-process nnimap-object)
305 (get-buffer-process (current-buffer)))
306 (when (and (nnimap-process nnimap-object)
307 (memq (process-status (nnimap-process nnimap-object))
309 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
310 (when (setq connection-result (nnimap-wait-for-connection))
311 (when (eq nnimap-stream 'starttls)
312 (nnimap-command "STARTTLS")
313 (starttls-negotiate (nnimap-process nnimap-object)))
314 (unless (equal connection-result "PREAUTH")
315 (if (not (setq credentials
316 (if (eq nnimap-authenticator 'anonymous)
318 (message-make-address))
321 (if nnimap-server-port
322 (cons (format "%s" nnimap-server-port) ports)
324 (setq nnimap-object nil)
325 (setq login-result (nnimap-command "LOGIN %S %S"
328 (unless (car login-result)
329 (delete-process (nnimap-process nnimap-object))
330 (setq nnimap-object nil))))
332 (setf (nnimap-capabilities nnimap-object)
335 (or (nnimap-find-parameter "CAPABILITY" (cdr login-result))
336 (nnimap-find-parameter
337 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
338 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
339 (nnimap-command "ENABLE QRESYNC"))
342 (defun nnimap-find-parameter (parameter elems)
346 ((equal (car elem) parameter)
347 (setq result (cdr elem)))
348 ((and (equal (car elem) "OK")
350 (equal (caadr elem) parameter))
351 (setq result (cdr (cadr elem))))))
354 (deffoo nnimap-close-server (&optional server)
357 (deffoo nnimap-request-close ()
360 (deffoo nnimap-server-opened (&optional server)
361 (and (nnoo-current-server-p 'nnimap server)
363 (gnus-buffer-live-p nntp-server-buffer)
364 (nnimap-find-connection nntp-server-buffer)))
366 (deffoo nnimap-status-message (&optional server)
367 nnimap-status-string)
369 (deffoo nnimap-request-article (article &optional group server to-buffer)
370 (with-current-buffer nntp-server-buffer
371 (let ((result (nnimap-possibly-change-group group server))
373 (when (stringp article)
374 (setq article (nnimap-find-article-by-message-id group article)))
378 (with-current-buffer (nnimap-buffer)
380 (when nnimap-fetch-partial-articles
381 (if (eq nnimap-fetch-partial-articles t)
383 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
384 (goto-char (point-min))
385 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
386 (setq structure (ignore-errors (read (current-buffer)))
387 parts (nnimap-find-wanted-parts structure)))))
389 (nnimap-get-partial-article article parts structure)
390 (nnimap-get-whole-article article))
391 (let ((buffer (current-buffer)))
392 (with-current-buffer (or to-buffer nntp-server-buffer)
394 (insert-buffer-substring buffer)
395 (nnheader-ms-strip-cr)
396 (cons group article)))))))))
398 (defun nnimap-get-whole-article (article)
402 "UID FETCH %d BODY.PEEK[]"
403 "UID FETCH %d RFC822.PEEK")
405 ;; Check that we really got an article.
406 (goto-char (point-min))
407 (unless (looking-at "\\* [0-9]+ FETCH")
410 (goto-char (point-min))
411 (let ((bytes (nnimap-get-length)))
412 (delete-region (line-beginning-position)
413 (progn (forward-line 1) (point)))
414 (goto-char (+ (point) bytes))
415 (delete-region (point) (point-max)))
418 (defun nnimap-ver4-p ()
419 (member "IMAP4REV1" (nnimap-capabilities nnimap-object)))
421 (defun nnimap-get-partial-article (article parts structure)
424 "UID FETCH %d (%s %s)"
430 (mapconcat (lambda (part)
431 (format "BODY.PEEK[%s]" part))
433 (mapconcat (lambda (part)
434 (format "RFC822.PEEK[%s]" part))
437 (nnimap-convert-partial-article structure))))
439 (defun nnimap-convert-partial-article (structure)
440 ;; First just skip past the headers.
441 (goto-char (point-min))
442 (let ((bytes (nnimap-get-length))
444 ;; Delete "FETCH" line.
445 (delete-region (line-beginning-position)
446 (progn (forward-line 1) (point)))
447 (goto-char (+ (point) bytes))
448 ;; Collect all the body parts.
449 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
450 (setq id (match-string 1)
451 bytes (nnimap-get-length))
453 (delete-region (point) (progn (forward-line 1) (point)))
454 (push (list id (buffer-substring (point) (+ (point) bytes)))
456 (delete-region (point) (+ (point) bytes)))
457 ;; Delete trailing junk.
458 (delete-region (point) (point-max))
459 ;; Now insert all the parts again where they fit in the structure.
460 (nnimap-insert-partial-structure structure parts)
463 (defun nnimap-insert-partial-structure (structure parts &optional subp)
464 (let ((type (car (last structure 4)))
465 (boundary (cadr (member "BOUNDARY" (car (last structure 3))))))
467 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
468 (downcase type) boundary)))
469 (while (not (stringp (car structure)))
470 (insert "\n--" boundary "\n")
471 (if (consp (caar structure))
472 (nnimap-insert-partial-structure (pop structure) parts t)
473 (let ((bit (pop structure)))
474 (insert (format "Content-type: %s/%s"
475 (downcase (nth 0 bit))
476 (downcase (nth 1 bit))))
477 (if (member "CHARSET" (nth 2 bit))
479 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
481 (insert (format "Content-transfer-encoding: %s\n"
484 (when (assoc (nth 9 bit) parts)
485 (insert (cadr (assoc (nth 9 bit) parts)))))))
486 (insert "\n--" boundary "--\n")))
488 (defun nnimap-find-wanted-parts (structure)
489 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
491 (defun nnimap-find-wanted-parts-1 (structure prefix)
494 (while (consp (car structure))
495 (let ((sub (pop structure)))
496 (if (consp (car sub))
497 (push (nnimap-find-wanted-parts-1
498 sub (if (string= prefix "")
499 (number-to-string num)
500 (format "%s.%s" prefix num)))
502 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
503 (id (if (string= prefix "")
504 (number-to-string num)
505 (format "%s.%s" prefix num))))
506 (setcar (nthcdr 9 sub) id)
507 (when (string-match nnimap-fetch-partial-articles type)
512 (deffoo nnimap-request-group (group &optional server dont-check info)
513 (let ((result (nnimap-possibly-change-group group server))
514 articles active marks high low)
515 (with-current-buffer nntp-server-buffer
518 (setq active (nth 2 (assoc group nnimap-current-infos))))
519 (insert (format "211 %d %d %d %S\n"
520 (- (cdr active) (car active))
524 (with-current-buffer (nnimap-buffer)
526 (let ((group-sequence
527 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
529 (nnimap-send-command "UID FETCH 1:* FLAGS")))
530 (nnimap-wait-for-response flag-sequence)
532 (nnimap-flags-to-marks
534 (list (list group-sequence flag-sequence 1 group)))))
536 (nnimap-update-infos marks (list info)))
537 (goto-char (point-max))
538 (let ((uidnext (nth 5 (car marks))))
539 (setq high (if uidnext
542 low (or (nth 4 (car marks)) uidnext)))))
546 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
549 (deffoo nnimap-request-create-group (group &optional server args)
550 (when (nnimap-possibly-change-group nil server)
551 (with-current-buffer (nnimap-buffer)
552 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
554 (deffoo nnimap-request-delete-group (group &optional force server)
555 (when (nnimap-possibly-change-group nil server)
556 (with-current-buffer (nnimap-buffer)
557 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
559 (deffoo nnimap-request-expunge-group (group &optional server)
560 (when (nnimap-possibly-change-group group server)
561 (with-current-buffer (nnimap-buffer)
562 (car (nnimap-command "EXPUNGE")))))
564 (defun nnimap-get-flags (spec)
567 (with-current-buffer (nnimap-buffer)
569 (nnimap-wait-for-response (nnimap-send-command
570 "UID FETCH %s FLAGS" spec))
571 (goto-char (point-min))
572 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
573 (setq elems (nnimap-parse-line (match-string 1)))
574 (push (cons (string-to-number (cadr (member "UID" elems)))
575 (cadr (member "FLAGS" elems)))
577 (nreverse articles)))
579 (deffoo nnimap-close-group (group &optional server)
582 (deffoo nnimap-request-move-article (article group server accept-form
583 &optional last internal-move-group)
585 (when (nnimap-request-article article group server (current-buffer))
586 ;; If the move is internal (on the same server), just do it the easy
588 (let ((message-id (message-field-value "message-id")))
589 (if internal-move-group
591 (with-current-buffer (nnimap-buffer)
592 (nnimap-command "UID COPY %d %S"
594 (utf7-encode internal-move-group t)))))
596 (nnimap-delete-article article)
597 (cons internal-move-group
598 (nnimap-find-article-by-message-id
599 internal-move-group message-id))))
600 ;; Move the article to a different method.
601 (let ((result (eval accept-form)))
603 (nnimap-delete-article article)
606 (deffoo nnimap-request-expire-articles (articles group &optional server force)
610 ((not (nnimap-possibly-change-group group server))
613 (eq nnmail-expiry-target 'delete))
614 (unless (nnimap-delete-article articles)
615 (message "Article marked for deletion, but not expunged."))
618 (let ((deletable-articles
620 (eq nnmail-expiry-wait 'immediate))
622 (gnus-sorted-intersection
624 (nnimap-find-expired-articles group)))))
625 (if (null deletable-articles)
627 (if (eq nnmail-expiry-target 'delete)
628 (nnimap-delete-article deletable-articles)
629 (setq deletable-articles
630 (nnimap-process-expiry-targets
631 deletable-articles group server)))
632 ;; Return the articles we didn't delete.
633 (gnus-sorted-complement articles deletable-articles))))))
635 (defun nnimap-process-expiry-targets (articles group server)
636 (let ((deleted-articles nil))
637 (dolist (article articles)
638 (let ((target nnmail-expiry-target))
640 (when (nnimap-request-article article group server (current-buffer))
641 (message "Expiring article %s:%d" group article)
642 (when (functionp target)
643 (setq target (funcall target group)))
645 (not (eq target 'delete)))
646 (if (or (gnus-request-group target t)
647 (gnus-request-create-group target))
648 (nnmail-expiry-target-group target group)
651 (push article deleted-articles))))))
652 ;; Change back to the current group again.
653 (nnimap-possibly-change-group group server)
654 (setq deleted-articles (nreverse deleted-articles))
655 (nnimap-delete-article deleted-articles)
658 (defun nnimap-find-expired-articles (group)
659 (let ((cutoff (nnmail-expired-article-p group nil nil)))
660 (with-current-buffer (nnimap-buffer)
663 "UID SEARCH SENTBEFORE %s"
667 (car (rassoc (nth 4 (decode-time cutoff))
668 parse-time-months))))
671 (delete 0 (mapcar #'string-to-number
672 (cdr (assoc "SEARCH" (cdr result))))))))))
675 (defun nnimap-find-article-by-message-id (group message-id)
676 (when (nnimap-possibly-change-group group nil)
677 (with-current-buffer (nnimap-buffer)
679 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
682 ;; Select the last instance of the message in the group.
684 (car (last (assoc "SEARCH" (cdr result)))))
685 (string-to-number article)))))))
687 (defun nnimap-delete-article (articles)
688 (with-current-buffer (nnimap-buffer)
689 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
690 (nnimap-article-ranges articles))
692 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
693 (nnimap-command "UID EXPUNGE %s"
694 (nnimap-article-ranges articles))
697 (nnimap-command "EXPUNGE")
699 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
700 "server doesn't support UIDPLUS, so we won't "
701 "delete this article now"))))))
703 (deffoo nnimap-request-scan (&optional group server)
704 (when (and (nnimap-possibly-change-group nil server)
706 nnimap-split-methods)
707 (message "nnimap %s splitting mail..." server)
708 (nnimap-split-incoming-mail)))
710 (defun nnimap-marks-to-flags (marks)
713 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
717 (deffoo nnimap-request-set-mark (group actions &optional server)
718 (when (nnimap-possibly-change-group group server)
720 (with-current-buffer (nnimap-buffer)
721 ;; Just send all the STORE commands without waiting for
722 ;; response. If they're successful, they're successful.
723 (dolist (action actions)
724 (destructuring-bind (range action marks) action
725 (let ((flags (nnimap-marks-to-flags marks)))
727 (setq sequence (nnimap-send-command
728 "UID STORE %s %sFLAGS.SILENT (%s)"
729 (nnimap-article-ranges range)
733 (mapconcat #'identity flags " ")))))))
734 ;; Wait for the last command to complete to avoid later
735 ;; syncronisation problems with the stream.
737 (nnimap-wait-for-response sequence))))))
739 (deffoo nnimap-request-accept-article (group &optional server last)
740 (when (nnimap-possibly-change-group nil server)
741 (nnmail-check-syntax)
742 (let ((message (buffer-string))
743 (message-id (message-field-value "message-id"))
745 (with-current-buffer (nnimap-buffer)
746 (setq sequence (nnimap-send-command
747 "APPEND %S {%d}" (utf7-encode group t)
749 (process-send-string (get-buffer-process (current-buffer)) message)
750 (process-send-string (get-buffer-process (current-buffer))
751 (if (nnimap-newlinep nnimap-object)
754 (let ((result (nnimap-get-response sequence)))
757 (nnimap-find-article-by-message-id group message-id))))))))
759 (defun nnimap-add-cr ()
760 (goto-char (point-min))
761 (while (re-search-forward "\r?\n" nil t)
762 (replace-match "\r\n" t t)))
764 (defun nnimap-get-groups ()
765 (let ((result (nnimap-command "LIST \"\" \"*\""))
768 (dolist (line (cdr result))
769 (when (and (equal (car line) "LIST")
770 (not (and (caadr line)
771 (string-match "noselect" (caadr line)))))
772 (push (car (last line)) groups)))
775 (deffoo nnimap-request-list (&optional server)
776 (nnimap-possibly-change-group nil server)
777 (with-current-buffer nntp-server-buffer
780 (with-current-buffer (nnimap-buffer)
781 (nnimap-get-groups)))
784 (with-current-buffer (nnimap-buffer)
785 (setf (nnimap-group nnimap-object) nil)
786 (dolist (group groups)
787 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
790 (nnimap-wait-for-response (caar sequences))
792 (nnimap-get-responses (mapcar #'car sequences))))
793 (dolist (response responses)
794 (let* ((sequence (car response))
795 (response (cadr response))
796 (group (cadr (assoc sequence sequences))))
798 (equal (caar response) "OK"))
799 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
801 (dolist (elem response)
802 (when (equal (cadr elem) "EXISTS")
803 (setq exists (string-to-number (car elem)))))
805 (setq highest (1- (string-to-number (car uidnext)))))
808 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
811 (insert (format "%S %d %d y\n"
812 (utf7-decode group t) highest (1+ highest))))
814 ;; Return the widest possible range.
815 (insert (format "%S %d 1 y\n" (utf7-decode group t)
816 (or highest exists)))))))))
819 (deffoo nnimap-retrieve-group-data-early (server infos)
820 (when (nnimap-possibly-change-group nil server)
821 (with-current-buffer (nnimap-buffer)
822 ;; QRESYNC handling isn't implemented.
823 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
824 marks groups sequences)
825 ;; Go through the infos and gather the data needed to know
826 ;; what and how to request the data.
828 (setq marks (gnus-info-marks info))
829 (push (list (gnus-group-real-name (gnus-info-group info))
830 (cdr (assq 'active marks))
831 (cdr (assq 'uid marks)))
833 ;; Then request the data.
835 (setf (nnimap-group nnimap-object) nil)
836 (dolist (elem groups)
841 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
850 ;; Fetch the last 100 flags.
851 (max 1 (- (cdr (nth 1 elem)) 100))
853 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
854 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
860 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
862 (nnimap-possibly-change-group nil server))
863 (with-current-buffer (nnimap-buffer)
864 ;; Wait for the final data to trickle in.
865 (nnimap-wait-for-response (cadar sequences))
866 ;; Now we should have all the data we need, no matter whether
867 ;; we're QRESYNCING, fetching all the flags from scratch, or
868 ;; just fetching the last 100 flags per group.
869 (nnimap-update-infos (nnimap-flags-to-marks
871 (nreverse sequences)))
873 ;; Finally, just return something resembling an active file in
874 ;; the nntp buffer, so that the agent can save the info, too.
875 (with-current-buffer nntp-server-buffer
878 (let* ((group (gnus-info-group info))
879 (active (gnus-active group)))
881 (insert (format "%S %d %d y\n"
882 (gnus-group-real-name group)
884 (car active))))))))))
886 (defun nnimap-update-infos (flags infos)
888 (let ((group (gnus-group-real-name (gnus-info-group info))))
889 (nnimap-update-info info (cdr (assoc group flags))))))
891 (defun nnimap-update-info (info marks)
893 (destructuring-bind (existing flags high low uidnext start-article
894 permanent-flags) marks
895 (let ((group (gnus-info-group info))
896 (completep (and start-article
897 (= start-article 1))))
899 (setq high (1- uidnext)))
900 ;; First set the active ranges based on high/low.
902 (not (gnus-active group)))
903 (gnus-set-active group
908 ;; No articles in this group.
909 (cons uidnext (1- uidnext)))
911 (cons start-article (1- start-article)))
913 ;; No articles and no uidnext.
915 (setcdr (gnus-active group) (or high (1- uidnext))))
916 (when (and (not high)
918 (setq high (1- uidnext)))
919 ;; Then update the list of read articles.
921 (gnus-compress-sequence
925 (cdr (assoc '%Seen flags)))
926 (cdr (assoc '%Flagged flags)))))
927 (read (gnus-range-difference
928 (cons start-article high) unread)))
929 (when (> start-article 1)
932 (if (> start-article 1)
933 (gnus-sorted-range-intersection
934 (cons 1 (1- start-article))
935 (gnus-info-read info))
936 (gnus-info-read info))
938 (gnus-info-set-read info read)
940 (setq marks (gnus-info-marks info))
941 ;; Note the active level for the next run-through.
942 (let ((active (assq 'active marks)))
944 (setcdr active (gnus-active group))
945 (push (cons 'active (gnus-active group)) marks)))
946 (dolist (type (cdr nnimap-mark-alist))
947 (let ((old-marks (assoc (car type) marks))
949 (gnus-compress-sequence
950 (cdr (or (assoc (caddr type) flags) ; %Flagged
951 (assoc (intern (cadr type) obarray) flags)
952 (assoc (cadr type) flags)))))) ; "\Flagged"
953 (setq marks (delq old-marks marks))
957 (setq old-marks (gnus-range-difference
959 (cons start-article high)))
960 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
962 (push (cons (car type) new-marks) marks)))
963 (gnus-info-set-marks info marks t)
964 (nnimap-store-info info (gnus-active group))))))))
966 (defun nnimap-store-info (info active)
967 (let* ((group (gnus-group-real-name (gnus-info-group info)))
968 (entry (assoc group nnimap-current-infos)))
970 (setcdr entry (list info active))
971 (push (list group info active) nnimap-current-infos))))
973 (defun nnimap-flags-to-marks (groups)
974 (let (data group totalp uidnext articles start-article mark permanent-flags)
975 (dolist (elem groups)
976 (setq group (car elem)
978 start-article (nth 2 elem)
979 permanent-flags (nth 3 elem)
980 articles (nthcdr 4 elem))
981 (let ((high (caar articles))
983 (dolist (article articles)
984 (setq low (car article))
985 (push (car article) existing)
986 (dolist (flag (cdr article))
987 (setq mark (assoc flag marks))
989 (push (list flag (car article)) marks)
990 (setcdr mark (cons (car article) (cdr mark))))))
991 (push (list group existing marks high low uidnext start-article
996 (defun nnimap-parse-flags (sequences)
997 (goto-char (point-min))
998 ;; Change \Delete etc to %Delete, so that the reader can read it.
999 (subst-char-in-region (point-min) (point-max)
1001 (let (start end articles groups uidnext elems permanent-flags)
1002 (dolist (elem sequences)
1003 (destructuring-bind (group-sequence flag-sequence totalp group) elem
1004 (setq start (point))
1005 ;; The EXAMINE was successful.
1006 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
1011 (setq permanent-flags
1012 (and (search-forward "PERMANENTFLAGS "
1013 (or end (point-min)) t)
1014 (read (current-buffer))))
1017 (and (search-forward "UIDNEXT "
1018 (or end (point-min)) t)
1019 (read (current-buffer))))
1022 ;; The UID FETCH FLAGS was successful.
1023 (search-forward (format "\n%d OK " flag-sequence) nil t))
1024 (setq start (point))
1026 (while (search-forward " FETCH " start t)
1027 (setq elems (read (current-buffer)))
1028 (push (cons (cadr (memq 'UID elems))
1029 (cadr (memq 'FLAGS elems)))
1031 (push (nconc (list group uidnext totalp permanent-flags) articles)
1033 (setq articles nil))))
1036 (defun nnimap-find-process-buffer (buffer)
1037 (cadr (assoc buffer nnimap-connection-alist)))
1039 (deffoo nnimap-request-post (&optional server)
1040 (setq nnimap-status-string "Read-only server")
1043 (defun nnimap-possibly-change-group (group server)
1044 (let ((open-result t))
1046 (not (nnimap-server-opened server)))
1047 (setq open-result (nnimap-open-server server)))
1054 (with-current-buffer (nnimap-buffer)
1055 (if (equal group (nnimap-group nnimap-object))
1057 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1059 (setf (nnimap-group nnimap-object) group
1060 (nnimap-select-result nnimap-object) result)
1063 (defun nnimap-find-connection (buffer)
1064 "Find the connection delivering to BUFFER."
1065 (let ((entry (assoc buffer nnimap-connection-alist)))
1067 (if (and (buffer-name (cadr entry))
1068 (get-buffer-process (cadr entry))
1069 (memq (process-status (get-buffer-process (cadr entry)))
1071 (get-buffer-process (cadr entry))
1072 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1075 (defvar nnimap-sequence 0)
1077 (defun nnimap-send-command (&rest args)
1078 (process-send-string
1079 (get-buffer-process (current-buffer))
1082 (incf nnimap-sequence)
1083 (apply #'format args)
1084 (if (nnimap-newlinep nnimap-object)
1089 (defun nnimap-log-command (command)
1090 (with-current-buffer (get-buffer-create "*imap log*")
1091 (goto-char (point-max))
1092 (insert (format-time-string "%H:%M:%S") " " command))
1095 (defun nnimap-command (&rest args)
1097 (setf (nnimap-last-command-time nnimap-object) (current-time))
1098 (let* ((sequence (apply #'nnimap-send-command args))
1099 (response (nnimap-get-response sequence)))
1100 (if (equal (caar response) "OK")
1102 (nnheader-report 'nnimap "%s"
1103 (mapconcat (lambda (a)
1105 (car response) " "))
1108 (defun nnimap-get-response (sequence)
1109 (nnimap-wait-for-response sequence)
1110 (nnimap-parse-response))
1112 (defun nnimap-wait-for-connection ()
1113 (let ((process (get-buffer-process (current-buffer))))
1114 (goto-char (point-min))
1115 (while (and (memq (process-status process)
1117 (not (re-search-forward "^\\* .*\n" nil t)))
1118 (nnheader-accept-process-output process)
1119 (goto-char (point-min)))
1121 (and (looking-at "\\* \\([A-Z0-9]+\\)")
1124 (defun nnimap-wait-for-response (sequence &optional messagep)
1125 (let ((process (get-buffer-process (current-buffer))))
1126 (goto-char (point-max))
1127 (while (and (memq (process-status process)
1129 (not (re-search-backward (format "^%d .*\n" sequence)
1130 (max (point-min) (- (point) 500))
1133 (message "Read %dKB" (/ (buffer-size) 1000)))
1134 (nnheader-accept-process-output process)
1135 (goto-char (point-max)))))
1137 (defun nnimap-parse-response ()
1138 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1140 (dolist (line lines)
1141 (push (cdr (nnimap-parse-line line)) result))
1142 ;; Return the OK/error code first, and then all the "continuation
1143 ;; lines" afterwards.
1145 (nreverse result))))
1147 ;; Parse an IMAP response line lightly. They look like
1148 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1149 ;; the lines into a list of strings and lists of string.
1150 (defun nnimap-parse-line (line)
1154 (goto-char (point-min))
1156 (if (eql (setq char (following-char)) ? )
1161 (split-string (buffer-substring
1162 (1+ (point)) (1- (search-forward "]")))))
1164 (split-string (buffer-substring
1165 (1+ (point)) (1- (search-forward ")")))))
1168 (buffer-substring (point) (1- (search-forward "\""))))
1170 (buffer-substring (point) (if (search-forward " " nil t)
1172 (goto-char (point-max))))))
1174 (nreverse result))))
1176 (defun nnimap-last-response-string ()
1179 (let ((end (point)))
1183 (while (and (not (bobp))
1184 (eql (following-char) ?*))
1186 (unless (eql (following-char) ?*)
1188 (buffer-substring (point) end))))
1190 (defun nnimap-get-responses (sequences)
1192 (dolist (sequence sequences)
1193 (goto-char (point-min))
1194 (when (re-search-forward (format "^%d " sequence) nil t)
1195 (push (list sequence (nnimap-parse-response))
1199 (defvar nnimap-incoming-split-list nil)
1201 (defun nnimap-fetch-inbox (articles)
1203 (nnimap-wait-for-response
1204 (nnimap-send-command
1206 (nnimap-article-ranges articles)
1207 (format "(UID %s%s)"
1210 "BODY.PEEK[HEADER] BODY.PEEK"
1212 (if nnimap-split-download-body-default
1217 (defun nnimap-split-incoming-mail ()
1218 (with-current-buffer (nnimap-buffer)
1219 (let ((nnimap-incoming-split-list nil)
1220 (nnmail-split-methods nnimap-split-methods)
1221 (nnmail-inhibit-default-split-group t)
1222 (groups (nnimap-get-groups))
1225 (nnimap-command "SELECT %S" nnimap-inbox)
1226 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1228 (nnimap-fetch-inbox new-articles)
1229 (nnimap-transform-split-mail)
1230 (nnheader-ms-strip-cr)
1232 (nnmail-split-incoming (current-buffer)
1233 #'nnimap-save-mail-spec
1235 #'nnimap-dummy-active-number
1236 #'nnimap-save-mail-spec)
1237 (when nnimap-incoming-split-list
1238 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1239 sequences junk-articles)
1240 ;; Create any groups that doesn't already exist on the
1242 (dolist (spec specs)
1243 (when (and (not (member (car spec) groups))
1244 (not (eq (car spec) 'junk)))
1245 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1246 ;; Then copy over all the messages.
1248 (dolist (spec specs)
1249 (let ((group (car spec))
1250 (ranges (cdr spec)))
1251 (if (eq group 'junk)
1252 (setq junk-articles ranges)
1253 (push (list (nnimap-send-command
1255 (nnimap-article-ranges ranges)
1256 (utf7-encode group t))
1259 ;; Wait for the last COPY response...
1261 (nnimap-wait-for-response (caar sequences))
1262 ;; And then mark the successful copy actions as deleted,
1263 ;; and possibly expunge them.
1264 (nnimap-mark-and-expunge-incoming
1265 (nnimap-parse-copied-articles sequences)))
1266 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1268 (defun nnimap-mark-and-expunge-incoming (range)
1270 (setq range (nnimap-article-ranges range))
1272 (nnimap-send-command
1273 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1275 ;; If the server supports it, we now delete the message we have
1276 ;; just copied over.
1277 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1278 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1279 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1280 ;; user has configured it.
1282 (setq sequence (nnimap-send-command "EXPUNGE"))))
1283 (nnimap-wait-for-response sequence))))
1285 (defun nnimap-parse-copied-articles (sequences)
1286 (let (sequence copied range)
1287 (goto-char (point-min))
1288 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1289 (setq sequence (string-to-number (match-string 1)))
1290 (when (setq range (cadr (assq sequence sequences)))
1291 (push (gnus-uncompress-range range) copied)))
1292 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1294 (defun nnimap-new-articles (flags)
1296 (dolist (elem flags)
1297 (when (or (null (cdr elem))
1298 (and (not (memq '%Deleted (cdr elem)))
1299 (not (memq '%Seen (cdr elem)))))
1300 (push (car elem) new)))
1301 (gnus-compress-sequence (nreverse new))))
1303 (defun nnimap-make-split-specs (list)
1307 (destructuring-bind (article spec) elem
1308 (dolist (group (delete nil (mapcar #'car spec)))
1309 (unless (setq entry (assoc group specs))
1310 (push (setq entry (list group)) specs))
1311 (setcdr entry (cons article (cdr entry))))))
1312 (dolist (entry specs)
1313 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1316 (defun nnimap-transform-split-mail ()
1317 (goto-char (point-min))
1318 (let (article bytes)
1321 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1322 (delete-region (point) (progn (forward-line 1) (point)))
1325 (setq article (match-string 1)
1326 bytes (nnimap-get-length))
1327 (delete-region (line-beginning-position) (line-end-position))
1328 ;; Insert MMDF separator, and a way to remember what this
1330 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1331 (forward-char (1+ bytes))
1332 (setq bytes (nnimap-get-length))
1333 (delete-region (line-beginning-position) (line-end-position))
1334 (forward-char (1+ bytes))
1335 (delete-region (line-beginning-position) (line-end-position))))))
1337 (defun nnimap-dummy-active-number (group &optional server)
1340 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1342 (goto-char (point-min))
1343 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1344 (error "Invalid nnimap mail")
1345 (setq article (string-to-number (match-string 1))))
1347 (if (eq group-art 'junk)
1348 (list (cons 'junk 1))
1350 nnimap-incoming-split-list)))
1354 ;;; nnimap.el ends here