1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
48 (require 'proto-stream)
50 (autoload 'auth-source-forget+ "auth-source")
51 (autoload 'auth-source-search "auth-source")
55 (defvoo nnimap-address nil
56 "The address of the IMAP server.")
58 (defvoo nnimap-server-port nil
60 If nnimap-stream is `ssl', this will default to `imaps'. If not,
61 it will default to `imap'.")
63 (defvoo nnimap-stream 'undecided
64 "How nnimap will talk to the IMAP server.
65 Values are `ssl', `network', `starttls' or `shell'.
66 The default is to try `ssl' first, and then `network'.")
68 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
69 (if (listp imap-shell-program)
70 (car imap-shell-program)
74 (defvoo nnimap-inbox nil
75 "The mail box where incoming mail arrives and should be split out of.
76 For example, \"INBOX\".")
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 stream-type)
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 (defvar nnimap-quirks
144 '(("QRESYNC" "Zimbra" "QRESYNC ")))
146 (defvar nnimap-inhibit-logging nil)
148 (defun nnimap-buffer ()
149 (nnimap-find-process-buffer nntp-server-buffer))
151 (defun nnimap-header-parameters ()
152 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
155 "BODY.PEEK[HEADER.FIELDS %s]"
156 "RFC822.HEADER.LINES %s")
157 (append '(Subject From Date Message-Id
158 References In-Reply-To Xref)
159 nnmail-extra-headers))))
161 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
162 (with-current-buffer nntp-server-buffer
164 (when (nnimap-possibly-change-group group server)
165 (with-current-buffer (nnimap-buffer)
167 (nnimap-wait-for-response
170 (nnimap-article-ranges (gnus-compress-sequence articles))
171 (nnimap-header-parameters))
173 (nnimap-transform-headers)
174 (nnheader-remove-cr-followed-by-lf))
175 (insert-buffer-substring
176 (nnimap-find-process-buffer (current-buffer))))
179 (defun nnimap-transform-headers ()
180 (goto-char (point-min))
181 (let (article bytes lines size string)
184 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
185 (delete-region (point) (progn (forward-line 1) (point)))
188 (setq article (match-string 1))
189 ;; Unfold quoted {number} strings.
190 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
191 (1+ (line-end-position)) t)
192 (setq size (string-to-number (match-string 1)))
193 (delete-region (+ (match-beginning 0) 2) (point))
194 (setq string (buffer-substring (point) (+ (point) size)))
195 (delete-region (point) (+ (point) size))
196 (insert (format "%S" string)))
197 (setq bytes (nnimap-get-length)
201 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
206 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
207 (let ((structure (ignore-errors
208 (read (current-buffer)))))
209 (while (and (consp structure)
210 (not (stringp (car structure))))
211 (setq structure (car structure)))
212 (setq lines (nth 7 structure))))
213 (delete-region (line-beginning-position) (line-end-position))
214 (insert (format "211 %s Article retrieved." article))
217 (insert (format "Chars: %s\n" size)))
219 (insert (format "Lines: %s\n" lines)))
220 (unless (re-search-forward "^\r$" nil t)
221 (goto-char (point-max)))
222 (delete-region (line-beginning-position) (line-end-position))
226 (defun nnimap-unfold-quoted-lines ()
227 ;; Unfold quoted {number} strings.
229 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
230 (setq size (string-to-number (match-string 1)))
231 (delete-region (1+ (match-beginning 0)) (point))
232 (setq string (buffer-substring (point) (+ (point) size)))
233 (delete-region (point) (+ (point) size))
234 (insert (format "%S" string)))))
236 (defun nnimap-get-length ()
237 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
238 (string-to-number (match-string 1))))
240 (defun nnimap-article-ranges (ranges)
244 (number-to-string ranges))
245 ((numberp (cdr ranges))
246 (format "%d:%d" (car ranges) (cdr ranges)))
248 (dolist (elem ranges)
251 (format "%d:%d" (car elem) (cdr elem))
252 (number-to-string elem))
254 (mapconcat #'identity (nreverse result) ",")))))
256 (deffoo nnimap-open-server (server &optional defs)
257 (if (nnimap-server-opened server)
259 (unless (assq 'nnimap-address defs)
260 (setq defs (append defs (list (list 'nnimap-address server)))))
261 (nnoo-change-server 'nnimap server defs)
262 (or (nnimap-find-connection nntp-server-buffer)
263 (nnimap-open-connection nntp-server-buffer))))
265 (defun nnimap-make-process-buffer (buffer)
267 (generate-new-buffer (format "*nnimap %s %s %s*"
268 nnimap-address nnimap-server-port
269 (gnus-buffer-exists-p buffer)))
270 (mm-disable-multibyte)
271 (buffer-disable-undo)
273 (set (make-local-variable 'after-change-functions) nil)
274 (set (make-local-variable 'nnimap-object)
275 (make-nnimap :server (nnoo-current-server 'nnimap)))
276 (push (list buffer (current-buffer)) nnimap-connection-alist)
277 (push (current-buffer) nnimap-process-buffers)
280 (defun nnimap-credentials (address ports)
281 (let ((found (nth 0 (auth-source-search :max 1
286 (list (plist-get found :user)
287 (let ((secret (plist-get found :secret)))
288 (if (functionp secret)
293 (defun nnimap-keepalive ()
294 (let ((now (current-time)))
295 (dolist (buffer nnimap-process-buffers)
296 (when (buffer-name buffer)
297 (with-current-buffer buffer
298 (when (and nnimap-object
299 (nnimap-last-command-time nnimap-object)
303 (nnimap-last-command-time nnimap-object)))
304 ;; More than five minutes since the last command.
306 (nnimap-send-command "NOOP")))))))
308 (defun nnimap-open-connection (buffer)
309 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
310 ;; `ssl' when nnimap-server-port was nil. Sort of.
311 (when (and nnimap-server-port
312 (eq nnimap-stream 'undecided))
313 (setq nnimap-stream 'ssl))
315 (if (eq nnimap-stream 'undecided)
316 (loop for type in '(ssl network)
317 for stream = (let ((nnimap-stream type))
318 (nnimap-open-connection-1 buffer))
319 while (eq stream 'no-connect)
320 finally (return stream))
321 (nnimap-open-connection-1 buffer))))
322 (if (eq stream 'no-connect)
326 (defun nnimap-open-connection-1 (buffer)
327 (unless nnimap-keepalive-timer
328 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
330 (with-current-buffer (nnimap-make-process-buffer buffer)
331 (let* ((coding-system-for-read 'binary)
332 (coding-system-for-write 'binary)
336 ((or (eq nnimap-stream 'network)
337 (eq nnimap-stream 'starttls))
338 (nnheader-message 7 "Opening connection to %s..."
341 ((eq nnimap-stream 'shell)
342 (nnheader-message 7 "Opening connection to %s via shell..."
345 ((memq nnimap-stream '(ssl tls))
346 (nnheader-message 7 "Opening connection to %s via tls..."
348 '("143" "993" "imap" "imaps"))
350 (error "Unknown stream type: %s" nnimap-stream))))
351 (proto-stream-always-use-starttls t)
352 login-result credentials)
353 (when nnimap-server-port
354 (setq ports (append ports (list nnimap-server-port))))
355 (destructuring-bind (stream greeting capabilities stream-type)
356 (open-protocol-stream
357 "*nnimap*" (current-buffer) nnimap-address (car (last ports))
359 :shell-command nnimap-shell-program
360 :capability-command "1 CAPABILITY\r\n"
363 (lambda (capabilities)
364 (when (gnus-string-match-p "STARTTLS" capabilities)
366 (setf (nnimap-process nnimap-object) stream)
367 (setf (nnimap-stream-type nnimap-object) stream-type)
370 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
371 nnimap-address port nnimap-stream)
373 (gnus-set-process-query-on-exit-flag stream nil)
374 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
375 (nnheader-report 'nnimap "%s" greeting)
376 ;; Store the greeting (for debugging purposes).
377 (setf (nnimap-greeting nnimap-object) greeting)
378 (setf (nnimap-capabilities nnimap-object)
380 (split-string capabilities)))
381 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
382 (if (not (setq credentials
383 (if (eq nnimap-authenticator 'anonymous)
385 (message-make-address))
386 ;; Look for the credentials based on
387 ;; the virtual server name and the address
389 (gnus-delete-duplicates
392 (nnoo-current-server 'nnimap)))
394 (setq nnimap-object nil)
395 (let ((nnimap-inhibit-logging t))
397 (nnimap-login (car credentials) (cadr credentials))))
398 (unless (car login-result)
399 ;; If the login failed, then forget the credentials
400 ;; that are now possibly cached.
401 (dolist (host (list (nnoo-current-server 'nnimap)
404 (auth-source-forget+ :host host :port port)))
405 (delete-process (nnimap-process nnimap-object))
406 (setq nnimap-object nil))))
408 (when (nnimap-capability "QRESYNC")
409 (nnimap-command "ENABLE QRESYNC"))
410 (nnimap-process nnimap-object))))))))
412 (autoload 'rfc2104-hash "rfc2104")
414 (defun nnimap-login (user password)
416 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
417 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
418 ;; and we're using an encrypted connection.
419 ((and (not (nnimap-capability "LOGINDISABLED"))
420 (eq (nnimap-stream-type nnimap-object) 'tls))
421 (nnimap-command "LOGIN %S %S" user password))
422 ((nnimap-capability "AUTH=CRAM-MD5")
424 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
425 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
427 (get-buffer-process (current-buffer))
429 (base64-encode-string
431 (rfc2104-hash 'md5 64 16 password
432 (base64-decode-string challenge))))
434 (nnimap-wait-for-response sequence)))
435 ((not (nnimap-capability "LOGINDISABLED"))
436 (nnimap-command "LOGIN %S %S" user password))
437 ((nnimap-capability "AUTH=PLAIN")
439 "AUTHENTICATE PLAIN %s"
440 (base64-encode-string
441 (format "\000%s\000%s"
442 (nnimap-quote-specials user)
443 (nnimap-quote-specials password)))))))
445 (defun nnimap-quote-specials (string)
448 (goto-char (point-min))
449 (while (re-search-forward "[\\\"]" nil t)
455 (defun nnimap-find-parameter (parameter elems)
459 ((equal (car elem) parameter)
460 (setq result (cdr elem)))
461 ((and (equal (car elem) "OK")
463 (equal (caadr elem) parameter))
464 (setq result (cdr (cadr elem))))))
467 (deffoo nnimap-close-server (&optional server)
468 (when (nnoo-change-server 'nnimap server nil)
470 (delete-process (get-buffer-process (nnimap-buffer))))
471 (nnoo-close-server 'nnimap server)
474 (deffoo nnimap-request-close ()
477 (deffoo nnimap-server-opened (&optional server)
478 (and (nnoo-current-server-p 'nnimap server)
480 (gnus-buffer-live-p nntp-server-buffer)
481 (nnimap-find-connection nntp-server-buffer)))
483 (deffoo nnimap-status-message (&optional server)
484 nnimap-status-string)
486 (deffoo nnimap-request-article (article &optional group server to-buffer)
487 (with-current-buffer nntp-server-buffer
488 (let ((result (nnimap-possibly-change-group group server))
490 (when (stringp article)
491 (setq article (nnimap-find-article-by-message-id group article)))
495 (with-current-buffer (nnimap-buffer)
497 (when nnimap-fetch-partial-articles
498 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
499 (goto-char (point-min))
500 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
501 (setq structure (ignore-errors
502 (let ((start (point)))
504 (downcase-region start (point))
506 (read (current-buffer))))
507 parts (nnimap-find-wanted-parts structure))))
509 (nnimap-get-partial-article article parts structure)
510 (nnimap-get-whole-article article))
511 (let ((buffer (current-buffer)))
512 (with-current-buffer (or to-buffer nntp-server-buffer)
514 (insert-buffer-substring buffer)
515 (nnheader-ms-strip-cr)
516 (cons group article)))))))))
518 (deffoo nnimap-request-head (article &optional group server to-buffer)
519 (when (nnimap-possibly-change-group group server)
520 (with-current-buffer (nnimap-buffer)
521 (when (stringp article)
522 (setq article (nnimap-find-article-by-message-id group article)))
525 (nnimap-get-whole-article
526 article (format "UID FETCH %%d %s"
527 (nnimap-header-parameters)))
528 (let ((buffer (current-buffer)))
529 (with-current-buffer (or to-buffer nntp-server-buffer)
531 (insert-buffer-substring buffer)
532 (nnheader-ms-strip-cr)
533 (cons group article)))))))
535 (defun nnimap-get-whole-article (article &optional command)
540 "UID FETCH %d BODY.PEEK[]"
541 "UID FETCH %d RFC822.PEEK"))
543 ;; Check that we really got an article.
544 (goto-char (point-min))
545 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
548 ;; Remove any data that may have arrived before the FETCH data.
551 (delete-region (point-min) (point)))
552 (let ((bytes (nnimap-get-length)))
553 (delete-region (line-beginning-position)
554 (progn (forward-line 1) (point)))
555 (goto-char (+ (point) bytes))
556 (delete-region (point) (point-max)))
559 (defun nnimap-capability (capability)
560 (member capability (nnimap-capabilities nnimap-object)))
562 (defun nnimap-ver4-p ()
563 (nnimap-capability "IMAP4REV1"))
565 (defun nnimap-get-partial-article (article parts structure)
568 "UID FETCH %d (%s %s)"
574 (mapconcat (lambda (part)
575 (format "BODY.PEEK[%s]" part))
577 (mapconcat (lambda (part)
578 (format "RFC822.PEEK[%s]" part))
581 (nnimap-convert-partial-article structure))))
583 (defun nnimap-convert-partial-article (structure)
584 ;; First just skip past the headers.
585 (goto-char (point-min))
586 (let ((bytes (nnimap-get-length))
588 ;; Delete "FETCH" line.
589 (delete-region (line-beginning-position)
590 (progn (forward-line 1) (point)))
591 (goto-char (+ (point) bytes))
592 ;; Collect all the body parts.
593 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
594 (setq id (match-string 1)
595 bytes (or (nnimap-get-length) 0))
597 (delete-region (point) (progn (forward-line 1) (point)))
598 (push (list id (buffer-substring (point) (+ (point) bytes)))
600 (delete-region (point) (+ (point) bytes)))
601 ;; Delete trailing junk.
602 (delete-region (point) (point-max))
603 ;; Now insert all the parts again where they fit in the structure.
604 (nnimap-insert-partial-structure structure parts)
607 (defun nnimap-insert-partial-structure (structure parts &optional subp)
609 (let ((bstruc structure))
610 (while (consp (car bstruc))
612 (setq type (car bstruc))
613 (setq bstruc (car (cdr bstruc)))
614 (let ((has-boundary (member "boundary" bstruc)))
616 (setq boundary (cadr has-boundary)))))
618 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
619 (downcase type) boundary)))
620 (while (not (stringp (car structure)))
621 (insert "\n--" boundary "\n")
622 (if (consp (caar structure))
623 (nnimap-insert-partial-structure (pop structure) parts t)
624 (let ((bit (pop structure)))
625 (insert (format "Content-type: %s/%s"
626 (downcase (nth 0 bit))
627 (downcase (nth 1 bit))))
628 (if (member "CHARSET" (nth 2 bit))
630 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
632 (insert (format "Content-transfer-encoding: %s\n"
635 (when (assoc (nth 9 bit) parts)
636 (insert (cadr (assoc (nth 9 bit) parts)))))))
637 (insert "\n--" boundary "--\n")))
639 (defun nnimap-find-wanted-parts (structure)
640 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
642 (defun nnimap-find-wanted-parts-1 (structure prefix)
645 (while (consp (car structure))
646 (let ((sub (pop structure)))
647 (if (consp (car sub))
648 (push (nnimap-find-wanted-parts-1
649 sub (if (string= prefix "")
650 (number-to-string num)
651 (format "%s.%s" prefix num)))
653 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
654 (id (if (string= prefix "")
655 (number-to-string num)
656 (format "%s.%s" prefix num))))
657 (setcar (nthcdr 9 sub) id)
658 (when (if (eq nnimap-fetch-partial-articles t)
660 (string-match nnimap-fetch-partial-articles type))
665 (deffoo nnimap-request-group (group &optional server dont-check info)
666 (let ((result (nnimap-possibly-change-group
667 ;; Don't SELECT the group if we're going to select it
669 (if (and (not dont-check)
670 (assoc group nnimap-current-infos))
674 articles active marks high low)
675 (with-current-buffer nntp-server-buffer
678 (setq active (nth 2 (assoc group nnimap-current-infos))))
679 (insert (format "211 %d %d %d %S\n"
680 (- (cdr active) (car active))
684 (with-current-buffer (nnimap-buffer)
686 (let ((group-sequence
687 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
689 (nnimap-send-command "UID FETCH 1:* FLAGS")))
690 (setf (nnimap-group nnimap-object) group)
691 (nnimap-wait-for-response flag-sequence)
693 (nnimap-flags-to-marks
695 (list (list group-sequence flag-sequence
696 1 group "SELECT")))))
699 (nnimap-update-infos marks (list info))
700 (nnimap-store-info info (gnus-active (gnus-info-group info))))
701 (goto-char (point-max))
702 (let ((uidnext (nth 5 (car marks))))
703 (setq high (or (if uidnext
707 low (or (nth 4 (car marks)) uidnext 1)))))
711 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
714 (deffoo nnimap-request-create-group (group &optional server args)
715 (when (nnimap-possibly-change-group nil server)
716 (with-current-buffer (nnimap-buffer)
717 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
719 (deffoo nnimap-request-delete-group (group &optional force server)
720 (when (nnimap-possibly-change-group nil server)
721 (with-current-buffer (nnimap-buffer)
722 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
724 (deffoo nnimap-request-rename-group (group new-name &optional server)
725 (when (nnimap-possibly-change-group nil server)
726 (with-current-buffer (nnimap-buffer)
727 (nnimap-unselect-group)
728 (car (nnimap-command "RENAME %S %S"
729 (utf7-encode group t) (utf7-encode new-name t))))))
731 (defun nnimap-unselect-group ()
732 ;; Make sure we don't have this group open read/write by asking
733 ;; to examine a mailbox that doesn't exist. This seems to be
734 ;; the only way that allows us to reliably go back to unselected
736 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
738 (deffoo nnimap-request-expunge-group (group &optional server)
739 (when (nnimap-possibly-change-group group server)
740 (with-current-buffer (nnimap-buffer)
741 (car (nnimap-command "EXPUNGE")))))
743 (defun nnimap-get-flags (spec)
746 (with-current-buffer (nnimap-buffer)
748 (nnimap-wait-for-response (nnimap-send-command
749 "UID FETCH %s FLAGS" spec))
751 (subst-char-in-region (point-min) (point-max)
753 (goto-char (point-min))
754 (while (search-forward " FETCH " end t)
755 (setq elems (read (current-buffer)))
756 (push (cons (cadr (memq 'UID elems))
757 (cadr (memq 'FLAGS elems)))
759 (nreverse articles)))
761 (deffoo nnimap-close-group (group &optional server)
764 (deffoo nnimap-request-move-article (article group server accept-form
765 &optional last internal-move-group)
767 (mm-disable-multibyte)
768 (when (funcall (if internal-move-group
770 'nnimap-request-article)
771 article group server (current-buffer))
772 ;; If the move is internal (on the same server), just do it the easy
774 (let ((message-id (message-field-value "message-id")))
775 (if internal-move-group
777 (with-current-buffer (nnimap-buffer)
778 (nnimap-command "UID COPY %d %S"
780 (utf7-encode internal-move-group t)))))
782 (nnimap-delete-article article)
783 (cons internal-move-group
784 (or (nnimap-find-uid-response "COPYUID" (cadr result))
785 (nnimap-find-article-by-message-id
786 internal-move-group message-id)))))
787 ;; Move the article to a different method.
788 (let ((result (eval accept-form)))
790 (nnimap-delete-article article)
793 (deffoo nnimap-request-expire-articles (articles group &optional server force)
797 ((not (nnimap-possibly-change-group group server))
800 (eq nnmail-expiry-target 'delete))
801 (unless (nnimap-delete-article (gnus-compress-sequence articles))
802 (nnheader-message 7 "Article marked for deletion, but not expunged."))
805 (let ((deletable-articles
807 (eq nnmail-expiry-wait 'immediate))
809 (gnus-sorted-intersection
811 (nnimap-find-expired-articles group)))))
812 (if (null deletable-articles)
814 (if (eq nnmail-expiry-target 'delete)
815 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
816 (setq deletable-articles
817 (nnimap-process-expiry-targets
818 deletable-articles group server)))
819 ;; Return the articles we didn't delete.
820 (gnus-sorted-complement articles deletable-articles))))))
822 (defun nnimap-process-expiry-targets (articles group server)
823 (let ((deleted-articles nil))
825 ;; shortcut further processing if we're going to delete the articles
826 ((eq nnmail-expiry-target 'delete)
827 (setq deleted-articles articles)
829 ;; or just move them to another folder on the same IMAP server
830 ((and (not (functionp nnmail-expiry-target))
831 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
832 (gnus-server-to-method
833 (format "nnimap:%s" server))))
834 (and (nnimap-possibly-change-group group server)
835 (with-current-buffer (nnimap-buffer)
836 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
839 (nnimap-article-ranges (gnus-compress-sequence articles))
840 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
841 (setq deleted-articles articles)))
844 (dolist (article articles)
845 (let ((target nnmail-expiry-target))
847 (mm-disable-multibyte)
848 (when (nnimap-request-article article group server (current-buffer))
849 (nnheader-message 7 "Expiring article %s:%d" group article)
850 (when (functionp target)
851 (setq target (funcall target group)))
853 (not (eq target 'delete)))
854 (if (or (gnus-request-group target t)
855 (gnus-request-create-group target))
856 (nnmail-expiry-target-group target group)
859 (push article deleted-articles))))))))
860 ;; Change back to the current group again.
861 (nnimap-possibly-change-group group server)
862 (setq deleted-articles (nreverse deleted-articles))
863 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
866 (defun nnimap-find-expired-articles (group)
867 (let ((cutoff (nnmail-expired-article-p group nil nil)))
868 (with-current-buffer (nnimap-buffer)
871 "UID SEARCH SENTBEFORE %s"
875 (car (rassoc (nth 4 (decode-time cutoff))
876 parse-time-months))))
879 (delete 0 (mapcar #'string-to-number
880 (cdr (assoc "SEARCH" (cdr result))))))))))
883 (defun nnimap-find-article-by-message-id (group message-id)
884 (with-current-buffer (nnimap-buffer)
886 (unless (equal group (nnimap-group nnimap-object))
887 (setf (nnimap-group nnimap-object) nil)
888 (setf (nnimap-examined nnimap-object) group)
889 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
891 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
893 (setq result (nnimap-wait-for-response sequence))
895 (car (setq result (nnimap-parse-response))))
896 ;; Select the last instance of the message in the group.
898 (car (last (assoc "SEARCH" (cdr result)))))
899 (string-to-number article))))))
901 (defun nnimap-delete-article (articles)
902 (with-current-buffer (nnimap-buffer)
903 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
904 (nnimap-article-ranges articles))
906 ((nnimap-capability "UIDPLUS")
907 (nnimap-command "UID EXPUNGE %s"
908 (nnimap-article-ranges articles))
911 (nnimap-command "EXPUNGE")
913 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
914 "server doesn't support UIDPLUS, so we won't "
915 "delete this article now"))))))
917 (deffoo nnimap-request-scan (&optional group server)
918 (when (and (nnimap-possibly-change-group nil server)
920 nnimap-split-methods)
921 (nnheader-message 7 "nnimap %s splitting mail..." server)
922 (nnimap-split-incoming-mail)))
924 (defun nnimap-marks-to-flags (marks)
927 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
931 (deffoo nnimap-request-update-group-status (group status &optional server)
932 (when (nnimap-possibly-change-group nil server)
933 (let ((command (assoc
935 '((subscribe "SUBSCRIBE")
936 (unsubscribe "UNSUBSCRIBE")))))
938 (with-current-buffer (nnimap-buffer)
939 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
941 (deffoo nnimap-request-set-mark (group actions &optional server)
942 (when (nnimap-possibly-change-group group server)
944 (with-current-buffer (nnimap-buffer)
946 ;; Just send all the STORE commands without waiting for
947 ;; response. If they're successful, they're successful.
948 (dolist (action actions)
949 (destructuring-bind (range action marks) action
950 (let ((flags (nnimap-marks-to-flags marks)))
952 (setq sequence (nnimap-send-command
953 "UID STORE %s %sFLAGS.SILENT (%s)"
954 (nnimap-article-ranges range)
956 ((eq action 'del) "-")
957 ((eq action 'add) "+")
958 ((eq action 'set) ""))
959 (mapconcat #'identity flags " ")))))))
960 ;; Wait for the last command to complete to avoid later
961 ;; syncronisation problems with the stream.
963 (nnimap-wait-for-response sequence))))))
965 (deffoo nnimap-request-accept-article (group &optional server last)
966 (when (nnimap-possibly-change-group nil server)
967 (nnmail-check-syntax)
968 (let ((message-id (message-field-value "message-id"))
971 (setq message (buffer-substring-no-properties (point-min) (point-max)))
972 (with-current-buffer (nnimap-buffer)
973 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
975 ;; If we have this group open read-only, then unselect it
976 ;; before appending to it.
977 (when (equal (nnimap-examined nnimap-object) group)
978 (nnimap-unselect-group))
980 (setq sequence (nnimap-send-command
981 "APPEND %S {%d}" (utf7-encode group t)
983 (unless nnimap-streaming
984 (nnimap-wait-for-connection "^[+]"))
985 (process-send-string (get-buffer-process (current-buffer)) message)
986 (process-send-string (get-buffer-process (current-buffer))
987 (if (nnimap-newlinep nnimap-object)
990 (let ((result (nnimap-get-response sequence)))
991 (if (not (nnimap-ok-p result))
993 (nnheader-report 'nnimap "%s" result)
996 (or (nnimap-find-uid-response "APPENDUID" (car result))
997 (nnimap-find-article-by-message-id
998 group message-id))))))))))
1000 (defun nnimap-process-quirk (greeting-match type data)
1001 (when (and (nnimap-greeting nnimap-object)
1002 (string-match greeting-match (nnimap-greeting nnimap-object))
1004 (string-match "\000" data))
1005 (let ((choice (gnus-multiple-choice
1006 "Message contains NUL characters. Delete, continue, abort? "
1007 '((?d "Delete NUL characters")
1008 (?c "Try to APPEND the message as is")
1012 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1018 (goto-char (point-min))
1019 (while (search-forward "\000" nil t)
1020 (replace-match "" t t))
1021 (buffer-string)))))))
1023 (defun nnimap-ok-p (value)
1026 (equal (caar value) "OK")))
1028 (defun nnimap-find-uid-response (name list)
1029 (let ((result (car (last (nnimap-find-response-element name list)))))
1031 (string-to-number result))))
1033 (defun nnimap-find-response-element (name list)
1036 (when (and (consp elem)
1037 (equal name (car elem)))
1038 (setq result elem)))
1041 (deffoo nnimap-request-replace-article (article group buffer)
1043 (when (and (nnimap-possibly-change-group group nil)
1044 ;; Put the article into the group.
1045 (with-current-buffer buffer
1047 (nnimap-request-accept-article group nil t))))
1048 (nnimap-delete-article (list article))
1049 ;; Return the new article number.
1052 (defun nnimap-add-cr ()
1053 (goto-char (point-min))
1054 (while (re-search-forward "\r?\n" nil t)
1055 (replace-match "\r\n" t t)))
1057 (defun nnimap-get-groups ()
1059 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1061 (nnimap-wait-for-response sequence)
1062 (subst-char-in-region (point-min) (point-max)
1064 (goto-char (point-min))
1065 (nnimap-unfold-quoted-lines)
1066 (goto-char (point-min))
1067 (while (search-forward "* LIST " nil t)
1068 (let ((flags (read (current-buffer)))
1069 (separator (read (current-buffer)))
1070 (group (read (current-buffer))))
1071 (unless (member '%NoSelect flags)
1072 (push (if (stringp group)
1074 (format "%s" group))
1078 (deffoo nnimap-request-list (&optional server)
1079 (when (nnimap-possibly-change-group nil server)
1080 (with-current-buffer nntp-server-buffer
1083 (with-current-buffer (nnimap-buffer)
1084 (nnimap-get-groups)))
1085 sequences responses)
1087 (with-current-buffer (nnimap-buffer)
1088 (setf (nnimap-group nnimap-object) nil)
1089 (dolist (group groups)
1090 (setf (nnimap-examined nnimap-object) group)
1091 (push (list (nnimap-send-command "EXAMINE %S"
1092 (utf7-encode group t))
1095 (nnimap-wait-for-response (caar sequences))
1097 (nnimap-get-responses (mapcar #'car sequences))))
1098 (dolist (response responses)
1099 (let* ((sequence (car response))
1100 (response (cadr response))
1101 (group (cadr (assoc sequence sequences))))
1103 (equal (caar response) "OK"))
1104 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1106 (dolist (elem response)
1107 (when (equal (cadr elem) "EXISTS")
1108 (setq exists (string-to-number (car elem)))))
1110 (setq highest (1- (string-to-number (car uidnext)))))
1113 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1116 (insert (format "%S %d %d y\n"
1117 (utf7-decode group t)
1118 highest (1+ highest))))
1120 ;; Return the widest possible range.
1121 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1122 (or highest exists)))))))))
1125 (deffoo nnimap-request-newgroups (date &optional server)
1126 (when (nnimap-possibly-change-group nil server)
1127 (with-current-buffer nntp-server-buffer
1129 (dolist (group (with-current-buffer (nnimap-buffer)
1130 (nnimap-get-groups)))
1131 (unless (assoc group nnimap-current-infos)
1132 ;; Insert dummy numbers here -- they don't matter.
1133 (insert (format "%S 0 1 y\n" group))))
1136 (deffoo nnimap-retrieve-group-data-early (server infos)
1137 (when (nnimap-possibly-change-group nil server)
1138 (with-current-buffer (nnimap-buffer)
1140 (setf (nnimap-group nnimap-object) nil)
1141 (let ((qresyncp (nnimap-capability "QRESYNC"))
1142 params groups sequences active uidvalidity modseq group)
1143 ;; Go through the infos and gather the data needed to know
1144 ;; what and how to request the data.
1145 (dolist (info infos)
1146 (setq params (gnus-info-params info)
1147 group (gnus-group-real-name (gnus-info-group info))
1148 active (cdr (assq 'active params))
1149 uidvalidity (cdr (assq 'uidvalidity params))
1150 modseq (cdr (assq 'modseq params)))
1151 (setf (nnimap-examined nnimap-object) group)
1156 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1157 (utf7-encode group t)
1158 (nnimap-quirk "QRESYNC")
1164 (if (and active uidvalidity)
1165 ;; Fetch the last 100 flags.
1166 (max 1 (- (cdr active) 100))
1171 ;; If we don't have a UIDVALIDITY, then this is
1172 ;; the first time we've seen the group, so we
1173 ;; have to do a SELECT (which is slower than an
1174 ;; examine), but will tell us whether the group
1175 ;; is read-only or not.
1177 (push (list (nnimap-send-command "%s %S" command
1178 (utf7-encode group t))
1179 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1180 start group command)
1184 (defun nnimap-quirk (command)
1185 (let ((quirk (assoc command nnimap-quirks)))
1186 ;; If this server is of a type that matches a quirk, then return
1187 ;; the "quirked" command instead of the proper one.
1188 (if (or (null quirk)
1189 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1193 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1194 (when (and sequences
1195 (nnimap-possibly-change-group nil server))
1196 (with-current-buffer (nnimap-buffer)
1197 ;; Wait for the final data to trickle in.
1198 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1202 ;; Now we should have most of the data we need, no matter
1203 ;; whether we're QRESYNCING, fetching all the flags from
1204 ;; scratch, or just fetching the last 100 flags per group.
1205 (nnimap-update-infos (nnimap-flags-to-marks
1207 (nreverse sequences)))
1209 ;; Finally, just return something resembling an active file in
1210 ;; the nntp buffer, so that the agent can save the info, too.
1211 (with-current-buffer nntp-server-buffer
1213 (dolist (info infos)
1214 (let* ((group (gnus-info-group info))
1215 (active (gnus-active group)))
1217 (insert (format "%S %d %d y\n"
1218 (gnus-group-real-name group)
1220 (car active)))))))))))
1222 (defun nnimap-update-infos (flags infos)
1223 (dolist (info infos)
1224 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1225 (marks (cdr (assoc group flags))))
1227 (nnimap-update-info info marks)))))
1229 (defun nnimap-update-info (info marks)
1230 (destructuring-bind (existing flags high low uidnext start-article
1231 permanent-flags uidvalidity
1232 vanished highestmodseq) marks
1234 ;; Ignore groups with no UIDNEXT/marks. This happens for
1235 ;; completely empty groups.
1236 ((and (not existing)
1238 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1240 (gnus-set-active (gnus-info-group info) active))))
1241 ;; We have a mismatch between the old and new UIDVALIDITY
1242 ;; identifiers, so we have to re-request the group info (the next
1243 ;; time). This virtually never happens.
1244 ((let ((old-uidvalidity
1245 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1246 (and old-uidvalidity
1247 (not (equal old-uidvalidity uidvalidity))
1248 (> start-article 1)))
1249 (gnus-group-remove-parameter info 'uidvalidity)
1250 (gnus-group-remove-parameter info 'modseq))
1251 ;; We have the data needed to update.
1253 (let* ((group (gnus-info-group info))
1254 (completep (and start-article
1255 (= start-article 1)))
1256 (active (or (gnus-active group)
1257 (cdr (assq 'active (gnus-info-params info))))))
1259 (setq high (1- uidnext)))
1260 ;; First set the active ranges based on high/low.
1262 (not (gnus-active group)))
1263 (gnus-set-active group
1266 (cons (min (or low (car active))
1268 (max (or high (cdr active))
1273 ;; No articles in this group.
1274 (cons uidnext (1- uidnext)))
1276 (cons start-article (1- start-article)))
1278 ;; No articles and no uidnext.
1280 (gnus-set-active group
1282 (or high (1- uidnext)))))
1283 ;; See whether this is a read-only group.
1284 (unless (eq permanent-flags 'not-scanned)
1285 (gnus-group-set-parameter
1286 info 'permanent-flags
1287 (and (or (memq '%* permanent-flags)
1288 (memq '%Seen permanent-flags))
1290 ;; Update marks and read articles if this isn't a
1291 ;; read-only IMAP group.
1292 (when (setq permanent-flags
1293 (cdr (assq 'permanent-flags (gnus-info-params info))))
1294 (if (and highestmodseq
1295 (not start-article))
1296 ;; We've gotten the data by QRESYNCing.
1297 (nnimap-update-qresync-info
1298 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1299 ;; Do normal non-QRESYNC flag updates.
1300 ;; Update the list of read articles.
1302 (gnus-compress-sequence
1303 (gnus-set-difference
1304 (gnus-set-difference
1306 (cdr (assoc '%Seen flags)))
1307 (cdr (assoc '%Flagged flags)))))
1308 (read (gnus-range-difference
1309 (cons start-article high) unread)))
1310 (when (> start-article 1)
1313 (if (> start-article 1)
1314 (gnus-sorted-range-intersection
1315 (cons 1 (1- start-article))
1316 (gnus-info-read info))
1317 (gnus-info-read info))
1319 (when (or (not (listp permanent-flags))
1320 (memq '%Seen permanent-flags))
1321 (gnus-info-set-read info read))
1322 ;; Update the marks.
1323 (setq marks (gnus-info-marks info))
1324 (dolist (type (cdr nnimap-mark-alist))
1325 (when (or (not (listp permanent-flags))
1326 (memq (car (assoc (caddr type) flags))
1328 (memq '%* permanent-flags))
1329 (let ((old-marks (assoc (car type) marks))
1331 (gnus-compress-sequence
1332 (cdr (or (assoc (caddr type) flags) ; %Flagged
1333 (assoc (intern (cadr type) obarray) flags)
1334 (assoc (cadr type) flags)))))) ; "\Flagged"
1335 (setq marks (delq old-marks marks))
1337 (when (and old-marks
1338 (> start-article 1))
1339 (setq old-marks (gnus-range-difference
1341 (cons start-article high)))
1342 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1344 (push (cons (car type) new-marks) marks)))))
1345 (gnus-info-set-marks info marks t))))
1346 ;; Tell Gnus whether there are any \Recent messages in any of
1348 (let ((recent (cdr (assoc '%Recent flags))))
1351 (> (car (last recent)) (cdr active)))
1352 (push (list (cons (gnus-group-real-name group) 0))
1353 nnmail-split-history)))
1354 ;; Note the active level for the next run-through.
1355 (gnus-group-set-parameter info 'active (gnus-active group))
1356 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1357 (gnus-group-set-parameter info 'modseq highestmodseq)
1358 (nnimap-store-info info (gnus-active group)))))))
1360 (defun nnimap-update-qresync-info (info existing vanished flags)
1361 ;; Add all the vanished articles to the list of read articles.
1366 (gnus-range-add (gnus-info-read info)
1368 (cdr (assq '%Flagged flags)))
1369 (cdr (assq '%Seen flags))))
1370 (let ((marks (gnus-info-marks info)))
1371 (dolist (type (cdr nnimap-mark-alist))
1372 (let ((ticks (assoc (car type) marks))
1374 (cdr (or (assoc (caddr type) flags) ; %Flagged
1375 (assoc (intern (cadr type) obarray) flags)
1376 (assoc (cadr type) flags))))) ; "\Flagged"
1377 (setq marks (delq ticks marks))
1379 ;; Add the new marks we got.
1380 (setq ticks (gnus-add-to-range ticks new-marks))
1381 ;; Remove the marks from messages that don't have them.
1382 (setq ticks (gnus-remove-from-range
1384 (gnus-compress-sequence
1385 (gnus-sorted-complement existing new-marks))))
1387 (push (cons (car type) ticks) marks)))
1388 (gnus-info-set-marks info marks t))))
1390 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1391 (if (zerop (length irange))
1394 (dolist (elem (split-string irange ","))
1396 (if (string-match ":" elem)
1397 (let ((numbers (split-string elem ":")))
1398 (cons (string-to-number (car numbers))
1399 (string-to-number (cadr numbers))))
1400 (string-to-number elem))
1402 (nreverse result))))
1404 (defun nnimap-store-info (info active)
1405 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1406 (entry (assoc group nnimap-current-infos)))
1408 (setcdr entry (list info active))
1409 (push (list group info active) nnimap-current-infos))))
1411 (defun nnimap-flags-to-marks (groups)
1412 (let (data group totalp uidnext articles start-article mark permanent-flags
1413 uidvalidity vanished highestmodseq)
1414 (dolist (elem groups)
1415 (setq group (car elem)
1416 uidnext (nth 1 elem)
1417 start-article (nth 2 elem)
1418 permanent-flags (nth 3 elem)
1419 uidvalidity (nth 4 elem)
1420 vanished (nth 5 elem)
1421 highestmodseq (nth 6 elem)
1422 articles (nthcdr 7 elem))
1423 (let ((high (caar articles))
1425 (dolist (article articles)
1426 (setq low (car article))
1427 (push (car article) existing)
1428 (dolist (flag (cdr article))
1429 (setq mark (assoc flag marks))
1431 (push (list flag (car article)) marks)
1432 (setcdr mark (cons (car article) (cdr mark))))))
1433 (push (list group existing marks high low uidnext start-article
1434 permanent-flags uidvalidity vanished highestmodseq)
1438 (defun nnimap-parse-flags (sequences)
1439 (goto-char (point-min))
1440 ;; Change \Delete etc to %Delete, so that the reader can read it.
1441 (subst-char-in-region (point-min) (point-max)
1443 (let (start end articles groups uidnext elems permanent-flags
1444 uidvalidity vanished highestmodseq)
1445 (dolist (elem sequences)
1446 (destructuring-bind (group-sequence flag-sequence totalp group command)
1448 (setq start (point))
1450 ;; The EXAMINE was successful.
1451 (search-forward (format "\n%d OK " group-sequence) nil t)
1456 (setq permanent-flags
1457 (if (equal command "SELECT")
1458 (and (search-forward "PERMANENTFLAGS "
1459 (or end (point-min)) t)
1460 (read (current-buffer)))
1464 (and (search-forward "UIDNEXT "
1465 (or end (point-min)) t)
1466 (read (current-buffer))))
1469 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1470 (or end (point-min)) t)
1471 ;; Store UIDVALIDITY as a string, as it's
1472 ;; too big for 32-bit Emacsen, usually.
1476 (and (eq flag-sequence 'qresync)
1477 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1478 (or end (point-min)) t)
1482 (and (search-forward "HIGHESTMODSEQ "
1483 (or end (point-min)) t)
1484 (read (current-buffer))))
1487 ;; The UID FETCH FLAGS was successful.
1488 (or (eq flag-sequence 'qresync)
1489 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1490 (if (eq flag-sequence 'qresync)
1494 (setq start (point))
1496 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1497 (setq elems (read (current-buffer)))
1498 (push (cons (cadr (memq 'UID elems))
1499 (cadr (memq 'FLAGS elems)))
1501 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1502 vanished highestmodseq)
1506 (setq articles nil))))
1509 (defun nnimap-find-process-buffer (buffer)
1510 (cadr (assoc buffer nnimap-connection-alist)))
1512 (deffoo nnimap-request-post (&optional server)
1513 (setq nnimap-status-string "Read-only server")
1516 (declare-function gnus-fetch-headers "gnus-sum"
1517 (articles &optional limit force-new dependencies))
1519 (deffoo nnimap-request-thread (header)
1520 (let* ((id (mail-header-id header))
1522 (or (mail-header-references header)
1526 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1528 (dolist (refid refs value)
1530 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1531 refid refid value)))))
1532 (result (with-current-buffer (nnimap-buffer)
1533 (nnimap-command "UID SEARCH %s" cmd))))
1535 (and (car result) (delete 0 (mapcar #'string-to-number
1536 (cdr (assoc "SEARCH" (cdr result))))))
1539 (defun nnimap-possibly-change-group (group server)
1540 (let ((open-result t))
1542 (not (nnimap-server-opened server)))
1543 (setq open-result (nnimap-open-server server)))
1550 (with-current-buffer (nnimap-buffer)
1551 (if (equal group (nnimap-group nnimap-object))
1553 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1555 (setf (nnimap-group nnimap-object) group
1556 (nnimap-select-result nnimap-object) result)
1559 (defun nnimap-find-connection (buffer)
1560 "Find the connection delivering to BUFFER."
1561 (let ((entry (assoc buffer nnimap-connection-alist)))
1563 (if (and (buffer-name (cadr entry))
1564 (get-buffer-process (cadr entry))
1565 (memq (process-status (get-buffer-process (cadr entry)))
1567 (get-buffer-process (cadr entry))
1568 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1571 (defvar nnimap-sequence 0)
1573 (defun nnimap-send-command (&rest args)
1574 (setf (nnimap-last-command-time nnimap-object) (current-time))
1575 (process-send-string
1576 (get-buffer-process (current-buffer))
1579 (incf nnimap-sequence)
1580 (apply #'format args)
1581 (if (nnimap-newlinep nnimap-object)
1584 ;; Some servers apparently can't have many outstanding
1585 ;; commands, so throttle them.
1586 (unless nnimap-streaming
1587 (nnimap-wait-for-response nnimap-sequence))
1590 (defun nnimap-log-command (command)
1591 (with-current-buffer (get-buffer-create "*imap log*")
1592 (goto-char (point-max))
1593 (insert (format-time-string "%H:%M:%S") " "
1594 (if nnimap-inhibit-logging
1599 (defun nnimap-command (&rest args)
1601 (let* ((sequence (apply #'nnimap-send-command args))
1602 (response (nnimap-get-response sequence)))
1603 (if (equal (caar response) "OK")
1605 (nnheader-report 'nnimap "%s"
1606 (mapconcat (lambda (a)
1608 (car response) " "))
1611 (defun nnimap-get-response (sequence)
1612 (nnimap-wait-for-response sequence)
1613 (nnimap-parse-response))
1615 (defun nnimap-wait-for-connection (&optional regexp)
1616 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1618 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1619 (let ((process (get-buffer-process (current-buffer))))
1620 (goto-char (point-min))
1621 (while (and (memq (process-status process)
1623 (not (re-search-forward regexp nil t)))
1624 (nnheader-accept-process-output process)
1625 (goto-char (point-min)))
1627 (and (looking-at (or response-regexp regexp))
1630 (defun nnimap-wait-for-response (sequence &optional messagep)
1631 (let ((process (get-buffer-process (current-buffer)))
1635 (goto-char (point-max))
1636 (while (and (setq openp (memq (process-status process)
1639 ;; Skip past any "*" lines that the server has
1641 (while (and (not (bobp))
1644 (looking-at "\\*"))))
1645 (not (looking-at (format "%d .*\n" sequence)))))
1647 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1648 (nnheader-accept-process-output process)
1649 (goto-char (point-max)))
1652 ;; The user hit C-g while we were waiting: kill the process, in case
1653 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1655 (delete-process process)
1658 (defun nnimap-parse-response ()
1659 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1661 (dolist (line lines)
1662 (push (cdr (nnimap-parse-line line)) result))
1663 ;; Return the OK/error code first, and then all the "continuation
1664 ;; lines" afterwards.
1666 (nreverse result))))
1668 ;; Parse an IMAP response line lightly. They look like
1669 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1670 ;; the lines into a list of strings and lists of string.
1671 (defun nnimap-parse-line (line)
1674 (mm-disable-multibyte)
1676 (goto-char (point-min))
1678 (if (eql (setq char (following-char)) ? )
1686 (if (search-forward "]" (line-end-position) 'move)
1693 (if (search-forward ")" (line-end-position) 'move)
1700 (1- (or (search-forward "\"" (line-end-position) 'move)
1703 (buffer-substring (point) (if (search-forward " " nil t)
1705 (goto-char (point-max))))))
1707 (nreverse result))))
1709 (defun nnimap-last-response-string ()
1712 (let ((end (point)))
1716 (while (and (not (bobp))
1717 (eql (following-char) ?*))
1719 (unless (eql (following-char) ?*)
1721 (buffer-substring (point) end))))
1723 (defun nnimap-get-responses (sequences)
1725 (dolist (sequence sequences)
1726 (goto-char (point-min))
1727 (when (re-search-forward (format "^%d " sequence) nil t)
1728 (push (list sequence (nnimap-parse-response))
1732 (defvar nnimap-incoming-split-list nil)
1734 (defun nnimap-fetch-inbox (articles)
1736 (nnimap-wait-for-response
1737 (nnimap-send-command
1739 (nnimap-article-ranges articles)
1740 (format "(UID %s%s)"
1743 "BODY.PEEK[HEADER] BODY.PEEK"
1745 (if nnimap-split-download-body-default
1750 (defun nnimap-split-incoming-mail ()
1751 (with-current-buffer (nnimap-buffer)
1752 (let ((nnimap-incoming-split-list nil)
1753 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1754 nnmail-split-methods
1755 nnimap-split-methods))
1756 (nnmail-split-fancy (or nnimap-split-fancy
1757 nnmail-split-fancy))
1758 (nnmail-inhibit-default-split-group t)
1759 (groups (nnimap-get-groups))
1762 (nnimap-command "SELECT %S" nnimap-inbox)
1763 (setf (nnimap-group nnimap-object) nnimap-inbox)
1764 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1766 (nnimap-fetch-inbox new-articles)
1767 (nnimap-transform-split-mail)
1768 (nnheader-ms-strip-cr)
1770 (nnmail-split-incoming (current-buffer)
1771 #'nnimap-save-mail-spec
1773 #'nnimap-dummy-active-number
1774 #'nnimap-save-mail-spec)
1775 (when nnimap-incoming-split-list
1776 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1777 sequences junk-articles)
1778 ;; Create any groups that doesn't already exist on the
1780 (dolist (spec specs)
1781 (when (and (not (member (car spec) groups))
1782 (not (eq (car spec) 'junk)))
1783 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1784 ;; Then copy over all the messages.
1786 (dolist (spec specs)
1787 (let ((group (car spec))
1788 (ranges (cdr spec)))
1789 (if (eq group 'junk)
1790 (setq junk-articles ranges)
1791 (push (list (nnimap-send-command
1793 (nnimap-article-ranges ranges)
1794 (utf7-encode group t))
1797 ;; Wait for the last COPY response...
1799 (nnimap-wait-for-response (caar sequences))
1800 ;; And then mark the successful copy actions as deleted,
1801 ;; and possibly expunge them.
1802 (nnimap-mark-and-expunge-incoming
1803 (nnimap-parse-copied-articles sequences)))
1804 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1806 (defun nnimap-mark-and-expunge-incoming (range)
1808 (setq range (nnimap-article-ranges range))
1811 (nnimap-send-command
1812 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1814 ;; If the server supports it, we now delete the message we have
1815 ;; just copied over.
1816 ((nnimap-capability "UIDPLUS")
1817 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1818 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1819 ;; user has configured it.
1821 (setq sequence (nnimap-send-command "EXPUNGE"))))
1822 (nnimap-wait-for-response sequence))))
1824 (defun nnimap-parse-copied-articles (sequences)
1825 (let (sequence copied range)
1826 (goto-char (point-min))
1827 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
1828 (setq sequence (string-to-number (match-string 1)))
1829 (when (setq range (cadr (assq sequence sequences)))
1830 (push (gnus-uncompress-range range) copied)))
1831 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1833 (defun nnimap-new-articles (flags)
1835 (dolist (elem flags)
1836 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1838 (push (car elem) new)))
1839 (gnus-compress-sequence (nreverse new))))
1841 (defun nnimap-make-split-specs (list)
1845 (destructuring-bind (article spec) elem
1846 (dolist (group (delete nil (mapcar #'car spec)))
1847 (unless (setq entry (assoc group specs))
1848 (push (setq entry (list group)) specs))
1849 (setcdr entry (cons article (cdr entry))))))
1850 (dolist (entry specs)
1851 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1854 (defun nnimap-transform-split-mail ()
1855 (goto-char (point-min))
1856 (let (article bytes)
1859 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1860 (delete-region (point) (progn (forward-line 1) (point)))
1863 (setq article (match-string 1)
1864 bytes (nnimap-get-length))
1865 (delete-region (line-beginning-position) (line-end-position))
1866 ;; Insert MMDF separator, and a way to remember what this
1868 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1869 (forward-char (1+ bytes))
1870 (setq bytes (nnimap-get-length))
1871 (delete-region (line-beginning-position) (line-end-position))
1872 ;; There's a body; skip past that.
1874 (forward-char (1+ bytes))
1875 (delete-region (line-beginning-position) (line-end-position)))))))
1877 (defun nnimap-dummy-active-number (group &optional server)
1880 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1882 (goto-char (point-min))
1883 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1884 (error "Invalid nnimap mail")
1885 (setq article (string-to-number (match-string 1))))
1887 (if (eq group-art 'junk)
1888 (list (cons 'junk 1))
1890 nnimap-incoming-split-list)))
1894 ;;; nnimap.el ends here