1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
48 (require 'proto-stream)
50 (autoload 'auth-source-forget+ "auth-source")
51 (autoload 'auth-source-search "auth-source")
55 (defvoo nnimap-address nil
56 "The address of the IMAP server.")
58 (defvoo nnimap-server-port nil
60 If nnimap-stream is `ssl', this will default to `imaps'. If not,
61 it will default to `imap'.")
63 (defvoo nnimap-stream 'undecided
64 "How nnimap will talk to the IMAP server.
65 Values are `ssl', `network', `network-only, `starttls' or
66 `shell'. The default is to try `ssl' first, and then
69 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
70 (if (listp imap-shell-program)
71 (car imap-shell-program)
75 (defvoo nnimap-inbox nil
76 "The mail box where incoming mail arrives and should be split out of.
77 For example, \"INBOX\".")
79 (defvoo nnimap-split-methods nil
81 Uses the same syntax as `nnmail-split-methods'.")
83 (defvoo nnimap-split-fancy nil
84 "Uses the same syntax as `nnmail-split-fancy'.")
86 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
87 "Articles with the flags in the list will not be considered when splitting.")
89 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
92 (defvoo nnimap-authenticator nil
93 "How nnimap authenticate itself to the server.
94 Possible choices are nil (use default methods) or `anonymous'.")
96 (defvoo nnimap-expunge t
97 "If non-nil, expunge articles after deleting them.
98 This is always done if the server supports UID EXPUNGE, but it's
99 not done by default on servers that doesn't support that command.")
101 (defvoo nnimap-streaming t
102 "If non-nil, try to use streaming commands with IMAP servers.
103 Switching this off will make nnimap slower, but it helps with
106 (defvoo nnimap-connection-alist nil)
108 (defvoo nnimap-current-infos nil)
110 (defvoo nnimap-fetch-partial-articles nil
111 "If non-nil, Gnus will fetch partial articles.
112 If t, nnimap will fetch only the first part. If a string, it
113 will fetch all parts that have types that match that string. A
114 likely value would be \"text/\" to automatically fetch all
117 (defvar nnimap-process nil)
119 (defvar nnimap-status-string "")
121 (defvar nnimap-split-download-body-default nil
122 "Internal variable with default value for `nnimap-split-download-body'.")
124 (defvar nnimap-keepalive-timer nil)
125 (defvar nnimap-process-buffers nil)
128 group process commands capabilities select-result newlinep server
129 last-command-time greeting examined stream-type)
131 (defvar nnimap-object nil)
133 (defvar nnimap-mark-alist
134 '((read "\\Seen" %Seen)
135 (tick "\\Flagged" %Flagged)
136 (reply "\\Answered" %Answered)
137 (expire "gnus-expire")
138 (dormant "gnus-dormant")
141 (download "gnus-download")
142 (forward "gnus-forward")))
144 (defvar nnimap-quirks
145 '(("QRESYNC" "Zimbra" "QRESYNC ")))
147 (defvar nnimap-inhibit-logging nil)
149 (defun nnimap-buffer ()
150 (nnimap-find-process-buffer nntp-server-buffer))
152 (defun nnimap-header-parameters ()
153 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
156 "BODY.PEEK[HEADER.FIELDS %s]"
157 "RFC822.HEADER.LINES %s")
158 (append '(Subject From Date Message-Id
159 References In-Reply-To Xref)
160 nnmail-extra-headers))))
162 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
163 (with-current-buffer nntp-server-buffer
165 (when (nnimap-possibly-change-group group server)
166 (with-current-buffer (nnimap-buffer)
168 (nnimap-wait-for-response
171 (nnimap-article-ranges (gnus-compress-sequence articles))
172 (nnimap-header-parameters))
174 (nnimap-transform-headers)
175 (nnheader-remove-cr-followed-by-lf))
176 (insert-buffer-substring
177 (nnimap-find-process-buffer (current-buffer))))
180 (defun nnimap-transform-headers ()
181 (goto-char (point-min))
182 (let (article bytes lines size string)
185 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
186 (delete-region (point) (progn (forward-line 1) (point)))
189 (setq article (match-string 1))
190 ;; Unfold quoted {number} strings.
191 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
192 (1+ (line-end-position)) t)
193 (setq size (string-to-number (match-string 1)))
194 (delete-region (+ (match-beginning 0) 2) (point))
195 (setq string (buffer-substring (point) (+ (point) size)))
196 (delete-region (point) (+ (point) size))
197 (insert (format "%S" string)))
198 (setq bytes (nnimap-get-length)
202 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
207 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
208 (let ((structure (ignore-errors
209 (read (current-buffer)))))
210 (while (and (consp structure)
211 (not (stringp (car structure))))
212 (setq structure (car structure)))
213 (setq lines (nth 7 structure))))
214 (delete-region (line-beginning-position) (line-end-position))
215 (insert (format "211 %s Article retrieved." article))
218 (insert (format "Chars: %s\n" size)))
220 (insert (format "Lines: %s\n" lines)))
221 (unless (re-search-forward "^\r$" nil t)
222 (goto-char (point-max)))
223 (delete-region (line-beginning-position) (line-end-position))
227 (defun nnimap-unfold-quoted-lines ()
228 ;; Unfold quoted {number} strings.
230 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
231 (setq size (string-to-number (match-string 1)))
232 (delete-region (1+ (match-beginning 0)) (point))
233 (setq string (buffer-substring (point) (+ (point) size)))
234 (delete-region (point) (+ (point) size))
235 (insert (format "%S" string)))))
237 (defun nnimap-get-length ()
238 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
239 (string-to-number (match-string 1))))
241 (defun nnimap-article-ranges (ranges)
245 (number-to-string ranges))
246 ((numberp (cdr ranges))
247 (format "%d:%d" (car ranges) (cdr ranges)))
249 (dolist (elem ranges)
252 (format "%d:%d" (car elem) (cdr elem))
253 (number-to-string elem))
255 (mapconcat #'identity (nreverse result) ",")))))
257 (deffoo nnimap-open-server (server &optional defs)
258 (if (nnimap-server-opened server)
260 (unless (assq 'nnimap-address defs)
261 (setq defs (append defs (list (list 'nnimap-address server)))))
262 (nnoo-change-server 'nnimap server defs)
263 (or (nnimap-find-connection nntp-server-buffer)
264 (nnimap-open-connection nntp-server-buffer))))
266 (defun nnimap-make-process-buffer (buffer)
268 (generate-new-buffer (format "*nnimap %s %s %s*"
269 nnimap-address nnimap-server-port
270 (gnus-buffer-exists-p buffer)))
271 (mm-disable-multibyte)
272 (buffer-disable-undo)
274 (set (make-local-variable 'after-change-functions) nil)
275 (set (make-local-variable 'nnimap-object)
276 (make-nnimap :server (nnoo-current-server 'nnimap)))
277 (push (list buffer (current-buffer)) nnimap-connection-alist)
278 (push (current-buffer) nnimap-process-buffers)
281 (defun nnimap-credentials (address ports)
282 (let ((found (nth 0 (auth-source-search :max 1
287 (list (plist-get found :user)
288 (let ((secret (plist-get found :secret)))
289 (if (functionp secret)
294 (defun nnimap-keepalive ()
295 (let ((now (current-time)))
296 (dolist (buffer nnimap-process-buffers)
297 (when (buffer-name buffer)
298 (with-current-buffer buffer
299 (when (and nnimap-object
300 (nnimap-last-command-time nnimap-object)
304 (nnimap-last-command-time nnimap-object)))
305 ;; More than five minutes since the last command.
307 (nnimap-send-command "NOOP")))))))
309 (defun nnimap-open-connection (buffer)
310 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
311 ;; `ssl' when nnimap-server-port was nil. Sort of.
312 (when (and nnimap-server-port
313 (eq nnimap-stream 'undecided))
314 (setq nnimap-stream 'ssl))
316 (if (eq nnimap-stream 'undecided)
317 (loop for type in '(ssl network)
318 for stream = (let ((nnimap-stream type))
319 (nnimap-open-connection-1 buffer))
320 while (eq stream 'no-connect)
321 finally (return stream))
322 (nnimap-open-connection-1 buffer))))
323 (if (eq stream 'no-connect)
327 (defun nnimap-open-connection-1 (buffer)
328 (unless nnimap-keepalive-timer
329 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
331 (with-current-buffer (nnimap-make-process-buffer buffer)
332 (let* ((coding-system-for-read 'binary)
333 (coding-system-for-write 'binary)
337 ((or (eq nnimap-stream 'network)
338 (eq nnimap-stream 'starttls))
339 (nnheader-message 7 "Opening connection to %s..."
342 ((eq nnimap-stream 'shell)
343 (nnheader-message 7 "Opening connection to %s via shell..."
346 ((memq nnimap-stream '(ssl tls))
347 (nnheader-message 7 "Opening connection to %s via tls..."
349 '("imaps" "imap" "993" "143"))
351 (error "Unknown stream type: %s" nnimap-stream))))
352 (proto-stream-always-use-starttls t)
353 login-result credentials)
354 (when nnimap-server-port
355 (push nnimap-server-port ports))
356 (destructuring-bind (stream greeting capabilities stream-type)
357 (open-protocol-stream
358 "*nnimap*" (current-buffer) nnimap-address (car ports)
360 :shell-command nnimap-shell-program
361 :capability-command "1 CAPABILITY\r\n"
364 (lambda (capabilities)
365 (when (gnus-string-match-p "STARTTLS" capabilities)
367 (setf (nnimap-process nnimap-object) stream)
368 (setf (nnimap-stream-type nnimap-object) stream-type)
371 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
372 nnimap-address port nnimap-stream)
374 (gnus-set-process-query-on-exit-flag stream nil)
375 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
376 (nnheader-report 'nnimap "%s" greeting)
377 ;; Store the greeting (for debugging purposes).
378 (setf (nnimap-greeting nnimap-object) greeting)
379 (setf (nnimap-capabilities nnimap-object)
381 (split-string capabilities)))
382 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
383 (if (not (setq credentials
384 (if (eq nnimap-authenticator 'anonymous)
386 (message-make-address))
387 ;; Look for the credentials based on
388 ;; the virtual server name and the address
390 (gnus-delete-duplicates
393 (nnoo-current-server 'nnimap)))
395 (setq nnimap-object nil)
396 (let ((nnimap-inhibit-logging t))
398 (nnimap-login (car credentials) (cadr credentials))))
399 (unless (car login-result)
400 ;; If the login failed, then forget the credentials
401 ;; that are now possibly cached.
402 (dolist (host (list (nnoo-current-server 'nnimap)
405 (auth-source-forget+ :host host :port port)))
406 (delete-process (nnimap-process nnimap-object))
407 (setq nnimap-object nil))))
409 (when (nnimap-capability "QRESYNC")
410 (nnimap-command "ENABLE QRESYNC"))
411 (nnimap-process nnimap-object))))))))
413 (autoload 'rfc2104-hash "rfc2104")
415 (defun nnimap-login (user password)
417 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
418 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
419 ;; and we're using an encrypted connection.
420 ((and (not (nnimap-capability "LOGINDISABLED"))
421 (eq (nnimap-stream-type nnimap-object) 'tls))
422 (nnimap-command "LOGIN %S %S" user password))
423 ((nnimap-capability "AUTH=CRAM-MD5")
425 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
426 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
428 (get-buffer-process (current-buffer))
430 (base64-encode-string
432 (rfc2104-hash 'md5 64 16 password
433 (base64-decode-string challenge))))
435 (nnimap-wait-for-response sequence)))
436 ((not (nnimap-capability "LOGINDISABLED"))
437 (nnimap-command "LOGIN %S %S" user password))
438 ((nnimap-capability "AUTH=PLAIN")
440 "AUTHENTICATE PLAIN %s"
441 (base64-encode-string
442 (format "\000%s\000%s"
443 (nnimap-quote-specials user)
444 (nnimap-quote-specials password)))))))
446 (defun nnimap-quote-specials (string)
449 (goto-char (point-min))
450 (while (re-search-forward "[\\\"]" nil t)
456 (defun nnimap-find-parameter (parameter elems)
460 ((equal (car elem) parameter)
461 (setq result (cdr elem)))
462 ((and (equal (car elem) "OK")
464 (equal (caadr elem) parameter))
465 (setq result (cdr (cadr elem))))))
468 (deffoo nnimap-close-server (&optional server)
469 (when (nnoo-change-server 'nnimap server nil)
471 (delete-process (get-buffer-process (nnimap-buffer))))
472 (nnoo-close-server 'nnimap server)
475 (deffoo nnimap-request-close ()
478 (deffoo nnimap-server-opened (&optional server)
479 (and (nnoo-current-server-p 'nnimap server)
481 (gnus-buffer-live-p nntp-server-buffer)
482 (nnimap-find-connection nntp-server-buffer)))
484 (deffoo nnimap-status-message (&optional server)
485 nnimap-status-string)
487 (deffoo nnimap-request-article (article &optional group server to-buffer)
488 (with-current-buffer nntp-server-buffer
489 (let ((result (nnimap-possibly-change-group group server))
491 (when (stringp article)
492 (setq article (nnimap-find-article-by-message-id group article)))
496 (with-current-buffer (nnimap-buffer)
498 (when nnimap-fetch-partial-articles
499 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
500 (goto-char (point-min))
501 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
502 (setq structure (ignore-errors
503 (let ((start (point)))
505 (downcase-region start (point))
507 (read (current-buffer))))
508 parts (nnimap-find-wanted-parts structure))))
510 (nnimap-get-partial-article article parts structure)
511 (nnimap-get-whole-article article))
512 (let ((buffer (current-buffer)))
513 (with-current-buffer (or to-buffer nntp-server-buffer)
515 (insert-buffer-substring buffer)
516 (nnheader-ms-strip-cr)
517 (cons group article)))))))))
519 (deffoo nnimap-request-head (article &optional group server to-buffer)
520 (when (nnimap-possibly-change-group group server)
521 (with-current-buffer (nnimap-buffer)
522 (when (stringp article)
523 (setq article (nnimap-find-article-by-message-id group article)))
526 (nnimap-get-whole-article
527 article (format "UID FETCH %%d %s"
528 (nnimap-header-parameters)))
529 (let ((buffer (current-buffer)))
530 (with-current-buffer (or to-buffer nntp-server-buffer)
532 (insert-buffer-substring buffer)
533 (nnheader-ms-strip-cr)
534 (cons group article)))))))
536 (defun nnimap-get-whole-article (article &optional command)
541 "UID FETCH %d BODY.PEEK[]"
542 "UID FETCH %d RFC822.PEEK"))
544 ;; Check that we really got an article.
545 (goto-char (point-min))
546 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
549 ;; Remove any data that may have arrived before the FETCH data.
552 (delete-region (point-min) (point)))
553 (let ((bytes (nnimap-get-length)))
554 (delete-region (line-beginning-position)
555 (progn (forward-line 1) (point)))
556 (goto-char (+ (point) bytes))
557 (delete-region (point) (point-max)))
560 (defun nnimap-capability (capability)
561 (member capability (nnimap-capabilities nnimap-object)))
563 (defun nnimap-ver4-p ()
564 (nnimap-capability "IMAP4REV1"))
566 (defun nnimap-get-partial-article (article parts structure)
569 "UID FETCH %d (%s %s)"
575 (mapconcat (lambda (part)
576 (format "BODY.PEEK[%s]" part))
578 (mapconcat (lambda (part)
579 (format "RFC822.PEEK[%s]" part))
582 (nnimap-convert-partial-article structure))))
584 (defun nnimap-convert-partial-article (structure)
585 ;; First just skip past the headers.
586 (goto-char (point-min))
587 (let ((bytes (nnimap-get-length))
589 ;; Delete "FETCH" line.
590 (delete-region (line-beginning-position)
591 (progn (forward-line 1) (point)))
592 (goto-char (+ (point) bytes))
593 ;; Collect all the body parts.
594 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
595 (setq id (match-string 1)
596 bytes (or (nnimap-get-length) 0))
598 (delete-region (point) (progn (forward-line 1) (point)))
599 (push (list id (buffer-substring (point) (+ (point) bytes)))
601 (delete-region (point) (+ (point) bytes)))
602 ;; Delete trailing junk.
603 (delete-region (point) (point-max))
604 ;; Now insert all the parts again where they fit in the structure.
605 (nnimap-insert-partial-structure structure parts)
608 (defun nnimap-insert-partial-structure (structure parts &optional subp)
610 (let ((bstruc structure))
611 (while (consp (car bstruc))
613 (setq type (car bstruc))
614 (setq bstruc (car (cdr bstruc)))
615 (let ((has-boundary (member "boundary" bstruc)))
617 (setq boundary (cadr has-boundary)))))
619 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
620 (downcase type) boundary)))
621 (while (not (stringp (car structure)))
622 (insert "\n--" boundary "\n")
623 (if (consp (caar structure))
624 (nnimap-insert-partial-structure (pop structure) parts t)
625 (let ((bit (pop structure)))
626 (insert (format "Content-type: %s/%s"
627 (downcase (nth 0 bit))
628 (downcase (nth 1 bit))))
629 (if (member "CHARSET" (nth 2 bit))
631 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
633 (insert (format "Content-transfer-encoding: %s\n"
636 (when (assoc (nth 9 bit) parts)
637 (insert (cadr (assoc (nth 9 bit) parts)))))))
638 (insert "\n--" boundary "--\n")))
640 (defun nnimap-find-wanted-parts (structure)
641 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
643 (defun nnimap-find-wanted-parts-1 (structure prefix)
646 (while (consp (car structure))
647 (let ((sub (pop structure)))
648 (if (consp (car sub))
649 (push (nnimap-find-wanted-parts-1
650 sub (if (string= prefix "")
651 (number-to-string num)
652 (format "%s.%s" prefix num)))
654 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
655 (id (if (string= prefix "")
656 (number-to-string num)
657 (format "%s.%s" prefix num))))
658 (setcar (nthcdr 9 sub) id)
659 (when (if (eq nnimap-fetch-partial-articles t)
661 (string-match nnimap-fetch-partial-articles type))
666 (deffoo nnimap-request-group (group &optional server dont-check info)
667 (let ((result (nnimap-possibly-change-group
668 ;; Don't SELECT the group if we're going to select it
670 (if (and (not dont-check)
671 (assoc group nnimap-current-infos))
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 (nnimap-store-info info (gnus-active (gnus-info-group info))))
702 (goto-char (point-max))
703 (let ((uidnext (nth 5 (car marks))))
704 (setq high (or (if uidnext
708 low (or (nth 4 (car marks)) uidnext 1)))))
712 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
715 (deffoo nnimap-request-create-group (group &optional server args)
716 (when (nnimap-possibly-change-group nil server)
717 (with-current-buffer (nnimap-buffer)
718 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
720 (deffoo nnimap-request-delete-group (group &optional force server)
721 (when (nnimap-possibly-change-group nil server)
722 (with-current-buffer (nnimap-buffer)
723 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
725 (deffoo nnimap-request-rename-group (group new-name &optional server)
726 (when (nnimap-possibly-change-group nil server)
727 (with-current-buffer (nnimap-buffer)
728 (nnimap-unselect-group)
729 (car (nnimap-command "RENAME %S %S"
730 (utf7-encode group t) (utf7-encode new-name t))))))
732 (defun nnimap-unselect-group ()
733 ;; Make sure we don't have this group open read/write by asking
734 ;; to examine a mailbox that doesn't exist. This seems to be
735 ;; the only way that allows us to reliably go back to unselected
737 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
739 (deffoo nnimap-request-expunge-group (group &optional server)
740 (when (nnimap-possibly-change-group group server)
741 (with-current-buffer (nnimap-buffer)
742 (car (nnimap-command "EXPUNGE")))))
744 (defun nnimap-get-flags (spec)
747 (with-current-buffer (nnimap-buffer)
749 (nnimap-wait-for-response (nnimap-send-command
750 "UID FETCH %s FLAGS" spec))
752 (subst-char-in-region (point-min) (point-max)
754 (goto-char (point-min))
755 (while (search-forward " FETCH " end t)
756 (setq elems (read (current-buffer)))
757 (push (cons (cadr (memq 'UID elems))
758 (cadr (memq 'FLAGS elems)))
760 (nreverse articles)))
762 (deffoo nnimap-close-group (group &optional server)
765 (deffoo nnimap-request-move-article (article group server accept-form
766 &optional last internal-move-group)
768 (mm-disable-multibyte)
769 (when (funcall (if internal-move-group
771 'nnimap-request-article)
772 article group server (current-buffer))
773 ;; If the move is internal (on the same server), just do it the easy
775 (let ((message-id (message-field-value "message-id")))
776 (if internal-move-group
778 (with-current-buffer (nnimap-buffer)
779 (nnimap-command "UID COPY %d %S"
781 (utf7-encode internal-move-group t)))))
783 (nnimap-delete-article article)
784 (cons internal-move-group
785 (or (nnimap-find-uid-response "COPYUID" (cadr result))
786 (nnimap-find-article-by-message-id
787 internal-move-group message-id)))))
788 ;; Move the article to a different method.
789 (let ((result (eval accept-form)))
791 (nnimap-delete-article article)
794 (deffoo nnimap-request-expire-articles (articles group &optional server force)
798 ((not (nnimap-possibly-change-group group server))
801 (eq nnmail-expiry-target 'delete))
802 (unless (nnimap-delete-article (gnus-compress-sequence articles))
803 (nnheader-message 7 "Article marked for deletion, but not expunged."))
806 (let ((deletable-articles
808 (eq nnmail-expiry-wait 'immediate))
810 (gnus-sorted-intersection
812 (nnimap-find-expired-articles group)))))
813 (if (null deletable-articles)
815 (if (eq nnmail-expiry-target 'delete)
816 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
817 (setq deletable-articles
818 (nnimap-process-expiry-targets
819 deletable-articles group server)))
820 ;; Return the articles we didn't delete.
821 (gnus-sorted-complement articles deletable-articles))))))
823 (defun nnimap-process-expiry-targets (articles group server)
824 (let ((deleted-articles nil))
826 ;; shortcut further processing if we're going to delete the articles
827 ((eq nnmail-expiry-target 'delete)
828 (setq deleted-articles articles)
830 ;; or just move them to another folder on the same IMAP server
831 ((and (not (functionp nnmail-expiry-target))
832 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
833 (gnus-server-to-method
834 (format "nnimap:%s" server))))
835 (and (nnimap-possibly-change-group group server)
836 (with-current-buffer (nnimap-buffer)
837 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
840 (nnimap-article-ranges (gnus-compress-sequence articles))
841 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
842 (setq deleted-articles articles)))
845 (dolist (article articles)
846 (let ((target nnmail-expiry-target))
848 (mm-disable-multibyte)
849 (when (nnimap-request-article article group server (current-buffer))
850 (nnheader-message 7 "Expiring article %s:%d" group article)
851 (when (functionp target)
852 (setq target (funcall target group)))
854 (not (eq target 'delete)))
855 (if (or (gnus-request-group target t)
856 (gnus-request-create-group target))
857 (nnmail-expiry-target-group target group)
860 (push article deleted-articles))))))))
861 ;; Change back to the current group again.
862 (nnimap-possibly-change-group group server)
863 (setq deleted-articles (nreverse deleted-articles))
864 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
867 (defun nnimap-find-expired-articles (group)
868 (let ((cutoff (nnmail-expired-article-p group nil nil)))
869 (with-current-buffer (nnimap-buffer)
872 "UID SEARCH SENTBEFORE %s"
876 (car (rassoc (nth 4 (decode-time cutoff))
877 parse-time-months))))
880 (delete 0 (mapcar #'string-to-number
881 (cdr (assoc "SEARCH" (cdr result))))))))))
884 (defun nnimap-find-article-by-message-id (group message-id)
885 (with-current-buffer (nnimap-buffer)
887 (unless (equal group (nnimap-group nnimap-object))
888 (setf (nnimap-group nnimap-object) nil)
889 (setf (nnimap-examined nnimap-object) group)
890 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
892 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
894 (setq result (nnimap-wait-for-response sequence))
896 (car (setq result (nnimap-parse-response))))
897 ;; Select the last instance of the message in the group.
899 (car (last (assoc "SEARCH" (cdr result)))))
900 (string-to-number article))))))
902 (defun nnimap-delete-article (articles)
903 (with-current-buffer (nnimap-buffer)
904 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
905 (nnimap-article-ranges articles))
907 ((nnimap-capability "UIDPLUS")
908 (nnimap-command "UID EXPUNGE %s"
909 (nnimap-article-ranges articles))
912 (nnimap-command "EXPUNGE")
914 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
915 "server doesn't support UIDPLUS, so we won't "
916 "delete this article now"))))))
918 (deffoo nnimap-request-scan (&optional group server)
919 (when (and (nnimap-possibly-change-group nil server)
921 nnimap-split-methods)
922 (nnheader-message 7 "nnimap %s splitting mail..." server)
923 (nnimap-split-incoming-mail)))
925 (defun nnimap-marks-to-flags (marks)
928 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
932 (deffoo nnimap-request-update-group-status (group status &optional server)
933 (when (nnimap-possibly-change-group nil server)
934 (let ((command (assoc
936 '((subscribe "SUBSCRIBE")
937 (unsubscribe "UNSUBSCRIBE")))))
939 (with-current-buffer (nnimap-buffer)
940 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
942 (deffoo nnimap-request-set-mark (group actions &optional server)
943 (when (nnimap-possibly-change-group group server)
945 (with-current-buffer (nnimap-buffer)
947 ;; Just send all the STORE commands without waiting for
948 ;; response. If they're successful, they're successful.
949 (dolist (action actions)
950 (destructuring-bind (range action marks) action
951 (let ((flags (nnimap-marks-to-flags marks)))
953 (setq sequence (nnimap-send-command
954 "UID STORE %s %sFLAGS.SILENT (%s)"
955 (nnimap-article-ranges range)
957 ((eq action 'del) "-")
958 ((eq action 'add) "+")
959 ((eq action 'set) ""))
960 (mapconcat #'identity flags " ")))))))
961 ;; Wait for the last command to complete to avoid later
962 ;; syncronisation problems with the stream.
964 (nnimap-wait-for-response sequence))))))
966 (deffoo nnimap-request-accept-article (group &optional server last)
967 (when (nnimap-possibly-change-group nil server)
968 (nnmail-check-syntax)
969 (let ((message-id (message-field-value "message-id"))
972 (setq message (buffer-substring-no-properties (point-min) (point-max)))
973 (with-current-buffer (nnimap-buffer)
974 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
976 ;; If we have this group open read-only, then unselect it
977 ;; before appending to it.
978 (when (equal (nnimap-examined nnimap-object) group)
979 (nnimap-unselect-group))
981 (setq sequence (nnimap-send-command
982 "APPEND %S {%d}" (utf7-encode group t)
984 (unless nnimap-streaming
985 (nnimap-wait-for-connection "^[+]"))
986 (process-send-string (get-buffer-process (current-buffer)) message)
987 (process-send-string (get-buffer-process (current-buffer))
988 (if (nnimap-newlinep nnimap-object)
991 (let ((result (nnimap-get-response sequence)))
992 (if (not (nnimap-ok-p result))
994 (nnheader-report 'nnimap "%s" result)
997 (or (nnimap-find-uid-response "APPENDUID" (car result))
998 (nnimap-find-article-by-message-id
999 group message-id))))))))))
1001 (defun nnimap-process-quirk (greeting-match type data)
1002 (when (and (nnimap-greeting nnimap-object)
1003 (string-match greeting-match (nnimap-greeting nnimap-object))
1005 (string-match "\000" data))
1006 (let ((choice (gnus-multiple-choice
1007 "Message contains NUL characters. Delete, continue, abort? "
1008 '((?d "Delete NUL characters")
1009 (?c "Try to APPEND the message as is")
1013 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1019 (goto-char (point-min))
1020 (while (search-forward "\000" nil t)
1021 (replace-match "" t t))
1022 (buffer-string)))))))
1024 (defun nnimap-ok-p (value)
1027 (equal (caar value) "OK")))
1029 (defun nnimap-find-uid-response (name list)
1030 (let ((result (car (last (nnimap-find-response-element name list)))))
1032 (string-to-number result))))
1034 (defun nnimap-find-response-element (name list)
1037 (when (and (consp elem)
1038 (equal name (car elem)))
1039 (setq result elem)))
1042 (deffoo nnimap-request-replace-article (article group buffer)
1044 (when (and (nnimap-possibly-change-group group nil)
1045 ;; Put the article into the group.
1046 (with-current-buffer buffer
1048 (nnimap-request-accept-article group nil t))))
1049 (nnimap-delete-article (list article))
1050 ;; Return the new article number.
1053 (defun nnimap-add-cr ()
1054 (goto-char (point-min))
1055 (while (re-search-forward "\r?\n" nil t)
1056 (replace-match "\r\n" t t)))
1058 (defun nnimap-get-groups ()
1060 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1062 (nnimap-wait-for-response sequence)
1063 (subst-char-in-region (point-min) (point-max)
1065 (goto-char (point-min))
1066 (nnimap-unfold-quoted-lines)
1067 (goto-char (point-min))
1068 (while (search-forward "* LIST " nil t)
1069 (let ((flags (read (current-buffer)))
1070 (separator (read (current-buffer)))
1071 (group (read (current-buffer))))
1072 (unless (member '%NoSelect flags)
1073 (push (if (stringp group)
1075 (format "%s" group))
1079 (deffoo nnimap-request-list (&optional server)
1080 (when (nnimap-possibly-change-group nil server)
1081 (with-current-buffer nntp-server-buffer
1084 (with-current-buffer (nnimap-buffer)
1085 (nnimap-get-groups)))
1086 sequences responses)
1088 (with-current-buffer (nnimap-buffer)
1089 (setf (nnimap-group nnimap-object) nil)
1090 (dolist (group groups)
1091 (setf (nnimap-examined nnimap-object) group)
1092 (push (list (nnimap-send-command "EXAMINE %S"
1093 (utf7-encode group t))
1096 (nnimap-wait-for-response (caar sequences))
1098 (nnimap-get-responses (mapcar #'car sequences))))
1099 (dolist (response responses)
1100 (let* ((sequence (car response))
1101 (response (cadr response))
1102 (group (cadr (assoc sequence sequences))))
1104 (equal (caar response) "OK"))
1105 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1107 (dolist (elem response)
1108 (when (equal (cadr elem) "EXISTS")
1109 (setq exists (string-to-number (car elem)))))
1111 (setq highest (1- (string-to-number (car uidnext)))))
1114 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1117 (insert (format "%S %d %d y\n"
1118 (utf7-decode group t)
1119 highest (1+ highest))))
1121 ;; Return the widest possible range.
1122 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1123 (or highest exists)))))))))
1126 (deffoo nnimap-request-newgroups (date &optional server)
1127 (when (nnimap-possibly-change-group nil server)
1128 (with-current-buffer nntp-server-buffer
1130 (dolist (group (with-current-buffer (nnimap-buffer)
1131 (nnimap-get-groups)))
1132 (unless (assoc group nnimap-current-infos)
1133 ;; Insert dummy numbers here -- they don't matter.
1134 (insert (format "%S 0 1 y\n" group))))
1137 (deffoo nnimap-retrieve-group-data-early (server infos)
1138 (when (nnimap-possibly-change-group nil server)
1139 (with-current-buffer (nnimap-buffer)
1141 (setf (nnimap-group nnimap-object) nil)
1142 (let ((qresyncp (nnimap-capability "QRESYNC"))
1143 params groups sequences active uidvalidity modseq group)
1144 ;; Go through the infos and gather the data needed to know
1145 ;; what and how to request the data.
1146 (dolist (info infos)
1147 (setq params (gnus-info-params info)
1148 group (gnus-group-real-name (gnus-info-group info))
1149 active (cdr (assq 'active params))
1150 uidvalidity (cdr (assq 'uidvalidity params))
1151 modseq (cdr (assq 'modseq params)))
1152 (setf (nnimap-examined nnimap-object) group)
1158 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1159 (utf7-encode group t)
1160 (nnimap-quirk "QRESYNC")
1166 (if (and active uidvalidity)
1167 ;; Fetch the last 100 flags.
1168 (max 1 (- (cdr active) 100))
1173 ;; If we don't have a UIDVALIDITY, then this is
1174 ;; the first time we've seen the group, so we
1175 ;; have to do a SELECT (which is slower than an
1176 ;; examine), but will tell us whether the group
1177 ;; is read-only or not.
1179 (push (list (nnimap-send-command "%s %S" command
1180 (utf7-encode group t))
1181 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1182 start group command)
1186 (defun nnimap-quirk (command)
1187 (let ((quirk (assoc command nnimap-quirks)))
1188 ;; If this server is of a type that matches a quirk, then return
1189 ;; the "quirked" command instead of the proper one.
1190 (if (or (null quirk)
1191 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1195 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1196 (when (and sequences
1197 (nnimap-possibly-change-group nil server))
1198 (with-current-buffer (nnimap-buffer)
1199 ;; Wait for the final data to trickle in.
1200 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1204 ;; Now we should have most of the data we need, no matter
1205 ;; whether we're QRESYNCING, fetching all the flags from
1206 ;; scratch, or just fetching the last 100 flags per group.
1207 (nnimap-update-infos (nnimap-flags-to-marks
1209 (nreverse sequences)))
1211 ;; Finally, just return something resembling an active file in
1212 ;; the nntp buffer, so that the agent can save the info, too.
1213 (with-current-buffer nntp-server-buffer
1215 (dolist (info infos)
1216 (let* ((group (gnus-info-group info))
1217 (active (gnus-active group)))
1219 (insert (format "%S %d %d y\n"
1220 (gnus-group-real-name group)
1222 (car active)))))))))))
1224 (defun nnimap-update-infos (flags infos)
1225 (dolist (info infos)
1226 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1227 (marks (cdr (assoc group flags))))
1229 (nnimap-update-info info marks)))))
1231 (defun nnimap-update-info (info marks)
1232 (destructuring-bind (existing flags high low uidnext start-article
1233 permanent-flags uidvalidity
1234 vanished highestmodseq) marks
1236 ;; Ignore groups with no UIDNEXT/marks. This happens for
1237 ;; completely empty groups.
1238 ((and (not existing)
1240 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1242 (gnus-set-active (gnus-info-group info) active))))
1243 ;; We have a mismatch between the old and new UIDVALIDITY
1244 ;; identifiers, so we have to re-request the group info (the next
1245 ;; time). This virtually never happens.
1246 ((let ((old-uidvalidity
1247 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1248 (and old-uidvalidity
1249 (not (equal old-uidvalidity uidvalidity))
1250 (> start-article 1)))
1251 (gnus-group-remove-parameter info 'uidvalidity)
1252 (gnus-group-remove-parameter info 'modseq))
1253 ;; We have the data needed to update.
1255 (let* ((group (gnus-info-group info))
1256 (completep (and start-article
1257 (= start-article 1)))
1258 (active (or (gnus-active group)
1259 (cdr (assq 'active (gnus-info-params info))))))
1261 (setq high (1- uidnext)))
1262 ;; First set the active ranges based on high/low.
1264 (not (gnus-active group)))
1265 (gnus-set-active group
1268 (cons (min (or low (car active))
1270 (max (or high (cdr active))
1275 ;; No articles in this group.
1276 (cons uidnext (1- uidnext)))
1278 (cons start-article (1- start-article)))
1280 ;; No articles and no uidnext.
1282 (gnus-set-active group
1284 (or high (1- uidnext)))))
1285 ;; See whether this is a read-only group.
1286 (unless (eq permanent-flags 'not-scanned)
1287 (gnus-group-set-parameter
1288 info 'permanent-flags
1289 (and (or (memq '%* permanent-flags)
1290 (memq '%Seen permanent-flags))
1292 ;; Update marks and read articles if this isn't a
1293 ;; read-only IMAP group.
1294 (when (setq permanent-flags
1295 (cdr (assq 'permanent-flags (gnus-info-params info))))
1296 (if (and highestmodseq
1297 (not start-article))
1298 ;; We've gotten the data by QRESYNCing.
1299 (nnimap-update-qresync-info
1300 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1301 ;; Do normal non-QRESYNC flag updates.
1302 ;; Update the list of read articles.
1304 (gnus-compress-sequence
1305 (gnus-set-difference
1306 (gnus-set-difference
1308 (cdr (assoc '%Seen flags)))
1309 (cdr (assoc '%Flagged flags)))))
1310 (read (gnus-range-difference
1311 (cons start-article high) unread)))
1312 (when (> start-article 1)
1315 (if (> start-article 1)
1316 (gnus-sorted-range-intersection
1317 (cons 1 (1- start-article))
1318 (gnus-info-read info))
1319 (gnus-info-read info))
1321 (when (or (not (listp permanent-flags))
1322 (memq '%Seen permanent-flags))
1323 (gnus-info-set-read info read))
1324 ;; Update the marks.
1325 (setq marks (gnus-info-marks info))
1326 (dolist (type (cdr nnimap-mark-alist))
1327 (when (or (not (listp permanent-flags))
1328 (memq (car (assoc (caddr type) flags))
1330 (memq '%* permanent-flags))
1331 (let ((old-marks (assoc (car type) marks))
1333 (gnus-compress-sequence
1334 (cdr (or (assoc (caddr type) flags) ; %Flagged
1335 (assoc (intern (cadr type) obarray) flags)
1336 (assoc (cadr type) flags)))))) ; "\Flagged"
1337 (setq marks (delq old-marks marks))
1339 (when (and old-marks
1340 (> start-article 1))
1341 (setq old-marks (gnus-range-difference
1343 (cons start-article high)))
1344 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1346 (push (cons (car type) new-marks) marks)))))
1347 (gnus-info-set-marks info marks t))))
1348 ;; Tell Gnus whether there are any \Recent messages in any of
1350 (let ((recent (cdr (assoc '%Recent flags))))
1353 (> (car (last recent)) (cdr active)))
1354 (push (list (cons (gnus-group-real-name group) 0))
1355 nnmail-split-history)))
1356 ;; Note the active level for the next run-through.
1357 (gnus-group-set-parameter info 'active (gnus-active group))
1358 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1359 (gnus-group-set-parameter info 'modseq highestmodseq)
1360 (nnimap-store-info info (gnus-active group)))))))
1362 (defun nnimap-update-qresync-info (info existing vanished flags)
1363 ;; Add all the vanished articles to the list of read articles.
1368 (gnus-range-add (gnus-info-read info)
1370 (cdr (assq '%Flagged flags)))
1371 (cdr (assq '%Seen flags))))
1372 (let ((marks (gnus-info-marks info)))
1373 (dolist (type (cdr nnimap-mark-alist))
1374 (let ((ticks (assoc (car type) marks))
1376 (cdr (or (assoc (caddr type) flags) ; %Flagged
1377 (assoc (intern (cadr type) obarray) flags)
1378 (assoc (cadr type) flags))))) ; "\Flagged"
1379 (setq marks (delq ticks marks))
1381 ;; Add the new marks we got.
1382 (setq ticks (gnus-add-to-range ticks new-marks))
1383 ;; Remove the marks from messages that don't have them.
1384 (setq ticks (gnus-remove-from-range
1386 (gnus-compress-sequence
1387 (gnus-sorted-complement existing new-marks))))
1389 (push (cons (car type) ticks) marks)))
1390 (gnus-info-set-marks info marks t))))
1392 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1393 (if (zerop (length irange))
1396 (dolist (elem (split-string irange ","))
1398 (if (string-match ":" elem)
1399 (let ((numbers (split-string elem ":")))
1400 (cons (string-to-number (car numbers))
1401 (string-to-number (cadr numbers))))
1402 (string-to-number elem))
1404 (nreverse result))))
1406 (defun nnimap-store-info (info active)
1407 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1408 (entry (assoc group nnimap-current-infos)))
1410 (setcdr entry (list info active))
1411 (push (list group info active) nnimap-current-infos))))
1413 (defun nnimap-flags-to-marks (groups)
1414 (let (data group totalp uidnext articles start-article mark permanent-flags
1415 uidvalidity vanished highestmodseq)
1416 (dolist (elem groups)
1417 (setq group (car elem)
1418 uidnext (nth 1 elem)
1419 start-article (nth 2 elem)
1420 permanent-flags (nth 3 elem)
1421 uidvalidity (nth 4 elem)
1422 vanished (nth 5 elem)
1423 highestmodseq (nth 6 elem)
1424 articles (nthcdr 7 elem))
1425 (let ((high (caar articles))
1427 (dolist (article articles)
1428 (setq low (car article))
1429 (push (car article) existing)
1430 (dolist (flag (cdr article))
1431 (setq mark (assoc flag marks))
1433 (push (list flag (car article)) marks)
1434 (setcdr mark (cons (car article) (cdr mark))))))
1435 (push (list group existing marks high low uidnext start-article
1436 permanent-flags uidvalidity vanished highestmodseq)
1440 (defun nnimap-parse-flags (sequences)
1441 (goto-char (point-min))
1442 ;; Change \Delete etc to %Delete, so that the reader can read it.
1443 (subst-char-in-region (point-min) (point-max)
1445 (let (start end articles groups uidnext elems permanent-flags
1446 uidvalidity vanished highestmodseq)
1447 (dolist (elem sequences)
1448 (destructuring-bind (group-sequence flag-sequence totalp group command)
1450 (setq start (point))
1452 ;; The EXAMINE was successful.
1453 (search-forward (format "\n%d OK " group-sequence) nil t)
1458 (setq permanent-flags
1459 (if (equal command "SELECT")
1460 (and (search-forward "PERMANENTFLAGS "
1461 (or end (point-min)) t)
1462 (read (current-buffer)))
1466 (and (search-forward "UIDNEXT "
1467 (or end (point-min)) t)
1468 (read (current-buffer))))
1471 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1472 (or end (point-min)) t)
1473 ;; Store UIDVALIDITY as a string, as it's
1474 ;; too big for 32-bit Emacsen, usually.
1478 (and (eq flag-sequence 'qresync)
1479 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1480 (or end (point-min)) t)
1484 (and (search-forward "HIGHESTMODSEQ "
1485 (or end (point-min)) t)
1486 (read (current-buffer))))
1489 ;; The UID FETCH FLAGS was successful.
1490 (or (eq flag-sequence 'qresync)
1491 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1492 (if (eq flag-sequence 'qresync)
1496 (setq start (point))
1498 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1500 ;; FIXME: For FETCH lines like "* 2971 FETCH (FLAGS (%Recent) UID
1501 ;; 12509 MODSEQ (13419098521433281274))" we get an
1502 ;; overflow-error. The handler simply deletes that large number
1503 ;; and reads again. But maybe there's a better fix...
1504 (setq elems (condition-case nil (read (current-buffer))
1506 ;; After an overflow-error, point is just after
1507 ;; the too large number. So delete it and try
1509 (delete-region (point) (progn (backward-word) (point)))
1511 (read (current-buffer)))))
1512 (push (cons (cadr (memq 'UID elems))
1513 (cadr (memq 'FLAGS elems)))
1515 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1516 vanished highestmodseq)
1520 (setq articles nil))))
1523 (defun nnimap-find-process-buffer (buffer)
1524 (cadr (assoc buffer nnimap-connection-alist)))
1526 (deffoo nnimap-request-post (&optional server)
1527 (setq nnimap-status-string "Read-only server")
1530 (declare-function gnus-fetch-headers "gnus-sum"
1531 (articles &optional limit force-new dependencies))
1533 (deffoo nnimap-request-thread (header)
1534 (let* ((id (mail-header-id header))
1536 (or (mail-header-references header)
1540 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1542 (dolist (refid refs value)
1544 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1545 refid refid value)))))
1546 (result (with-current-buffer (nnimap-buffer)
1547 (nnimap-command "UID SEARCH %s" cmd))))
1550 (and (car result) (delete 0 (mapcar #'string-to-number
1551 (cdr (assoc "SEARCH" (cdr result))))))
1554 (defun nnimap-possibly-change-group (group server)
1555 (let ((open-result t))
1557 (not (nnimap-server-opened server)))
1558 (setq open-result (nnimap-open-server server)))
1565 (with-current-buffer (nnimap-buffer)
1566 (if (equal group (nnimap-group nnimap-object))
1568 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1570 (setf (nnimap-group nnimap-object) group
1571 (nnimap-select-result nnimap-object) result)
1574 (defun nnimap-find-connection (buffer)
1575 "Find the connection delivering to BUFFER."
1576 (let ((entry (assoc buffer nnimap-connection-alist)))
1578 (if (and (buffer-name (cadr entry))
1579 (get-buffer-process (cadr entry))
1580 (memq (process-status (get-buffer-process (cadr entry)))
1582 (get-buffer-process (cadr entry))
1583 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1586 (defvar nnimap-sequence 0)
1588 (defun nnimap-send-command (&rest args)
1589 (setf (nnimap-last-command-time nnimap-object) (current-time))
1590 (process-send-string
1591 (get-buffer-process (current-buffer))
1594 (incf nnimap-sequence)
1595 (apply #'format args)
1596 (if (nnimap-newlinep nnimap-object)
1599 ;; Some servers apparently can't have many outstanding
1600 ;; commands, so throttle them.
1601 (unless nnimap-streaming
1602 (nnimap-wait-for-response nnimap-sequence))
1605 (defun nnimap-log-command (command)
1606 (with-current-buffer (get-buffer-create "*imap log*")
1607 (goto-char (point-max))
1608 (insert (format-time-string "%H:%M:%S") " "
1609 (if nnimap-inhibit-logging
1614 (defun nnimap-command (&rest args)
1616 (let* ((sequence (apply #'nnimap-send-command args))
1617 (response (nnimap-get-response sequence)))
1618 (if (equal (caar response) "OK")
1620 (nnheader-report 'nnimap "%s"
1621 (mapconcat (lambda (a)
1623 (car response) " "))
1626 (defun nnimap-get-response (sequence)
1627 (nnimap-wait-for-response sequence)
1628 (nnimap-parse-response))
1630 (defun nnimap-wait-for-connection (&optional regexp)
1631 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1633 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1634 (let ((process (get-buffer-process (current-buffer))))
1635 (goto-char (point-min))
1636 (while (and (memq (process-status process)
1638 (not (re-search-forward regexp nil t)))
1639 (nnheader-accept-process-output process)
1640 (goto-char (point-min)))
1642 (and (looking-at (or response-regexp regexp))
1645 (defun nnimap-wait-for-response (sequence &optional messagep)
1646 (let ((process (get-buffer-process (current-buffer)))
1650 (goto-char (point-max))
1651 (while (and (setq openp (memq (process-status process)
1654 ;; Skip past any "*" lines that the server has
1656 (while (and (not (bobp))
1659 (looking-at "\\*"))))
1660 (not (looking-at (format "%d .*\n" sequence)))))
1662 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1663 (nnheader-accept-process-output process)
1664 (goto-char (point-max)))
1667 ;; The user hit C-g while we were waiting: kill the process, in case
1668 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1670 (delete-process process)
1673 (defun nnimap-parse-response ()
1674 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1676 (dolist (line lines)
1677 (push (cdr (nnimap-parse-line line)) result))
1678 ;; Return the OK/error code first, and then all the "continuation
1679 ;; lines" afterwards.
1681 (nreverse result))))
1683 ;; Parse an IMAP response line lightly. They look like
1684 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1685 ;; the lines into a list of strings and lists of string.
1686 (defun nnimap-parse-line (line)
1689 (mm-disable-multibyte)
1691 (goto-char (point-min))
1693 (if (eql (setq char (following-char)) ? )
1701 (if (search-forward "]" (line-end-position) 'move)
1708 (if (search-forward ")" (line-end-position) 'move)
1715 (1- (or (search-forward "\"" (line-end-position) 'move)
1718 (buffer-substring (point) (if (search-forward " " nil t)
1720 (goto-char (point-max))))))
1722 (nreverse result))))
1724 (defun nnimap-last-response-string ()
1727 (let ((end (point)))
1731 (while (and (not (bobp))
1732 (eql (following-char) ?*))
1734 (unless (eql (following-char) ?*)
1736 (buffer-substring (point) end))))
1738 (defun nnimap-get-responses (sequences)
1740 (dolist (sequence sequences)
1741 (goto-char (point-min))
1742 (when (re-search-forward (format "^%d " sequence) nil t)
1743 (push (list sequence (nnimap-parse-response))
1747 (defvar nnimap-incoming-split-list nil)
1749 (defun nnimap-fetch-inbox (articles)
1751 (nnimap-wait-for-response
1752 (nnimap-send-command
1754 (nnimap-article-ranges articles)
1755 (format "(UID %s%s)"
1758 "BODY.PEEK[HEADER] BODY.PEEK"
1760 (if nnimap-split-download-body-default
1765 (defun nnimap-split-incoming-mail ()
1766 (with-current-buffer (nnimap-buffer)
1767 (let ((nnimap-incoming-split-list nil)
1768 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1769 nnmail-split-methods
1770 nnimap-split-methods))
1771 (nnmail-split-fancy (or nnimap-split-fancy
1772 nnmail-split-fancy))
1773 (nnmail-inhibit-default-split-group t)
1774 (groups (nnimap-get-groups))
1777 (nnimap-command "SELECT %S" nnimap-inbox)
1778 (setf (nnimap-group nnimap-object) nnimap-inbox)
1779 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1781 (nnimap-fetch-inbox new-articles)
1782 (nnimap-transform-split-mail)
1783 (nnheader-ms-strip-cr)
1785 (nnmail-split-incoming (current-buffer)
1786 #'nnimap-save-mail-spec
1788 #'nnimap-dummy-active-number
1789 #'nnimap-save-mail-spec)
1790 (when nnimap-incoming-split-list
1791 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1792 sequences junk-articles)
1793 ;; Create any groups that doesn't already exist on the
1795 (dolist (spec specs)
1796 (when (and (not (member (car spec) groups))
1797 (not (eq (car spec) 'junk)))
1798 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1799 ;; Then copy over all the messages.
1801 (dolist (spec specs)
1802 (let ((group (car spec))
1803 (ranges (cdr spec)))
1804 (if (eq group 'junk)
1805 (setq junk-articles ranges)
1806 (push (list (nnimap-send-command
1808 (nnimap-article-ranges ranges)
1809 (utf7-encode group t))
1812 ;; Wait for the last COPY response...
1814 (nnimap-wait-for-response (caar sequences))
1815 ;; And then mark the successful copy actions as deleted,
1816 ;; and possibly expunge them.
1817 (nnimap-mark-and-expunge-incoming
1818 (nnimap-parse-copied-articles sequences)))
1819 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1821 (defun nnimap-mark-and-expunge-incoming (range)
1823 (setq range (nnimap-article-ranges range))
1826 (nnimap-send-command
1827 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1829 ;; If the server supports it, we now delete the message we have
1830 ;; just copied over.
1831 ((nnimap-capability "UIDPLUS")
1832 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1833 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1834 ;; user has configured it.
1836 (setq sequence (nnimap-send-command "EXPUNGE"))))
1837 (nnimap-wait-for-response sequence))))
1839 (defun nnimap-parse-copied-articles (sequences)
1840 (let (sequence copied range)
1841 (goto-char (point-min))
1842 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
1843 (setq sequence (string-to-number (match-string 1)))
1844 (when (setq range (cadr (assq sequence sequences)))
1845 (push (gnus-uncompress-range range) copied)))
1846 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1848 (defun nnimap-new-articles (flags)
1850 (dolist (elem flags)
1851 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1853 (push (car elem) new)))
1854 (gnus-compress-sequence (nreverse new))))
1856 (defun nnimap-make-split-specs (list)
1860 (destructuring-bind (article spec) elem
1861 (dolist (group (delete nil (mapcar #'car spec)))
1862 (unless (setq entry (assoc group specs))
1863 (push (setq entry (list group)) specs))
1864 (setcdr entry (cons article (cdr entry))))))
1865 (dolist (entry specs)
1866 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1869 (defun nnimap-transform-split-mail ()
1870 (goto-char (point-min))
1871 (let (article bytes)
1874 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1875 (delete-region (point) (progn (forward-line 1) (point)))
1878 (setq article (match-string 1)
1879 bytes (nnimap-get-length))
1880 (delete-region (line-beginning-position) (line-end-position))
1881 ;; Insert MMDF separator, and a way to remember what this
1883 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1884 (forward-char (1+ bytes))
1885 (setq bytes (nnimap-get-length))
1886 (delete-region (line-beginning-position) (line-end-position))
1887 ;; There's a body; skip past that.
1889 (forward-char (1+ bytes))
1890 (delete-region (line-beginning-position) (line-end-position)))))))
1892 (defun nnimap-dummy-active-number (group &optional server)
1895 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1897 (goto-char (point-min))
1898 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1899 (error "Invalid nnimap mail")
1900 (setq article (string-to-number (match-string 1))))
1902 (if (eq group-art 'junk)
1903 (list (cons 'junk 1))
1905 nnimap-incoming-split-list)))
1909 ;;; nnimap.el ends here