* fns.c (Fcompare_strings): Doc fix.
[emacs.git] / lisp / gnus / nnimap.el
blob5126c25f66b614db9b9f19d0d3b51bed7b305490
1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; nnimap interfaces Gnus with IMAP servers.
27 ;;; Code:
29 ;; For Emacs <22.2 and XEmacs.
30 (eval-and-compile
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-and-compile
34 (require 'nnheader)
35 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
36 ;; `make-network-stream'.
37 (unless (fboundp 'open-protocol-stream)
38 (require 'proto-stream)))
40 (eval-when-compile
41 (require 'cl))
43 (require 'nnheader)
44 (require 'gnus-util)
45 (require 'gnus)
46 (require 'nnoo)
47 (require 'netrc)
48 (require 'utf7)
49 (require 'tls)
50 (require 'parse-time)
51 (require 'nnmail)
53 (autoload 'auth-source-forget+ "auth-source")
54 (autoload 'auth-source-search "auth-source")
56 (nnoo-declare nnimap)
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-user nil
62 "Username to use for authentication to the IMAP server.")
64 (defvoo nnimap-server-port nil
65 "The IMAP port used.
66 If nnimap-stream is `ssl', this will default to `imaps'. If not,
67 it will default to `imap'.")
69 (defvoo nnimap-stream 'undecided
70 "How nnimap talks to the IMAP server.
71 The value should be either `undecided', `ssl' or `tls',
72 `network', `starttls', `plain', or `shell'.
74 If the value is `undecided', nnimap tries `ssl' first, then falls
75 back on `network'.")
77 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
78 (if (listp imap-shell-program)
79 (car imap-shell-program)
80 imap-shell-program)
81 "ssh %s imapd"))
83 (defvoo nnimap-inbox nil
84 "The mail box where incoming mail arrives and should be split out of.
85 For example, \"INBOX\".")
87 (defvoo nnimap-split-methods nil
88 "How mail is split.
89 Uses the same syntax as `nnmail-split-methods'.")
91 (defvoo nnimap-split-fancy nil
92 "Uses the same syntax as `nnmail-split-fancy'.")
94 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
95 "Articles with the flags in the list will not be considered when splitting.")
97 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
98 "Emacs 24.1")
100 (defvoo nnimap-authenticator nil
101 "How nnimap authenticate itself to the server.
102 Possible choices are nil (use default methods) or `anonymous'.")
104 (defvoo nnimap-expunge t
105 "If non-nil, expunge articles after deleting them.
106 This is always done if the server supports UID EXPUNGE, but it's
107 not done by default on servers that doesn't support that command.")
109 (defvoo nnimap-streaming t
110 "If non-nil, try to use streaming commands with IMAP servers.
111 Switching this off will make nnimap slower, but it helps with
112 some servers.")
114 (defvoo nnimap-connection-alist nil)
116 (defvoo nnimap-current-infos nil)
118 (defvoo nnimap-fetch-partial-articles nil
119 "If non-nil, Gnus will fetch partial articles.
120 If t, Gnus will fetch only the first part. If a string, it
121 will fetch all parts that have types that match that string. A
122 likely value would be \"text/\" to automatically fetch all
123 textual parts.")
125 (defvar nnimap-process nil)
127 (defvar nnimap-status-string "")
129 (defvar nnimap-split-download-body-default nil
130 "Internal variable with default value for `nnimap-split-download-body'.")
132 (defvar nnimap-keepalive-timer nil)
133 (defvar nnimap-process-buffers nil)
135 (defstruct nnimap
136 group process commands capabilities select-result newlinep server
137 last-command-time greeting examined stream-type initial-resync)
139 (defvar nnimap-object nil)
141 (defvar nnimap-mark-alist
142 '((read "\\Seen" %Seen)
143 (tick "\\Flagged" %Flagged)
144 (reply "\\Answered" %Answered)
145 (expire "gnus-expire")
146 (dormant "gnus-dormant")
147 (score "gnus-score")
148 (save "gnus-save")
149 (download "gnus-download")
150 (forward "gnus-forward")))
152 (defvar nnimap-quirks
153 '(("QRESYNC" "Zimbra" "QRESYNC ")))
155 (defvar nnimap-inhibit-logging nil)
157 (defun nnimap-buffer ()
158 (nnimap-find-process-buffer nntp-server-buffer))
160 (defun nnimap-header-parameters ()
161 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
162 (format
163 (if (nnimap-ver4-p)
164 "BODY.PEEK[HEADER.FIELDS %s]"
165 "RFC822.HEADER.LINES %s")
166 (append '(Subject From Date Message-Id
167 References In-Reply-To Xref)
168 nnmail-extra-headers))))
170 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
171 (when group
172 (setq group (nnimap-decode-gnus-group group)))
173 (with-current-buffer nntp-server-buffer
174 (erase-buffer)
175 (when (nnimap-possibly-change-group group server)
176 (with-current-buffer (nnimap-buffer)
177 (erase-buffer)
178 (nnimap-wait-for-response
179 (nnimap-send-command
180 "UID FETCH %s %s"
181 (nnimap-article-ranges (gnus-compress-sequence articles))
182 (nnimap-header-parameters))
184 (nnimap-transform-headers)
185 (nnheader-remove-cr-followed-by-lf))
186 (insert-buffer-substring
187 (nnimap-find-process-buffer (current-buffer))))
188 'headers))
190 (defun nnimap-transform-headers ()
191 (goto-char (point-min))
192 (let (article lines size string)
193 (block nil
194 (while (not (eobp))
195 (while (not (looking-at "\\* [0-9]+ FETCH"))
196 (delete-region (point) (progn (forward-line 1) (point)))
197 (when (eobp)
198 (return)))
199 (goto-char (match-end 0))
200 ;; Unfold quoted {number} strings.
201 (while (re-search-forward
202 "[^]][ (]{\\([0-9]+\\)}\r?\n"
203 (save-excursion
204 ;; Start of the header section.
205 (or (re-search-forward "] {[0-9]+}\r?\n" nil t)
206 ;; Start of the next FETCH.
207 (re-search-forward "\\* [0-9]+ FETCH" nil t)
208 (point-max)))
210 (setq size (string-to-number (match-string 1)))
211 (delete-region (+ (match-beginning 0) 2) (point))
212 (setq string (buffer-substring (point) (+ (point) size)))
213 (delete-region (point) (+ (point) size))
214 (insert (format "%S" (mm-subst-char-in-string ?\n ?\s string))))
215 (beginning-of-line)
216 (setq article
217 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
219 (match-string 1)))
220 (setq lines nil)
221 (setq size
222 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
223 (line-end-position)
225 (match-string 1)))
226 (beginning-of-line)
227 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
228 (let ((structure (ignore-errors
229 (read (current-buffer)))))
230 (while (and (consp structure)
231 (not (atom (car structure))))
232 (setq structure (car structure)))
233 (setq lines (if (and
234 (stringp (car structure))
235 (equal (upcase (nth 0 structure)) "MESSAGE")
236 (equal (upcase (nth 1 structure)) "RFC822"))
237 (nth 9 structure)
238 (nth 7 structure)))))
239 (delete-region (line-beginning-position) (line-end-position))
240 (insert (format "211 %s Article retrieved." article))
241 (forward-line 1)
242 (when size
243 (insert (format "Chars: %s\n" size)))
244 (when lines
245 (insert (format "Lines: %s\n" lines)))
246 (unless (re-search-forward "^\r$" nil t)
247 (goto-char (point-max)))
248 (delete-region (line-beginning-position) (line-end-position))
249 (insert ".")
250 (forward-line 1)))))
252 (defun nnimap-unfold-quoted-lines ()
253 ;; Unfold quoted {number} strings.
254 (let (size string)
255 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t)
256 (setq size (string-to-number (match-string 1)))
257 (delete-region (1+ (match-beginning 0)) (point))
258 (setq string (buffer-substring (point) (+ (point) size)))
259 (delete-region (point) (+ (point) size))
260 (insert (format "%S" string)))))
262 (defun nnimap-get-length ()
263 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
264 (string-to-number (match-string 1))))
266 (defun nnimap-article-ranges (ranges)
267 (let (result)
268 (cond
269 ((numberp ranges)
270 (number-to-string ranges))
271 ((numberp (cdr ranges))
272 (format "%d:%d" (car ranges) (cdr ranges)))
274 (dolist (elem ranges)
275 (push
276 (if (consp elem)
277 (format "%d:%d" (car elem) (cdr elem))
278 (number-to-string elem))
279 result))
280 (mapconcat #'identity (nreverse result) ",")))))
282 (deffoo nnimap-open-server (server &optional defs no-reconnect)
283 (if (nnimap-server-opened server)
285 (unless (assq 'nnimap-address defs)
286 (setq defs (append defs (list (list 'nnimap-address server)))))
287 (nnoo-change-server 'nnimap server defs)
288 (if no-reconnect
289 (nnimap-find-connection nntp-server-buffer)
290 (or (nnimap-find-connection nntp-server-buffer)
291 (nnimap-open-connection nntp-server-buffer)))))
293 (defun nnimap-make-process-buffer (buffer)
294 (with-current-buffer
295 (generate-new-buffer (format " *nnimap %s %s %s*"
296 nnimap-address nnimap-server-port
297 (gnus-buffer-exists-p buffer)))
298 (mm-disable-multibyte)
299 (buffer-disable-undo)
300 (gnus-add-buffer)
301 (set (make-local-variable 'after-change-functions) nil)
302 (set (make-local-variable 'nnimap-object)
303 (make-nnimap :server (nnoo-current-server 'nnimap)
304 :initial-resync 0))
305 (push (list buffer (current-buffer)) nnimap-connection-alist)
306 (push (current-buffer) nnimap-process-buffers)
307 (current-buffer)))
309 (defun nnimap-credentials (address ports user)
310 (let* ((auth-source-creation-prompts
311 '((user . "IMAP user at %h: ")
312 (secret . "IMAP password for %u@%h: ")))
313 (found (nth 0 (auth-source-search :max 1
314 :host address
315 :port ports
316 :user user
317 :require '(:user :secret)
318 :create t))))
319 (if found
320 (list (plist-get found :user)
321 (let ((secret (plist-get found :secret)))
322 (if (functionp secret)
323 (funcall secret)
324 secret))
325 (plist-get found :save-function))
326 nil)))
328 (defun nnimap-keepalive ()
329 (let ((now (current-time)))
330 (dolist (buffer nnimap-process-buffers)
331 (when (buffer-name buffer)
332 (with-current-buffer buffer
333 (when (and nnimap-object
334 (nnimap-last-command-time nnimap-object)
335 (> (gnus-float-time
336 (time-subtract
338 (nnimap-last-command-time nnimap-object)))
339 ;; More than five minutes since the last command.
340 (* 5 60)))
341 (nnimap-send-command "NOOP")))))))
343 (defun nnimap-open-connection (buffer)
344 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
345 ;; `ssl' when nnimap-server-port was nil. Sort of.
346 (when (and nnimap-server-port
347 (eq nnimap-stream 'undecided))
348 (setq nnimap-stream 'ssl))
349 (let ((stream
350 (if (eq nnimap-stream 'undecided)
351 (loop for type in '(ssl network)
352 for stream = (let ((nnimap-stream type))
353 (nnimap-open-connection-1 buffer))
354 while (eq stream 'no-connect)
355 finally (return stream))
356 (nnimap-open-connection-1 buffer))))
357 (if (eq stream 'no-connect)
359 stream)))
361 (defun nnimap-map-port (port)
362 (if (equal port "imaps")
363 "993"
364 port))
366 (defun nnimap-open-connection-1 (buffer)
367 (unless nnimap-keepalive-timer
368 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
369 'nnimap-keepalive)))
370 (with-current-buffer (nnimap-make-process-buffer buffer)
371 (let* ((coding-system-for-read 'binary)
372 (coding-system-for-write 'binary)
373 (ports
374 (cond
375 ((memq nnimap-stream '(network plain starttls))
376 (nnheader-message 7 "Opening connection to %s..."
377 nnimap-address)
378 '("imap" "143"))
379 ((eq nnimap-stream 'shell)
380 (nnheader-message 7 "Opening connection to %s via shell..."
381 nnimap-address)
382 '("imap"))
383 ((memq nnimap-stream '(ssl tls))
384 (nnheader-message 7 "Opening connection to %s via tls..."
385 nnimap-address)
386 '("imaps" "imap" "993" "143"))
388 (error "Unknown stream type: %s" nnimap-stream))))
389 login-result credentials)
390 (when nnimap-server-port
391 (push nnimap-server-port ports))
392 (let* ((stream-list
393 (open-protocol-stream
394 "*nnimap*" (current-buffer) nnimap-address
395 (nnimap-map-port (car ports))
396 :type nnimap-stream
397 :return-list t
398 :shell-command nnimap-shell-program
399 :capability-command "1 CAPABILITY\r\n"
400 :end-of-command "\r\n"
401 :success " OK "
402 :starttls-function
403 (lambda (capabilities)
404 (when (gnus-string-match-p "STARTTLS" capabilities)
405 "1 STARTTLS\r\n"))))
406 (stream (car stream-list))
407 (props (cdr stream-list))
408 (greeting (plist-get props :greeting))
409 (capabilities (plist-get props :capabilities))
410 (stream-type (plist-get props :type)))
411 (when (and stream (not (memq (process-status stream) '(open run))))
412 (setq stream nil))
414 (when (and (fboundp 'set-network-process-option) ;; Not in XEmacs.
415 (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
416 (eq (process-type stream) 'network))
417 ;; Use TCP-keepalive so that connections that pass through a NAT
418 ;; router don't hang when left idle.
419 (set-network-process-option stream :keepalive t))
421 (setf (nnimap-process nnimap-object) stream)
422 (setf (nnimap-stream-type nnimap-object) stream-type)
423 (if (not stream)
424 (progn
425 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
426 nnimap-address (car ports) nnimap-stream)
427 'no-connect)
428 (gnus-set-process-query-on-exit-flag stream nil)
429 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting))
430 (nnheader-report 'nnimap "%s" greeting)
431 ;; Store the greeting (for debugging purposes).
432 (setf (nnimap-greeting nnimap-object) greeting)
433 (setf (nnimap-capabilities nnimap-object)
434 (mapcar #'upcase
435 (split-string capabilities)))
436 (unless (gnus-string-match-p "[*.] PREAUTH" greeting)
437 (if (not (setq credentials
438 (if (eq nnimap-authenticator 'anonymous)
439 (list "anonymous"
440 (message-make-address))
441 ;; Look for the credentials based on
442 ;; the virtual server name and the address
443 (nnimap-credentials
444 (gnus-delete-duplicates
445 (list
446 nnimap-address
447 (nnoo-current-server 'nnimap)))
448 ports
449 nnimap-user))))
450 (setq nnimap-object nil)
451 (let ((nnimap-inhibit-logging t))
452 (setq login-result
453 (nnimap-login (car credentials) (cadr credentials))))
454 (if (car login-result)
455 (progn
456 ;; Save the credentials if a save function exists
457 ;; (such a function will only be passed if a new
458 ;; token was created).
459 (when (functionp (nth 2 credentials))
460 (funcall (nth 2 credentials)))
461 ;; See if CAPABILITY is set as part of login
462 ;; response.
463 (dolist (response (cddr login-result))
464 (when (string= "CAPABILITY" (upcase (car response)))
465 (setf (nnimap-capabilities nnimap-object)
466 (mapcar #'upcase (cdr response))))))
467 ;; If the login failed, then forget the credentials
468 ;; that are now possibly cached.
469 (dolist (host (list (nnoo-current-server 'nnimap)
470 nnimap-address))
471 (dolist (port ports)
472 (auth-source-forget+ :host host :port port)))
473 (delete-process (nnimap-process nnimap-object))
474 (setq nnimap-object nil))))
475 (when nnimap-object
476 (when (nnimap-capability "QRESYNC")
477 (nnimap-command "ENABLE QRESYNC"))
478 (nnheader-message 7 "Opening connection to %s...done"
479 nnimap-address)
480 (nnimap-process nnimap-object))))))))
482 (autoload 'rfc2104-hash "rfc2104")
484 (defun nnimap-login (user password)
485 (cond
486 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
487 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
488 ;; and we're using an encrypted connection.
489 ((and (not (nnimap-capability "LOGINDISABLED"))
490 (eq (nnimap-stream-type nnimap-object) 'tls))
491 (nnimap-command "LOGIN %S %S" user password))
492 ((nnimap-capability "AUTH=CRAM-MD5")
493 (erase-buffer)
494 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
495 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
496 (process-send-string
497 (get-buffer-process (current-buffer))
498 (concat
499 (base64-encode-string
500 (concat user " "
501 (rfc2104-hash 'md5 64 16 password
502 (base64-decode-string challenge))))
503 "\r\n"))
504 (nnimap-wait-for-response sequence)))
505 ((not (nnimap-capability "LOGINDISABLED"))
506 (nnimap-command "LOGIN %S %S" user password))
507 ((nnimap-capability "AUTH=PLAIN")
508 (nnimap-command
509 "AUTHENTICATE PLAIN %s"
510 (base64-encode-string
511 (format "\000%s\000%s"
512 (nnimap-quote-specials user)
513 (nnimap-quote-specials password)))))))
515 (defun nnimap-quote-specials (string)
516 (with-temp-buffer
517 (insert string)
518 (goto-char (point-min))
519 (while (re-search-forward "[\\\"]" nil t)
520 (forward-char -1)
521 (insert "\\")
522 (forward-char 1))
523 (buffer-string)))
525 (defun nnimap-find-parameter (parameter elems)
526 (let (result)
527 (dolist (elem elems)
528 (cond
529 ((equal (car elem) parameter)
530 (setq result (cdr elem)))
531 ((and (equal (car elem) "OK")
532 (consp (cadr elem))
533 (equal (caadr elem) parameter))
534 (setq result (cdr (cadr elem))))))
535 result))
537 (deffoo nnimap-close-server (&optional server)
538 (when (nnoo-change-server 'nnimap server nil)
539 (ignore-errors
540 (delete-process (get-buffer-process (nnimap-buffer))))
541 (nnoo-close-server 'nnimap server)
544 (deffoo nnimap-request-close ()
547 (deffoo nnimap-server-opened (&optional server)
548 (and (nnoo-current-server-p 'nnimap server)
549 nntp-server-buffer
550 (gnus-buffer-live-p nntp-server-buffer)
551 (nnimap-find-connection nntp-server-buffer)))
553 (deffoo nnimap-status-message (&optional server)
554 nnimap-status-string)
556 (deffoo nnimap-request-article (article &optional group server to-buffer)
557 (when group
558 (setq group (nnimap-decode-gnus-group group)))
559 (with-current-buffer nntp-server-buffer
560 (let ((result (nnimap-possibly-change-group group server))
561 parts structure)
562 (when (stringp article)
563 (setq article (nnimap-find-article-by-message-id group article)))
564 (when (and result
565 article)
566 (erase-buffer)
567 (with-current-buffer (nnimap-buffer)
568 (erase-buffer)
569 (when nnimap-fetch-partial-articles
570 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
571 (goto-char (point-min))
572 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
573 (setq structure (ignore-errors
574 (let ((start (point)))
575 (forward-sexp 1)
576 (downcase-region start (point))
577 (goto-char start)
578 (read (current-buffer))))
579 parts (nnimap-find-wanted-parts structure))))
580 (when (if parts
581 (nnimap-get-partial-article article parts structure)
582 (nnimap-get-whole-article article))
583 (let ((buffer (current-buffer)))
584 (with-current-buffer (or to-buffer nntp-server-buffer)
585 (nnheader-insert-buffer-substring buffer)
586 (nnheader-ms-strip-cr)))
587 (cons group article)))))))
589 (deffoo nnimap-request-head (article &optional group server to-buffer)
590 (when group
591 (setq group (nnimap-decode-gnus-group group)))
592 (when (nnimap-possibly-change-group group server)
593 (with-current-buffer (nnimap-buffer)
594 (when (stringp article)
595 (setq article (nnimap-find-article-by-message-id group article)))
596 (if (null article)
598 (nnimap-get-whole-article
599 article (format "UID FETCH %%d %s"
600 (nnimap-header-parameters)))
601 (let ((buffer (current-buffer)))
602 (with-current-buffer (or to-buffer nntp-server-buffer)
603 (erase-buffer)
604 (insert-buffer-substring buffer)
605 (nnheader-ms-strip-cr)
606 (cons group article)))))))
608 (defun nnimap-get-whole-article (article &optional command)
609 (let ((result
610 (nnimap-command
611 (or command
612 (if (nnimap-ver4-p)
613 "UID FETCH %d BODY.PEEK[]"
614 "UID FETCH %d RFC822.PEEK"))
615 article)))
616 ;; Check that we really got an article.
617 (goto-char (point-min))
618 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
619 (setq result nil))
620 (when result
621 ;; Remove any data that may have arrived before the FETCH data.
622 (beginning-of-line)
623 (unless (bobp)
624 (delete-region (point-min) (point)))
625 (let ((bytes (nnimap-get-length)))
626 (delete-region (line-beginning-position)
627 (progn (forward-line 1) (point)))
628 (goto-char (+ (point) bytes))
629 (delete-region (point) (point-max)))
630 t)))
632 (defun nnimap-capability (capability)
633 (member capability (nnimap-capabilities nnimap-object)))
635 (defun nnimap-ver4-p ()
636 (nnimap-capability "IMAP4REV1"))
638 (defun nnimap-get-partial-article (article parts structure)
639 (let ((result
640 (nnimap-command
641 "UID FETCH %d (%s %s)"
642 article
643 (if (nnimap-ver4-p)
644 "BODY.PEEK[HEADER]"
645 "RFC822.HEADER")
646 (if (nnimap-ver4-p)
647 (mapconcat (lambda (part)
648 (format "BODY.PEEK[%s]" part))
649 parts " ")
650 (mapconcat (lambda (part)
651 (format "RFC822.PEEK[%s]" part))
652 parts " ")))))
653 (when result
654 (nnimap-convert-partial-article structure))))
656 (defun nnimap-convert-partial-article (structure)
657 ;; First just skip past the headers.
658 (goto-char (point-min))
659 (let ((bytes (nnimap-get-length))
660 id parts)
661 ;; Delete "FETCH" line.
662 (delete-region (line-beginning-position)
663 (progn (forward-line 1) (point)))
664 (goto-char (+ (point) bytes))
665 ;; Collect all the body parts.
666 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
667 (setq id (match-string 1)
668 bytes (or (nnimap-get-length) 0))
669 (beginning-of-line)
670 (delete-region (point) (progn (forward-line 1) (point)))
671 (push (list id (buffer-substring (point) (+ (point) bytes)))
672 parts)
673 (delete-region (point) (+ (point) bytes)))
674 ;; Delete trailing junk.
675 (delete-region (point) (point-max))
676 ;; Now insert all the parts again where they fit in the structure.
677 (nnimap-insert-partial-structure structure parts)
680 (defun nnimap-insert-partial-structure (structure parts &optional subp)
681 (let (type boundary)
682 (let ((bstruc structure))
683 (while (consp (car bstruc))
684 (pop bstruc))
685 (setq type (car bstruc))
686 (setq bstruc (car (cdr bstruc)))
687 (let ((has-boundary (member "boundary" bstruc)))
688 (when has-boundary
689 (setq boundary (cadr has-boundary)))))
690 (when subp
691 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
692 (downcase type) boundary)))
693 (while (not (stringp (car structure)))
694 (insert "\n--" boundary "\n")
695 (if (consp (caar structure))
696 (nnimap-insert-partial-structure (pop structure) parts t)
697 (let ((bit (pop structure)))
698 (insert (format "Content-type: %s/%s"
699 (downcase (nth 0 bit))
700 (downcase (nth 1 bit))))
701 (if (member-ignore-case "CHARSET" (nth 2 bit))
702 (insert (format
703 "; charset=%S\n"
704 (cadr (member-ignore-case "CHARSET" (nth 2 bit)))))
705 (insert "\n"))
706 (insert (format "Content-transfer-encoding: %s\n"
707 (nth 5 bit)))
708 (insert "\n")
709 (when (assoc (nth 9 bit) parts)
710 (insert (cadr (assoc (nth 9 bit) parts)))))))
711 (insert "\n--" boundary "--\n")))
713 (defun nnimap-find-wanted-parts (structure)
714 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
716 (defun nnimap-find-wanted-parts-1 (structure prefix)
717 (let ((num 1)
718 parts)
719 (while (consp (car structure))
720 (let ((sub (pop structure)))
721 (if (consp (car sub))
722 (push (nnimap-find-wanted-parts-1
723 sub (if (string= prefix "")
724 (number-to-string num)
725 (format "%s.%s" prefix num)))
726 parts)
727 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
728 (id (if (string= prefix "")
729 (number-to-string num)
730 (format "%s.%s" prefix num))))
731 (setcar (nthcdr 9 sub) id)
732 (when (if (eq nnimap-fetch-partial-articles t)
733 (equal id "1")
734 (string-match nnimap-fetch-partial-articles type))
735 (push id parts))))
736 (incf num)))
737 (nreverse parts)))
739 (defun nnimap-decode-gnus-group (group)
740 (decode-coding-string group 'utf-8))
742 (deffoo nnimap-request-group (group &optional server dont-check info)
743 (setq group (nnimap-decode-gnus-group group))
744 (let ((result (nnimap-possibly-change-group
745 ;; Don't SELECT the group if we're going to select it
746 ;; later, anyway.
747 (if (and (not dont-check)
748 (assoc group nnimap-current-infos))
750 group)
751 server))
752 articles active marks high low)
753 (with-current-buffer nntp-server-buffer
754 (when result
755 (if (and dont-check
756 (setq active (nth 2 (assoc group nnimap-current-infos))))
757 (insert (format "211 %d %d %d %S\n"
758 (- (cdr active) (car active))
759 (car active)
760 (cdr active)
761 group))
762 (with-current-buffer (nnimap-buffer)
763 (erase-buffer)
764 (let ((group-sequence
765 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
766 (flag-sequence
767 (nnimap-send-command "UID FETCH 1:* FLAGS")))
768 (setf (nnimap-group nnimap-object) group)
769 (nnimap-wait-for-response flag-sequence)
770 (setq marks
771 (nnimap-flags-to-marks
772 (nnimap-parse-flags
773 (list (list group-sequence flag-sequence
774 1 group "SELECT")))))
775 (when (and info
776 marks)
777 (nnimap-update-infos marks (list info))
778 (nnimap-store-info info (gnus-active (gnus-info-group info))))
779 (goto-char (point-max))
780 (let ((uidnext (nth 5 (car marks))))
781 (setq high (or (if uidnext
782 (1- uidnext)
783 (nth 3 (car marks)))
785 low (or (nth 4 (car marks)) uidnext 1)))))
786 (erase-buffer)
787 (insert
788 (format
789 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
790 t))))
792 (deffoo nnimap-request-create-group (group &optional server args)
793 (setq group (nnimap-decode-gnus-group group))
794 (when (nnimap-possibly-change-group nil server)
795 (with-current-buffer (nnimap-buffer)
796 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
798 (deffoo nnimap-request-delete-group (group &optional force server)
799 (setq group (nnimap-decode-gnus-group group))
800 (when (nnimap-possibly-change-group nil server)
801 (with-current-buffer (nnimap-buffer)
802 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
804 (deffoo nnimap-request-rename-group (group new-name &optional server)
805 (setq group (nnimap-decode-gnus-group group))
806 (when (nnimap-possibly-change-group nil server)
807 (with-current-buffer (nnimap-buffer)
808 (nnimap-unselect-group)
809 (car (nnimap-command "RENAME %S %S"
810 (utf7-encode group t) (utf7-encode new-name t))))))
812 (defun nnimap-unselect-group ()
813 ;; Make sure we don't have this group open read/write by asking
814 ;; to examine a mailbox that doesn't exist. This seems to be
815 ;; the only way that allows us to reliably go back to unselected
816 ;; state on Courier.
817 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
819 (deffoo nnimap-request-expunge-group (group &optional server)
820 (setq group (nnimap-decode-gnus-group group))
821 (when (nnimap-possibly-change-group group server)
822 (with-current-buffer (nnimap-buffer)
823 (car (nnimap-command "EXPUNGE")))))
825 (defun nnimap-get-flags (spec)
826 (let ((articles nil)
827 elems end)
828 (with-current-buffer (nnimap-buffer)
829 (erase-buffer)
830 (nnimap-wait-for-response (nnimap-send-command
831 "UID FETCH %s FLAGS" spec))
832 (setq end (point))
833 (subst-char-in-region (point-min) (point-max)
834 ?\\ ?% t)
835 (goto-char (point-min))
836 (while (search-forward " FETCH " end t)
837 (setq elems (read (current-buffer)))
838 (push (cons (cadr (memq 'UID elems))
839 (cadr (memq 'FLAGS elems)))
840 articles)))
841 (nreverse articles)))
843 (deffoo nnimap-close-group (group &optional server)
846 (deffoo nnimap-request-move-article (article group server accept-form
847 &optional last internal-move-group)
848 (setq group (nnimap-decode-gnus-group group))
849 (with-temp-buffer
850 (mm-disable-multibyte)
851 (when (funcall (if internal-move-group
852 'nnimap-request-head
853 'nnimap-request-article)
854 article group server (current-buffer))
855 ;; If the move is internal (on the same server), just do it the easy
856 ;; way.
857 (let ((message-id (message-field-value "message-id")))
858 (if internal-move-group
859 (let ((result
860 (with-current-buffer (nnimap-buffer)
861 (nnimap-command "UID COPY %d %S"
862 article
863 (utf7-encode internal-move-group t)))))
864 (when (car result)
865 (nnimap-delete-article article)
866 (cons internal-move-group
867 (or (nnimap-find-uid-response "COPYUID" (cadr result))
868 (nnimap-find-article-by-message-id
869 internal-move-group message-id)))))
870 ;; Move the article to a different method.
871 (let ((result (eval accept-form)))
872 (when result
873 (nnimap-possibly-change-group group server)
874 (nnimap-delete-article article)
875 result)))))))
877 (deffoo nnimap-request-expire-articles (articles group &optional server force)
878 (setq group (nnimap-decode-gnus-group group))
879 (cond
880 ((null articles)
881 nil)
882 ((not (nnimap-possibly-change-group group server))
883 articles)
884 ((and force
885 (eq nnmail-expiry-target 'delete))
886 (unless (nnimap-delete-article (gnus-compress-sequence articles))
887 (nnheader-message 7 "Article marked for deletion, but not expunged."))
888 nil)
890 (let ((deletable-articles
891 (if (or force
892 (eq nnmail-expiry-wait 'immediate))
893 articles
894 (gnus-sorted-intersection
895 articles
896 (nnimap-find-expired-articles group)))))
897 (if (null deletable-articles)
898 articles
899 (if (eq nnmail-expiry-target 'delete)
900 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
901 (setq deletable-articles
902 (nnimap-process-expiry-targets
903 deletable-articles group server)))
904 ;; Return the articles we didn't delete.
905 (gnus-sorted-complement articles deletable-articles))))))
907 (defun nnimap-process-expiry-targets (articles group server)
908 (let ((deleted-articles nil))
909 (cond
910 ;; shortcut further processing if we're going to delete the articles
911 ((eq nnmail-expiry-target 'delete)
912 (setq deleted-articles articles)
914 ;; or just move them to another folder on the same IMAP server
915 ((and (not (functionp nnmail-expiry-target))
916 (gnus-server-equal (gnus-group-method nnmail-expiry-target)
917 (gnus-server-to-method
918 (format "nnimap:%s" server))))
919 (and (nnimap-possibly-change-group group server)
920 (with-current-buffer (nnimap-buffer)
921 (nnheader-message 7 "Expiring articles from %s: %s" group articles)
922 (nnimap-command
923 "UID COPY %s %S"
924 (nnimap-article-ranges (gnus-compress-sequence articles))
925 (utf7-encode (gnus-group-real-name nnmail-expiry-target) t))
926 (setq deleted-articles articles)))
929 (dolist (article articles)
930 (let ((target nnmail-expiry-target))
931 (with-temp-buffer
932 (mm-disable-multibyte)
933 (when (nnimap-request-article article group server (current-buffer))
934 (when (functionp target)
935 (setq target (funcall target group)))
936 (if (and target
937 (not (eq target 'delete)))
938 (if (or (gnus-request-group target t)
939 (gnus-request-create-group target))
940 (progn
941 (nnmail-expiry-target-group target group)
942 (nnheader-message 7 "Expiring article %s:%d to %s"
943 group article target))
944 (setq target nil))
945 (nnheader-message 7 "Expiring article %s:%d" group article))
946 (when target
947 (push article deleted-articles))))))))
948 ;; Change back to the current group again.
949 (nnimap-possibly-change-group group server)
950 (setq deleted-articles (nreverse deleted-articles))
951 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
952 deleted-articles))
954 (defun nnimap-find-expired-articles (group)
955 (let ((cutoff (nnmail-expired-article-p group nil nil)))
956 (with-current-buffer (nnimap-buffer)
957 (let ((result
958 (nnimap-command
959 "UID SEARCH SENTBEFORE %s"
960 (format-time-string
961 (format "%%d-%s-%%Y"
962 (upcase
963 (car (rassoc (nth 4 (decode-time cutoff))
964 parse-time-months))))
965 cutoff))))
966 (and (car result)
967 (delete 0 (mapcar #'string-to-number
968 (cdr (assoc "SEARCH" (cdr result))))))))))
971 (defun nnimap-find-article-by-message-id (group message-id)
972 (with-current-buffer (nnimap-buffer)
973 (erase-buffer)
974 (unless (equal group (nnimap-group nnimap-object))
975 (setf (nnimap-group nnimap-object) nil)
976 (setf (nnimap-examined nnimap-object) group)
977 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
978 (let ((sequence
979 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
980 article result)
981 (setq result (nnimap-wait-for-response sequence))
982 (when (and result
983 (car (setq result (nnimap-parse-response))))
984 ;; Select the last instance of the message in the group.
985 (and (setq article
986 (car (last (cdr (assoc "SEARCH" (cdr result))))))
987 (string-to-number article))))))
989 (defun nnimap-delete-article (articles)
990 (with-current-buffer (nnimap-buffer)
991 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
992 (nnimap-article-ranges articles))
993 (cond
994 ((nnimap-capability "UIDPLUS")
995 (nnimap-command "UID EXPUNGE %s"
996 (nnimap-article-ranges articles))
998 (nnimap-expunge
999 (nnimap-command "EXPUNGE")
1001 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
1002 "server doesn't support UIDPLUS, so we won't "
1003 "delete this article now"))))))
1005 (deffoo nnimap-request-scan (&optional group server)
1006 (when group
1007 (setq group (nnimap-decode-gnus-group group)))
1008 (when (and (nnimap-possibly-change-group nil server)
1009 nnimap-inbox
1010 nnimap-split-methods)
1011 (nnheader-message 7 "nnimap %s splitting mail..." server)
1012 (nnimap-split-incoming-mail)
1013 (nnheader-message 7 "nnimap %s splitting mail...done" server)))
1015 (defun nnimap-marks-to-flags (marks)
1016 (let (flags flag)
1017 (dolist (mark marks)
1018 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
1019 (push flag flags)))
1020 flags))
1022 (deffoo nnimap-request-update-group-status (group status &optional server)
1023 (setq group (nnimap-decode-gnus-group group))
1024 (when (nnimap-possibly-change-group nil server)
1025 (let ((command (assoc
1026 status
1027 '((subscribe "SUBSCRIBE")
1028 (unsubscribe "UNSUBSCRIBE")))))
1029 (when command
1030 (with-current-buffer (nnimap-buffer)
1031 (nnimap-command "%s %S" (cadr command) (utf7-encode group t)))))))
1033 (deffoo nnimap-request-set-mark (group actions &optional server)
1034 (setq group (nnimap-decode-gnus-group group))
1035 (when (nnimap-possibly-change-group group server)
1036 (let (sequence)
1037 (with-current-buffer (nnimap-buffer)
1038 (erase-buffer)
1039 ;; Just send all the STORE commands without waiting for
1040 ;; response. If they're successful, they're successful.
1041 (dolist (action actions)
1042 (destructuring-bind (range action marks) action
1043 (let ((flags (nnimap-marks-to-flags marks)))
1044 (when flags
1045 (setq sequence (nnimap-send-command
1046 "UID STORE %s %sFLAGS.SILENT (%s)"
1047 (nnimap-article-ranges range)
1048 (cond
1049 ((eq action 'del) "-")
1050 ((eq action 'add) "+")
1051 ((eq action 'set) ""))
1052 (mapconcat #'identity flags " ")))))))
1053 ;; Wait for the last command to complete to avoid later
1054 ;; synchronization problems with the stream.
1055 (when sequence
1056 (nnimap-wait-for-response sequence))))))
1058 (deffoo nnimap-request-accept-article (group &optional server last)
1059 (setq group (nnimap-decode-gnus-group group))
1060 (when (nnimap-possibly-change-group nil server)
1061 (nnmail-check-syntax)
1062 (let ((message-id (message-field-value "message-id"))
1063 sequence message)
1064 (nnimap-add-cr)
1065 (setq message (buffer-substring-no-properties (point-min) (point-max)))
1066 (with-current-buffer (nnimap-buffer)
1067 (when (setq message (or (nnimap-process-quirk "OK Gimap " 'append message)
1068 message))
1069 ;; If we have this group open read-only, then unselect it
1070 ;; before appending to it.
1071 (when (equal (nnimap-examined nnimap-object) group)
1072 (nnimap-unselect-group))
1073 (erase-buffer)
1074 (setq sequence (nnimap-send-command
1075 "APPEND %S {%d}" (utf7-encode group t)
1076 (length message)))
1077 (unless nnimap-streaming
1078 (nnimap-wait-for-connection "^[+]"))
1079 (process-send-string (get-buffer-process (current-buffer)) message)
1080 (process-send-string (get-buffer-process (current-buffer))
1081 (if (nnimap-newlinep nnimap-object)
1082 "\n"
1083 "\r\n"))
1084 (let ((result (nnimap-get-response sequence)))
1085 (if (not (nnimap-ok-p result))
1086 (progn
1087 (nnheader-report 'nnimap "%s" result)
1088 nil)
1089 (cons group
1090 (or (nnimap-find-uid-response "APPENDUID" (car result))
1091 (nnimap-find-article-by-message-id
1092 group message-id))))))))))
1094 (defun nnimap-process-quirk (greeting-match type data)
1095 (when (and (nnimap-greeting nnimap-object)
1096 (string-match greeting-match (nnimap-greeting nnimap-object))
1097 (eq type 'append)
1098 (string-match "\000" data))
1099 (let ((choice (gnus-multiple-choice
1100 "Message contains NUL characters. Delete, continue, abort? "
1101 '((?d "Delete NUL characters")
1102 (?c "Try to APPEND the message as is")
1103 (?a "Abort")))))
1104 (cond
1105 ((eq choice ?a)
1106 (nnheader-report 'nnimap "Aborted APPEND due to NUL characters"))
1107 ((eq choice ?c)
1108 data)
1110 (with-temp-buffer
1111 (insert data)
1112 (goto-char (point-min))
1113 (while (search-forward "\000" nil t)
1114 (replace-match "" t t))
1115 (buffer-string)))))))
1117 (defun nnimap-ok-p (value)
1118 (and (consp value)
1119 (consp (car value))
1120 (equal (caar value) "OK")))
1122 (defun nnimap-find-uid-response (name list)
1123 (let ((result (car (last (nnimap-find-response-element name list)))))
1124 (and result
1125 (string-to-number result))))
1127 (defun nnimap-find-response-element (name list)
1128 (let (result)
1129 (dolist (elem list)
1130 (when (and (consp elem)
1131 (equal name (car elem)))
1132 (setq result elem)))
1133 result))
1135 (deffoo nnimap-request-replace-article (article group buffer)
1136 (setq group (nnimap-decode-gnus-group group))
1137 (let (group-art)
1138 (when (and (nnimap-possibly-change-group group nil)
1139 ;; Put the article into the group.
1140 (with-current-buffer buffer
1141 (setq group-art
1142 (nnimap-request-accept-article group nil t))))
1143 (nnimap-delete-article (list article))
1144 ;; Return the new article number.
1145 (cdr group-art))))
1147 (defun nnimap-add-cr ()
1148 (goto-char (point-min))
1149 (while (re-search-forward "\r?\n" nil t)
1150 (replace-match "\r\n" t t)))
1152 (defun nnimap-get-groups ()
1153 (erase-buffer)
1154 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1155 groups)
1156 (nnimap-wait-for-response sequence)
1157 (subst-char-in-region (point-min) (point-max)
1158 ?\\ ?% t)
1159 (goto-char (point-min))
1160 (nnimap-unfold-quoted-lines)
1161 (goto-char (point-min))
1162 (while (search-forward "* LIST " nil t)
1163 (let ((flags (read (current-buffer)))
1164 (separator (read (current-buffer)))
1165 (group (read (current-buffer))))
1166 (unless (member '%NoSelect flags)
1167 (push (utf7-decode (if (stringp group)
1168 group
1169 (format "%s" group)) t)
1170 groups))))
1171 (nreverse groups)))
1173 (deffoo nnimap-request-list (&optional server)
1174 (when (nnimap-possibly-change-group nil server)
1175 (with-current-buffer nntp-server-buffer
1176 (erase-buffer)
1177 (let ((groups
1178 (with-current-buffer (nnimap-buffer)
1179 (nnimap-get-groups)))
1180 sequences responses)
1181 (when groups
1182 (with-current-buffer (nnimap-buffer)
1183 (setf (nnimap-group nnimap-object) nil)
1184 (dolist (group groups)
1185 (setf (nnimap-examined nnimap-object) group)
1186 (push (list (nnimap-send-command "EXAMINE %S"
1187 (utf7-encode group t))
1188 group)
1189 sequences))
1190 (nnimap-wait-for-response (caar sequences))
1191 (setq responses
1192 (nnimap-get-responses (mapcar #'car sequences))))
1193 (dolist (response responses)
1194 (let* ((sequence (car response))
1195 (response (cadr response))
1196 (group (cadr (assoc sequence sequences)))
1197 (egroup (encode-coding-string group 'utf-8)))
1198 (when (and group
1199 (equal (caar response) "OK"))
1200 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1201 highest exists)
1202 (dolist (elem response)
1203 (when (equal (cadr elem) "EXISTS")
1204 (setq exists (string-to-number (car elem)))))
1205 (when uidnext
1206 (setq highest (1- (string-to-number (car uidnext)))))
1207 (cond
1208 ((null highest)
1209 (insert (format "%S 0 1 y\n" egroup)))
1210 ((zerop exists)
1211 ;; Empty group.
1212 (insert (format "%S %d %d y\n" egroup
1213 highest (1+ highest))))
1215 ;; Return the widest possible range.
1216 (insert (format "%S %d 1 y\n" egroup
1217 (or highest exists)))))))))
1218 t)))))
1220 (deffoo nnimap-request-newgroups (date &optional server)
1221 (when (nnimap-possibly-change-group nil server)
1222 (with-current-buffer nntp-server-buffer
1223 (erase-buffer)
1224 (dolist (group (with-current-buffer (nnimap-buffer)
1225 (nnimap-get-groups)))
1226 (unless (assoc group nnimap-current-infos)
1227 ;; Insert dummy numbers here -- they don't matter.
1228 (insert (format "%S 0 1 y\n" (encode-coding-string group 'utf-8)))))
1229 t)))
1231 (deffoo nnimap-retrieve-group-data-early (server infos)
1232 (when (and (nnimap-possibly-change-group nil server)
1233 infos)
1234 (with-current-buffer (nnimap-buffer)
1235 (erase-buffer)
1236 (setf (nnimap-group nnimap-object) nil)
1237 (setf (nnimap-initial-resync nnimap-object) 0)
1238 (let ((qresyncp (nnimap-capability "QRESYNC"))
1239 params groups sequences active uidvalidity modseq group)
1240 ;; Go through the infos and gather the data needed to know
1241 ;; what and how to request the data.
1242 (dolist (info infos)
1243 (setq params (gnus-info-params info)
1244 group (nnimap-decode-gnus-group
1245 (gnus-group-real-name (gnus-info-group info)))
1246 active (cdr (assq 'active params))
1247 uidvalidity (cdr (assq 'uidvalidity params))
1248 modseq (cdr (assq 'modseq params)))
1249 (setf (nnimap-examined nnimap-object) group)
1250 (if (and qresyncp
1251 uidvalidity
1252 active
1253 modseq)
1254 (push
1255 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1256 (utf7-encode group t)
1257 (nnimap-quirk "QRESYNC")
1258 uidvalidity modseq)
1259 'qresync
1260 nil group 'qresync)
1261 sequences)
1262 (let ((command
1263 (if uidvalidity
1264 "EXAMINE"
1265 ;; If we don't have a UIDVALIDITY, then this is
1266 ;; the first time we've seen the group, so we
1267 ;; have to do a SELECT (which is slower than an
1268 ;; examine), but will tell us whether the group
1269 ;; is read-only or not.
1270 "SELECT"))
1271 start)
1272 (if (and active uidvalidity)
1273 ;; Fetch the last 100 flags.
1274 (setq start (max 1 (- (cdr active) 100)))
1275 (setf (nnimap-initial-resync nnimap-object)
1276 (1+ (nnimap-initial-resync nnimap-object)))
1277 (setq start 1))
1278 (push (list (nnimap-send-command "%s %S" command
1279 (utf7-encode group t))
1280 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1281 start group command)
1282 sequences))))
1283 sequences))))
1285 (defun nnimap-quirk (command)
1286 (let ((quirk (assoc command nnimap-quirks)))
1287 ;; If this server is of a type that matches a quirk, then return
1288 ;; the "quirked" command instead of the proper one.
1289 (if (or (null quirk)
1290 (not (string-match (nth 1 quirk) (nnimap-greeting nnimap-object))))
1291 command
1292 (nth 2 quirk))))
1294 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1295 (when (and sequences
1296 (nnimap-possibly-change-group nil server t)
1297 ;; Check that the process is still alive.
1298 (get-buffer-process (nnimap-buffer))
1299 (memq (process-status (get-buffer-process (nnimap-buffer)))
1300 '(open run)))
1301 (with-current-buffer (nnimap-buffer)
1302 ;; Wait for the final data to trickle in.
1303 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1304 (caar sequences)
1305 (cadar sequences))
1307 ;; Now we should have most of the data we need, no matter
1308 ;; whether we're QRESYNCING, fetching all the flags from
1309 ;; scratch, or just fetching the last 100 flags per group.
1310 (nnimap-update-infos (nnimap-flags-to-marks
1311 (nnimap-parse-flags
1312 (nreverse sequences)))
1313 infos)
1314 ;; Finally, just return something resembling an active file in
1315 ;; the nntp buffer, so that the agent can save the info, too.
1316 (with-current-buffer nntp-server-buffer
1317 (erase-buffer)
1318 (dolist (info infos)
1319 (let* ((group (gnus-info-group info))
1320 (active (gnus-active group)))
1321 (when active
1322 (insert (format "%S %d %d y\n"
1323 (decode-coding-string
1324 (gnus-group-real-name group) 'utf-8)
1325 (cdr active)
1326 (car active)))))))))))
1328 (defun nnimap-update-infos (flags infos)
1329 (dolist (info infos)
1330 (let* ((group (nnimap-decode-gnus-group
1331 (gnus-group-real-name (gnus-info-group info))))
1332 (marks (cdr (assoc group flags))))
1333 (when marks
1334 (nnimap-update-info info marks)))))
1336 (defun nnimap-update-info (info marks)
1337 (destructuring-bind (existing flags high low uidnext start-article
1338 permanent-flags uidvalidity
1339 vanished highestmodseq) marks
1340 (cond
1341 ;; Ignore groups with no UIDNEXT/marks. This happens for
1342 ;; completely empty groups.
1343 ((and (not existing)
1344 (not uidnext))
1345 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1346 (when active
1347 (gnus-set-active (gnus-info-group info) active))))
1348 ;; We have a mismatch between the old and new UIDVALIDITY
1349 ;; identifiers, so we have to re-request the group info (the next
1350 ;; time). This virtually never happens.
1351 ((let ((old-uidvalidity
1352 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1353 (and old-uidvalidity
1354 (not (equal old-uidvalidity uidvalidity))
1355 (or (not start-article)
1356 (> start-article 1))))
1357 (gnus-group-remove-parameter info 'uidvalidity)
1358 (gnus-group-remove-parameter info 'modseq))
1359 ;; We have the data needed to update.
1361 (let* ((group (gnus-info-group info))
1362 (completep (and start-article
1363 (= start-article 1)))
1364 (active (or (gnus-active group)
1365 (cdr (assq 'active (gnus-info-params info))))))
1366 (when uidnext
1367 (setq high (1- uidnext)))
1368 ;; First set the active ranges based on high/low.
1369 (if (or completep
1370 (not (gnus-active group)))
1371 (gnus-set-active group
1372 (cond
1373 (active
1374 (cons (min (or low (car active))
1375 (car active))
1376 (max (or high (cdr active))
1377 (cdr active))))
1378 ((and low high)
1379 (cons low high))
1380 (uidnext
1381 ;; No articles in this group.
1382 (cons uidnext (1- uidnext)))
1383 (start-article
1384 (cons start-article (1- start-article)))
1386 ;; No articles and no uidnext.
1387 nil)))
1388 (gnus-set-active group
1389 (cons (car active)
1390 (or high (1- uidnext)))))
1391 ;; See whether this is a read-only group.
1392 (unless (eq permanent-flags 'not-scanned)
1393 (gnus-group-set-parameter
1394 info 'permanent-flags
1395 (and (or (memq '%* permanent-flags)
1396 (memq '%Seen permanent-flags))
1397 permanent-flags)))
1398 ;; Update marks and read articles if this isn't a
1399 ;; read-only IMAP group.
1400 (when (setq permanent-flags
1401 (cdr (assq 'permanent-flags (gnus-info-params info))))
1402 (if (and highestmodseq
1403 (not start-article))
1404 ;; We've gotten the data by QRESYNCing.
1405 (nnimap-update-qresync-info
1406 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1407 ;; Do normal non-QRESYNC flag updates.
1408 ;; Update the list of read articles.
1409 (let* ((unread
1410 (gnus-compress-sequence
1411 (gnus-set-difference
1412 (gnus-set-difference
1413 existing
1414 (cdr (assoc '%Seen flags)))
1415 (cdr (assoc '%Flagged flags)))))
1416 (read (gnus-range-difference
1417 (cons start-article high) unread)))
1418 (when (> start-article 1)
1419 (setq read
1420 (gnus-range-nconcat
1421 (if (> start-article 1)
1422 (gnus-sorted-range-intersection
1423 (cons 1 (1- start-article))
1424 (gnus-info-read info))
1425 (gnus-info-read info))
1426 read)))
1427 (when (or (not (listp permanent-flags))
1428 (memq '%Seen permanent-flags))
1429 (gnus-info-set-read info read))
1430 ;; Update the marks.
1431 (setq marks (gnus-info-marks info))
1432 (dolist (type (cdr nnimap-mark-alist))
1433 (when (or (not (listp permanent-flags))
1434 (memq (car (assoc (caddr type) flags))
1435 permanent-flags)
1436 (memq '%* permanent-flags))
1437 (let ((old-marks (assoc (car type) marks))
1438 (new-marks
1439 (gnus-compress-sequence
1440 (cdr (or (assoc (caddr type) flags) ; %Flagged
1441 (assoc (intern (cadr type) obarray) flags)
1442 (assoc (cadr type) flags)))))) ; "\Flagged"
1443 (setq marks (delq old-marks marks))
1444 (pop old-marks)
1445 (when (and old-marks
1446 (> start-article 1))
1447 (setq old-marks (gnus-range-difference
1448 old-marks
1449 (cons start-article high)))
1450 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1451 (when new-marks
1452 (push (cons (car type) new-marks) marks)))))
1453 (gnus-info-set-marks info marks t))))
1454 ;; Tell Gnus whether there are any \Recent messages in any of
1455 ;; the groups.
1456 (let ((recent (cdr (assoc '%Recent flags))))
1457 (when (and active
1458 recent
1459 (> (car (last recent)) (cdr active)))
1460 (push (list (cons (gnus-group-real-name group) 0))
1461 nnmail-split-history)))
1462 ;; Note the active level for the next run-through.
1463 (gnus-group-set-parameter info 'active (gnus-active group))
1464 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1465 (gnus-group-set-parameter info 'modseq highestmodseq)
1466 (nnimap-store-info info (gnus-active group)))))))
1468 (defun nnimap-update-qresync-info (info existing vanished flags)
1469 ;; Add all the vanished articles to the list of read articles.
1470 (gnus-info-set-read
1471 info
1472 (gnus-add-to-range
1473 (gnus-add-to-range
1474 (gnus-range-add (gnus-info-read info)
1475 vanished)
1476 (cdr (assq '%Flagged flags)))
1477 (cdr (assq '%Seen flags))))
1478 (let ((marks (gnus-info-marks info)))
1479 (dolist (type (cdr nnimap-mark-alist))
1480 (let ((ticks (assoc (car type) marks))
1481 (new-marks
1482 (cdr (or (assoc (caddr type) flags) ; %Flagged
1483 (assoc (intern (cadr type) obarray) flags)
1484 (assoc (cadr type) flags))))) ; "\Flagged"
1485 (setq marks (delq ticks marks))
1486 (pop ticks)
1487 ;; Add the new marks we got.
1488 (setq ticks (gnus-add-to-range ticks new-marks))
1489 ;; Remove the marks from messages that don't have them.
1490 (setq ticks (gnus-remove-from-range
1491 ticks
1492 (gnus-compress-sequence
1493 (gnus-sorted-complement existing new-marks))))
1494 (when ticks
1495 (push (cons (car type) ticks) marks)))
1496 (gnus-info-set-marks info marks t))))
1498 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1499 (if (zerop (length irange))
1501 (let ((result nil))
1502 (dolist (elem (split-string irange ","))
1503 (push
1504 (if (string-match ":" elem)
1505 (let ((numbers (split-string elem ":")))
1506 (cons (string-to-number (car numbers))
1507 (string-to-number (cadr numbers))))
1508 (string-to-number elem))
1509 result))
1510 (nreverse result))))
1512 (defun nnimap-store-info (info active)
1513 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1514 (entry (assoc group nnimap-current-infos)))
1515 (if entry
1516 (setcdr entry (list info active))
1517 (push (list group info active) nnimap-current-infos))))
1519 (defun nnimap-flags-to-marks (groups)
1520 (let (data group totalp uidnext articles start-article mark permanent-flags
1521 uidvalidity vanished highestmodseq)
1522 (dolist (elem groups)
1523 (setq group (car elem)
1524 uidnext (nth 1 elem)
1525 start-article (nth 2 elem)
1526 permanent-flags (nth 3 elem)
1527 uidvalidity (nth 4 elem)
1528 vanished (nth 5 elem)
1529 highestmodseq (nth 6 elem)
1530 articles (nthcdr 7 elem))
1531 (let ((high (caar articles))
1532 marks low existing)
1533 (dolist (article articles)
1534 (setq low (car article))
1535 (push (car article) existing)
1536 (dolist (flag (cdr article))
1537 (setq mark (assoc flag marks))
1538 (if (not mark)
1539 (push (list flag (car article)) marks)
1540 (setcdr mark (cons (car article) (cdr mark))))))
1541 (push (list group existing marks high low uidnext start-article
1542 permanent-flags uidvalidity vanished highestmodseq)
1543 data)))
1544 data))
1546 (defun nnimap-parse-flags (sequences)
1547 (goto-char (point-min))
1548 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1549 ;; read it.
1550 (subst-char-in-region (point-min) (point-max)
1551 ?\\ ?% t)
1552 ;; Remove any MODSEQ entries in the buffer, because they may contain
1553 ;; numbers that are too large for 32-bit Emacsen.
1554 (while (re-search-forward " MODSEQ ([0-9]+)" nil t)
1555 (replace-match "" t t))
1556 (goto-char (point-min))
1557 (let (start end articles groups uidnext elems permanent-flags
1558 uidvalidity vanished highestmodseq)
1559 (dolist (elem sequences)
1560 (destructuring-bind (group-sequence flag-sequence totalp group command)
1561 elem
1562 (setq start (point))
1563 (when (and
1564 ;; The EXAMINE was successful.
1565 (search-forward (format "\n%d OK " group-sequence) nil t)
1566 (progn
1567 (forward-line 1)
1568 (setq end (point))
1569 (goto-char start)
1570 (setq permanent-flags
1571 (if (equal command "SELECT")
1572 (and (search-forward "PERMANENTFLAGS "
1573 (or end (point-min)) t)
1574 (read (current-buffer)))
1575 'not-scanned))
1576 (goto-char start)
1577 (setq uidnext
1578 (and (search-forward "UIDNEXT "
1579 (or end (point-min)) t)
1580 (read (current-buffer))))
1581 (goto-char start)
1582 (setq uidvalidity
1583 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1584 (or end (point-min)) t)
1585 ;; Store UIDVALIDITY as a string, as it's
1586 ;; too big for 32-bit Emacsen, usually.
1587 (match-string 1)))
1588 (goto-char start)
1589 (setq vanished
1590 (and (eq flag-sequence 'qresync)
1591 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1592 (or end (point-min)) t)
1593 (match-string 1)))
1594 (goto-char start)
1595 (setq highestmodseq
1596 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1597 (or end (point-min)) t)
1598 (match-string 1)))
1599 (goto-char end)
1600 (forward-line -1))
1601 ;; The UID FETCH FLAGS was successful.
1602 (or (eq flag-sequence 'qresync)
1603 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1604 (if (eq flag-sequence 'qresync)
1605 (progn
1606 (goto-char start)
1607 (setq start end))
1608 (setq start (point))
1609 (goto-char end))
1610 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1611 (let ((p (point)))
1612 (setq elems (read (current-buffer)))
1613 (push (cons (cadr (memq 'UID elems))
1614 (cadr (memq 'FLAGS elems)))
1615 articles)))
1616 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1617 vanished highestmodseq)
1618 articles)
1619 groups)
1620 (if (eq flag-sequence 'qresync)
1621 (goto-char end)
1622 (setq end (point)))
1623 (setq articles nil))))
1624 groups))
1626 (defun nnimap-find-process-buffer (buffer)
1627 (cadr (assoc buffer nnimap-connection-alist)))
1629 (deffoo nnimap-request-post (&optional server)
1630 (setq nnimap-status-string "Read-only server")
1631 nil)
1633 (declare-function gnus-fetch-headers "gnus-sum"
1634 (articles &optional limit force-new dependencies))
1636 (autoload 'nnir-search-thread "nnir")
1638 (deffoo nnimap-request-thread (header &optional group server)
1639 (when group
1640 (setq group (nnimap-decode-gnus-group group)))
1641 (if gnus-refer-thread-use-nnir
1642 (nnir-search-thread header)
1643 (when (nnimap-possibly-change-group group server)
1644 (let* ((cmd (nnimap-make-thread-query header))
1645 (result (with-current-buffer (nnimap-buffer)
1646 (nnimap-command "UID SEARCH %s" cmd))))
1647 (when result
1648 (gnus-fetch-headers
1649 (and (car result)
1650 (delete 0 (mapcar #'string-to-number
1651 (cdr (assoc "SEARCH" (cdr result))))))
1652 nil t))))))
1654 (defun nnimap-possibly-change-group (group server &optional no-reconnect)
1655 (let ((open-result t))
1656 (when (and server
1657 (not (nnimap-server-opened server)))
1658 (setq open-result (nnimap-open-server server nil no-reconnect)))
1659 (cond
1660 ((not open-result)
1661 nil)
1662 ((not group)
1665 (with-current-buffer (nnimap-buffer)
1666 (if (equal group (nnimap-group nnimap-object))
1668 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1669 (when (car result)
1670 (setf (nnimap-group nnimap-object) group
1671 (nnimap-select-result nnimap-object) result)
1672 result))))))))
1674 (defun nnimap-find-connection (buffer)
1675 "Find the connection delivering to BUFFER."
1676 (let ((entry (assoc buffer nnimap-connection-alist)))
1677 (when entry
1678 (if (and (buffer-name (cadr entry))
1679 (get-buffer-process (cadr entry))
1680 (memq (process-status (get-buffer-process (cadr entry)))
1681 '(open run)))
1682 (get-buffer-process (cadr entry))
1683 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1684 nil))))
1686 (defvar nnimap-sequence 0)
1688 (defun nnimap-send-command (&rest args)
1689 (setf (nnimap-last-command-time nnimap-object) (current-time))
1690 (process-send-string
1691 (get-buffer-process (current-buffer))
1692 (nnimap-log-command
1693 (format "%d %s%s\n"
1694 (incf nnimap-sequence)
1695 (apply #'format args)
1696 (if (nnimap-newlinep nnimap-object)
1698 "\r"))))
1699 ;; Some servers apparently can't have many outstanding
1700 ;; commands, so throttle them.
1701 (unless nnimap-streaming
1702 (nnimap-wait-for-response nnimap-sequence))
1703 nnimap-sequence)
1705 (defvar nnimap-record-commands nil
1706 "If non-nil, log commands to the \"*imap log*\" buffer.")
1708 (defun nnimap-log-command (command)
1709 (when nnimap-record-commands
1710 (with-current-buffer (get-buffer-create "*imap log*")
1711 (goto-char (point-max))
1712 (insert (format-time-string "%H:%M:%S")
1713 " [" nnimap-address "] "
1714 (if nnimap-inhibit-logging
1715 "(inhibited)\n"
1716 command))))
1717 command)
1719 (defun nnimap-command (&rest args)
1720 (erase-buffer)
1721 (let* ((sequence (apply #'nnimap-send-command args))
1722 (response (nnimap-get-response sequence)))
1723 (if (equal (caar response) "OK")
1724 (cons t response)
1725 (nnheader-report 'nnimap "%s"
1726 (mapconcat (lambda (a)
1727 (format "%s" a))
1728 (car response) " "))
1729 nil)))
1731 (defun nnimap-get-response (sequence)
1732 (nnimap-wait-for-response sequence)
1733 (nnimap-parse-response))
1735 (defun nnimap-wait-for-connection (&optional regexp)
1736 (nnimap-wait-for-line (or regexp "^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1738 (defun nnimap-wait-for-line (regexp &optional response-regexp)
1739 (let ((process (get-buffer-process (current-buffer))))
1740 (goto-char (point-min))
1741 (while (and (memq (process-status process)
1742 '(open run))
1743 (not (re-search-forward regexp nil t)))
1744 (nnheader-accept-process-output process)
1745 (goto-char (point-min)))
1746 (forward-line -1)
1747 (and (looking-at (or response-regexp regexp))
1748 (match-string 1))))
1750 (defun nnimap-wait-for-response (sequence &optional messagep)
1751 (let ((process (get-buffer-process (current-buffer)))
1752 openp)
1753 (condition-case nil
1754 (progn
1755 (goto-char (point-max))
1756 (while (and (setq openp (memq (process-status process)
1757 '(open run)))
1758 (progn
1759 ;; Skip past any "*" lines that the server has
1760 ;; output.
1761 (while (and (not (bobp))
1762 (progn
1763 (forward-line -1)
1764 (looking-at "\\*"))))
1765 (not (looking-at (format "%d .*\n" sequence)))))
1766 (when messagep
1767 (nnheader-message-maybe
1768 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
1769 nnimap-address
1770 (if (not (zerop (nnimap-initial-resync nnimap-object)))
1771 (format " (initial sync of %d group%s; please wait)"
1772 (nnimap-initial-resync nnimap-object)
1773 (if (= (nnimap-initial-resync nnimap-object) 1)
1775 "s"))
1776 "")))
1777 (nnheader-accept-process-output process)
1778 (goto-char (point-max)))
1779 (setf (nnimap-initial-resync nnimap-object) 0)
1780 openp)
1781 (quit
1782 (when debug-on-quit
1783 (debug "Quit"))
1784 ;; The user hit C-g while we were waiting: kill the process, in case
1785 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1786 ;; NAT routers).
1787 (delete-process process)
1788 nil))))
1790 (defun nnimap-parse-response ()
1791 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1792 result)
1793 (dolist (line lines)
1794 (push (cdr (nnimap-parse-line line)) result))
1795 ;; Return the OK/error code first, and then all the "continuation
1796 ;; lines" afterwards.
1797 (cons (pop result)
1798 (nreverse result))))
1800 ;; Parse an IMAP response line lightly. They look like
1801 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1802 ;; the lines into a list of strings and lists of string.
1803 (defun nnimap-parse-line (line)
1804 (let (char result)
1805 (with-temp-buffer
1806 (mm-disable-multibyte)
1807 (insert line)
1808 (goto-char (point-min))
1809 (while (not (eobp))
1810 (if (eql (setq char (following-char)) ? )
1811 (forward-char 1)
1812 (push
1813 (cond
1814 ((eql char ?\[)
1815 (split-string
1816 (buffer-substring
1817 (1+ (point))
1818 (if (search-forward "]" (line-end-position) 'move)
1819 (1- (point))
1820 (point)))))
1821 ((eql char ?\()
1822 (split-string
1823 (buffer-substring
1824 (1+ (point))
1825 (if (search-forward ")" (line-end-position) 'move)
1826 (1- (point))
1827 (point)))))
1828 ((eql char ?\")
1829 (forward-char 1)
1830 (buffer-substring
1831 (point)
1832 (1- (or (search-forward "\"" (line-end-position) 'move)
1833 (point)))))
1835 (buffer-substring (point) (if (search-forward " " nil t)
1836 (1- (point))
1837 (goto-char (point-max))))))
1838 result)))
1839 (nreverse result))))
1841 (defun nnimap-last-response-string ()
1842 (save-excursion
1843 (forward-line 1)
1844 (let ((end (point)))
1845 (forward-line -1)
1846 (when (not (bobp))
1847 (forward-line -1)
1848 (while (and (not (bobp))
1849 (eql (following-char) ?*))
1850 (forward-line -1))
1851 (unless (eql (following-char) ?*)
1852 (forward-line 1)))
1853 (buffer-substring (point) end))))
1855 (defun nnimap-get-responses (sequences)
1856 (let (responses)
1857 (dolist (sequence sequences)
1858 (goto-char (point-min))
1859 (when (re-search-forward (format "^%d " sequence) nil t)
1860 (push (list sequence (nnimap-parse-response))
1861 responses)))
1862 responses))
1864 (defvar nnimap-incoming-split-list nil)
1866 (defun nnimap-fetch-inbox (articles)
1867 (erase-buffer)
1868 (nnimap-wait-for-response
1869 (nnimap-send-command
1870 "UID FETCH %s %s"
1871 (nnimap-article-ranges articles)
1872 (format "(UID %s%s)"
1873 (format
1874 (if (nnimap-ver4-p)
1875 "BODY.PEEK"
1876 "RFC822.PEEK"))
1877 (cond
1878 (nnimap-split-download-body-default
1879 "[]")
1880 ((nnimap-ver4-p)
1881 "[HEADER]")
1883 "[1]"))))
1886 (defun nnimap-split-incoming-mail ()
1887 (with-current-buffer (nnimap-buffer)
1888 (let ((nnimap-incoming-split-list nil)
1889 (nnmail-split-methods
1890 (cond
1891 ((eq nnimap-split-methods 'default)
1892 nnmail-split-methods)
1893 (nnimap-split-methods
1894 nnimap-split-methods)
1895 (nnimap-split-fancy
1896 'nnmail-split-fancy)))
1897 (nnmail-split-fancy (or nnimap-split-fancy
1898 nnmail-split-fancy))
1899 (nnmail-inhibit-default-split-group t)
1900 (groups (nnimap-get-groups))
1901 new-articles)
1902 (erase-buffer)
1903 (nnimap-command "SELECT %S" nnimap-inbox)
1904 (setf (nnimap-group nnimap-object) nnimap-inbox)
1905 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1906 (when new-articles
1907 (nnimap-fetch-inbox new-articles)
1908 (nnimap-transform-split-mail)
1909 (nnheader-ms-strip-cr)
1910 (nnmail-cache-open)
1911 (nnmail-split-incoming (current-buffer)
1912 #'nnimap-save-mail-spec
1913 nil nil
1914 #'nnimap-dummy-active-number
1915 #'nnimap-save-mail-spec)
1916 (when nnimap-incoming-split-list
1917 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1918 sequences junk-articles)
1919 ;; Create any groups that doesn't already exist on the
1920 ;; server first.
1921 (dolist (spec specs)
1922 (when (and (not (member (car spec) groups))
1923 (not (eq (car spec) 'junk)))
1924 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1925 ;; Then copy over all the messages.
1926 (erase-buffer)
1927 (dolist (spec specs)
1928 (let ((group (car spec))
1929 (ranges (cdr spec)))
1930 (if (eq group 'junk)
1931 (setq junk-articles ranges)
1932 (push (list (nnimap-send-command
1933 "UID COPY %s %S"
1934 (nnimap-article-ranges ranges)
1935 (utf7-encode group t))
1936 ranges)
1937 sequences))))
1938 ;; Wait for the last COPY response...
1939 (when sequences
1940 (nnimap-wait-for-response (caar sequences))
1941 ;; And then mark the successful copy actions as deleted,
1942 ;; and possibly expunge them.
1943 (nnimap-mark-and-expunge-incoming
1944 (nnimap-parse-copied-articles sequences)))
1945 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1947 (defun nnimap-mark-and-expunge-incoming (range)
1948 (when range
1949 (setq range (nnimap-article-ranges range))
1950 (erase-buffer)
1951 (let ((sequence
1952 (nnimap-send-command
1953 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1954 (cond
1955 ;; If the server supports it, we now delete the message we have
1956 ;; just copied over.
1957 ((nnimap-capability "UIDPLUS")
1958 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1959 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1960 ;; user has configured it.
1961 (nnimap-expunge
1962 (setq sequence (nnimap-send-command "EXPUNGE"))))
1963 (nnimap-wait-for-response sequence))))
1965 (defun nnimap-parse-copied-articles (sequences)
1966 (let (sequence copied range)
1967 (goto-char (point-min))
1968 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t)
1969 (setq sequence (string-to-number (match-string 1)))
1970 (when (setq range (cadr (assq sequence sequences)))
1971 (push (gnus-uncompress-range range) copied)))
1972 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1974 (defun nnimap-new-articles (flags)
1975 (let (new)
1976 (dolist (elem flags)
1977 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1978 (cdr elem))
1979 (push (car elem) new)))
1980 (gnus-compress-sequence (nreverse new))))
1982 (defun nnimap-make-split-specs (list)
1983 (let ((specs nil)
1984 entry)
1985 (dolist (elem list)
1986 (destructuring-bind (article spec) elem
1987 (dolist (group (delete nil (mapcar #'car spec)))
1988 (unless (setq entry (assoc group specs))
1989 (push (setq entry (list group)) specs))
1990 (setcdr entry (cons article (cdr entry))))))
1991 (dolist (entry specs)
1992 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1993 specs))
1995 (defun nnimap-transform-split-mail ()
1996 (goto-char (point-min))
1997 (let (article bytes)
1998 (block nil
1999 (while (not (eobp))
2000 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2001 (delete-region (point) (progn (forward-line 1) (point)))
2002 (when (eobp)
2003 (return)))
2004 (setq article (match-string 1)
2005 bytes (nnimap-get-length))
2006 (delete-region (line-beginning-position) (line-end-position))
2007 ;; Insert MMDF separator, and a way to remember what this
2008 ;; article UID is.
2009 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
2010 (forward-char (1+ bytes))
2011 (setq bytes (nnimap-get-length))
2012 (delete-region (line-beginning-position) (line-end-position))
2013 ;; There's a body; skip past that.
2014 (when bytes
2015 (forward-char (1+ bytes))
2016 (delete-region (line-beginning-position) (line-end-position)))))))
2018 (defun nnimap-dummy-active-number (group &optional server)
2021 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
2022 (let (article)
2023 (goto-char (point-min))
2024 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
2025 (error "Invalid nnimap mail")
2026 (setq article (string-to-number (match-string 1))))
2027 (push (list article
2028 (if (eq group-art 'junk)
2029 (list (cons 'junk 1))
2030 group-art))
2031 nnimap-incoming-split-list)))
2033 (defun nnimap-make-thread-query (header)
2034 (let* ((id (mail-header-id header))
2035 (refs (split-string
2036 (or (mail-header-references header)
2037 "")))
2038 (value
2039 (format
2040 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2041 id id)))
2042 (dolist (refid refs value)
2043 (setq value (format
2044 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2045 refid refid value)))))
2048 (provide 'nnimap)
2050 ;;; nnimap.el ends here