Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / nnimap.el
blob12892c516a717c004b5f0bb43b35583b8823f1a6
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/>.
23 ;;; Commentary:
25 ;; nnimap interfaces Gnus with IMAP servers.
27 ;;; Code:
29 (eval-when-compile
30 (require 'cl-lib)
31 (require 'subr-x))
33 (require 'nnheader)
34 (require 'gnus-util)
35 (require 'gnus)
36 (require 'nnoo)
37 (require 'netrc)
38 (require 'utf7)
39 (require 'parse-time)
40 (require 'nnmail)
42 (autoload 'auth-source-forget+ "auth-source")
43 (autoload 'auth-source-search "auth-source")
45 (nnoo-declare nnimap)
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
54 "The IMAP port used.
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
71 back on `network'.")
73 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
74 (if (listp imap-shell-program)
75 (car imap-shell-program)
76 imap-shell-program)
77 "ssh %s imapd")
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
81 the format.")
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
89 "How mail is split.
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'."
99 "Emacs 24.1")
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
114 some servers.")
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
133 textual parts.")
135 (defgroup nnimap nil
136 "IMAP for Gnus."
137 :group 'gnus)
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)
142 :version "24.4"
143 :group 'nnimap)
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)
155 (cl-defstruct nnimap
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")
167 (score "gnus-score")
168 (save "gnus-save")
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)))
182 (utf7-encode
183 (cond ((or (not inbox)
184 (string-equal group inbox))
185 group)
186 ((string-prefix-p "#" group)
187 (substring group 1))
189 (concat nnimap-namespace group))) t)))
191 (defun nnimap-buffer ()
192 (nnimap-find-process-buffer nntp-server-buffer))
194 (defun nnimap-header-parameters ()
195 (let (params)
196 (push "UID" params)
197 (push "RFC822.SIZE" params)
198 (when (nnimap-capability "X-GM-EXT-1")
199 (push "X-GM-LABELS" params))
200 (push "BODYSTRUCTURE" params)
201 (push (format
202 (if (nnimap-ver4-p)
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))
208 params)
209 (format "%s" (nreverse params))))
211 (deffoo nnimap-retrieve-headers (articles &optional group server _fetch-old)
212 (when group
213 (setq group (nnimap-decode-gnus-group group)))
214 (with-current-buffer nntp-server-buffer
215 (erase-buffer)
216 (when (nnimap-change-group group server)
217 (with-current-buffer (nnimap-buffer)
218 (erase-buffer)
219 (nnimap-wait-for-response
220 (nnimap-send-command
221 "UID FETCH %s %s"
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))))
231 'headers))
233 (defun nnimap-transform-headers ()
234 (goto-char (point-min))
235 (let (article lines size string labels)
236 (cl-block nil
237 (while (not (eobp))
238 (while (not (looking-at "\\* [0-9]+ FETCH"))
239 (delete-region (point) (progn (forward-line 1) (point)))
240 (when (eobp)
241 (cl-return)))
242 (goto-char (match-end 0))
243 ;; Unfold quoted {number} strings.
244 (while (or (looking-at "[ (]{\\([0-9]+\\)}\r?\n")
245 (re-search-forward
246 "[^]][ (]{\\([0-9]+\\)}\r?\n"
247 (save-excursion
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)
252 (point-max)))
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))))
259 (beginning-of-line)
260 (setq article
261 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
263 (match-string 1)))
264 (setq lines nil)
265 (beginning-of-line)
266 (setq size
267 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
268 (line-end-position)
270 (match-string 1)))
271 (beginning-of-line)
272 (when (search-forward "X-GM-LABELS" (line-end-position) t)
273 (setq labels (ignore-errors (read (current-buffer)))))
274 (beginning-of-line)
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)))
281 (setq lines (if (and
282 (stringp (car structure))
283 (equal (upcase (nth 0 structure)) "MESSAGE")
284 (equal (upcase (nth 1 structure)) "RFC822"))
285 (nth 9 structure)
286 (nth 7 structure)))))
287 (delete-region (line-beginning-position) (line-end-position))
288 (insert (format "211 %s Article retrieved." article))
289 (forward-line 1)
290 (when size
291 (insert (format "Chars: %s\n" size)))
292 (when lines
293 (insert (format "Lines: %s\n" lines)))
294 (when labels
295 (insert (format "X-GM-LABELS: %s\n" labels)))
296 ;; Most servers have a blank line after the headers, but
297 ;; Davmail doesn't.
298 (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
299 (goto-char (point-max)))
300 (delete-region (line-beginning-position) (line-end-position))
301 (insert ".")
302 (forward-line 1)))))
304 (defun nnimap-unfold-quoted-lines ()
305 ;; Unfold quoted {number} strings.
306 (let (size string)
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)
319 (let (result)
320 (cond
321 ((numberp ranges)
322 (number-to-string ranges))
323 ((numberp (cdr ranges))
324 (format "%d:%d" (car ranges) (cdr ranges)))
326 (dolist (elem ranges)
327 (push
328 (if (consp elem)
329 (format "%d:%d" (car elem) (cdr elem))
330 (number-to-string elem))
331 result))
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)
340 (if no-reconnect
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)
346 (with-current-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)
352 (gnus-add-buffer)
353 (set (make-local-variable 'after-change-functions) nil)
354 (set (make-local-variable 'nnimap-object)
355 (make-nnimap :server (nnoo-current-server 'nnimap)
356 :initial-resync 0))
357 (push (list buffer (current-buffer)) nnimap-connection-alist)
358 (push (current-buffer) nnimap-process-buffers)
359 (current-buffer)))
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
368 :host address
369 :port ports
370 :user user
371 :require '(:user :secret)
372 :create t))))
373 (if found
374 (list (plist-get found :user)
375 (let ((secret (plist-get found :secret)))
376 (if (functionp secret)
377 (funcall secret)
378 secret))
379 (plist-get found :save-function))
380 nil)))
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)
389 (> (float-time
390 (time-subtract
392 (nnimap-last-command-time nnimap-object)))
393 ;; More than five minutes since the last command.
394 (* 5 60)))
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))
404 (let ((stream
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)
414 stream)))
416 (defun nnimap-map-port (port)
417 (if (equal port "imaps")
418 "993"
419 port))
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)
428 (ports
429 (cond
430 ((memq nnimap-stream '(network plain starttls))
431 (nnheader-message 7 "Opening connection to %s..."
432 nnimap-address)
433 '("imap" "143"))
434 ((eq nnimap-stream 'shell)
435 (nnheader-message 7 "Opening connection to %s via shell..."
436 nnimap-address)
437 '("imap"))
438 ((memq nnimap-stream '(ssl tls))
439 (nnheader-message 7 "Opening connection to %s via tls..."
440 nnimap-address)
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))
447 (let* ((stream-list
448 (open-network-stream
449 "*nnimap*" (current-buffer) nnimap-address
450 (nnimap-map-port (car ports))
451 :type nnimap-stream
452 :warn-unless-encrypted t
453 :return-list 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"
458 :success " OK "
459 :starttls-function
460 (lambda (capabilities)
461 (when (string-match-p "STARTTLS" capabilities)
462 "1 STARTTLS\r\n"))))
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))))
470 (setq stream nil))
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)
479 (if (not stream)
480 (progn
481 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
482 nnimap-address (car ports) nnimap-stream)
483 'no-connect)
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)
490 (mapcar #'upcase
491 (split-string capabilities)))
492 (unless (string-match-p "[*.] PREAUTH" greeting)
493 (if (not (setq credentials
494 (if (eq nnimap-authenticator 'anonymous)
495 (list "anonymous"
496 (message-make-address))
497 ;; Look for the credentials based on
498 ;; the virtual server name and the address
499 (nnimap-credentials
500 (gnus-delete-duplicates
501 (list server nnimap-address))
502 ports
503 nnimap-user))))
504 (setq nnimap-object nil)
505 (let ((nnimap-inhibit-logging t))
506 (setq login-result
507 (nnimap-login (car credentials) (cadr credentials))))
508 (if (car login-result)
509 (progn
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
516 ;; response.
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"))
523 (erase-buffer)
524 (nnimap-wait-for-response (nnimap-send-command "NAMESPACE"))
525 (let ((response (nnimap-last-response-string)))
526 (when (string-match
527 "^\\*\\W+NAMESPACE\\W+((\"\\([^\"\n]+\\)\"\\W+\"\\(.\\)\"))\\W+"
528 response)
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)
533 nnimap-address))
534 (dolist (port ports)
535 (auth-source-forget+ :host host :port port)))
536 (delete-process (nnimap-process nnimap-object))
537 (setq nnimap-object nil))))
538 (when nnimap-object
539 (when (nnimap-capability "QRESYNC")
540 (nnimap-command "ENABLE QRESYNC"))
541 (nnheader-message 7 "Opening connection to %s...done"
542 nnimap-address)
543 (nnimap-process nnimap-object))))))))
545 (autoload 'rfc2104-hash "rfc2104")
547 (defun nnimap-login (user password)
548 (cond
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)))
561 (erase-buffer)
562 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
563 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
564 (process-send-string
565 (get-buffer-process (current-buffer))
566 (concat
567 (base64-encode-string
568 (concat user " "
569 (rfc2104-hash 'md5 64 16 password
570 (base64-decode-string challenge))))
571 "\r\n"))
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)))
581 (nnimap-command
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)
589 (with-temp-buffer
590 (insert string)
591 (goto-char (point-min))
592 (while (re-search-forward "[\\\"]" nil t)
593 (forward-char -1)
594 (insert "\\")
595 (forward-char 1))
596 (buffer-string)))
598 (defun nnimap-find-parameter (parameter elems)
599 (let (result)
600 (dolist (elem elems)
601 (cond
602 ((equal (car elem) parameter)
603 (setq result (cdr elem)))
604 ((and (equal (car elem) "OK")
605 (consp (cadr elem))
606 (equal (caadr elem) parameter))
607 (setq result (cdr (cadr elem))))))
608 result))
610 (deffoo nnimap-close-server (&optional server)
611 (when (nnoo-change-server 'nnimap server nil)
612 (ignore-errors
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)
622 nntp-server-buffer
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)
630 (when group
631 (setq group (nnimap-decode-gnus-group group)))
632 (with-current-buffer nntp-server-buffer
633 (let ((result (nnimap-change-group group server))
634 parts structure)
635 (when (stringp article)
636 (setq article (nnimap-find-article-by-message-id group server article)))
637 (when (and result
638 article)
639 (erase-buffer)
640 (with-current-buffer (nnimap-buffer)
641 (erase-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)))
648 (forward-sexp 1)
649 (downcase-region start (point))
650 (goto-char start)
651 (read (current-buffer))))
652 parts (nnimap-find-wanted-parts structure))))
653 (when (if parts
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)
663 (when group
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)))
669 (if (null 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)
676 (erase-buffer)
677 (insert-buffer-substring buffer)
678 (nnheader-ms-strip-cr)
679 (cons group article)))))))
681 (deffoo nnimap-request-articles (articles &optional group server)
682 (when group
683 (setq group (nnimap-decode-gnus-group group)))
684 (with-current-buffer nntp-server-buffer
685 (let ((result (nnimap-change-group group server)))
686 (when result
687 (erase-buffer)
688 (with-current-buffer (nnimap-buffer)
689 (erase-buffer)
690 (when (nnimap-command
691 (if (nnimap-ver4-p)
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)))
699 t))))))
701 (defun nnimap-get-whole-article (article &optional command)
702 (let ((result
703 (nnimap-command
704 (or command
705 (if (nnimap-ver4-p)
706 "UID FETCH %d BODY.PEEK[]"
707 "UID FETCH %d RFC822.PEEK"))
708 article)))
709 ;; Check that we really got an article.
710 (goto-char (point-min))
711 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
712 (setq result nil))
713 (when result
714 ;; Remove any data that may have arrived before the FETCH data.
715 (beginning-of-line)
716 (unless (bobp)
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)))
723 t)))
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)
732 (let ((result
733 (nnimap-command
734 "UID FETCH %d (%s %s)"
735 article
736 (if (nnimap-ver4-p)
737 "BODY.PEEK[HEADER]"
738 "RFC822.HEADER")
739 (if (nnimap-ver4-p)
740 (mapconcat (lambda (part)
741 (format "BODY.PEEK[%s]" part))
742 parts " ")
743 (mapconcat (lambda (part)
744 (format "RFC822.PEEK[%s]" part))
745 parts " ")))))
746 (when result
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))
753 id parts)
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))
762 (beginning-of-line)
763 (delete-region (point) (progn (forward-line 1) (point)))
764 (push (list id (buffer-substring (point) (+ (point) bytes)))
765 parts)
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)
774 (let (type boundary)
775 (let ((bstruc structure))
776 (while (consp (car bstruc))
777 (pop bstruc))
778 (setq type (car bstruc))
779 (setq bstruc (car (cdr bstruc)))
780 (let ((has-boundary (member "boundary" bstruc)))
781 (when has-boundary
782 (setq boundary (cadr has-boundary)))))
783 (when subp
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))
795 (insert (format
796 "; charset=%S\n"
797 (cadr (member-ignore-case "CHARSET" (nth 2 bit)))))
798 (insert "\n"))
799 (insert (format "Content-transfer-encoding: %s\n"
800 (nth 5 bit)))
801 (insert "\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)
810 (let ((num 1)
811 parts)
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)))
819 parts)
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)
826 (equal id "1")
827 (string-match nnimap-fetch-partial-articles type))
828 (push id parts))))
829 (cl-incf num)))
830 (nreverse parts)))
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
836 ;; later, anyway.
837 (if (and (not dont-check)
838 (assoc group nnimap-current-infos))
840 group)
841 server))
842 (info (when info (list info)))
843 active)
844 (with-current-buffer nntp-server-buffer
845 (when result
846 (when (or (not dont-check)
847 (not (setq active
848 (nth 2 (assoc group nnimap-current-infos)))))
849 (let ((sequences (nnimap-retrieve-group-data-early
850 server info)))
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)))
855 (erase-buffer)
856 (insert (format "211 %d %d %d %S\n"
857 (- (cdr active) (car active))
858 (car active)
859 (cdr active)
860 (nnimap-encode-gnus-group group)))
861 t))))
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)
868 (erase-buffer)
869 (let ((group-sequence
870 (nnimap-send-command "SELECT %S" (nnimap-group-to-imap group)))
871 (flag-sequence
872 (nnimap-send-command "UID FETCH 1:* FLAGS")))
873 (setf (nnimap-group nnimap-object) group)
874 (nnimap-wait-for-response flag-sequence)
875 (setq marks
876 (nnimap-flags-to-marks
877 (nnimap-parse-flags
878 (list (list group-sequence flag-sequence
879 1 group "SELECT")))))
880 (when (and info
881 marks)
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
887 (1- uidnext)
888 (nth 3 (car marks)))
890 low (or (nth 4 (car marks)) uidnext 1)))))
891 (with-current-buffer nntp-server-buffer
892 (erase-buffer)
893 (insert
894 (format
895 "211 %d %d %d %S\n" (1+ (- high low)) low high
896 (nnimap-encode-gnus-group group)))
897 t))))
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
923 ;; state on Courier.
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)
933 (let ((articles nil)
934 elems end)
935 (with-current-buffer (nnimap-buffer)
936 (erase-buffer)
937 (nnimap-wait-for-response (nnimap-send-command
938 "UID FETCH %s FLAGS" spec))
939 (setq end (point))
940 (subst-char-in-region (point-min) (point-max)
941 ?\\ ?% t)
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)))
947 articles)))
948 (nreverse articles)))
950 (deffoo nnimap-close-group (_group &optional _server)
953 (deffoo nnimap-request-move-article (article group server accept-form
954 &optional _last
955 internal-move-group)
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)))
959 (with-temp-buffer
960 (mm-disable-multibyte)
961 (when (funcall (if internal-move-group
962 'nnimap-request-head
963 'nnimap-request-article)
964 article group server (current-buffer))
965 ;; If the move is internal (on the same server), just do it the
966 ;; easy way.
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
973 "UID MOVE %d %S"
974 "UID COPY %d %S"))
975 (result (nnimap-command
976 command article
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)
989 result))))))
991 (deffoo nnimap-request-expire-articles (articles group &optional server force)
992 (setq group (nnimap-decode-gnus-group group))
993 (cond
994 ((null articles)
995 nil)
996 ((not (nnimap-change-group group server))
997 articles)
998 ((and force
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."))
1002 nil)
1004 (let ((deletable-articles
1005 (if (or force
1006 (eq nnmail-expiry-wait 'immediate))
1007 articles
1008 (gnus-sorted-intersection
1009 articles
1010 (nnimap-find-expired-articles group)))))
1011 (if (null deletable-articles)
1012 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))
1024 (cond
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"))))
1039 (nnimap-command
1040 (if can-move
1041 "UID MOVE %s %S"
1042 "UID COPY %s %S")
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))
1050 (with-temp-buffer
1051 (mm-disable-multibyte)
1052 (when (nnimap-request-article article group server (current-buffer))
1053 (when (functionp target)
1054 (setq target (funcall target group)))
1055 (if (and target
1056 (not (eq target 'delete)))
1057 (if (or (gnus-request-group target t)
1058 (gnus-request-create-group target))
1059 (progn
1060 (nnmail-expiry-target-group target group)
1061 (nnheader-message 7 "Expiring article %s:%d to %s"
1062 group article target))
1063 (setq target nil))
1064 (nnheader-message 7 "Expiring article %s:%d" group article))
1065 (when target
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))
1073 deleted-articles))
1075 (defun nnimap-find-expired-articles (group)
1076 (let ((cutoff (nnmail-expired-article-p group nil nil)))
1077 (when cutoff
1078 (with-current-buffer (nnimap-buffer)
1079 (let ((result
1080 (nnimap-command
1081 "UID SEARCH SENTBEFORE %s"
1082 (format-time-string
1083 (format "%%d-%s-%%Y"
1084 (upcase
1085 (car (rassoc (nth 4 (decode-time cutoff))
1086 parse-time-months))))
1087 cutoff))))
1088 (and (car result)
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
1093 &optional limit)
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)
1097 (erase-buffer)
1098 (let* ((change-group-result (nnimap-change-group group server nil t))
1099 (number-of-article
1100 (and (listp change-group-result)
1101 (catch 'found
1102 (dolist (result (cdr change-group-result))
1103 (when (equal "EXISTS" (cadr result))
1104 (throw 'found (car result)))))))
1105 (sequence
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
1111 ;; zero-based.
1112 (format " %s:*" (- (string-to-number number-of-article)
1113 limit -1))
1115 message-id)))
1116 (when (nnimap-wait-for-response sequence)
1117 (let ((article (car (last (cdr (assoc "SEARCH"
1118 (nnimap-parse-response)))))))
1119 (if article
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))
1128 (cond
1129 ((nnimap-capability "UIDPLUS")
1130 (nnimap-command "UID EXPUNGE %s"
1131 (nnimap-article-ranges articles))
1133 (nnimap-expunge
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)
1141 (when group
1142 (setq group (nnimap-decode-gnus-group group)))
1143 (when (and (nnimap-change-group nil server)
1144 nnimap-inbox
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)
1154 (let (flags flag)
1155 (dolist (mark marks)
1156 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
1157 (push flag flags)))
1158 flags))
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
1164 status
1165 '((subscribe "SUBSCRIBE")
1166 (unsubscribe "UNSUBSCRIBE")))))
1167 (when command
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)
1174 (let (sequence)
1175 (with-current-buffer (nnimap-buffer)
1176 (erase-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)))
1182 (when flags
1183 (setq sequence (nnimap-send-command
1184 "UID STORE %s %sFLAGS.SILENT (%s)"
1185 (nnimap-article-ranges range)
1186 (cond
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.
1193 (when sequence
1194 (nnimap-wait-for-response sequence))))))
1196 (deffoo nnimap-request-accept-article (group &optional server _last)
1197 (unless group
1198 ;; We're respooling. Find out where mail splitting would place
1199 ;; this article.
1200 (setq group
1201 (caar
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.
1206 `(lambda (group)
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"))
1212 sequence message)
1213 (nnimap-add-cr)
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)
1217 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))
1222 (erase-buffer)
1223 (setq sequence (nnimap-send-command
1224 "APPEND %S {%d}" (nnimap-group-to-imap group)
1225 (length message)))
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)
1231 "\n"
1232 "\r\n"))
1233 (let ((result (nnimap-get-response sequence)))
1234 (if (not (nnimap-ok-p result))
1235 (progn
1236 (nnheader-report 'nnimap "%s" result)
1237 nil)
1238 (cons group
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))
1247 (eq type 'append)
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")
1253 (?a "Abort")))))
1254 (cond
1255 ((eq choice ?a)
1256 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1257 ((eq choice ?c)
1258 data)
1260 (with-temp-buffer
1261 (insert data)
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)
1268 (and (consp value)
1269 (consp (car 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)))))
1274 (and result
1275 (string-to-number result))))
1277 (defun nnimap-find-response-element (name list)
1278 (let (result)
1279 (dolist (elem list)
1280 (when (and (consp elem)
1281 (equal name (car elem)))
1282 (setq result elem)))
1283 result))
1285 (deffoo nnimap-request-replace-article (article group buffer)
1286 (setq group (nnimap-decode-gnus-group group))
1287 (let (group-art)
1288 (when (and (nnimap-change-group group)
1289 ;; Put the article into the group.
1290 (with-current-buffer buffer
1291 (setq group-art
1292 (nnimap-request-accept-article group nil t))))
1293 (nnimap-delete-article (list article))
1294 ;; Return the new article number.
1295 (cdr group-art))))
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 ()
1303 (erase-buffer)
1304 (let* ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1305 (prefix nnimap-namespace)
1306 (prefix-len (if prefix (length prefix) nil))
1307 (inbox (if prefix
1308 (substring prefix 0 -1) nil))
1309 groups)
1310 (nnimap-wait-for-response sequence)
1311 (subst-char-in-region (point-min) (point-max)
1312 ?\\ ?% t)
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 " \"")
1321 (point))
1322 (progn (end-of-line)
1323 (skip-chars-backward " \r\"")
1324 (point)))))
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))
1330 group)
1331 ((string-prefix-p prefix group)
1332 (substring group prefix-len))
1334 (concat "#" group)))))
1335 (push group groups)))))
1336 (nreverse groups)))
1338 (defun nnimap-get-responses (sequences)
1339 (let (responses)
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))
1344 responses)))
1345 responses))
1347 (deffoo nnimap-request-list (&optional server)
1348 (when (nnimap-change-group nil server)
1349 (with-current-buffer nntp-server-buffer
1350 (erase-buffer)
1351 (let ((groups
1352 (with-current-buffer (nnimap-buffer)
1353 (nnimap-get-groups)))
1354 sequences responses)
1355 (when groups
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))
1362 group)
1363 sequences))
1364 (nnimap-wait-for-response (caar sequences))
1365 (setq responses
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)))
1372 (when (and group
1373 (equal (caar response) "OK"))
1374 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1375 highest exists)
1376 (dolist (elem response)
1377 (when (equal (cadr elem) "EXISTS")
1378 (setq exists (string-to-number (car elem)))))
1379 (when uidnext
1380 (setq highest (1- (string-to-number (car uidnext)))))
1381 (cond
1382 ((null highest)
1383 (insert (format "%S 0 1 y\n" egroup)))
1384 ((zerop exists)
1385 ;; Empty group.
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)))))))))
1392 t)))))
1394 (deffoo nnimap-request-newgroups (_date &optional server)
1395 (when (nnimap-change-group nil server)
1396 (with-current-buffer nntp-server-buffer
1397 (erase-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)))))
1403 t)))
1405 (deffoo nnimap-retrieve-group-data-early (server infos)
1406 (when (and (nnimap-change-group nil server)
1407 infos)
1408 (with-current-buffer (nnimap-buffer)
1409 (erase-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
1414 unexist)
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)
1426 (if (and qresyncp
1427 uidvalidity
1428 active
1429 modseq
1430 unexist)
1431 (push
1432 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1433 (nnimap-group-to-imap group)
1434 (nnimap-quirk "QRESYNC")
1435 uidvalidity modseq)
1436 'qresync
1437 nil group 'qresync)
1438 sequences)
1439 (let ((command
1440 (if uidvalidity
1441 "EXAMINE"
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.
1447 "SELECT"))
1448 start)
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))
1453 (setq start 1))
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)
1458 sequences))))
1459 sequences))))
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))))
1467 command
1468 (nth 2 quirk))))
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)))
1477 '(open run)))
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)
1481 (caar sequences)
1482 (cadar sequences))
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
1488 (nnimap-parse-flags
1489 (nreverse sequences)))
1490 infos)
1491 (unless dont-insert
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
1495 (erase-buffer)
1496 (dolist (info infos)
1497 (let* ((group (gnus-info-group info))
1498 (active (gnus-active group)))
1499 (when active
1500 (insert (format "%S %d %d y\n"
1501 (nnimap-encode-gnus-group
1502 (nnimap-decode-gnus-group
1503 (gnus-group-real-name group)))
1504 (cdr active)
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))))
1512 (when marks
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
1519 (cond
1520 ;; Ignore groups with no UIDNEXT/marks. This happens for
1521 ;; completely empty groups.
1522 ((and (not existing)
1523 (not uidnext))
1524 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1525 (when active
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))))))
1545 (when uidnext
1546 (setq high (1- uidnext)))
1547 ;; First set the active ranges based on high/low.
1548 (if (or completep
1549 (not (gnus-active group)))
1550 (gnus-set-active group
1551 (cond
1552 (active
1553 (cons (min (or low (car active))
1554 (car active))
1555 (max (or high (cdr active))
1556 (cdr active))))
1557 ((and low high)
1558 (cons low high))
1559 (uidnext
1560 ;; No articles in this group.
1561 (cons uidnext (1- uidnext)))
1562 (start-article
1563 (cons start-article (1- start-article)))
1565 ;; No articles and no uidnext.
1566 nil)))
1567 (gnus-set-active group
1568 (cons (car active)
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))
1576 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))
1590 (let* ((unread
1591 (gnus-compress-sequence
1592 (gnus-set-difference
1593 (gnus-set-difference
1594 existing
1595 (gnus-sorted-union
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)
1602 (setq read
1603 (gnus-range-nconcat
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))
1609 read)))
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))
1618 permanent-flags)
1619 (memq '%* permanent-flags))
1620 (let ((old-marks (assoc (car type) marks))
1621 (new-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))
1627 (pop old-marks)
1628 (when (and old-marks
1629 (> start-article 1))
1630 (setq old-marks (gnus-range-difference
1631 old-marks
1632 (cons start-article high)))
1633 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1634 (when 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))
1639 (unexists
1640 (if completep
1641 (gnus-range-difference
1642 active
1643 (gnus-compress-sequence existing))
1644 (gnus-add-to-range
1645 (cdr old-unexists)
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)))
1651 unexists)))
1652 (if old-unexists
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
1657 ;; the groups.
1658 (let ((recent (cdr (assoc '%Recent flags))))
1659 (when (and active
1660 recent
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.
1672 (gnus-info-set-read
1673 info
1674 (gnus-add-to-range
1675 (gnus-add-to-range
1676 (gnus-range-add (gnus-info-read info)
1677 vanished)
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))
1683 (new-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))
1688 (pop ticks)
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
1693 ticks
1694 (gnus-compress-sequence
1695 (gnus-sorted-complement existing new-marks))))
1696 (when ticks
1697 (push (cons (car type) ticks) marks)))
1698 (gnus-info-set-marks info marks t))
1699 ;; Add vanished to the list of unexisting articles.
1700 (when vanished
1701 (let* ((old-unexists (assq 'unexist marks))
1702 (unexists (gnus-range-add (cdr old-unexists) vanished)))
1703 (if old-unexists
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))
1711 (let ((result nil))
1712 (dolist (elem (split-string irange ","))
1713 (push
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))
1719 result))
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)))
1726 (if entry
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))
1743 marks low existing)
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))
1749 (if (not mark)
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)
1754 data)))
1755 data))
1757 (defun nnimap-parse-flags (sequences)
1758 (goto-char (point-min))
1759 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1760 ;; read it.
1761 (subst-char-in-region (point-min) (point-max)
1762 ?\\ ?% t)
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)
1772 elem
1773 (setq start (point))
1774 (when (and
1775 ;; The EXAMINE was successful.
1776 (search-forward (format "\n%d OK " group-sequence) nil t)
1777 (progn
1778 (forward-line 1)
1779 (setq end (point))
1780 (goto-char start)
1781 (setq permanent-flags
1782 (if (equal command "SELECT")
1783 (and (search-forward "PERMANENTFLAGS "
1784 (or end (point-min)) t)
1785 (read (current-buffer)))
1786 'not-scanned))
1787 (goto-char start)
1788 (setq uidnext
1789 (and (search-forward "UIDNEXT "
1790 (or end (point-min)) t)
1791 (read (current-buffer))))
1792 (goto-char start)
1793 (setq uidvalidity
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.
1798 (match-string 1)))
1799 (goto-char start)
1800 (setq vanished
1801 (and (eq flag-sequence 'qresync)
1802 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1803 (or end (point-min)) t)
1804 (match-string 1)))
1805 (goto-char start)
1806 (setq highestmodseq
1807 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1808 (or end (point-min)) t)
1809 (match-string 1)))
1810 (goto-char end)
1811 (forward-line -1))
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)
1816 (progn
1817 (goto-char start)
1818 (setq start end))
1819 (setq start (point))
1820 (goto-char end))
1821 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1822 (progn
1823 (setq elems (read (current-buffer)))
1824 (push (cons (cadr (memq 'UID elems))
1825 (cadr (memq 'FLAGS elems)))
1826 articles)))
1827 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1828 vanished highestmodseq)
1829 articles)
1830 groups)
1831 (if (eq flag-sequence 'qresync)
1832 (goto-char end)
1833 (setq end (point)))
1834 (setq articles nil))))
1835 groups))
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")
1842 nil)
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)
1851 (when group
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))))
1859 (when result
1860 (gnus-fetch-headers
1861 (and (car result)
1862 (delete 0 (mapcar #'string-to-number
1863 (cdr (assoc "SEARCH" (cdr result))))))
1864 nil t))))))
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))
1875 (when (and server
1876 (not (nnimap-server-opened server)))
1877 (setq open-result (nnimap-open-server server nil no-reconnect)))
1878 (cond
1879 ((not open-result)
1880 nil)
1881 ((not group)
1884 (with-current-buffer (nnimap-buffer)
1885 (let ((result (nnimap-command "%s %S"
1886 (if read-only
1887 "EXAMINE"
1888 "SELECT")
1889 (nnimap-group-to-imap group))))
1890 (when (car result)
1891 (setf (nnimap-group nnimap-object) group
1892 (nnimap-select-result nnimap-object) result)
1893 result)))))))
1895 (defun nnimap-find-connection (buffer)
1896 "Find the connection delivering to BUFFER."
1897 (let ((entry (assoc buffer nnimap-connection-alist)))
1898 (when entry
1899 (if (and (buffer-name (cadr entry))
1900 (get-buffer-process (cadr entry))
1901 (memq (process-status (get-buffer-process (cadr entry)))
1902 '(open run)))
1903 (get-buffer-process (cadr entry))
1904 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1905 nil))))
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))
1915 (nnimap-log-command
1916 (format "%d %s%s\n"
1917 (cl-incf nnimap-sequence)
1918 (apply #'format args)
1919 (if (nnimap-newlinep nnimap-object)
1921 "\r"))))
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))
1926 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
1945 "(inhibited)\n"
1946 command))))
1947 command)
1949 (defun nnimap-command (&rest args)
1950 (erase-buffer)
1951 (let* ((sequence (apply #'nnimap-send-command args))
1952 (response (nnimap-get-response sequence)))
1953 (if (equal (caar response) "OK")
1954 (cons t response)
1955 (nnheader-report 'nnimap "%s"
1956 (mapconcat (lambda (a)
1957 (format "%s" a))
1958 (car response) " "))
1959 nil)))
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)
1972 '(open run))
1973 (not (re-search-forward regexp nil t)))
1974 (nnheader-accept-process-output process)
1975 (goto-char (point-min)))
1976 (forward-line -1)
1977 (and (looking-at (or response-regexp regexp))
1978 (match-string 1))))
1980 (defun nnimap-wait-for-response (sequence &optional messagep)
1981 (let ((process (get-buffer-process (current-buffer)))
1982 openp)
1983 (condition-case nil
1984 (progn
1985 (goto-char (point-max))
1986 (while (and (setq openp (memq (process-status process)
1987 '(open run)))
1988 (progn
1989 ;; Skip past any "*" lines that the server has
1990 ;; output.
1991 (while (and (not (bobp))
1992 (progn
1993 (forward-line -1)
1994 (looking-at "\\*\\|[0-9]+ OK NOOP"))))
1995 (not (looking-at (format "%d .*\n" sequence)))))
1996 (when messagep
1997 (nnheader-message-maybe
1998 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
1999 nnimap-address
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)
2005 "s"))
2006 "")))
2007 (nnheader-accept-process-output process)
2008 (goto-char (point-max)))
2009 (setf (nnimap-initial-resync nnimap-object) 0)
2010 openp)
2011 (quit
2012 (when debug-on-quit
2013 (debug "Quit"))
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
2016 ;; NAT routers).
2017 (delete-process process)
2018 nil))))
2020 (defun nnimap-parse-response ()
2021 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
2022 result)
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.
2027 (cons (pop result)
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)
2034 (let (char result)
2035 (with-temp-buffer
2036 (mm-disable-multibyte)
2037 (insert line)
2038 (goto-char (point-min))
2039 (while (not (eobp))
2040 (if (eql (setq char (following-char)) ? )
2041 (forward-char 1)
2042 (push
2043 (cond
2044 ((eql char ?\[)
2045 (split-string
2046 (buffer-substring
2047 (1+ (point))
2048 (if (search-forward "]" (line-end-position) 'move)
2049 (1- (point))
2050 (point)))))
2051 ((eql char ?\()
2052 (split-string
2053 (buffer-substring
2054 (1+ (point))
2055 (if (search-forward ")" (line-end-position) 'move)
2056 (1- (point))
2057 (point)))))
2058 ((eql char ?\")
2059 (forward-char 1)
2060 (buffer-substring
2061 (point)
2062 (1- (or (search-forward "\"" (line-end-position) 'move)
2063 (point)))))
2065 (buffer-substring (point) (if (search-forward " " nil t)
2066 (1- (point))
2067 (goto-char (point-max))))))
2068 result)))
2069 (nreverse result))))
2071 (defun nnimap-last-response-string ()
2072 (save-excursion
2073 (forward-line 1)
2074 (let ((end (point)))
2075 (forward-line -1)
2076 (when (not (bobp))
2077 (forward-line -1)
2078 (while (and (not (bobp))
2079 (eql (following-char) ?*))
2080 (forward-line -1))
2081 (unless (eql (following-char) ?*)
2082 (forward-line 1)))
2083 (buffer-substring (point) end))))
2085 (defvar nnimap-incoming-split-list nil)
2087 (defun nnimap-fetch-inbox (articles)
2088 (erase-buffer)
2089 (nnimap-wait-for-response
2090 (nnimap-send-command
2091 "UID FETCH %s %s"
2092 (nnimap-article-ranges articles)
2093 (format "(UID %s%s)"
2094 (format
2095 (if (nnimap-ver4-p)
2096 "BODY.PEEK"
2097 "RFC822.PEEK"))
2098 (cond
2099 (nnimap-split-download-body-default
2100 "[]")
2101 ((nnimap-ver4-p)
2102 "[HEADER]")
2104 "[1]"))))
2107 (defun nnimap-split-incoming-mail ()
2108 (with-current-buffer (nnimap-buffer)
2109 (let ((nnimap-incoming-split-list nil)
2110 (nnmail-split-methods
2111 (cond
2112 ((eq nnimap-split-methods 'default)
2113 nnmail-split-methods)
2114 (nnimap-split-methods
2115 nnimap-split-methods)
2116 (nnimap-split-fancy
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")))
2124 new-articles)
2125 (erase-buffer)
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:*")))
2129 (when new-articles
2130 (nnimap-fetch-inbox new-articles)
2131 (nnimap-transform-split-mail)
2132 (nnheader-ms-strip-cr)
2133 (nnmail-cache-open)
2134 (nnmail-split-incoming (current-buffer)
2135 #'nnimap-save-mail-spec
2136 nil nil
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
2143 ;; server first.
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.
2149 (erase-buffer)
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
2156 ;; target group.
2157 (unless (string= group nnimap-inbox)
2158 (push (list (nnimap-send-command
2159 (if can-move
2160 "UID MOVE %s %S"
2161 "UID COPY %s %S")
2162 (nnimap-article-ranges ranges)
2163 (nnimap-group-to-imap group))
2164 ranges)
2165 sequences)))))
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)
2176 (when range
2177 (setq range (nnimap-article-ranges range))
2178 (erase-buffer)
2179 (let ((sequence
2180 (nnimap-send-command
2181 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
2182 (cond
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.
2189 (nnimap-expunge
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)
2203 (let (new)
2204 (dolist (elem flags)
2205 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
2206 (cdr elem))
2207 (push (car elem) new)))
2208 (gnus-compress-sequence (nreverse new))))
2210 (defun nnimap-make-split-specs (list)
2211 (let ((specs nil)
2212 entry)
2213 (dolist (elem 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) #'<))))
2221 specs))
2223 (defun nnimap-transform-split-mail ()
2224 (goto-char (point-min))
2225 (let (article bytes)
2226 (cl-block nil
2227 (while (not (eobp))
2228 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2229 (delete-region (point) (progn (forward-line 1) (point)))
2230 (when (eobp)
2231 (cl-return)))
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
2236 ;; article UID is.
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.
2242 (when bytes
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)
2250 (let (article)
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))))
2255 (push (list article
2256 (if (eq group-art 'junk)
2257 (list (cons 'junk 1))
2258 group-art))
2259 nnimap-incoming-split-list)))
2261 (defun nnimap-make-thread-query (header)
2262 (let* ((id (mail-header-id header))
2263 (refs (split-string
2264 (or (mail-header-references header)
2265 "")))
2266 (value
2267 (format
2268 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2269 id id)))
2270 (dolist (refid refs value)
2271 (setq value (format
2272 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2273 refid refid value)))))
2276 (provide 'nnimap)
2278 ;;; nnimap.el ends here