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))))
52 (autoload 'auth-source-forget-user-or-password "auth-source")
53 (autoload 'auth-source-user-or-password "auth-source")
57 (defvoo nnimap-address nil
58 "The address of the IMAP server.")
60 (defvoo nnimap-server-port nil
62 If nnimap-stream is `ssl', this will default to `imaps'. If not,
63 it will default to `imap'.")
65 (defvoo nnimap-stream 'ssl
66 "How nnimap will talk to the IMAP server.
67 Values are `ssl', `network', `starttls' or `shell'.")
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.")
78 (defvoo nnimap-split-methods nil
80 Uses the same syntax as nnmail-split-methods")
82 (defvoo nnimap-split-fancy nil
83 "Uses the same syntax as nnmail-split-fancy.")
85 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
86 "Articles with the flags in the list will not be considered when splitting.")
88 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
91 (defvoo nnimap-authenticator nil
92 "How nnimap authenticate itself to the server.
93 Possible choices are nil (use default methods) or `anonymous'.")
95 (defvoo nnimap-expunge t
96 "If non-nil, expunge articles after deleting them.
97 This is always done if the server supports UID EXPUNGE, but it's
98 not done by default on servers that doesn't support that command.")
100 (defvoo nnimap-streaming t
101 "If non-nil, try to use streaming commands with IMAP servers.
102 Switching this off will make nnimap slower, but it helps with
105 (defvoo nnimap-connection-alist nil)
107 (defvoo nnimap-current-infos nil)
109 (defvoo nnimap-fetch-partial-articles nil
110 "If non-nil, Gnus will fetch partial articles.
111 If t, nnimap will fetch only the first part. If a string, it
112 will fetch all parts that have types that match that string. A
113 likely value would be \"text/\" to automatically fetch all
116 (defvar nnimap-process nil)
118 (defvar nnimap-status-string "")
120 (defvar nnimap-split-download-body-default nil
121 "Internal variable with default value for `nnimap-split-download-body'.")
123 (defvar nnimap-keepalive-timer nil)
124 (defvar nnimap-process-buffers nil)
127 group process commands capabilities select-result newlinep server
128 last-command-time greeting)
130 (defvar nnimap-object nil)
132 (defvar nnimap-mark-alist
133 '((read "\\Seen" %Seen)
134 (tick "\\Flagged" %Flagged)
135 (reply "\\Answered" %Answered)
136 (expire "gnus-expire")
137 (dormant "gnus-dormant")
140 (download "gnus-download")
141 (forward "gnus-forward")))
143 (defun nnimap-buffer ()
144 (nnimap-find-process-buffer nntp-server-buffer))
146 (defun nnimap-header-parameters ()
147 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
150 "BODY.PEEK[HEADER.FIELDS %s]"
151 "RFC822.HEADER.LINES %s")
152 (append '(Subject From Date Message-Id
153 References In-Reply-To Xref)
154 nnmail-extra-headers))))
156 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
157 (with-current-buffer nntp-server-buffer
159 (when (nnimap-possibly-change-group group server)
160 (with-current-buffer (nnimap-buffer)
162 (nnimap-wait-for-response
165 (nnimap-article-ranges (gnus-compress-sequence articles))
166 (nnimap-header-parameters))
168 (nnimap-transform-headers))
169 (insert-buffer-substring
170 (nnimap-find-process-buffer (current-buffer))))
173 (defun nnimap-transform-headers ()
174 (goto-char (point-min))
175 (let (article bytes lines size string)
178 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
179 (delete-region (point) (progn (forward-line 1) (point)))
182 (setq article (match-string 1))
183 ;; Unfold quoted {number} strings.
184 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r\n"
185 (1+ (line-end-position)) t)
186 (setq size (string-to-number (match-string 1)))
187 (delete-region (+ (match-beginning 0) 2) (point))
188 (setq string (delete-region (point) (+ (point) size)))
189 (insert (format "%S" string)))
190 (setq bytes (nnimap-get-length)
194 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
199 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
200 (let ((structure (ignore-errors
201 (read (current-buffer)))))
202 (while (and (consp structure)
203 (not (stringp (car structure))))
204 (setq structure (car structure)))
205 (setq lines (nth 7 structure))))
206 (delete-region (line-beginning-position) (line-end-position))
207 (insert (format "211 %s Article retrieved." article))
210 (insert (format "Chars: %s\n" size)))
212 (insert (format "Lines: %s\n" lines)))
213 (unless (re-search-forward "^\r$" nil t)
214 (goto-char (point-max)))
215 (delete-region (line-beginning-position) (line-end-position))
219 (defun nnimap-get-length ()
220 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
221 (string-to-number (match-string 1))))
223 (defun nnimap-article-ranges (ranges)
227 (number-to-string ranges))
228 ((numberp (cdr ranges))
229 (format "%d:%d" (car ranges) (cdr ranges)))
231 (dolist (elem ranges)
234 (format "%d:%d" (car elem) (cdr elem))
235 (number-to-string elem))
237 (mapconcat #'identity (nreverse result) ",")))))
239 (deffoo nnimap-open-server (server &optional defs)
240 (if (nnimap-server-opened server)
242 (unless (assq 'nnimap-address defs)
243 (setq defs (append defs (list (list 'nnimap-address server)))))
244 (nnoo-change-server 'nnimap server defs)
245 (or (nnimap-find-connection nntp-server-buffer)
246 (nnimap-open-connection nntp-server-buffer))))
248 (defun nnimap-make-process-buffer (buffer)
250 (generate-new-buffer (format "*nnimap %s %s %s*"
251 nnimap-address nnimap-server-port
252 (gnus-buffer-exists-p buffer)))
253 (mm-disable-multibyte)
254 (buffer-disable-undo)
256 (set (make-local-variable 'after-change-functions) nil)
257 (set (make-local-variable 'nnimap-object)
258 (make-nnimap :server (nnoo-current-server 'nnimap)))
259 (push (list buffer (current-buffer)) nnimap-connection-alist)
260 (push (current-buffer) nnimap-process-buffers)
263 (defun nnimap-open-shell-stream (name buffer host port)
264 (let ((process-connection-type nil))
265 (start-process name buffer shell-file-name
273 (defun nnimap-credentials (address ports &optional inhibit-create)
274 (let (port credentials)
275 ;; Request the credentials from all ports, but only query on the
276 ;; last port if all the previous ones have failed.
277 (while (and (null credentials)
278 (setq port (pop ports)))
280 (auth-source-user-or-password
281 '("login" "password") address port nil
287 (defun nnimap-keepalive ()
288 (let ((now (current-time)))
289 (dolist (buffer nnimap-process-buffers)
290 (when (buffer-name buffer)
291 (with-current-buffer buffer
292 (when (and nnimap-object
293 (nnimap-last-command-time nnimap-object)
297 (nnimap-last-command-time nnimap-object)))
298 ;; More than five minutes since the last command.
300 (nnimap-send-command "NOOP")))))))
302 (declare-function gnutls-negotiate "subr" (fn file &optional arglist fileonly))
304 (defun nnimap-open-connection (buffer)
305 (unless nnimap-keepalive-timer
306 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
309 (with-current-buffer (nnimap-make-process-buffer buffer)
310 (let* ((coding-system-for-read 'binary)
311 (coding-system-for-write 'binary)
315 ((or (eq nnimap-stream 'network)
316 (and (eq nnimap-stream 'starttls)
317 (fboundp 'open-gnutls-stream)))
318 (nnheader-message 7 "Opening connection to %s..."
321 "*nnimap*" (current-buffer) nnimap-address
323 (or nnimap-server-port
324 (if (netrc-find-service-number "imap")
328 ((eq nnimap-stream 'shell)
329 (nnheader-message 7 "Opening connection to %s via shell..."
331 (nnimap-open-shell-stream
332 "*nnimap*" (current-buffer) nnimap-address
333 (setq port (or nnimap-server-port "imap")))
335 ((eq nnimap-stream 'starttls)
336 (nnheader-message 7 "Opening connection to %s via starttls..."
339 '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -starttls imap")))
341 "*nnimap*" (current-buffer) nnimap-address
342 (setq port (or nnimap-server-port "imap"))))
344 ((memq nnimap-stream '(ssl tls))
345 (nnheader-message 7 "Opening connection to %s via tls..."
347 (funcall (if (fboundp 'open-gnutls-stream)
350 "*nnimap*" (current-buffer) nnimap-address
352 (or nnimap-server-port
353 (if (netrc-find-service-number "imaps")
356 '("143" "993" "imap" "imaps"))
358 (error "Unknown stream type: %s" nnimap-stream))))
359 connection-result login-result credentials)
360 (setf (nnimap-process nnimap-object)
361 (get-buffer-process (current-buffer)))
362 (if (not (and (nnimap-process nnimap-object)
363 (memq (process-status (nnimap-process nnimap-object))
365 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
366 nnimap-address port nnimap-stream)
367 (gnus-set-process-query-on-exit-flag
368 (nnimap-process nnimap-object) nil)
369 (if (not (setq connection-result (nnimap-wait-for-connection)))
370 (nnheader-report 'nnimap
371 "%s" (buffer-substring
372 (point) (line-end-position)))
373 ;; Store the greeting (for debugging purposes).
374 (setf (nnimap-greeting nnimap-object)
375 (buffer-substring (line-beginning-position)
376 (line-end-position)))
377 ;; Store the capabilities.
378 (setf (nnimap-capabilities nnimap-object)
381 (nnimap-find-parameter
382 "CAPABILITY" (cdr (nnimap-command "CAPABILITY")))))
383 (when nnimap-server-port
384 (push (format "%s" nnimap-server-port) ports))
385 ;; If this is a STARTTLS-capable server, then sever the
386 ;; connection and start a STARTTLS connection instead.
388 ((and (or (and (eq nnimap-stream 'network)
389 (nnimap-capability "STARTTLS"))
390 (eq nnimap-stream 'starttls))
391 (fboundp 'open-gnutls-stream))
392 (nnimap-command "STARTTLS")
393 (gnutls-negotiate (nnimap-process nnimap-object) nil))
394 ((and (eq nnimap-stream 'network)
395 (nnimap-capability "STARTTLS"))
396 (let ((nnimap-stream 'starttls))
398 (nnimap-open-connection buffer)))
399 ;; If the STARTTLS connection was successful, we
400 ;; kill our first non-encrypted connection. If it
401 ;; wasn't successful, we just use our unencrypted
403 (when (memq (process-status tls-process) '(open run))
404 (delete-process (nnimap-process nnimap-object))
405 (kill-buffer (current-buffer))
406 (return tls-process))))))
407 (unless (equal connection-result "PREAUTH")
408 (if (not (setq credentials
409 (if (eq nnimap-authenticator 'anonymous)
411 (message-make-address))
413 ;; First look for the credentials based
414 ;; on the virtual server name.
416 (nnoo-current-server 'nnimap) ports t)
417 ;; Then look them up based on the
419 (nnimap-credentials nnimap-address ports)))))
420 (setq nnimap-object nil)
422 (if (and (nnimap-capability "AUTH=PLAIN")
423 (nnimap-capability "LOGINDISABLED"))
425 "AUTHENTICATE PLAIN %s"
426 (base64-encode-string
427 (format "\000%s\000%s"
428 (nnimap-quote-specials (car credentials))
429 (nnimap-quote-specials (cadr credentials)))))
430 (nnimap-command "LOGIN %S %S"
432 (cadr credentials))))
433 (unless (car login-result)
434 ;; If the login failed, then forget the credentials
435 ;; that are now possibly cached.
436 (dolist (host (list (nnoo-current-server 'nnimap)
439 (dolist (element '("login" "password"))
440 (auth-source-forget-user-or-password
441 element host port))))
442 (delete-process (nnimap-process nnimap-object))
443 (setq nnimap-object nil))))
445 (when (nnimap-capability "QRESYNC")
446 (nnimap-command "ENABLE QRESYNC"))
447 (nnimap-process nnimap-object))))))))
449 (defun nnimap-quote-specials (string)
452 (goto-char (point-min))
453 (while (re-search-forward "[\\\"]" nil t)
459 (defun nnimap-find-parameter (parameter elems)
463 ((equal (car elem) parameter)
464 (setq result (cdr elem)))
465 ((and (equal (car elem) "OK")
467 (equal (caadr elem) parameter))
468 (setq result (cdr (cadr elem))))))
471 (deffoo nnimap-close-server (&optional server)
472 (when (nnoo-change-server 'nnimap server nil)
474 (delete-process (get-buffer-process (nnimap-buffer))))
475 (nnoo-close-server 'nnimap server)
478 (deffoo nnimap-request-close ()
481 (deffoo nnimap-server-opened (&optional server)
482 (and (nnoo-current-server-p 'nnimap server)
484 (gnus-buffer-live-p nntp-server-buffer)
485 (nnimap-find-connection nntp-server-buffer)))
487 (deffoo nnimap-status-message (&optional server)
488 nnimap-status-string)
490 (deffoo nnimap-request-article (article &optional group server to-buffer)
491 (with-current-buffer nntp-server-buffer
492 (let ((result (nnimap-possibly-change-group group server))
494 (when (stringp article)
495 (setq article (nnimap-find-article-by-message-id group article)))
499 (with-current-buffer (nnimap-buffer)
501 (when nnimap-fetch-partial-articles
502 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
503 (goto-char (point-min))
504 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
505 (setq structure (ignore-errors
506 (let ((start (point)))
508 (downcase-region start (point))
510 (read (current-buffer))))
511 parts (nnimap-find-wanted-parts structure))))
513 (nnimap-get-partial-article article parts structure)
514 (nnimap-get-whole-article article))
515 (let ((buffer (current-buffer)))
516 (with-current-buffer (or to-buffer nntp-server-buffer)
518 (insert-buffer-substring buffer)
519 (nnheader-ms-strip-cr)
520 (cons group article)))))))))
522 (deffoo nnimap-request-head (article &optional group server to-buffer)
523 (when (nnimap-possibly-change-group group server)
524 (with-current-buffer (nnimap-buffer)
525 (when (stringp article)
526 (setq article (nnimap-find-article-by-message-id group article)))
527 (nnimap-get-whole-article
528 article (format "UID FETCH %%d %s"
529 (nnimap-header-parameters)))
530 (let ((buffer (current-buffer)))
531 (with-current-buffer (or to-buffer nntp-server-buffer)
533 (insert-buffer-substring buffer)
534 (nnheader-ms-strip-cr)
535 (cons group article))))))
537 (defun nnimap-get-whole-article (article &optional command)
542 "UID FETCH %d BODY.PEEK[]"
543 "UID FETCH %d RFC822.PEEK"))
545 ;; Check that we really got an article.
546 (goto-char (point-min))
547 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
550 ;; Remove any data that may have arrived before the FETCH data.
553 (delete-region (point-min) (point)))
554 (let ((bytes (nnimap-get-length)))
555 (delete-region (line-beginning-position)
556 (progn (forward-line 1) (point)))
557 (goto-char (+ (point) bytes))
558 (delete-region (point) (point-max)))
561 (defun nnimap-capability (capability)
562 (member capability (nnimap-capabilities nnimap-object)))
564 (defun nnimap-ver4-p ()
565 (nnimap-capability "IMAP4REV1"))
567 (defun nnimap-get-partial-article (article parts structure)
570 "UID FETCH %d (%s %s)"
576 (mapconcat (lambda (part)
577 (format "BODY.PEEK[%s]" part))
579 (mapconcat (lambda (part)
580 (format "RFC822.PEEK[%s]" part))
583 (nnimap-convert-partial-article structure))))
585 (defun nnimap-convert-partial-article (structure)
586 ;; First just skip past the headers.
587 (goto-char (point-min))
588 (let ((bytes (nnimap-get-length))
590 ;; Delete "FETCH" line.
591 (delete-region (line-beginning-position)
592 (progn (forward-line 1) (point)))
593 (goto-char (+ (point) bytes))
594 ;; Collect all the body parts.
595 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
596 (setq id (match-string 1)
597 bytes (nnimap-get-length))
599 (delete-region (point) (progn (forward-line 1) (point)))
600 (push (list id (buffer-substring (point) (+ (point) bytes)))
602 (delete-region (point) (+ (point) bytes)))
603 ;; Delete trailing junk.
604 (delete-region (point) (point-max))
605 ;; Now insert all the parts again where they fit in the structure.
606 (nnimap-insert-partial-structure structure parts)
609 (defun nnimap-insert-partial-structure (structure parts &optional subp)
611 (let ((bstruc structure))
612 (while (consp (car bstruc))
614 (setq type (car bstruc))
615 (setq bstruc (car (cdr bstruc)))
616 (let ((has-boundary (member "boundary" bstruc)))
618 (setq boundary (cadr has-boundary)))))
620 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
621 (downcase type) boundary)))
622 (while (not (stringp (car structure)))
623 (insert "\n--" boundary "\n")
624 (if (consp (caar structure))
625 (nnimap-insert-partial-structure (pop structure) parts t)
626 (let ((bit (pop structure)))
627 (insert (format "Content-type: %s/%s"
628 (downcase (nth 0 bit))
629 (downcase (nth 1 bit))))
630 (if (member "CHARSET" (nth 2 bit))
632 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
634 (insert (format "Content-transfer-encoding: %s\n"
637 (when (assoc (nth 9 bit) parts)
638 (insert (cadr (assoc (nth 9 bit) parts)))))))
639 (insert "\n--" boundary "--\n")))
641 (defun nnimap-find-wanted-parts (structure)
642 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
644 (defun nnimap-find-wanted-parts-1 (structure prefix)
647 (while (consp (car structure))
648 (let ((sub (pop structure)))
649 (if (consp (car sub))
650 (push (nnimap-find-wanted-parts-1
651 sub (if (string= prefix "")
652 (number-to-string num)
653 (format "%s.%s" prefix num)))
655 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
656 (id (if (string= prefix "")
657 (number-to-string num)
658 (format "%s.%s" prefix num))))
659 (setcar (nthcdr 9 sub) id)
660 (when (if (eq nnimap-fetch-partial-articles t)
662 (string-match nnimap-fetch-partial-articles type))
667 (deffoo nnimap-request-group (group &optional server dont-check info)
668 (let ((result (nnimap-possibly-change-group
669 ;; Don't SELECT the group if we're going to select it
675 articles active marks high low)
676 (with-current-buffer nntp-server-buffer
679 (setq active (nth 2 (assoc group nnimap-current-infos))))
680 (insert (format "211 %d %d %d %S\n"
681 (- (cdr active) (car active))
685 (with-current-buffer (nnimap-buffer)
687 (let ((group-sequence
688 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
690 (nnimap-send-command "UID FETCH 1:* FLAGS")))
691 (setf (nnimap-group nnimap-object) group)
692 (nnimap-wait-for-response flag-sequence)
694 (nnimap-flags-to-marks
696 (list (list group-sequence flag-sequence
697 1 group "SELECT")))))
700 (nnimap-update-infos marks (list info)))
701 (goto-char (point-max))
702 (let ((uidnext (nth 5 (car marks))))
703 (setq high (or (if uidnext
707 low (or (nth 4 (car marks)) uidnext 1)))))
711 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
714 (deffoo nnimap-request-create-group (group &optional server args)
715 (when (nnimap-possibly-change-group nil server)
716 (with-current-buffer (nnimap-buffer)
717 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
719 (deffoo nnimap-request-delete-group (group &optional force server)
720 (when (nnimap-possibly-change-group nil server)
721 (with-current-buffer (nnimap-buffer)
722 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
724 (deffoo nnimap-request-rename-group (group new-name &optional server)
725 (when (nnimap-possibly-change-group nil server)
726 (with-current-buffer (nnimap-buffer)
727 ;; Make sure we don't have this group open read/write by asking
728 ;; to examine a mailbox that doesn't exist. This seems to be
729 ;; the only way that allows us to reliably go back to unselected
731 (nnimap-command "EXAMINE DOES.NOT.EXIST")
732 (setf (nnimap-group nnimap-object) nil)
733 (car (nnimap-command "RENAME %S %S"
734 (utf7-encode group t) (utf7-encode new-name t))))))
736 (deffoo nnimap-request-expunge-group (group &optional server)
737 (when (nnimap-possibly-change-group group server)
738 (with-current-buffer (nnimap-buffer)
739 (car (nnimap-command "EXPUNGE")))))
741 (defun nnimap-get-flags (spec)
744 (with-current-buffer (nnimap-buffer)
746 (nnimap-wait-for-response (nnimap-send-command
747 "UID FETCH %s FLAGS" spec))
749 (subst-char-in-region (point-min) (point-max)
751 (goto-char (point-min))
752 (while (search-forward " FETCH " end t)
753 (setq elems (read (current-buffer)))
754 (push (cons (cadr (memq 'UID elems))
755 (cadr (memq 'FLAGS elems)))
757 (nreverse articles)))
759 (deffoo nnimap-close-group (group &optional server)
762 (deffoo nnimap-request-move-article (article group server accept-form
763 &optional last internal-move-group)
765 (mm-disable-multibyte)
766 (when (funcall (if internal-move-group
768 'nnimap-request-article)
769 article group server (current-buffer))
770 ;; If the move is internal (on the same server), just do it the easy
772 (let ((message-id (message-field-value "message-id")))
773 (if internal-move-group
775 (with-current-buffer (nnimap-buffer)
776 (nnimap-command "UID COPY %d %S"
778 (utf7-encode internal-move-group t)))))
780 (nnimap-delete-article article)
781 (cons internal-move-group
782 (nnimap-find-article-by-message-id
783 internal-move-group message-id))))
784 ;; Move the article to a different method.
785 (let ((result (eval accept-form)))
787 (nnimap-delete-article article)
790 (deffoo nnimap-request-expire-articles (articles group &optional server force)
794 ((not (nnimap-possibly-change-group group server))
797 (eq nnmail-expiry-target 'delete))
798 (unless (nnimap-delete-article (gnus-compress-sequence articles))
799 (nnheader-message 7 "Article marked for deletion, but not expunged."))
802 (let ((deletable-articles
804 (eq nnmail-expiry-wait 'immediate))
806 (gnus-sorted-intersection
808 (nnimap-find-expired-articles group)))))
809 (if (null deletable-articles)
811 (if (eq nnmail-expiry-target 'delete)
812 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
813 (setq deletable-articles
814 (nnimap-process-expiry-targets
815 deletable-articles group server)))
816 ;; Return the articles we didn't delete.
817 (gnus-sorted-complement articles deletable-articles))))))
819 (defun nnimap-process-expiry-targets (articles group server)
820 (let ((deleted-articles nil))
821 (dolist (article articles)
822 (let ((target nnmail-expiry-target))
824 (mm-disable-multibyte)
825 (when (nnimap-request-article article group server (current-buffer))
826 (nnheader-message 7 "Expiring article %s:%d" group article)
827 (when (functionp target)
828 (setq target (funcall target group)))
830 (not (eq target 'delete)))
831 (if (or (gnus-request-group target t)
832 (gnus-request-create-group target))
833 (nnmail-expiry-target-group target group)
836 (push article deleted-articles))))))
837 ;; Change back to the current group again.
838 (nnimap-possibly-change-group group server)
839 (setq deleted-articles (nreverse deleted-articles))
840 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
843 (defun nnimap-find-expired-articles (group)
844 (let ((cutoff (nnmail-expired-article-p group nil nil)))
845 (with-current-buffer (nnimap-buffer)
848 "UID SEARCH SENTBEFORE %s"
852 (car (rassoc (nth 4 (decode-time cutoff))
853 parse-time-months))))
856 (delete 0 (mapcar #'string-to-number
857 (cdr (assoc "SEARCH" (cdr result))))))))))
860 (defun nnimap-find-article-by-message-id (group message-id)
861 (with-current-buffer (nnimap-buffer)
863 (setf (nnimap-group nnimap-object) nil)
864 (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
866 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
868 (setq result (nnimap-wait-for-response sequence))
870 (car (setq result (nnimap-parse-response))))
871 ;; Select the last instance of the message in the group.
873 (car (last (assoc "SEARCH" (cdr result)))))
874 (string-to-number article))))))
876 (defun nnimap-delete-article (articles)
877 (with-current-buffer (nnimap-buffer)
878 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
879 (nnimap-article-ranges articles))
881 ((nnimap-capability "UIDPLUS")
882 (nnimap-command "UID EXPUNGE %s"
883 (nnimap-article-ranges articles))
886 (nnimap-command "EXPUNGE")
888 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
889 "server doesn't support UIDPLUS, so we won't "
890 "delete this article now"))))))
892 (deffoo nnimap-request-scan (&optional group server)
893 (when (and (nnimap-possibly-change-group nil server)
895 nnimap-split-methods)
896 (nnheader-message 7 "nnimap %s splitting mail..." server)
897 (nnimap-split-incoming-mail)))
899 (defun nnimap-marks-to-flags (marks)
902 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
906 (deffoo nnimap-request-set-mark (group actions &optional server)
907 (when (nnimap-possibly-change-group group server)
909 (with-current-buffer (nnimap-buffer)
911 ;; Just send all the STORE commands without waiting for
912 ;; response. If they're successful, they're successful.
913 (dolist (action actions)
914 (destructuring-bind (range action marks) action
915 (let ((flags (nnimap-marks-to-flags marks)))
917 (setq sequence (nnimap-send-command
918 "UID STORE %s %sFLAGS.SILENT (%s)"
919 (nnimap-article-ranges range)
923 (mapconcat #'identity flags " ")))))))
924 ;; Wait for the last command to complete to avoid later
925 ;; syncronisation problems with the stream.
927 (nnimap-wait-for-response sequence))))))
929 (deffoo nnimap-request-accept-article (group &optional server last)
930 (when (nnimap-possibly-change-group nil server)
931 (nnmail-check-syntax)
932 (let ((message-id (message-field-value "message-id"))
935 (setq message (buffer-substring-no-properties (point-min) (point-max)))
936 (with-current-buffer (nnimap-buffer)
938 (setq sequence (nnimap-send-command
939 "APPEND %S {%d}" (utf7-encode group t)
941 (unless nnimap-streaming
942 (nnimap-wait-for-connection "^[+]"))
943 (process-send-string (get-buffer-process (current-buffer)) message)
944 (process-send-string (get-buffer-process (current-buffer))
945 (if (nnimap-newlinep nnimap-object)
948 (let ((result (nnimap-get-response sequence)))
949 (if (not (car result))
951 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
954 (nnimap-find-article-by-message-id group message-id))))))))
956 (deffoo nnimap-request-replace-article (article group buffer)
958 (when (and (nnimap-possibly-change-group group nil)
959 ;; Put the article into the group.
960 (with-current-buffer buffer
962 (nnimap-request-accept-article group nil t))))
963 (nnimap-delete-article (list article))
964 ;; Return the new article number.
967 (defun nnimap-add-cr ()
968 (goto-char (point-min))
969 (while (re-search-forward "\r?\n" nil t)
970 (replace-match "\r\n" t t)))
972 (defun nnimap-get-groups ()
973 (let ((result (nnimap-command "LIST \"\" \"*\""))
976 (dolist (line (cdr result))
977 (when (and (equal (car line) "LIST")
978 (not (and (caadr line)
979 (string-match "noselect" (caadr line)))))
980 (push (car (last line)) groups)))
983 (deffoo nnimap-request-list (&optional server)
984 (nnimap-possibly-change-group nil server)
985 (with-current-buffer nntp-server-buffer
988 (with-current-buffer (nnimap-buffer)
989 (nnimap-get-groups)))
992 (with-current-buffer (nnimap-buffer)
993 (setf (nnimap-group nnimap-object) nil)
994 (dolist (group groups)
995 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
998 (nnimap-wait-for-response (caar sequences))
1000 (nnimap-get-responses (mapcar #'car sequences))))
1001 (dolist (response responses)
1002 (let* ((sequence (car response))
1003 (response (cadr response))
1004 (group (cadr (assoc sequence sequences))))
1006 (equal (caar response) "OK"))
1007 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1009 (dolist (elem response)
1010 (when (equal (cadr elem) "EXISTS")
1011 (setq exists (string-to-number (car elem)))))
1013 (setq highest (1- (string-to-number (car uidnext)))))
1016 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1019 (insert (format "%S %d %d y\n"
1020 (utf7-decode group t) highest (1+ highest))))
1022 ;; Return the widest possible range.
1023 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1024 (or highest exists)))))))))
1027 (deffoo nnimap-request-newgroups (date &optional server)
1028 (nnimap-possibly-change-group nil server)
1029 (with-current-buffer nntp-server-buffer
1031 (dolist (group (with-current-buffer (nnimap-buffer)
1032 (nnimap-get-groups)))
1033 (unless (assoc group nnimap-current-infos)
1034 ;; Insert dummy numbers here -- they don't matter.
1035 (insert (format "%S 0 1 y\n" group))))
1038 (deffoo nnimap-retrieve-group-data-early (server infos)
1039 (when (nnimap-possibly-change-group nil server)
1040 (with-current-buffer (nnimap-buffer)
1042 (setf (nnimap-group nnimap-object) nil)
1043 (let ((qresyncp (nnimap-capability "QRESYNC"))
1044 params groups sequences active uidvalidity modseq group)
1045 ;; Go through the infos and gather the data needed to know
1046 ;; what and how to request the data.
1047 (dolist (info infos)
1048 (setq params (gnus-info-params info)
1049 group (gnus-group-real-name (gnus-info-group info))
1050 active (cdr (assq 'active params))
1051 uidvalidity (cdr (assq 'uidvalidity params))
1052 modseq (cdr (assq 'modseq params)))
1057 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1058 (utf7-encode group t)
1064 (if (and active uidvalidity)
1065 ;; Fetch the last 100 flags.
1066 (max 1 (- (cdr active) 100))
1071 ;; If we don't have a UIDVALIDITY, then this is
1072 ;; the first time we've seen the group, so we
1073 ;; have to do a SELECT (which is slower than an
1074 ;; examine), but will tell us whether the group
1075 ;; is read-only or not.
1077 (push (list (nnimap-send-command "%s %S" command
1078 (utf7-encode group t))
1079 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1080 start group command)
1084 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1085 (when (and sequences
1086 (nnimap-possibly-change-group nil server))
1087 (with-current-buffer (nnimap-buffer)
1088 ;; Wait for the final data to trickle in.
1089 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1093 ;; Now we should have most of the data we need, no matter
1094 ;; whether we're QRESYNCING, fetching all the flags from
1095 ;; scratch, or just fetching the last 100 flags per group.
1096 (nnimap-update-infos (nnimap-flags-to-marks
1098 (nreverse sequences)))
1100 ;; Finally, just return something resembling an active file in
1101 ;; the nntp buffer, so that the agent can save the info, too.
1102 (with-current-buffer nntp-server-buffer
1104 (dolist (info infos)
1105 (let* ((group (gnus-info-group info))
1106 (active (gnus-active group)))
1108 (insert (format "%S %d %d y\n"
1109 (gnus-group-real-name group)
1111 (car active)))))))))))
1113 (defun nnimap-update-infos (flags infos)
1114 (dolist (info infos)
1115 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1116 (marks (cdr (assoc group flags))))
1118 (nnimap-update-info info marks)))))
1120 (defun nnimap-update-info (info marks)
1121 (destructuring-bind (existing flags high low uidnext start-article
1122 permanent-flags uidvalidity
1123 vanished highestmodseq) marks
1125 ;; Ignore groups with no UIDNEXT/marks. This happens for
1126 ;; completely empty groups.
1127 ((and (not existing)
1129 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1131 (gnus-set-active (gnus-info-group info) active))))
1132 ;; We have a mismatch between the old and new UIDVALIDITY
1133 ;; identifiers, so we have to re-request the group info (the next
1134 ;; time). This virtually never happens.
1135 ((let ((old-uidvalidity
1136 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1137 (and old-uidvalidity
1138 (not (equal old-uidvalidity uidvalidity))
1139 (> start-article 1)))
1140 (gnus-group-remove-parameter info 'uidvalidity)
1141 (gnus-group-remove-parameter info 'modseq))
1142 ;; We have the data needed to update.
1144 (let* ((group (gnus-info-group info))
1145 (completep (and start-article
1146 (= start-article 1)))
1147 (active (or (gnus-active group)
1148 (cdr (assq 'active (gnus-info-params info))))))
1150 (setq high (1- uidnext)))
1151 ;; First set the active ranges based on high/low.
1153 (not (gnus-active group)))
1154 (gnus-set-active group
1159 ;; No articles in this group.
1160 (cons uidnext (1- uidnext)))
1164 (cons start-article (1- start-article)))
1166 ;; No articles and no uidnext.
1171 (or high (1- uidnext)))))
1172 ;; See whether this is a read-only group.
1173 (unless (eq permanent-flags 'not-scanned)
1174 (gnus-group-set-parameter
1175 info 'permanent-flags
1176 (and (or (memq '%* permanent-flags)
1177 (memq '%Seen permanent-flags))
1179 ;; Update marks and read articles if this isn't a
1180 ;; read-only IMAP group.
1181 (when (setq permanent-flags
1182 (cdr (assq 'permanent-flags (gnus-info-params info))))
1183 (if (and highestmodseq
1184 (not start-article))
1185 ;; We've gotten the data by QRESYNCing.
1186 (nnimap-update-qresync-info
1187 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1188 ;; Do normal non-QRESYNC flag updates.
1189 ;; Update the list of read articles.
1191 (gnus-compress-sequence
1192 (gnus-set-difference
1193 (gnus-set-difference
1195 (cdr (assoc '%Seen flags)))
1196 (cdr (assoc '%Flagged flags)))))
1197 (read (gnus-range-difference
1198 (cons start-article high) unread)))
1199 (when (> start-article 1)
1202 (if (> start-article 1)
1203 (gnus-sorted-range-intersection
1204 (cons 1 (1- start-article))
1205 (gnus-info-read info))
1206 (gnus-info-read info))
1208 (when (or (not (listp permanent-flags))
1209 (memq '%Seen permanent-flags))
1210 (gnus-info-set-read info read))
1211 ;; Update the marks.
1212 (setq marks (gnus-info-marks info))
1213 (dolist (type (cdr nnimap-mark-alist))
1214 (when (or (not (listp permanent-flags))
1215 (memq (car (assoc (caddr type) flags))
1217 (memq '%* permanent-flags))
1218 (let ((old-marks (assoc (car type) marks))
1220 (gnus-compress-sequence
1221 (cdr (or (assoc (caddr type) flags) ; %Flagged
1222 (assoc (intern (cadr type) obarray) flags)
1223 (assoc (cadr type) flags)))))) ; "\Flagged"
1224 (setq marks (delq old-marks marks))
1226 (when (and old-marks
1227 (> start-article 1))
1228 (setq old-marks (gnus-range-difference
1230 (cons start-article high)))
1231 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1233 (push (cons (car type) new-marks) marks)))))
1234 (gnus-info-set-marks info marks t))))
1235 ;; Note the active level for the next run-through.
1236 (gnus-group-set-parameter info 'active (gnus-active group))
1237 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1238 (gnus-group-set-parameter info 'modseq highestmodseq)
1239 (nnimap-store-info info (gnus-active group)))))))
1241 (defun nnimap-update-qresync-info (info existing vanished flags)
1242 ;; Add all the vanished articles to the list of read articles.
1247 (gnus-range-add (gnus-info-read info)
1249 (cdr (assq '%Flagged flags)))
1250 (cdr (assq '%Seen flags))))
1251 (let ((marks (gnus-info-marks info)))
1252 (dolist (type (cdr nnimap-mark-alist))
1253 (let ((ticks (assoc (car type) marks))
1255 (cdr (or (assoc (caddr type) flags) ; %Flagged
1256 (assoc (intern (cadr type) obarray) flags)
1257 (assoc (cadr type) flags))))) ; "\Flagged"
1258 (setq marks (delq ticks marks))
1260 ;; Add the new marks we got.
1261 (setq ticks (gnus-add-to-range ticks new-marks))
1262 ;; Remove the marks from messages that don't have them.
1263 (setq ticks (gnus-remove-from-range
1265 (gnus-compress-sequence
1266 (gnus-sorted-complement existing new-marks))))
1268 (push (cons (car type) ticks) marks)))
1269 (gnus-info-set-marks info marks t))))
1271 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1272 (if (zerop (length irange))
1275 (dolist (elem (split-string irange ","))
1277 (if (string-match ":" elem)
1278 (let ((numbers (split-string elem ":")))
1279 (cons (string-to-number (car numbers))
1280 (string-to-number (cadr numbers))))
1281 (string-to-number elem))
1283 (nreverse result))))
1285 (defun nnimap-store-info (info active)
1286 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1287 (entry (assoc group nnimap-current-infos)))
1289 (setcdr entry (list info active))
1290 (push (list group info active) nnimap-current-infos))))
1292 (defun nnimap-flags-to-marks (groups)
1293 (let (data group totalp uidnext articles start-article mark permanent-flags
1294 uidvalidity vanished highestmodseq)
1295 (dolist (elem groups)
1296 (setq group (car elem)
1297 uidnext (nth 1 elem)
1298 start-article (nth 2 elem)
1299 permanent-flags (nth 3 elem)
1300 uidvalidity (nth 4 elem)
1301 vanished (nth 5 elem)
1302 highestmodseq (nth 6 elem)
1303 articles (nthcdr 7 elem))
1304 (let ((high (caar articles))
1306 (dolist (article articles)
1307 (setq low (car article))
1308 (push (car article) existing)
1309 (dolist (flag (cdr article))
1310 (setq mark (assoc flag marks))
1312 (push (list flag (car article)) marks)
1313 (setcdr mark (cons (car article) (cdr mark))))))
1314 (push (list group existing marks high low uidnext start-article
1315 permanent-flags uidvalidity vanished highestmodseq)
1319 (defun nnimap-parse-flags (sequences)
1320 (goto-char (point-min))
1321 ;; Change \Delete etc to %Delete, so that the reader can read it.
1322 (subst-char-in-region (point-min) (point-max)
1324 (let (start end articles groups uidnext elems permanent-flags
1325 uidvalidity vanished highestmodseq)
1326 (dolist (elem sequences)
1327 (destructuring-bind (group-sequence flag-sequence totalp group command)
1329 (setq start (point))
1331 ;; The EXAMINE was successful.
1332 (search-forward (format "\n%d OK " group-sequence) nil t)
1337 (setq permanent-flags
1338 (if (equal command "SELECT")
1339 (and (search-forward "PERMANENTFLAGS "
1340 (or end (point-min)) t)
1341 (read (current-buffer)))
1345 (and (search-forward "UIDNEXT "
1346 (or end (point-min)) t)
1347 (read (current-buffer))))
1350 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1351 (or end (point-min)) t)
1352 ;; Store UIDVALIDITY as a string, as it's
1353 ;; too big for 32-bit Emacsen, usually.
1357 (and (eq flag-sequence 'qresync)
1358 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1359 (or end (point-min)) t)
1363 (and (search-forward "HIGHESTMODSEQ "
1364 (or end (point-min)) t)
1365 (read (current-buffer))))
1368 ;; The UID FETCH FLAGS was successful.
1369 (or (eq flag-sequence 'qresync)
1370 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1371 (if (eq flag-sequence 'qresync)
1375 (setq start (point))
1377 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1378 (setq elems (read (current-buffer)))
1379 (push (cons (cadr (memq 'UID elems))
1380 (cadr (memq 'FLAGS elems)))
1382 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1383 vanished highestmodseq)
1387 (setq articles nil))))
1390 (defun nnimap-find-process-buffer (buffer)
1391 (cadr (assoc buffer nnimap-connection-alist)))
1393 (deffoo nnimap-request-post (&optional server)
1394 (setq nnimap-status-string "Read-only server")
1397 (deffoo nnimap-request-thread (id)
1398 (let* ((refs (split-string
1399 (or (mail-header-references (gnus-summary-article-header))
1403 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1405 (dolist (refid refs value)
1407 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1408 refid refid value)))))
1410 (with-current-buffer (nnimap-buffer)
1411 (nnimap-command "UID SEARCH %s" cmd))))
1412 (gnus-fetch-headers (and (car result)
1413 (delete 0 (mapcar #'string-to-number
1414 (cdr (assoc "SEARCH" (cdr result)))))))))
1416 (defun nnimap-possibly-change-group (group server)
1417 (let ((open-result t))
1419 (not (nnimap-server-opened server)))
1420 (setq open-result (nnimap-open-server server)))
1427 (with-current-buffer (nnimap-buffer)
1428 (if (equal group (nnimap-group nnimap-object))
1430 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1432 (setf (nnimap-group nnimap-object) group
1433 (nnimap-select-result nnimap-object) result)
1436 (defun nnimap-find-connection (buffer)
1437 "Find the connection delivering to BUFFER."
1438 (let ((entry (assoc buffer nnimap-connection-alist)))
1440 (if (and (buffer-name (cadr entry))
1441 (get-buffer-process (cadr entry))
1442 (memq (process-status (get-buffer-process (cadr entry)))
1444 (get-buffer-process (cadr entry))
1445 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1448 (defvar nnimap-sequence 0)
1450 (defun nnimap-send-command (&rest args)
1451 (process-send-string
1452 (get-buffer-process (current-buffer))
1455 (incf nnimap-sequence)
1456 (apply #'format args)
1457 (if (nnimap-newlinep nnimap-object)
1460 ;; Some servers apparently can't have many outstanding
1461 ;; commands, so throttle them.
1462 (unless nnimap-streaming
1463 (nnimap-wait-for-response nnimap-sequence))
1466 (defun nnimap-log-command (command)
1467 (with-current-buffer (get-buffer-create "*imap log*")
1468 (goto-char (point-max))
1469 (insert (format-time-string "%H:%M:%S") " " command))
1472 (defun nnimap-command (&rest args)
1474 (setf (nnimap-last-command-time nnimap-object) (current-time))
1475 (let* ((sequence (apply #'nnimap-send-command args))
1476 (response (nnimap-get-response sequence)))
1477 (if (equal (caar response) "OK")
1479 (nnheader-report 'nnimap "%s"
1480 (mapconcat (lambda (a)
1482 (car response) " "))
1485 (defun nnimap-get-response (sequence)
1486 (nnimap-wait-for-response sequence)
1487 (nnimap-parse-response))
1489 (defun nnimap-wait-for-connection (&optional regexp)
1491 (setq regexp "^[*.] .*\n"))
1492 (let ((process (get-buffer-process (current-buffer))))
1493 (goto-char (point-min))
1494 (while (and (memq (process-status process)
1496 (not (re-search-forward regexp nil t)))
1497 (nnheader-accept-process-output process)
1498 (goto-char (point-min)))
1500 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1503 (defun nnimap-wait-for-response (sequence &optional messagep)
1504 (let ((process (get-buffer-process (current-buffer)))
1508 (goto-char (point-max))
1509 (while (and (setq openp (memq (process-status process)
1511 (not (re-search-backward
1512 (format "^%d .*\n" sequence)
1513 (if nnimap-streaming
1514 (max (point-min) (- (point) 500))
1518 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1519 (nnheader-accept-process-output process)
1520 (goto-char (point-max)))
1523 ;; The user hit C-g while we were waiting: kill the process, in case
1524 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1526 (delete-process process)
1529 (defun nnimap-parse-response ()
1530 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1532 (dolist (line lines)
1533 (push (cdr (nnimap-parse-line line)) result))
1534 ;; Return the OK/error code first, and then all the "continuation
1535 ;; lines" afterwards.
1537 (nreverse result))))
1539 ;; Parse an IMAP response line lightly. They look like
1540 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1541 ;; the lines into a list of strings and lists of string.
1542 (defun nnimap-parse-line (line)
1545 (mm-disable-multibyte)
1547 (goto-char (point-min))
1549 (if (eql (setq char (following-char)) ? )
1557 (1- (search-forward "]" (line-end-position) 'move)))))
1562 (1- (search-forward ")" (line-end-position) 'move)))))
1567 (1- (or (search-forward "\"" (line-end-position) 'move)
1570 (buffer-substring (point) (if (search-forward " " nil t)
1572 (goto-char (point-max))))))
1574 (nreverse result))))
1576 (defun nnimap-last-response-string ()
1579 (let ((end (point)))
1583 (while (and (not (bobp))
1584 (eql (following-char) ?*))
1586 (unless (eql (following-char) ?*)
1588 (buffer-substring (point) end))))
1590 (defun nnimap-get-responses (sequences)
1592 (dolist (sequence sequences)
1593 (goto-char (point-min))
1594 (when (re-search-forward (format "^%d " sequence) nil t)
1595 (push (list sequence (nnimap-parse-response))
1599 (defvar nnimap-incoming-split-list nil)
1601 (defun nnimap-fetch-inbox (articles)
1603 (nnimap-wait-for-response
1604 (nnimap-send-command
1606 (nnimap-article-ranges articles)
1607 (format "(UID %s%s)"
1610 "BODY.PEEK[HEADER] BODY.PEEK"
1612 (if nnimap-split-download-body-default
1617 (defun nnimap-split-incoming-mail ()
1618 (with-current-buffer (nnimap-buffer)
1619 (let ((nnimap-incoming-split-list nil)
1620 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1621 nnmail-split-methods
1622 nnimap-split-methods))
1623 (nnmail-split-fancy (or nnimap-split-fancy
1624 nnmail-split-fancy))
1625 (nnmail-inhibit-default-split-group t)
1626 (groups (nnimap-get-groups))
1629 (nnimap-command "SELECT %S" nnimap-inbox)
1630 (setf (nnimap-group nnimap-object) nnimap-inbox)
1631 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1633 (nnimap-fetch-inbox new-articles)
1634 (nnimap-transform-split-mail)
1635 (nnheader-ms-strip-cr)
1637 (nnmail-split-incoming (current-buffer)
1638 #'nnimap-save-mail-spec
1640 #'nnimap-dummy-active-number
1641 #'nnimap-save-mail-spec)
1642 (when nnimap-incoming-split-list
1643 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1644 sequences junk-articles)
1645 ;; Create any groups that doesn't already exist on the
1647 (dolist (spec specs)
1648 (when (and (not (member (car spec) groups))
1649 (not (eq (car spec) 'junk)))
1650 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1651 ;; Then copy over all the messages.
1653 (dolist (spec specs)
1654 (let ((group (car spec))
1655 (ranges (cdr spec)))
1656 (if (eq group 'junk)
1657 (setq junk-articles ranges)
1658 (push (list (nnimap-send-command
1660 (nnimap-article-ranges ranges)
1661 (utf7-encode group t))
1664 ;; Wait for the last COPY response...
1666 (nnimap-wait-for-response (caar sequences))
1667 ;; And then mark the successful copy actions as deleted,
1668 ;; and possibly expunge them.
1669 (nnimap-mark-and-expunge-incoming
1670 (nnimap-parse-copied-articles sequences)))
1671 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1673 (defun nnimap-mark-and-expunge-incoming (range)
1675 (setq range (nnimap-article-ranges range))
1678 (nnimap-send-command
1679 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1681 ;; If the server supports it, we now delete the message we have
1682 ;; just copied over.
1683 ((nnimap-capability "UIDPLUS")
1684 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1685 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1686 ;; user has configured it.
1688 (setq sequence (nnimap-send-command "EXPUNGE"))))
1689 (nnimap-wait-for-response sequence))))
1691 (defun nnimap-parse-copied-articles (sequences)
1692 (let (sequence copied range)
1693 (goto-char (point-min))
1694 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1695 (setq sequence (string-to-number (match-string 1)))
1696 (when (setq range (cadr (assq sequence sequences)))
1697 (push (gnus-uncompress-range range) copied)))
1698 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1700 (defun nnimap-new-articles (flags)
1702 (dolist (elem flags)
1703 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1705 (push (car elem) new)))
1706 (gnus-compress-sequence (nreverse new))))
1708 (defun nnimap-make-split-specs (list)
1712 (destructuring-bind (article spec) elem
1713 (dolist (group (delete nil (mapcar #'car spec)))
1714 (unless (setq entry (assoc group specs))
1715 (push (setq entry (list group)) specs))
1716 (setcdr entry (cons article (cdr entry))))))
1717 (dolist (entry specs)
1718 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1721 (defun nnimap-transform-split-mail ()
1722 (goto-char (point-min))
1723 (let (article bytes)
1726 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1727 (delete-region (point) (progn (forward-line 1) (point)))
1730 (setq article (match-string 1)
1731 bytes (nnimap-get-length))
1732 (delete-region (line-beginning-position) (line-end-position))
1733 ;; Insert MMDF separator, and a way to remember what this
1735 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1736 (forward-char (1+ bytes))
1737 (setq bytes (nnimap-get-length))
1738 (delete-region (line-beginning-position) (line-end-position))
1739 ;; There's a body; skip past that.
1741 (forward-char (1+ bytes))
1742 (delete-region (line-beginning-position) (line-end-position)))))))
1744 (defun nnimap-dummy-active-number (group &optional server)
1747 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1749 (goto-char (point-min))
1750 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1751 (error "Invalid nnimap mail")
1752 (setq article (string-to-number (match-string 1))))
1754 (if (eq group-art 'junk)
1755 (list (cons 'junk 1))
1757 nnimap-incoming-split-list)))
1761 ;;; nnimap.el ends here