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', `network-only, `starttls' or
66 `shell'. The default is to try `ssl' first, and then
69 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
70 (if (listp imap-shell-program)
71 (car imap-shell-program)
75 (defvoo nnimap-inbox nil
76 "The mail box where incoming mail arrives and should be split out of.
77 For example, \"INBOX\".")
79 (defvoo nnimap-split-methods nil
81 Uses the same syntax as `nnmail-split-methods'.")
83 (defvoo nnimap-split-fancy nil
84 "Uses the same syntax as `nnmail-split-fancy'.")
86 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
87 "Articles with the flags in the list will not be considered when splitting.")
89 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
92 (defvoo nnimap-authenticator nil
93 "How nnimap authenticate itself to the server.
94 Possible choices are nil (use default methods) or `anonymous'.")
96 (defvoo nnimap-expunge t
97 "If non-nil, expunge articles after deleting them.
98 This is always done if the server supports UID EXPUNGE, but it's
99 not done by default on servers that doesn't support that command.")
101 (defvoo nnimap-streaming t
102 "If non-nil, try to use streaming commands with IMAP servers.
103 Switching this off will make nnimap slower, but it helps with
106 (defvoo nnimap-connection-alist nil)
108 (defvoo nnimap-current-infos nil)
110 (defvoo nnimap-fetch-partial-articles nil
111 "If non-nil, Gnus will fetch partial articles.
112 If t, nnimap will fetch only the first part. If a string, it
113 will fetch all parts that have types that match that string. A
114 likely value would be \"text/\" to automatically fetch all
117 (defvar nnimap-process nil)
119 (defvar nnimap-status-string "")
121 (defvar nnimap-split-download-body-default nil
122 "Internal variable with default value for `nnimap-split-download-body'.")
124 (defvar nnimap-keepalive-timer nil)
125 (defvar nnimap-process-buffers nil)
128 group process commands capabilities select-result newlinep server
129 last-command-time greeting examined stream-type)
131 (defvar nnimap-object nil)
133 (defvar nnimap-mark-alist
134 '((read "\\Seen" %Seen)
135 (tick "\\Flagged" %Flagged)
136 (reply "\\Answered" %Answered)
137 (expire "gnus-expire")
138 (dormant "gnus-dormant")
141 (download "gnus-download")
142 (forward "gnus-forward")))
144 (defvar nnimap-quirks
145 '(("QRESYNC" "Zimbra" "QRESYNC ")))
147 (defvar nnimap-inhibit-logging nil)
149 (defun nnimap-buffer ()
150 (nnimap-find-process-buffer nntp-server-buffer))
152 (defun nnimap-header-parameters ()
153 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
156 "BODY.PEEK[HEADER.FIELDS %s]"
157 "RFC822.HEADER.LINES %s")
158 (append '(Subject From Date Message-Id
159 References In-Reply-To Xref)
160 nnmail-extra-headers))))
162 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
163 (with-current-buffer nntp-server-buffer
165 (when (nnimap-possibly-change-group group server)
166 (with-current-buffer (nnimap-buffer)
168 (nnimap-wait-for-response
171 (nnimap-article-ranges (gnus-compress-sequence articles))
172 (nnimap-header-parameters))
174 (nnimap-transform-headers)
175 (nnheader-remove-cr-followed-by-lf))
176 (insert-buffer-substring
177 (nnimap-find-process-buffer (current-buffer))))
180 (defun nnimap-transform-headers ()
181 (goto-char (point-min))
182 (let (article bytes lines size string)
185 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
186 (delete-region (point) (progn (forward-line 1) (point)))
189 (setq article (match-string 1))
190 ;; Unfold quoted {number} strings.
191 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
192 (1+ (line-end-position)) t)
193 (setq size (string-to-number (match-string 1)))
194 (delete-region (+ (match-beginning 0) 2) (point))
195 (setq string (buffer-substring (point) (+ (point) size)))
196 (delete-region (point) (+ (point) size))
197 (insert (format "%S" string)))
198 (setq bytes (nnimap-get-length)
202 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
207 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
208 (let ((structure (ignore-errors
209 (read (current-buffer)))))
210 (while (and (consp structure)
211 (not (stringp (car structure))))
212 (setq structure (car structure)))
213 (setq lines (nth 7 structure))))
214 (delete-region (line-beginning-position) (line-end-position))
215 (insert (format "211 %s Article retrieved." article))
218 (insert (format "Chars: %s\n" size)))
220 (insert (format "Lines: %s\n" lines)))
221 (unless (re-search-forward "^\r$" nil t)
222 (goto-char (point-max)))
223 (delete-region (line-beginning-position) (line-end-position))
227 (defun nnimap-unfold-quoted-lines ()
228 ;; Unfold quoted {number} strings.
230 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
231 (setq size (string-to-number (match-string 1)))
232 (delete-region (1+ (match-beginning 0)) (point))
233 (setq string (buffer-substring (point) (+ (point) size)))
234 (delete-region (point) (+ (point) size))
235 (insert (format "%S" string)))))
237 (defun nnimap-get-length ()
238 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
239 (string-to-number (match-string 1))))
241 (defun nnimap-article-ranges (ranges)
245 (number-to-string ranges))
246 ((numberp (cdr ranges))
247 (format "%d:%d" (car ranges) (cdr ranges)))
249 (dolist (elem ranges)
252 (format "%d:%d" (car elem) (cdr elem))
253 (number-to-string elem))
255 (mapconcat #'identity (nreverse result) ",")))))
257 (deffoo nnimap-open-server (server &optional defs)
258 (if (nnimap-server-opened server)
260 (unless (assq 'nnimap-address defs)
261 (setq defs (append defs (list (list 'nnimap-address server)))))
262 (nnoo-change-server 'nnimap server defs)
263 (or (nnimap-find-connection nntp-server-buffer)
264 (nnimap-open-connection nntp-server-buffer))))
266 (defun nnimap-make-process-buffer (buffer)
268 (generate-new-buffer (format "*nnimap %s %s %s*"
269 nnimap-address nnimap-server-port
270 (gnus-buffer-exists-p buffer)))
271 (mm-disable-multibyte)
272 (buffer-disable-undo)
274 (set (make-local-variable 'after-change-functions) nil)
275 (set (make-local-variable 'nnimap-object)
276 (make-nnimap :server (nnoo-current-server 'nnimap)))
277 (push (list buffer (current-buffer)) nnimap-connection-alist)
278 (push (current-buffer) nnimap-process-buffers)
281 (defun nnimap-credentials (address ports)
282 (let ((found (nth 0 (auth-source-search :max 1
285 :require '(:user :secret)
288 (list (plist-get found :user)
289 (let ((secret (plist-get found :secret)))
290 (if (functionp secret)
293 (plist-get found :save-function))
296 (defun nnimap-keepalive ()
297 (let ((now (current-time)))
298 (dolist (buffer nnimap-process-buffers)
299 (when (buffer-name buffer)
300 (with-current-buffer buffer
301 (when (and nnimap-object
302 (nnimap-last-command-time nnimap-object)
306 (nnimap-last-command-time nnimap-object)))
307 ;; More than five minutes since the last command.
309 (nnimap-send-command "NOOP")))))))
311 (defun nnimap-open-connection (buffer)
312 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
313 ;; `ssl' when nnimap-server-port was nil. Sort of.
314 (when (and nnimap-server-port
315 (eq nnimap-stream 'undecided))
316 (setq nnimap-stream 'ssl))
318 (if (eq nnimap-stream 'undecided)
319 (loop for type in '(ssl network)
320 for stream = (let ((nnimap-stream type))
321 (nnimap-open-connection-1 buffer))
322 while (eq stream 'no-connect)
323 finally (return stream))
324 (nnimap-open-connection-1 buffer))))
325 (if (eq stream 'no-connect)
329 (defun nnimap-open-connection-1 (buffer)
330 (unless nnimap-keepalive-timer
331 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
333 (with-current-buffer (nnimap-make-process-buffer buffer)
334 (let* ((coding-system-for-read 'binary)
335 (coding-system-for-write 'binary)
339 ((or (eq nnimap-stream 'network)
340 (eq nnimap-stream 'starttls))
341 (nnheader-message 7 "Opening connection to %s..."
344 ((eq nnimap-stream 'shell)
345 (nnheader-message 7 "Opening connection to %s via shell..."
348 ((memq nnimap-stream '(ssl tls))
349 (nnheader-message 7 "Opening connection to %s via tls..."
351 '("imaps" "imap" "993" "143"))
353 (error "Unknown stream type: %s" nnimap-stream))))
354 (proto-stream-always-use-starttls t)
355 login-result credentials)
356 (when nnimap-server-port
357 (push nnimap-server-port ports))
358 (destructuring-bind (stream greeting capabilities stream-type)
359 (open-protocol-stream
360 "*nnimap*" (current-buffer) nnimap-address (car ports)
362 :shell-command nnimap-shell-program
363 :capability-command "1 CAPABILITY\r\n"
366 (lambda (capabilities)
367 (when (gnus-string-match-p "STARTTLS" capabilities)
369 (setf (nnimap-process nnimap-object) stream)
370 (setf (nnimap-stream-type nnimap-object) stream-type)
373 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
374 nnimap-address port nnimap-stream)
376 (gnus-set-process-query-on-exit-flag stream nil)
377 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
378 (nnheader-report 'nnimap "%s" greeting)
379 ;; Store the greeting (for debugging purposes).
380 (setf (nnimap-greeting nnimap-object) greeting)
381 (setf (nnimap-capabilities nnimap-object)
383 (split-string capabilities)))
384 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
385 (if (not (setq credentials
386 (if (eq nnimap-authenticator 'anonymous)
388 (message-make-address))
389 ;; Look for the credentials based on
390 ;; the virtual server name and the address
392 (gnus-delete-duplicates
395 (nnoo-current-server 'nnimap)))
397 (setq nnimap-object nil)
398 (let ((nnimap-inhibit-logging t))
400 (nnimap-login (car credentials) (cadr credentials))))
401 (if (car login-result)
402 ;; save the credentials if a save function exists
403 ;; (such a function will only be passed if a new
404 ;; token was created)
405 (when (functionp (nth 2 credentials))
406 (funcall (nth 2 credentials)))
407 ;; If the login failed, then forget the credentials
408 ;; that are now possibly cached.
409 (dolist (host (list (nnoo-current-server 'nnimap)
412 (auth-source-forget+ :host host :port port)))
413 (delete-process (nnimap-process nnimap-object))
414 (setq nnimap-object nil))))
416 (when (nnimap-capability "QRESYNC")
417 (nnimap-command "ENABLE QRESYNC"))
418 (nnimap-process nnimap-object))))))))
420 (autoload 'rfc2104-hash "rfc2104")
422 (defun nnimap-login (user password)
424 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
425 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
426 ;; and we're using an encrypted connection.
427 ((and (not (nnimap-capability "LOGINDISABLED"))
428 (eq (nnimap-stream-type nnimap-object) 'tls))
429 (nnimap-command "LOGIN %S %S" user password))
430 ((nnimap-capability "AUTH=CRAM-MD5")
432 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
433 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
435 (get-buffer-process (current-buffer))
437 (base64-encode-string
439 (rfc2104-hash 'md5 64 16 password
440 (base64-decode-string challenge))))
442 (nnimap-wait-for-response sequence)))
443 ((not (nnimap-capability "LOGINDISABLED"))
444 (nnimap-command "LOGIN %S %S" user password))
445 ((nnimap-capability "AUTH=PLAIN")
447 "AUTHENTICATE PLAIN %s"
448 (base64-encode-string
449 (format "\000%s\000%s"
450 (nnimap-quote-specials user)
451 (nnimap-quote-specials password)))))))
453 (defun nnimap-quote-specials (string)
456 (goto-char (point-min))
457 (while (re-search-forward "[\\\"]" nil t)
463 (defun nnimap-find-parameter (parameter elems)
467 ((equal (car elem) parameter)
468 (setq result (cdr elem)))
469 ((and (equal (car elem) "OK")
471 (equal (caadr elem) parameter))
472 (setq result (cdr (cadr elem))))))
475 (deffoo nnimap-close-server (&optional server)
476 (when (nnoo-change-server 'nnimap server nil)
478 (delete-process (get-buffer-process (nnimap-buffer))))
479 (nnoo-close-server 'nnimap server)
482 (deffoo nnimap-request-close ()
485 (deffoo nnimap-server-opened (&optional server)
486 (and (nnoo-current-server-p 'nnimap server)
488 (gnus-buffer-live-p nntp-server-buffer)
489 (nnimap-find-connection nntp-server-buffer)))
491 (deffoo nnimap-status-message (&optional server)
492 nnimap-status-string)
494 (deffoo nnimap-request-article (article &optional group server to-buffer)
495 (with-current-buffer nntp-server-buffer
496 (let ((result (nnimap-possibly-change-group group server))
498 (when (stringp article)
499 (setq article (nnimap-find-article-by-message-id group article)))
503 (with-current-buffer (nnimap-buffer)
505 (when nnimap-fetch-partial-articles
506 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
507 (goto-char (point-min))
508 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
509 (setq structure (ignore-errors
510 (let ((start (point)))
512 (downcase-region start (point))
514 (read (current-buffer))))
515 parts (nnimap-find-wanted-parts structure))))
517 (nnimap-get-partial-article article parts structure)
518 (nnimap-get-whole-article article))
519 (let ((buffer (current-buffer)))
520 (with-current-buffer (or to-buffer nntp-server-buffer)
522 (insert-buffer-substring buffer)
523 (nnheader-ms-strip-cr)
524 (cons group article)))))))))
526 (deffoo nnimap-request-head (article &optional group server to-buffer)
527 (when (nnimap-possibly-change-group group server)
528 (with-current-buffer (nnimap-buffer)
529 (when (stringp article)
530 (setq article (nnimap-find-article-by-message-id group article)))
533 (nnimap-get-whole-article
534 article (format "UID FETCH %%d %s"
535 (nnimap-header-parameters)))
536 (let ((buffer (current-buffer)))
537 (with-current-buffer (or to-buffer nntp-server-buffer)
539 (insert-buffer-substring buffer)
540 (nnheader-ms-strip-cr)
541 (cons group article)))))))
543 (defun nnimap-get-whole-article (article &optional command)
548 "UID FETCH %d BODY.PEEK[]"
549 "UID FETCH %d RFC822.PEEK"))
551 ;; Check that we really got an article.
552 (goto-char (point-min))
553 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
556 ;; Remove any data that may have arrived before the FETCH data.
559 (delete-region (point-min) (point)))
560 (let ((bytes (nnimap-get-length)))
561 (delete-region (line-beginning-position)
562 (progn (forward-line 1) (point)))
563 (goto-char (+ (point) bytes))
564 (delete-region (point) (point-max)))
567 (defun nnimap-capability (capability)
568 (member capability (nnimap-capabilities nnimap-object)))
570 (defun nnimap-ver4-p ()
571 (nnimap-capability "IMAP4REV1"))
573 (defun nnimap-get-partial-article (article parts structure)
576 "UID FETCH %d (%s %s)"
582 (mapconcat (lambda (part)
583 (format "BODY.PEEK[%s]" part))
585 (mapconcat (lambda (part)
586 (format "RFC822.PEEK[%s]" part))
589 (nnimap-convert-partial-article structure))))
591 (defun nnimap-convert-partial-article (structure)
592 ;; First just skip past the headers.
593 (goto-char (point-min))
594 (let ((bytes (nnimap-get-length))
596 ;; Delete "FETCH" line.
597 (delete-region (line-beginning-position)
598 (progn (forward-line 1) (point)))
599 (goto-char (+ (point) bytes))
600 ;; Collect all the body parts.
601 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
602 (setq id (match-string 1)
603 bytes (or (nnimap-get-length) 0))
605 (delete-region (point) (progn (forward-line 1) (point)))
606 (push (list id (buffer-substring (point) (+ (point) bytes)))
608 (delete-region (point) (+ (point) bytes)))
609 ;; Delete trailing junk.
610 (delete-region (point) (point-max))
611 ;; Now insert all the parts again where they fit in the structure.
612 (nnimap-insert-partial-structure structure parts)
615 (defun nnimap-insert-partial-structure (structure parts &optional subp)
617 (let ((bstruc structure))
618 (while (consp (car bstruc))
620 (setq type (car bstruc))
621 (setq bstruc (car (cdr bstruc)))
622 (let ((has-boundary (member "boundary" bstruc)))
624 (setq boundary (cadr has-boundary)))))
626 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
627 (downcase type) boundary)))
628 (while (not (stringp (car structure)))
629 (insert "\n--" boundary "\n")
630 (if (consp (caar structure))
631 (nnimap-insert-partial-structure (pop structure) parts t)
632 (let ((bit (pop structure)))
633 (insert (format "Content-type: %s/%s"
634 (downcase (nth 0 bit))
635 (downcase (nth 1 bit))))
636 (if (member "CHARSET" (nth 2 bit))
638 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
640 (insert (format "Content-transfer-encoding: %s\n"
643 (when (assoc (nth 9 bit) parts)
644 (insert (cadr (assoc (nth 9 bit) parts)))))))
645 (insert "\n--" boundary "--\n")))
647 (defun nnimap-find-wanted-parts (structure)
648 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
650 (defun nnimap-find-wanted-parts-1 (structure prefix)
653 (while (consp (car structure))
654 (let ((sub (pop structure)))
655 (if (consp (car sub))
656 (push (nnimap-find-wanted-parts-1
657 sub (if (string= prefix "")
658 (number-to-string num)
659 (format "%s.%s" prefix num)))
661 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
662 (id (if (string= prefix "")
663 (number-to-string num)
664 (format "%s.%s" prefix num))))
665 (setcar (nthcdr 9 sub) id)
666 (when (if (eq nnimap-fetch-partial-articles t)
668 (string-match nnimap-fetch-partial-articles type))
673 (deffoo nnimap-request-group (group &optional server dont-check info)
674 (let ((result (nnimap-possibly-change-group
675 ;; Don't SELECT the group if we're going to select it
677 (if (and (not dont-check)
678 (assoc group nnimap-current-infos))
682 articles active marks high low)
683 (with-current-buffer nntp-server-buffer
686 (setq active (nth 2 (assoc group nnimap-current-infos))))
687 (insert (format "211 %d %d %d %S\n"
688 (- (cdr active) (car active))
692 (with-current-buffer (nnimap-buffer)
694 (let ((group-sequence
695 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
697 (nnimap-send-command "UID FETCH 1:* FLAGS")))
698 (setf (nnimap-group nnimap-object) group)
699 (nnimap-wait-for-response flag-sequence)
701 (nnimap-flags-to-marks
703 (list (list group-sequence flag-sequence
704 1 group "SELECT")))))
707 (nnimap-update-infos marks (list info))
708 (nnimap-store-info info (gnus-active (gnus-info-group info))))
709 (goto-char (point-max))
710 (let ((uidnext (nth 5 (car marks))))
711 (setq high (or (if uidnext
715 low (or (nth 4 (car marks)) uidnext 1)))))
719 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
722 (deffoo nnimap-request-create-group (group &optional server args)
723 (when (nnimap-possibly-change-group nil server)
724 (with-current-buffer (nnimap-buffer)
725 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
727 (deffoo nnimap-request-delete-group (group &optional force server)
728 (when (nnimap-possibly-change-group nil server)
729 (with-current-buffer (nnimap-buffer)
730 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
732 (deffoo nnimap-request-rename-group (group new-name &optional server)
733 (when (nnimap-possibly-change-group nil server)
734 (with-current-buffer (nnimap-buffer)
735 (nnimap-unselect-group)
736 (car (nnimap-command "RENAME %S %S"
737 (utf7-encode group t) (utf7-encode new-name t))))))
739 (defun nnimap-unselect-group ()
740 ;; Make sure we don't have this group open read/write by asking
741 ;; to examine a mailbox that doesn't exist. This seems to be
742 ;; the only way that allows us to reliably go back to unselected
744 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
746 (deffoo nnimap-request-expunge-group (group &optional server)
747 (when (nnimap-possibly-change-group group server)
748 (with-current-buffer (nnimap-buffer)
749 (car (nnimap-command "EXPUNGE")))))
751 (defun nnimap-get-flags (spec)
754 (with-current-buffer (nnimap-buffer)
756 (nnimap-wait-for-response (nnimap-send-command
757 "UID FETCH %s FLAGS" spec))
759 (subst-char-in-region (point-min) (point-max)
761 (goto-char (point-min))
762 (while (search-forward " FETCH " end t)
763 (setq elems (read (current-buffer)))
764 (push (cons (cadr (memq 'UID elems))
765 (cadr (memq 'FLAGS elems)))
767 (nreverse articles)))
769 (deffoo nnimap-close-group (group &optional server)
772 (deffoo nnimap-request-move-article (article group server accept-form
773 &optional last internal-move-group)
775 (mm-disable-multibyte)
776 (when (funcall (if internal-move-group
778 'nnimap-request-article)
779 article group server (current-buffer))
780 ;; If the move is internal (on the same server), just do it the easy
782 (let ((message-id (message-field-value "message-id")))
783 (if internal-move-group
785 (with-current-buffer (nnimap-buffer)
786 (nnimap-command "UID COPY %d %S"
788 (utf7-encode internal-move-group t)))))
790 (nnimap-delete-article article)
791 (cons internal-move-group
792 (or (nnimap-find-uid-response "COPYUID" (cadr result))
793 (nnimap-find-article-by-message-id
794 internal-move-group message-id)))))
795 ;; Move the article to a different method.
796 (let ((result (eval accept-form)))
798 (nnimap-delete-article article)
801 (deffoo nnimap-request-expire-articles (articles group &optional server force)
805 ((not (nnimap-possibly-change-group group server))
808 (eq nnmail-expiry-target 'delete))
809 (unless (nnimap-delete-article (gnus-compress-sequence articles))
810 (nnheader-message 7 "Article marked for deletion, but not expunged."))
813 (let ((deletable-articles
815 (eq nnmail-expiry-wait 'immediate))
817 (gnus-sorted-intersection
819 (nnimap-find-expired-articles group)))))
820 (if (null deletable-articles)
822 (if (eq nnmail-expiry-target 'delete)
823 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
824 (setq deletable-articles
825 (nnimap-process-expiry-targets
826 deletable-articles group server)))
827 ;; Return the articles we didn't delete.
828 (gnus-sorted-complement articles deletable-articles))))))
830 (defun nnimap-process-expiry-targets (articles group server)
831 (let ((deleted-articles nil))
833 ;; shortcut further processing if we're going to delete the articles
834 ((eq nnmail-expiry-target 'delete)
835 (setq deleted-articles articles)
837 ;; or just move them to another folder on the same IMAP server
838 ((and (not (functionp nnmail-expiry-target))
839 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
840 (gnus-server-to-method
841 (format "nnimap:%s" server))))
842 (and (nnimap-possibly-change-group group server)
843 (with-current-buffer (nnimap-buffer)
844 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
847 (nnimap-article-ranges (gnus-compress-sequence articles))
848 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
849 (setq deleted-articles articles)))
852 (dolist (article articles)
853 (let ((target nnmail-expiry-target))
855 (mm-disable-multibyte)
856 (when (nnimap-request-article article group server (current-buffer))
857 (nnheader-message 7 "Expiring article %s:%d" group article)
858 (when (functionp target)
859 (setq target (funcall target group)))
861 (not (eq target 'delete)))
862 (if (or (gnus-request-group target t)
863 (gnus-request-create-group target))
864 (nnmail-expiry-target-group target group)
867 (push article deleted-articles))))))))
868 ;; Change back to the current group again.
869 (nnimap-possibly-change-group group server)
870 (setq deleted-articles (nreverse deleted-articles))
871 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
874 (defun nnimap-find-expired-articles (group)
875 (let ((cutoff (nnmail-expired-article-p group nil nil)))
876 (with-current-buffer (nnimap-buffer)
879 "UID SEARCH SENTBEFORE %s"
883 (car (rassoc (nth 4 (decode-time cutoff))
884 parse-time-months))))
887 (delete 0 (mapcar #'string-to-number
888 (cdr (assoc "SEARCH" (cdr result))))))))))
891 (defun nnimap-find-article-by-message-id (group message-id)
892 (with-current-buffer (nnimap-buffer)
894 (unless (equal group (nnimap-group nnimap-object))
895 (setf (nnimap-group nnimap-object) nil)
896 (setf (nnimap-examined nnimap-object) group)
897 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
899 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
901 (setq result (nnimap-wait-for-response sequence))
903 (car (setq result (nnimap-parse-response))))
904 ;; Select the last instance of the message in the group.
906 (car (last (assoc "SEARCH" (cdr result)))))
907 (string-to-number article))))))
909 (defun nnimap-delete-article (articles)
910 (with-current-buffer (nnimap-buffer)
911 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
912 (nnimap-article-ranges articles))
914 ((nnimap-capability "UIDPLUS")
915 (nnimap-command "UID EXPUNGE %s"
916 (nnimap-article-ranges articles))
919 (nnimap-command "EXPUNGE")
921 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
922 "server doesn't support UIDPLUS, so we won't "
923 "delete this article now"))))))
925 (deffoo nnimap-request-scan (&optional group server)
926 (when (and (nnimap-possibly-change-group nil server)
928 nnimap-split-methods)
929 (nnheader-message 7 "nnimap %s splitting mail..." server)
930 (nnimap-split-incoming-mail)))
932 (defun nnimap-marks-to-flags (marks)
935 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
939 (deffoo nnimap-request-update-group-status (group status &optional server)
940 (when (nnimap-possibly-change-group nil server)
941 (let ((command (assoc
943 '((subscribe "SUBSCRIBE")
944 (unsubscribe "UNSUBSCRIBE")))))
946 (with-current-buffer (nnimap-buffer)
947 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
949 (deffoo nnimap-request-set-mark (group actions &optional server)
950 (when (nnimap-possibly-change-group group server)
952 (with-current-buffer (nnimap-buffer)
954 ;; Just send all the STORE commands without waiting for
955 ;; response. If they're successful, they're successful.
956 (dolist (action actions)
957 (destructuring-bind (range action marks) action
958 (let ((flags (nnimap-marks-to-flags marks)))
960 (setq sequence (nnimap-send-command
961 "UID STORE %s %sFLAGS.SILENT (%s)"
962 (nnimap-article-ranges range)
964 ((eq action 'del) "-")
965 ((eq action 'add) "+")
966 ((eq action 'set) ""))
967 (mapconcat #'identity flags " ")))))))
968 ;; Wait for the last command to complete to avoid later
969 ;; syncronisation problems with the stream.
971 (nnimap-wait-for-response sequence))))))
973 (deffoo nnimap-request-accept-article (group &optional server last)
974 (when (nnimap-possibly-change-group nil server)
975 (nnmail-check-syntax)
976 (let ((message-id (message-field-value "message-id"))
979 (setq message (buffer-substring-no-properties (point-min) (point-max)))
980 (with-current-buffer (nnimap-buffer)
981 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
983 ;; If we have this group open read-only, then unselect it
984 ;; before appending to it.
985 (when (equal (nnimap-examined nnimap-object) group)
986 (nnimap-unselect-group))
988 (setq sequence (nnimap-send-command
989 "APPEND %S {%d}" (utf7-encode group t)
991 (unless nnimap-streaming
992 (nnimap-wait-for-connection "^[+]"))
993 (process-send-string (get-buffer-process (current-buffer)) message)
994 (process-send-string (get-buffer-process (current-buffer))
995 (if (nnimap-newlinep nnimap-object)
998 (let ((result (nnimap-get-response sequence)))
999 (if (not (nnimap-ok-p result))
1001 (nnheader-report 'nnimap "%s" result)
1004 (or (nnimap-find-uid-response "APPENDUID" (car result))
1005 (nnimap-find-article-by-message-id
1006 group message-id))))))))))
1008 (defun nnimap-process-quirk (greeting-match type data)
1009 (when (and (nnimap-greeting nnimap-object)
1010 (string-match greeting-match (nnimap-greeting nnimap-object))
1012 (string-match "\000" data))
1013 (let ((choice (gnus-multiple-choice
1014 "Message contains NUL characters. Delete, continue, abort? "
1015 '((?d "Delete NUL characters")
1016 (?c "Try to APPEND the message as is")
1020 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1026 (goto-char (point-min))
1027 (while (search-forward "\000" nil t)
1028 (replace-match "" t t))
1029 (buffer-string)))))))
1031 (defun nnimap-ok-p (value)
1034 (equal (caar value) "OK")))
1036 (defun nnimap-find-uid-response (name list)
1037 (let ((result (car (last (nnimap-find-response-element name list)))))
1039 (string-to-number result))))
1041 (defun nnimap-find-response-element (name list)
1044 (when (and (consp elem)
1045 (equal name (car elem)))
1046 (setq result elem)))
1049 (deffoo nnimap-request-replace-article (article group buffer)
1051 (when (and (nnimap-possibly-change-group group nil)
1052 ;; Put the article into the group.
1053 (with-current-buffer buffer
1055 (nnimap-request-accept-article group nil t))))
1056 (nnimap-delete-article (list article))
1057 ;; Return the new article number.
1060 (defun nnimap-add-cr ()
1061 (goto-char (point-min))
1062 (while (re-search-forward "\r?\n" nil t)
1063 (replace-match "\r\n" t t)))
1065 (defun nnimap-get-groups ()
1067 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1069 (nnimap-wait-for-response sequence)
1070 (subst-char-in-region (point-min) (point-max)
1072 (goto-char (point-min))
1073 (nnimap-unfold-quoted-lines)
1074 (goto-char (point-min))
1075 (while (search-forward "* LIST " nil t)
1076 (let ((flags (read (current-buffer)))
1077 (separator (read (current-buffer)))
1078 (group (read (current-buffer))))
1079 (unless (member '%NoSelect flags)
1080 (push (if (stringp group)
1082 (format "%s" group))
1086 (deffoo nnimap-request-list (&optional server)
1087 (when (nnimap-possibly-change-group nil server)
1088 (with-current-buffer nntp-server-buffer
1091 (with-current-buffer (nnimap-buffer)
1092 (nnimap-get-groups)))
1093 sequences responses)
1095 (with-current-buffer (nnimap-buffer)
1096 (setf (nnimap-group nnimap-object) nil)
1097 (dolist (group groups)
1098 (setf (nnimap-examined nnimap-object) group)
1099 (push (list (nnimap-send-command "EXAMINE %S"
1100 (utf7-encode group t))
1103 (nnimap-wait-for-response (caar sequences))
1105 (nnimap-get-responses (mapcar #'car sequences))))
1106 (dolist (response responses)
1107 (let* ((sequence (car response))
1108 (response (cadr response))
1109 (group (cadr (assoc sequence sequences))))
1111 (equal (caar response) "OK"))
1112 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1114 (dolist (elem response)
1115 (when (equal (cadr elem) "EXISTS")
1116 (setq exists (string-to-number (car elem)))))
1118 (setq highest (1- (string-to-number (car uidnext)))))
1121 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1124 (insert (format "%S %d %d y\n"
1125 (utf7-decode group t)
1126 highest (1+ highest))))
1128 ;; Return the widest possible range.
1129 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1130 (or highest exists)))))))))
1133 (deffoo nnimap-request-newgroups (date &optional server)
1134 (when (nnimap-possibly-change-group nil server)
1135 (with-current-buffer nntp-server-buffer
1137 (dolist (group (with-current-buffer (nnimap-buffer)
1138 (nnimap-get-groups)))
1139 (unless (assoc group nnimap-current-infos)
1140 ;; Insert dummy numbers here -- they don't matter.
1141 (insert (format "%S 0 1 y\n" group))))
1144 (deffoo nnimap-retrieve-group-data-early (server infos)
1145 (when (nnimap-possibly-change-group nil server)
1146 (with-current-buffer (nnimap-buffer)
1148 (setf (nnimap-group nnimap-object) nil)
1149 (let ((qresyncp (nnimap-capability "QRESYNC"))
1150 params groups sequences active uidvalidity modseq group)
1151 ;; Go through the infos and gather the data needed to know
1152 ;; what and how to request the data.
1153 (dolist (info infos)
1154 (setq params (gnus-info-params info)
1155 group (gnus-group-real-name (gnus-info-group info))
1156 active (cdr (assq 'active params))
1157 uidvalidity (cdr (assq 'uidvalidity params))
1158 modseq (cdr (assq 'modseq params)))
1159 (setf (nnimap-examined nnimap-object) group)
1165 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1166 (utf7-encode group t)
1167 (nnimap-quirk "QRESYNC")
1173 (if (and active uidvalidity)
1174 ;; Fetch the last 100 flags.
1175 (max 1 (- (cdr active) 100))
1180 ;; If we don't have a UIDVALIDITY, then this is
1181 ;; the first time we've seen the group, so we
1182 ;; have to do a SELECT (which is slower than an
1183 ;; examine), but will tell us whether the group
1184 ;; is read-only or not.
1186 (push (list (nnimap-send-command "%s %S" command
1187 (utf7-encode group t))
1188 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1189 start group command)
1193 (defun nnimap-quirk (command)
1194 (let ((quirk (assoc command nnimap-quirks)))
1195 ;; If this server is of a type that matches a quirk, then return
1196 ;; the "quirked" command instead of the proper one.
1197 (if (or (null quirk)
1198 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1202 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1203 (when (and sequences
1204 (nnimap-possibly-change-group nil server))
1205 (with-current-buffer (nnimap-buffer)
1206 ;; Wait for the final data to trickle in.
1207 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1211 ;; Now we should have most of the data we need, no matter
1212 ;; whether we're QRESYNCING, fetching all the flags from
1213 ;; scratch, or just fetching the last 100 flags per group.
1214 (nnimap-update-infos (nnimap-flags-to-marks
1216 (nreverse sequences)))
1218 ;; Finally, just return something resembling an active file in
1219 ;; the nntp buffer, so that the agent can save the info, too.
1220 (with-current-buffer nntp-server-buffer
1222 (dolist (info infos)
1223 (let* ((group (gnus-info-group info))
1224 (active (gnus-active group)))
1226 (insert (format "%S %d %d y\n"
1227 (gnus-group-real-name group)
1229 (car active)))))))))))
1231 (defun nnimap-update-infos (flags infos)
1232 (dolist (info infos)
1233 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1234 (marks (cdr (assoc group flags))))
1236 (nnimap-update-info info marks)))))
1238 (defun nnimap-update-info (info marks)
1239 (destructuring-bind (existing flags high low uidnext start-article
1240 permanent-flags uidvalidity
1241 vanished highestmodseq) marks
1243 ;; Ignore groups with no UIDNEXT/marks. This happens for
1244 ;; completely empty groups.
1245 ((and (not existing)
1247 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1249 (gnus-set-active (gnus-info-group info) active))))
1250 ;; We have a mismatch between the old and new UIDVALIDITY
1251 ;; identifiers, so we have to re-request the group info (the next
1252 ;; time). This virtually never happens.
1253 ((let ((old-uidvalidity
1254 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1255 (and old-uidvalidity
1256 (not (equal old-uidvalidity uidvalidity))
1257 (> start-article 1)))
1258 (gnus-group-remove-parameter info 'uidvalidity)
1259 (gnus-group-remove-parameter info 'modseq))
1260 ;; We have the data needed to update.
1262 (let* ((group (gnus-info-group info))
1263 (completep (and start-article
1264 (= start-article 1)))
1265 (active (or (gnus-active group)
1266 (cdr (assq 'active (gnus-info-params info))))))
1268 (setq high (1- uidnext)))
1269 ;; First set the active ranges based on high/low.
1271 (not (gnus-active group)))
1272 (gnus-set-active group
1275 (cons (min (or low (car active))
1277 (max (or high (cdr active))
1282 ;; No articles in this group.
1283 (cons uidnext (1- uidnext)))
1285 (cons start-article (1- start-article)))
1287 ;; No articles and no uidnext.
1289 (gnus-set-active group
1291 (or high (1- uidnext)))))
1292 ;; See whether this is a read-only group.
1293 (unless (eq permanent-flags 'not-scanned)
1294 (gnus-group-set-parameter
1295 info 'permanent-flags
1296 (and (or (memq '%* permanent-flags)
1297 (memq '%Seen permanent-flags))
1299 ;; Update marks and read articles if this isn't a
1300 ;; read-only IMAP group.
1301 (when (setq permanent-flags
1302 (cdr (assq 'permanent-flags (gnus-info-params info))))
1303 (if (and highestmodseq
1304 (not start-article))
1305 ;; We've gotten the data by QRESYNCing.
1306 (nnimap-update-qresync-info
1307 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1308 ;; Do normal non-QRESYNC flag updates.
1309 ;; Update the list of read articles.
1311 (gnus-compress-sequence
1312 (gnus-set-difference
1313 (gnus-set-difference
1315 (cdr (assoc '%Seen flags)))
1316 (cdr (assoc '%Flagged flags)))))
1317 (read (gnus-range-difference
1318 (cons start-article high) unread)))
1319 (when (> start-article 1)
1322 (if (> start-article 1)
1323 (gnus-sorted-range-intersection
1324 (cons 1 (1- start-article))
1325 (gnus-info-read info))
1326 (gnus-info-read info))
1328 (when (or (not (listp permanent-flags))
1329 (memq '%Seen permanent-flags))
1330 (gnus-info-set-read info read))
1331 ;; Update the marks.
1332 (setq marks (gnus-info-marks info))
1333 (dolist (type (cdr nnimap-mark-alist))
1334 (when (or (not (listp permanent-flags))
1335 (memq (car (assoc (caddr type) flags))
1337 (memq '%* permanent-flags))
1338 (let ((old-marks (assoc (car type) marks))
1340 (gnus-compress-sequence
1341 (cdr (or (assoc (caddr type) flags) ; %Flagged
1342 (assoc (intern (cadr type) obarray) flags)
1343 (assoc (cadr type) flags)))))) ; "\Flagged"
1344 (setq marks (delq old-marks marks))
1346 (when (and old-marks
1347 (> start-article 1))
1348 (setq old-marks (gnus-range-difference
1350 (cons start-article high)))
1351 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1353 (push (cons (car type) new-marks) marks)))))
1354 (gnus-info-set-marks info marks t))))
1355 ;; Tell Gnus whether there are any \Recent messages in any of
1357 (let ((recent (cdr (assoc '%Recent flags))))
1360 (> (car (last recent)) (cdr active)))
1361 (push (list (cons (gnus-group-real-name group) 0))
1362 nnmail-split-history)))
1363 ;; Note the active level for the next run-through.
1364 (gnus-group-set-parameter info 'active (gnus-active group))
1365 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1366 (gnus-group-set-parameter info 'modseq highestmodseq)
1367 (nnimap-store-info info (gnus-active group)))))))
1369 (defun nnimap-update-qresync-info (info existing vanished flags)
1370 ;; Add all the vanished articles to the list of read articles.
1375 (gnus-range-add (gnus-info-read info)
1377 (cdr (assq '%Flagged flags)))
1378 (cdr (assq '%Seen flags))))
1379 (let ((marks (gnus-info-marks info)))
1380 (dolist (type (cdr nnimap-mark-alist))
1381 (let ((ticks (assoc (car type) marks))
1383 (cdr (or (assoc (caddr type) flags) ; %Flagged
1384 (assoc (intern (cadr type) obarray) flags)
1385 (assoc (cadr type) flags))))) ; "\Flagged"
1386 (setq marks (delq ticks marks))
1388 ;; Add the new marks we got.
1389 (setq ticks (gnus-add-to-range ticks new-marks))
1390 ;; Remove the marks from messages that don't have them.
1391 (setq ticks (gnus-remove-from-range
1393 (gnus-compress-sequence
1394 (gnus-sorted-complement existing new-marks))))
1396 (push (cons (car type) ticks) marks)))
1397 (gnus-info-set-marks info marks t))))
1399 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1400 (if (zerop (length irange))
1403 (dolist (elem (split-string irange ","))
1405 (if (string-match ":" elem)
1406 (let ((numbers (split-string elem ":")))
1407 (cons (string-to-number (car numbers))
1408 (string-to-number (cadr numbers))))
1409 (string-to-number elem))
1411 (nreverse result))))
1413 (defun nnimap-store-info (info active)
1414 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1415 (entry (assoc group nnimap-current-infos)))
1417 (setcdr entry (list info active))
1418 (push (list group info active) nnimap-current-infos))))
1420 (defun nnimap-flags-to-marks (groups)
1421 (let (data group totalp uidnext articles start-article mark permanent-flags
1422 uidvalidity vanished highestmodseq)
1423 (dolist (elem groups)
1424 (setq group (car elem)
1425 uidnext (nth 1 elem)
1426 start-article (nth 2 elem)
1427 permanent-flags (nth 3 elem)
1428 uidvalidity (nth 4 elem)
1429 vanished (nth 5 elem)
1430 highestmodseq (nth 6 elem)
1431 articles (nthcdr 7 elem))
1432 (let ((high (caar articles))
1434 (dolist (article articles)
1435 (setq low (car article))
1436 (push (car article) existing)
1437 (dolist (flag (cdr article))
1438 (setq mark (assoc flag marks))
1440 (push (list flag (car article)) marks)
1441 (setcdr mark (cons (car article) (cdr mark))))))
1442 (push (list group existing marks high low uidnext start-article
1443 permanent-flags uidvalidity vanished highestmodseq)
1447 (defun nnimap-parse-flags (sequences)
1448 (goto-char (point-min))
1449 ;; Change \Delete etc to %Delete, so that the reader can read it.
1450 (subst-char-in-region (point-min) (point-max)
1452 (let (start end articles groups uidnext elems permanent-flags
1453 uidvalidity vanished highestmodseq)
1454 (dolist (elem sequences)
1455 (destructuring-bind (group-sequence flag-sequence totalp group command)
1457 (setq start (point))
1459 ;; The EXAMINE was successful.
1460 (search-forward (format "\n%d OK " group-sequence) nil t)
1465 (setq permanent-flags
1466 (if (equal command "SELECT")
1467 (and (search-forward "PERMANENTFLAGS "
1468 (or end (point-min)) t)
1469 (read (current-buffer)))
1473 (and (search-forward "UIDNEXT "
1474 (or end (point-min)) t)
1475 (read (current-buffer))))
1478 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1479 (or end (point-min)) t)
1480 ;; Store UIDVALIDITY as a string, as it's
1481 ;; too big for 32-bit Emacsen, usually.
1485 (and (eq flag-sequence 'qresync)
1486 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1487 (or end (point-min)) t)
1491 (and (search-forward "HIGHESTMODSEQ "
1492 (or end (point-min)) t)
1493 (read (current-buffer))))
1496 ;; The UID FETCH FLAGS was successful.
1497 (or (eq flag-sequence 'qresync)
1498 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1499 (if (eq flag-sequence 'qresync)
1503 (setq start (point))
1505 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1507 ;; FIXME: For FETCH lines like "* 2971 FETCH (FLAGS (%Recent) UID
1508 ;; 12509 MODSEQ (13419098521433281274))" we get an
1509 ;; overflow-error. The handler simply deletes that large number
1510 ;; and reads again. But maybe there's a better fix...
1511 (setq elems (condition-case nil (read (current-buffer))
1513 ;; After an overflow-error, point is just after
1514 ;; the too large number. So delete it and try
1516 (delete-region (point) (progn (backward-word) (point)))
1518 (read (current-buffer)))))
1519 (push (cons (cadr (memq 'UID elems))
1520 (cadr (memq 'FLAGS elems)))
1522 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1523 vanished highestmodseq)
1527 (setq articles nil))))
1530 (defun nnimap-find-process-buffer (buffer)
1531 (cadr (assoc buffer nnimap-connection-alist)))
1533 (deffoo nnimap-request-post (&optional server)
1534 (setq nnimap-status-string "Read-only server")
1537 (declare-function gnus-fetch-headers "gnus-sum"
1538 (articles &optional limit force-new dependencies))
1540 (deffoo nnimap-request-thread (header)
1541 (let* ((id (mail-header-id header))
1543 (or (mail-header-references header)
1547 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1549 (dolist (refid refs value)
1551 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1552 refid refid value)))))
1553 (result (with-current-buffer (nnimap-buffer)
1554 (nnimap-command "UID SEARCH %s" cmd))))
1557 (and (car result) (delete 0 (mapcar #'string-to-number
1558 (cdr (assoc "SEARCH" (cdr result))))))
1561 (defun nnimap-possibly-change-group (group server)
1562 (let ((open-result t))
1564 (not (nnimap-server-opened server)))
1565 (setq open-result (nnimap-open-server server)))
1572 (with-current-buffer (nnimap-buffer)
1573 (if (equal group (nnimap-group nnimap-object))
1575 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1577 (setf (nnimap-group nnimap-object) group
1578 (nnimap-select-result nnimap-object) result)
1581 (defun nnimap-find-connection (buffer)
1582 "Find the connection delivering to BUFFER."
1583 (let ((entry (assoc buffer nnimap-connection-alist)))
1585 (if (and (buffer-name (cadr entry))
1586 (get-buffer-process (cadr entry))
1587 (memq (process-status (get-buffer-process (cadr entry)))
1589 (get-buffer-process (cadr entry))
1590 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1593 (defvar nnimap-sequence 0)
1595 (defun nnimap-send-command (&rest args)
1596 (setf (nnimap-last-command-time nnimap-object) (current-time))
1597 (process-send-string
1598 (get-buffer-process (current-buffer))
1601 (incf nnimap-sequence)
1602 (apply #'format args)
1603 (if (nnimap-newlinep nnimap-object)
1606 ;; Some servers apparently can't have many outstanding
1607 ;; commands, so throttle them.
1608 (unless nnimap-streaming
1609 (nnimap-wait-for-response nnimap-sequence))
1612 (defun nnimap-log-command (command)
1613 (with-current-buffer (get-buffer-create "*imap log*")
1614 (goto-char (point-max))
1615 (insert (format-time-string "%H:%M:%S") " "
1616 (if nnimap-inhibit-logging
1621 (defun nnimap-command (&rest args)
1623 (let* ((sequence (apply #'nnimap-send-command args))
1624 (response (nnimap-get-response sequence)))
1625 (if (equal (caar response) "OK")
1627 (nnheader-report 'nnimap "%s"
1628 (mapconcat (lambda (a)
1630 (car response) " "))
1633 (defun nnimap-get-response (sequence)
1634 (nnimap-wait-for-response sequence)
1635 (nnimap-parse-response))
1637 (defun nnimap-wait-for-connection (&optional regexp)
1638 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1640 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1641 (let ((process (get-buffer-process (current-buffer))))
1642 (goto-char (point-min))
1643 (while (and (memq (process-status process)
1645 (not (re-search-forward regexp nil t)))
1646 (nnheader-accept-process-output process)
1647 (goto-char (point-min)))
1649 (and (looking-at (or response-regexp regexp))
1652 (defun nnimap-wait-for-response (sequence &optional messagep)
1653 (let ((process (get-buffer-process (current-buffer)))
1657 (goto-char (point-max))
1658 (while (and (setq openp (memq (process-status process)
1661 ;; Skip past any "*" lines that the server has
1663 (while (and (not (bobp))
1666 (looking-at "\\*"))))
1667 (not (looking-at (format "%d .*\n" sequence)))))
1669 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1670 (nnheader-accept-process-output process)
1671 (goto-char (point-max)))
1674 ;; The user hit C-g while we were waiting: kill the process, in case
1675 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1677 (delete-process process)
1680 (defun nnimap-parse-response ()
1681 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1683 (dolist (line lines)
1684 (push (cdr (nnimap-parse-line line)) result))
1685 ;; Return the OK/error code first, and then all the "continuation
1686 ;; lines" afterwards.
1688 (nreverse result))))
1690 ;; Parse an IMAP response line lightly. They look like
1691 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1692 ;; the lines into a list of strings and lists of string.
1693 (defun nnimap-parse-line (line)
1696 (mm-disable-multibyte)
1698 (goto-char (point-min))
1700 (if (eql (setq char (following-char)) ? )
1708 (if (search-forward "]" (line-end-position) 'move)
1715 (if (search-forward ")" (line-end-position) 'move)
1722 (1- (or (search-forward "\"" (line-end-position) 'move)
1725 (buffer-substring (point) (if (search-forward " " nil t)
1727 (goto-char (point-max))))))
1729 (nreverse result))))
1731 (defun nnimap-last-response-string ()
1734 (let ((end (point)))
1738 (while (and (not (bobp))
1739 (eql (following-char) ?*))
1741 (unless (eql (following-char) ?*)
1743 (buffer-substring (point) end))))
1745 (defun nnimap-get-responses (sequences)
1747 (dolist (sequence sequences)
1748 (goto-char (point-min))
1749 (when (re-search-forward (format "^%d " sequence) nil t)
1750 (push (list sequence (nnimap-parse-response))
1754 (defvar nnimap-incoming-split-list nil)
1756 (defun nnimap-fetch-inbox (articles)
1758 (nnimap-wait-for-response
1759 (nnimap-send-command
1761 (nnimap-article-ranges articles)
1762 (format "(UID %s%s)"
1765 "BODY.PEEK[HEADER] BODY.PEEK"
1767 (if nnimap-split-download-body-default
1772 (defun nnimap-split-incoming-mail ()
1773 (with-current-buffer (nnimap-buffer)
1774 (let ((nnimap-incoming-split-list nil)
1775 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1776 nnmail-split-methods
1777 nnimap-split-methods))
1778 (nnmail-split-fancy (or nnimap-split-fancy
1779 nnmail-split-fancy))
1780 (nnmail-inhibit-default-split-group t)
1781 (groups (nnimap-get-groups))
1784 (nnimap-command "SELECT %S" nnimap-inbox)
1785 (setf (nnimap-group nnimap-object) nnimap-inbox)
1786 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1788 (nnimap-fetch-inbox new-articles)
1789 (nnimap-transform-split-mail)
1790 (nnheader-ms-strip-cr)
1792 (nnmail-split-incoming (current-buffer)
1793 #'nnimap-save-mail-spec
1795 #'nnimap-dummy-active-number
1796 #'nnimap-save-mail-spec)
1797 (when nnimap-incoming-split-list
1798 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1799 sequences junk-articles)
1800 ;; Create any groups that doesn't already exist on the
1802 (dolist (spec specs)
1803 (when (and (not (member (car spec) groups))
1804 (not (eq (car spec) 'junk)))
1805 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1806 ;; Then copy over all the messages.
1808 (dolist (spec specs)
1809 (let ((group (car spec))
1810 (ranges (cdr spec)))
1811 (if (eq group 'junk)
1812 (setq junk-articles ranges)
1813 (push (list (nnimap-send-command
1815 (nnimap-article-ranges ranges)
1816 (utf7-encode group t))
1819 ;; Wait for the last COPY response...
1821 (nnimap-wait-for-response (caar sequences))
1822 ;; And then mark the successful copy actions as deleted,
1823 ;; and possibly expunge them.
1824 (nnimap-mark-and-expunge-incoming
1825 (nnimap-parse-copied-articles sequences)))
1826 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1828 (defun nnimap-mark-and-expunge-incoming (range)
1830 (setq range (nnimap-article-ranges range))
1833 (nnimap-send-command
1834 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1836 ;; If the server supports it, we now delete the message we have
1837 ;; just copied over.
1838 ((nnimap-capability "UIDPLUS")
1839 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1840 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1841 ;; user has configured it.
1843 (setq sequence (nnimap-send-command "EXPUNGE"))))
1844 (nnimap-wait-for-response sequence))))
1846 (defun nnimap-parse-copied-articles (sequences)
1847 (let (sequence copied range)
1848 (goto-char (point-min))
1849 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
1850 (setq sequence (string-to-number (match-string 1)))
1851 (when (setq range (cadr (assq sequence sequences)))
1852 (push (gnus-uncompress-range range) copied)))
1853 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1855 (defun nnimap-new-articles (flags)
1857 (dolist (elem flags)
1858 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1860 (push (car elem) new)))
1861 (gnus-compress-sequence (nreverse new))))
1863 (defun nnimap-make-split-specs (list)
1867 (destructuring-bind (article spec) elem
1868 (dolist (group (delete nil (mapcar #'car spec)))
1869 (unless (setq entry (assoc group specs))
1870 (push (setq entry (list group)) specs))
1871 (setcdr entry (cons article (cdr entry))))))
1872 (dolist (entry specs)
1873 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1876 (defun nnimap-transform-split-mail ()
1877 (goto-char (point-min))
1878 (let (article bytes)
1881 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1882 (delete-region (point) (progn (forward-line 1) (point)))
1885 (setq article (match-string 1)
1886 bytes (nnimap-get-length))
1887 (delete-region (line-beginning-position) (line-end-position))
1888 ;; Insert MMDF separator, and a way to remember what this
1890 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1891 (forward-char (1+ bytes))
1892 (setq bytes (nnimap-get-length))
1893 (delete-region (line-beginning-position) (line-end-position))
1894 ;; There's a body; skip past that.
1896 (forward-char (1+ bytes))
1897 (delete-region (line-beginning-position) (line-end-position)))))))
1899 (defun nnimap-dummy-active-number (group &optional server)
1902 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1904 (goto-char (point-min))
1905 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1906 (error "Invalid nnimap mail")
1907 (setq article (string-to-number (match-string 1))))
1909 (if (eq group-art 'junk)
1910 (list (cons 'junk 1))
1912 nnimap-incoming-split-list)))
1916 ;;; nnimap.el ends here