1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
48 (require 'proto-stream)
50 (autoload 'auth-source-forget-user-or-password "auth-source")
51 (autoload 'auth-source-user-or-password "auth-source")
55 (defvoo nnimap-address nil
56 "The address of the IMAP server.")
58 (defvoo nnimap-server-port nil
60 If nnimap-stream is `ssl', this will default to `imaps'. If not,
61 it will default to `imap'.")
63 (defvoo nnimap-stream 'undecided
64 "How nnimap will talk to the IMAP server.
65 Values are `ssl', `network', `starttls' or `shell'.
66 The default is to try `ssl' first, and then `network'.")
68 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
69 (if (listp imap-shell-program)
70 (car imap-shell-program)
74 (defvoo nnimap-inbox nil
75 "The mail box where incoming mail arrives and should be split out of.")
77 (defvoo nnimap-split-methods nil
79 Uses the same syntax as nnmail-split-methods")
81 (defvoo nnimap-split-fancy nil
82 "Uses the same syntax as nnmail-split-fancy.")
84 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
85 "Articles with the flags in the list will not be considered when splitting.")
87 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
90 (defvoo nnimap-authenticator nil
91 "How nnimap authenticate itself to the server.
92 Possible choices are nil (use default methods) or `anonymous'.")
94 (defvoo nnimap-expunge t
95 "If non-nil, expunge articles after deleting them.
96 This is always done if the server supports UID EXPUNGE, but it's
97 not done by default on servers that doesn't support that command.")
99 (defvoo nnimap-streaming t
100 "If non-nil, try to use streaming commands with IMAP servers.
101 Switching this off will make nnimap slower, but it helps with
104 (defvoo nnimap-connection-alist nil)
106 (defvoo nnimap-current-infos nil)
108 (defvoo nnimap-fetch-partial-articles nil
109 "If non-nil, Gnus will fetch partial articles.
110 If t, nnimap will fetch only the first part. If a string, it
111 will fetch all parts that have types that match that string. A
112 likely value would be \"text/\" to automatically fetch all
115 (defvar nnimap-process nil)
117 (defvar nnimap-status-string "")
119 (defvar nnimap-split-download-body-default nil
120 "Internal variable with default value for `nnimap-split-download-body'.")
122 (defvar nnimap-keepalive-timer nil)
123 (defvar nnimap-process-buffers nil)
126 group process commands capabilities select-result newlinep server
127 last-command-time greeting examined stream-type)
129 (defvar nnimap-object nil)
131 (defvar nnimap-mark-alist
132 '((read "\\Seen" %Seen)
133 (tick "\\Flagged" %Flagged)
134 (reply "\\Answered" %Answered)
135 (expire "gnus-expire")
136 (dormant "gnus-dormant")
139 (download "gnus-download")
140 (forward "gnus-forward")))
142 (defvar nnimap-quirks
143 '(("QRESYNC" "Zimbra" "QRESYNC ")))
145 (defun nnimap-buffer ()
146 (nnimap-find-process-buffer nntp-server-buffer))
148 (defun nnimap-header-parameters ()
149 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
152 "BODY.PEEK[HEADER.FIELDS %s]"
153 "RFC822.HEADER.LINES %s")
154 (append '(Subject From Date Message-Id
155 References In-Reply-To Xref)
156 nnmail-extra-headers))))
158 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
159 (with-current-buffer nntp-server-buffer
161 (when (nnimap-possibly-change-group group server)
162 (with-current-buffer (nnimap-buffer)
164 (nnimap-wait-for-response
167 (nnimap-article-ranges (gnus-compress-sequence articles))
168 (nnimap-header-parameters))
170 (nnimap-transform-headers)
171 (nnheader-remove-cr-followed-by-lf))
172 (insert-buffer-substring
173 (nnimap-find-process-buffer (current-buffer))))
176 (defun nnimap-transform-headers ()
177 (goto-char (point-min))
178 (let (article bytes lines size string)
181 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
182 (delete-region (point) (progn (forward-line 1) (point)))
185 (setq article (match-string 1))
186 ;; Unfold quoted {number} strings.
187 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
188 (1+ (line-end-position)) t)
189 (setq size (string-to-number (match-string 1)))
190 (delete-region (+ (match-beginning 0) 2) (point))
191 (setq string (buffer-substring (point) (+ (point) size)))
192 (delete-region (point) (+ (point) size))
193 (insert (format "%S" string)))
194 (setq bytes (nnimap-get-length)
198 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
203 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
204 (let ((structure (ignore-errors
205 (read (current-buffer)))))
206 (while (and (consp structure)
207 (not (stringp (car structure))))
208 (setq structure (car structure)))
209 (setq lines (nth 7 structure))))
210 (delete-region (line-beginning-position) (line-end-position))
211 (insert (format "211 %s Article retrieved." article))
214 (insert (format "Chars: %s\n" size)))
216 (insert (format "Lines: %s\n" lines)))
217 (unless (re-search-forward "^\r$" nil t)
218 (goto-char (point-max)))
219 (delete-region (line-beginning-position) (line-end-position))
223 (defun nnimap-unfold-quoted-lines ()
224 ;; Unfold quoted {number} strings.
226 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
227 (setq size (string-to-number (match-string 1)))
228 (delete-region (1+ (match-beginning 0)) (point))
229 (setq string (buffer-substring (point) (+ (point) size)))
230 (delete-region (point) (+ (point) size))
231 (insert (format "%S" string)))))
233 (defun nnimap-get-length ()
234 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
235 (string-to-number (match-string 1))))
237 (defun nnimap-article-ranges (ranges)
241 (number-to-string ranges))
242 ((numberp (cdr ranges))
243 (format "%d:%d" (car ranges) (cdr ranges)))
245 (dolist (elem ranges)
248 (format "%d:%d" (car elem) (cdr elem))
249 (number-to-string elem))
251 (mapconcat #'identity (nreverse result) ",")))))
253 (deffoo nnimap-open-server (server &optional defs)
254 (if (nnimap-server-opened server)
256 (unless (assq 'nnimap-address defs)
257 (setq defs (append defs (list (list 'nnimap-address server)))))
258 (nnoo-change-server 'nnimap server defs)
259 (or (nnimap-find-connection nntp-server-buffer)
260 (nnimap-open-connection nntp-server-buffer))))
262 (defun nnimap-make-process-buffer (buffer)
264 (generate-new-buffer (format "*nnimap %s %s %s*"
265 nnimap-address nnimap-server-port
266 (gnus-buffer-exists-p buffer)))
267 (mm-disable-multibyte)
268 (buffer-disable-undo)
270 (set (make-local-variable 'after-change-functions) nil)
271 (set (make-local-variable 'nnimap-object)
272 (make-nnimap :server (nnoo-current-server 'nnimap)))
273 (push (list buffer (current-buffer)) nnimap-connection-alist)
274 (push (current-buffer) nnimap-process-buffers)
277 (defun nnimap-credentials (address ports &optional inhibit-create)
278 (let (port credentials)
279 ;; Request the credentials from all ports, but only query on the
280 ;; last port if all the previous ones have failed.
281 (while (and (null credentials)
282 (setq port (pop ports)))
284 (auth-source-user-or-password
285 '("login" "password") address port nil
291 (defun nnimap-keepalive ()
292 (let ((now (current-time)))
293 (dolist (buffer nnimap-process-buffers)
294 (when (buffer-name buffer)
295 (with-current-buffer buffer
296 (when (and nnimap-object
297 (nnimap-last-command-time nnimap-object)
301 (nnimap-last-command-time nnimap-object)))
302 ;; More than five minutes since the last command.
304 (nnimap-send-command "NOOP")))))))
306 (defun nnimap-open-connection (buffer)
307 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
308 ;; `ssl' when nnimap-server-port was nil. Sort of.
309 (when (and nnimap-server-port
310 (eq nnimap-stream 'undecided))
311 (setq nnimap-stream 'ssl))
313 (if (eq nnimap-stream 'undecided)
314 (loop for type in '(ssl network)
315 for stream = (let ((nnimap-stream type))
316 (nnimap-open-connection-1 buffer))
317 while (eq stream 'no-connect)
318 finally (return stream))
319 (nnimap-open-connection-1 buffer))))
320 (if (eq stream 'no-connect)
324 (defun nnimap-open-connection-1 (buffer)
325 (unless nnimap-keepalive-timer
326 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
328 (with-current-buffer (nnimap-make-process-buffer buffer)
329 (let* ((coding-system-for-read 'binary)
330 (coding-system-for-write 'binary)
334 ((or (eq nnimap-stream 'network)
335 (eq nnimap-stream 'starttls))
336 (nnheader-message 7 "Opening connection to %s..."
339 ((eq nnimap-stream 'shell)
340 (nnheader-message 7 "Opening connection to %s via shell..."
343 ((memq nnimap-stream '(ssl tls))
344 (nnheader-message 7 "Opening connection to %s via tls..."
346 '("143" "993" "imap" "imaps"))
348 (error "Unknown stream type: %s" nnimap-stream))))
349 (proto-stream-always-use-starttls t)
350 login-result credentials)
351 (when nnimap-server-port
352 (setq ports (append ports (list nnimap-server-port))))
353 (destructuring-bind (stream greeting capabilities stream-type)
354 (open-protocol-stream
355 "*nnimap*" (current-buffer) nnimap-address (car (last ports))
357 :shell-command nnimap-shell-program
358 :capability-command "1 CAPABILITY\r\n"
361 (lambda (capabilities)
362 (when (gnus-string-match-p "STARTTLS" capabilities)
364 (setf (nnimap-process nnimap-object) stream)
365 (setf (nnimap-stream-type nnimap-object) stream-type)
368 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
369 nnimap-address port nnimap-stream)
371 (gnus-set-process-query-on-exit-flag stream nil)
372 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
373 (nnheader-report 'nnimap "%s" greeting)
374 ;; Store the greeting (for debugging purposes).
375 (setf (nnimap-greeting nnimap-object) greeting)
376 (setf (nnimap-capabilities nnimap-object)
378 (split-string capabilities)))
379 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
380 (if (not (setq credentials
381 (if (eq nnimap-authenticator 'anonymous)
383 (message-make-address))
385 ;; First look for the credentials based
386 ;; on the virtual server name.
388 (nnoo-current-server 'nnimap) ports t)
389 ;; Then look them up based on the
391 (nnimap-credentials nnimap-address ports)))))
392 (setq nnimap-object nil)
394 (nnimap-login (car credentials) (cadr credentials)))
395 (unless (car login-result)
396 ;; If the login failed, then forget the credentials
397 ;; that are now possibly cached.
398 (dolist (host (list (nnoo-current-server 'nnimap)
401 (dolist (element '("login" "password"))
402 (auth-source-forget-user-or-password
403 element host port))))
404 (delete-process (nnimap-process nnimap-object))
405 (setq nnimap-object nil))))
407 (when (nnimap-capability "QRESYNC")
408 (nnimap-command "ENABLE QRESYNC"))
409 (nnimap-process nnimap-object))))))))
411 (autoload 'rfc2104-hash "rfc2104")
413 (defun nnimap-login (user password)
415 ((nnimap-capability "AUTH=CRAM-MD5")
417 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
418 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
420 (get-buffer-process (current-buffer))
422 (base64-encode-string
424 (rfc2104-hash 'md5 64 16 password
425 (base64-decode-string challenge))))
427 (nnimap-wait-for-response sequence)))
428 ((not (nnimap-capability "LOGINDISABLED"))
429 (nnimap-command "LOGIN %S %S" user password))
430 ((nnimap-capability "AUTH=PLAIN")
432 "AUTHENTICATE PLAIN %s"
433 (base64-encode-string
434 (format "\000%s\000%s"
435 (nnimap-quote-specials user)
436 (nnimap-quote-specials password)))))))
438 (defun nnimap-quote-specials (string)
441 (goto-char (point-min))
442 (while (re-search-forward "[\\\"]" nil t)
448 (defun nnimap-find-parameter (parameter elems)
452 ((equal (car elem) parameter)
453 (setq result (cdr elem)))
454 ((and (equal (car elem) "OK")
456 (equal (caadr elem) parameter))
457 (setq result (cdr (cadr elem))))))
460 (deffoo nnimap-close-server (&optional server)
461 (when (nnoo-change-server 'nnimap server nil)
463 (delete-process (get-buffer-process (nnimap-buffer))))
464 (nnoo-close-server 'nnimap server)
467 (deffoo nnimap-request-close ()
470 (deffoo nnimap-server-opened (&optional server)
471 (and (nnoo-current-server-p 'nnimap server)
473 (gnus-buffer-live-p nntp-server-buffer)
474 (nnimap-find-connection nntp-server-buffer)))
476 (deffoo nnimap-status-message (&optional server)
477 nnimap-status-string)
479 (deffoo nnimap-request-article (article &optional group server to-buffer)
480 (with-current-buffer nntp-server-buffer
481 (let ((result (nnimap-possibly-change-group group server))
483 (when (stringp article)
484 (setq article (nnimap-find-article-by-message-id group article)))
488 (with-current-buffer (nnimap-buffer)
490 (when nnimap-fetch-partial-articles
491 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
492 (goto-char (point-min))
493 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
494 (setq structure (ignore-errors
495 (let ((start (point)))
497 (downcase-region start (point))
499 (read (current-buffer))))
500 parts (nnimap-find-wanted-parts structure))))
502 (nnimap-get-partial-article article parts structure)
503 (nnimap-get-whole-article article))
504 (let ((buffer (current-buffer)))
505 (with-current-buffer (or to-buffer nntp-server-buffer)
507 (insert-buffer-substring buffer)
508 (nnheader-ms-strip-cr)
509 (cons group article)))))))))
511 (deffoo nnimap-request-head (article &optional group server to-buffer)
512 (when (nnimap-possibly-change-group group server)
513 (with-current-buffer (nnimap-buffer)
514 (when (stringp article)
515 (setq article (nnimap-find-article-by-message-id group article)))
516 (nnimap-get-whole-article
517 article (format "UID FETCH %%d %s"
518 (nnimap-header-parameters)))
519 (let ((buffer (current-buffer)))
520 (with-current-buffer (or to-buffer nntp-server-buffer)
522 (insert-buffer-substring buffer)
523 (nnheader-ms-strip-cr)
524 (cons group article))))))
526 (defun nnimap-get-whole-article (article &optional command)
531 "UID FETCH %d BODY.PEEK[]"
532 "UID FETCH %d RFC822.PEEK"))
534 ;; Check that we really got an article.
535 (goto-char (point-min))
536 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
539 ;; Remove any data that may have arrived before the FETCH data.
542 (delete-region (point-min) (point)))
543 (let ((bytes (nnimap-get-length)))
544 (delete-region (line-beginning-position)
545 (progn (forward-line 1) (point)))
546 (goto-char (+ (point) bytes))
547 (delete-region (point) (point-max)))
550 (defun nnimap-capability (capability)
551 (member capability (nnimap-capabilities nnimap-object)))
553 (defun nnimap-ver4-p ()
554 (nnimap-capability "IMAP4REV1"))
556 (defun nnimap-get-partial-article (article parts structure)
559 "UID FETCH %d (%s %s)"
565 (mapconcat (lambda (part)
566 (format "BODY.PEEK[%s]" part))
568 (mapconcat (lambda (part)
569 (format "RFC822.PEEK[%s]" part))
572 (nnimap-convert-partial-article structure))))
574 (defun nnimap-convert-partial-article (structure)
575 ;; First just skip past the headers.
576 (goto-char (point-min))
577 (let ((bytes (nnimap-get-length))
579 ;; Delete "FETCH" line.
580 (delete-region (line-beginning-position)
581 (progn (forward-line 1) (point)))
582 (goto-char (+ (point) bytes))
583 ;; Collect all the body parts.
584 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
585 (setq id (match-string 1)
586 bytes (or (nnimap-get-length) 0))
588 (delete-region (point) (progn (forward-line 1) (point)))
589 (push (list id (buffer-substring (point) (+ (point) bytes)))
591 (delete-region (point) (+ (point) bytes)))
592 ;; Delete trailing junk.
593 (delete-region (point) (point-max))
594 ;; Now insert all the parts again where they fit in the structure.
595 (nnimap-insert-partial-structure structure parts)
598 (defun nnimap-insert-partial-structure (structure parts &optional subp)
600 (let ((bstruc structure))
601 (while (consp (car bstruc))
603 (setq type (car bstruc))
604 (setq bstruc (car (cdr bstruc)))
605 (let ((has-boundary (member "boundary" bstruc)))
607 (setq boundary (cadr has-boundary)))))
609 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
610 (downcase type) boundary)))
611 (while (not (stringp (car structure)))
612 (insert "\n--" boundary "\n")
613 (if (consp (caar structure))
614 (nnimap-insert-partial-structure (pop structure) parts t)
615 (let ((bit (pop structure)))
616 (insert (format "Content-type: %s/%s"
617 (downcase (nth 0 bit))
618 (downcase (nth 1 bit))))
619 (if (member "CHARSET" (nth 2 bit))
621 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
623 (insert (format "Content-transfer-encoding: %s\n"
626 (when (assoc (nth 9 bit) parts)
627 (insert (cadr (assoc (nth 9 bit) parts)))))))
628 (insert "\n--" boundary "--\n")))
630 (defun nnimap-find-wanted-parts (structure)
631 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
633 (defun nnimap-find-wanted-parts-1 (structure prefix)
636 (while (consp (car structure))
637 (let ((sub (pop structure)))
638 (if (consp (car sub))
639 (push (nnimap-find-wanted-parts-1
640 sub (if (string= prefix "")
641 (number-to-string num)
642 (format "%s.%s" prefix num)))
644 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
645 (id (if (string= prefix "")
646 (number-to-string num)
647 (format "%s.%s" prefix num))))
648 (setcar (nthcdr 9 sub) id)
649 (when (if (eq nnimap-fetch-partial-articles t)
651 (string-match nnimap-fetch-partial-articles type))
656 (deffoo nnimap-request-group (group &optional server dont-check info)
657 (let ((result (nnimap-possibly-change-group
658 ;; Don't SELECT the group if we're going to select it
660 (if (and (not dont-check)
661 (assoc group nnimap-current-infos))
665 articles active marks high low)
666 (with-current-buffer nntp-server-buffer
669 (setq active (nth 2 (assoc group nnimap-current-infos))))
670 (insert (format "211 %d %d %d %S\n"
671 (- (cdr active) (car active))
675 (with-current-buffer (nnimap-buffer)
677 (let ((group-sequence
678 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
680 (nnimap-send-command "UID FETCH 1:* FLAGS")))
681 (setf (nnimap-group nnimap-object) group)
682 (nnimap-wait-for-response flag-sequence)
684 (nnimap-flags-to-marks
686 (list (list group-sequence flag-sequence
687 1 group "SELECT")))))
690 (nnimap-update-infos marks (list info))
691 (nnimap-store-info info (gnus-active (gnus-info-group info))))
692 (goto-char (point-max))
693 (let ((uidnext (nth 5 (car marks))))
694 (setq high (or (if uidnext
698 low (or (nth 4 (car marks)) uidnext 1)))))
702 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
705 (deffoo nnimap-request-create-group (group &optional server args)
706 (when (nnimap-possibly-change-group nil server)
707 (with-current-buffer (nnimap-buffer)
708 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
710 (deffoo nnimap-request-delete-group (group &optional force server)
711 (when (nnimap-possibly-change-group nil server)
712 (with-current-buffer (nnimap-buffer)
713 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
715 (deffoo nnimap-request-rename-group (group new-name &optional server)
716 (when (nnimap-possibly-change-group nil server)
717 (with-current-buffer (nnimap-buffer)
718 (nnimap-unselect-group)
719 (car (nnimap-command "RENAME %S %S"
720 (utf7-encode group t) (utf7-encode new-name t))))))
722 (defun nnimap-unselect-group ()
723 ;; Make sure we don't have this group open read/write by asking
724 ;; to examine a mailbox that doesn't exist. This seems to be
725 ;; the only way that allows us to reliably go back to unselected
727 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
729 (deffoo nnimap-request-expunge-group (group &optional server)
730 (when (nnimap-possibly-change-group group server)
731 (with-current-buffer (nnimap-buffer)
732 (car (nnimap-command "EXPUNGE")))))
734 (defun nnimap-get-flags (spec)
737 (with-current-buffer (nnimap-buffer)
739 (nnimap-wait-for-response (nnimap-send-command
740 "UID FETCH %s FLAGS" spec))
742 (subst-char-in-region (point-min) (point-max)
744 (goto-char (point-min))
745 (while (search-forward " FETCH " end t)
746 (setq elems (read (current-buffer)))
747 (push (cons (cadr (memq 'UID elems))
748 (cadr (memq 'FLAGS elems)))
750 (nreverse articles)))
752 (deffoo nnimap-close-group (group &optional server)
755 (deffoo nnimap-request-move-article (article group server accept-form
756 &optional last internal-move-group)
758 (mm-disable-multibyte)
759 (when (funcall (if internal-move-group
761 'nnimap-request-article)
762 article group server (current-buffer))
763 ;; If the move is internal (on the same server), just do it the easy
765 (let ((message-id (message-field-value "message-id")))
766 (if internal-move-group
768 (with-current-buffer (nnimap-buffer)
769 (nnimap-command "UID COPY %d %S"
771 (utf7-encode internal-move-group t)))))
773 (nnimap-delete-article article)
774 (cons internal-move-group
775 (or (nnimap-find-uid-response "COPYUID" (cadr result))
776 (nnimap-find-article-by-message-id
777 internal-move-group message-id)))))
778 ;; Move the article to a different method.
779 (let ((result (eval accept-form)))
781 (nnimap-delete-article article)
784 (deffoo nnimap-request-expire-articles (articles group &optional server force)
788 ((not (nnimap-possibly-change-group group server))
791 (eq nnmail-expiry-target 'delete))
792 (unless (nnimap-delete-article (gnus-compress-sequence articles))
793 (nnheader-message 7 "Article marked for deletion, but not expunged."))
796 (let ((deletable-articles
798 (eq nnmail-expiry-wait 'immediate))
800 (gnus-sorted-intersection
802 (nnimap-find-expired-articles group)))))
803 (if (null deletable-articles)
805 (if (eq nnmail-expiry-target 'delete)
806 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
807 (setq deletable-articles
808 (nnimap-process-expiry-targets
809 deletable-articles group server)))
810 ;; Return the articles we didn't delete.
811 (gnus-sorted-complement articles deletable-articles))))))
813 (defun nnimap-process-expiry-targets (articles group server)
814 (let ((deleted-articles nil))
816 ;; shortcut further processing if we're going to delete the articles
817 ((eq nnmail-expiry-target 'delete)
818 (setq deleted-articles articles)
820 ;; or just move them to another folder on the same IMAP server
821 ((and (not (functionp nnmail-expiry-target))
822 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
823 (gnus-server-to-method
824 (format "nnimap:%s" server))))
825 (and (nnimap-possibly-change-group group server)
826 (with-current-buffer (nnimap-buffer)
827 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
830 (nnimap-article-ranges (gnus-compress-sequence articles))
831 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
832 (setq deleted-articles articles)))
835 (dolist (article articles)
836 (let ((target nnmail-expiry-target))
838 (mm-disable-multibyte)
839 (when (nnimap-request-article article group server (current-buffer))
840 (nnheader-message 7 "Expiring article %s:%d" group article)
841 (when (functionp target)
842 (setq target (funcall target group)))
844 (not (eq target 'delete)))
845 (if (or (gnus-request-group target t)
846 (gnus-request-create-group target))
847 (nnmail-expiry-target-group target group)
850 (push article deleted-articles))))))))
851 ;; Change back to the current group again.
852 (nnimap-possibly-change-group group server)
853 (setq deleted-articles (nreverse deleted-articles))
854 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
857 (defun nnimap-find-expired-articles (group)
858 (let ((cutoff (nnmail-expired-article-p group nil nil)))
859 (with-current-buffer (nnimap-buffer)
862 "UID SEARCH SENTBEFORE %s"
866 (car (rassoc (nth 4 (decode-time cutoff))
867 parse-time-months))))
870 (delete 0 (mapcar #'string-to-number
871 (cdr (assoc "SEARCH" (cdr result))))))))))
874 (defun nnimap-find-article-by-message-id (group message-id)
875 (with-current-buffer (nnimap-buffer)
877 (unless (equal group (nnimap-group nnimap-object))
878 (setf (nnimap-group nnimap-object) nil)
879 (setf (nnimap-examined nnimap-object) group)
880 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
882 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
884 (setq result (nnimap-wait-for-response sequence))
886 (car (setq result (nnimap-parse-response))))
887 ;; Select the last instance of the message in the group.
889 (car (last (assoc "SEARCH" (cdr result)))))
890 (string-to-number article))))))
892 (defun nnimap-delete-article (articles)
893 (with-current-buffer (nnimap-buffer)
894 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
895 (nnimap-article-ranges articles))
897 ((nnimap-capability "UIDPLUS")
898 (nnimap-command "UID EXPUNGE %s"
899 (nnimap-article-ranges articles))
902 (nnimap-command "EXPUNGE")
904 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
905 "server doesn't support UIDPLUS, so we won't "
906 "delete this article now"))))))
908 (deffoo nnimap-request-scan (&optional group server)
909 (when (and (nnimap-possibly-change-group nil server)
911 nnimap-split-methods)
912 (nnheader-message 7 "nnimap %s splitting mail..." server)
913 (nnimap-split-incoming-mail)))
915 (defun nnimap-marks-to-flags (marks)
918 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
922 (deffoo nnimap-request-update-group-status (group status &optional server)
923 (when (nnimap-possibly-change-group nil server)
924 (let ((command (assoc
926 '((subscribe "SUBSCRIBE")
927 (unsubscribe "UNSUBSCRIBE")))))
929 (with-current-buffer (nnimap-buffer)
930 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
932 (deffoo nnimap-request-set-mark (group actions &optional server)
933 (when (nnimap-possibly-change-group group server)
935 (with-current-buffer (nnimap-buffer)
937 ;; Just send all the STORE commands without waiting for
938 ;; response. If they're successful, they're successful.
939 (dolist (action actions)
940 (destructuring-bind (range action marks) action
941 (let ((flags (nnimap-marks-to-flags marks)))
943 (setq sequence (nnimap-send-command
944 "UID STORE %s %sFLAGS.SILENT (%s)"
945 (nnimap-article-ranges range)
947 ((eq action 'del) "-")
948 ((eq action 'add) "+")
949 ((eq action 'set) ""))
950 (mapconcat #'identity flags " ")))))))
951 ;; Wait for the last command to complete to avoid later
952 ;; syncronisation problems with the stream.
954 (nnimap-wait-for-response sequence))))))
956 (deffoo nnimap-request-accept-article (group &optional server last)
957 (when (nnimap-possibly-change-group nil server)
958 (nnmail-check-syntax)
959 (let ((message-id (message-field-value "message-id"))
962 (setq message (buffer-substring-no-properties (point-min) (point-max)))
963 (with-current-buffer (nnimap-buffer)
964 ;; If we have this group open read-only, then unselect it
965 ;; before appending to it.
966 (when (equal (nnimap-examined nnimap-object) group)
967 (nnimap-unselect-group))
969 (setq sequence (nnimap-send-command
970 "APPEND %S {%d}" (utf7-encode group t)
972 (unless nnimap-streaming
973 (nnimap-wait-for-connection "^[+]"))
974 (process-send-string (get-buffer-process (current-buffer)) message)
975 (process-send-string (get-buffer-process (current-buffer))
976 (if (nnimap-newlinep nnimap-object)
979 (let ((result (nnimap-get-response sequence)))
980 (if (not (car result))
982 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
985 (or (nnimap-find-uid-response "APPENDUID" (car result))
986 (nnimap-find-article-by-message-id
987 group message-id)))))))))
989 (defun nnimap-find-uid-response (name list)
990 (let ((result (car (last (nnimap-find-response-element name list)))))
992 (string-to-number result))))
994 (defun nnimap-find-response-element (name list)
997 (when (and (consp elem)
998 (equal name (car elem)))
1002 (deffoo nnimap-request-replace-article (article group buffer)
1004 (when (and (nnimap-possibly-change-group group nil)
1005 ;; Put the article into the group.
1006 (with-current-buffer buffer
1008 (nnimap-request-accept-article group nil t))))
1009 (nnimap-delete-article (list article))
1010 ;; Return the new article number.
1013 (defun nnimap-add-cr ()
1014 (goto-char (point-min))
1015 (while (re-search-forward "\r?\n" nil t)
1016 (replace-match "\r\n" t t)))
1018 (defun nnimap-get-groups ()
1020 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1022 (nnimap-wait-for-response sequence)
1023 (subst-char-in-region (point-min) (point-max)
1025 (goto-char (point-min))
1026 (nnimap-unfold-quoted-lines)
1027 (goto-char (point-min))
1028 (while (search-forward "* LIST " nil t)
1029 (let ((flags (read (current-buffer)))
1030 (separator (read (current-buffer)))
1031 (group (read (current-buffer))))
1032 (unless (member '%NoSelect flags)
1033 (push (if (stringp group)
1035 (format "%s" group))
1039 (deffoo nnimap-request-list (&optional server)
1040 (nnimap-possibly-change-group nil server)
1041 (with-current-buffer nntp-server-buffer
1044 (with-current-buffer (nnimap-buffer)
1045 (nnimap-get-groups)))
1046 sequences responses)
1048 (with-current-buffer (nnimap-buffer)
1049 (setf (nnimap-group nnimap-object) nil)
1050 (dolist (group groups)
1051 (setf (nnimap-examined nnimap-object) group)
1052 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
1055 (nnimap-wait-for-response (caar sequences))
1057 (nnimap-get-responses (mapcar #'car sequences))))
1058 (dolist (response responses)
1059 (let* ((sequence (car response))
1060 (response (cadr response))
1061 (group (cadr (assoc sequence sequences))))
1063 (equal (caar response) "OK"))
1064 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1066 (dolist (elem response)
1067 (when (equal (cadr elem) "EXISTS")
1068 (setq exists (string-to-number (car elem)))))
1070 (setq highest (1- (string-to-number (car uidnext)))))
1073 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1076 (insert (format "%S %d %d y\n"
1077 (utf7-decode group t) highest (1+ highest))))
1079 ;; Return the widest possible range.
1080 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1081 (or highest exists)))))))))
1084 (deffoo nnimap-request-newgroups (date &optional server)
1085 (nnimap-possibly-change-group nil server)
1086 (with-current-buffer nntp-server-buffer
1088 (dolist (group (with-current-buffer (nnimap-buffer)
1089 (nnimap-get-groups)))
1090 (unless (assoc group nnimap-current-infos)
1091 ;; Insert dummy numbers here -- they don't matter.
1092 (insert (format "%S 0 1 y\n" group))))
1095 (deffoo nnimap-retrieve-group-data-early (server infos)
1096 (when (nnimap-possibly-change-group nil server)
1097 (with-current-buffer (nnimap-buffer)
1099 (setf (nnimap-group nnimap-object) nil)
1100 (let ((qresyncp (nnimap-capability "QRESYNC"))
1101 params groups sequences active uidvalidity modseq group)
1102 ;; Go through the infos and gather the data needed to know
1103 ;; what and how to request the data.
1104 (dolist (info infos)
1105 (setq params (gnus-info-params info)
1106 group (gnus-group-real-name (gnus-info-group info))
1107 active (cdr (assq 'active params))
1108 uidvalidity (cdr (assq 'uidvalidity params))
1109 modseq (cdr (assq 'modseq params)))
1110 (setf (nnimap-examined nnimap-object) group)
1115 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1116 (utf7-encode group t)
1117 (nnimap-quirk "QRESYNC")
1123 (if (and active uidvalidity)
1124 ;; Fetch the last 100 flags.
1125 (max 1 (- (cdr active) 100))
1130 ;; If we don't have a UIDVALIDITY, then this is
1131 ;; the first time we've seen the group, so we
1132 ;; have to do a SELECT (which is slower than an
1133 ;; examine), but will tell us whether the group
1134 ;; is read-only or not.
1136 (push (list (nnimap-send-command "%s %S" command
1137 (utf7-encode group t))
1138 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1139 start group command)
1143 (defun nnimap-quirk (command)
1144 (let ((quirk (assoc command nnimap-quirks)))
1145 ;; If this server is of a type that matches a quirk, then return
1146 ;; the "quirked" command instead of the proper one.
1147 (if (or (null quirk)
1148 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1152 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1153 (when (and sequences
1154 (nnimap-possibly-change-group nil server))
1155 (with-current-buffer (nnimap-buffer)
1156 ;; Wait for the final data to trickle in.
1157 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1161 ;; Now we should have most of the data we need, no matter
1162 ;; whether we're QRESYNCING, fetching all the flags from
1163 ;; scratch, or just fetching the last 100 flags per group.
1164 (nnimap-update-infos (nnimap-flags-to-marks
1166 (nreverse sequences)))
1168 ;; Finally, just return something resembling an active file in
1169 ;; the nntp buffer, so that the agent can save the info, too.
1170 (with-current-buffer nntp-server-buffer
1172 (dolist (info infos)
1173 (let* ((group (gnus-info-group info))
1174 (active (gnus-active group)))
1176 (insert (format "%S %d %d y\n"
1177 (gnus-group-real-name group)
1179 (car active)))))))))))
1181 (defun nnimap-update-infos (flags infos)
1182 (dolist (info infos)
1183 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1184 (marks (cdr (assoc group flags))))
1186 (nnimap-update-info info marks)))))
1188 (defun nnimap-update-info (info marks)
1189 (destructuring-bind (existing flags high low uidnext start-article
1190 permanent-flags uidvalidity
1191 vanished highestmodseq) marks
1193 ;; Ignore groups with no UIDNEXT/marks. This happens for
1194 ;; completely empty groups.
1195 ((and (not existing)
1197 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1199 (gnus-set-active (gnus-info-group info) active))))
1200 ;; We have a mismatch between the old and new UIDVALIDITY
1201 ;; identifiers, so we have to re-request the group info (the next
1202 ;; time). This virtually never happens.
1203 ((let ((old-uidvalidity
1204 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1205 (and old-uidvalidity
1206 (not (equal old-uidvalidity uidvalidity))
1207 (> start-article 1)))
1208 (gnus-group-remove-parameter info 'uidvalidity)
1209 (gnus-group-remove-parameter info 'modseq))
1210 ;; We have the data needed to update.
1212 (let* ((group (gnus-info-group info))
1213 (completep (and start-article
1214 (= start-article 1)))
1215 (active (or (gnus-active group)
1216 (cdr (assq 'active (gnus-info-params info))))))
1218 (setq high (1- uidnext)))
1219 ;; First set the active ranges based on high/low.
1221 (not (gnus-active group)))
1222 (gnus-set-active group
1225 (cons (min (or low (car active))
1227 (max (or high (cdr active))
1232 ;; No articles in this group.
1233 (cons uidnext (1- uidnext)))
1235 (cons start-article (1- start-article)))
1237 ;; No articles and no uidnext.
1242 (or high (1- uidnext)))))
1243 ;; See whether this is a read-only group.
1244 (unless (eq permanent-flags 'not-scanned)
1245 (gnus-group-set-parameter
1246 info 'permanent-flags
1247 (and (or (memq '%* permanent-flags)
1248 (memq '%Seen permanent-flags))
1250 ;; Update marks and read articles if this isn't a
1251 ;; read-only IMAP group.
1252 (when (setq permanent-flags
1253 (cdr (assq 'permanent-flags (gnus-info-params info))))
1254 (if (and highestmodseq
1255 (not start-article))
1256 ;; We've gotten the data by QRESYNCing.
1257 (nnimap-update-qresync-info
1258 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1259 ;; Do normal non-QRESYNC flag updates.
1260 ;; Update the list of read articles.
1262 (gnus-compress-sequence
1263 (gnus-set-difference
1264 (gnus-set-difference
1266 (cdr (assoc '%Seen flags)))
1267 (cdr (assoc '%Flagged flags)))))
1268 (read (gnus-range-difference
1269 (cons start-article high) unread)))
1270 (when (> start-article 1)
1273 (if (> start-article 1)
1274 (gnus-sorted-range-intersection
1275 (cons 1 (1- start-article))
1276 (gnus-info-read info))
1277 (gnus-info-read info))
1279 (when (or (not (listp permanent-flags))
1280 (memq '%Seen permanent-flags))
1281 (gnus-info-set-read info read))
1282 ;; Update the marks.
1283 (setq marks (gnus-info-marks info))
1284 (dolist (type (cdr nnimap-mark-alist))
1285 (when (or (not (listp permanent-flags))
1286 (memq (car (assoc (caddr type) flags))
1288 (memq '%* permanent-flags))
1289 (let ((old-marks (assoc (car type) marks))
1291 (gnus-compress-sequence
1292 (cdr (or (assoc (caddr type) flags) ; %Flagged
1293 (assoc (intern (cadr type) obarray) flags)
1294 (assoc (cadr type) flags)))))) ; "\Flagged"
1295 (setq marks (delq old-marks marks))
1297 (when (and old-marks
1298 (> start-article 1))
1299 (setq old-marks (gnus-range-difference
1301 (cons start-article high)))
1302 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1304 (push (cons (car type) new-marks) marks)))))
1305 (gnus-info-set-marks info marks t))))
1306 ;; Note the active level for the next run-through.
1307 (gnus-group-set-parameter info 'active (gnus-active group))
1308 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1309 (gnus-group-set-parameter info 'modseq highestmodseq)
1310 (nnimap-store-info info (gnus-active group)))))))
1312 (defun nnimap-update-qresync-info (info existing vanished flags)
1313 ;; Add all the vanished articles to the list of read articles.
1318 (gnus-range-add (gnus-info-read info)
1320 (cdr (assq '%Flagged flags)))
1321 (cdr (assq '%Seen flags))))
1322 (let ((marks (gnus-info-marks info)))
1323 (dolist (type (cdr nnimap-mark-alist))
1324 (let ((ticks (assoc (car type) marks))
1326 (cdr (or (assoc (caddr type) flags) ; %Flagged
1327 (assoc (intern (cadr type) obarray) flags)
1328 (assoc (cadr type) flags))))) ; "\Flagged"
1329 (setq marks (delq ticks marks))
1331 ;; Add the new marks we got.
1332 (setq ticks (gnus-add-to-range ticks new-marks))
1333 ;; Remove the marks from messages that don't have them.
1334 (setq ticks (gnus-remove-from-range
1336 (gnus-compress-sequence
1337 (gnus-sorted-complement existing new-marks))))
1339 (push (cons (car type) ticks) marks)))
1340 (gnus-info-set-marks info marks t))))
1342 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1343 (if (zerop (length irange))
1346 (dolist (elem (split-string irange ","))
1348 (if (string-match ":" elem)
1349 (let ((numbers (split-string elem ":")))
1350 (cons (string-to-number (car numbers))
1351 (string-to-number (cadr numbers))))
1352 (string-to-number elem))
1354 (nreverse result))))
1356 (defun nnimap-store-info (info active)
1357 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1358 (entry (assoc group nnimap-current-infos)))
1360 (setcdr entry (list info active))
1361 (push (list group info active) nnimap-current-infos))))
1363 (defun nnimap-flags-to-marks (groups)
1364 (let (data group totalp uidnext articles start-article mark permanent-flags
1365 uidvalidity vanished highestmodseq)
1366 (dolist (elem groups)
1367 (setq group (car elem)
1368 uidnext (nth 1 elem)
1369 start-article (nth 2 elem)
1370 permanent-flags (nth 3 elem)
1371 uidvalidity (nth 4 elem)
1372 vanished (nth 5 elem)
1373 highestmodseq (nth 6 elem)
1374 articles (nthcdr 7 elem))
1375 (let ((high (caar articles))
1377 (dolist (article articles)
1378 (setq low (car article))
1379 (push (car article) existing)
1380 (dolist (flag (cdr article))
1381 (setq mark (assoc flag marks))
1383 (push (list flag (car article)) marks)
1384 (setcdr mark (cons (car article) (cdr mark))))))
1385 (push (list group existing marks high low uidnext start-article
1386 permanent-flags uidvalidity vanished highestmodseq)
1390 (defun nnimap-parse-flags (sequences)
1391 (goto-char (point-min))
1392 ;; Change \Delete etc to %Delete, so that the reader can read it.
1393 (subst-char-in-region (point-min) (point-max)
1395 (let (start end articles groups uidnext elems permanent-flags
1396 uidvalidity vanished highestmodseq)
1397 (dolist (elem sequences)
1398 (destructuring-bind (group-sequence flag-sequence totalp group command)
1400 (setq start (point))
1402 ;; The EXAMINE was successful.
1403 (search-forward (format "\n%d OK " group-sequence) nil t)
1408 (setq permanent-flags
1409 (if (equal command "SELECT")
1410 (and (search-forward "PERMANENTFLAGS "
1411 (or end (point-min)) t)
1412 (read (current-buffer)))
1416 (and (search-forward "UIDNEXT "
1417 (or end (point-min)) t)
1418 (read (current-buffer))))
1421 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1422 (or end (point-min)) t)
1423 ;; Store UIDVALIDITY as a string, as it's
1424 ;; too big for 32-bit Emacsen, usually.
1428 (and (eq flag-sequence 'qresync)
1429 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1430 (or end (point-min)) t)
1434 (and (search-forward "HIGHESTMODSEQ "
1435 (or end (point-min)) t)
1436 (read (current-buffer))))
1439 ;; The UID FETCH FLAGS was successful.
1440 (or (eq flag-sequence 'qresync)
1441 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1442 (if (eq flag-sequence 'qresync)
1446 (setq start (point))
1448 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1449 (setq elems (read (current-buffer)))
1450 (push (cons (cadr (memq 'UID elems))
1451 (cadr (memq 'FLAGS elems)))
1453 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1454 vanished highestmodseq)
1458 (setq articles nil))))
1461 (defun nnimap-find-process-buffer (buffer)
1462 (cadr (assoc buffer nnimap-connection-alist)))
1464 (deffoo nnimap-request-post (&optional server)
1465 (setq nnimap-status-string "Read-only server")
1468 (deffoo nnimap-request-thread (header)
1469 (let* ((id (mail-header-id header))
1471 (or (mail-header-references header)
1475 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1477 (dolist (refid refs value)
1479 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1480 refid refid value)))))
1481 (result (with-current-buffer (nnimap-buffer)
1482 (nnimap-command "UID SEARCH %s" cmd))))
1484 (and (car result) (delete 0 (mapcar #'string-to-number
1485 (cdr (assoc "SEARCH" (cdr result))))))
1488 (defun nnimap-possibly-change-group (group server)
1489 (let ((open-result t))
1491 (not (nnimap-server-opened server)))
1492 (setq open-result (nnimap-open-server server)))
1499 (with-current-buffer (nnimap-buffer)
1500 (if (equal group (nnimap-group nnimap-object))
1502 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1504 (setf (nnimap-group nnimap-object) group
1505 (nnimap-select-result nnimap-object) result)
1508 (defun nnimap-find-connection (buffer)
1509 "Find the connection delivering to BUFFER."
1510 (let ((entry (assoc buffer nnimap-connection-alist)))
1512 (if (and (buffer-name (cadr entry))
1513 (get-buffer-process (cadr entry))
1514 (memq (process-status (get-buffer-process (cadr entry)))
1516 (get-buffer-process (cadr entry))
1517 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1520 (defvar nnimap-sequence 0)
1522 (defun nnimap-send-command (&rest args)
1523 (process-send-string
1524 (get-buffer-process (current-buffer))
1527 (incf nnimap-sequence)
1528 (apply #'format args)
1529 (if (nnimap-newlinep nnimap-object)
1532 ;; Some servers apparently can't have many outstanding
1533 ;; commands, so throttle them.
1534 (unless nnimap-streaming
1535 (nnimap-wait-for-response nnimap-sequence))
1538 (defun nnimap-log-command (command)
1539 (with-current-buffer (get-buffer-create "*imap log*")
1540 (goto-char (point-max))
1541 (insert (format-time-string "%H:%M:%S") " " command))
1544 (defun nnimap-command (&rest args)
1546 (setf (nnimap-last-command-time nnimap-object) (current-time))
1547 (let* ((sequence (apply #'nnimap-send-command args))
1548 (response (nnimap-get-response sequence)))
1549 (if (equal (caar response) "OK")
1551 (nnheader-report 'nnimap "%s"
1552 (mapconcat (lambda (a)
1554 (car response) " "))
1557 (defun nnimap-get-response (sequence)
1558 (nnimap-wait-for-response sequence)
1559 (nnimap-parse-response))
1561 (defun nnimap-wait-for-connection (&optional regexp)
1562 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1564 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1565 (let ((process (get-buffer-process (current-buffer))))
1566 (goto-char (point-min))
1567 (while (and (memq (process-status process)
1569 (not (re-search-forward regexp nil t)))
1570 (nnheader-accept-process-output process)
1571 (goto-char (point-min)))
1573 (and (looking-at (or response-regexp regexp))
1576 (defun nnimap-wait-for-response (sequence &optional messagep)
1577 (let ((process (get-buffer-process (current-buffer)))
1581 (goto-char (point-max))
1582 (while (and (setq openp (memq (process-status process)
1584 (not (re-search-backward
1585 (format "^%d .*\n" sequence)
1586 (if nnimap-streaming
1596 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1597 (nnheader-accept-process-output process)
1598 (goto-char (point-max)))
1601 ;; The user hit C-g while we were waiting: kill the process, in case
1602 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1604 (delete-process process)
1607 (defun nnimap-parse-response ()
1608 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1610 (dolist (line lines)
1611 (push (cdr (nnimap-parse-line line)) result))
1612 ;; Return the OK/error code first, and then all the "continuation
1613 ;; lines" afterwards.
1615 (nreverse result))))
1617 ;; Parse an IMAP response line lightly. They look like
1618 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1619 ;; the lines into a list of strings and lists of string.
1620 (defun nnimap-parse-line (line)
1623 (mm-disable-multibyte)
1625 (goto-char (point-min))
1627 (if (eql (setq char (following-char)) ? )
1635 (if (search-forward "]" (line-end-position) 'move)
1642 (if (search-forward ")" (line-end-position) 'move)
1649 (1- (or (search-forward "\"" (line-end-position) 'move)
1652 (buffer-substring (point) (if (search-forward " " nil t)
1654 (goto-char (point-max))))))
1656 (nreverse result))))
1658 (defun nnimap-last-response-string ()
1661 (let ((end (point)))
1665 (while (and (not (bobp))
1666 (eql (following-char) ?*))
1668 (unless (eql (following-char) ?*)
1670 (buffer-substring (point) end))))
1672 (defun nnimap-get-responses (sequences)
1674 (dolist (sequence sequences)
1675 (goto-char (point-min))
1676 (when (re-search-forward (format "^%d " sequence) nil t)
1677 (push (list sequence (nnimap-parse-response))
1681 (defvar nnimap-incoming-split-list nil)
1683 (defun nnimap-fetch-inbox (articles)
1685 (nnimap-wait-for-response
1686 (nnimap-send-command
1688 (nnimap-article-ranges articles)
1689 (format "(UID %s%s)"
1692 "BODY.PEEK[HEADER] BODY.PEEK"
1694 (if nnimap-split-download-body-default
1699 (defun nnimap-split-incoming-mail ()
1700 (with-current-buffer (nnimap-buffer)
1701 (let ((nnimap-incoming-split-list nil)
1702 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1703 nnmail-split-methods
1704 nnimap-split-methods))
1705 (nnmail-split-fancy (or nnimap-split-fancy
1706 nnmail-split-fancy))
1707 (nnmail-inhibit-default-split-group t)
1708 (groups (nnimap-get-groups))
1711 (nnimap-command "SELECT %S" nnimap-inbox)
1712 (setf (nnimap-group nnimap-object) nnimap-inbox)
1713 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1715 (nnimap-fetch-inbox new-articles)
1716 (nnimap-transform-split-mail)
1717 (nnheader-ms-strip-cr)
1719 (nnmail-split-incoming (current-buffer)
1720 #'nnimap-save-mail-spec
1722 #'nnimap-dummy-active-number
1723 #'nnimap-save-mail-spec)
1724 (when nnimap-incoming-split-list
1725 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1726 sequences junk-articles)
1727 ;; Create any groups that doesn't already exist on the
1729 (dolist (spec specs)
1730 (when (and (not (member (car spec) groups))
1731 (not (eq (car spec) 'junk)))
1732 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1733 ;; Then copy over all the messages.
1735 (dolist (spec specs)
1736 (let ((group (car spec))
1737 (ranges (cdr spec)))
1738 (if (eq group 'junk)
1739 (setq junk-articles ranges)
1740 (push (list (nnimap-send-command
1742 (nnimap-article-ranges ranges)
1743 (utf7-encode group t))
1746 ;; Wait for the last COPY response...
1748 (nnimap-wait-for-response (caar sequences))
1749 ;; And then mark the successful copy actions as deleted,
1750 ;; and possibly expunge them.
1751 (nnimap-mark-and-expunge-incoming
1752 (nnimap-parse-copied-articles sequences)))
1753 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1755 (defun nnimap-mark-and-expunge-incoming (range)
1757 (setq range (nnimap-article-ranges range))
1760 (nnimap-send-command
1761 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1763 ;; If the server supports it, we now delete the message we have
1764 ;; just copied over.
1765 ((nnimap-capability "UIDPLUS")
1766 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1767 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1768 ;; user has configured it.
1770 (setq sequence (nnimap-send-command "EXPUNGE"))))
1771 (nnimap-wait-for-response sequence))))
1773 (defun nnimap-parse-copied-articles (sequences)
1774 (let (sequence copied range)
1775 (goto-char (point-min))
1776 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1777 (setq sequence (string-to-number (match-string 1)))
1778 (when (setq range (cadr (assq sequence sequences)))
1779 (push (gnus-uncompress-range range) copied)))
1780 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1782 (defun nnimap-new-articles (flags)
1784 (dolist (elem flags)
1785 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1787 (push (car elem) new)))
1788 (gnus-compress-sequence (nreverse new))))
1790 (defun nnimap-make-split-specs (list)
1794 (destructuring-bind (article spec) elem
1795 (dolist (group (delete nil (mapcar #'car spec)))
1796 (unless (setq entry (assoc group specs))
1797 (push (setq entry (list group)) specs))
1798 (setcdr entry (cons article (cdr entry))))))
1799 (dolist (entry specs)
1800 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1803 (defun nnimap-transform-split-mail ()
1804 (goto-char (point-min))
1805 (let (article bytes)
1808 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1809 (delete-region (point) (progn (forward-line 1) (point)))
1812 (setq article (match-string 1)
1813 bytes (nnimap-get-length))
1814 (delete-region (line-beginning-position) (line-end-position))
1815 ;; Insert MMDF separator, and a way to remember what this
1817 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1818 (forward-char (1+ bytes))
1819 (setq bytes (nnimap-get-length))
1820 (delete-region (line-beginning-position) (line-end-position))
1821 ;; There's a body; skip past that.
1823 (forward-char (1+ bytes))
1824 (delete-region (line-beginning-position) (line-end-position)))))))
1826 (defun nnimap-dummy-active-number (group &optional server)
1829 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1831 (goto-char (point-min))
1832 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1833 (error "Invalid nnimap mail")
1834 (setq article (string-to-number (match-string 1))))
1836 (if (eq group-art 'junk)
1837 (list (cons 'junk 1))
1839 nnimap-incoming-split-list)))
1843 ;;; nnimap.el ends here