1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
48 (require 'proto-stream)
53 (autoload 'auth-source-forget-user-or-password "auth-source")
54 (autoload 'auth-source-user-or-password "auth-source")
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-server-port nil
63 If nnimap-stream is `ssl', this will default to `imaps'. If not,
64 it will default to `imap'.")
66 (defvoo nnimap-stream 'ssl
67 "How nnimap will talk to the IMAP server.
68 Values are `ssl', `network', `starttls' or `shell'.")
70 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
71 (if (listp imap-shell-program)
72 (car imap-shell-program)
76 (defvoo nnimap-inbox nil
77 "The mail box where incoming mail arrives and should be split out of.")
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)
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 (defun nnimap-buffer ()
145 (nnimap-find-process-buffer nntp-server-buffer))
147 (defun nnimap-header-parameters ()
148 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
151 "BODY.PEEK[HEADER.FIELDS %s]"
152 "RFC822.HEADER.LINES %s")
153 (append '(Subject From Date Message-Id
154 References In-Reply-To Xref)
155 nnmail-extra-headers))))
157 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
158 (with-current-buffer nntp-server-buffer
160 (when (nnimap-possibly-change-group group server)
161 (with-current-buffer (nnimap-buffer)
163 (nnimap-wait-for-response
166 (nnimap-article-ranges (gnus-compress-sequence articles))
167 (nnimap-header-parameters))
169 (nnimap-transform-headers))
170 (insert-buffer-substring
171 (nnimap-find-process-buffer (current-buffer))))
174 (defun nnimap-transform-headers ()
175 (goto-char (point-min))
176 (let (article bytes lines size string)
179 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
180 (delete-region (point) (progn (forward-line 1) (point)))
183 (setq article (match-string 1))
184 ;; Unfold quoted {number} strings.
185 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
186 (1+ (line-end-position)) t)
187 (setq size (string-to-number (match-string 1)))
188 (delete-region (+ (match-beginning 0) 2) (point))
189 (setq string (buffer-substring (point) (+ (point) size)))
190 (delete-region (point) (+ (point) size))
191 (insert (format "%S" string)))
192 (setq bytes (nnimap-get-length)
196 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
201 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
202 (let ((structure (ignore-errors
203 (read (current-buffer)))))
204 (while (and (consp structure)
205 (not (stringp (car structure))))
206 (setq structure (car structure)))
207 (setq lines (nth 7 structure))))
208 (delete-region (line-beginning-position) (line-end-position))
209 (insert (format "211 %s Article retrieved." article))
212 (insert (format "Chars: %s\n" size)))
214 (insert (format "Lines: %s\n" lines)))
215 (unless (re-search-forward "^\r$" nil t)
216 (goto-char (point-max)))
217 (delete-region (line-beginning-position) (line-end-position))
221 (defun nnimap-unfold-quoted-lines ()
222 ;; Unfold quoted {number} strings.
224 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
225 (setq size (string-to-number (match-string 1)))
226 (delete-region (1+ (match-beginning 0)) (point))
227 (setq string (buffer-substring (point) (+ (point) size)))
228 (delete-region (point) (+ (point) size))
229 (insert (format "%S" string)))))
231 (defun nnimap-get-length ()
232 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
233 (string-to-number (match-string 1))))
235 (defun nnimap-article-ranges (ranges)
239 (number-to-string ranges))
240 ((numberp (cdr ranges))
241 (format "%d:%d" (car ranges) (cdr ranges)))
243 (dolist (elem ranges)
246 (format "%d:%d" (car elem) (cdr elem))
247 (number-to-string elem))
249 (mapconcat #'identity (nreverse result) ",")))))
251 (deffoo nnimap-open-server (server &optional defs)
252 (if (nnimap-server-opened server)
254 (unless (assq 'nnimap-address defs)
255 (setq defs (append defs (list (list 'nnimap-address server)))))
256 (nnoo-change-server 'nnimap server defs)
257 (or (nnimap-find-connection nntp-server-buffer)
258 (nnimap-open-connection nntp-server-buffer))))
260 (defun nnimap-make-process-buffer (buffer)
262 (generate-new-buffer (format "*nnimap %s %s %s*"
263 nnimap-address nnimap-server-port
264 (gnus-buffer-exists-p buffer)))
265 (mm-disable-multibyte)
266 (buffer-disable-undo)
268 (set (make-local-variable 'after-change-functions) nil)
269 (set (make-local-variable 'nnimap-object)
270 (make-nnimap :server (nnoo-current-server 'nnimap)))
271 (push (list buffer (current-buffer)) nnimap-connection-alist)
272 (push (current-buffer) nnimap-process-buffers)
275 (defun nnimap-credentials (address ports &optional inhibit-create)
276 (let (port credentials)
277 ;; Request the credentials from all ports, but only query on the
278 ;; last port if all the previous ones have failed.
279 (while (and (null credentials)
280 (setq port (pop ports)))
282 (auth-source-user-or-password
283 '("login" "password") address port nil
289 (defun nnimap-keepalive ()
290 (let ((now (current-time)))
291 (dolist (buffer nnimap-process-buffers)
292 (when (buffer-name buffer)
293 (with-current-buffer buffer
294 (when (and nnimap-object
295 (nnimap-last-command-time nnimap-object)
299 (nnimap-last-command-time nnimap-object)))
300 ;; More than five minutes since the last command.
302 (nnimap-send-command "NOOP")))))))
304 (defun nnimap-open-connection (buffer)
305 (unless nnimap-keepalive-timer
306 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
308 (with-current-buffer (nnimap-make-process-buffer buffer)
309 (let* ((coding-system-for-read 'binary)
310 (coding-system-for-write 'binary)
314 ((or (eq nnimap-stream 'network)
315 (eq nnimap-stream 'starttls))
316 (nnheader-message 7 "Opening connection to %s..."
319 ((eq nnimap-stream 'shell)
320 (nnheader-message 7 "Opening connection to %s via shell..."
323 ((memq nnimap-stream '(ssl tls))
324 (nnheader-message 7 "Opening connection to %s via tls..."
326 '("143" "993" "imap" "imaps"))
328 (error "Unknown stream type: %s" nnimap-stream))))
329 connection-result login-result credentials)
330 (destructuring-bind (stream greeting capabilities)
332 "*nnimap*" (current-buffer) nnimap-address (car (last ports))
334 :shell-command nnimap-shell-program
335 :capability-command "1 CAPABILITY\r\n"
337 (lambda (capabilities)
338 (if (not (string-match "STARTTLS" capabilities))
339 ;; Not a STARTTLS-capable server.
342 (setf (nnimap-process nnimap-object) stream)
344 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
345 nnimap-address port nnimap-stream)
346 (gnus-set-process-query-on-exit-flag stream nil)
347 (if (not (string-match "[*.] \\([A-Z0-9]+\\)" greeting))
348 (nnheader-report 'nnimap "%s" greeting)
349 ;; Store the greeting (for debugging purposes).
350 (setf (nnimap-greeting nnimap-object) greeting)
351 (setf (nnimap-capabilities nnimap-object)
353 (split-string capabilities)))
354 (when nnimap-server-port
355 (push (format "%s" nnimap-server-port) ports))
356 (unless (equal connection-result "PREAUTH")
357 (if (not (setq credentials
358 (if (eq nnimap-authenticator 'anonymous)
360 (message-make-address))
362 ;; First look for the credentials based
363 ;; on the virtual server name.
365 (nnoo-current-server 'nnimap) ports t)
366 ;; Then look them up based on the
368 (nnimap-credentials nnimap-address ports)))))
369 (setq nnimap-object nil)
371 (if (and (nnimap-capability "AUTH=PLAIN")
372 (nnimap-capability "LOGINDISABLED"))
374 "AUTHENTICATE PLAIN %s"
375 (base64-encode-string
376 (format "\000%s\000%s"
377 (nnimap-quote-specials (car credentials))
378 (nnimap-quote-specials (cadr credentials)))))
379 (nnimap-command "LOGIN %S %S"
381 (cadr credentials))))
382 (unless (car login-result)
383 ;; If the login failed, then forget the credentials
384 ;; that are now possibly cached.
385 (dolist (host (list (nnoo-current-server 'nnimap)
388 (dolist (element '("login" "password"))
389 (auth-source-forget-user-or-password
390 element host port))))
391 (delete-process (nnimap-process nnimap-object))
392 (setq nnimap-object nil))))
394 (when (nnimap-capability "QRESYNC")
395 (nnimap-command "ENABLE QRESYNC"))
396 (nnimap-process nnimap-object))))))))
398 (defun nnimap-quote-specials (string)
401 (goto-char (point-min))
402 (while (re-search-forward "[\\\"]" nil t)
408 (defun nnimap-find-parameter (parameter elems)
412 ((equal (car elem) parameter)
413 (setq result (cdr elem)))
414 ((and (equal (car elem) "OK")
416 (equal (caadr elem) parameter))
417 (setq result (cdr (cadr elem))))))
420 (deffoo nnimap-close-server (&optional server)
421 (when (nnoo-change-server 'nnimap server nil)
423 (delete-process (get-buffer-process (nnimap-buffer))))
424 (nnoo-close-server 'nnimap server)
427 (deffoo nnimap-request-close ()
430 (deffoo nnimap-server-opened (&optional server)
431 (and (nnoo-current-server-p 'nnimap server)
433 (gnus-buffer-live-p nntp-server-buffer)
434 (nnimap-find-connection nntp-server-buffer)))
436 (deffoo nnimap-status-message (&optional server)
437 nnimap-status-string)
439 (deffoo nnimap-request-article (article &optional group server to-buffer)
440 (with-current-buffer nntp-server-buffer
441 (let ((result (nnimap-possibly-change-group group server))
443 (when (stringp article)
444 (setq article (nnimap-find-article-by-message-id group article)))
448 (with-current-buffer (nnimap-buffer)
450 (when nnimap-fetch-partial-articles
451 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
452 (goto-char (point-min))
453 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
454 (setq structure (ignore-errors
455 (let ((start (point)))
457 (downcase-region start (point))
459 (read (current-buffer))))
460 parts (nnimap-find-wanted-parts structure))))
462 (nnimap-get-partial-article article parts structure)
463 (nnimap-get-whole-article article))
464 (let ((buffer (current-buffer)))
465 (with-current-buffer (or to-buffer nntp-server-buffer)
467 (insert-buffer-substring buffer)
468 (nnheader-ms-strip-cr)
469 (cons group article)))))))))
471 (deffoo nnimap-request-head (article &optional group server to-buffer)
472 (when (nnimap-possibly-change-group group server)
473 (with-current-buffer (nnimap-buffer)
474 (when (stringp article)
475 (setq article (nnimap-find-article-by-message-id group article)))
476 (nnimap-get-whole-article
477 article (format "UID FETCH %%d %s"
478 (nnimap-header-parameters)))
479 (let ((buffer (current-buffer)))
480 (with-current-buffer (or to-buffer nntp-server-buffer)
482 (insert-buffer-substring buffer)
483 (nnheader-ms-strip-cr)
484 (cons group article))))))
486 (defun nnimap-get-whole-article (article &optional command)
491 "UID FETCH %d BODY.PEEK[]"
492 "UID FETCH %d RFC822.PEEK"))
494 ;; Check that we really got an article.
495 (goto-char (point-min))
496 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
499 ;; Remove any data that may have arrived before the FETCH data.
502 (delete-region (point-min) (point)))
503 (let ((bytes (nnimap-get-length)))
504 (delete-region (line-beginning-position)
505 (progn (forward-line 1) (point)))
506 (goto-char (+ (point) bytes))
507 (delete-region (point) (point-max)))
510 (defun nnimap-capability (capability)
511 (member capability (nnimap-capabilities nnimap-object)))
513 (defun nnimap-ver4-p ()
514 (nnimap-capability "IMAP4REV1"))
516 (defun nnimap-get-partial-article (article parts structure)
519 "UID FETCH %d (%s %s)"
525 (mapconcat (lambda (part)
526 (format "BODY.PEEK[%s]" part))
528 (mapconcat (lambda (part)
529 (format "RFC822.PEEK[%s]" part))
532 (nnimap-convert-partial-article structure))))
534 (defun nnimap-convert-partial-article (structure)
535 ;; First just skip past the headers.
536 (goto-char (point-min))
537 (let ((bytes (nnimap-get-length))
539 ;; Delete "FETCH" line.
540 (delete-region (line-beginning-position)
541 (progn (forward-line 1) (point)))
542 (goto-char (+ (point) bytes))
543 ;; Collect all the body parts.
544 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
545 (setq id (match-string 1)
546 bytes (nnimap-get-length))
548 (delete-region (point) (progn (forward-line 1) (point)))
549 (push (list id (buffer-substring (point) (+ (point) bytes)))
551 (delete-region (point) (+ (point) bytes)))
552 ;; Delete trailing junk.
553 (delete-region (point) (point-max))
554 ;; Now insert all the parts again where they fit in the structure.
555 (nnimap-insert-partial-structure structure parts)
558 (defun nnimap-insert-partial-structure (structure parts &optional subp)
560 (let ((bstruc structure))
561 (while (consp (car bstruc))
563 (setq type (car bstruc))
564 (setq bstruc (car (cdr bstruc)))
565 (let ((has-boundary (member "boundary" bstruc)))
567 (setq boundary (cadr has-boundary)))))
569 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
570 (downcase type) boundary)))
571 (while (not (stringp (car structure)))
572 (insert "\n--" boundary "\n")
573 (if (consp (caar structure))
574 (nnimap-insert-partial-structure (pop structure) parts t)
575 (let ((bit (pop structure)))
576 (insert (format "Content-type: %s/%s"
577 (downcase (nth 0 bit))
578 (downcase (nth 1 bit))))
579 (if (member "CHARSET" (nth 2 bit))
581 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
583 (insert (format "Content-transfer-encoding: %s\n"
586 (when (assoc (nth 9 bit) parts)
587 (insert (cadr (assoc (nth 9 bit) parts)))))))
588 (insert "\n--" boundary "--\n")))
590 (defun nnimap-find-wanted-parts (structure)
591 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
593 (defun nnimap-find-wanted-parts-1 (structure prefix)
596 (while (consp (car structure))
597 (let ((sub (pop structure)))
598 (if (consp (car sub))
599 (push (nnimap-find-wanted-parts-1
600 sub (if (string= prefix "")
601 (number-to-string num)
602 (format "%s.%s" prefix num)))
604 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
605 (id (if (string= prefix "")
606 (number-to-string num)
607 (format "%s.%s" prefix num))))
608 (setcar (nthcdr 9 sub) id)
609 (when (if (eq nnimap-fetch-partial-articles t)
611 (string-match nnimap-fetch-partial-articles type))
616 (deffoo nnimap-request-group (group &optional server dont-check info)
617 (let ((result (nnimap-possibly-change-group
618 ;; Don't SELECT the group if we're going to select it
621 (assoc group nnimap-current-infos))
625 articles active marks high low)
626 (with-current-buffer nntp-server-buffer
629 (setq active (nth 2 (assoc group nnimap-current-infos))))
630 (insert (format "211 %d %d %d %S\n"
631 (- (cdr active) (car active))
635 (with-current-buffer (nnimap-buffer)
637 (let ((group-sequence
638 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
640 (nnimap-send-command "UID FETCH 1:* FLAGS")))
641 (setf (nnimap-group nnimap-object) group)
642 (nnimap-wait-for-response flag-sequence)
644 (nnimap-flags-to-marks
646 (list (list group-sequence flag-sequence
647 1 group "SELECT")))))
650 (nnimap-update-infos marks (list info))
651 (nnimap-store-info info (gnus-active (gnus-info-group info))))
652 (goto-char (point-max))
653 (let ((uidnext (nth 5 (car marks))))
654 (setq high (or (if uidnext
658 low (or (nth 4 (car marks)) uidnext 1)))))
662 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
665 (deffoo nnimap-request-create-group (group &optional server args)
666 (when (nnimap-possibly-change-group nil server)
667 (with-current-buffer (nnimap-buffer)
668 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
670 (deffoo nnimap-request-delete-group (group &optional force server)
671 (when (nnimap-possibly-change-group nil server)
672 (with-current-buffer (nnimap-buffer)
673 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
675 (deffoo nnimap-request-rename-group (group new-name &optional server)
676 (when (nnimap-possibly-change-group nil server)
677 (with-current-buffer (nnimap-buffer)
678 (nnimap-unselect-group)
679 (car (nnimap-command "RENAME %S %S"
680 (utf7-encode group t) (utf7-encode new-name t))))))
682 (defun nnimap-unselect-group ()
683 ;; Make sure we don't have this group open read/write by asking
684 ;; to examine a mailbox that doesn't exist. This seems to be
685 ;; the only way that allows us to reliably go back to unselected
687 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
689 (deffoo nnimap-request-expunge-group (group &optional server)
690 (when (nnimap-possibly-change-group group server)
691 (with-current-buffer (nnimap-buffer)
692 (car (nnimap-command "EXPUNGE")))))
694 (defun nnimap-get-flags (spec)
697 (with-current-buffer (nnimap-buffer)
699 (nnimap-wait-for-response (nnimap-send-command
700 "UID FETCH %s FLAGS" spec))
702 (subst-char-in-region (point-min) (point-max)
704 (goto-char (point-min))
705 (while (search-forward " FETCH " end t)
706 (setq elems (read (current-buffer)))
707 (push (cons (cadr (memq 'UID elems))
708 (cadr (memq 'FLAGS elems)))
710 (nreverse articles)))
712 (deffoo nnimap-close-group (group &optional server)
715 (deffoo nnimap-request-move-article (article group server accept-form
716 &optional last internal-move-group)
718 (mm-disable-multibyte)
719 (when (funcall (if internal-move-group
721 'nnimap-request-article)
722 article group server (current-buffer))
723 ;; If the move is internal (on the same server), just do it the easy
725 (let ((message-id (message-field-value "message-id")))
726 (if internal-move-group
728 (with-current-buffer (nnimap-buffer)
729 (nnimap-command "UID COPY %d %S"
731 (utf7-encode internal-move-group t)))))
733 (nnimap-delete-article article)
734 (cons internal-move-group
735 (or (nnimap-find-uid-response "COPYUID" (cadr result))
736 (nnimap-find-article-by-message-id
737 internal-move-group message-id)))))
738 ;; Move the article to a different method.
739 (let ((result (eval accept-form)))
741 (nnimap-delete-article article)
744 (deffoo nnimap-request-expire-articles (articles group &optional server force)
748 ((not (nnimap-possibly-change-group group server))
751 (eq nnmail-expiry-target 'delete))
752 (unless (nnimap-delete-article (gnus-compress-sequence articles))
753 (nnheader-message 7 "Article marked for deletion, but not expunged."))
756 (let ((deletable-articles
758 (eq nnmail-expiry-wait 'immediate))
760 (gnus-sorted-intersection
762 (nnimap-find-expired-articles group)))))
763 (if (null deletable-articles)
765 (if (eq nnmail-expiry-target 'delete)
766 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
767 (setq deletable-articles
768 (nnimap-process-expiry-targets
769 deletable-articles group server)))
770 ;; Return the articles we didn't delete.
771 (gnus-sorted-complement articles deletable-articles))))))
773 (defun nnimap-process-expiry-targets (articles group server)
774 (let ((deleted-articles nil))
775 (dolist (article articles)
776 (let ((target nnmail-expiry-target))
778 (mm-disable-multibyte)
779 (when (nnimap-request-article article group server (current-buffer))
780 (nnheader-message 7 "Expiring article %s:%d" group article)
781 (when (functionp target)
782 (setq target (funcall target group)))
784 (not (eq target 'delete)))
785 (if (or (gnus-request-group target t)
786 (gnus-request-create-group target))
787 (nnmail-expiry-target-group target group)
790 (push article deleted-articles))))))
791 ;; Change back to the current group again.
792 (nnimap-possibly-change-group group server)
793 (setq deleted-articles (nreverse deleted-articles))
794 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
797 (defun nnimap-find-expired-articles (group)
798 (let ((cutoff (nnmail-expired-article-p group nil nil)))
799 (with-current-buffer (nnimap-buffer)
802 "UID SEARCH SENTBEFORE %s"
806 (car (rassoc (nth 4 (decode-time cutoff))
807 parse-time-months))))
810 (delete 0 (mapcar #'string-to-number
811 (cdr (assoc "SEARCH" (cdr result))))))))))
814 (defun nnimap-find-article-by-message-id (group message-id)
815 (with-current-buffer (nnimap-buffer)
817 (unless (equal group (nnimap-group nnimap-object))
818 (setf (nnimap-group nnimap-object) nil)
819 (setf (nnimap-examined nnimap-object) group)
820 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
822 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
824 (setq result (nnimap-wait-for-response sequence))
826 (car (setq result (nnimap-parse-response))))
827 ;; Select the last instance of the message in the group.
829 (car (last (assoc "SEARCH" (cdr result)))))
830 (string-to-number article))))))
832 (defun nnimap-delete-article (articles)
833 (with-current-buffer (nnimap-buffer)
834 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
835 (nnimap-article-ranges articles))
837 ((nnimap-capability "UIDPLUS")
838 (nnimap-command "UID EXPUNGE %s"
839 (nnimap-article-ranges articles))
842 (nnimap-command "EXPUNGE")
844 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
845 "server doesn't support UIDPLUS, so we won't "
846 "delete this article now"))))))
848 (deffoo nnimap-request-scan (&optional group server)
849 (when (and (nnimap-possibly-change-group nil server)
851 nnimap-split-methods)
852 (nnheader-message 7 "nnimap %s splitting mail..." server)
853 (nnimap-split-incoming-mail)))
855 (defun nnimap-marks-to-flags (marks)
858 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
862 (deffoo nnimap-request-set-mark (group actions &optional server)
863 (when (nnimap-possibly-change-group group server)
865 (with-current-buffer (nnimap-buffer)
867 ;; Just send all the STORE commands without waiting for
868 ;; response. If they're successful, they're successful.
869 (dolist (action actions)
870 (destructuring-bind (range action marks) action
871 (let ((flags (nnimap-marks-to-flags marks)))
873 (setq sequence (nnimap-send-command
874 "UID STORE %s %sFLAGS.SILENT (%s)"
875 (nnimap-article-ranges range)
877 ((eq action 'del) "-")
878 ((eq action 'add) "+")
879 ((eq action 'set) ""))
880 (mapconcat #'identity flags " ")))))))
881 ;; Wait for the last command to complete to avoid later
882 ;; syncronisation problems with the stream.
884 (nnimap-wait-for-response sequence))))))
886 (deffoo nnimap-request-accept-article (group &optional server last)
887 (when (nnimap-possibly-change-group nil server)
888 (nnmail-check-syntax)
889 (let ((message-id (message-field-value "message-id"))
892 (setq message (buffer-substring-no-properties (point-min) (point-max)))
893 (with-current-buffer (nnimap-buffer)
894 ;; If we have this group open read-only, then unselect it
895 ;; before appending to it.
896 (when (equal (nnimap-examined nnimap-object) group)
897 (nnimap-unselect-group))
899 (setq sequence (nnimap-send-command
900 "APPEND %S {%d}" (utf7-encode group t)
902 (unless nnimap-streaming
903 (nnimap-wait-for-connection "^[+]"))
904 (process-send-string (get-buffer-process (current-buffer)) message)
905 (process-send-string (get-buffer-process (current-buffer))
906 (if (nnimap-newlinep nnimap-object)
909 (let ((result (nnimap-get-response sequence)))
910 (if (not (car result))
912 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
915 (or (nnimap-find-uid-response "APPENDUID" (car result))
916 (nnimap-find-article-by-message-id
917 group message-id)))))))))
919 (defun nnimap-find-uid-response (name list)
920 (let ((result (car (last (nnimap-find-response-element name list)))))
922 (string-to-number result))))
924 (defun nnimap-find-response-element (name list)
927 (when (and (consp elem)
928 (equal name (car elem)))
932 (deffoo nnimap-request-replace-article (article group buffer)
934 (when (and (nnimap-possibly-change-group group nil)
935 ;; Put the article into the group.
936 (with-current-buffer buffer
938 (nnimap-request-accept-article group nil t))))
939 (nnimap-delete-article (list article))
940 ;; Return the new article number.
943 (defun nnimap-add-cr ()
944 (goto-char (point-min))
945 (while (re-search-forward "\r?\n" nil t)
946 (replace-match "\r\n" t t)))
948 (defun nnimap-get-groups ()
950 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
952 (nnimap-wait-for-response sequence)
953 (subst-char-in-region (point-min) (point-max)
955 (goto-char (point-min))
956 (nnimap-unfold-quoted-lines)
957 (goto-char (point-min))
958 (while (search-forward "* LIST " nil t)
959 (let ((flags (read (current-buffer)))
960 (separator (read (current-buffer)))
961 (group (read (current-buffer))))
962 (unless (member '%NoSelect flags)
963 (push (if (stringp group)
969 (deffoo nnimap-request-list (&optional server)
970 (nnimap-possibly-change-group nil server)
971 (with-current-buffer nntp-server-buffer
974 (with-current-buffer (nnimap-buffer)
975 (nnimap-get-groups)))
978 (with-current-buffer (nnimap-buffer)
979 (setf (nnimap-group nnimap-object) nil)
980 (dolist (group groups)
981 (setf (nnimap-examined nnimap-object) group)
982 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
985 (nnimap-wait-for-response (caar sequences))
987 (nnimap-get-responses (mapcar #'car sequences))))
988 (dolist (response responses)
989 (let* ((sequence (car response))
990 (response (cadr response))
991 (group (cadr (assoc sequence sequences))))
993 (equal (caar response) "OK"))
994 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
996 (dolist (elem response)
997 (when (equal (cadr elem) "EXISTS")
998 (setq exists (string-to-number (car elem)))))
1000 (setq highest (1- (string-to-number (car uidnext)))))
1003 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1006 (insert (format "%S %d %d y\n"
1007 (utf7-decode group t) highest (1+ highest))))
1009 ;; Return the widest possible range.
1010 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1011 (or highest exists)))))))))
1014 (deffoo nnimap-request-newgroups (date &optional server)
1015 (nnimap-possibly-change-group nil server)
1016 (with-current-buffer nntp-server-buffer
1018 (dolist (group (with-current-buffer (nnimap-buffer)
1019 (nnimap-get-groups)))
1020 (unless (assoc group nnimap-current-infos)
1021 ;; Insert dummy numbers here -- they don't matter.
1022 (insert (format "%S 0 1 y\n" group))))
1025 (deffoo nnimap-retrieve-group-data-early (server infos)
1026 (when (nnimap-possibly-change-group nil server)
1027 (with-current-buffer (nnimap-buffer)
1029 (setf (nnimap-group nnimap-object) nil)
1030 (let ((qresyncp (nnimap-capability "QRESYNC"))
1031 params groups sequences active uidvalidity modseq group)
1032 ;; Go through the infos and gather the data needed to know
1033 ;; what and how to request the data.
1034 (dolist (info infos)
1035 (setq params (gnus-info-params info)
1036 group (gnus-group-real-name (gnus-info-group info))
1037 active (cdr (assq 'active params))
1038 uidvalidity (cdr (assq 'uidvalidity params))
1039 modseq (cdr (assq 'modseq params)))
1040 (setf (nnimap-examined nnimap-object) group)
1045 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1046 (utf7-encode group t)
1052 (if (and active uidvalidity)
1053 ;; Fetch the last 100 flags.
1054 (max 1 (- (cdr active) 100))
1059 ;; If we don't have a UIDVALIDITY, then this is
1060 ;; the first time we've seen the group, so we
1061 ;; have to do a SELECT (which is slower than an
1062 ;; examine), but will tell us whether the group
1063 ;; is read-only or not.
1065 (push (list (nnimap-send-command "%s %S" command
1066 (utf7-encode group t))
1067 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1068 start group command)
1072 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1073 (when (and sequences
1074 (nnimap-possibly-change-group nil server))
1075 (with-current-buffer (nnimap-buffer)
1076 ;; Wait for the final data to trickle in.
1077 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1081 ;; Now we should have most of the data we need, no matter
1082 ;; whether we're QRESYNCING, fetching all the flags from
1083 ;; scratch, or just fetching the last 100 flags per group.
1084 (nnimap-update-infos (nnimap-flags-to-marks
1086 (nreverse sequences)))
1088 ;; Finally, just return something resembling an active file in
1089 ;; the nntp buffer, so that the agent can save the info, too.
1090 (with-current-buffer nntp-server-buffer
1092 (dolist (info infos)
1093 (let* ((group (gnus-info-group info))
1094 (active (gnus-active group)))
1096 (insert (format "%S %d %d y\n"
1097 (gnus-group-real-name group)
1099 (car active)))))))))))
1101 (defun nnimap-update-infos (flags infos)
1102 (dolist (info infos)
1103 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1104 (marks (cdr (assoc group flags))))
1106 (nnimap-update-info info marks)))))
1108 (defun nnimap-update-info (info marks)
1109 (destructuring-bind (existing flags high low uidnext start-article
1110 permanent-flags uidvalidity
1111 vanished highestmodseq) marks
1113 ;; Ignore groups with no UIDNEXT/marks. This happens for
1114 ;; completely empty groups.
1115 ((and (not existing)
1117 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1119 (gnus-set-active (gnus-info-group info) active))))
1120 ;; We have a mismatch between the old and new UIDVALIDITY
1121 ;; identifiers, so we have to re-request the group info (the next
1122 ;; time). This virtually never happens.
1123 ((let ((old-uidvalidity
1124 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1125 (and old-uidvalidity
1126 (not (equal old-uidvalidity uidvalidity))
1127 (> start-article 1)))
1128 (gnus-group-remove-parameter info 'uidvalidity)
1129 (gnus-group-remove-parameter info 'modseq))
1130 ;; We have the data needed to update.
1132 (let* ((group (gnus-info-group info))
1133 (completep (and start-article
1134 (= start-article 1)))
1135 (active (or (gnus-active group)
1136 (cdr (assq 'active (gnus-info-params info))))))
1138 (setq high (1- uidnext)))
1139 ;; First set the active ranges based on high/low.
1141 (not (gnus-active group)))
1142 (gnus-set-active group
1145 (cons (min (or low (car active))
1147 (max (or high (cdr active))
1152 ;; No articles in this group.
1153 (cons uidnext (1- uidnext)))
1155 (cons start-article (1- start-article)))
1157 ;; No articles and no uidnext.
1162 (or high (1- uidnext)))))
1163 ;; See whether this is a read-only group.
1164 (unless (eq permanent-flags 'not-scanned)
1165 (gnus-group-set-parameter
1166 info 'permanent-flags
1167 (and (or (memq '%* permanent-flags)
1168 (memq '%Seen permanent-flags))
1170 ;; Update marks and read articles if this isn't a
1171 ;; read-only IMAP group.
1172 (when (setq permanent-flags
1173 (cdr (assq 'permanent-flags (gnus-info-params info))))
1174 (if (and highestmodseq
1175 (not start-article))
1176 ;; We've gotten the data by QRESYNCing.
1177 (nnimap-update-qresync-info
1178 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1179 ;; Do normal non-QRESYNC flag updates.
1180 ;; Update the list of read articles.
1182 (gnus-compress-sequence
1183 (gnus-set-difference
1184 (gnus-set-difference
1186 (cdr (assoc '%Seen flags)))
1187 (cdr (assoc '%Flagged flags)))))
1188 (read (gnus-range-difference
1189 (cons start-article high) unread)))
1190 (when (> start-article 1)
1193 (if (> start-article 1)
1194 (gnus-sorted-range-intersection
1195 (cons 1 (1- start-article))
1196 (gnus-info-read info))
1197 (gnus-info-read info))
1199 (when (or (not (listp permanent-flags))
1200 (memq '%Seen permanent-flags))
1201 (gnus-info-set-read info read))
1202 ;; Update the marks.
1203 (setq marks (gnus-info-marks info))
1204 (dolist (type (cdr nnimap-mark-alist))
1205 (when (or (not (listp permanent-flags))
1206 (memq (car (assoc (caddr type) flags))
1208 (memq '%* permanent-flags))
1209 (let ((old-marks (assoc (car type) marks))
1211 (gnus-compress-sequence
1212 (cdr (or (assoc (caddr type) flags) ; %Flagged
1213 (assoc (intern (cadr type) obarray) flags)
1214 (assoc (cadr type) flags)))))) ; "\Flagged"
1215 (setq marks (delq old-marks marks))
1217 (when (and old-marks
1218 (> start-article 1))
1219 (setq old-marks (gnus-range-difference
1221 (cons start-article high)))
1222 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1224 (push (cons (car type) new-marks) marks)))))
1225 (gnus-info-set-marks info marks t))))
1226 ;; Note the active level for the next run-through.
1227 (gnus-group-set-parameter info 'active (gnus-active group))
1228 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1229 (gnus-group-set-parameter info 'modseq highestmodseq)
1230 (nnimap-store-info info (gnus-active group)))))))
1232 (defun nnimap-update-qresync-info (info existing vanished flags)
1233 ;; Add all the vanished articles to the list of read articles.
1238 (gnus-range-add (gnus-info-read info)
1240 (cdr (assq '%Flagged flags)))
1241 (cdr (assq '%Seen flags))))
1242 (let ((marks (gnus-info-marks info)))
1243 (dolist (type (cdr nnimap-mark-alist))
1244 (let ((ticks (assoc (car type) marks))
1246 (cdr (or (assoc (caddr type) flags) ; %Flagged
1247 (assoc (intern (cadr type) obarray) flags)
1248 (assoc (cadr type) flags))))) ; "\Flagged"
1249 (setq marks (delq ticks marks))
1251 ;; Add the new marks we got.
1252 (setq ticks (gnus-add-to-range ticks new-marks))
1253 ;; Remove the marks from messages that don't have them.
1254 (setq ticks (gnus-remove-from-range
1256 (gnus-compress-sequence
1257 (gnus-sorted-complement existing new-marks))))
1259 (push (cons (car type) ticks) marks)))
1260 (gnus-info-set-marks info marks t))))
1262 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1263 (if (zerop (length irange))
1266 (dolist (elem (split-string irange ","))
1268 (if (string-match ":" elem)
1269 (let ((numbers (split-string elem ":")))
1270 (cons (string-to-number (car numbers))
1271 (string-to-number (cadr numbers))))
1272 (string-to-number elem))
1274 (nreverse result))))
1276 (defun nnimap-store-info (info active)
1277 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1278 (entry (assoc group nnimap-current-infos)))
1280 (setcdr entry (list info active))
1281 (push (list group info active) nnimap-current-infos))))
1283 (defun nnimap-flags-to-marks (groups)
1284 (let (data group totalp uidnext articles start-article mark permanent-flags
1285 uidvalidity vanished highestmodseq)
1286 (dolist (elem groups)
1287 (setq group (car elem)
1288 uidnext (nth 1 elem)
1289 start-article (nth 2 elem)
1290 permanent-flags (nth 3 elem)
1291 uidvalidity (nth 4 elem)
1292 vanished (nth 5 elem)
1293 highestmodseq (nth 6 elem)
1294 articles (nthcdr 7 elem))
1295 (let ((high (caar articles))
1297 (dolist (article articles)
1298 (setq low (car article))
1299 (push (car article) existing)
1300 (dolist (flag (cdr article))
1301 (setq mark (assoc flag marks))
1303 (push (list flag (car article)) marks)
1304 (setcdr mark (cons (car article) (cdr mark))))))
1305 (push (list group existing marks high low uidnext start-article
1306 permanent-flags uidvalidity vanished highestmodseq)
1310 (defun nnimap-parse-flags (sequences)
1311 (goto-char (point-min))
1312 ;; Change \Delete etc to %Delete, so that the reader can read it.
1313 (subst-char-in-region (point-min) (point-max)
1315 (let (start end articles groups uidnext elems permanent-flags
1316 uidvalidity vanished highestmodseq)
1317 (dolist (elem sequences)
1318 (destructuring-bind (group-sequence flag-sequence totalp group command)
1320 (setq start (point))
1322 ;; The EXAMINE was successful.
1323 (search-forward (format "\n%d OK " group-sequence) nil t)
1328 (setq permanent-flags
1329 (if (equal command "SELECT")
1330 (and (search-forward "PERMANENTFLAGS "
1331 (or end (point-min)) t)
1332 (read (current-buffer)))
1336 (and (search-forward "UIDNEXT "
1337 (or end (point-min)) t)
1338 (read (current-buffer))))
1341 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1342 (or end (point-min)) t)
1343 ;; Store UIDVALIDITY as a string, as it's
1344 ;; too big for 32-bit Emacsen, usually.
1348 (and (eq flag-sequence 'qresync)
1349 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1350 (or end (point-min)) t)
1354 (and (search-forward "HIGHESTMODSEQ "
1355 (or end (point-min)) t)
1356 (read (current-buffer))))
1359 ;; The UID FETCH FLAGS was successful.
1360 (or (eq flag-sequence 'qresync)
1361 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1362 (if (eq flag-sequence 'qresync)
1366 (setq start (point))
1368 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1369 (setq elems (read (current-buffer)))
1370 (push (cons (cadr (memq 'UID elems))
1371 (cadr (memq 'FLAGS elems)))
1373 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1374 vanished highestmodseq)
1378 (setq articles nil))))
1381 (defun nnimap-find-process-buffer (buffer)
1382 (cadr (assoc buffer nnimap-connection-alist)))
1384 (deffoo nnimap-request-post (&optional server)
1385 (setq nnimap-status-string "Read-only server")
1388 (deffoo nnimap-request-thread (id)
1389 (let* ((refs (split-string
1390 (or (mail-header-references (gnus-summary-article-header))
1394 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1396 (dolist (refid refs value)
1398 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1399 refid refid value)))))
1400 (result (with-current-buffer (nnimap-buffer)
1401 (nnimap-command "UID SEARCH %s" cmd))))
1403 (and (car result) (delete 0 (mapcar #'string-to-number
1404 (cdr (assoc "SEARCH" (cdr result))))))
1407 (defun nnimap-possibly-change-group (group server)
1408 (let ((open-result t))
1410 (not (nnimap-server-opened server)))
1411 (setq open-result (nnimap-open-server server)))
1418 (with-current-buffer (nnimap-buffer)
1419 (if (equal group (nnimap-group nnimap-object))
1421 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1423 (setf (nnimap-group nnimap-object) group
1424 (nnimap-select-result nnimap-object) result)
1427 (defun nnimap-find-connection (buffer)
1428 "Find the connection delivering to BUFFER."
1429 (let ((entry (assoc buffer nnimap-connection-alist)))
1431 (if (and (buffer-name (cadr entry))
1432 (get-buffer-process (cadr entry))
1433 (memq (process-status (get-buffer-process (cadr entry)))
1435 (get-buffer-process (cadr entry))
1436 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1439 (defvar nnimap-sequence 0)
1441 (defun nnimap-send-command (&rest args)
1442 (process-send-string
1443 (get-buffer-process (current-buffer))
1446 (incf nnimap-sequence)
1447 (apply #'format args)
1448 (if (nnimap-newlinep nnimap-object)
1451 ;; Some servers apparently can't have many outstanding
1452 ;; commands, so throttle them.
1453 (unless nnimap-streaming
1454 (nnimap-wait-for-response nnimap-sequence))
1457 (defun nnimap-log-command (command)
1458 (with-current-buffer (get-buffer-create "*imap log*")
1459 (goto-char (point-max))
1460 (insert (format-time-string "%H:%M:%S") " " command))
1463 (defun nnimap-command (&rest args)
1465 (setf (nnimap-last-command-time nnimap-object) (current-time))
1466 (let* ((sequence (apply #'nnimap-send-command args))
1467 (response (nnimap-get-response sequence)))
1468 (if (equal (caar response) "OK")
1470 (nnheader-report 'nnimap "%s"
1471 (mapconcat (lambda (a)
1473 (car response) " "))
1476 (defun nnimap-get-response (sequence)
1477 (nnimap-wait-for-response sequence)
1478 (nnimap-parse-response))
1480 (defun nnimap-wait-for-connection (&optional regexp)
1482 (setq regexp "^[*.] .*\n"))
1483 (let ((process (get-buffer-process (current-buffer))))
1484 (goto-char (point-min))
1485 (while (and (memq (process-status process)
1487 (not (re-search-forward regexp nil t)))
1488 (nnheader-accept-process-output process)
1489 (goto-char (point-min)))
1491 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1494 (defun nnimap-wait-for-response (sequence &optional messagep)
1495 (let ((process (get-buffer-process (current-buffer)))
1499 (goto-char (point-max))
1500 (while (and (setq openp (memq (process-status process)
1502 (not (re-search-backward
1503 (format "^%d .*\n" sequence)
1504 (if nnimap-streaming
1505 (max (point-min) (- (point) 500))
1509 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1510 (nnheader-accept-process-output process)
1511 (goto-char (point-max)))
1514 ;; The user hit C-g while we were waiting: kill the process, in case
1515 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1517 (delete-process process)
1520 (defun nnimap-parse-response ()
1521 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1523 (dolist (line lines)
1524 (push (cdr (nnimap-parse-line line)) result))
1525 ;; Return the OK/error code first, and then all the "continuation
1526 ;; lines" afterwards.
1528 (nreverse result))))
1530 ;; Parse an IMAP response line lightly. They look like
1531 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1532 ;; the lines into a list of strings and lists of string.
1533 (defun nnimap-parse-line (line)
1536 (mm-disable-multibyte)
1538 (goto-char (point-min))
1540 (if (eql (setq char (following-char)) ? )
1548 (if (search-forward "]" (line-end-position) 'move)
1555 (if (search-forward ")" (line-end-position) 'move)
1562 (1- (or (search-forward "\"" (line-end-position) 'move)
1565 (buffer-substring (point) (if (search-forward " " nil t)
1567 (goto-char (point-max))))))
1569 (nreverse result))))
1571 (defun nnimap-last-response-string ()
1574 (let ((end (point)))
1578 (while (and (not (bobp))
1579 (eql (following-char) ?*))
1581 (unless (eql (following-char) ?*)
1583 (buffer-substring (point) end))))
1585 (defun nnimap-get-responses (sequences)
1587 (dolist (sequence sequences)
1588 (goto-char (point-min))
1589 (when (re-search-forward (format "^%d " sequence) nil t)
1590 (push (list sequence (nnimap-parse-response))
1594 (defvar nnimap-incoming-split-list nil)
1596 (defun nnimap-fetch-inbox (articles)
1598 (nnimap-wait-for-response
1599 (nnimap-send-command
1601 (nnimap-article-ranges articles)
1602 (format "(UID %s%s)"
1605 "BODY.PEEK[HEADER] BODY.PEEK"
1607 (if nnimap-split-download-body-default
1612 (defun nnimap-split-incoming-mail ()
1613 (with-current-buffer (nnimap-buffer)
1614 (let ((nnimap-incoming-split-list nil)
1615 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1616 nnmail-split-methods
1617 nnimap-split-methods))
1618 (nnmail-split-fancy (or nnimap-split-fancy
1619 nnmail-split-fancy))
1620 (nnmail-inhibit-default-split-group t)
1621 (groups (nnimap-get-groups))
1624 (nnimap-command "SELECT %S" nnimap-inbox)
1625 (setf (nnimap-group nnimap-object) nnimap-inbox)
1626 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1628 (nnimap-fetch-inbox new-articles)
1629 (nnimap-transform-split-mail)
1630 (nnheader-ms-strip-cr)
1632 (nnmail-split-incoming (current-buffer)
1633 #'nnimap-save-mail-spec
1635 #'nnimap-dummy-active-number
1636 #'nnimap-save-mail-spec)
1637 (when nnimap-incoming-split-list
1638 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1639 sequences junk-articles)
1640 ;; Create any groups that doesn't already exist on the
1642 (dolist (spec specs)
1643 (when (and (not (member (car spec) groups))
1644 (not (eq (car spec) 'junk)))
1645 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1646 ;; Then copy over all the messages.
1648 (dolist (spec specs)
1649 (let ((group (car spec))
1650 (ranges (cdr spec)))
1651 (if (eq group 'junk)
1652 (setq junk-articles ranges)
1653 (push (list (nnimap-send-command
1655 (nnimap-article-ranges ranges)
1656 (utf7-encode group t))
1659 ;; Wait for the last COPY response...
1661 (nnimap-wait-for-response (caar sequences))
1662 ;; And then mark the successful copy actions as deleted,
1663 ;; and possibly expunge them.
1664 (nnimap-mark-and-expunge-incoming
1665 (nnimap-parse-copied-articles sequences)))
1666 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1668 (defun nnimap-mark-and-expunge-incoming (range)
1670 (setq range (nnimap-article-ranges range))
1673 (nnimap-send-command
1674 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1676 ;; If the server supports it, we now delete the message we have
1677 ;; just copied over.
1678 ((nnimap-capability "UIDPLUS")
1679 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1680 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1681 ;; user has configured it.
1683 (setq sequence (nnimap-send-command "EXPUNGE"))))
1684 (nnimap-wait-for-response sequence))))
1686 (defun nnimap-parse-copied-articles (sequences)
1687 (let (sequence copied range)
1688 (goto-char (point-min))
1689 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1690 (setq sequence (string-to-number (match-string 1)))
1691 (when (setq range (cadr (assq sequence sequences)))
1692 (push (gnus-uncompress-range range) copied)))
1693 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1695 (defun nnimap-new-articles (flags)
1697 (dolist (elem flags)
1698 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1700 (push (car elem) new)))
1701 (gnus-compress-sequence (nreverse new))))
1703 (defun nnimap-make-split-specs (list)
1707 (destructuring-bind (article spec) elem
1708 (dolist (group (delete nil (mapcar #'car spec)))
1709 (unless (setq entry (assoc group specs))
1710 (push (setq entry (list group)) specs))
1711 (setcdr entry (cons article (cdr entry))))))
1712 (dolist (entry specs)
1713 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1716 (defun nnimap-transform-split-mail ()
1717 (goto-char (point-min))
1718 (let (article bytes)
1721 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1722 (delete-region (point) (progn (forward-line 1) (point)))
1725 (setq article (match-string 1)
1726 bytes (nnimap-get-length))
1727 (delete-region (line-beginning-position) (line-end-position))
1728 ;; Insert MMDF separator, and a way to remember what this
1730 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1731 (forward-char (1+ bytes))
1732 (setq bytes (nnimap-get-length))
1733 (delete-region (line-beginning-position) (line-end-position))
1734 ;; There's a body; skip past that.
1736 (forward-char (1+ bytes))
1737 (delete-region (line-beginning-position) (line-end-position)))))))
1739 (defun nnimap-dummy-active-number (group &optional server)
1742 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1744 (goto-char (point-min))
1745 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1746 (error "Invalid nnimap mail")
1747 (setq article (string-to-number (match-string 1))))
1749 (if (eq group-art 'junk)
1750 (list (cons 'junk 1))
1752 nnimap-incoming-split-list)))
1756 ;;; nnimap.el ends here