1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2018 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 <https://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
42 (autoload 'auth-source-forget
+ "auth-source")
43 (autoload 'auth-source-search
"auth-source")
47 (defvoo nnimap-address nil
48 "The address of the IMAP server.")
50 (defvoo nnimap-user nil
51 "Username to use for authentication to the IMAP server.")
53 (defvoo nnimap-server-port nil
55 If nnimap-stream is `ssl', this will default to `imaps'. If not,
56 it will default to `imap'.")
58 (defvoo nnimap-use-namespaces nil
59 "Whether to use IMAP namespaces.
60 If in Gnus your folder names in all start with (e.g.) `INBOX',
61 you probably want to set this to t. The effects of this are
62 purely cosmetic, but changing this variable will affect the
63 names of your nnimap groups. ")
65 (defvoo nnimap-stream
'undecided
66 "How nnimap talks to the IMAP server.
67 The value should be either `undecided', `ssl' or `tls',
68 `network', `starttls', `plain', or `shell'.
70 If the value is `undecided', nnimap tries `ssl' first, then falls
73 (defvoo nnimap-shell-program
(if (boundp 'imap-shell-program
)
74 (if (listp imap-shell-program
)
75 (car imap-shell-program
)
78 "What command to execute to connect to an IMAP server.
79 This will only be used if the connection type is `shell'. See
80 the `open-network-stream' documentation for an explanation of
83 (defvoo nnimap-inbox nil
84 "The mail box where incoming mail arrives and should be split out of.
85 This can be a string or a list of strings
86 For example, \"INBOX\" or (\"INBOX\" \"SENT\").")
88 (defvoo nnimap-split-methods nil
90 Uses the same syntax as `nnmail-split-methods'.")
92 (defvoo nnimap-split-fancy nil
93 "Uses the same syntax as `nnmail-split-fancy'.")
95 (defvoo nnimap-unsplittable-articles
'(%Deleted %Seen
)
96 "Articles with the flags in the list will not be considered when splitting.")
98 (make-obsolete-variable 'nnimap-split-rule
"see `nnimap-split-methods'."
101 (defvoo nnimap-authenticator nil
102 "How nnimap authenticate itself to the server.
103 Possible choices are nil (use default methods), `anonymous',
104 `login', `plain' and `cram-md5'.")
106 (defvoo nnimap-expunge t
107 "If non-nil, expunge articles after deleting them.
108 This is always done if the server supports UID EXPUNGE, but it's
109 not done by default on servers that doesn't support that command.")
111 (defvoo nnimap-streaming t
112 "If non-nil, try to use streaming commands with IMAP servers.
113 Switching this off will make nnimap slower, but it helps with
116 (defvoo nnimap-connection-alist nil
)
118 (defvoo nnimap-current-infos nil
)
120 (defvoo nnimap-namespace nil
)
122 (defun nnimap-decode-gnus-group (group)
123 (decode-coding-string group
'utf-8
))
125 (defun nnimap-encode-gnus-group (group)
126 (encode-coding-string group
'utf-8
))
128 (defvoo nnimap-fetch-partial-articles nil
129 "If non-nil, Gnus will fetch partial articles.
130 If t, Gnus will fetch only the first part. If a string, it
131 will fetch all parts that have types that match that string. A
132 likely value would be \"text/\" to automatically fetch all
139 (defcustom nnimap-request-articles-find-limit nil
140 "Limit the number of articles to look for after moving an article."
141 :type
'(choice (const nil
) integer
)
145 (defvar nnimap-process nil
)
147 (defvar nnimap-status-string
"")
149 (defvar nnimap-split-download-body-default nil
150 "Internal variable with default value for `nnimap-split-download-body'.")
152 (defvar nnimap-keepalive-timer nil
)
153 (defvar nnimap-process-buffers nil
)
156 group process commands capabilities select-result newlinep server
157 last-command-time greeting examined stream-type initial-resync
)
159 (defvar nnimap-object nil
)
161 (defvar nnimap-mark-alist
162 '((read "\\Seen" %Seen
)
163 (tick "\\Flagged" %Flagged
)
164 (reply "\\Answered" %Answered
)
165 (expire "gnus-expire")
166 (dormant "gnus-dormant")
169 (download "gnus-download")
170 (forward "gnus-forward")))
172 (defvar nnimap-quirks
173 '(("QRESYNC" "Zimbra" "QRESYNC ")
174 ("MOVE" "Dovecot" nil
)))
176 (defvar nnimap-inhibit-logging nil
)
178 (defun nnimap-group-to-imap (group)
179 "Convert Gnus group name to IMAP mailbox name."
180 (let* ((inbox (if nnimap-namespace
181 (substring nnimap-namespace
0 -
1) nil
)))
183 (cond ((or (not inbox
)
184 (string-equal group inbox
))
186 ((string-prefix-p "#" group
)
189 (concat nnimap-namespace group
))) t
)))
191 (defun nnimap-buffer ()
192 (nnimap-find-process-buffer nntp-server-buffer
))
194 (defun nnimap-header-parameters ()
197 (push "RFC822.SIZE" params
)
198 (when (nnimap-capability "X-GM-EXT-1")
199 (push "X-GM-LABELS" params
))
200 (push "BODYSTRUCTURE" params
)
203 "BODY.PEEK[HEADER.FIELDS %s]"
204 "RFC822.HEADER.LINES %s")
205 (append '(Subject From Date Message-Id
206 References In-Reply-To Xref
)
207 nnmail-extra-headers
))
209 (format "%s" (nreverse params
))))
211 (deffoo nnimap-retrieve-headers
(articles &optional group server _fetch-old
)
213 (setq group
(nnimap-decode-gnus-group group
)))
214 (with-current-buffer nntp-server-buffer
216 (when (nnimap-change-group group server
)
217 (with-current-buffer (nnimap-buffer)
219 (nnimap-wait-for-response
222 (nnimap-article-ranges (gnus-compress-sequence articles
))
223 (nnimap-header-parameters))
225 (unless (process-live-p (get-buffer-process (current-buffer)))
226 (error "Server closed connection"))
227 (nnimap-transform-headers)
228 (nnheader-remove-cr-followed-by-lf))
229 (insert-buffer-substring
230 (nnimap-find-process-buffer (current-buffer))))
233 (defun nnimap-transform-headers ()
234 (goto-char (point-min))
235 (let (article lines size string labels
)
238 (while (not (looking-at "\\* [0-9]+ FETCH"))
239 (delete-region (point) (progn (forward-line 1) (point)))
242 (goto-char (match-end 0))
243 ;; Unfold quoted {number} strings.
244 (while (or (looking-at "[ (]{\\([0-9]+\\)}\r?\n")
246 "[^]][ (]{\\([0-9]+\\)}\r?\n"
248 ;; Start of the header section.
249 (or (re-search-forward "] {[0-9]+}\r?\n" nil t
)
250 ;; Start of the next FETCH.
251 (re-search-forward "\\* [0-9]+ FETCH" nil t
)
254 (setq size
(string-to-number (match-string 1)))
255 (delete-region (+ (match-beginning 0) 2) (point))
256 (setq string
(buffer-substring (point) (+ (point) size
)))
257 (delete-region (point) (+ (point) size
))
258 (insert (format "%S" (subst-char-in-string ?
\n ?\s string
))))
261 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
267 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
272 (when (search-forward "X-GM-LABELS" (line-end-position) t
)
273 (setq labels
(ignore-errors (read (current-buffer)))))
275 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
276 (let ((structure (ignore-errors
277 (read (current-buffer)))))
278 (while (and (consp structure
)
279 (not (atom (car structure
))))
280 (setq structure
(car structure
)))
282 (stringp (car structure
))
283 (equal (upcase (nth 0 structure
)) "MESSAGE")
284 (equal (upcase (nth 1 structure
)) "RFC822"))
286 (nth 7 structure
)))))
287 (delete-region (line-beginning-position) (line-end-position))
288 (insert (format "211 %s Article retrieved." article
))
291 (insert (format "Chars: %s\n" size
)))
293 (insert (format "Lines: %s\n" lines
)))
295 (insert (format "X-GM-LABELS: %s\n" labels
)))
296 ;; Most servers have a blank line after the headers, but
298 (unless (re-search-forward "^\r$\\|^)\r?$" nil t
)
299 (goto-char (point-max)))
300 (delete-region (line-beginning-position) (line-end-position))
304 (defun nnimap-unfold-quoted-lines ()
305 ;; Unfold quoted {number} strings.
307 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
308 (setq size
(string-to-number (match-string 1)))
309 (delete-region (1+ (match-beginning 0)) (point))
310 (setq string
(buffer-substring (point) (+ (point) size
)))
311 (delete-region (point) (+ (point) size
))
312 (insert (format "%S" string
)))))
314 (defun nnimap-get-length ()
315 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
316 (string-to-number (match-string 1))))
318 (defun nnimap-article-ranges (ranges)
322 (number-to-string ranges
))
323 ((numberp (cdr ranges
))
324 (format "%d:%d" (car ranges
) (cdr ranges
)))
326 (dolist (elem ranges
)
329 (format "%d:%d" (car elem
) (cdr elem
))
330 (number-to-string elem
))
332 (mapconcat #'identity
(nreverse result
) ",")))))
334 (deffoo nnimap-open-server
(server &optional defs no-reconnect
)
335 (if (nnimap-server-opened server
)
337 (unless (assq 'nnimap-address defs
)
338 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
339 (nnoo-change-server 'nnimap server defs
)
341 (nnimap-find-connection nntp-server-buffer
)
342 (or (nnimap-find-connection nntp-server-buffer
)
343 (nnimap-open-connection nntp-server-buffer
)))))
345 (defun nnimap-make-process-buffer (buffer)
347 (generate-new-buffer (format " *nnimap %s %s %s*"
348 nnimap-address nnimap-server-port
349 (gnus-buffer-exists-p buffer
)))
350 (mm-disable-multibyte)
351 (buffer-disable-undo)
353 (set (make-local-variable 'after-change-functions
) nil
)
354 (set (make-local-variable 'nnimap-object
)
355 (make-nnimap :server
(nnoo-current-server 'nnimap
)
357 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
358 (push (current-buffer) nnimap-process-buffers
)
361 (defvar auth-source-creation-prompts
)
363 (defun nnimap-credentials (address ports user
)
364 (let* ((auth-source-creation-prompts
365 '((user .
"IMAP user at %h: ")
366 (secret .
"IMAP password for %u@%h: ")))
367 (found (nth 0 (auth-source-search :max
1
371 :require
'(:user
:secret
)
374 (list (plist-get found
:user
)
375 (let ((secret (plist-get found
:secret
)))
376 (if (functionp secret
)
379 (plist-get found
:save-function
))
382 (defun nnimap-keepalive ()
383 (let ((now (current-time)))
384 (dolist (buffer nnimap-process-buffers
)
385 (when (buffer-name buffer
)
386 (with-current-buffer buffer
387 (when (and nnimap-object
388 (nnimap-last-command-time nnimap-object
)
392 (nnimap-last-command-time nnimap-object
)))
393 ;; More than five minutes since the last command.
395 (ignore-errors ;E.g. "buffer foo has no process".
396 (nnimap-send-command "NOOP"))))))))
398 (defun nnimap-open-connection (buffer)
399 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
400 ;; `ssl' when nnimap-server-port was nil. Sort of.
401 (when (and nnimap-server-port
402 (eq nnimap-stream
'undecided
))
403 (setq nnimap-stream
'ssl
))
405 (if (eq nnimap-stream
'undecided
)
406 (cl-loop for type in
'(ssl network
)
407 for stream
= (let ((nnimap-stream type
))
408 (nnimap-open-connection-1 buffer
))
409 while
(eq stream
'no-connect
)
410 finally
(return stream
))
411 (nnimap-open-connection-1 buffer
))))
412 (if (eq stream
'no-connect
)
416 (defun nnimap-map-port (port)
417 (if (equal port
"imaps")
421 (defun nnimap-open-connection-1 (buffer)
422 (unless nnimap-keepalive-timer
423 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
424 #'nnimap-keepalive
)))
425 (with-current-buffer (nnimap-make-process-buffer buffer
)
426 (let* ((coding-system-for-read 'binary
)
427 (coding-system-for-write 'binary
)
430 ((memq nnimap-stream
'(network plain starttls
))
431 (nnheader-message 7 "Opening connection to %s..."
434 ((eq nnimap-stream
'shell
)
435 (nnheader-message 7 "Opening connection to %s via shell..."
438 ((memq nnimap-stream
'(ssl tls
))
439 (nnheader-message 7 "Opening connection to %s via tls..."
441 '("imaps" "imap" "993" "143"))
443 (error "Unknown stream type: %s" nnimap-stream
))))
444 login-result credentials
)
445 (when nnimap-server-port
446 (push nnimap-server-port ports
))
449 "*nnimap*" (current-buffer) nnimap-address
450 (nnimap-map-port (car ports
))
452 :warn-unless-encrypted t
454 :shell-command nnimap-shell-program
455 :capability-command
"1 CAPABILITY\r\n"
456 :always-query-capabilities t
457 :end-of-command
"\r\n"
460 (lambda (capabilities)
461 (when (string-match-p "STARTTLS" capabilities
)
463 (stream (car stream-list
))
464 (props (cdr stream-list
))
465 (greeting (plist-get props
:greeting
))
466 (capabilities (plist-get props
:capabilities
))
467 (stream-type (plist-get props
:type
))
468 (server (nnoo-current-server 'nnimap
)))
469 (when (and stream
(not (memq (process-status stream
) '(open run
))))
472 (when (eq (process-type stream
) 'network
)
473 ;; Use TCP-keepalive so that connections that pass through a NAT
474 ;; router don't hang when left idle.
475 (set-network-process-option stream
:keepalive t
))
477 (setf (nnimap-process nnimap-object
) stream
)
478 (setf (nnimap-stream-type nnimap-object
) stream-type
)
481 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
482 nnimap-address
(car ports
) nnimap-stream
)
484 (set-process-query-on-exit-flag stream nil
)
485 (if (not (string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
486 (nnheader-report 'nnimap
"%s" greeting
)
487 ;; Store the greeting (for debugging purposes).
488 (setf (nnimap-greeting nnimap-object
) greeting
)
489 (setf (nnimap-capabilities nnimap-object
)
491 (split-string capabilities
)))
492 (unless (string-match-p "[*.] PREAUTH" greeting
)
493 (if (not (setq credentials
494 (if (eq nnimap-authenticator
'anonymous
)
496 (message-make-address))
497 ;; Look for the credentials based on
498 ;; the virtual server name and the address
500 (gnus-delete-duplicates
501 (list server nnimap-address
))
504 (setq nnimap-object nil
)
505 (let ((nnimap-inhibit-logging t
))
507 (nnimap-login (car credentials
) (cadr credentials
))))
508 (if (car login-result
)
510 ;; Save the credentials if a save function exists
511 ;; (such a function will only be passed if a new
512 ;; token was created).
513 (when (functionp (nth 2 credentials
))
514 (funcall (nth 2 credentials
)))
515 ;; See if CAPABILITY is set as part of login
517 (dolist (response (cddr (nnimap-command "CAPABILITY")))
518 (when (string= "CAPABILITY" (upcase (car response
)))
519 (setf (nnimap-capabilities nnimap-object
)
520 (mapcar #'upcase
(cdr response
)))))
521 (when (and nnimap-use-namespaces
522 (nnimap-capability "NAMESPACE"))
524 (nnimap-wait-for-response (nnimap-send-command "NAMESPACE"))
525 (let ((response (nnimap-last-response-string)))
527 "^\\*\\W+NAMESPACE\\W+((\"\\([^\"\n]+\\)\"\\W+\"\\(.\\)\"))\\W+"
529 (setq nnimap-namespace
(match-string 1 response
))))))
530 ;; If the login failed, then forget the credentials
531 ;; that are now possibly cached.
532 (dolist (host (list (nnoo-current-server 'nnimap
)
535 (auth-source-forget+ :host host
:port port
)))
536 (delete-process (nnimap-process nnimap-object
))
537 (setq nnimap-object nil
))))
539 (when (nnimap-capability "QRESYNC")
540 (nnimap-command "ENABLE QRESYNC"))
541 (nnheader-message 7 "Opening connection to %s...done"
543 (nnimap-process nnimap-object
))))))))
545 (autoload 'rfc2104-hash
"rfc2104")
547 (defun nnimap-login (user password
)
549 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
550 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
551 ;; and we're using an encrypted connection.
552 ((and (not (nnimap-capability "LOGINDISABLED"))
553 (eq (nnimap-stream-type nnimap-object
) 'tls
)
554 (or (null nnimap-authenticator
)
555 (eq nnimap-authenticator
'anonymous
)
556 (eq nnimap-authenticator
'login
)))
557 (nnimap-command "LOGIN %S %S" user password
))
558 ((and (nnimap-capability "AUTH=CRAM-MD5")
559 (or (null nnimap-authenticator
)
560 (eq nnimap-authenticator
'cram-md5
)))
562 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
563 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
565 (get-buffer-process (current-buffer))
567 (base64-encode-string
569 (rfc2104-hash 'md5
64 16 password
570 (base64-decode-string challenge
))))
572 (nnimap-wait-for-response sequence
)))
573 ((and (not (nnimap-capability "LOGINDISABLED"))
574 (or (null nnimap-authenticator
)
575 (eq nnimap-authenticator
'anonymous
)
576 (eq nnimap-authenticator
'login
)))
577 (nnimap-command "LOGIN %S %S" user password
))
578 ((and (nnimap-capability "AUTH=PLAIN")
579 (or (null nnimap-authenticator
)
580 (eq nnimap-authenticator
'plain
)))
582 "AUTHENTICATE PLAIN %s"
583 (base64-encode-string
584 (format "\000%s\000%s"
585 (nnimap-quote-specials user
)
586 (nnimap-quote-specials password
)))))))
588 (defun nnimap-quote-specials (string)
591 (goto-char (point-min))
592 (while (re-search-forward "[\\\"]" nil t
)
598 (defun nnimap-find-parameter (parameter elems
)
602 ((equal (car elem
) parameter
)
603 (setq result
(cdr elem
)))
604 ((and (equal (car elem
) "OK")
606 (equal (caadr elem
) parameter
))
607 (setq result
(cdr (cadr elem
))))))
610 (deffoo nnimap-close-server
(&optional server
)
611 (when (nnoo-change-server 'nnimap server nil
)
613 (delete-process (get-buffer-process (nnimap-buffer))))
614 (nnoo-close-server 'nnimap server
)
617 (deffoo nnimap-request-close
()
620 (deffoo nnimap-server-opened
(&optional server
)
621 (and (nnoo-current-server-p 'nnimap server
)
623 (gnus-buffer-live-p nntp-server-buffer
)
624 (nnimap-find-connection nntp-server-buffer
)))
626 (deffoo nnimap-status-message
(&optional _server
)
627 nnimap-status-string
)
629 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
631 (setq group
(nnimap-decode-gnus-group group
)))
632 (with-current-buffer nntp-server-buffer
633 (let ((result (nnimap-change-group group server
))
635 (when (stringp article
)
636 (setq article
(nnimap-find-article-by-message-id group server article
)))
640 (with-current-buffer (nnimap-buffer)
642 (when nnimap-fetch-partial-articles
643 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
644 (goto-char (point-min))
645 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
646 (setq structure
(ignore-errors
647 (let ((start (point)))
649 (downcase-region start
(point))
651 (read (current-buffer))))
652 parts
(nnimap-find-wanted-parts structure
))))
654 (nnimap-get-partial-article article parts structure
)
655 (nnimap-get-whole-article article
))
656 (let ((buffer (current-buffer)))
657 (with-current-buffer (or to-buffer nntp-server-buffer
)
658 (nnheader-insert-buffer-substring buffer
)
659 (nnheader-ms-strip-cr)))
660 (cons group article
)))))))
662 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
664 (setq group
(nnimap-decode-gnus-group group
)))
665 (when (nnimap-change-group group server
)
666 (with-current-buffer (nnimap-buffer)
667 (when (stringp article
)
668 (setq article
(nnimap-find-article-by-message-id group server article
)))
671 (nnimap-get-whole-article
672 article
(format "UID FETCH %%d %s"
673 (nnimap-header-parameters)))
674 (let ((buffer (current-buffer)))
675 (with-current-buffer (or to-buffer nntp-server-buffer
)
677 (insert-buffer-substring buffer
)
678 (nnheader-ms-strip-cr)
679 (cons group article
)))))))
681 (deffoo nnimap-request-articles
(articles &optional group server
)
683 (setq group
(nnimap-decode-gnus-group group
)))
684 (with-current-buffer nntp-server-buffer
685 (let ((result (nnimap-change-group group server
)))
688 (with-current-buffer (nnimap-buffer)
690 (when (nnimap-command
692 "UID FETCH %s BODY.PEEK[]"
693 "UID FETCH %s RFC822.PEEK")
694 (nnimap-article-ranges (gnus-compress-sequence articles
)))
695 (let ((buffer (current-buffer)))
696 (with-current-buffer nntp-server-buffer
697 (nnheader-insert-buffer-substring buffer
)
698 (nnheader-ms-strip-cr)))
701 (defun nnimap-get-whole-article (article &optional command
)
706 "UID FETCH %d BODY.PEEK[]"
707 "UID FETCH %d RFC822.PEEK"))
709 ;; Check that we really got an article.
710 (goto-char (point-min))
711 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
714 ;; Remove any data that may have arrived before the FETCH data.
717 (delete-region (point-min) (point)))
718 (let ((bytes (nnimap-get-length)))
719 (delete-region (line-beginning-position)
720 (progn (forward-line 1) (point)))
721 (goto-char (+ (point) bytes
))
722 (delete-region (point) (point-max)))
725 (defun nnimap-capability (capability)
726 (member capability
(nnimap-capabilities nnimap-object
)))
728 (defun nnimap-ver4-p ()
729 (nnimap-capability "IMAP4REV1"))
731 (defun nnimap-get-partial-article (article parts structure
)
734 "UID FETCH %d (%s %s)"
740 (mapconcat (lambda (part)
741 (format "BODY.PEEK[%s]" part
))
743 (mapconcat (lambda (part)
744 (format "RFC822.PEEK[%s]" part
))
747 (nnimap-convert-partial-article structure
))))
749 (defun nnimap-convert-partial-article (structure)
750 ;; First just skip past the headers.
751 (goto-char (point-min))
752 (let ((bytes (nnimap-get-length))
754 ;; Delete "FETCH" line.
755 (delete-region (line-beginning-position)
756 (progn (forward-line 1) (point)))
757 (goto-char (+ (point) bytes
))
758 ;; Collect all the body parts.
759 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
760 (setq id
(match-string 1)
761 bytes
(or (nnimap-get-length) 0))
763 (delete-region (point) (progn (forward-line 1) (point)))
764 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
766 (delete-region (point) (+ (point) bytes
)))
767 ;; Delete trailing junk.
768 (delete-region (point) (point-max))
769 ;; Now insert all the parts again where they fit in the structure.
770 (nnimap-insert-partial-structure structure parts
)
773 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
775 (let ((bstruc structure
))
776 (while (consp (car bstruc
))
778 (setq type
(car bstruc
))
779 (setq bstruc
(car (cdr bstruc
)))
780 (let ((has-boundary (member "boundary" bstruc
)))
782 (setq boundary
(cadr has-boundary
)))))
784 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
785 (downcase type
) boundary
)))
786 (while (not (stringp (car structure
)))
787 (insert "\n--" boundary
"\n")
788 (if (consp (caar structure
))
789 (nnimap-insert-partial-structure (pop structure
) parts t
)
790 (let ((bit (pop structure
)))
791 (insert (format "Content-type: %s/%s"
792 (downcase (nth 0 bit
))
793 (downcase (nth 1 bit
))))
794 (if (member-ignore-case "CHARSET" (nth 2 bit
))
797 (cadr (member-ignore-case "CHARSET" (nth 2 bit
)))))
799 (insert (format "Content-transfer-encoding: %s\n"
802 (when (assoc (nth 9 bit
) parts
)
803 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
804 (insert "\n--" boundary
"--\n")))
806 (defun nnimap-find-wanted-parts (structure)
807 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
809 (defun nnimap-find-wanted-parts-1 (structure prefix
)
812 (while (consp (car structure
))
813 (let ((sub (pop structure
)))
814 (if (consp (car sub
))
815 (push (nnimap-find-wanted-parts-1
816 sub
(if (string= prefix
"")
817 (number-to-string num
)
818 (format "%s.%s" prefix num
)))
820 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
821 (id (if (string= prefix
"")
822 (number-to-string num
)
823 (format "%s.%s" prefix num
))))
824 (setcar (nthcdr 9 sub
) id
)
825 (when (if (eq nnimap-fetch-partial-articles t
)
827 (string-match nnimap-fetch-partial-articles type
))
832 (deffoo nnimap-request-group
(group &optional server dont-check info
)
833 (setq group
(nnimap-decode-gnus-group group
))
834 (let ((result (nnimap-change-group
835 ;; Don't SELECT the group if we're going to select it
837 (if (and (not dont-check
)
838 (assoc group nnimap-current-infos
))
842 (info (when info
(list info
)))
844 (with-current-buffer nntp-server-buffer
846 (when (or (not dont-check
)
848 (nth 2 (assoc group nnimap-current-infos
)))))
849 (let ((sequences (nnimap-retrieve-group-data-early
851 (nnimap-finish-retrieve-group-infos server info sequences
853 (setq active
(nth 2 (assoc group nnimap-current-infos
)))))
854 (setq active
(or active
'(0 .
1)))
856 (insert (format "211 %d %d %d %S\n"
857 (- (cdr active
) (car active
))
860 (nnimap-encode-gnus-group group
)))
863 (deffoo nnimap-request-group-scan
(group &optional server info
)
864 (setq group
(nnimap-decode-gnus-group group
))
865 (when (nnimap-change-group nil server
)
866 (let (marks high low
)
867 (with-current-buffer (nnimap-buffer)
869 (let ((group-sequence
870 (nnimap-send-command "SELECT %S" (nnimap-group-to-imap group
)))
872 (nnimap-send-command "UID FETCH 1:* FLAGS")))
873 (setf (nnimap-group nnimap-object
) group
)
874 (nnimap-wait-for-response flag-sequence
)
876 (nnimap-flags-to-marks
878 (list (list group-sequence flag-sequence
879 1 group
"SELECT")))))
882 (nnimap-update-infos marks
(list info
))
883 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
884 (goto-char (point-max))
885 (let ((uidnext (nth 5 (car marks
))))
886 (setq high
(or (if uidnext
890 low
(or (nth 4 (car marks
)) uidnext
1)))))
891 (with-current-buffer nntp-server-buffer
895 "211 %d %d %d %S\n" (1+ (- high low
)) low high
896 (nnimap-encode-gnus-group group
)))
899 (deffoo nnimap-request-create-group
(group &optional server _args
)
900 (setq group
(nnimap-decode-gnus-group group
))
901 (when (nnimap-change-group nil server
)
902 (with-current-buffer (nnimap-buffer)
903 (car (nnimap-command "CREATE %S" (nnimap-group-to-imap group
))))))
905 (deffoo nnimap-request-delete-group
(group &optional _force server
)
906 (setq group
(nnimap-decode-gnus-group group
))
907 (when (nnimap-change-group nil server
)
908 (with-current-buffer (nnimap-buffer)
909 (car (nnimap-command "DELETE %S" (nnimap-group-to-imap group
))))))
911 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
912 (setq group
(nnimap-decode-gnus-group group
))
913 (when (nnimap-change-group nil server
)
914 (with-current-buffer (nnimap-buffer)
915 (nnimap-unselect-group)
916 (car (nnimap-command "RENAME %S %S"
917 (nnimap-group-to-imap group
) (nnimap-group-to-imap new-name
))))))
919 (defun nnimap-unselect-group ()
920 ;; Make sure we don't have this group open read/write by asking
921 ;; to examine a mailbox that doesn't exist. This seems to be
922 ;; the only way that allows us to reliably go back to unselected
924 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
926 (deffoo nnimap-request-expunge-group
(group &optional server
)
927 (setq group
(nnimap-decode-gnus-group group
))
928 (when (nnimap-change-group group server
)
929 (with-current-buffer (nnimap-buffer)
930 (car (nnimap-command "EXPUNGE")))))
932 (defun nnimap-get-flags (spec)
935 (with-current-buffer (nnimap-buffer)
937 (nnimap-wait-for-response (nnimap-send-command
938 "UID FETCH %s FLAGS" spec
))
940 (subst-char-in-region (point-min) (point-max)
942 (goto-char (point-min))
943 (while (search-forward " FETCH " end t
)
944 (setq elems
(read (current-buffer)))
945 (push (cons (cadr (memq 'UID elems
))
946 (cadr (memq 'FLAGS elems
)))
948 (nreverse articles
)))
950 (deffoo nnimap-close-group
(_group &optional _server
)
953 (deffoo nnimap-request-move-article
(article group server accept-form
956 (setq group
(nnimap-decode-gnus-group group
))
957 (when internal-move-group
958 (setq internal-move-group
(nnimap-decode-gnus-group internal-move-group
)))
960 (mm-disable-multibyte)
961 (when (funcall (if internal-move-group
963 'nnimap-request-article
)
964 article group server
(current-buffer))
965 ;; If the move is internal (on the same server), just do it the
967 (let ((message-id (message-field-value "message-id")))
968 (if internal-move-group
969 (with-current-buffer (nnimap-buffer)
970 (let* ((can-move (and (nnimap-capability "MOVE")
971 (equal (nnimap-quirk "MOVE") "MOVE")))
972 (command (if can-move
975 (result (nnimap-command
977 (nnimap-group-to-imap internal-move-group
))))
978 (when (and (car result
) (not can-move
))
979 (nnimap-delete-article article
))
980 (cons internal-move-group
981 (or (nnimap-find-uid-response "COPYUID" (caddr result
))
982 (nnimap-find-article-by-message-id
983 internal-move-group server message-id
984 nnimap-request-articles-find-limit
)))))
985 ;; Move the article to a different method.
986 (when-let* ((result (eval accept-form
)))
987 (nnimap-change-group group server
)
988 (nnimap-delete-article article
)
991 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
992 (setq group
(nnimap-decode-gnus-group group
))
996 ((not (nnimap-change-group group server
))
999 (eq nnmail-expiry-target
'delete
))
1000 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
1001 (nnheader-message 7 "Article marked for deletion, but not expunged."))
1004 (let ((deletable-articles
1006 (eq nnmail-expiry-wait
'immediate
))
1008 (gnus-sorted-intersection
1010 (nnimap-find-expired-articles group
)))))
1011 (if (null deletable-articles
)
1013 (if (eq nnmail-expiry-target
'delete
)
1014 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
1015 (setq deletable-articles
1016 (nnimap-process-expiry-targets
1017 deletable-articles group server
)))
1018 ;; Return the articles we didn't delete.
1019 (gnus-sorted-complement articles deletable-articles
))))))
1021 (defun nnimap-process-expiry-targets (articles group server
)
1022 (let ((deleted-articles nil
)
1023 (articles-to-delete nil
))
1025 ;; shortcut further processing if we're going to delete the articles
1026 ((eq nnmail-expiry-target
'delete
)
1027 (setq articles-to-delete articles
)
1029 ;; or just move them to another folder on the same IMAP server
1030 ((and (not (functionp nnmail-expiry-target
))
1031 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
1032 (gnus-server-to-method
1033 (format "nnimap:%s" server
))))
1034 (and (nnimap-change-group group server
)
1035 (with-current-buffer (nnimap-buffer)
1036 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
1037 (let ((can-move (and (nnimap-capability "MOVE")
1038 (equal (nnimap-quirk "MOVE") "MOVE"))))
1043 (nnimap-article-ranges (gnus-compress-sequence articles
))
1044 (nnimap-group-to-imap (gnus-group-real-name nnmail-expiry-target
)))
1045 (set (if can-move
'deleted-articles
'articles-to-delete
) articles
))))
1048 (dolist (article articles
)
1049 (let ((target nnmail-expiry-target
))
1051 (mm-disable-multibyte)
1052 (when (nnimap-request-article article group server
(current-buffer))
1053 (when (functionp target
)
1054 (setq target
(funcall target group
)))
1056 (not (eq target
'delete
)))
1057 (if (or (gnus-request-group target t
)
1058 (gnus-request-create-group target
))
1060 (nnmail-expiry-target-group target group
)
1061 (nnheader-message 7 "Expiring article %s:%d to %s"
1062 group article target
))
1064 (nnheader-message 7 "Expiring article %s:%d" group article
))
1066 (push article articles-to-delete
))))))
1067 (setq articles-to-delete
(nreverse articles-to-delete
))))
1068 ;; Change back to the current group again.
1069 (nnimap-change-group group server
)
1070 (when articles-to-delete
1071 (nnimap-delete-article (gnus-compress-sequence articles-to-delete
))
1072 (setq deleted-articles articles-to-delete
))
1075 (defun nnimap-find-expired-articles (group)
1076 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
1078 (with-current-buffer (nnimap-buffer)
1081 "UID SEARCH SENTBEFORE %s"
1083 (format "%%d-%s-%%Y"
1085 (car (rassoc (nth 4 (decode-time cutoff
))
1086 parse-time-months
))))
1089 (delete 0 (mapcar #'string-to-number
1090 (cdr (assoc "SEARCH" (cdr result
)))))))))))
1092 (defun nnimap-find-article-by-message-id (group server message-id
1094 "Search for message with MESSAGE-ID in GROUP from SERVER.
1095 If LIMIT, first try to limit the search to the N last articles."
1096 (with-current-buffer (nnimap-buffer)
1098 (let* ((change-group-result (nnimap-change-group group server nil t
))
1100 (and (listp change-group-result
)
1102 (dolist (result (cdr change-group-result
))
1103 (when (equal "EXISTS" (cadr result
))
1104 (throw 'found
(car result
)))))))
1106 (nnimap-send-command
1107 "UID SEARCH%s HEADER Message-Id %S"
1108 (if (and limit number-of-article
)
1109 ;; The -1 is because IMAP message
1110 ;; numbers are one-based rather than
1112 (format " %s:*" (- (string-to-number number-of-article
)
1116 (when (nnimap-wait-for-response sequence
)
1117 (let ((article (car (last (cdr (assoc "SEARCH"
1118 (nnimap-parse-response)))))))
1120 (string-to-number article
)
1121 (when (and limit number-of-article
)
1122 (nnimap-find-article-by-message-id group server message-id
))))))))
1124 (defun nnimap-delete-article (articles)
1125 (with-current-buffer (nnimap-buffer)
1126 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
1127 (nnimap-article-ranges articles
))
1129 ((nnimap-capability "UIDPLUS")
1130 (nnimap-command "UID EXPUNGE %s"
1131 (nnimap-article-ranges articles
))
1134 (nnimap-command "EXPUNGE")
1136 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
1137 "server doesn't support UIDPLUS, so we won't "
1138 "delete this article now"))))))
1140 (deffoo nnimap-request-scan
(&optional group server
)
1142 (setq group
(nnimap-decode-gnus-group group
)))
1143 (when (and (nnimap-change-group nil server
)
1145 nnimap-split-methods
)
1146 (nnheader-message 7 "nnimap %s splitting mail..." server
)
1147 (if (listp nnimap-inbox
)
1148 (dolist (nnimap-inbox nnimap-inbox
)
1149 (nnimap-split-incoming-mail))
1150 (nnimap-split-incoming-mail))
1151 (nnheader-message 7 "nnimap %s splitting mail...done" server
)))
1153 (defun nnimap-marks-to-flags (marks)
1155 (dolist (mark marks
)
1156 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
1160 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
1161 (setq group
(nnimap-decode-gnus-group group
))
1162 (when (nnimap-change-group nil server
)
1163 (let ((command (assoc
1165 '((subscribe "SUBSCRIBE")
1166 (unsubscribe "UNSUBSCRIBE")))))
1168 (with-current-buffer (nnimap-buffer)
1169 (nnimap-command "%s %S" (cadr command
) (nnimap-group-to-imap group
)))))))
1171 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
1172 (setq group
(nnimap-decode-gnus-group group
))
1173 (when (nnimap-change-group group server
)
1175 (with-current-buffer (nnimap-buffer)
1177 ;; Just send all the STORE commands without waiting for
1178 ;; response. If they're successful, they're successful.
1179 (dolist (action actions
)
1180 (cl-destructuring-bind (range action marks
) action
1181 (let ((flags (nnimap-marks-to-flags marks
)))
1183 (setq sequence
(nnimap-send-command
1184 "UID STORE %s %sFLAGS.SILENT (%s)"
1185 (nnimap-article-ranges range
)
1187 ((eq action
'del
) "-")
1188 ((eq action
'add
) "+")
1189 ((eq action
'set
) ""))
1190 (mapconcat #'identity flags
" ")))))))
1191 ;; Wait for the last command to complete to avoid later
1192 ;; synchronization problems with the stream.
1194 (nnimap-wait-for-response sequence
))))))
1196 (deffoo nnimap-request-accept-article
(group &optional server _last
)
1198 ;; We're respooling. Find out where mail splitting would place
1202 (nnmail-article-group
1203 ;; We don't really care about the article number, because
1204 ;; that's determined by the IMAP server later. So just
1205 ;; return the group name.
1207 (list (list group
)))))))
1208 (setq group
(nnimap-decode-gnus-group group
))
1209 (when (nnimap-change-group nil server
)
1210 (nnmail-check-syntax)
1211 (let ((message-id (message-field-value "message-id"))
1214 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
1215 (with-current-buffer (nnimap-buffer)
1216 (when (setq message
(or (nnimap-process-quirk "OK Gimap " 'append message
)
1218 ;; If we have this group open read-only, then unselect it
1219 ;; before appending to it.
1220 (when (equal (nnimap-examined nnimap-object
) group
)
1221 (nnimap-unselect-group))
1223 (setq sequence
(nnimap-send-command
1224 "APPEND %S {%d}" (nnimap-group-to-imap group
)
1226 (unless nnimap-streaming
1227 (nnimap-wait-for-connection "^[+]"))
1228 (process-send-string (get-buffer-process (current-buffer)) message
)
1229 (process-send-string (get-buffer-process (current-buffer))
1230 (if (nnimap-newlinep nnimap-object
)
1233 (let ((result (nnimap-get-response sequence
)))
1234 (if (not (nnimap-ok-p result
))
1236 (nnheader-report 'nnimap
"%s" result
)
1239 (or (nnimap-find-uid-response "APPENDUID" (car result
))
1240 (nnimap-find-article-by-message-id
1241 group server message-id
1242 nnimap-request-articles-find-limit
))))))))))
1244 (defun nnimap-process-quirk (greeting-match type data
)
1245 (when (and (nnimap-greeting nnimap-object
)
1246 (string-match greeting-match
(nnimap-greeting nnimap-object
))
1248 (string-match "\000" data
))
1249 (let ((choice (gnus-multiple-choice
1250 "Message contains NUL characters. Delete, continue, abort? "
1251 '((?d
"Delete NUL characters")
1252 (?c
"Try to APPEND the message as is")
1256 (nnheader-report 'nnimap
"Aborted APPEND due to NUL characters"))
1262 (goto-char (point-min))
1263 (while (search-forward "\000" nil t
)
1264 (replace-match "" t t
))
1265 (buffer-string)))))))
1267 (defun nnimap-ok-p (value)
1270 (equal (caar value
) "OK")))
1272 (defun nnimap-find-uid-response (name list
)
1273 (let ((result (car (last (nnimap-find-response-element name list
)))))
1275 (string-to-number result
))))
1277 (defun nnimap-find-response-element (name list
)
1280 (when (and (consp elem
)
1281 (equal name
(car elem
)))
1282 (setq result elem
)))
1285 (deffoo nnimap-request-replace-article
(article group buffer
)
1286 (setq group
(nnimap-decode-gnus-group group
))
1288 (when (and (nnimap-change-group group
)
1289 ;; Put the article into the group.
1290 (with-current-buffer buffer
1292 (nnimap-request-accept-article group nil t
))))
1293 (nnimap-delete-article (list article
))
1294 ;; Return the new article number.
1297 (defun nnimap-add-cr ()
1298 (goto-char (point-min))
1299 (while (re-search-forward "\r?\n" nil t
)
1300 (replace-match "\r\n" t t
)))
1302 (defun nnimap-get-groups ()
1304 (let* ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1305 (prefix nnimap-namespace
)
1306 (prefix-len (if prefix
(length prefix
) nil
))
1308 (substring prefix
0 -
1) nil
))
1310 (nnimap-wait-for-response sequence
)
1311 (subst-char-in-region (point-min) (point-max)
1313 (goto-char (point-min))
1314 (nnimap-unfold-quoted-lines)
1315 (goto-char (point-min))
1316 (while (search-forward "* LIST " nil t
)
1317 (let ((flags (read (current-buffer)))
1318 (_separator (read (current-buffer)))
1319 (group (buffer-substring-no-properties
1320 (progn (skip-chars-forward " \"")
1322 (progn (end-of-line)
1323 (skip-chars-backward " \r\"")
1325 (unless (member '%NoSelect flags
)
1326 (let* ((group (utf7-decode (if (stringp group
) group
1327 (format "%s" group
)) t
))
1328 (group (cond ((or (not prefix
)
1329 (equal inbox group
))
1331 ((string-prefix-p prefix group
)
1332 (substring group prefix-len
))
1334 (concat "#" group
)))))
1335 (push group groups
)))))
1338 (defun nnimap-get-responses (sequences)
1340 (dolist (sequence sequences
)
1341 (goto-char (point-min))
1342 (when (re-search-forward (format "^%d " sequence
) nil t
)
1343 (push (list sequence
(nnimap-parse-response))
1347 (deffoo nnimap-request-list
(&optional server
)
1348 (when (nnimap-change-group nil server
)
1349 (with-current-buffer nntp-server-buffer
1352 (with-current-buffer (nnimap-buffer)
1353 (nnimap-get-groups)))
1354 sequences responses
)
1356 (with-current-buffer (nnimap-buffer)
1357 (setf (nnimap-group nnimap-object
) nil
)
1358 (dolist (group groups
)
1359 (setf (nnimap-examined nnimap-object
) group
)
1360 (push (list (nnimap-send-command "EXAMINE %S"
1361 (nnimap-group-to-imap group
))
1364 (nnimap-wait-for-response (caar sequences
))
1366 (nnimap-get-responses (mapcar #'car sequences
))))
1367 (dolist (response responses
)
1368 (let* ((sequence (car response
))
1369 (response (cadr response
))
1370 (group (cadr (assoc sequence sequences
)))
1371 (egroup (nnimap-encode-gnus-group group
)))
1373 (equal (caar response
) "OK"))
1374 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1376 (dolist (elem response
)
1377 (when (equal (cadr elem
) "EXISTS")
1378 (setq exists
(string-to-number (car elem
)))))
1380 (setq highest
(1- (string-to-number (car uidnext
)))))
1383 (insert (format "%S 0 1 y\n" egroup
)))
1386 (insert (format "%S %d %d y\n" egroup
1387 highest
(1+ highest
))))
1389 ;; Return the widest possible range.
1390 (insert (format "%S %d 1 y\n" egroup
1391 (or highest exists
)))))))))
1394 (deffoo nnimap-request-newgroups
(_date &optional server
)
1395 (when (nnimap-change-group nil server
)
1396 (with-current-buffer nntp-server-buffer
1398 (dolist (group (with-current-buffer (nnimap-buffer)
1399 (nnimap-get-groups)))
1400 (unless (assoc group nnimap-current-infos
)
1401 ;; Insert dummy numbers here -- they don't matter.
1402 (insert (format "%S 0 1 y\n" (nnimap-encode-gnus-group group
)))))
1405 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1406 (when (and (nnimap-change-group nil server
)
1408 (with-current-buffer (nnimap-buffer)
1410 (setf (nnimap-group nnimap-object
) nil
)
1411 (setf (nnimap-initial-resync nnimap-object
) 0)
1412 (let ((qresyncp (nnimap-capability "QRESYNC"))
1413 params sequences active uidvalidity modseq group
1415 ;; Go through the infos and gather the data needed to know
1416 ;; what and how to request the data.
1417 (dolist (info infos
)
1418 (setq params
(gnus-info-params info
)
1419 group
(nnimap-decode-gnus-group
1420 (gnus-group-real-name (gnus-info-group info
)))
1421 active
(cdr (assq 'active params
))
1422 unexist
(assq 'unexist
(gnus-info-marks info
))
1423 uidvalidity
(cdr (assq 'uidvalidity params
))
1424 modseq
(cdr (assq 'modseq params
)))
1425 (setf (nnimap-examined nnimap-object
) group
)
1432 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1433 (nnimap-group-to-imap group
)
1434 (nnimap-quirk "QRESYNC")
1442 ;; If we don't have a UIDVALIDITY, then this is
1443 ;; the first time we've seen the group, so we
1444 ;; have to do a SELECT (which is slower than an
1445 ;; examine), but will tell us whether the group
1446 ;; is read-only or not.
1449 (if (and active uidvalidity unexist
)
1450 ;; Fetch the last 100 flags.
1451 (setq start
(max 1 (- (cdr active
) 100)))
1452 (cl-incf (nnimap-initial-resync nnimap-object
))
1454 (push (list (nnimap-send-command "%s %S" command
1455 (nnimap-group-to-imap group
))
1456 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1457 start group command
)
1461 (defun nnimap-quirk (command)
1462 (let ((quirk (assoc command nnimap-quirks
)))
1463 ;; If this server is of a type that matches a quirk, then return
1464 ;; the "quirked" command instead of the proper one.
1465 (if (or (null quirk
)
1466 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1470 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
1471 &optional dont-insert
)
1472 (when (and sequences
1473 (nnimap-change-group nil server t
)
1474 ;; Check that the process is still alive.
1475 (get-buffer-process (nnimap-buffer))
1476 (memq (process-status (get-buffer-process (nnimap-buffer)))
1478 (with-current-buffer (nnimap-buffer)
1479 ;; Wait for the final data to trickle in.
1480 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1484 ;; Now we should have most of the data we need, no matter
1485 ;; whether we're QRESYNCING, fetching all the flags from
1486 ;; scratch, or just fetching the last 100 flags per group.
1487 (nnimap-update-infos (nnimap-flags-to-marks
1489 (nreverse sequences
)))
1492 ;; Finally, just return something resembling an active file in
1493 ;; the nntp buffer, so that the agent can save the info, too.
1494 (with-current-buffer nntp-server-buffer
1496 (dolist (info infos
)
1497 (let* ((group (gnus-info-group info
))
1498 (active (gnus-active group
)))
1500 (insert (format "%S %d %d y\n"
1501 (nnimap-encode-gnus-group
1502 (nnimap-decode-gnus-group
1503 (gnus-group-real-name group
)))
1505 (car active
))))))))))))
1507 (defun nnimap-update-infos (flags infos
)
1508 (dolist (info infos
)
1509 (let* ((group (nnimap-decode-gnus-group
1510 (gnus-group-real-name (gnus-info-group info
))))
1511 (marks (cdr (assoc group flags
))))
1513 (nnimap-update-info info marks
)))))
1515 (defun nnimap-update-info (info marks
)
1516 (cl-destructuring-bind (existing flags high low uidnext start-article
1517 permanent-flags uidvalidity
1518 vanished highestmodseq
) marks
1520 ;; Ignore groups with no UIDNEXT/marks. This happens for
1521 ;; completely empty groups.
1522 ((and (not existing
)
1524 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1526 (gnus-set-active (gnus-info-group info
) active
))))
1527 ;; We have a mismatch between the old and new UIDVALIDITY
1528 ;; identifiers, so we have to re-request the group info (the next
1529 ;; time). This virtually never happens.
1530 ((let ((old-uidvalidity
1531 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1532 (and old-uidvalidity
1533 (not (equal old-uidvalidity uidvalidity
))
1534 (or (not start-article
)
1535 (> start-article
1))))
1536 (gnus-group-remove-parameter info
'uidvalidity
)
1537 (gnus-group-remove-parameter info
'modseq
))
1538 ;; We have the data needed to update.
1540 (let* ((group (gnus-info-group info
))
1541 (completep (and start-article
1542 (= start-article
1)))
1543 (active (or (gnus-active group
)
1544 (cdr (assq 'active
(gnus-info-params info
))))))
1546 (setq high
(1- uidnext
)))
1547 ;; First set the active ranges based on high/low.
1549 (not (gnus-active group
)))
1550 (gnus-set-active group
1553 (cons (min (or low
(car active
))
1555 (max (or high
(cdr active
))
1560 ;; No articles in this group.
1561 (cons uidnext
(1- uidnext
)))
1563 (cons start-article
(1- start-article
)))
1565 ;; No articles and no uidnext.
1567 (gnus-set-active group
1569 (or high
(1- uidnext
)))))
1570 ;; See whether this is a read-only group.
1571 (unless (eq permanent-flags
'not-scanned
)
1572 (gnus-group-set-parameter
1573 info
'permanent-flags
1574 (and (or (memq '%
* permanent-flags
)
1575 (memq '%Seen permanent-flags
))
1577 ;; Update marks and read articles if this isn't a
1578 ;; read-only IMAP group.
1579 (when (setq permanent-flags
1580 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1581 (if (and highestmodseq
1582 (not start-article
))
1583 ;; We've gotten the data by QRESYNCing.
1584 (nnimap-update-qresync-info
1585 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1586 ;; Do normal non-QRESYNC flag updates.
1587 ;; Update the list of read articles.
1588 (unless start-article
1589 (setq start-article
1))
1591 (gnus-compress-sequence
1592 (gnus-set-difference
1593 (gnus-set-difference
1596 (cdr (assoc '%Seen flags
))
1597 (cdr (assoc '%Deleted flags
))))
1598 (cdr (assoc '%Flagged flags
)))))
1599 (read (gnus-range-difference
1600 (cons start-article high
) unread
)))
1601 (when (> start-article
1)
1604 (if (> start-article
1)
1605 (gnus-sorted-range-intersection
1606 (cons 1 (1- start-article
))
1607 (gnus-info-read info
))
1608 (gnus-info-read info
))
1610 (when (or (not (listp permanent-flags
))
1611 (memq '%Seen permanent-flags
))
1612 (gnus-info-set-read info read
))
1613 ;; Update the marks.
1614 (setq marks
(gnus-info-marks info
))
1615 (dolist (type (cdr nnimap-mark-alist
))
1616 (when (or (not (listp permanent-flags
))
1617 (memq (car (assoc (caddr type
) flags
))
1619 (memq '%
* permanent-flags
))
1620 (let ((old-marks (assoc (car type
) marks
))
1622 (gnus-compress-sequence
1623 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1624 (assoc (intern (cadr type
) obarray
) flags
)
1625 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1626 (setq marks
(delq old-marks marks
))
1628 (when (and old-marks
1629 (> start-article
1))
1630 (setq old-marks
(gnus-range-difference
1632 (cons start-article high
)))
1633 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1635 (push (cons (car type
) new-marks
) marks
)))))
1636 ;; Keep track of non-existing articles.
1637 (let* ((old-unexists (assq 'unexist marks
))
1638 (active (gnus-active group
))
1641 (gnus-range-difference
1643 (gnus-compress-sequence existing
))
1646 (gnus-list-range-difference
1647 existing
(gnus-active group
))))))
1648 (when (> (car active
) 1)
1649 (setq unexists
(gnus-range-add
1650 (cons 1 (1- (car active
)))
1653 (setcdr old-unexists unexists
)
1654 (push (cons 'unexist unexists
) marks
)))
1655 (gnus-info-set-marks info marks t
))))
1656 ;; Tell Gnus whether there are any \Recent messages in any of
1658 (let ((recent (cdr (assoc '%Recent flags
))))
1661 (> (car (last recent
)) (cdr active
)))
1662 (push (list (cons (gnus-group-real-name group
) 0))
1663 nnmail-split-history
)))
1664 ;; Note the active level for the next run-through.
1665 (gnus-group-set-parameter info
'active
(gnus-active group
))
1666 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1667 (gnus-group-set-parameter info
'modseq highestmodseq
)
1668 (nnimap-store-info info
(gnus-active group
)))))))
1670 (defun nnimap-update-qresync-info (info existing vanished flags
)
1671 ;; Add all the vanished articles to the list of read articles.
1676 (gnus-range-add (gnus-info-read info
)
1678 (cdr (assq '%Flagged flags
)))
1679 (cdr (assq '%Seen flags
))))
1680 (let ((marks (gnus-info-marks info
)))
1681 (dolist (type (cdr nnimap-mark-alist
))
1682 (let ((ticks (assoc (car type
) marks
))
1684 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1685 (assoc (intern (cadr type
) obarray
) flags
)
1686 (assoc (cadr type
) flags
))))) ; "\Flagged"
1687 (setq marks
(delq ticks marks
))
1689 ;; Add the new marks we got.
1690 (setq ticks
(gnus-add-to-range ticks new-marks
))
1691 ;; Remove the marks from messages that don't have them.
1692 (setq ticks
(gnus-remove-from-range
1694 (gnus-compress-sequence
1695 (gnus-sorted-complement existing new-marks
))))
1697 (push (cons (car type
) ticks
) marks
)))
1698 (gnus-info-set-marks info marks t
))
1699 ;; Add vanished to the list of unexisting articles.
1701 (let* ((old-unexists (assq 'unexist marks
))
1702 (unexists (gnus-range-add (cdr old-unexists
) vanished
)))
1704 (setcdr old-unexists unexists
)
1705 (push (cons 'unexist unexists
) marks
)))
1706 (gnus-info-set-marks info marks t
))))
1708 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1709 (if (zerop (length irange
))
1712 (dolist (elem (split-string irange
","))
1714 (if (string-match ":" elem
)
1715 (let ((numbers (split-string elem
":")))
1716 (cons (string-to-number (car numbers
))
1717 (string-to-number (cadr numbers
))))
1718 (string-to-number elem
))
1720 (nreverse result
))))
1722 (defun nnimap-store-info (info active
)
1723 (let* ((group (nnimap-decode-gnus-group
1724 (gnus-group-real-name (gnus-info-group info
))))
1725 (entry (assoc group nnimap-current-infos
)))
1727 (setcdr entry
(list info active
))
1728 (push (list group info active
) nnimap-current-infos
))))
1730 (defun nnimap-flags-to-marks (groups)
1731 (let (data group uidnext articles start-article mark permanent-flags
1732 uidvalidity vanished highestmodseq
)
1733 (dolist (elem groups
)
1734 (setq group
(car elem
)
1735 uidnext
(nth 1 elem
)
1736 start-article
(nth 2 elem
)
1737 permanent-flags
(nth 3 elem
)
1738 uidvalidity
(nth 4 elem
)
1739 vanished
(nth 5 elem
)
1740 highestmodseq
(nth 6 elem
)
1741 articles
(nthcdr 7 elem
))
1742 (let ((high (caar articles
))
1744 (dolist (article articles
)
1745 (setq low
(car article
))
1746 (push (car article
) existing
)
1747 (dolist (flag (cdr article
))
1748 (setq mark
(assoc flag marks
))
1750 (push (list flag
(car article
)) marks
)
1751 (setcdr mark
(cons (car article
) (cdr mark
))))))
1752 (push (list group existing marks high low uidnext start-article
1753 permanent-flags uidvalidity vanished highestmodseq
)
1757 (defun nnimap-parse-flags (sequences)
1758 (goto-char (point-min))
1759 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1761 (subst-char-in-region (point-min) (point-max)
1763 ;; Remove any MODSEQ entries in the buffer, because they may contain
1764 ;; numbers that are too large for 32-bit Emacsen.
1765 (while (re-search-forward " MODSEQ ([0-9]+)" nil t
)
1766 (replace-match "" t t
))
1767 (goto-char (point-min))
1768 (let (start end articles groups uidnext elems permanent-flags
1769 uidvalidity vanished highestmodseq
)
1770 (dolist (elem sequences
)
1771 (cl-destructuring-bind (group-sequence flag-sequence totalp group command
)
1773 (setq start
(point))
1775 ;; The EXAMINE was successful.
1776 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1781 (setq permanent-flags
1782 (if (equal command
"SELECT")
1783 (and (search-forward "PERMANENTFLAGS "
1784 (or end
(point-min)) t
)
1785 (read (current-buffer)))
1789 (and (search-forward "UIDNEXT "
1790 (or end
(point-min)) t
)
1791 (read (current-buffer))))
1794 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1795 (or end
(point-min)) t
)
1796 ;; Store UIDVALIDITY as a string, as it's
1797 ;; too big for 32-bit Emacsen, usually.
1801 (and (eq flag-sequence
'qresync
)
1802 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1803 (or end
(point-min)) t
)
1807 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1808 (or end
(point-min)) t
)
1812 ;; The UID FETCH FLAGS was successful.
1813 (or (eq flag-sequence
'qresync
)
1814 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1815 (if (eq flag-sequence
'qresync
)
1819 (setq start
(point))
1821 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1823 (setq elems
(read (current-buffer)))
1824 (push (cons (cadr (memq 'UID elems
))
1825 (cadr (memq 'FLAGS elems
)))
1827 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1828 vanished highestmodseq
)
1831 (if (eq flag-sequence
'qresync
)
1834 (setq articles nil
))))
1837 (defun nnimap-find-process-buffer (buffer)
1838 (cadr (assoc buffer nnimap-connection-alist
)))
1840 (deffoo nnimap-request-post
(&optional _server
)
1841 (setq nnimap-status-string
"Read-only server")
1844 (defvar gnus-refer-thread-use-nnir
) ;; gnus-sum.el
1845 (declare-function gnus-fetch-headers
"gnus-sum"
1846 (articles &optional limit force-new dependencies
))
1848 (autoload 'nnir-search-thread
"nnir")
1850 (deffoo nnimap-request-thread
(header &optional group server
)
1852 (setq group
(nnimap-decode-gnus-group group
)))
1853 (if gnus-refer-thread-use-nnir
1854 (nnir-search-thread header
)
1855 (when (nnimap-change-group group server
)
1856 (let* ((cmd (nnimap-make-thread-query header
))
1857 (result (with-current-buffer (nnimap-buffer)
1858 (nnimap-command "UID SEARCH %s" cmd
))))
1862 (delete 0 (mapcar #'string-to-number
1863 (cdr (assoc "SEARCH" (cdr result
))))))
1866 (defun nnimap-change-group (group &optional server no-reconnect read-only
)
1867 "Change group to GROUP if non-nil.
1868 If SERVER is set, check that server is connected, otherwise retry
1869 to reconnect, unless NO-RECONNECT is set to t. Return nil if
1870 unsuccessful in connecting.
1871 If GROUP is nil, return t.
1872 If READ-ONLY is set, send EXAMINE rather than SELECT to the server.
1873 Return the server's response to the SELECT or EXAMINE command."
1874 (let ((open-result t
))
1876 (not (nnimap-server-opened server
)))
1877 (setq open-result
(nnimap-open-server server nil no-reconnect
)))
1884 (with-current-buffer (nnimap-buffer)
1885 (let ((result (nnimap-command "%s %S"
1889 (nnimap-group-to-imap group
))))
1891 (setf (nnimap-group nnimap-object
) group
1892 (nnimap-select-result nnimap-object
) result
)
1895 (defun nnimap-find-connection (buffer)
1896 "Find the connection delivering to BUFFER."
1897 (let ((entry (assoc buffer nnimap-connection-alist
)))
1899 (if (and (buffer-name (cadr entry
))
1900 (get-buffer-process (cadr entry
))
1901 (memq (process-status (get-buffer-process (cadr entry
)))
1903 (get-buffer-process (cadr entry
))
1904 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1907 ;; Leave room for `open-network-stream' to issue a couple of IMAP
1908 ;; commands before nnimap starts.
1909 (defvar nnimap-sequence
5)
1911 (defun nnimap-send-command (&rest args
)
1912 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1913 (process-send-string
1914 (get-buffer-process (current-buffer))
1917 (cl-incf nnimap-sequence
)
1918 (apply #'format args
)
1919 (if (nnimap-newlinep nnimap-object
)
1922 ;; Some servers apparently can't have many outstanding
1923 ;; commands, so throttle them.
1924 (unless nnimap-streaming
1925 (nnimap-wait-for-response nnimap-sequence
))
1928 (defvar nnimap-record-commands nil
1929 "If non-nil, log commands to the \"*imap log*\" buffer.")
1931 (defun nnimap-log-buffer ()
1932 (let ((name "*imap log*"))
1933 (or (get-buffer name
)
1934 (with-current-buffer (get-buffer-create name
)
1935 (setq-local window-point-insertion-type t
)
1936 (current-buffer)))))
1938 (defun nnimap-log-command (command)
1939 (when nnimap-record-commands
1940 (with-current-buffer (nnimap-log-buffer)
1941 (goto-char (point-max))
1942 (insert (format-time-string "%H:%M:%S")
1943 " [" nnimap-address
"] "
1944 (if nnimap-inhibit-logging
1949 (defun nnimap-command (&rest args
)
1951 (let* ((sequence (apply #'nnimap-send-command args
))
1952 (response (nnimap-get-response sequence
)))
1953 (if (equal (caar response
) "OK")
1955 (nnheader-report 'nnimap
"%s"
1956 (mapconcat (lambda (a)
1958 (car response
) " "))
1961 (defun nnimap-get-response (sequence)
1962 (nnimap-wait-for-response sequence
)
1963 (nnimap-parse-response))
1965 (defun nnimap-wait-for-connection (&optional regexp
)
1966 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1968 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1969 (let ((process (get-buffer-process (current-buffer))))
1970 (goto-char (point-min))
1971 (while (and (memq (process-status process
)
1973 (not (re-search-forward regexp nil t
)))
1974 (nnheader-accept-process-output process
)
1975 (goto-char (point-min)))
1977 (and (looking-at (or response-regexp regexp
))
1980 (defun nnimap-wait-for-response (sequence &optional messagep
)
1981 (let ((process (get-buffer-process (current-buffer)))
1985 (goto-char (point-max))
1986 (while (and (setq openp
(memq (process-status process
)
1989 ;; Skip past any "*" lines that the server has
1991 (while (and (not (bobp))
1994 (looking-at "\\*\\|[0-9]+ OK NOOP"))))
1995 (not (looking-at (format "%d .*\n" sequence
)))))
1997 (nnheader-message-maybe
1998 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
2000 (if (not (zerop (nnimap-initial-resync nnimap-object
)))
2001 (format " (initial sync of %d group%s; please wait)"
2002 (nnimap-initial-resync nnimap-object
)
2003 (if (= (nnimap-initial-resync nnimap-object
) 1)
2007 (nnheader-accept-process-output process
)
2008 (goto-char (point-max)))
2009 (setf (nnimap-initial-resync nnimap-object
) 0)
2014 ;; The user hit C-g while we were waiting: kill the process, in case
2015 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
2017 (delete-process process
)
2020 (defun nnimap-parse-response ()
2021 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
2023 (dolist (line lines
)
2024 (push (cdr (nnimap-parse-line line
)) result
))
2025 ;; Return the OK/error code first, and then all the "continuation
2026 ;; lines" afterwards.
2028 (nreverse result
))))
2030 ;; Parse an IMAP response line lightly. They look like
2031 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
2032 ;; the lines into a list of strings and lists of string.
2033 (defun nnimap-parse-line (line)
2036 (mm-disable-multibyte)
2038 (goto-char (point-min))
2040 (if (eql (setq char
(following-char)) ?
)
2048 (if (search-forward "]" (line-end-position) 'move
)
2055 (if (search-forward ")" (line-end-position) 'move
)
2062 (1- (or (search-forward "\"" (line-end-position) 'move
)
2065 (buffer-substring (point) (if (search-forward " " nil t
)
2067 (goto-char (point-max))))))
2069 (nreverse result
))))
2071 (defun nnimap-last-response-string ()
2074 (let ((end (point)))
2078 (while (and (not (bobp))
2079 (eql (following-char) ?
*))
2081 (unless (eql (following-char) ?
*)
2083 (buffer-substring (point) end
))))
2085 (defvar nnimap-incoming-split-list nil
)
2087 (defun nnimap-fetch-inbox (articles)
2089 (nnimap-wait-for-response
2090 (nnimap-send-command
2092 (nnimap-article-ranges articles
)
2093 (format "(UID %s%s)"
2099 (nnimap-split-download-body-default
2107 (defun nnimap-split-incoming-mail ()
2108 (with-current-buffer (nnimap-buffer)
2109 (let ((nnimap-incoming-split-list nil
)
2110 (nnmail-split-methods
2112 ((eq nnimap-split-methods
'default
)
2113 nnmail-split-methods
)
2114 (nnimap-split-methods
2115 nnimap-split-methods
)
2117 'nnmail-split-fancy
)))
2118 (nnmail-split-fancy (or nnimap-split-fancy
2119 nnmail-split-fancy
))
2120 (nnmail-inhibit-default-split-group t
)
2121 (groups (nnimap-get-groups))
2122 (can-move (and (nnimap-capability "MOVE")
2123 (equal (nnimap-quirk "MOVE") "MOVE")))
2126 (nnimap-command "SELECT %S" nnimap-inbox
)
2127 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
2128 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
2130 (nnimap-fetch-inbox new-articles
)
2131 (nnimap-transform-split-mail)
2132 (nnheader-ms-strip-cr)
2134 (nnmail-split-incoming (current-buffer)
2135 #'nnimap-save-mail-spec
2137 #'nnimap-dummy-active-number
2138 #'nnimap-save-mail-spec
)
2139 (when nnimap-incoming-split-list
2140 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
2141 sequences junk-articles
)
2142 ;; Create any groups that doesn't already exist on the
2144 (dolist (spec specs
)
2145 (when (and (not (member (car spec
) groups
))
2146 (not (eq (car spec
) 'junk
)))
2147 (nnimap-command "CREATE %S" (nnimap-group-to-imap (car spec
)))))
2148 ;; Then copy over all the messages.
2150 (dolist (spec specs
)
2151 (let ((group (car spec
))
2152 (ranges (cdr spec
)))
2153 (if (eq group
'junk
)
2154 (setq junk-articles ranges
)
2155 ;; Don't copy if the message is already in its
2157 (unless (string= group nnimap-inbox
)
2158 (push (list (nnimap-send-command
2162 (nnimap-article-ranges ranges
)
2163 (nnimap-group-to-imap group
))
2166 ;; Wait for the last COPY response...
2167 (when (and (not can-move
) sequences
)
2168 (nnimap-wait-for-response (caar sequences
))
2169 ;; And then mark the successful copy actions as deleted,
2170 ;; and possibly expunge them.
2171 (nnimap-mark-and-expunge-incoming
2172 (nnimap-parse-copied-articles sequences
)))
2173 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
2175 (defun nnimap-mark-and-expunge-incoming (range)
2177 (setq range
(nnimap-article-ranges range
))
2180 (nnimap-send-command
2181 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
2183 ;; If the server supports it, we now delete the message we have
2184 ;; just copied over.
2185 ((nnimap-capability "UIDPLUS")
2186 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
2187 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
2188 ;; user has configured it.
2190 (setq sequence
(nnimap-send-command "EXPUNGE"))))
2191 (nnimap-wait-for-response sequence
))))
2193 (defun nnimap-parse-copied-articles (sequences)
2194 (let (sequence copied range
)
2195 (goto-char (point-min))
2196 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t
)
2197 (setq sequence
(string-to-number (match-string 1)))
2198 (when (setq range
(cadr (assq sequence sequences
)))
2199 (push (gnus-uncompress-range range
) copied
)))
2200 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
2202 (defun nnimap-new-articles (flags)
2204 (dolist (elem flags
)
2205 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
2207 (push (car elem
) new
)))
2208 (gnus-compress-sequence (nreverse new
))))
2210 (defun nnimap-make-split-specs (list)
2214 (cl-destructuring-bind (article spec
) elem
2215 (dolist (group (delete nil
(mapcar #'car spec
)))
2216 (unless (setq entry
(assoc group specs
))
2217 (push (setq entry
(list group
)) specs
))
2218 (setcdr entry
(cons article
(cdr entry
))))))
2219 (dolist (entry specs
)
2220 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
2223 (defun nnimap-transform-split-mail ()
2224 (goto-char (point-min))
2225 (let (article bytes
)
2228 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2229 (delete-region (point) (progn (forward-line 1) (point)))
2232 (setq article
(match-string 1)
2233 bytes
(nnimap-get-length))
2234 (delete-region (line-beginning-position) (line-end-position))
2235 ;; Insert MMDF separator, and a way to remember what this
2237 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
2238 (forward-char (1+ bytes
))
2239 (setq bytes
(nnimap-get-length))
2240 (delete-region (line-beginning-position) (line-end-position))
2241 ;; There's a body; skip past that.
2243 (forward-char (1+ bytes
))
2244 (delete-region (line-beginning-position) (line-end-position)))))))
2246 (defun nnimap-dummy-active-number (_group &optional _server
)
2249 (defun nnimap-save-mail-spec (group-art &optional _server _full-nov
)
2251 (goto-char (point-min))
2252 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
2253 (error "Invalid nnimap mail")
2254 (setq article
(string-to-number (match-string 1))))
2256 (if (eq group-art
'junk
)
2257 (list (cons 'junk
1))
2259 nnimap-incoming-split-list
)))
2261 (defun nnimap-make-thread-query (header)
2262 (let* ((id (mail-header-id header
))
2264 (or (mail-header-references header
)
2268 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2270 (dolist (refid refs value
)
2272 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2273 refid refid value
)))))
2278 ;;; nnimap.el ends here