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 (autoload 'auth-source-forget-user-or-password "auth-source")
45 (autoload 'auth-source-user-or-password "auth-source")
49 (defvoo nnimap-address nil
50 "The address of the IMAP server.")
52 (defvoo nnimap-server-port nil
54 If nnimap-stream is `ssl', this will default to `imaps'. If not,
55 it will default to `imap'.")
57 (defvoo nnimap-stream 'ssl
58 "How nnimap will talk to the IMAP server.
59 Values are `ssl', `network', `starttls' or `shell'.")
61 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
62 (if (listp imap-shell-program)
63 (car imap-shell-program)
67 (defvoo nnimap-inbox nil
68 "The mail box where incoming mail arrives and should be split out of.")
70 (defvoo nnimap-split-methods nil
72 Uses the same syntax as nnmail-split-methods")
74 (defvoo nnimap-split-fancy nil
75 "Uses the same syntax as nnmail-split-fancy.")
77 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
80 (defvoo nnimap-authenticator nil
81 "How nnimap authenticate itself to the server.
82 Possible choices are nil (use default methods) or `anonymous'.")
84 (defvoo nnimap-expunge t
85 "If non-nil, expunge articles after deleting them.
86 This is always done if the server supports UID EXPUNGE, but it's
87 not done by default on servers that doesn't support that command.")
89 (defvoo nnimap-streaming t
90 "If non-nil, try to use streaming commands with IMAP servers.
91 Switching this off will make nnimap slower, but it helps with
94 (defvoo nnimap-connection-alist nil)
96 (defvoo nnimap-current-infos nil)
98 (defvoo nnimap-fetch-partial-articles nil
99 "If non-nil, Gnus will fetch partial articles.
100 If t, nnimap will fetch only the first part. If a string, it
101 will fetch all parts that have types that match that string. A
102 likely value would be \"text/\" to automatically fetch all
105 (defvar nnimap-process nil)
107 (defvar nnimap-status-string "")
109 (defvar nnimap-split-download-body-default nil
110 "Internal variable with default value for `nnimap-split-download-body'.")
112 (defvar nnimap-keepalive-timer nil)
113 (defvar nnimap-process-buffers nil)
116 group process commands capabilities select-result newlinep server
117 last-command-time greeting)
119 (defvar nnimap-object nil)
121 (defvar nnimap-mark-alist
122 '((read "\\Seen" %Seen)
123 (tick "\\Flagged" %Flagged)
124 (reply "\\Answered" %Answered)
125 (expire "gnus-expire")
126 (dormant "gnus-dormant")
129 (download "gnus-download")
130 (forward "gnus-forward")))
132 (defun nnimap-buffer ()
133 (nnimap-find-process-buffer nntp-server-buffer))
135 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
136 (with-current-buffer nntp-server-buffer
138 (when (nnimap-possibly-change-group group server)
139 (with-current-buffer (nnimap-buffer)
141 (nnimap-wait-for-response
144 (nnimap-article-ranges (gnus-compress-sequence articles))
145 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
148 "BODY.PEEK[HEADER.FIELDS %s]"
149 "RFC822.HEADER.LINES %s")
150 (append '(Subject From Date Message-Id
151 References In-Reply-To Xref)
152 nnmail-extra-headers))))
154 (nnimap-transform-headers))
155 (insert-buffer-substring
156 (nnimap-find-process-buffer (current-buffer))))
159 (defun nnimap-transform-headers ()
160 (goto-char (point-min))
161 (let (article bytes lines size string)
164 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
165 (delete-region (point) (progn (forward-line 1) (point)))
168 (setq article (match-string 1))
169 ;; Unfold quoted {number} strings.
170 (while (re-search-forward "[^]] {\\([0-9]+\\)}\r\n"
171 (1+ (line-end-position)) t)
172 (setq size (string-to-number (match-string 1)))
173 (delete-region (+ (match-beginning 0) 2) (point))
174 (setq string (delete-region (point) (+ (point) size)))
175 (insert (format "%S" string)))
176 (setq bytes (nnimap-get-length)
180 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
185 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
186 (let ((structure (ignore-errors
187 (read (current-buffer)))))
188 (while (and (consp structure)
189 (not (stringp (car structure))))
190 (setq structure (car structure)))
191 (setq lines (nth 7 structure))))
192 (delete-region (line-beginning-position) (line-end-position))
193 (insert (format "211 %s Article retrieved." article))
196 (insert (format "Chars: %s\n" size)))
198 (insert (format "Lines: %s\n" lines)))
199 (re-search-forward "^\r$")
200 (delete-region (line-beginning-position) (line-end-position))
204 (defun nnimap-get-length ()
205 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
206 (string-to-number (match-string 1))))
208 (defun nnimap-article-ranges (ranges)
212 (number-to-string ranges))
213 ((numberp (cdr ranges))
214 (format "%d:%d" (car ranges) (cdr ranges)))
216 (dolist (elem ranges)
219 (format "%d:%d" (car elem) (cdr elem))
220 (number-to-string elem))
222 (mapconcat #'identity (nreverse result) ",")))))
224 (deffoo nnimap-open-server (server &optional defs)
225 (if (nnimap-server-opened server)
227 (unless (assq 'nnimap-address defs)
228 (setq defs (append defs (list (list 'nnimap-address server)))))
229 (nnoo-change-server 'nnimap server defs)
230 (or (nnimap-find-connection nntp-server-buffer)
231 (nnimap-open-connection nntp-server-buffer))))
233 (defun nnimap-make-process-buffer (buffer)
235 (generate-new-buffer (format "*nnimap %s %s %s*"
236 nnimap-address nnimap-server-port
237 (gnus-buffer-exists-p buffer)))
238 (mm-disable-multibyte)
239 (buffer-disable-undo)
241 (set (make-local-variable 'after-change-functions) nil)
242 (set (make-local-variable 'nnimap-object)
243 (make-nnimap :server (nnoo-current-server 'nnimap)))
244 (push (list buffer (current-buffer)) nnimap-connection-alist)
245 (push (current-buffer) nnimap-process-buffers)
248 (defun nnimap-open-shell-stream (name buffer host port)
249 (let ((process-connection-type nil))
250 (start-process name buffer shell-file-name
258 (defun nnimap-credentials (address ports &optional inhibit-create)
259 (let (port credentials)
260 ;; Request the credentials from all ports, but only query on the
261 ;; last port if all the previous ones have failed.
262 (while (and (null credentials)
263 (setq port (pop ports)))
265 (auth-source-user-or-password
266 '("login" "password") address port nil
272 (defun nnimap-keepalive ()
273 (let ((now (current-time)))
274 (dolist (buffer nnimap-process-buffers)
275 (when (buffer-name buffer)
276 (with-current-buffer buffer
277 (when (and nnimap-object
278 (nnimap-last-command-time nnimap-object)
282 (nnimap-last-command-time nnimap-object)))
283 ;; More than five minutes since the last command.
285 (nnimap-send-command "NOOP")))))))
287 (defun nnimap-open-connection (buffer)
288 (unless nnimap-keepalive-timer
289 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
292 (with-current-buffer (nnimap-make-process-buffer buffer)
293 (let* ((coding-system-for-read 'binary)
294 (coding-system-for-write 'binary)
298 ((eq nnimap-stream 'network)
300 "*nnimap*" (current-buffer) nnimap-address
302 (or nnimap-server-port
303 (if (netrc-find-service-number "imap")
307 ((eq nnimap-stream 'shell)
308 (nnimap-open-shell-stream
309 "*nnimap*" (current-buffer) nnimap-address
310 (setq port (or nnimap-server-port "imap")))
312 ((eq nnimap-stream 'starttls)
313 (let ((tls-program (nnimap-extend-tls-programs)))
315 "*nnimap*" (current-buffer) nnimap-address
316 (setq port (or nnimap-server-port "imap"))
319 ((memq nnimap-stream '(ssl tls))
320 (funcall (if (and nil
321 (fboundp 'open-gnutls-stream))
324 "*nnimap*" (current-buffer) nnimap-address
326 (or nnimap-server-port
327 (if (netrc-find-service-number "imaps")
330 '("143" "993" "imap" "imaps"))
332 (error "Unknown stream type: %s" nnimap-stream))))
333 connection-result login-result credentials)
334 (setf (nnimap-process nnimap-object)
335 (get-buffer-process (current-buffer)))
336 (if (not (and (nnimap-process nnimap-object)
337 (memq (process-status (nnimap-process nnimap-object))
339 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
340 nnimap-address port nnimap-stream)
341 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
342 (if (not (setq connection-result (nnimap-wait-for-connection)))
343 (nnheader-report 'nnimap
344 "%s" (buffer-substring
345 (point) (line-end-position)))
346 ;; Store the greeting (for debugging purposes).
347 (setf (nnimap-greeting nnimap-object)
348 (buffer-substring (line-beginning-position)
349 (line-end-position)))
350 ;; Store the capabilities.
351 (setf (nnimap-capabilities nnimap-object)
354 (nnimap-find-parameter
355 "CAPABILITY" (cdr (nnimap-command "CAPABILITY")))))
356 (when nnimap-server-port
357 (push (format "%s" nnimap-server-port) ports))
358 ;; If this is a STARTTLS-capable server, then sever the
359 ;; connection and start a STARTTLS connection instead.
360 (when (and (eq nnimap-stream 'network)
361 (member "STARTTLS" (nnimap-capabilities nnimap-object)))
362 (let ((nnimap-stream 'starttls))
364 (nnimap-open-connection buffer)))
365 ;; If the STARTTLS connection was successful, we
366 ;; kill our first non-encrypted connection. If it
367 ;; wasn't successful, we just use our unencrypted
369 (when (memq (process-status tls-process) '(open run))
370 (delete-process (nnimap-process nnimap-object))
371 (kill-buffer (current-buffer))
372 (return tls-process)))))
373 (unless (equal connection-result "PREAUTH")
374 (if (not (setq credentials
375 (if (eq nnimap-authenticator 'anonymous)
377 (message-make-address))
379 ;; First look for the credentials based
380 ;; on the virtual server name.
382 (nnoo-current-server 'nnimap) ports t)
383 ;; Then look them up based on the
385 (nnimap-credentials nnimap-address ports)))))
386 (setq nnimap-object nil)
387 (setq login-result (nnimap-command "LOGIN %S %S"
390 (unless (car login-result)
391 ;; If the login failed, then forget the credentials
392 ;; that are now possibly cached.
393 (dolist (host (list (nnoo-current-server 'nnimap)
396 (dolist (element '("login" "password"))
397 (auth-source-forget-user-or-password
398 element host port))))
399 (delete-process (nnimap-process nnimap-object))
400 (setq nnimap-object nil))))
402 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
403 (nnimap-command "ENABLE QRESYNC"))
404 (nnimap-process nnimap-object))))))))
406 (defun nnimap-extend-tls-programs ()
407 (let ((programs tls-program)
409 (unless (consp programs)
410 (setq programs (list programs)))
411 (dolist (program programs)
412 (when (assoc (car (split-string program)) tls-starttls-switches)
413 (push (if (not (string-match "%s" program))
414 (concat program " " "%s")
419 (defun nnimap-find-parameter (parameter elems)
423 ((equal (car elem) parameter)
424 (setq result (cdr elem)))
425 ((and (equal (car elem) "OK")
427 (equal (caadr elem) parameter))
428 (setq result (cdr (cadr elem))))))
431 (deffoo nnimap-close-server (&optional server)
432 (when (nnoo-change-server 'nnimap server nil)
434 (delete-process (get-buffer-process (nnimap-buffer))))
437 (deffoo nnimap-request-close ()
440 (deffoo nnimap-server-opened (&optional server)
441 (and (nnoo-current-server-p 'nnimap server)
443 (gnus-buffer-live-p nntp-server-buffer)
444 (nnimap-find-connection nntp-server-buffer)))
446 (deffoo nnimap-status-message (&optional server)
447 nnimap-status-string)
449 (deffoo nnimap-request-article (article &optional group server to-buffer)
450 (with-current-buffer nntp-server-buffer
451 (let ((result (nnimap-possibly-change-group group server))
453 (when (stringp article)
454 (setq article (nnimap-find-article-by-message-id group article)))
458 (with-current-buffer (nnimap-buffer)
460 (when nnimap-fetch-partial-articles
461 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
462 (goto-char (point-min))
463 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
464 (setq structure (ignore-errors
465 (let ((start (point)))
467 (downcase-region start (point))
469 (read (current-buffer))))
470 parts (nnimap-find-wanted-parts structure))))
472 (nnimap-get-partial-article article parts structure)
473 (nnimap-get-whole-article article))
474 (let ((buffer (current-buffer)))
475 (with-current-buffer (or to-buffer nntp-server-buffer)
477 (insert-buffer-substring buffer)
478 (nnheader-ms-strip-cr)
479 (cons group article)))))))))
481 (defun nnimap-get-whole-article (article)
485 "UID FETCH %d BODY.PEEK[]"
486 "UID FETCH %d RFC822.PEEK")
488 ;; Check that we really got an article.
489 (goto-char (point-min))
490 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
493 ;; Remove any data that may have arrived before the FETCH data.
496 (delete-region (point-min) (point)))
497 (let ((bytes (nnimap-get-length)))
498 (delete-region (line-beginning-position)
499 (progn (forward-line 1) (point)))
500 (goto-char (+ (point) bytes))
501 (delete-region (point) (point-max)))
504 (defun nnimap-ver4-p ()
505 (member "IMAP4REV1" (nnimap-capabilities nnimap-object)))
507 (defun nnimap-get-partial-article (article parts structure)
510 "UID FETCH %d (%s %s)"
516 (mapconcat (lambda (part)
517 (format "BODY.PEEK[%s]" part))
519 (mapconcat (lambda (part)
520 (format "RFC822.PEEK[%s]" part))
523 (nnimap-convert-partial-article structure))))
525 (defun nnimap-convert-partial-article (structure)
526 ;; First just skip past the headers.
527 (goto-char (point-min))
528 (let ((bytes (nnimap-get-length))
530 ;; Delete "FETCH" line.
531 (delete-region (line-beginning-position)
532 (progn (forward-line 1) (point)))
533 (goto-char (+ (point) bytes))
534 ;; Collect all the body parts.
535 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
536 (setq id (match-string 1)
537 bytes (nnimap-get-length))
539 (delete-region (point) (progn (forward-line 1) (point)))
540 (push (list id (buffer-substring (point) (+ (point) bytes)))
542 (delete-region (point) (+ (point) bytes)))
543 ;; Delete trailing junk.
544 (delete-region (point) (point-max))
545 ;; Now insert all the parts again where they fit in the structure.
546 (nnimap-insert-partial-structure structure parts)
549 (defun nnimap-insert-partial-structure (structure parts &optional subp)
551 (let ((bstruc structure))
552 (while (consp (car bstruc))
554 (setq type (car bstruc))
555 (setq bstruc (car (cdr bstruc)))
556 (when (and (stringp (car bstruc))
557 (string= (downcase (car bstruc)) "boundary"))
558 (setq boundary (cadr bstruc))))
560 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
561 (downcase type) boundary)))
562 (while (not (stringp (car structure)))
563 (insert "\n--" boundary "\n")
564 (if (consp (caar structure))
565 (nnimap-insert-partial-structure (pop structure) parts t)
566 (let ((bit (pop structure)))
567 (insert (format "Content-type: %s/%s"
568 (downcase (nth 0 bit))
569 (downcase (nth 1 bit))))
570 (if (member "CHARSET" (nth 2 bit))
572 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
574 (insert (format "Content-transfer-encoding: %s\n"
577 (when (assoc (nth 9 bit) parts)
578 (insert (cadr (assoc (nth 9 bit) parts)))))))
579 (insert "\n--" boundary "--\n")))
581 (defun nnimap-find-wanted-parts (structure)
582 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
584 (defun nnimap-find-wanted-parts-1 (structure prefix)
587 (while (consp (car structure))
588 (let ((sub (pop structure)))
589 (if (consp (car sub))
590 (push (nnimap-find-wanted-parts-1
591 sub (if (string= prefix "")
592 (number-to-string num)
593 (format "%s.%s" prefix num)))
595 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
596 (id (if (string= prefix "")
597 (number-to-string num)
598 (format "%s.%s" prefix num))))
599 (setcar (nthcdr 9 sub) id)
600 (when (if (eq nnimap-fetch-partial-articles t)
602 (string-match nnimap-fetch-partial-articles type))
607 (deffoo nnimap-request-group (group &optional server dont-check info)
608 (let ((result (nnimap-possibly-change-group group server))
609 articles active marks high low)
610 (with-current-buffer nntp-server-buffer
613 (setq active (nth 2 (assoc group nnimap-current-infos))))
614 (insert (format "211 %d %d %d %S\n"
615 (- (cdr active) (car active))
619 (with-current-buffer (nnimap-buffer)
621 (let ((group-sequence
622 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
624 (nnimap-send-command "UID FETCH 1:* FLAGS")))
625 (nnimap-wait-for-response flag-sequence)
627 (nnimap-flags-to-marks
629 (list (list group-sequence flag-sequence 1 group)))))
631 (nnimap-update-infos marks (list info)))
632 (goto-char (point-max))
633 (let ((uidnext (nth 5 (car marks))))
634 (setq high (or (if uidnext
638 low (or (nth 4 (car marks)) uidnext 1)))))
642 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
645 (deffoo nnimap-request-create-group (group &optional server args)
646 (when (nnimap-possibly-change-group nil server)
647 (with-current-buffer (nnimap-buffer)
648 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
650 (deffoo nnimap-request-delete-group (group &optional force server)
651 (when (nnimap-possibly-change-group nil server)
652 (with-current-buffer (nnimap-buffer)
653 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
655 (deffoo nnimap-request-expunge-group (group &optional server)
656 (when (nnimap-possibly-change-group group server)
657 (with-current-buffer (nnimap-buffer)
658 (car (nnimap-command "EXPUNGE")))))
660 (defun nnimap-get-flags (spec)
663 (with-current-buffer (nnimap-buffer)
665 (nnimap-wait-for-response (nnimap-send-command
666 "UID FETCH %s FLAGS" spec))
667 (goto-char (point-min))
668 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
669 (setq elems (nnimap-parse-line (match-string 1)))
670 (push (cons (string-to-number (cadr (member "UID" elems)))
671 (cadr (member "FLAGS" elems)))
673 (nreverse articles)))
675 (deffoo nnimap-close-group (group &optional server)
678 (deffoo nnimap-request-move-article (article group server accept-form
679 &optional last internal-move-group)
681 (when (nnimap-request-article article group server (current-buffer))
682 ;; If the move is internal (on the same server), just do it the easy
684 (let ((message-id (message-field-value "message-id")))
685 (if internal-move-group
687 (with-current-buffer (nnimap-buffer)
688 (nnimap-command "UID COPY %d %S"
690 (utf7-encode internal-move-group t)))))
692 (nnimap-delete-article article)
693 (cons internal-move-group
694 (nnimap-find-article-by-message-id
695 internal-move-group message-id))))
696 ;; Move the article to a different method.
697 (let ((result (eval accept-form)))
699 (nnimap-delete-article article)
702 (deffoo nnimap-request-expire-articles (articles group &optional server force)
706 ((not (nnimap-possibly-change-group group server))
709 (eq nnmail-expiry-target 'delete))
710 (unless (nnimap-delete-article (gnus-compress-sequence articles))
711 (message "Article marked for deletion, but not expunged."))
714 (let ((deletable-articles
716 (eq nnmail-expiry-wait 'immediate))
718 (gnus-sorted-intersection
720 (nnimap-find-expired-articles group)))))
721 (if (null deletable-articles)
723 (if (eq nnmail-expiry-target 'delete)
724 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
725 (setq deletable-articles
726 (nnimap-process-expiry-targets
727 deletable-articles group server)))
728 ;; Return the articles we didn't delete.
729 (gnus-sorted-complement articles deletable-articles))))))
731 (defun nnimap-process-expiry-targets (articles group server)
732 (let ((deleted-articles nil))
733 (dolist (article articles)
734 (let ((target nnmail-expiry-target))
736 (when (nnimap-request-article article group server (current-buffer))
737 (message "Expiring article %s:%d" group article)
738 (when (functionp target)
739 (setq target (funcall target group)))
741 (not (eq target 'delete)))
742 (if (or (gnus-request-group target t)
743 (gnus-request-create-group target))
744 (nnmail-expiry-target-group target group)
747 (push article deleted-articles))))))
748 ;; Change back to the current group again.
749 (nnimap-possibly-change-group group server)
750 (setq deleted-articles (nreverse deleted-articles))
751 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
754 (defun nnimap-find-expired-articles (group)
755 (let ((cutoff (nnmail-expired-article-p group nil nil)))
756 (with-current-buffer (nnimap-buffer)
759 "UID SEARCH SENTBEFORE %s"
763 (car (rassoc (nth 4 (decode-time cutoff))
764 parse-time-months))))
767 (delete 0 (mapcar #'string-to-number
768 (cdr (assoc "SEARCH" (cdr result))))))))))
771 (defun nnimap-find-article-by-message-id (group message-id)
772 (with-current-buffer (nnimap-buffer)
774 (setf (nnimap-group nnimap-object) nil)
775 (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
777 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
779 (setq result (nnimap-wait-for-response sequence))
781 (car (setq result (nnimap-parse-response))))
782 ;; Select the last instance of the message in the group.
784 (car (last (assoc "SEARCH" (cdr result)))))
785 (string-to-number article))))))
787 (defun nnimap-delete-article (articles)
788 (with-current-buffer (nnimap-buffer)
789 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
790 (nnimap-article-ranges articles))
792 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
793 (nnimap-command "UID EXPUNGE %s"
794 (nnimap-article-ranges articles))
797 (nnimap-command "EXPUNGE")
799 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
800 "server doesn't support UIDPLUS, so we won't "
801 "delete this article now"))))))
803 (deffoo nnimap-request-scan (&optional group server)
804 (when (and (nnimap-possibly-change-group nil server)
806 nnimap-split-methods)
807 (message "nnimap %s splitting mail..." server)
808 (nnimap-split-incoming-mail)))
810 (defun nnimap-marks-to-flags (marks)
813 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
817 (deffoo nnimap-request-set-mark (group actions &optional server)
818 (when (nnimap-possibly-change-group group server)
820 (with-current-buffer (nnimap-buffer)
822 ;; Just send all the STORE commands without waiting for
823 ;; response. If they're successful, they're successful.
824 (dolist (action actions)
825 (destructuring-bind (range action marks) action
826 (let ((flags (nnimap-marks-to-flags marks)))
828 (setq sequence (nnimap-send-command
829 "UID STORE %s %sFLAGS.SILENT (%s)"
830 (nnimap-article-ranges range)
834 (mapconcat #'identity flags " ")))))))
835 ;; Wait for the last command to complete to avoid later
836 ;; syncronisation problems with the stream.
838 (nnimap-wait-for-response sequence))))))
840 (deffoo nnimap-request-accept-article (group &optional server last)
841 (when (nnimap-possibly-change-group nil server)
842 (nnmail-check-syntax)
843 (let ((message-id (message-field-value "message-id"))
846 (setq message (buffer-string))
847 (with-current-buffer (nnimap-buffer)
848 (setq sequence (nnimap-send-command
849 "APPEND %S {%d}" (utf7-encode group t)
851 (process-send-string (get-buffer-process (current-buffer)) message)
852 (process-send-string (get-buffer-process (current-buffer))
853 (if (nnimap-newlinep nnimap-object)
856 (let ((result (nnimap-get-response sequence)))
857 (if (not (car result))
859 (message "%s" (nnheader-get-report-string 'nnimap))
862 (nnimap-find-article-by-message-id group message-id))))))))
864 (defun nnimap-add-cr ()
865 (goto-char (point-min))
866 (while (re-search-forward "\r?\n" nil t)
867 (replace-match "\r\n" t t)))
869 (defun nnimap-get-groups ()
870 (let ((result (nnimap-command "LIST \"\" \"*\""))
873 (dolist (line (cdr result))
874 (when (and (equal (car line) "LIST")
875 (not (and (caadr line)
876 (string-match "noselect" (caadr line)))))
877 (push (car (last line)) groups)))
880 (deffoo nnimap-request-list (&optional server)
881 (nnimap-possibly-change-group nil server)
882 (with-current-buffer nntp-server-buffer
885 (with-current-buffer (nnimap-buffer)
886 (nnimap-get-groups)))
889 (with-current-buffer (nnimap-buffer)
890 (setf (nnimap-group nnimap-object) nil)
891 (dolist (group groups)
892 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
895 (nnimap-wait-for-response (caar sequences))
897 (nnimap-get-responses (mapcar #'car sequences))))
898 (dolist (response responses)
899 (let* ((sequence (car response))
900 (response (cadr response))
901 (group (cadr (assoc sequence sequences))))
903 (equal (caar response) "OK"))
904 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
906 (dolist (elem response)
907 (when (equal (cadr elem) "EXISTS")
908 (setq exists (string-to-number (car elem)))))
910 (setq highest (1- (string-to-number (car uidnext)))))
913 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
916 (insert (format "%S %d %d y\n"
917 (utf7-decode group t) highest (1+ highest))))
919 ;; Return the widest possible range.
920 (insert (format "%S %d 1 y\n" (utf7-decode group t)
921 (or highest exists)))))))))
924 (deffoo nnimap-request-newgroups (date &optional server)
925 (nnimap-possibly-change-group nil server)
926 (with-current-buffer nntp-server-buffer
928 (dolist (group (with-current-buffer (nnimap-buffer)
929 (nnimap-get-groups)))
930 (unless (assoc group nnimap-current-infos)
931 ;; Insert dummy numbers here -- they don't matter.
932 (insert (format "%S 0 1 y\n" group))))
935 (deffoo nnimap-retrieve-group-data-early (server infos)
936 (when (nnimap-possibly-change-group nil server)
937 (with-current-buffer (nnimap-buffer)
938 ;; QRESYNC handling isn't implemented.
939 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
940 marks groups sequences)
941 ;; Go through the infos and gather the data needed to know
942 ;; what and how to request the data.
944 (setq marks (gnus-info-marks info))
945 (push (list (gnus-group-real-name (gnus-info-group info))
946 (cdr (assq 'active marks))
947 (cdr (assq 'uid marks)))
949 ;; Then request the data.
951 (setf (nnimap-group nnimap-object) nil)
952 (dolist (elem groups)
957 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
966 ;; Fetch the last 100 flags.
967 (max 1 (- (cdr (nth 1 elem)) 100))
969 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
970 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
974 ;; Some servers apparently can't have many outstanding
975 ;; commands, so throttle them.
976 (when (and (not nnimap-streaming)
978 (nnimap-wait-for-response (caar sequences))))
981 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
983 (nnimap-possibly-change-group nil server))
984 (with-current-buffer (nnimap-buffer)
985 ;; Wait for the final data to trickle in.
986 (when (nnimap-wait-for-response (cadar sequences) t)
987 ;; Now we should have all the data we need, no matter whether
988 ;; we're QRESYNCING, fetching all the flags from scratch, or
989 ;; just fetching the last 100 flags per group.
990 (nnimap-update-infos (nnimap-flags-to-marks
992 (nreverse sequences)))
994 ;; Finally, just return something resembling an active file in
995 ;; the nntp buffer, so that the agent can save the info, too.
996 (with-current-buffer nntp-server-buffer
999 (let* ((group (gnus-info-group info))
1000 (active (gnus-active group)))
1002 (insert (format "%S %d %d y\n"
1003 (gnus-group-real-name group)
1005 (car active)))))))))))
1007 (defun nnimap-update-infos (flags infos)
1008 (dolist (info infos)
1009 (let ((group (gnus-group-real-name (gnus-info-group info))))
1010 (nnimap-update-info info (cdr (assoc group flags))))))
1012 (defun nnimap-update-info (info marks)
1014 ;; Ignore groups with no UIDNEXT values.
1016 (destructuring-bind (existing flags high low uidnext start-article
1017 permanent-flags) marks
1018 (let ((group (gnus-info-group info))
1019 (completep (and start-article
1020 (= start-article 1))))
1022 (setq high (1- uidnext)))
1023 ;; First set the active ranges based on high/low.
1025 (not (gnus-active group)))
1026 (gnus-set-active group
1031 ;; No articles in this group.
1032 (cons uidnext (1- uidnext)))
1034 (cons start-article (1- start-article)))
1036 ;; No articles and no uidnext.
1040 (cons (car (gnus-active group))
1041 (or high (1- uidnext)))))
1042 (when (and (not high)
1044 (setq high (1- uidnext)))
1045 ;; Then update the list of read articles.
1047 (gnus-compress-sequence
1048 (gnus-set-difference
1049 (gnus-set-difference
1051 (cdr (assoc '%Seen flags)))
1052 (cdr (assoc '%Flagged flags)))))
1053 (read (gnus-range-difference
1054 (cons start-article high) unread)))
1055 (when (> start-article 1)
1058 (if (> start-article 1)
1059 (gnus-sorted-range-intersection
1060 (cons 1 (1- start-article))
1061 (gnus-info-read info))
1062 (gnus-info-read info))
1064 (gnus-info-set-read info read)
1065 ;; Update the marks.
1066 (setq marks (gnus-info-marks info))
1067 ;; Note the active level for the next run-through.
1068 (let ((active (assq 'active marks)))
1070 (setcdr active (gnus-active group))
1071 (push (cons 'active (gnus-active group)) marks)))
1072 (dolist (type (cdr nnimap-mark-alist))
1073 (let ((old-marks (assoc (car type) marks))
1075 (gnus-compress-sequence
1076 (cdr (or (assoc (caddr type) flags) ; %Flagged
1077 (assoc (intern (cadr type) obarray) flags)
1078 (assoc (cadr type) flags)))))) ; "\Flagged"
1079 (setq marks (delq old-marks marks))
1081 (when (and old-marks
1082 (> start-article 1))
1083 (setq old-marks (gnus-range-difference
1085 (cons start-article high)))
1086 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1088 (push (cons (car type) new-marks) marks)))
1089 (gnus-info-set-marks info marks t)
1090 (nnimap-store-info info (gnus-active group))))))))
1092 (defun nnimap-store-info (info active)
1093 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1094 (entry (assoc group nnimap-current-infos)))
1096 (setcdr entry (list info active))
1097 (push (list group info active) nnimap-current-infos))))
1099 (defun nnimap-flags-to-marks (groups)
1100 (let (data group totalp uidnext articles start-article mark permanent-flags)
1101 (dolist (elem groups)
1102 (setq group (car elem)
1103 uidnext (nth 1 elem)
1104 start-article (nth 2 elem)
1105 permanent-flags (nth 3 elem)
1106 articles (nthcdr 4 elem))
1107 (let ((high (caar articles))
1109 (dolist (article articles)
1110 (setq low (car article))
1111 (push (car article) existing)
1112 (dolist (flag (cdr article))
1113 (setq mark (assoc flag marks))
1115 (push (list flag (car article)) marks)
1116 (setcdr mark (cons (car article) (cdr mark))))))
1117 (push (list group existing marks high low uidnext start-article
1122 (defun nnimap-parse-flags (sequences)
1123 (goto-char (point-min))
1124 ;; Change \Delete etc to %Delete, so that the reader can read it.
1125 (subst-char-in-region (point-min) (point-max)
1127 (let (start end articles groups uidnext elems permanent-flags)
1128 (dolist (elem sequences)
1129 (destructuring-bind (group-sequence flag-sequence totalp group) elem
1130 (setq start (point))
1131 ;; The EXAMINE was successful.
1132 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
1137 (setq permanent-flags
1138 (and (search-forward "PERMANENTFLAGS "
1139 (or end (point-min)) t)
1140 (read (current-buffer))))
1143 (and (search-forward "UIDNEXT "
1144 (or end (point-min)) t)
1145 (read (current-buffer))))
1148 ;; The UID FETCH FLAGS was successful.
1149 (search-forward (format "\n%d OK " flag-sequence) nil t))
1150 (setq start (point))
1152 (while (search-forward " FETCH " start t)
1153 (setq elems (read (current-buffer)))
1154 (push (cons (cadr (memq 'UID elems))
1155 (cadr (memq 'FLAGS elems)))
1157 (push (nconc (list group uidnext totalp permanent-flags) articles)
1159 (setq articles nil))))
1162 (defun nnimap-find-process-buffer (buffer)
1163 (cadr (assoc buffer nnimap-connection-alist)))
1165 (deffoo nnimap-request-post (&optional server)
1166 (setq nnimap-status-string "Read-only server")
1169 (defun nnimap-possibly-change-group (group server)
1170 (let ((open-result t))
1172 (not (nnimap-server-opened server)))
1173 (setq open-result (nnimap-open-server server)))
1180 (with-current-buffer (nnimap-buffer)
1181 (if (equal group (nnimap-group nnimap-object))
1183 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1185 (setf (nnimap-group nnimap-object) group
1186 (nnimap-select-result nnimap-object) result)
1189 (defun nnimap-find-connection (buffer)
1190 "Find the connection delivering to BUFFER."
1191 (let ((entry (assoc buffer nnimap-connection-alist)))
1193 (if (and (buffer-name (cadr entry))
1194 (get-buffer-process (cadr entry))
1195 (memq (process-status (get-buffer-process (cadr entry)))
1197 (get-buffer-process (cadr entry))
1198 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1201 (defvar nnimap-sequence 0)
1203 (defun nnimap-send-command (&rest args)
1204 (process-send-string
1205 (get-buffer-process (current-buffer))
1208 (incf nnimap-sequence)
1209 (apply #'format args)
1210 (if (nnimap-newlinep nnimap-object)
1215 (defun nnimap-log-command (command)
1216 (with-current-buffer (get-buffer-create "*imap log*")
1217 (goto-char (point-max))
1218 (insert (format-time-string "%H:%M:%S") " " command))
1221 (defun nnimap-command (&rest args)
1223 (setf (nnimap-last-command-time nnimap-object) (current-time))
1224 (let* ((sequence (apply #'nnimap-send-command args))
1225 (response (nnimap-get-response sequence)))
1226 (if (equal (caar response) "OK")
1228 (nnheader-report 'nnimap "%s"
1229 (mapconcat (lambda (a)
1231 (car response) " "))
1234 (defun nnimap-get-response (sequence)
1235 (nnimap-wait-for-response sequence)
1236 (nnimap-parse-response))
1238 (defun nnimap-wait-for-connection ()
1239 (let ((process (get-buffer-process (current-buffer))))
1240 (goto-char (point-min))
1241 (while (and (memq (process-status process)
1243 (not (re-search-forward "^[*.] .*\n" nil t)))
1244 (nnheader-accept-process-output process)
1245 (goto-char (point-min)))
1247 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1250 (defun nnimap-wait-for-response (sequence &optional messagep)
1251 (let ((process (get-buffer-process (current-buffer)))
1253 (goto-char (point-max))
1254 (while (and (setq openp (memq (process-status process)
1256 (not (re-search-backward
1257 (format "^%d .*\n" sequence)
1258 (if nnimap-streaming
1259 (max (point-min) (- (point) 500))
1263 (message "nnimap read %dk" (/ (buffer-size) 1000)))
1264 (nnheader-accept-process-output process)
1265 (goto-char (point-max)))
1268 (defun nnimap-parse-response ()
1269 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1271 (dolist (line lines)
1272 (push (cdr (nnimap-parse-line line)) result))
1273 ;; Return the OK/error code first, and then all the "continuation
1274 ;; lines" afterwards.
1276 (nreverse result))))
1278 ;; Parse an IMAP response line lightly. They look like
1279 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1280 ;; the lines into a list of strings and lists of string.
1281 (defun nnimap-parse-line (line)
1285 (goto-char (point-min))
1287 (if (eql (setq char (following-char)) ? )
1292 (split-string (buffer-substring
1294 (1- (search-forward "]" (line-end-position) 'move)))))
1296 (split-string (buffer-substring
1298 (1- (search-forward ")" (line-end-position) 'move)))))
1303 (1- (or (search-forward "\"" (line-end-position) 'move)
1306 (buffer-substring (point) (if (search-forward " " nil t)
1308 (goto-char (point-max))))))
1310 (nreverse result))))
1312 (defun nnimap-last-response-string ()
1315 (let ((end (point)))
1319 (while (and (not (bobp))
1320 (eql (following-char) ?*))
1322 (unless (eql (following-char) ?*)
1324 (buffer-substring (point) end))))
1326 (defun nnimap-get-responses (sequences)
1328 (dolist (sequence sequences)
1329 (goto-char (point-min))
1330 (when (re-search-forward (format "^%d " sequence) nil t)
1331 (push (list sequence (nnimap-parse-response))
1335 (defvar nnimap-incoming-split-list nil)
1337 (defun nnimap-fetch-inbox (articles)
1339 (nnimap-wait-for-response
1340 (nnimap-send-command
1342 (nnimap-article-ranges articles)
1343 (format "(UID %s%s)"
1346 "BODY.PEEK[HEADER] BODY.PEEK"
1348 (if nnimap-split-download-body-default
1353 (defun nnimap-split-incoming-mail ()
1354 (with-current-buffer (nnimap-buffer)
1355 (let ((nnimap-incoming-split-list nil)
1356 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1357 nnmail-split-methods
1358 nnimap-split-methods))
1359 (nnmail-split-fancy (or nnimap-split-fancy
1360 nnmail-split-fancy))
1361 (nnmail-inhibit-default-split-group t)
1362 (groups (nnimap-get-groups))
1365 (nnimap-command "SELECT %S" nnimap-inbox)
1366 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1368 (nnimap-fetch-inbox new-articles)
1369 (nnimap-transform-split-mail)
1370 (nnheader-ms-strip-cr)
1372 (nnmail-split-incoming (current-buffer)
1373 #'nnimap-save-mail-spec
1375 #'nnimap-dummy-active-number
1376 #'nnimap-save-mail-spec)
1377 (when nnimap-incoming-split-list
1378 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1379 sequences junk-articles)
1380 ;; Create any groups that doesn't already exist on the
1382 (dolist (spec specs)
1383 (when (and (not (member (car spec) groups))
1384 (not (eq (car spec) 'junk)))
1385 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1386 ;; Then copy over all the messages.
1388 (dolist (spec specs)
1389 (let ((group (car spec))
1390 (ranges (cdr spec)))
1391 (if (eq group 'junk)
1392 (setq junk-articles ranges)
1393 (push (list (nnimap-send-command
1395 (nnimap-article-ranges ranges)
1396 (utf7-encode group t))
1399 ;; Wait for the last COPY response...
1401 (nnimap-wait-for-response (caar sequences))
1402 ;; And then mark the successful copy actions as deleted,
1403 ;; and possibly expunge them.
1404 (nnimap-mark-and-expunge-incoming
1405 (nnimap-parse-copied-articles sequences)))
1406 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1408 (defun nnimap-mark-and-expunge-incoming (range)
1410 (setq range (nnimap-article-ranges range))
1413 (nnimap-send-command
1414 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1416 ;; If the server supports it, we now delete the message we have
1417 ;; just copied over.
1418 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1419 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1420 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1421 ;; user has configured it.
1423 (setq sequence (nnimap-send-command "EXPUNGE"))))
1424 (nnimap-wait-for-response sequence))))
1426 (defun nnimap-parse-copied-articles (sequences)
1427 (let (sequence copied range)
1428 (goto-char (point-min))
1429 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1430 (setq sequence (string-to-number (match-string 1)))
1431 (when (setq range (cadr (assq sequence sequences)))
1432 (push (gnus-uncompress-range range) copied)))
1433 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1435 (defun nnimap-new-articles (flags)
1437 (dolist (elem flags)
1438 (when (or (null (cdr elem))
1439 (and (not (memq '%Deleted (cdr elem)))
1440 (not (memq '%Seen (cdr elem)))))
1441 (push (car elem) new)))
1442 (gnus-compress-sequence (nreverse new))))
1444 (defun nnimap-make-split-specs (list)
1448 (destructuring-bind (article spec) elem
1449 (dolist (group (delete nil (mapcar #'car spec)))
1450 (unless (setq entry (assoc group specs))
1451 (push (setq entry (list group)) specs))
1452 (setcdr entry (cons article (cdr entry))))))
1453 (dolist (entry specs)
1454 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1457 (defun nnimap-transform-split-mail ()
1458 (goto-char (point-min))
1459 (let (article bytes)
1462 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1463 (delete-region (point) (progn (forward-line 1) (point)))
1466 (setq article (match-string 1)
1467 bytes (nnimap-get-length))
1468 (delete-region (line-beginning-position) (line-end-position))
1469 ;; Insert MMDF separator, and a way to remember what this
1471 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1472 (forward-char (1+ bytes))
1473 (setq bytes (nnimap-get-length))
1474 (delete-region (line-beginning-position) (line-end-position))
1475 (forward-char (1+ bytes))
1476 (delete-region (line-beginning-position) (line-end-position))))))
1478 (defun nnimap-dummy-active-number (group &optional server)
1481 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1483 (goto-char (point-min))
1484 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1485 (error "Invalid nnimap mail")
1486 (setq article (string-to-number (match-string 1))))
1488 (if (eq group-art 'junk)
1489 (list (cons 'junk 1))
1491 nnimap-incoming-split-list)))
1495 ;;; nnimap.el ends here