1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2012 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
))))
35 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
36 ;; `make-network-stream'.
37 (unless (fboundp 'open-protocol-stream
)
38 (require 'proto-stream
)))
53 (autoload 'auth-source-forget
+ "auth-source")
54 (autoload 'auth-source-search
"auth-source")
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-user nil
62 "Username to use for authentication to the IMAP server.")
64 (defvoo nnimap-server-port nil
66 If nnimap-stream is `ssl', this will default to `imaps'. If not,
67 it will default to `imap'.")
69 (defvoo nnimap-stream
'undecided
70 "How nnimap talks to the IMAP server.
71 The value should be either `undecided', `ssl' or `tls',
72 `network', `starttls', `plain', or `shell'.
74 If the value is `undecided', nnimap tries `ssl' first, then falls
77 (defvoo nnimap-shell-program
(if (boundp 'imap-shell-program
)
78 (if (listp imap-shell-program
)
79 (car imap-shell-program
)
83 (defvoo nnimap-inbox nil
84 "The mail box where incoming mail arrives and should be split out of.
85 For example, \"INBOX\".")
87 (defvoo nnimap-split-methods nil
89 Uses the same syntax as `nnmail-split-methods'.")
91 (defvoo nnimap-split-fancy nil
92 "Uses the same syntax as `nnmail-split-fancy'.")
94 (defvoo nnimap-unsplittable-articles
'(%Deleted %Seen
)
95 "Articles with the flags in the list will not be considered when splitting.")
97 (make-obsolete-variable 'nnimap-split-rule
"see `nnimap-split-methods'"
100 (defvoo nnimap-authenticator nil
101 "How nnimap authenticate itself to the server.
102 Possible choices are nil (use default methods) or `anonymous'.")
104 (defvoo nnimap-expunge t
105 "If non-nil, expunge articles after deleting them.
106 This is always done if the server supports UID EXPUNGE, but it's
107 not done by default on servers that doesn't support that command.")
109 (defvoo nnimap-streaming t
110 "If non-nil, try to use streaming commands with IMAP servers.
111 Switching this off will make nnimap slower, but it helps with
114 (defvoo nnimap-connection-alist nil
)
116 (defvoo nnimap-current-infos nil
)
118 (defvoo nnimap-fetch-partial-articles nil
119 "If non-nil, Gnus will fetch partial articles.
120 If t, Gnus will fetch only the first part. If a string, it
121 will fetch all parts that have types that match that string. A
122 likely value would be \"text/\" to automatically fetch all
125 (defvar nnimap-process nil
)
127 (defvar nnimap-status-string
"")
129 (defvar nnimap-split-download-body-default nil
130 "Internal variable with default value for `nnimap-split-download-body'.")
132 (defvar nnimap-keepalive-timer nil
)
133 (defvar nnimap-process-buffers nil
)
136 group process commands capabilities select-result newlinep server
137 last-command-time greeting examined stream-type initial-resync
)
139 (defvar nnimap-object nil
)
141 (defvar nnimap-mark-alist
142 '((read "\\Seen" %Seen
)
143 (tick "\\Flagged" %Flagged
)
144 (reply "\\Answered" %Answered
)
145 (expire "gnus-expire")
146 (dormant "gnus-dormant")
149 (download "gnus-download")
150 (forward "gnus-forward")))
152 (defvar nnimap-quirks
153 '(("QRESYNC" "Zimbra" "QRESYNC ")))
155 (defvar nnimap-inhibit-logging nil
)
157 (defun nnimap-buffer ()
158 (nnimap-find-process-buffer nntp-server-buffer
))
160 (defun nnimap-header-parameters ()
161 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
164 "BODY.PEEK[HEADER.FIELDS %s]"
165 "RFC822.HEADER.LINES %s")
166 (append '(Subject From Date Message-Id
167 References In-Reply-To Xref
)
168 nnmail-extra-headers
))))
170 (deffoo nnimap-retrieve-headers
(articles &optional group server fetch-old
)
172 (setq group
(nnimap-decode-gnus-group group
)))
173 (with-current-buffer nntp-server-buffer
175 (when (nnimap-possibly-change-group group server
)
176 (with-current-buffer (nnimap-buffer)
178 (nnimap-wait-for-response
181 (nnimap-article-ranges (gnus-compress-sequence articles
))
182 (nnimap-header-parameters))
184 (nnimap-transform-headers)
185 (nnheader-remove-cr-followed-by-lf))
186 (insert-buffer-substring
187 (nnimap-find-process-buffer (current-buffer))))
190 (defun nnimap-transform-headers ()
191 (goto-char (point-min))
192 (let (article lines size string
)
195 (while (not (looking-at "\\* [0-9]+ FETCH"))
196 (delete-region (point) (progn (forward-line 1) (point)))
199 (goto-char (match-end 0))
200 ;; Unfold quoted {number} strings.
201 (while (re-search-forward
202 "[^]][ (]{\\([0-9]+\\)}\r?\n"
204 ;; Start of the header section.
205 (or (re-search-forward "] {[0-9]+}\r?\n" nil t
)
206 ;; Start of the next FETCH.
207 (re-search-forward "\\* [0-9]+ FETCH" nil t
)
210 (setq size
(string-to-number (match-string 1)))
211 (delete-region (+ (match-beginning 0) 2) (point))
212 (setq string
(buffer-substring (point) (+ (point) size
)))
213 (delete-region (point) (+ (point) size
))
214 (insert (format "%S" (mm-subst-char-in-string ?
\n ?\s string
))))
217 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
222 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
227 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
228 (let ((structure (ignore-errors
229 (read (current-buffer)))))
230 (while (and (consp structure
)
231 (not (atom (car structure
))))
232 (setq structure
(car structure
)))
234 (stringp (car structure
))
235 (equal (upcase (nth 0 structure
)) "MESSAGE")
236 (equal (upcase (nth 1 structure
)) "RFC822"))
238 (nth 7 structure
)))))
239 (delete-region (line-beginning-position) (line-end-position))
240 (insert (format "211 %s Article retrieved." article
))
243 (insert (format "Chars: %s\n" size
)))
245 (insert (format "Lines: %s\n" lines
)))
246 (unless (re-search-forward "^\r$" nil t
)
247 (goto-char (point-max)))
248 (delete-region (line-beginning-position) (line-end-position))
252 (defun nnimap-unfold-quoted-lines ()
253 ;; Unfold quoted {number} strings.
255 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
256 (setq size
(string-to-number (match-string 1)))
257 (delete-region (1+ (match-beginning 0)) (point))
258 (setq string
(buffer-substring (point) (+ (point) size
)))
259 (delete-region (point) (+ (point) size
))
260 (insert (format "%S" string
)))))
262 (defun nnimap-get-length ()
263 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
264 (string-to-number (match-string 1))))
266 (defun nnimap-article-ranges (ranges)
270 (number-to-string ranges
))
271 ((numberp (cdr ranges
))
272 (format "%d:%d" (car ranges
) (cdr ranges
)))
274 (dolist (elem ranges
)
277 (format "%d:%d" (car elem
) (cdr elem
))
278 (number-to-string elem
))
280 (mapconcat #'identity
(nreverse result
) ",")))))
282 (deffoo nnimap-open-server
(server &optional defs no-reconnect
)
283 (if (nnimap-server-opened server
)
285 (unless (assq 'nnimap-address defs
)
286 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
287 (nnoo-change-server 'nnimap server defs
)
289 (nnimap-find-connection nntp-server-buffer
)
290 (or (nnimap-find-connection nntp-server-buffer
)
291 (nnimap-open-connection nntp-server-buffer
)))))
293 (defun nnimap-make-process-buffer (buffer)
295 (generate-new-buffer (format " *nnimap %s %s %s*"
296 nnimap-address nnimap-server-port
297 (gnus-buffer-exists-p buffer
)))
298 (mm-disable-multibyte)
299 (buffer-disable-undo)
301 (set (make-local-variable 'after-change-functions
) nil
)
302 (set (make-local-variable 'nnimap-object
)
303 (make-nnimap :server
(nnoo-current-server 'nnimap
)
305 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
306 (push (current-buffer) nnimap-process-buffers
)
309 (defun nnimap-credentials (address ports user
)
310 (let* ((auth-source-creation-prompts
311 '((user .
"IMAP user at %h: ")
312 (secret .
"IMAP password for %u@%h: ")))
313 (found (nth 0 (auth-source-search :max
1
317 :require
'(:user
:secret
)
320 (list (plist-get found
:user
)
321 (let ((secret (plist-get found
:secret
)))
322 (if (functionp secret
)
325 (plist-get found
:save-function
))
328 (defun nnimap-keepalive ()
329 (let ((now (current-time)))
330 (dolist (buffer nnimap-process-buffers
)
331 (when (buffer-name buffer
)
332 (with-current-buffer buffer
333 (when (and nnimap-object
334 (nnimap-last-command-time nnimap-object
)
338 (nnimap-last-command-time nnimap-object
)))
339 ;; More than five minutes since the last command.
341 (nnimap-send-command "NOOP")))))))
343 (defun nnimap-open-connection (buffer)
344 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
345 ;; `ssl' when nnimap-server-port was nil. Sort of.
346 (when (and nnimap-server-port
347 (eq nnimap-stream
'undecided
))
348 (setq nnimap-stream
'ssl
))
350 (if (eq nnimap-stream
'undecided
)
351 (loop for type in
'(ssl network
)
352 for stream
= (let ((nnimap-stream type
))
353 (nnimap-open-connection-1 buffer
))
354 while
(eq stream
'no-connect
)
355 finally
(return stream
))
356 (nnimap-open-connection-1 buffer
))))
357 (if (eq stream
'no-connect
)
361 (defun nnimap-map-port (port)
362 (if (equal port
"imaps")
366 (defun nnimap-open-connection-1 (buffer)
367 (unless nnimap-keepalive-timer
368 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
370 (with-current-buffer (nnimap-make-process-buffer buffer
)
371 (let* ((coding-system-for-read 'binary
)
372 (coding-system-for-write 'binary
)
375 ((memq nnimap-stream
'(network plain starttls
))
376 (nnheader-message 7 "Opening connection to %s..."
379 ((eq nnimap-stream
'shell
)
380 (nnheader-message 7 "Opening connection to %s via shell..."
383 ((memq nnimap-stream
'(ssl tls
))
384 (nnheader-message 7 "Opening connection to %s via tls..."
386 '("imaps" "imap" "993" "143"))
388 (error "Unknown stream type: %s" nnimap-stream
))))
389 login-result credentials
)
390 (when nnimap-server-port
391 (push nnimap-server-port ports
))
393 (open-protocol-stream
394 "*nnimap*" (current-buffer) nnimap-address
395 (nnimap-map-port (car ports
))
398 :shell-command nnimap-shell-program
399 :capability-command
"1 CAPABILITY\r\n"
400 :end-of-command
"\r\n"
403 (lambda (capabilities)
404 (when (gnus-string-match-p "STARTTLS" capabilities
)
406 (stream (car stream-list
))
407 (props (cdr stream-list
))
408 (greeting (plist-get props
:greeting
))
409 (capabilities (plist-get props
:capabilities
))
410 (stream-type (plist-get props
:type
)))
411 (when (and stream
(not (memq (process-status stream
) '(open run
))))
414 (when (and (fboundp 'set-network-process-option
) ;; Not in XEmacs.
415 (fboundp 'process-type
) ;; Emacs 22 doesn't provide it.
416 (eq (process-type stream
) 'network
))
417 ;; Use TCP-keepalive so that connections that pass through a NAT
418 ;; router don't hang when left idle.
419 (set-network-process-option stream
:keepalive t
))
421 (setf (nnimap-process nnimap-object
) stream
)
422 (setf (nnimap-stream-type nnimap-object
) stream-type
)
425 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
426 nnimap-address
(car ports
) nnimap-stream
)
428 (gnus-set-process-query-on-exit-flag stream nil
)
429 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
430 (nnheader-report 'nnimap
"%s" greeting
)
431 ;; Store the greeting (for debugging purposes).
432 (setf (nnimap-greeting nnimap-object
) greeting
)
433 (setf (nnimap-capabilities nnimap-object
)
435 (split-string capabilities
)))
436 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
437 (if (not (setq credentials
438 (if (eq nnimap-authenticator
'anonymous
)
440 (message-make-address))
441 ;; Look for the credentials based on
442 ;; the virtual server name and the address
444 (gnus-delete-duplicates
447 (nnoo-current-server 'nnimap
)))
450 (setq nnimap-object nil
)
451 (let ((nnimap-inhibit-logging t
))
453 (nnimap-login (car credentials
) (cadr credentials
))))
454 (if (car login-result
)
456 ;; Save the credentials if a save function exists
457 ;; (such a function will only be passed if a new
458 ;; token was created).
459 (when (functionp (nth 2 credentials
))
460 (funcall (nth 2 credentials
)))
461 ;; See if CAPABILITY is set as part of login
463 (dolist (response (cddr login-result
))
464 (when (string= "CAPABILITY" (upcase (car response
)))
465 (setf (nnimap-capabilities nnimap-object
)
466 (mapcar #'upcase
(cdr response
))))))
467 ;; If the login failed, then forget the credentials
468 ;; that are now possibly cached.
469 (dolist (host (list (nnoo-current-server 'nnimap
)
472 (auth-source-forget+ :host host
:port port
)))
473 (delete-process (nnimap-process nnimap-object
))
474 (setq nnimap-object nil
))))
476 (when (nnimap-capability "QRESYNC")
477 (nnimap-command "ENABLE QRESYNC"))
478 (nnheader-message 7 "Opening connection to %s...done"
480 (nnimap-process nnimap-object
))))))))
482 (autoload 'rfc2104-hash
"rfc2104")
484 (defun nnimap-login (user password
)
486 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
487 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
488 ;; and we're using an encrypted connection.
489 ((and (not (nnimap-capability "LOGINDISABLED"))
490 (eq (nnimap-stream-type nnimap-object
) 'tls
))
491 (nnimap-command "LOGIN %S %S" user password
))
492 ((nnimap-capability "AUTH=CRAM-MD5")
494 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
495 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
497 (get-buffer-process (current-buffer))
499 (base64-encode-string
501 (rfc2104-hash 'md5
64 16 password
502 (base64-decode-string challenge
))))
504 (nnimap-wait-for-response sequence
)))
505 ((not (nnimap-capability "LOGINDISABLED"))
506 (nnimap-command "LOGIN %S %S" user password
))
507 ((nnimap-capability "AUTH=PLAIN")
509 "AUTHENTICATE PLAIN %s"
510 (base64-encode-string
511 (format "\000%s\000%s"
512 (nnimap-quote-specials user
)
513 (nnimap-quote-specials password
)))))))
515 (defun nnimap-quote-specials (string)
518 (goto-char (point-min))
519 (while (re-search-forward "[\\\"]" nil t
)
525 (defun nnimap-find-parameter (parameter elems
)
529 ((equal (car elem
) parameter
)
530 (setq result
(cdr elem
)))
531 ((and (equal (car elem
) "OK")
533 (equal (caadr elem
) parameter
))
534 (setq result
(cdr (cadr elem
))))))
537 (deffoo nnimap-close-server
(&optional server
)
538 (when (nnoo-change-server 'nnimap server nil
)
540 (delete-process (get-buffer-process (nnimap-buffer))))
541 (nnoo-close-server 'nnimap server
)
544 (deffoo nnimap-request-close
()
547 (deffoo nnimap-server-opened
(&optional server
)
548 (and (nnoo-current-server-p 'nnimap server
)
550 (gnus-buffer-live-p nntp-server-buffer
)
551 (nnimap-find-connection nntp-server-buffer
)))
553 (deffoo nnimap-status-message
(&optional server
)
554 nnimap-status-string
)
556 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
558 (setq group
(nnimap-decode-gnus-group group
)))
559 (with-current-buffer nntp-server-buffer
560 (let ((result (nnimap-possibly-change-group group server
))
562 (when (stringp article
)
563 (setq article
(nnimap-find-article-by-message-id group article
)))
567 (with-current-buffer (nnimap-buffer)
569 (when nnimap-fetch-partial-articles
570 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
571 (goto-char (point-min))
572 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
573 (setq structure
(ignore-errors
574 (let ((start (point)))
576 (downcase-region start
(point))
578 (read (current-buffer))))
579 parts
(nnimap-find-wanted-parts structure
))))
581 (nnimap-get-partial-article article parts structure
)
582 (nnimap-get-whole-article article
))
583 (let ((buffer (current-buffer)))
584 (with-current-buffer (or to-buffer nntp-server-buffer
)
585 (nnheader-insert-buffer-substring buffer
)
586 (nnheader-ms-strip-cr)))
587 (cons group article
)))))))
589 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
591 (setq group
(nnimap-decode-gnus-group group
)))
592 (when (nnimap-possibly-change-group group server
)
593 (with-current-buffer (nnimap-buffer)
594 (when (stringp article
)
595 (setq article
(nnimap-find-article-by-message-id group article
)))
598 (nnimap-get-whole-article
599 article
(format "UID FETCH %%d %s"
600 (nnimap-header-parameters)))
601 (let ((buffer (current-buffer)))
602 (with-current-buffer (or to-buffer nntp-server-buffer
)
604 (insert-buffer-substring buffer
)
605 (nnheader-ms-strip-cr)
606 (cons group article
)))))))
608 (defun nnimap-get-whole-article (article &optional command
)
613 "UID FETCH %d BODY.PEEK[]"
614 "UID FETCH %d RFC822.PEEK"))
616 ;; Check that we really got an article.
617 (goto-char (point-min))
618 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
621 ;; Remove any data that may have arrived before the FETCH data.
624 (delete-region (point-min) (point)))
625 (let ((bytes (nnimap-get-length)))
626 (delete-region (line-beginning-position)
627 (progn (forward-line 1) (point)))
628 (goto-char (+ (point) bytes
))
629 (delete-region (point) (point-max)))
632 (defun nnimap-capability (capability)
633 (member capability
(nnimap-capabilities nnimap-object
)))
635 (defun nnimap-ver4-p ()
636 (nnimap-capability "IMAP4REV1"))
638 (defun nnimap-get-partial-article (article parts structure
)
641 "UID FETCH %d (%s %s)"
647 (mapconcat (lambda (part)
648 (format "BODY.PEEK[%s]" part
))
650 (mapconcat (lambda (part)
651 (format "RFC822.PEEK[%s]" part
))
654 (nnimap-convert-partial-article structure
))))
656 (defun nnimap-convert-partial-article (structure)
657 ;; First just skip past the headers.
658 (goto-char (point-min))
659 (let ((bytes (nnimap-get-length))
661 ;; Delete "FETCH" line.
662 (delete-region (line-beginning-position)
663 (progn (forward-line 1) (point)))
664 (goto-char (+ (point) bytes
))
665 ;; Collect all the body parts.
666 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
667 (setq id
(match-string 1)
668 bytes
(or (nnimap-get-length) 0))
670 (delete-region (point) (progn (forward-line 1) (point)))
671 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
673 (delete-region (point) (+ (point) bytes
)))
674 ;; Delete trailing junk.
675 (delete-region (point) (point-max))
676 ;; Now insert all the parts again where they fit in the structure.
677 (nnimap-insert-partial-structure structure parts
)
680 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
682 (let ((bstruc structure
))
683 (while (consp (car bstruc
))
685 (setq type
(car bstruc
))
686 (setq bstruc
(car (cdr bstruc
)))
687 (let ((has-boundary (member "boundary" bstruc
)))
689 (setq boundary
(cadr has-boundary
)))))
691 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
692 (downcase type
) boundary
)))
693 (while (not (stringp (car structure
)))
694 (insert "\n--" boundary
"\n")
695 (if (consp (caar structure
))
696 (nnimap-insert-partial-structure (pop structure
) parts t
)
697 (let ((bit (pop structure
)))
698 (insert (format "Content-type: %s/%s"
699 (downcase (nth 0 bit
))
700 (downcase (nth 1 bit
))))
701 (if (member-ignore-case "CHARSET" (nth 2 bit
))
704 (cadr (member-ignore-case "CHARSET" (nth 2 bit
)))))
706 (insert (format "Content-transfer-encoding: %s\n"
709 (when (assoc (nth 9 bit
) parts
)
710 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
711 (insert "\n--" boundary
"--\n")))
713 (defun nnimap-find-wanted-parts (structure)
714 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
716 (defun nnimap-find-wanted-parts-1 (structure prefix
)
719 (while (consp (car structure
))
720 (let ((sub (pop structure
)))
721 (if (consp (car sub
))
722 (push (nnimap-find-wanted-parts-1
723 sub
(if (string= prefix
"")
724 (number-to-string num
)
725 (format "%s.%s" prefix num
)))
727 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
728 (id (if (string= prefix
"")
729 (number-to-string num
)
730 (format "%s.%s" prefix num
))))
731 (setcar (nthcdr 9 sub
) id
)
732 (when (if (eq nnimap-fetch-partial-articles t
)
734 (string-match nnimap-fetch-partial-articles type
))
739 (defun nnimap-decode-gnus-group (group)
740 (decode-coding-string group
'utf-8
))
742 (deffoo nnimap-request-group
(group &optional server dont-check info
)
743 (setq group
(nnimap-decode-gnus-group group
))
744 (let ((result (nnimap-possibly-change-group
745 ;; Don't SELECT the group if we're going to select it
747 (if (and (not dont-check
)
748 (assoc group nnimap-current-infos
))
752 articles active marks high low
)
753 (with-current-buffer nntp-server-buffer
756 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
757 (insert (format "211 %d %d %d %S\n"
758 (- (cdr active
) (car active
))
762 (with-current-buffer (nnimap-buffer)
764 (let ((group-sequence
765 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
767 (nnimap-send-command "UID FETCH 1:* FLAGS")))
768 (setf (nnimap-group nnimap-object
) group
)
769 (nnimap-wait-for-response flag-sequence
)
771 (nnimap-flags-to-marks
773 (list (list group-sequence flag-sequence
774 1 group
"SELECT")))))
777 (nnimap-update-infos marks
(list info
))
778 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
779 (goto-char (point-max))
780 (let ((uidnext (nth 5 (car marks
))))
781 (setq high
(or (if uidnext
785 low
(or (nth 4 (car marks
)) uidnext
1)))))
789 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
792 (deffoo nnimap-request-create-group
(group &optional server args
)
793 (setq group
(nnimap-decode-gnus-group group
))
794 (when (nnimap-possibly-change-group nil server
)
795 (with-current-buffer (nnimap-buffer)
796 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
798 (deffoo nnimap-request-delete-group
(group &optional force server
)
799 (setq group
(nnimap-decode-gnus-group group
))
800 (when (nnimap-possibly-change-group nil server
)
801 (with-current-buffer (nnimap-buffer)
802 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
804 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
805 (setq group
(nnimap-decode-gnus-group group
))
806 (when (nnimap-possibly-change-group nil server
)
807 (with-current-buffer (nnimap-buffer)
808 (nnimap-unselect-group)
809 (car (nnimap-command "RENAME %S %S"
810 (utf7-encode group t
) (utf7-encode new-name t
))))))
812 (defun nnimap-unselect-group ()
813 ;; Make sure we don't have this group open read/write by asking
814 ;; to examine a mailbox that doesn't exist. This seems to be
815 ;; the only way that allows us to reliably go back to unselected
817 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
819 (deffoo nnimap-request-expunge-group
(group &optional server
)
820 (setq group
(nnimap-decode-gnus-group group
))
821 (when (nnimap-possibly-change-group group server
)
822 (with-current-buffer (nnimap-buffer)
823 (car (nnimap-command "EXPUNGE")))))
825 (defun nnimap-get-flags (spec)
828 (with-current-buffer (nnimap-buffer)
830 (nnimap-wait-for-response (nnimap-send-command
831 "UID FETCH %s FLAGS" spec
))
833 (subst-char-in-region (point-min) (point-max)
835 (goto-char (point-min))
836 (while (search-forward " FETCH " end t
)
837 (setq elems
(read (current-buffer)))
838 (push (cons (cadr (memq 'UID elems
))
839 (cadr (memq 'FLAGS elems
)))
841 (nreverse articles
)))
843 (deffoo nnimap-close-group
(group &optional server
)
846 (deffoo nnimap-request-move-article
(article group server accept-form
847 &optional last internal-move-group
)
848 (setq group
(nnimap-decode-gnus-group group
))
850 (mm-disable-multibyte)
851 (when (funcall (if internal-move-group
853 'nnimap-request-article
)
854 article group server
(current-buffer))
855 ;; If the move is internal (on the same server), just do it the easy
857 (let ((message-id (message-field-value "message-id")))
858 (if internal-move-group
860 (with-current-buffer (nnimap-buffer)
861 (nnimap-command "UID COPY %d %S"
863 (utf7-encode internal-move-group t
)))))
865 (nnimap-delete-article article
)
866 (cons internal-move-group
867 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
868 (nnimap-find-article-by-message-id
869 internal-move-group message-id
)))))
870 ;; Move the article to a different method.
871 (let ((result (eval accept-form
)))
873 (nnimap-possibly-change-group group server
)
874 (nnimap-delete-article article
)
877 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
878 (setq group
(nnimap-decode-gnus-group group
))
882 ((not (nnimap-possibly-change-group group server
))
885 (eq nnmail-expiry-target
'delete
))
886 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
887 (nnheader-message 7 "Article marked for deletion, but not expunged."))
890 (let ((deletable-articles
892 (eq nnmail-expiry-wait
'immediate
))
894 (gnus-sorted-intersection
896 (nnimap-find-expired-articles group
)))))
897 (if (null deletable-articles
)
899 (if (eq nnmail-expiry-target
'delete
)
900 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
901 (setq deletable-articles
902 (nnimap-process-expiry-targets
903 deletable-articles group server
)))
904 ;; Return the articles we didn't delete.
905 (gnus-sorted-complement articles deletable-articles
))))))
907 (defun nnimap-process-expiry-targets (articles group server
)
908 (let ((deleted-articles nil
))
910 ;; shortcut further processing if we're going to delete the articles
911 ((eq nnmail-expiry-target
'delete
)
912 (setq deleted-articles articles
)
914 ;; or just move them to another folder on the same IMAP server
915 ((and (not (functionp nnmail-expiry-target
))
916 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
917 (gnus-server-to-method
918 (format "nnimap:%s" server
))))
919 (and (nnimap-possibly-change-group group server
)
920 (with-current-buffer (nnimap-buffer)
921 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
924 (nnimap-article-ranges (gnus-compress-sequence articles
))
925 (utf7-encode (gnus-group-real-name nnmail-expiry-target
) t
))
926 (setq deleted-articles articles
)))
929 (dolist (article articles
)
930 (let ((target nnmail-expiry-target
))
932 (mm-disable-multibyte)
933 (when (nnimap-request-article article group server
(current-buffer))
934 (when (functionp target
)
935 (setq target
(funcall target group
)))
937 (not (eq target
'delete
)))
938 (if (or (gnus-request-group target t
)
939 (gnus-request-create-group target
))
941 (nnmail-expiry-target-group target group
)
942 (nnheader-message 7 "Expiring article %s:%d to %s"
943 group article target
))
945 (nnheader-message 7 "Expiring article %s:%d" group article
))
947 (push article deleted-articles
))))))))
948 ;; Change back to the current group again.
949 (nnimap-possibly-change-group group server
)
950 (setq deleted-articles
(nreverse deleted-articles
))
951 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
954 (defun nnimap-find-expired-articles (group)
955 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
956 (with-current-buffer (nnimap-buffer)
959 "UID SEARCH SENTBEFORE %s"
963 (car (rassoc (nth 4 (decode-time cutoff
))
964 parse-time-months
))))
967 (delete 0 (mapcar #'string-to-number
968 (cdr (assoc "SEARCH" (cdr result
))))))))))
971 (defun nnimap-find-article-by-message-id (group message-id
)
972 (with-current-buffer (nnimap-buffer)
974 (unless (equal group
(nnimap-group nnimap-object
))
975 (setf (nnimap-group nnimap-object
) nil
)
976 (setf (nnimap-examined nnimap-object
) group
)
977 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
979 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
981 (setq result
(nnimap-wait-for-response sequence
))
983 (car (setq result
(nnimap-parse-response))))
984 ;; Select the last instance of the message in the group.
986 (car (last (cdr (assoc "SEARCH" (cdr result
))))))
987 (string-to-number article
))))))
989 (defun nnimap-delete-article (articles)
990 (with-current-buffer (nnimap-buffer)
991 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
992 (nnimap-article-ranges articles
))
994 ((nnimap-capability "UIDPLUS")
995 (nnimap-command "UID EXPUNGE %s"
996 (nnimap-article-ranges articles
))
999 (nnimap-command "EXPUNGE")
1001 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
1002 "server doesn't support UIDPLUS, so we won't "
1003 "delete this article now"))))))
1005 (deffoo nnimap-request-scan
(&optional group server
)
1007 (setq group
(nnimap-decode-gnus-group group
)))
1008 (when (and (nnimap-possibly-change-group nil server
)
1010 nnimap-split-methods
)
1011 (nnheader-message 7 "nnimap %s splitting mail..." server
)
1012 (nnimap-split-incoming-mail)
1013 (nnheader-message 7 "nnimap %s splitting mail...done" server
)))
1015 (defun nnimap-marks-to-flags (marks)
1017 (dolist (mark marks
)
1018 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
1022 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
1023 (setq group
(nnimap-decode-gnus-group group
))
1024 (when (nnimap-possibly-change-group nil server
)
1025 (let ((command (assoc
1027 '((subscribe "SUBSCRIBE")
1028 (unsubscribe "UNSUBSCRIBE")))))
1030 (with-current-buffer (nnimap-buffer)
1031 (nnimap-command "%s %S" (cadr command
) (utf7-encode group t
)))))))
1033 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
1034 (setq group
(nnimap-decode-gnus-group group
))
1035 (when (nnimap-possibly-change-group group server
)
1037 (with-current-buffer (nnimap-buffer)
1039 ;; Just send all the STORE commands without waiting for
1040 ;; response. If they're successful, they're successful.
1041 (dolist (action actions
)
1042 (destructuring-bind (range action marks
) action
1043 (let ((flags (nnimap-marks-to-flags marks
)))
1045 (setq sequence
(nnimap-send-command
1046 "UID STORE %s %sFLAGS.SILENT (%s)"
1047 (nnimap-article-ranges range
)
1049 ((eq action
'del
) "-")
1050 ((eq action
'add
) "+")
1051 ((eq action
'set
) ""))
1052 (mapconcat #'identity flags
" ")))))))
1053 ;; Wait for the last command to complete to avoid later
1054 ;; synchronization problems with the stream.
1056 (nnimap-wait-for-response sequence
))))))
1058 (deffoo nnimap-request-accept-article
(group &optional server last
)
1059 (setq group
(nnimap-decode-gnus-group group
))
1060 (when (nnimap-possibly-change-group nil server
)
1061 (nnmail-check-syntax)
1062 (let ((message-id (message-field-value "message-id"))
1065 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
1066 (with-current-buffer (nnimap-buffer)
1067 (when (setq message
(or (nnimap-process-quirk "OK Gimap " 'append message
)
1069 ;; If we have this group open read-only, then unselect it
1070 ;; before appending to it.
1071 (when (equal (nnimap-examined nnimap-object
) group
)
1072 (nnimap-unselect-group))
1074 (setq sequence
(nnimap-send-command
1075 "APPEND %S {%d}" (utf7-encode group t
)
1077 (unless nnimap-streaming
1078 (nnimap-wait-for-connection "^[+]"))
1079 (process-send-string (get-buffer-process (current-buffer)) message
)
1080 (process-send-string (get-buffer-process (current-buffer))
1081 (if (nnimap-newlinep nnimap-object
)
1084 (let ((result (nnimap-get-response sequence
)))
1085 (if (not (nnimap-ok-p result
))
1087 (nnheader-report 'nnimap
"%s" result
)
1090 (or (nnimap-find-uid-response "APPENDUID" (car result
))
1091 (nnimap-find-article-by-message-id
1092 group message-id
))))))))))
1094 (defun nnimap-process-quirk (greeting-match type data
)
1095 (when (and (nnimap-greeting nnimap-object
)
1096 (string-match greeting-match
(nnimap-greeting nnimap-object
))
1098 (string-match "\000" data
))
1099 (let ((choice (gnus-multiple-choice
1100 "Message contains NUL characters. Delete, continue, abort? "
1101 '((?d
"Delete NUL characters")
1102 (?c
"Try to APPEND the message as is")
1106 (nnheader-report 'nnimap
"Aborted APPEND due to NUL characters"))
1112 (goto-char (point-min))
1113 (while (search-forward "\000" nil t
)
1114 (replace-match "" t t
))
1115 (buffer-string)))))))
1117 (defun nnimap-ok-p (value)
1120 (equal (caar value
) "OK")))
1122 (defun nnimap-find-uid-response (name list
)
1123 (let ((result (car (last (nnimap-find-response-element name list
)))))
1125 (string-to-number result
))))
1127 (defun nnimap-find-response-element (name list
)
1130 (when (and (consp elem
)
1131 (equal name
(car elem
)))
1132 (setq result elem
)))
1135 (deffoo nnimap-request-replace-article
(article group buffer
)
1136 (setq group
(nnimap-decode-gnus-group group
))
1138 (when (and (nnimap-possibly-change-group group nil
)
1139 ;; Put the article into the group.
1140 (with-current-buffer buffer
1142 (nnimap-request-accept-article group nil t
))))
1143 (nnimap-delete-article (list article
))
1144 ;; Return the new article number.
1147 (defun nnimap-add-cr ()
1148 (goto-char (point-min))
1149 (while (re-search-forward "\r?\n" nil t
)
1150 (replace-match "\r\n" t t
)))
1152 (defun nnimap-get-groups ()
1154 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1156 (nnimap-wait-for-response sequence
)
1157 (subst-char-in-region (point-min) (point-max)
1159 (goto-char (point-min))
1160 (nnimap-unfold-quoted-lines)
1161 (goto-char (point-min))
1162 (while (search-forward "* LIST " nil t
)
1163 (let ((flags (read (current-buffer)))
1164 (separator (read (current-buffer)))
1165 (group (read (current-buffer))))
1166 (unless (member '%NoSelect flags
)
1167 (push (utf7-decode (if (stringp group
)
1169 (format "%s" group
)) t
)
1173 (deffoo nnimap-request-list
(&optional server
)
1174 (when (nnimap-possibly-change-group nil server
)
1175 (with-current-buffer nntp-server-buffer
1178 (with-current-buffer (nnimap-buffer)
1179 (nnimap-get-groups)))
1180 sequences responses
)
1182 (with-current-buffer (nnimap-buffer)
1183 (setf (nnimap-group nnimap-object
) nil
)
1184 (dolist (group groups
)
1185 (setf (nnimap-examined nnimap-object
) group
)
1186 (push (list (nnimap-send-command "EXAMINE %S"
1187 (utf7-encode group t
))
1190 (nnimap-wait-for-response (caar sequences
))
1192 (nnimap-get-responses (mapcar #'car sequences
))))
1193 (dolist (response responses
)
1194 (let* ((sequence (car response
))
1195 (response (cadr response
))
1196 (group (cadr (assoc sequence sequences
)))
1197 (egroup (encode-coding-string group
'utf-8
)))
1199 (equal (caar response
) "OK"))
1200 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1202 (dolist (elem response
)
1203 (when (equal (cadr elem
) "EXISTS")
1204 (setq exists
(string-to-number (car elem
)))))
1206 (setq highest
(1- (string-to-number (car uidnext
)))))
1209 (insert (format "%S 0 1 y\n" egroup
)))
1212 (insert (format "%S %d %d y\n" egroup
1213 highest
(1+ highest
))))
1215 ;; Return the widest possible range.
1216 (insert (format "%S %d 1 y\n" egroup
1217 (or highest exists
)))))))))
1220 (deffoo nnimap-request-newgroups
(date &optional server
)
1221 (when (nnimap-possibly-change-group nil server
)
1222 (with-current-buffer nntp-server-buffer
1224 (dolist (group (with-current-buffer (nnimap-buffer)
1225 (nnimap-get-groups)))
1226 (unless (assoc group nnimap-current-infos
)
1227 ;; Insert dummy numbers here -- they don't matter.
1228 (insert (format "%S 0 1 y\n" (encode-coding-string group
'utf-8
)))))
1231 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1232 (when (and (nnimap-possibly-change-group nil server
)
1234 (with-current-buffer (nnimap-buffer)
1236 (setf (nnimap-group nnimap-object
) nil
)
1237 (setf (nnimap-initial-resync nnimap-object
) 0)
1238 (let ((qresyncp (nnimap-capability "QRESYNC"))
1239 params groups sequences active uidvalidity modseq group
)
1240 ;; Go through the infos and gather the data needed to know
1241 ;; what and how to request the data.
1242 (dolist (info infos
)
1243 (setq params
(gnus-info-params info
)
1244 group
(nnimap-decode-gnus-group
1245 (gnus-group-real-name (gnus-info-group info
)))
1246 active
(cdr (assq 'active params
))
1247 uidvalidity
(cdr (assq 'uidvalidity params
))
1248 modseq
(cdr (assq 'modseq params
)))
1249 (setf (nnimap-examined nnimap-object
) group
)
1255 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1256 (utf7-encode group t
)
1257 (nnimap-quirk "QRESYNC")
1265 ;; If we don't have a UIDVALIDITY, then this is
1266 ;; the first time we've seen the group, so we
1267 ;; have to do a SELECT (which is slower than an
1268 ;; examine), but will tell us whether the group
1269 ;; is read-only or not.
1272 (if (and active uidvalidity
)
1273 ;; Fetch the last 100 flags.
1274 (setq start
(max 1 (- (cdr active
) 100)))
1275 (setf (nnimap-initial-resync nnimap-object
)
1276 (1+ (nnimap-initial-resync nnimap-object
)))
1278 (push (list (nnimap-send-command "%s %S" command
1279 (utf7-encode group t
))
1280 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1281 start group command
)
1285 (defun nnimap-quirk (command)
1286 (let ((quirk (assoc command nnimap-quirks
)))
1287 ;; If this server is of a type that matches a quirk, then return
1288 ;; the "quirked" command instead of the proper one.
1289 (if (or (null quirk
)
1290 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1294 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1295 (when (and sequences
1296 (nnimap-possibly-change-group nil server t
)
1297 ;; Check that the process is still alive.
1298 (get-buffer-process (nnimap-buffer))
1299 (memq (process-status (get-buffer-process (nnimap-buffer)))
1301 (with-current-buffer (nnimap-buffer)
1302 ;; Wait for the final data to trickle in.
1303 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1307 ;; Now we should have most of the data we need, no matter
1308 ;; whether we're QRESYNCING, fetching all the flags from
1309 ;; scratch, or just fetching the last 100 flags per group.
1310 (nnimap-update-infos (nnimap-flags-to-marks
1312 (nreverse sequences
)))
1314 ;; Finally, just return something resembling an active file in
1315 ;; the nntp buffer, so that the agent can save the info, too.
1316 (with-current-buffer nntp-server-buffer
1318 (dolist (info infos
)
1319 (let* ((group (gnus-info-group info
))
1320 (active (gnus-active group
)))
1322 (insert (format "%S %d %d y\n"
1323 (decode-coding-string
1324 (gnus-group-real-name group
) 'utf-8
)
1326 (car active
)))))))))))
1328 (defun nnimap-update-infos (flags infos
)
1329 (dolist (info infos
)
1330 (let* ((group (nnimap-decode-gnus-group
1331 (gnus-group-real-name (gnus-info-group info
))))
1332 (marks (cdr (assoc group flags
))))
1334 (nnimap-update-info info marks
)))))
1336 (defun nnimap-update-info (info marks
)
1337 (destructuring-bind (existing flags high low uidnext start-article
1338 permanent-flags uidvalidity
1339 vanished highestmodseq
) marks
1341 ;; Ignore groups with no UIDNEXT/marks. This happens for
1342 ;; completely empty groups.
1343 ((and (not existing
)
1345 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1347 (gnus-set-active (gnus-info-group info
) active
))))
1348 ;; We have a mismatch between the old and new UIDVALIDITY
1349 ;; identifiers, so we have to re-request the group info (the next
1350 ;; time). This virtually never happens.
1351 ((let ((old-uidvalidity
1352 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1353 (and old-uidvalidity
1354 (not (equal old-uidvalidity uidvalidity
))
1355 (or (not start-article
)
1356 (> start-article
1))))
1357 (gnus-group-remove-parameter info
'uidvalidity
)
1358 (gnus-group-remove-parameter info
'modseq
))
1359 ;; We have the data needed to update.
1361 (let* ((group (gnus-info-group info
))
1362 (completep (and start-article
1363 (= start-article
1)))
1364 (active (or (gnus-active group
)
1365 (cdr (assq 'active
(gnus-info-params info
))))))
1367 (setq high
(1- uidnext
)))
1368 ;; First set the active ranges based on high/low.
1370 (not (gnus-active group
)))
1371 (gnus-set-active group
1374 (cons (min (or low
(car active
))
1376 (max (or high
(cdr active
))
1381 ;; No articles in this group.
1382 (cons uidnext
(1- uidnext
)))
1384 (cons start-article
(1- start-article
)))
1386 ;; No articles and no uidnext.
1388 (gnus-set-active group
1390 (or high
(1- uidnext
)))))
1391 ;; See whether this is a read-only group.
1392 (unless (eq permanent-flags
'not-scanned
)
1393 (gnus-group-set-parameter
1394 info
'permanent-flags
1395 (and (or (memq '%
* permanent-flags
)
1396 (memq '%Seen permanent-flags
))
1398 ;; Update marks and read articles if this isn't a
1399 ;; read-only IMAP group.
1400 (when (setq permanent-flags
1401 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1402 (if (and highestmodseq
1403 (not start-article
))
1404 ;; We've gotten the data by QRESYNCing.
1405 (nnimap-update-qresync-info
1406 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1407 ;; Do normal non-QRESYNC flag updates.
1408 ;; Update the list of read articles.
1410 (gnus-compress-sequence
1411 (gnus-set-difference
1412 (gnus-set-difference
1414 (cdr (assoc '%Seen flags
)))
1415 (cdr (assoc '%Flagged flags
)))))
1416 (read (gnus-range-difference
1417 (cons start-article high
) unread
)))
1418 (when (> start-article
1)
1421 (if (> start-article
1)
1422 (gnus-sorted-range-intersection
1423 (cons 1 (1- start-article
))
1424 (gnus-info-read info
))
1425 (gnus-info-read info
))
1427 (when (or (not (listp permanent-flags
))
1428 (memq '%Seen permanent-flags
))
1429 (gnus-info-set-read info read
))
1430 ;; Update the marks.
1431 (setq marks
(gnus-info-marks info
))
1432 (dolist (type (cdr nnimap-mark-alist
))
1433 (when (or (not (listp permanent-flags
))
1434 (memq (car (assoc (caddr type
) flags
))
1436 (memq '%
* permanent-flags
))
1437 (let ((old-marks (assoc (car type
) marks
))
1439 (gnus-compress-sequence
1440 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1441 (assoc (intern (cadr type
) obarray
) flags
)
1442 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1443 (setq marks
(delq old-marks marks
))
1445 (when (and old-marks
1446 (> start-article
1))
1447 (setq old-marks
(gnus-range-difference
1449 (cons start-article high
)))
1450 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1452 (push (cons (car type
) new-marks
) marks
)))))
1453 (gnus-info-set-marks info marks t
))))
1454 ;; Tell Gnus whether there are any \Recent messages in any of
1456 (let ((recent (cdr (assoc '%Recent flags
))))
1459 (> (car (last recent
)) (cdr active
)))
1460 (push (list (cons (gnus-group-real-name group
) 0))
1461 nnmail-split-history
)))
1462 ;; Note the active level for the next run-through.
1463 (gnus-group-set-parameter info
'active
(gnus-active group
))
1464 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1465 (gnus-group-set-parameter info
'modseq highestmodseq
)
1466 (nnimap-store-info info
(gnus-active group
)))))))
1468 (defun nnimap-update-qresync-info (info existing vanished flags
)
1469 ;; Add all the vanished articles to the list of read articles.
1474 (gnus-range-add (gnus-info-read info
)
1476 (cdr (assq '%Flagged flags
)))
1477 (cdr (assq '%Seen flags
))))
1478 (let ((marks (gnus-info-marks info
)))
1479 (dolist (type (cdr nnimap-mark-alist
))
1480 (let ((ticks (assoc (car type
) marks
))
1482 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1483 (assoc (intern (cadr type
) obarray
) flags
)
1484 (assoc (cadr type
) flags
))))) ; "\Flagged"
1485 (setq marks
(delq ticks marks
))
1487 ;; Add the new marks we got.
1488 (setq ticks
(gnus-add-to-range ticks new-marks
))
1489 ;; Remove the marks from messages that don't have them.
1490 (setq ticks
(gnus-remove-from-range
1492 (gnus-compress-sequence
1493 (gnus-sorted-complement existing new-marks
))))
1495 (push (cons (car type
) ticks
) marks
)))
1496 (gnus-info-set-marks info marks t
))))
1498 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1499 (if (zerop (length irange
))
1502 (dolist (elem (split-string irange
","))
1504 (if (string-match ":" elem
)
1505 (let ((numbers (split-string elem
":")))
1506 (cons (string-to-number (car numbers
))
1507 (string-to-number (cadr numbers
))))
1508 (string-to-number elem
))
1510 (nreverse result
))))
1512 (defun nnimap-store-info (info active
)
1513 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1514 (entry (assoc group nnimap-current-infos
)))
1516 (setcdr entry
(list info active
))
1517 (push (list group info active
) nnimap-current-infos
))))
1519 (defun nnimap-flags-to-marks (groups)
1520 (let (data group totalp uidnext articles start-article mark permanent-flags
1521 uidvalidity vanished highestmodseq
)
1522 (dolist (elem groups
)
1523 (setq group
(car elem
)
1524 uidnext
(nth 1 elem
)
1525 start-article
(nth 2 elem
)
1526 permanent-flags
(nth 3 elem
)
1527 uidvalidity
(nth 4 elem
)
1528 vanished
(nth 5 elem
)
1529 highestmodseq
(nth 6 elem
)
1530 articles
(nthcdr 7 elem
))
1531 (let ((high (caar articles
))
1533 (dolist (article articles
)
1534 (setq low
(car article
))
1535 (push (car article
) existing
)
1536 (dolist (flag (cdr article
))
1537 (setq mark
(assoc flag marks
))
1539 (push (list flag
(car article
)) marks
)
1540 (setcdr mark
(cons (car article
) (cdr mark
))))))
1541 (push (list group existing marks high low uidnext start-article
1542 permanent-flags uidvalidity vanished highestmodseq
)
1546 (defun nnimap-parse-flags (sequences)
1547 (goto-char (point-min))
1548 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1550 (subst-char-in-region (point-min) (point-max)
1552 ;; Remove any MODSEQ entries in the buffer, because they may contain
1553 ;; numbers that are too large for 32-bit Emacsen.
1554 (while (re-search-forward " MODSEQ ([0-9]+)" nil t
)
1555 (replace-match "" t t
))
1556 (goto-char (point-min))
1557 (let (start end articles groups uidnext elems permanent-flags
1558 uidvalidity vanished highestmodseq
)
1559 (dolist (elem sequences
)
1560 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1562 (setq start
(point))
1564 ;; The EXAMINE was successful.
1565 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1570 (setq permanent-flags
1571 (if (equal command
"SELECT")
1572 (and (search-forward "PERMANENTFLAGS "
1573 (or end
(point-min)) t
)
1574 (read (current-buffer)))
1578 (and (search-forward "UIDNEXT "
1579 (or end
(point-min)) t
)
1580 (read (current-buffer))))
1583 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1584 (or end
(point-min)) t
)
1585 ;; Store UIDVALIDITY as a string, as it's
1586 ;; too big for 32-bit Emacsen, usually.
1590 (and (eq flag-sequence
'qresync
)
1591 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1592 (or end
(point-min)) t
)
1596 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1597 (or end
(point-min)) t
)
1601 ;; The UID FETCH FLAGS was successful.
1602 (or (eq flag-sequence
'qresync
)
1603 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1604 (if (eq flag-sequence
'qresync
)
1608 (setq start
(point))
1610 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1612 (setq elems
(read (current-buffer)))
1613 (push (cons (cadr (memq 'UID elems
))
1614 (cadr (memq 'FLAGS elems
)))
1616 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1617 vanished highestmodseq
)
1620 (if (eq flag-sequence
'qresync
)
1623 (setq articles nil
))))
1626 (defun nnimap-find-process-buffer (buffer)
1627 (cadr (assoc buffer nnimap-connection-alist
)))
1629 (deffoo nnimap-request-post
(&optional server
)
1630 (setq nnimap-status-string
"Read-only server")
1633 (declare-function gnus-fetch-headers
"gnus-sum"
1634 (articles &optional limit force-new dependencies
))
1636 (autoload 'nnir-search-thread
"nnir")
1638 (deffoo nnimap-request-thread
(header &optional group server
)
1640 (setq group
(nnimap-decode-gnus-group group
)))
1641 (if gnus-refer-thread-use-nnir
1642 (nnir-search-thread header
)
1643 (when (nnimap-possibly-change-group group server
)
1644 (let* ((cmd (nnimap-make-thread-query header
))
1645 (result (with-current-buffer (nnimap-buffer)
1646 (nnimap-command "UID SEARCH %s" cmd
))))
1650 (delete 0 (mapcar #'string-to-number
1651 (cdr (assoc "SEARCH" (cdr result
))))))
1654 (defun nnimap-possibly-change-group (group server
&optional no-reconnect
)
1655 (let ((open-result t
))
1657 (not (nnimap-server-opened server
)))
1658 (setq open-result
(nnimap-open-server server nil no-reconnect
)))
1665 (with-current-buffer (nnimap-buffer)
1666 (if (equal group
(nnimap-group nnimap-object
))
1668 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1670 (setf (nnimap-group nnimap-object
) group
1671 (nnimap-select-result nnimap-object
) result
)
1674 (defun nnimap-find-connection (buffer)
1675 "Find the connection delivering to BUFFER."
1676 (let ((entry (assoc buffer nnimap-connection-alist
)))
1678 (if (and (buffer-name (cadr entry
))
1679 (get-buffer-process (cadr entry
))
1680 (memq (process-status (get-buffer-process (cadr entry
)))
1682 (get-buffer-process (cadr entry
))
1683 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1686 (defvar nnimap-sequence
0)
1688 (defun nnimap-send-command (&rest args
)
1689 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1690 (process-send-string
1691 (get-buffer-process (current-buffer))
1694 (incf nnimap-sequence
)
1695 (apply #'format args
)
1696 (if (nnimap-newlinep nnimap-object
)
1699 ;; Some servers apparently can't have many outstanding
1700 ;; commands, so throttle them.
1701 (unless nnimap-streaming
1702 (nnimap-wait-for-response nnimap-sequence
))
1705 (defvar nnimap-record-commands nil
1706 "If non-nil, log commands to the \"*imap log*\" buffer.")
1708 (defun nnimap-log-command (command)
1709 (when nnimap-record-commands
1710 (with-current-buffer (get-buffer-create "*imap log*")
1711 (goto-char (point-max))
1712 (insert (format-time-string "%H:%M:%S")
1713 " [" nnimap-address
"] "
1714 (if nnimap-inhibit-logging
1719 (defun nnimap-command (&rest args
)
1721 (let* ((sequence (apply #'nnimap-send-command args
))
1722 (response (nnimap-get-response sequence
)))
1723 (if (equal (caar response
) "OK")
1725 (nnheader-report 'nnimap
"%s"
1726 (mapconcat (lambda (a)
1728 (car response
) " "))
1731 (defun nnimap-get-response (sequence)
1732 (nnimap-wait-for-response sequence
)
1733 (nnimap-parse-response))
1735 (defun nnimap-wait-for-connection (&optional regexp
)
1736 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1738 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1739 (let ((process (get-buffer-process (current-buffer))))
1740 (goto-char (point-min))
1741 (while (and (memq (process-status process
)
1743 (not (re-search-forward regexp nil t
)))
1744 (nnheader-accept-process-output process
)
1745 (goto-char (point-min)))
1747 (and (looking-at (or response-regexp regexp
))
1750 (defun nnimap-wait-for-response (sequence &optional messagep
)
1751 (let ((process (get-buffer-process (current-buffer)))
1755 (goto-char (point-max))
1756 (while (and (setq openp
(memq (process-status process
)
1759 ;; Skip past any "*" lines that the server has
1761 (while (and (not (bobp))
1764 (looking-at "\\*"))))
1765 (not (looking-at (format "%d .*\n" sequence
)))))
1767 (nnheader-message-maybe
1768 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
1770 (if (not (zerop (nnimap-initial-resync nnimap-object
)))
1771 (format " (initial sync of %d group%s; please wait)"
1772 (nnimap-initial-resync nnimap-object
)
1773 (if (= (nnimap-initial-resync nnimap-object
) 1)
1777 (nnheader-accept-process-output process
)
1778 (goto-char (point-max)))
1779 (setf (nnimap-initial-resync nnimap-object
) 0)
1784 ;; The user hit C-g while we were waiting: kill the process, in case
1785 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1787 (delete-process process
)
1790 (defun nnimap-parse-response ()
1791 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1793 (dolist (line lines
)
1794 (push (cdr (nnimap-parse-line line
)) result
))
1795 ;; Return the OK/error code first, and then all the "continuation
1796 ;; lines" afterwards.
1798 (nreverse result
))))
1800 ;; Parse an IMAP response line lightly. They look like
1801 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1802 ;; the lines into a list of strings and lists of string.
1803 (defun nnimap-parse-line (line)
1806 (mm-disable-multibyte)
1808 (goto-char (point-min))
1810 (if (eql (setq char
(following-char)) ?
)
1818 (if (search-forward "]" (line-end-position) 'move
)
1825 (if (search-forward ")" (line-end-position) 'move
)
1832 (1- (or (search-forward "\"" (line-end-position) 'move
)
1835 (buffer-substring (point) (if (search-forward " " nil t
)
1837 (goto-char (point-max))))))
1839 (nreverse result
))))
1841 (defun nnimap-last-response-string ()
1844 (let ((end (point)))
1848 (while (and (not (bobp))
1849 (eql (following-char) ?
*))
1851 (unless (eql (following-char) ?
*)
1853 (buffer-substring (point) end
))))
1855 (defun nnimap-get-responses (sequences)
1857 (dolist (sequence sequences
)
1858 (goto-char (point-min))
1859 (when (re-search-forward (format "^%d " sequence
) nil t
)
1860 (push (list sequence
(nnimap-parse-response))
1864 (defvar nnimap-incoming-split-list nil
)
1866 (defun nnimap-fetch-inbox (articles)
1868 (nnimap-wait-for-response
1869 (nnimap-send-command
1871 (nnimap-article-ranges articles
)
1872 (format "(UID %s%s)"
1878 (nnimap-split-download-body-default
1886 (defun nnimap-split-incoming-mail ()
1887 (with-current-buffer (nnimap-buffer)
1888 (let ((nnimap-incoming-split-list nil
)
1889 (nnmail-split-methods
1891 ((eq nnimap-split-methods
'default
)
1892 nnmail-split-methods
)
1893 (nnimap-split-methods
1894 nnimap-split-methods
)
1896 'nnmail-split-fancy
)))
1897 (nnmail-split-fancy (or nnimap-split-fancy
1898 nnmail-split-fancy
))
1899 (nnmail-inhibit-default-split-group t
)
1900 (groups (nnimap-get-groups))
1903 (nnimap-command "SELECT %S" nnimap-inbox
)
1904 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1905 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1907 (nnimap-fetch-inbox new-articles
)
1908 (nnimap-transform-split-mail)
1909 (nnheader-ms-strip-cr)
1911 (nnmail-split-incoming (current-buffer)
1912 #'nnimap-save-mail-spec
1914 #'nnimap-dummy-active-number
1915 #'nnimap-save-mail-spec
)
1916 (when nnimap-incoming-split-list
1917 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1918 sequences junk-articles
)
1919 ;; Create any groups that doesn't already exist on the
1921 (dolist (spec specs
)
1922 (when (and (not (member (car spec
) groups
))
1923 (not (eq (car spec
) 'junk
)))
1924 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1925 ;; Then copy over all the messages.
1927 (dolist (spec specs
)
1928 (let ((group (car spec
))
1929 (ranges (cdr spec
)))
1930 (if (eq group
'junk
)
1931 (setq junk-articles ranges
)
1932 (push (list (nnimap-send-command
1934 (nnimap-article-ranges ranges
)
1935 (utf7-encode group t
))
1938 ;; Wait for the last COPY response...
1940 (nnimap-wait-for-response (caar sequences
))
1941 ;; And then mark the successful copy actions as deleted,
1942 ;; and possibly expunge them.
1943 (nnimap-mark-and-expunge-incoming
1944 (nnimap-parse-copied-articles sequences
)))
1945 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1947 (defun nnimap-mark-and-expunge-incoming (range)
1949 (setq range
(nnimap-article-ranges range
))
1952 (nnimap-send-command
1953 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1955 ;; If the server supports it, we now delete the message we have
1956 ;; just copied over.
1957 ((nnimap-capability "UIDPLUS")
1958 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1959 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1960 ;; user has configured it.
1962 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1963 (nnimap-wait-for-response sequence
))))
1965 (defun nnimap-parse-copied-articles (sequences)
1966 (let (sequence copied range
)
1967 (goto-char (point-min))
1968 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t
)
1969 (setq sequence
(string-to-number (match-string 1)))
1970 (when (setq range
(cadr (assq sequence sequences
)))
1971 (push (gnus-uncompress-range range
) copied
)))
1972 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1974 (defun nnimap-new-articles (flags)
1976 (dolist (elem flags
)
1977 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1979 (push (car elem
) new
)))
1980 (gnus-compress-sequence (nreverse new
))))
1982 (defun nnimap-make-split-specs (list)
1986 (destructuring-bind (article spec
) elem
1987 (dolist (group (delete nil
(mapcar #'car spec
)))
1988 (unless (setq entry
(assoc group specs
))
1989 (push (setq entry
(list group
)) specs
))
1990 (setcdr entry
(cons article
(cdr entry
))))))
1991 (dolist (entry specs
)
1992 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
1995 (defun nnimap-transform-split-mail ()
1996 (goto-char (point-min))
1997 (let (article bytes
)
2000 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2001 (delete-region (point) (progn (forward-line 1) (point)))
2004 (setq article
(match-string 1)
2005 bytes
(nnimap-get-length))
2006 (delete-region (line-beginning-position) (line-end-position))
2007 ;; Insert MMDF separator, and a way to remember what this
2009 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
2010 (forward-char (1+ bytes
))
2011 (setq bytes
(nnimap-get-length))
2012 (delete-region (line-beginning-position) (line-end-position))
2013 ;; There's a body; skip past that.
2015 (forward-char (1+ bytes
))
2016 (delete-region (line-beginning-position) (line-end-position)))))))
2018 (defun nnimap-dummy-active-number (group &optional server
)
2021 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
2023 (goto-char (point-min))
2024 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
2025 (error "Invalid nnimap mail")
2026 (setq article
(string-to-number (match-string 1))))
2028 (if (eq group-art
'junk
)
2029 (list (cons 'junk
1))
2031 nnimap-incoming-split-list
)))
2033 (defun nnimap-make-thread-query (header)
2034 (let* ((id (mail-header-id header
))
2036 (or (mail-header-references header
)
2040 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2042 (dolist (refid refs value
)
2044 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2045 refid refid value
)))))
2050 ;;; nnimap.el ends here