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