1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; nnimap interfaces Gnus with IMAP servers.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
48 (require 'proto-stream
)
53 (autoload 'auth-source-forget-user-or-password
"auth-source")
54 (autoload 'auth-source-user-or-password
"auth-source")
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-server-port nil
63 If nnimap-stream is `ssl', this will default to `imaps'. If not,
64 it will default to `imap'.")
66 (defvoo nnimap-stream
'undecided
67 "How nnimap will talk to the IMAP server.
68 Values are `ssl', `network', `starttls' or `shell'.
69 The default is to try `ssl' first, and then `network'.")
71 (defvoo nnimap-shell-program
(if (boundp 'imap-shell-program
)
72 (if (listp imap-shell-program
)
73 (car imap-shell-program
)
77 (defvoo nnimap-inbox nil
78 "The mail box where incoming mail arrives and should be split out of.")
80 (defvoo nnimap-split-methods nil
82 Uses the same syntax as nnmail-split-methods")
84 (defvoo nnimap-split-fancy nil
85 "Uses the same syntax as nnmail-split-fancy.")
87 (defvoo nnimap-unsplittable-articles
'(%Deleted %Seen
)
88 "Articles with the flags in the list will not be considered when splitting.")
90 (make-obsolete-variable 'nnimap-split-rule
"see `nnimap-split-methods'"
93 (defvoo nnimap-authenticator nil
94 "How nnimap authenticate itself to the server.
95 Possible choices are nil (use default methods) or `anonymous'.")
97 (defvoo nnimap-expunge t
98 "If non-nil, expunge articles after deleting them.
99 This is always done if the server supports UID EXPUNGE, but it's
100 not done by default on servers that doesn't support that command.")
102 (defvoo nnimap-streaming t
103 "If non-nil, try to use streaming commands with IMAP servers.
104 Switching this off will make nnimap slower, but it helps with
107 (defvoo nnimap-connection-alist nil
)
109 (defvoo nnimap-current-infos nil
)
111 (defvoo nnimap-fetch-partial-articles nil
112 "If non-nil, Gnus will fetch partial articles.
113 If t, nnimap will fetch only the first part. If a string, it
114 will fetch all parts that have types that match that string. A
115 likely value would be \"text/\" to automatically fetch all
118 (defvar nnimap-process nil
)
120 (defvar nnimap-status-string
"")
122 (defvar nnimap-split-download-body-default nil
123 "Internal variable with default value for `nnimap-split-download-body'.")
125 (defvar nnimap-keepalive-timer nil
)
126 (defvar nnimap-process-buffers nil
)
129 group process commands capabilities select-result newlinep server
130 last-command-time greeting examined
)
132 (defvar nnimap-object nil
)
134 (defvar nnimap-mark-alist
135 '((read "\\Seen" %Seen
)
136 (tick "\\Flagged" %Flagged
)
137 (reply "\\Answered" %Answered
)
138 (expire "gnus-expire")
139 (dormant "gnus-dormant")
142 (download "gnus-download")
143 (forward "gnus-forward")))
145 (defun nnimap-buffer ()
146 (nnimap-find-process-buffer nntp-server-buffer
))
148 (defun nnimap-header-parameters ()
149 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
152 "BODY.PEEK[HEADER.FIELDS %s]"
153 "RFC822.HEADER.LINES %s")
154 (append '(Subject From Date Message-Id
155 References In-Reply-To Xref
)
156 nnmail-extra-headers
))))
158 (deffoo nnimap-retrieve-headers
(articles &optional group server fetch-old
)
159 (with-current-buffer nntp-server-buffer
161 (when (nnimap-possibly-change-group group server
)
162 (with-current-buffer (nnimap-buffer)
164 (nnimap-wait-for-response
167 (nnimap-article-ranges (gnus-compress-sequence articles
))
168 (nnimap-header-parameters))
170 (nnimap-transform-headers))
171 (insert-buffer-substring
172 (nnimap-find-process-buffer (current-buffer))))
175 (defun nnimap-transform-headers ()
176 (goto-char (point-min))
177 (let (article bytes lines size string
)
180 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
181 (delete-region (point) (progn (forward-line 1) (point)))
184 (setq article
(match-string 1))
185 ;; Unfold quoted {number} strings.
186 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
187 (1+ (line-end-position)) t
)
188 (setq size
(string-to-number (match-string 1)))
189 (delete-region (+ (match-beginning 0) 2) (point))
190 (setq string
(buffer-substring (point) (+ (point) size
)))
191 (delete-region (point) (+ (point) size
))
192 (insert (format "%S" string
)))
193 (setq bytes
(nnimap-get-length)
197 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
202 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
203 (let ((structure (ignore-errors
204 (read (current-buffer)))))
205 (while (and (consp structure
)
206 (not (stringp (car structure
))))
207 (setq structure
(car structure
)))
208 (setq lines
(nth 7 structure
))))
209 (delete-region (line-beginning-position) (line-end-position))
210 (insert (format "211 %s Article retrieved." article
))
213 (insert (format "Chars: %s\n" size
)))
215 (insert (format "Lines: %s\n" lines
)))
216 (unless (re-search-forward "^\r$" nil t
)
217 (goto-char (point-max)))
218 (delete-region (line-beginning-position) (line-end-position))
222 (defun nnimap-unfold-quoted-lines ()
223 ;; Unfold quoted {number} strings.
225 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
226 (setq size
(string-to-number (match-string 1)))
227 (delete-region (1+ (match-beginning 0)) (point))
228 (setq string
(buffer-substring (point) (+ (point) size
)))
229 (delete-region (point) (+ (point) size
))
230 (insert (format "%S" string
)))))
232 (defun nnimap-get-length ()
233 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
234 (string-to-number (match-string 1))))
236 (defun nnimap-article-ranges (ranges)
240 (number-to-string ranges
))
241 ((numberp (cdr ranges
))
242 (format "%d:%d" (car ranges
) (cdr ranges
)))
244 (dolist (elem ranges
)
247 (format "%d:%d" (car elem
) (cdr elem
))
248 (number-to-string elem
))
250 (mapconcat #'identity
(nreverse result
) ",")))))
252 (deffoo nnimap-open-server
(server &optional defs
)
253 (if (nnimap-server-opened server
)
255 (unless (assq 'nnimap-address defs
)
256 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
257 (nnoo-change-server 'nnimap server defs
)
258 (or (nnimap-find-connection nntp-server-buffer
)
259 (nnimap-open-connection nntp-server-buffer
))))
261 (defun nnimap-make-process-buffer (buffer)
263 (generate-new-buffer (format "*nnimap %s %s %s*"
264 nnimap-address nnimap-server-port
265 (gnus-buffer-exists-p buffer
)))
266 (mm-disable-multibyte)
267 (buffer-disable-undo)
269 (set (make-local-variable 'after-change-functions
) nil
)
270 (set (make-local-variable 'nnimap-object
)
271 (make-nnimap :server
(nnoo-current-server 'nnimap
)))
272 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
273 (push (current-buffer) nnimap-process-buffers
)
276 (defun nnimap-credentials (address ports
&optional inhibit-create
)
277 (let (port credentials
)
278 ;; Request the credentials from all ports, but only query on the
279 ;; last port if all the previous ones have failed.
280 (while (and (null credentials
)
281 (setq port
(pop ports
)))
283 (auth-source-user-or-password
284 '("login" "password") address port nil
290 (defun nnimap-keepalive ()
291 (let ((now (current-time)))
292 (dolist (buffer nnimap-process-buffers
)
293 (when (buffer-name buffer
)
294 (with-current-buffer buffer
295 (when (and nnimap-object
296 (nnimap-last-command-time nnimap-object
)
300 (nnimap-last-command-time nnimap-object
)))
301 ;; More than five minutes since the last command.
303 (nnimap-send-command "NOOP")))))))
305 (defun nnimap-open-connection (buffer)
306 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
307 ;; `ssl' when nnimap-server-port was nil. Sort of.
308 (when (and nnimap-server-port
309 (eq nnimap-stream
'undecided
))
310 (setq nnimap-stream
'ssl
))
312 (if (eq nnimap-stream
'undecided
)
313 (loop for type in
'(ssl network
)
314 for stream
= (let ((nnimap-stream type
))
315 (nnimap-open-connection-1 buffer
))
316 while
(eq stream
'no-connect
)
317 finally
(return stream
))
318 (nnimap-open-connection-1 buffer
))))
319 (if (eq stream
'no-connect
)
323 (defun nnimap-open-connection-1 (buffer)
324 (unless nnimap-keepalive-timer
325 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
327 (with-current-buffer (nnimap-make-process-buffer buffer
)
328 (let* ((coding-system-for-read 'binary
)
329 (coding-system-for-write 'binary
)
333 ((or (eq nnimap-stream
'network
)
334 (eq nnimap-stream
'starttls
))
335 (nnheader-message 7 "Opening connection to %s..."
338 ((eq nnimap-stream
'shell
)
339 (nnheader-message 7 "Opening connection to %s via shell..."
342 ((memq nnimap-stream
'(ssl tls
))
343 (nnheader-message 7 "Opening connection to %s via tls..."
345 '("143" "993" "imap" "imaps"))
347 (error "Unknown stream type: %s" nnimap-stream
))))
348 (proto-stream-always-use-starttls t
)
349 login-result credentials
)
350 (when nnimap-server-port
351 (setq ports
(append ports
(list nnimap-server-port
))))
352 (destructuring-bind (stream greeting capabilities
)
353 (open-protocol-stream
354 "*nnimap*" (current-buffer) nnimap-address
(car (last ports
))
356 :shell-command nnimap-shell-program
357 :capability-command
"1 CAPABILITY\r\n"
360 (lambda (capabilities)
361 (when (gnus-string-match-p "STARTTLS" capabilities
)
363 (setf (nnimap-process nnimap-object
) stream
)
366 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
367 nnimap-address port nnimap-stream
)
369 (gnus-set-process-query-on-exit-flag stream nil
)
370 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
371 (nnheader-report 'nnimap
"%s" greeting
)
372 ;; Store the greeting (for debugging purposes).
373 (setf (nnimap-greeting nnimap-object
) greeting
)
374 (setf (nnimap-capabilities nnimap-object
)
376 (split-string capabilities
)))
377 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
378 (if (not (setq credentials
379 (if (eq nnimap-authenticator
'anonymous
)
381 (message-make-address))
383 ;; First look for the credentials based
384 ;; on the virtual server name.
386 (nnoo-current-server 'nnimap
) ports t
)
387 ;; Then look them up based on the
389 (nnimap-credentials nnimap-address ports
)))))
390 (setq nnimap-object nil
)
392 (if (and (nnimap-capability "AUTH=PLAIN")
393 (nnimap-capability "LOGINDISABLED"))
395 "AUTHENTICATE PLAIN %s"
396 (base64-encode-string
397 (format "\000%s\000%s"
398 (nnimap-quote-specials (car credentials
))
399 (nnimap-quote-specials (cadr credentials
)))))
400 (nnimap-command "LOGIN %S %S"
402 (cadr credentials
))))
403 (unless (car login-result
)
404 ;; If the login failed, then forget the credentials
405 ;; that are now possibly cached.
406 (dolist (host (list (nnoo-current-server 'nnimap
)
409 (dolist (element '("login" "password"))
410 (auth-source-forget-user-or-password
411 element host port
))))
412 (delete-process (nnimap-process nnimap-object
))
413 (setq nnimap-object nil
))))
415 (when (nnimap-capability "QRESYNC")
416 (nnimap-command "ENABLE QRESYNC"))
417 (nnimap-process nnimap-object
))))))))
419 (defun nnimap-quote-specials (string)
422 (goto-char (point-min))
423 (while (re-search-forward "[\\\"]" nil t
)
429 (defun nnimap-find-parameter (parameter elems
)
433 ((equal (car elem
) parameter
)
434 (setq result
(cdr elem
)))
435 ((and (equal (car elem
) "OK")
437 (equal (caadr elem
) parameter
))
438 (setq result
(cdr (cadr elem
))))))
441 (deffoo nnimap-close-server
(&optional server
)
442 (when (nnoo-change-server 'nnimap server nil
)
444 (delete-process (get-buffer-process (nnimap-buffer))))
445 (nnoo-close-server 'nnimap server
)
448 (deffoo nnimap-request-close
()
451 (deffoo nnimap-server-opened
(&optional server
)
452 (and (nnoo-current-server-p 'nnimap server
)
454 (gnus-buffer-live-p nntp-server-buffer
)
455 (nnimap-find-connection nntp-server-buffer
)))
457 (deffoo nnimap-status-message
(&optional server
)
458 nnimap-status-string
)
460 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
461 (with-current-buffer nntp-server-buffer
462 (let ((result (nnimap-possibly-change-group group server
))
464 (when (stringp article
)
465 (setq article
(nnimap-find-article-by-message-id group article
)))
469 (with-current-buffer (nnimap-buffer)
471 (when nnimap-fetch-partial-articles
472 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
473 (goto-char (point-min))
474 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
475 (setq structure
(ignore-errors
476 (let ((start (point)))
478 (downcase-region start
(point))
480 (read (current-buffer))))
481 parts
(nnimap-find-wanted-parts structure
))))
483 (nnimap-get-partial-article article parts structure
)
484 (nnimap-get-whole-article article
))
485 (let ((buffer (current-buffer)))
486 (with-current-buffer (or to-buffer nntp-server-buffer
)
488 (insert-buffer-substring buffer
)
489 (nnheader-ms-strip-cr)
490 (cons group article
)))))))))
492 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
493 (when (nnimap-possibly-change-group group server
)
494 (with-current-buffer (nnimap-buffer)
495 (when (stringp article
)
496 (setq article
(nnimap-find-article-by-message-id group article
)))
497 (nnimap-get-whole-article
498 article
(format "UID FETCH %%d %s"
499 (nnimap-header-parameters)))
500 (let ((buffer (current-buffer)))
501 (with-current-buffer (or to-buffer nntp-server-buffer
)
503 (insert-buffer-substring buffer
)
504 (nnheader-ms-strip-cr)
505 (cons group article
))))))
507 (defun nnimap-get-whole-article (article &optional command
)
512 "UID FETCH %d BODY.PEEK[]"
513 "UID FETCH %d RFC822.PEEK"))
515 ;; Check that we really got an article.
516 (goto-char (point-min))
517 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
520 ;; Remove any data that may have arrived before the FETCH data.
523 (delete-region (point-min) (point)))
524 (let ((bytes (nnimap-get-length)))
525 (delete-region (line-beginning-position)
526 (progn (forward-line 1) (point)))
527 (goto-char (+ (point) bytes
))
528 (delete-region (point) (point-max)))
531 (defun nnimap-capability (capability)
532 (member capability
(nnimap-capabilities nnimap-object
)))
534 (defun nnimap-ver4-p ()
535 (nnimap-capability "IMAP4REV1"))
537 (defun nnimap-get-partial-article (article parts structure
)
540 "UID FETCH %d (%s %s)"
546 (mapconcat (lambda (part)
547 (format "BODY.PEEK[%s]" part
))
549 (mapconcat (lambda (part)
550 (format "RFC822.PEEK[%s]" part
))
553 (nnimap-convert-partial-article structure
))))
555 (defun nnimap-convert-partial-article (structure)
556 ;; First just skip past the headers.
557 (goto-char (point-min))
558 (let ((bytes (nnimap-get-length))
560 ;; Delete "FETCH" line.
561 (delete-region (line-beginning-position)
562 (progn (forward-line 1) (point)))
563 (goto-char (+ (point) bytes
))
564 ;; Collect all the body parts.
565 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
566 (setq id
(match-string 1)
567 bytes
(nnimap-get-length))
569 (delete-region (point) (progn (forward-line 1) (point)))
570 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
572 (delete-region (point) (+ (point) bytes
)))
573 ;; Delete trailing junk.
574 (delete-region (point) (point-max))
575 ;; Now insert all the parts again where they fit in the structure.
576 (nnimap-insert-partial-structure structure parts
)
579 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
581 (let ((bstruc structure
))
582 (while (consp (car bstruc
))
584 (setq type
(car bstruc
))
585 (setq bstruc
(car (cdr bstruc
)))
586 (let ((has-boundary (member "boundary" bstruc
)))
588 (setq boundary
(cadr has-boundary
)))))
590 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
591 (downcase type
) boundary
)))
592 (while (not (stringp (car structure
)))
593 (insert "\n--" boundary
"\n")
594 (if (consp (caar structure
))
595 (nnimap-insert-partial-structure (pop structure
) parts t
)
596 (let ((bit (pop structure
)))
597 (insert (format "Content-type: %s/%s"
598 (downcase (nth 0 bit
))
599 (downcase (nth 1 bit
))))
600 (if (member "CHARSET" (nth 2 bit
))
602 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit
)))))
604 (insert (format "Content-transfer-encoding: %s\n"
607 (when (assoc (nth 9 bit
) parts
)
608 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
609 (insert "\n--" boundary
"--\n")))
611 (defun nnimap-find-wanted-parts (structure)
612 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
614 (defun nnimap-find-wanted-parts-1 (structure prefix
)
617 (while (consp (car structure
))
618 (let ((sub (pop structure
)))
619 (if (consp (car sub
))
620 (push (nnimap-find-wanted-parts-1
621 sub
(if (string= prefix
"")
622 (number-to-string num
)
623 (format "%s.%s" prefix num
)))
625 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
626 (id (if (string= prefix
"")
627 (number-to-string num
)
628 (format "%s.%s" prefix num
))))
629 (setcar (nthcdr 9 sub
) id
)
630 (when (if (eq nnimap-fetch-partial-articles t
)
632 (string-match nnimap-fetch-partial-articles type
))
637 (deffoo nnimap-request-group
(group &optional server dont-check info
)
638 (let ((result (nnimap-possibly-change-group
639 ;; Don't SELECT the group if we're going to select it
642 (assoc group nnimap-current-infos
))
646 articles active marks high low
)
647 (with-current-buffer nntp-server-buffer
650 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
651 (insert (format "211 %d %d %d %S\n"
652 (- (cdr active
) (car active
))
656 (with-current-buffer (nnimap-buffer)
658 (let ((group-sequence
659 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
661 (nnimap-send-command "UID FETCH 1:* FLAGS")))
662 (setf (nnimap-group nnimap-object
) group
)
663 (nnimap-wait-for-response flag-sequence
)
665 (nnimap-flags-to-marks
667 (list (list group-sequence flag-sequence
668 1 group
"SELECT")))))
671 (nnimap-update-infos marks
(list info
))
672 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
673 (goto-char (point-max))
674 (let ((uidnext (nth 5 (car marks
))))
675 (setq high
(or (if uidnext
679 low
(or (nth 4 (car marks
)) uidnext
1)))))
683 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
686 (deffoo nnimap-request-create-group
(group &optional server args
)
687 (when (nnimap-possibly-change-group nil server
)
688 (with-current-buffer (nnimap-buffer)
689 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
691 (deffoo nnimap-request-delete-group
(group &optional force server
)
692 (when (nnimap-possibly-change-group nil server
)
693 (with-current-buffer (nnimap-buffer)
694 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
696 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
697 (when (nnimap-possibly-change-group nil server
)
698 (with-current-buffer (nnimap-buffer)
699 (nnimap-unselect-group)
700 (car (nnimap-command "RENAME %S %S"
701 (utf7-encode group t
) (utf7-encode new-name t
))))))
703 (defun nnimap-unselect-group ()
704 ;; Make sure we don't have this group open read/write by asking
705 ;; to examine a mailbox that doesn't exist. This seems to be
706 ;; the only way that allows us to reliably go back to unselected
708 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
710 (deffoo nnimap-request-expunge-group
(group &optional server
)
711 (when (nnimap-possibly-change-group group server
)
712 (with-current-buffer (nnimap-buffer)
713 (car (nnimap-command "EXPUNGE")))))
715 (defun nnimap-get-flags (spec)
718 (with-current-buffer (nnimap-buffer)
720 (nnimap-wait-for-response (nnimap-send-command
721 "UID FETCH %s FLAGS" spec
))
723 (subst-char-in-region (point-min) (point-max)
725 (goto-char (point-min))
726 (while (search-forward " FETCH " end t
)
727 (setq elems
(read (current-buffer)))
728 (push (cons (cadr (memq 'UID elems
))
729 (cadr (memq 'FLAGS elems
)))
731 (nreverse articles
)))
733 (deffoo nnimap-close-group
(group &optional server
)
736 (deffoo nnimap-request-move-article
(article group server accept-form
737 &optional last internal-move-group
)
739 (mm-disable-multibyte)
740 (when (funcall (if internal-move-group
742 'nnimap-request-article
)
743 article group server
(current-buffer))
744 ;; If the move is internal (on the same server), just do it the easy
746 (let ((message-id (message-field-value "message-id")))
747 (if internal-move-group
749 (with-current-buffer (nnimap-buffer)
750 (nnimap-command "UID COPY %d %S"
752 (utf7-encode internal-move-group t
)))))
754 (nnimap-delete-article article
)
755 (cons internal-move-group
756 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
757 (nnimap-find-article-by-message-id
758 internal-move-group message-id
)))))
759 ;; Move the article to a different method.
760 (let ((result (eval accept-form
)))
762 (nnimap-delete-article article
)
765 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
769 ((not (nnimap-possibly-change-group group server
))
772 (eq nnmail-expiry-target
'delete
))
773 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
774 (nnheader-message 7 "Article marked for deletion, but not expunged."))
777 (let ((deletable-articles
779 (eq nnmail-expiry-wait
'immediate
))
781 (gnus-sorted-intersection
783 (nnimap-find-expired-articles group
)))))
784 (if (null deletable-articles
)
786 (if (eq nnmail-expiry-target
'delete
)
787 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
788 (setq deletable-articles
789 (nnimap-process-expiry-targets
790 deletable-articles group server
)))
791 ;; Return the articles we didn't delete.
792 (gnus-sorted-complement articles deletable-articles
))))))
794 (defun nnimap-process-expiry-targets (articles group server
)
795 (let ((deleted-articles nil
))
796 (dolist (article articles
)
797 (let ((target nnmail-expiry-target
))
799 (mm-disable-multibyte)
800 (when (nnimap-request-article article group server
(current-buffer))
801 (nnheader-message 7 "Expiring article %s:%d" group article
)
802 (when (functionp target
)
803 (setq target
(funcall target group
)))
805 (not (eq target
'delete
)))
806 (if (or (gnus-request-group target t
)
807 (gnus-request-create-group target
))
808 (nnmail-expiry-target-group target group
)
811 (push article deleted-articles
))))))
812 ;; Change back to the current group again.
813 (nnimap-possibly-change-group group server
)
814 (setq deleted-articles
(nreverse deleted-articles
))
815 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
818 (defun nnimap-find-expired-articles (group)
819 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
820 (with-current-buffer (nnimap-buffer)
823 "UID SEARCH SENTBEFORE %s"
827 (car (rassoc (nth 4 (decode-time cutoff
))
828 parse-time-months
))))
831 (delete 0 (mapcar #'string-to-number
832 (cdr (assoc "SEARCH" (cdr result
))))))))))
835 (defun nnimap-find-article-by-message-id (group message-id
)
836 (with-current-buffer (nnimap-buffer)
838 (unless (equal group
(nnimap-group nnimap-object
))
839 (setf (nnimap-group nnimap-object
) nil
)
840 (setf (nnimap-examined nnimap-object
) group
)
841 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
843 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
845 (setq result
(nnimap-wait-for-response sequence
))
847 (car (setq result
(nnimap-parse-response))))
848 ;; Select the last instance of the message in the group.
850 (car (last (assoc "SEARCH" (cdr result
)))))
851 (string-to-number article
))))))
853 (defun nnimap-delete-article (articles)
854 (with-current-buffer (nnimap-buffer)
855 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
856 (nnimap-article-ranges articles
))
858 ((nnimap-capability "UIDPLUS")
859 (nnimap-command "UID EXPUNGE %s"
860 (nnimap-article-ranges articles
))
863 (nnimap-command "EXPUNGE")
865 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
866 "server doesn't support UIDPLUS, so we won't "
867 "delete this article now"))))))
869 (deffoo nnimap-request-scan
(&optional group server
)
870 (when (and (nnimap-possibly-change-group nil server
)
872 nnimap-split-methods
)
873 (nnheader-message 7 "nnimap %s splitting mail..." server
)
874 (nnimap-split-incoming-mail)))
876 (defun nnimap-marks-to-flags (marks)
879 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
883 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
884 (when (nnimap-possibly-change-group group server
)
886 (with-current-buffer (nnimap-buffer)
888 ;; Just send all the STORE commands without waiting for
889 ;; response. If they're successful, they're successful.
890 (dolist (action actions
)
891 (destructuring-bind (range action marks
) action
892 (let ((flags (nnimap-marks-to-flags marks
)))
894 (setq sequence
(nnimap-send-command
895 "UID STORE %s %sFLAGS.SILENT (%s)"
896 (nnimap-article-ranges range
)
898 ((eq action
'del
) "-")
899 ((eq action
'add
) "+")
900 ((eq action
'set
) ""))
901 (mapconcat #'identity flags
" ")))))))
902 ;; Wait for the last command to complete to avoid later
903 ;; syncronisation problems with the stream.
905 (nnimap-wait-for-response sequence
))))))
907 (deffoo nnimap-request-accept-article
(group &optional server last
)
908 (when (nnimap-possibly-change-group nil server
)
909 (nnmail-check-syntax)
910 (let ((message-id (message-field-value "message-id"))
913 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
914 (with-current-buffer (nnimap-buffer)
915 ;; If we have this group open read-only, then unselect it
916 ;; before appending to it.
917 (when (equal (nnimap-examined nnimap-object
) group
)
918 (nnimap-unselect-group))
920 (setq sequence
(nnimap-send-command
921 "APPEND %S {%d}" (utf7-encode group t
)
923 (unless nnimap-streaming
924 (nnimap-wait-for-connection "^[+]"))
925 (process-send-string (get-buffer-process (current-buffer)) message
)
926 (process-send-string (get-buffer-process (current-buffer))
927 (if (nnimap-newlinep nnimap-object
)
930 (let ((result (nnimap-get-response sequence
)))
931 (if (not (car result
))
933 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap
))
936 (or (nnimap-find-uid-response "APPENDUID" (car result
))
937 (nnimap-find-article-by-message-id
938 group message-id
)))))))))
940 (defun nnimap-find-uid-response (name list
)
941 (let ((result (car (last (nnimap-find-response-element name list
)))))
943 (string-to-number result
))))
945 (defun nnimap-find-response-element (name list
)
948 (when (and (consp elem
)
949 (equal name
(car elem
)))
953 (deffoo nnimap-request-replace-article
(article group buffer
)
955 (when (and (nnimap-possibly-change-group group nil
)
956 ;; Put the article into the group.
957 (with-current-buffer buffer
959 (nnimap-request-accept-article group nil t
))))
960 (nnimap-delete-article (list article
))
961 ;; Return the new article number.
964 (defun nnimap-add-cr ()
965 (goto-char (point-min))
966 (while (re-search-forward "\r?\n" nil t
)
967 (replace-match "\r\n" t t
)))
969 (defun nnimap-get-groups ()
971 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
973 (nnimap-wait-for-response sequence
)
974 (subst-char-in-region (point-min) (point-max)
976 (goto-char (point-min))
977 (nnimap-unfold-quoted-lines)
978 (goto-char (point-min))
979 (while (search-forward "* LIST " nil t
)
980 (let ((flags (read (current-buffer)))
981 (separator (read (current-buffer)))
982 (group (read (current-buffer))))
983 (unless (member '%NoSelect flags
)
984 (push (if (stringp group
)
990 (deffoo nnimap-request-list
(&optional server
)
991 (nnimap-possibly-change-group nil server
)
992 (with-current-buffer nntp-server-buffer
995 (with-current-buffer (nnimap-buffer)
996 (nnimap-get-groups)))
999 (with-current-buffer (nnimap-buffer)
1000 (setf (nnimap-group nnimap-object
) nil
)
1001 (dolist (group groups
)
1002 (setf (nnimap-examined nnimap-object
) group
)
1003 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t
))
1006 (nnimap-wait-for-response (caar sequences
))
1008 (nnimap-get-responses (mapcar #'car sequences
))))
1009 (dolist (response responses
)
1010 (let* ((sequence (car response
))
1011 (response (cadr response
))
1012 (group (cadr (assoc sequence sequences
))))
1014 (equal (caar response
) "OK"))
1015 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1017 (dolist (elem response
)
1018 (when (equal (cadr elem
) "EXISTS")
1019 (setq exists
(string-to-number (car elem
)))))
1021 (setq highest
(1- (string-to-number (car uidnext
)))))
1024 (insert (format "%S 0 1 y\n" (utf7-decode group t
))))
1027 (insert (format "%S %d %d y\n"
1028 (utf7-decode group t
) highest
(1+ highest
))))
1030 ;; Return the widest possible range.
1031 (insert (format "%S %d 1 y\n" (utf7-decode group t
)
1032 (or highest exists
)))))))))
1035 (deffoo nnimap-request-newgroups
(date &optional server
)
1036 (nnimap-possibly-change-group nil server
)
1037 (with-current-buffer nntp-server-buffer
1039 (dolist (group (with-current-buffer (nnimap-buffer)
1040 (nnimap-get-groups)))
1041 (unless (assoc group nnimap-current-infos
)
1042 ;; Insert dummy numbers here -- they don't matter.
1043 (insert (format "%S 0 1 y\n" group
))))
1046 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1047 (when (nnimap-possibly-change-group nil server
)
1048 (with-current-buffer (nnimap-buffer)
1050 (setf (nnimap-group nnimap-object
) nil
)
1051 (let ((qresyncp (nnimap-capability "QRESYNC"))
1052 params groups sequences active uidvalidity modseq group
)
1053 ;; Go through the infos and gather the data needed to know
1054 ;; what and how to request the data.
1055 (dolist (info infos
)
1056 (setq params
(gnus-info-params info
)
1057 group
(gnus-group-real-name (gnus-info-group info
))
1058 active
(cdr (assq 'active params
))
1059 uidvalidity
(cdr (assq 'uidvalidity params
))
1060 modseq
(cdr (assq 'modseq params
)))
1061 (setf (nnimap-examined nnimap-object
) group
)
1066 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1067 (utf7-encode group t
)
1073 (if (and active uidvalidity
)
1074 ;; Fetch the last 100 flags.
1075 (max 1 (- (cdr active
) 100))
1080 ;; If we don't have a UIDVALIDITY, then this is
1081 ;; the first time we've seen the group, so we
1082 ;; have to do a SELECT (which is slower than an
1083 ;; examine), but will tell us whether the group
1084 ;; is read-only or not.
1086 (push (list (nnimap-send-command "%s %S" command
1087 (utf7-encode group t
))
1088 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1089 start group command
)
1093 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1094 (when (and sequences
1095 (nnimap-possibly-change-group nil server
))
1096 (with-current-buffer (nnimap-buffer)
1097 ;; Wait for the final data to trickle in.
1098 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1102 ;; Now we should have most of the data we need, no matter
1103 ;; whether we're QRESYNCING, fetching all the flags from
1104 ;; scratch, or just fetching the last 100 flags per group.
1105 (nnimap-update-infos (nnimap-flags-to-marks
1107 (nreverse sequences
)))
1109 ;; Finally, just return something resembling an active file in
1110 ;; the nntp buffer, so that the agent can save the info, too.
1111 (with-current-buffer nntp-server-buffer
1113 (dolist (info infos
)
1114 (let* ((group (gnus-info-group info
))
1115 (active (gnus-active group
)))
1117 (insert (format "%S %d %d y\n"
1118 (gnus-group-real-name group
)
1120 (car active
)))))))))))
1122 (defun nnimap-update-infos (flags infos
)
1123 (dolist (info infos
)
1124 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1125 (marks (cdr (assoc group flags
))))
1127 (nnimap-update-info info marks
)))))
1129 (defun nnimap-update-info (info marks
)
1130 (destructuring-bind (existing flags high low uidnext start-article
1131 permanent-flags uidvalidity
1132 vanished highestmodseq
) marks
1134 ;; Ignore groups with no UIDNEXT/marks. This happens for
1135 ;; completely empty groups.
1136 ((and (not existing
)
1138 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1140 (gnus-set-active (gnus-info-group info
) active
))))
1141 ;; We have a mismatch between the old and new UIDVALIDITY
1142 ;; identifiers, so we have to re-request the group info (the next
1143 ;; time). This virtually never happens.
1144 ((let ((old-uidvalidity
1145 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1146 (and old-uidvalidity
1147 (not (equal old-uidvalidity uidvalidity
))
1148 (> start-article
1)))
1149 (gnus-group-remove-parameter info
'uidvalidity
)
1150 (gnus-group-remove-parameter info
'modseq
))
1151 ;; We have the data needed to update.
1153 (let* ((group (gnus-info-group info
))
1154 (completep (and start-article
1155 (= start-article
1)))
1156 (active (or (gnus-active group
)
1157 (cdr (assq 'active
(gnus-info-params info
))))))
1159 (setq high
(1- uidnext
)))
1160 ;; First set the active ranges based on high/low.
1162 (not (gnus-active group
)))
1163 (gnus-set-active group
1166 (cons (min (or low
(car active
))
1168 (max (or high
(cdr active
))
1173 ;; No articles in this group.
1174 (cons uidnext
(1- uidnext
)))
1176 (cons start-article
(1- start-article
)))
1178 ;; No articles and no uidnext.
1183 (or high
(1- uidnext
)))))
1184 ;; See whether this is a read-only group.
1185 (unless (eq permanent-flags
'not-scanned
)
1186 (gnus-group-set-parameter
1187 info
'permanent-flags
1188 (and (or (memq '%
* permanent-flags
)
1189 (memq '%Seen permanent-flags
))
1191 ;; Update marks and read articles if this isn't a
1192 ;; read-only IMAP group.
1193 (when (setq permanent-flags
1194 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1195 (if (and highestmodseq
1196 (not start-article
))
1197 ;; We've gotten the data by QRESYNCing.
1198 (nnimap-update-qresync-info
1199 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1200 ;; Do normal non-QRESYNC flag updates.
1201 ;; Update the list of read articles.
1203 (gnus-compress-sequence
1204 (gnus-set-difference
1205 (gnus-set-difference
1207 (cdr (assoc '%Seen flags
)))
1208 (cdr (assoc '%Flagged flags
)))))
1209 (read (gnus-range-difference
1210 (cons start-article high
) unread
)))
1211 (when (> start-article
1)
1214 (if (> start-article
1)
1215 (gnus-sorted-range-intersection
1216 (cons 1 (1- start-article
))
1217 (gnus-info-read info
))
1218 (gnus-info-read info
))
1220 (when (or (not (listp permanent-flags
))
1221 (memq '%Seen permanent-flags
))
1222 (gnus-info-set-read info read
))
1223 ;; Update the marks.
1224 (setq marks
(gnus-info-marks info
))
1225 (dolist (type (cdr nnimap-mark-alist
))
1226 (when (or (not (listp permanent-flags
))
1227 (memq (car (assoc (caddr type
) flags
))
1229 (memq '%
* permanent-flags
))
1230 (let ((old-marks (assoc (car type
) marks
))
1232 (gnus-compress-sequence
1233 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1234 (assoc (intern (cadr type
) obarray
) flags
)
1235 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1236 (setq marks
(delq old-marks marks
))
1238 (when (and old-marks
1239 (> start-article
1))
1240 (setq old-marks
(gnus-range-difference
1242 (cons start-article high
)))
1243 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1245 (push (cons (car type
) new-marks
) marks
)))))
1246 (gnus-info-set-marks info marks t
))))
1247 ;; Note the active level for the next run-through.
1248 (gnus-group-set-parameter info
'active
(gnus-active group
))
1249 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1250 (gnus-group-set-parameter info
'modseq highestmodseq
)
1251 (nnimap-store-info info
(gnus-active group
)))))))
1253 (defun nnimap-update-qresync-info (info existing vanished flags
)
1254 ;; Add all the vanished articles to the list of read articles.
1259 (gnus-range-add (gnus-info-read info
)
1261 (cdr (assq '%Flagged flags
)))
1262 (cdr (assq '%Seen flags
))))
1263 (let ((marks (gnus-info-marks info
)))
1264 (dolist (type (cdr nnimap-mark-alist
))
1265 (let ((ticks (assoc (car type
) marks
))
1267 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1268 (assoc (intern (cadr type
) obarray
) flags
)
1269 (assoc (cadr type
) flags
))))) ; "\Flagged"
1270 (setq marks
(delq ticks marks
))
1272 ;; Add the new marks we got.
1273 (setq ticks
(gnus-add-to-range ticks new-marks
))
1274 ;; Remove the marks from messages that don't have them.
1275 (setq ticks
(gnus-remove-from-range
1277 (gnus-compress-sequence
1278 (gnus-sorted-complement existing new-marks
))))
1280 (push (cons (car type
) ticks
) marks
)))
1281 (gnus-info-set-marks info marks t
))))
1283 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1284 (if (zerop (length irange
))
1287 (dolist (elem (split-string irange
","))
1289 (if (string-match ":" elem
)
1290 (let ((numbers (split-string elem
":")))
1291 (cons (string-to-number (car numbers
))
1292 (string-to-number (cadr numbers
))))
1293 (string-to-number elem
))
1295 (nreverse result
))))
1297 (defun nnimap-store-info (info active
)
1298 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1299 (entry (assoc group nnimap-current-infos
)))
1301 (setcdr entry
(list info active
))
1302 (push (list group info active
) nnimap-current-infos
))))
1304 (defun nnimap-flags-to-marks (groups)
1305 (let (data group totalp uidnext articles start-article mark permanent-flags
1306 uidvalidity vanished highestmodseq
)
1307 (dolist (elem groups
)
1308 (setq group
(car elem
)
1309 uidnext
(nth 1 elem
)
1310 start-article
(nth 2 elem
)
1311 permanent-flags
(nth 3 elem
)
1312 uidvalidity
(nth 4 elem
)
1313 vanished
(nth 5 elem
)
1314 highestmodseq
(nth 6 elem
)
1315 articles
(nthcdr 7 elem
))
1316 (let ((high (caar articles
))
1318 (dolist (article articles
)
1319 (setq low
(car article
))
1320 (push (car article
) existing
)
1321 (dolist (flag (cdr article
))
1322 (setq mark
(assoc flag marks
))
1324 (push (list flag
(car article
)) marks
)
1325 (setcdr mark
(cons (car article
) (cdr mark
))))))
1326 (push (list group existing marks high low uidnext start-article
1327 permanent-flags uidvalidity vanished highestmodseq
)
1331 (defun nnimap-parse-flags (sequences)
1332 (goto-char (point-min))
1333 ;; Change \Delete etc to %Delete, so that the reader can read it.
1334 (subst-char-in-region (point-min) (point-max)
1336 (let (start end articles groups uidnext elems permanent-flags
1337 uidvalidity vanished highestmodseq
)
1338 (dolist (elem sequences
)
1339 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1341 (setq start
(point))
1343 ;; The EXAMINE was successful.
1344 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1349 (setq permanent-flags
1350 (if (equal command
"SELECT")
1351 (and (search-forward "PERMANENTFLAGS "
1352 (or end
(point-min)) t
)
1353 (read (current-buffer)))
1357 (and (search-forward "UIDNEXT "
1358 (or end
(point-min)) t
)
1359 (read (current-buffer))))
1362 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1363 (or end
(point-min)) t
)
1364 ;; Store UIDVALIDITY as a string, as it's
1365 ;; too big for 32-bit Emacsen, usually.
1369 (and (eq flag-sequence
'qresync
)
1370 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1371 (or end
(point-min)) t
)
1375 (and (search-forward "HIGHESTMODSEQ "
1376 (or end
(point-min)) t
)
1377 (read (current-buffer))))
1380 ;; The UID FETCH FLAGS was successful.
1381 (or (eq flag-sequence
'qresync
)
1382 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1383 (if (eq flag-sequence
'qresync
)
1387 (setq start
(point))
1389 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1390 (setq elems
(read (current-buffer)))
1391 (push (cons (cadr (memq 'UID elems
))
1392 (cadr (memq 'FLAGS elems
)))
1394 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1395 vanished highestmodseq
)
1399 (setq articles nil
))))
1402 (defun nnimap-find-process-buffer (buffer)
1403 (cadr (assoc buffer nnimap-connection-alist
)))
1405 (deffoo nnimap-request-post
(&optional server
)
1406 (setq nnimap-status-string
"Read-only server")
1409 (deffoo nnimap-request-thread
(id)
1410 (let* ((refs (split-string
1411 (or (mail-header-references (gnus-summary-article-header))
1415 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1417 (dolist (refid refs value
)
1419 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1420 refid refid value
)))))
1421 (result (with-current-buffer (nnimap-buffer)
1422 (nnimap-command "UID SEARCH %s" cmd
))))
1424 (and (car result
) (delete 0 (mapcar #'string-to-number
1425 (cdr (assoc "SEARCH" (cdr result
))))))
1428 (defun nnimap-possibly-change-group (group server
)
1429 (let ((open-result t
))
1431 (not (nnimap-server-opened server
)))
1432 (setq open-result
(nnimap-open-server server
)))
1439 (with-current-buffer (nnimap-buffer)
1440 (if (equal group
(nnimap-group nnimap-object
))
1442 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1444 (setf (nnimap-group nnimap-object
) group
1445 (nnimap-select-result nnimap-object
) result
)
1448 (defun nnimap-find-connection (buffer)
1449 "Find the connection delivering to BUFFER."
1450 (let ((entry (assoc buffer nnimap-connection-alist
)))
1452 (if (and (buffer-name (cadr entry
))
1453 (get-buffer-process (cadr entry
))
1454 (memq (process-status (get-buffer-process (cadr entry
)))
1456 (get-buffer-process (cadr entry
))
1457 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1460 (defvar nnimap-sequence
0)
1462 (defun nnimap-send-command (&rest args
)
1463 (process-send-string
1464 (get-buffer-process (current-buffer))
1467 (incf nnimap-sequence
)
1468 (apply #'format args
)
1469 (if (nnimap-newlinep nnimap-object
)
1472 ;; Some servers apparently can't have many outstanding
1473 ;; commands, so throttle them.
1474 (unless nnimap-streaming
1475 (nnimap-wait-for-response nnimap-sequence
))
1478 (defun nnimap-log-command (command)
1479 (with-current-buffer (get-buffer-create "*imap log*")
1480 (goto-char (point-max))
1481 (insert (format-time-string "%H:%M:%S") " " command
))
1484 (defun nnimap-command (&rest args
)
1486 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1487 (let* ((sequence (apply #'nnimap-send-command args
))
1488 (response (nnimap-get-response sequence
)))
1489 (if (equal (caar response
) "OK")
1491 (nnheader-report 'nnimap
"%s"
1492 (mapconcat (lambda (a)
1494 (car response
) " "))
1497 (defun nnimap-get-response (sequence)
1498 (nnimap-wait-for-response sequence
)
1499 (nnimap-parse-response))
1501 (defun nnimap-wait-for-connection (&optional regexp
)
1503 (setq regexp
"^[*.] .*\n"))
1504 (let ((process (get-buffer-process (current-buffer))))
1505 (goto-char (point-min))
1506 (while (and (memq (process-status process
)
1508 (not (re-search-forward regexp nil t
)))
1509 (nnheader-accept-process-output process
)
1510 (goto-char (point-min)))
1512 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1515 (defun nnimap-wait-for-response (sequence &optional messagep
)
1516 (let ((process (get-buffer-process (current-buffer)))
1520 (goto-char (point-max))
1521 (while (and (setq openp
(memq (process-status process
)
1523 (not (re-search-backward
1524 (format "^%d .*\n" sequence
)
1525 (if nnimap-streaming
1526 (max (point-min) (- (point) 500))
1530 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1531 (nnheader-accept-process-output process
)
1532 (goto-char (point-max)))
1535 ;; The user hit C-g while we were waiting: kill the process, in case
1536 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1538 (delete-process process
)
1541 (defun nnimap-parse-response ()
1542 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1544 (dolist (line lines
)
1545 (push (cdr (nnimap-parse-line line
)) result
))
1546 ;; Return the OK/error code first, and then all the "continuation
1547 ;; lines" afterwards.
1549 (nreverse result
))))
1551 ;; Parse an IMAP response line lightly. They look like
1552 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1553 ;; the lines into a list of strings and lists of string.
1554 (defun nnimap-parse-line (line)
1557 (mm-disable-multibyte)
1559 (goto-char (point-min))
1561 (if (eql (setq char
(following-char)) ?
)
1569 (if (search-forward "]" (line-end-position) 'move
)
1576 (if (search-forward ")" (line-end-position) 'move
)
1583 (1- (or (search-forward "\"" (line-end-position) 'move
)
1586 (buffer-substring (point) (if (search-forward " " nil t
)
1588 (goto-char (point-max))))))
1590 (nreverse result
))))
1592 (defun nnimap-last-response-string ()
1595 (let ((end (point)))
1599 (while (and (not (bobp))
1600 (eql (following-char) ?
*))
1602 (unless (eql (following-char) ?
*)
1604 (buffer-substring (point) end
))))
1606 (defun nnimap-get-responses (sequences)
1608 (dolist (sequence sequences
)
1609 (goto-char (point-min))
1610 (when (re-search-forward (format "^%d " sequence
) nil t
)
1611 (push (list sequence
(nnimap-parse-response))
1615 (defvar nnimap-incoming-split-list nil
)
1617 (defun nnimap-fetch-inbox (articles)
1619 (nnimap-wait-for-response
1620 (nnimap-send-command
1622 (nnimap-article-ranges articles
)
1623 (format "(UID %s%s)"
1626 "BODY.PEEK[HEADER] BODY.PEEK"
1628 (if nnimap-split-download-body-default
1633 (defun nnimap-split-incoming-mail ()
1634 (with-current-buffer (nnimap-buffer)
1635 (let ((nnimap-incoming-split-list nil
)
1636 (nnmail-split-methods (if (eq nnimap-split-methods
'default
)
1637 nnmail-split-methods
1638 nnimap-split-methods
))
1639 (nnmail-split-fancy (or nnimap-split-fancy
1640 nnmail-split-fancy
))
1641 (nnmail-inhibit-default-split-group t
)
1642 (groups (nnimap-get-groups))
1645 (nnimap-command "SELECT %S" nnimap-inbox
)
1646 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1647 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1649 (nnimap-fetch-inbox new-articles
)
1650 (nnimap-transform-split-mail)
1651 (nnheader-ms-strip-cr)
1653 (nnmail-split-incoming (current-buffer)
1654 #'nnimap-save-mail-spec
1656 #'nnimap-dummy-active-number
1657 #'nnimap-save-mail-spec
)
1658 (when nnimap-incoming-split-list
1659 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1660 sequences junk-articles
)
1661 ;; Create any groups that doesn't already exist on the
1663 (dolist (spec specs
)
1664 (when (and (not (member (car spec
) groups
))
1665 (not (eq (car spec
) 'junk
)))
1666 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1667 ;; Then copy over all the messages.
1669 (dolist (spec specs
)
1670 (let ((group (car spec
))
1671 (ranges (cdr spec
)))
1672 (if (eq group
'junk
)
1673 (setq junk-articles ranges
)
1674 (push (list (nnimap-send-command
1676 (nnimap-article-ranges ranges
)
1677 (utf7-encode group t
))
1680 ;; Wait for the last COPY response...
1682 (nnimap-wait-for-response (caar sequences
))
1683 ;; And then mark the successful copy actions as deleted,
1684 ;; and possibly expunge them.
1685 (nnimap-mark-and-expunge-incoming
1686 (nnimap-parse-copied-articles sequences
)))
1687 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1689 (defun nnimap-mark-and-expunge-incoming (range)
1691 (setq range
(nnimap-article-ranges range
))
1694 (nnimap-send-command
1695 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1697 ;; If the server supports it, we now delete the message we have
1698 ;; just copied over.
1699 ((nnimap-capability "UIDPLUS")
1700 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1701 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1702 ;; user has configured it.
1704 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1705 (nnimap-wait-for-response sequence
))))
1707 (defun nnimap-parse-copied-articles (sequences)
1708 (let (sequence copied range
)
1709 (goto-char (point-min))
1710 (while (re-search-forward "^\\([0-9]+\\) OK " nil t
)
1711 (setq sequence
(string-to-number (match-string 1)))
1712 (when (setq range
(cadr (assq sequence sequences
)))
1713 (push (gnus-uncompress-range range
) copied
)))
1714 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1716 (defun nnimap-new-articles (flags)
1718 (dolist (elem flags
)
1719 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1721 (push (car elem
) new
)))
1722 (gnus-compress-sequence (nreverse new
))))
1724 (defun nnimap-make-split-specs (list)
1728 (destructuring-bind (article spec
) elem
1729 (dolist (group (delete nil
(mapcar #'car spec
)))
1730 (unless (setq entry
(assoc group specs
))
1731 (push (setq entry
(list group
)) specs
))
1732 (setcdr entry
(cons article
(cdr entry
))))))
1733 (dolist (entry specs
)
1734 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
1737 (defun nnimap-transform-split-mail ()
1738 (goto-char (point-min))
1739 (let (article bytes
)
1742 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1743 (delete-region (point) (progn (forward-line 1) (point)))
1746 (setq article
(match-string 1)
1747 bytes
(nnimap-get-length))
1748 (delete-region (line-beginning-position) (line-end-position))
1749 ;; Insert MMDF separator, and a way to remember what this
1751 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
1752 (forward-char (1+ bytes
))
1753 (setq bytes
(nnimap-get-length))
1754 (delete-region (line-beginning-position) (line-end-position))
1755 ;; There's a body; skip past that.
1757 (forward-char (1+ bytes
))
1758 (delete-region (line-beginning-position) (line-end-position)))))))
1760 (defun nnimap-dummy-active-number (group &optional server
)
1763 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
1765 (goto-char (point-min))
1766 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
1767 (error "Invalid nnimap mail")
1768 (setq article
(string-to-number (match-string 1))))
1770 (if (eq group-art
'junk
)
1771 (list (cons 'junk
1))
1773 nnimap-incoming-split-list
)))
1777 ;;; nnimap.el ends here