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))))
52 (autoload 'auth-source-forget-user-or-password "auth-source")
53 (autoload 'auth-source-user-or-password "auth-source")
57 (defvoo nnimap-address nil
58 "The address of the IMAP server.")
60 (defvoo nnimap-server-port nil
62 If nnimap-stream is `ssl', this will default to `imaps'. If not,
63 it will default to `imap'.")
65 (defvoo nnimap-stream 'ssl
66 "How nnimap will talk to the IMAP server.
67 Values are `ssl', `network', `starttls' or `shell'.")
69 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
70 (if (listp imap-shell-program)
71 (car imap-shell-program)
75 (defvoo nnimap-inbox nil
76 "The mail box where incoming mail arrives and should be split out of.")
78 (defvoo nnimap-split-methods nil
80 Uses the same syntax as nnmail-split-methods")
82 (defvoo nnimap-split-fancy nil
83 "Uses the same syntax as nnmail-split-fancy.")
85 (defvoo nnimap-unsplittable-articles '(%Deleted %Seen)
86 "Articles with the flags in the list will not be considered when splitting.")
88 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
91 (defvoo nnimap-authenticator nil
92 "How nnimap authenticate itself to the server.
93 Possible choices are nil (use default methods) or `anonymous'.")
95 (defvoo nnimap-expunge t
96 "If non-nil, expunge articles after deleting them.
97 This is always done if the server supports UID EXPUNGE, but it's
98 not done by default on servers that doesn't support that command.")
100 (defvoo nnimap-streaming t
101 "If non-nil, try to use streaming commands with IMAP servers.
102 Switching this off will make nnimap slower, but it helps with
105 (defvoo nnimap-connection-alist nil)
107 (defvoo nnimap-current-infos nil)
109 (defvoo nnimap-fetch-partial-articles nil
110 "If non-nil, Gnus will fetch partial articles.
111 If t, nnimap will fetch only the first part. If a string, it
112 will fetch all parts that have types that match that string. A
113 likely value would be \"text/\" to automatically fetch all
116 (defvar nnimap-process nil)
118 (defvar nnimap-status-string "")
120 (defvar nnimap-split-download-body-default nil
121 "Internal variable with default value for `nnimap-split-download-body'.")
123 (defvar nnimap-keepalive-timer nil)
124 (defvar nnimap-process-buffers nil)
127 group process commands capabilities select-result newlinep server
128 last-command-time greeting)
130 (defvar nnimap-object nil)
132 (defvar nnimap-mark-alist
133 '((read "\\Seen" %Seen)
134 (tick "\\Flagged" %Flagged)
135 (reply "\\Answered" %Answered)
136 (expire "gnus-expire")
137 (dormant "gnus-dormant")
140 (download "gnus-download")
141 (forward "gnus-forward")))
143 (defun nnimap-buffer ()
144 (nnimap-find-process-buffer nntp-server-buffer))
146 (defun nnimap-header-parameters ()
147 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
150 "BODY.PEEK[HEADER.FIELDS %s]"
151 "RFC822.HEADER.LINES %s")
152 (append '(Subject From Date Message-Id
153 References In-Reply-To Xref)
154 nnmail-extra-headers))))
156 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
157 (with-current-buffer nntp-server-buffer
159 (when (nnimap-possibly-change-group group server)
160 (with-current-buffer (nnimap-buffer)
162 (nnimap-wait-for-response
165 (nnimap-article-ranges (gnus-compress-sequence articles))
166 (nnimap-header-parameters))
168 (nnimap-transform-headers))
169 (insert-buffer-substring
170 (nnimap-find-process-buffer (current-buffer))))
173 (defun nnimap-transform-headers ()
174 (goto-char (point-min))
175 (let (article bytes lines size string)
178 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
179 (delete-region (point) (progn (forward-line 1) (point)))
182 (setq article (match-string 1))
183 ;; Unfold quoted {number} strings.
184 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r\n"
185 (1+ (line-end-position)) t)
186 (setq size (string-to-number (match-string 1)))
187 (delete-region (+ (match-beginning 0) 2) (point))
188 (setq string (delete-region (point) (+ (point) size)))
189 (insert (format "%S" string)))
190 (setq bytes (nnimap-get-length)
194 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
199 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
200 (let ((structure (ignore-errors
201 (read (current-buffer)))))
202 (while (and (consp structure)
203 (not (stringp (car structure))))
204 (setq structure (car structure)))
205 (setq lines (nth 7 structure))))
206 (delete-region (line-beginning-position) (line-end-position))
207 (insert (format "211 %s Article retrieved." article))
210 (insert (format "Chars: %s\n" size)))
212 (insert (format "Lines: %s\n" lines)))
213 (unless (re-search-forward "^\r$" nil t)
214 (goto-char (point-max)))
215 (delete-region (line-beginning-position) (line-end-position))
219 (defun nnimap-get-length ()
220 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
221 (string-to-number (match-string 1))))
223 (defun nnimap-article-ranges (ranges)
227 (number-to-string ranges))
228 ((numberp (cdr ranges))
229 (format "%d:%d" (car ranges) (cdr ranges)))
231 (dolist (elem ranges)
234 (format "%d:%d" (car elem) (cdr elem))
235 (number-to-string elem))
237 (mapconcat #'identity (nreverse result) ",")))))
239 (deffoo nnimap-open-server (server &optional defs)
240 (if (nnimap-server-opened server)
242 (unless (assq 'nnimap-address defs)
243 (setq defs (append defs (list (list 'nnimap-address server)))))
244 (nnoo-change-server 'nnimap server defs)
245 (or (nnimap-find-connection nntp-server-buffer)
246 (nnimap-open-connection nntp-server-buffer))))
248 (defun nnimap-make-process-buffer (buffer)
250 (generate-new-buffer (format "*nnimap %s %s %s*"
251 nnimap-address nnimap-server-port
252 (gnus-buffer-exists-p buffer)))
253 (mm-disable-multibyte)
254 (buffer-disable-undo)
256 (set (make-local-variable 'after-change-functions) nil)
257 (set (make-local-variable 'nnimap-object)
258 (make-nnimap :server (nnoo-current-server 'nnimap)))
259 (push (list buffer (current-buffer)) nnimap-connection-alist)
260 (push (current-buffer) nnimap-process-buffers)
263 (defun nnimap-open-shell-stream (name buffer host port)
264 (let ((process-connection-type nil))
265 (start-process name buffer shell-file-name
273 (defun nnimap-credentials (address ports &optional inhibit-create)
274 (let (port credentials)
275 ;; Request the credentials from all ports, but only query on the
276 ;; last port if all the previous ones have failed.
277 (while (and (null credentials)
278 (setq port (pop ports)))
280 (auth-source-user-or-password
281 '("login" "password") address port nil
287 (defun nnimap-keepalive ()
288 (let ((now (current-time)))
289 (dolist (buffer nnimap-process-buffers)
290 (when (buffer-name buffer)
291 (with-current-buffer buffer
292 (when (and nnimap-object
293 (nnimap-last-command-time nnimap-object)
297 (nnimap-last-command-time nnimap-object)))
298 ;; More than five minutes since the last command.
300 (nnimap-send-command "NOOP")))))))
302 (declare-function gnutls-negotiate "gnutls"
303 (proc type &optional priority-string trustfiles keyfiles))
305 (defun nnimap-open-connection (buffer)
306 (unless nnimap-keepalive-timer
307 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
310 (with-current-buffer (nnimap-make-process-buffer buffer)
311 (let* ((coding-system-for-read 'binary)
312 (coding-system-for-write 'binary)
316 ((or (eq nnimap-stream 'network)
317 (and (eq nnimap-stream 'starttls)
318 (fboundp 'open-gnutls-stream)))
319 (nnheader-message 7 "Opening connection to %s..."
322 "*nnimap*" (current-buffer) nnimap-address
324 (or nnimap-server-port
325 (if (netrc-find-service-number "imap")
329 ((eq nnimap-stream 'shell)
330 (nnheader-message 7 "Opening connection to %s via shell..."
332 (nnimap-open-shell-stream
333 "*nnimap*" (current-buffer) nnimap-address
334 (setq port (or nnimap-server-port "imap")))
336 ((eq nnimap-stream 'starttls)
337 (nnheader-message 7 "Opening connection to %s via starttls..."
340 '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -starttls imap")))
342 "*nnimap*" (current-buffer) nnimap-address
343 (setq port (or nnimap-server-port "imap"))))
345 ((memq nnimap-stream '(ssl tls))
346 (nnheader-message 7 "Opening connection to %s via tls..."
348 (funcall (if (fboundp 'open-gnutls-stream)
351 "*nnimap*" (current-buffer) nnimap-address
353 (or nnimap-server-port
354 (if (netrc-find-service-number "imaps")
357 '("143" "993" "imap" "imaps"))
359 (error "Unknown stream type: %s" nnimap-stream))))
360 connection-result login-result credentials)
361 (setf (nnimap-process nnimap-object)
362 (get-buffer-process (current-buffer)))
363 (if (not (and (nnimap-process nnimap-object)
364 (memq (process-status (nnimap-process nnimap-object))
366 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
367 nnimap-address port nnimap-stream)
368 (gnus-set-process-query-on-exit-flag
369 (nnimap-process nnimap-object) nil)
370 (if (not (setq connection-result (nnimap-wait-for-connection)))
371 (nnheader-report 'nnimap
372 "%s" (buffer-substring
373 (point) (line-end-position)))
374 ;; Store the greeting (for debugging purposes).
375 (setf (nnimap-greeting nnimap-object)
376 (buffer-substring (line-beginning-position)
377 (line-end-position)))
378 ;; Store the capabilities.
379 (setf (nnimap-capabilities nnimap-object)
382 (nnimap-find-parameter
383 "CAPABILITY" (cdr (nnimap-command "CAPABILITY")))))
384 (when nnimap-server-port
385 (push (format "%s" nnimap-server-port) ports))
386 ;; If this is a STARTTLS-capable server, then sever the
387 ;; connection and start a STARTTLS connection instead.
389 ((and (or (and (eq nnimap-stream 'network)
390 (nnimap-capability "STARTTLS"))
391 (eq nnimap-stream 'starttls))
392 (fboundp 'open-gnutls-stream))
393 (nnimap-command "STARTTLS")
394 (gnutls-negotiate (nnimap-process nnimap-object) nil))
395 ((and (eq nnimap-stream 'network)
396 (nnimap-capability "STARTTLS"))
397 (let ((nnimap-stream 'starttls))
399 (nnimap-open-connection buffer)))
400 ;; If the STARTTLS connection was successful, we
401 ;; kill our first non-encrypted connection. If it
402 ;; wasn't successful, we just use our unencrypted
404 (when (memq (process-status tls-process) '(open run))
405 (delete-process (nnimap-process nnimap-object))
406 (kill-buffer (current-buffer))
407 (return tls-process))))))
408 (unless (equal connection-result "PREAUTH")
409 (if (not (setq credentials
410 (if (eq nnimap-authenticator 'anonymous)
412 (message-make-address))
414 ;; First look for the credentials based
415 ;; on the virtual server name.
417 (nnoo-current-server 'nnimap) ports t)
418 ;; Then look them up based on the
420 (nnimap-credentials nnimap-address ports)))))
421 (setq nnimap-object nil)
423 (if (and (nnimap-capability "AUTH=PLAIN")
424 (nnimap-capability "LOGINDISABLED"))
426 "AUTHENTICATE PLAIN %s"
427 (base64-encode-string
428 (format "\000%s\000%s"
429 (nnimap-quote-specials (car credentials))
430 (nnimap-quote-specials (cadr credentials)))))
431 (nnimap-command "LOGIN %S %S"
433 (cadr credentials))))
434 (unless (car login-result)
435 ;; If the login failed, then forget the credentials
436 ;; that are now possibly cached.
437 (dolist (host (list (nnoo-current-server 'nnimap)
440 (dolist (element '("login" "password"))
441 (auth-source-forget-user-or-password
442 element host port))))
443 (delete-process (nnimap-process nnimap-object))
444 (setq nnimap-object nil))))
446 (when (nnimap-capability "QRESYNC")
447 (nnimap-command "ENABLE QRESYNC"))
448 (nnimap-process nnimap-object))))))))
450 (defun nnimap-quote-specials (string)
453 (goto-char (point-min))
454 (while (re-search-forward "[\\\"]" nil t)
460 (defun nnimap-find-parameter (parameter elems)
464 ((equal (car elem) parameter)
465 (setq result (cdr elem)))
466 ((and (equal (car elem) "OK")
468 (equal (caadr elem) parameter))
469 (setq result (cdr (cadr elem))))))
472 (deffoo nnimap-close-server (&optional server)
473 (when (nnoo-change-server 'nnimap server nil)
475 (delete-process (get-buffer-process (nnimap-buffer))))
476 (nnoo-close-server 'nnimap server)
479 (deffoo nnimap-request-close ()
482 (deffoo nnimap-server-opened (&optional server)
483 (and (nnoo-current-server-p 'nnimap server)
485 (gnus-buffer-live-p nntp-server-buffer)
486 (nnimap-find-connection nntp-server-buffer)))
488 (deffoo nnimap-status-message (&optional server)
489 nnimap-status-string)
491 (deffoo nnimap-request-article (article &optional group server to-buffer)
492 (with-current-buffer nntp-server-buffer
493 (let ((result (nnimap-possibly-change-group group server))
495 (when (stringp article)
496 (setq article (nnimap-find-article-by-message-id group article)))
500 (with-current-buffer (nnimap-buffer)
502 (when nnimap-fetch-partial-articles
503 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
504 (goto-char (point-min))
505 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
506 (setq structure (ignore-errors
507 (let ((start (point)))
509 (downcase-region start (point))
511 (read (current-buffer))))
512 parts (nnimap-find-wanted-parts structure))))
514 (nnimap-get-partial-article article parts structure)
515 (nnimap-get-whole-article article))
516 (let ((buffer (current-buffer)))
517 (with-current-buffer (or to-buffer nntp-server-buffer)
519 (insert-buffer-substring buffer)
520 (nnheader-ms-strip-cr)
521 (cons group article)))))))))
523 (deffoo nnimap-request-head (article &optional group server to-buffer)
524 (when (nnimap-possibly-change-group group server)
525 (with-current-buffer (nnimap-buffer)
526 (when (stringp article)
527 (setq article (nnimap-find-article-by-message-id group article)))
528 (nnimap-get-whole-article
529 article (format "UID FETCH %%d %s"
530 (nnimap-header-parameters)))
531 (let ((buffer (current-buffer)))
532 (with-current-buffer (or to-buffer nntp-server-buffer)
534 (insert-buffer-substring buffer)
535 (nnheader-ms-strip-cr)
536 (cons group article))))))
538 (defun nnimap-get-whole-article (article &optional command)
543 "UID FETCH %d BODY.PEEK[]"
544 "UID FETCH %d RFC822.PEEK"))
546 ;; Check that we really got an article.
547 (goto-char (point-min))
548 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
551 ;; Remove any data that may have arrived before the FETCH data.
554 (delete-region (point-min) (point)))
555 (let ((bytes (nnimap-get-length)))
556 (delete-region (line-beginning-position)
557 (progn (forward-line 1) (point)))
558 (goto-char (+ (point) bytes))
559 (delete-region (point) (point-max)))
562 (defun nnimap-capability (capability)
563 (member capability (nnimap-capabilities nnimap-object)))
565 (defun nnimap-ver4-p ()
566 (nnimap-capability "IMAP4REV1"))
568 (defun nnimap-get-partial-article (article parts structure)
571 "UID FETCH %d (%s %s)"
577 (mapconcat (lambda (part)
578 (format "BODY.PEEK[%s]" part))
580 (mapconcat (lambda (part)
581 (format "RFC822.PEEK[%s]" part))
584 (nnimap-convert-partial-article structure))))
586 (defun nnimap-convert-partial-article (structure)
587 ;; First just skip past the headers.
588 (goto-char (point-min))
589 (let ((bytes (nnimap-get-length))
591 ;; Delete "FETCH" line.
592 (delete-region (line-beginning-position)
593 (progn (forward-line 1) (point)))
594 (goto-char (+ (point) bytes))
595 ;; Collect all the body parts.
596 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
597 (setq id (match-string 1)
598 bytes (nnimap-get-length))
600 (delete-region (point) (progn (forward-line 1) (point)))
601 (push (list id (buffer-substring (point) (+ (point) bytes)))
603 (delete-region (point) (+ (point) bytes)))
604 ;; Delete trailing junk.
605 (delete-region (point) (point-max))
606 ;; Now insert all the parts again where they fit in the structure.
607 (nnimap-insert-partial-structure structure parts)
610 (defun nnimap-insert-partial-structure (structure parts &optional subp)
612 (let ((bstruc structure))
613 (while (consp (car bstruc))
615 (setq type (car bstruc))
616 (setq bstruc (car (cdr bstruc)))
617 (let ((has-boundary (member "boundary" bstruc)))
619 (setq boundary (cadr has-boundary)))))
621 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
622 (downcase type) boundary)))
623 (while (not (stringp (car structure)))
624 (insert "\n--" boundary "\n")
625 (if (consp (caar structure))
626 (nnimap-insert-partial-structure (pop structure) parts t)
627 (let ((bit (pop structure)))
628 (insert (format "Content-type: %s/%s"
629 (downcase (nth 0 bit))
630 (downcase (nth 1 bit))))
631 (if (member "CHARSET" (nth 2 bit))
633 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
635 (insert (format "Content-transfer-encoding: %s\n"
638 (when (assoc (nth 9 bit) parts)
639 (insert (cadr (assoc (nth 9 bit) parts)))))))
640 (insert "\n--" boundary "--\n")))
642 (defun nnimap-find-wanted-parts (structure)
643 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
645 (defun nnimap-find-wanted-parts-1 (structure prefix)
648 (while (consp (car structure))
649 (let ((sub (pop structure)))
650 (if (consp (car sub))
651 (push (nnimap-find-wanted-parts-1
652 sub (if (string= prefix "")
653 (number-to-string num)
654 (format "%s.%s" prefix num)))
656 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
657 (id (if (string= prefix "")
658 (number-to-string num)
659 (format "%s.%s" prefix num))))
660 (setcar (nthcdr 9 sub) id)
661 (when (if (eq nnimap-fetch-partial-articles t)
663 (string-match nnimap-fetch-partial-articles type))
668 (deffoo nnimap-request-group (group &optional server dont-check info)
669 (let ((result (nnimap-possibly-change-group
670 ;; Don't SELECT the group if we're going to select it
673 (assoc group nnimap-current-infos))
677 articles active marks high low)
678 (with-current-buffer nntp-server-buffer
681 (setq active (nth 2 (assoc group nnimap-current-infos))))
682 (insert (format "211 %d %d %d %S\n"
683 (- (cdr active) (car active))
687 (with-current-buffer (nnimap-buffer)
689 (let ((group-sequence
690 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
692 (nnimap-send-command "UID FETCH 1:* FLAGS")))
693 (setf (nnimap-group nnimap-object) group)
694 (nnimap-wait-for-response flag-sequence)
696 (nnimap-flags-to-marks
698 (list (list group-sequence flag-sequence
699 1 group "SELECT")))))
702 (nnimap-update-infos marks (list info))
703 (nnimap-store-info info (gnus-active (gnus-info-group info))))
704 (goto-char (point-max))
705 (let ((uidnext (nth 5 (car marks))))
706 (setq high (or (if uidnext
710 low (or (nth 4 (car marks)) uidnext 1)))))
714 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
717 (deffoo nnimap-request-create-group (group &optional server args)
718 (when (nnimap-possibly-change-group nil server)
719 (with-current-buffer (nnimap-buffer)
720 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
722 (deffoo nnimap-request-delete-group (group &optional force server)
723 (when (nnimap-possibly-change-group nil server)
724 (with-current-buffer (nnimap-buffer)
725 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
727 (deffoo nnimap-request-rename-group (group new-name &optional server)
728 (when (nnimap-possibly-change-group nil server)
729 (with-current-buffer (nnimap-buffer)
730 (nnimap-unselect-group)
731 (car (nnimap-command "RENAME %S %S"
732 (utf7-encode group t) (utf7-encode new-name t))))))
734 (defun nnimap-unselect-group ()
735 ;; Make sure we don't have this group open read/write by asking
736 ;; to examine a mailbox that doesn't exist. This seems to be
737 ;; the only way that allows us to reliably go back to unselected
739 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
741 (deffoo nnimap-request-expunge-group (group &optional server)
742 (when (nnimap-possibly-change-group group server)
743 (with-current-buffer (nnimap-buffer)
744 (car (nnimap-command "EXPUNGE")))))
746 (defun nnimap-get-flags (spec)
749 (with-current-buffer (nnimap-buffer)
751 (nnimap-wait-for-response (nnimap-send-command
752 "UID FETCH %s FLAGS" spec))
754 (subst-char-in-region (point-min) (point-max)
756 (goto-char (point-min))
757 (while (search-forward " FETCH " end t)
758 (setq elems (read (current-buffer)))
759 (push (cons (cadr (memq 'UID elems))
760 (cadr (memq 'FLAGS elems)))
762 (nreverse articles)))
764 (deffoo nnimap-close-group (group &optional server)
767 (deffoo nnimap-request-move-article (article group server accept-form
768 &optional last internal-move-group)
770 (mm-disable-multibyte)
771 (when (funcall (if internal-move-group
773 'nnimap-request-article)
774 article group server (current-buffer))
775 ;; If the move is internal (on the same server), just do it the easy
777 (let ((message-id (message-field-value "message-id")))
778 (if internal-move-group
780 (with-current-buffer (nnimap-buffer)
781 (nnimap-command "UID COPY %d %S"
783 (utf7-encode internal-move-group t)))))
785 (nnimap-delete-article article)
786 (cons internal-move-group
787 (nnimap-find-article-by-message-id
788 internal-move-group message-id))))
789 ;; Move the article to a different method.
790 (let ((result (eval accept-form)))
792 (nnimap-delete-article article)
795 (deffoo nnimap-request-expire-articles (articles group &optional server force)
799 ((not (nnimap-possibly-change-group group server))
802 (eq nnmail-expiry-target 'delete))
803 (unless (nnimap-delete-article (gnus-compress-sequence articles))
804 (nnheader-message 7 "Article marked for deletion, but not expunged."))
807 (let ((deletable-articles
809 (eq nnmail-expiry-wait 'immediate))
811 (gnus-sorted-intersection
813 (nnimap-find-expired-articles group)))))
814 (if (null deletable-articles)
816 (if (eq nnmail-expiry-target 'delete)
817 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
818 (setq deletable-articles
819 (nnimap-process-expiry-targets
820 deletable-articles group server)))
821 ;; Return the articles we didn't delete.
822 (gnus-sorted-complement articles deletable-articles))))))
824 (defun nnimap-process-expiry-targets (articles group server)
825 (let ((deleted-articles nil))
826 (dolist (article articles)
827 (let ((target nnmail-expiry-target))
829 (mm-disable-multibyte)
830 (when (nnimap-request-article article group server (current-buffer))
831 (nnheader-message 7 "Expiring article %s:%d" group article)
832 (when (functionp target)
833 (setq target (funcall target group)))
835 (not (eq target 'delete)))
836 (if (or (gnus-request-group target t)
837 (gnus-request-create-group target))
838 (nnmail-expiry-target-group target group)
841 (push article deleted-articles))))))
842 ;; Change back to the current group again.
843 (nnimap-possibly-change-group group server)
844 (setq deleted-articles (nreverse deleted-articles))
845 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
848 (defun nnimap-find-expired-articles (group)
849 (let ((cutoff (nnmail-expired-article-p group nil nil)))
850 (with-current-buffer (nnimap-buffer)
853 "UID SEARCH SENTBEFORE %s"
857 (car (rassoc (nth 4 (decode-time cutoff))
858 parse-time-months))))
861 (delete 0 (mapcar #'string-to-number
862 (cdr (assoc "SEARCH" (cdr result))))))))))
865 (defun nnimap-find-article-by-message-id (group message-id)
866 (with-current-buffer (nnimap-buffer)
868 (unless (equal group (nnimap-group nnimap-object))
869 (setf (nnimap-group nnimap-object) nil)
870 (nnimap-send-command "EXAMINE %S" (utf7-encode group t)))
872 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
874 (setq result (nnimap-wait-for-response sequence))
876 (car (setq result (nnimap-parse-response))))
877 ;; Select the last instance of the message in the group.
879 (car (last (assoc "SEARCH" (cdr result)))))
880 (string-to-number article))))))
882 (defun nnimap-delete-article (articles)
883 (with-current-buffer (nnimap-buffer)
884 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
885 (nnimap-article-ranges articles))
887 ((nnimap-capability "UIDPLUS")
888 (nnimap-command "UID EXPUNGE %s"
889 (nnimap-article-ranges articles))
892 (nnimap-command "EXPUNGE")
894 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
895 "server doesn't support UIDPLUS, so we won't "
896 "delete this article now"))))))
898 (deffoo nnimap-request-scan (&optional group server)
899 (when (and (nnimap-possibly-change-group nil server)
901 nnimap-split-methods)
902 (nnheader-message 7 "nnimap %s splitting mail..." server)
903 (nnimap-split-incoming-mail)))
905 (defun nnimap-marks-to-flags (marks)
908 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
912 (deffoo nnimap-request-set-mark (group actions &optional server)
913 (when (nnimap-possibly-change-group group server)
915 (with-current-buffer (nnimap-buffer)
917 ;; Just send all the STORE commands without waiting for
918 ;; response. If they're successful, they're successful.
919 (dolist (action actions)
920 (destructuring-bind (range action marks) action
921 (let ((flags (nnimap-marks-to-flags marks)))
923 (setq sequence (nnimap-send-command
924 "UID STORE %s %sFLAGS.SILENT (%s)"
925 (nnimap-article-ranges range)
929 (mapconcat #'identity flags " ")))))))
930 ;; Wait for the last command to complete to avoid later
931 ;; syncronisation problems with the stream.
933 (nnimap-wait-for-response sequence))))))
935 (deffoo nnimap-request-accept-article (group &optional server last)
936 (when (nnimap-possibly-change-group nil server)
937 (nnmail-check-syntax)
938 (let ((message-id (message-field-value "message-id"))
941 (setq message (buffer-substring-no-properties (point-min) (point-max)))
942 (with-current-buffer (nnimap-buffer)
944 (setq sequence (nnimap-send-command
945 "APPEND %S {%d}" (utf7-encode group t)
947 (unless nnimap-streaming
948 (nnimap-wait-for-connection "^[+]"))
949 (process-send-string (get-buffer-process (current-buffer)) message)
950 (process-send-string (get-buffer-process (current-buffer))
951 (if (nnimap-newlinep nnimap-object)
954 (let ((result (nnimap-get-response sequence)))
955 (if (not (car result))
957 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap))
960 (nnimap-find-article-by-message-id group message-id))))))))
962 (deffoo nnimap-request-replace-article (article group buffer)
964 (when (and (nnimap-possibly-change-group group nil)
965 ;; Put the article into the group.
966 (with-current-buffer buffer
968 (nnimap-request-accept-article group nil t))))
969 (nnimap-delete-article (list article))
970 ;; Return the new article number.
973 (defun nnimap-add-cr ()
974 (goto-char (point-min))
975 (while (re-search-forward "\r?\n" nil t)
976 (replace-match "\r\n" t t)))
978 (defun nnimap-get-groups ()
979 (let ((result (nnimap-command "LIST \"\" \"*\""))
982 (dolist (line (cdr result))
983 (when (and (equal (car line) "LIST")
984 (not (and (caadr line)
985 (string-match "noselect" (caadr line)))))
986 (push (car (last line)) groups)))
989 (deffoo nnimap-request-list (&optional server)
990 (nnimap-possibly-change-group nil server)
991 (with-current-buffer nntp-server-buffer
994 (with-current-buffer (nnimap-buffer)
995 (nnimap-get-groups)))
998 (with-current-buffer (nnimap-buffer)
999 (setf (nnimap-group nnimap-object) nil)
1000 (dolist (group groups)
1001 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
1004 (nnimap-wait-for-response (caar sequences))
1006 (nnimap-get-responses (mapcar #'car sequences))))
1007 (dolist (response responses)
1008 (let* ((sequence (car response))
1009 (response (cadr response))
1010 (group (cadr (assoc sequence sequences))))
1012 (equal (caar response) "OK"))
1013 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
1015 (dolist (elem response)
1016 (when (equal (cadr elem) "EXISTS")
1017 (setq exists (string-to-number (car elem)))))
1019 (setq highest (1- (string-to-number (car uidnext)))))
1022 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
1025 (insert (format "%S %d %d y\n"
1026 (utf7-decode group t) highest (1+ highest))))
1028 ;; Return the widest possible range.
1029 (insert (format "%S %d 1 y\n" (utf7-decode group t)
1030 (or highest exists)))))))))
1033 (deffoo nnimap-request-newgroups (date &optional server)
1034 (nnimap-possibly-change-group nil server)
1035 (with-current-buffer nntp-server-buffer
1037 (dolist (group (with-current-buffer (nnimap-buffer)
1038 (nnimap-get-groups)))
1039 (unless (assoc group nnimap-current-infos)
1040 ;; Insert dummy numbers here -- they don't matter.
1041 (insert (format "%S 0 1 y\n" group))))
1044 (deffoo nnimap-retrieve-group-data-early (server infos)
1045 (when (nnimap-possibly-change-group nil server)
1046 (with-current-buffer (nnimap-buffer)
1048 (setf (nnimap-group nnimap-object) nil)
1049 (let ((qresyncp (nnimap-capability "QRESYNC"))
1050 params groups sequences active uidvalidity modseq group)
1051 ;; Go through the infos and gather the data needed to know
1052 ;; what and how to request the data.
1053 (dolist (info infos)
1054 (setq params (gnus-info-params info)
1055 group (gnus-group-real-name (gnus-info-group info))
1056 active (cdr (assq 'active params))
1057 uidvalidity (cdr (assq 'uidvalidity params))
1058 modseq (cdr (assq 'modseq params)))
1063 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
1064 (utf7-encode group t)
1070 (if (and active uidvalidity)
1071 ;; Fetch the last 100 flags.
1072 (max 1 (- (cdr active) 100))
1077 ;; If we don't have a UIDVALIDITY, then this is
1078 ;; the first time we've seen the group, so we
1079 ;; have to do a SELECT (which is slower than an
1080 ;; examine), but will tell us whether the group
1081 ;; is read-only or not.
1083 (push (list (nnimap-send-command "%s %S" command
1084 (utf7-encode group t))
1085 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
1086 start group command)
1090 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1091 (when (and sequences
1092 (nnimap-possibly-change-group nil server))
1093 (with-current-buffer (nnimap-buffer)
1094 ;; Wait for the final data to trickle in.
1095 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1099 ;; Now we should have most of the data we need, no matter
1100 ;; whether we're QRESYNCING, fetching all the flags from
1101 ;; scratch, or just fetching the last 100 flags per group.
1102 (nnimap-update-infos (nnimap-flags-to-marks
1104 (nreverse sequences)))
1106 ;; Finally, just return something resembling an active file in
1107 ;; the nntp buffer, so that the agent can save the info, too.
1108 (with-current-buffer nntp-server-buffer
1110 (dolist (info infos)
1111 (let* ((group (gnus-info-group info))
1112 (active (gnus-active group)))
1114 (insert (format "%S %d %d y\n"
1115 (gnus-group-real-name group)
1117 (car active)))))))))))
1119 (defun nnimap-update-infos (flags infos)
1120 (dolist (info infos)
1121 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1122 (marks (cdr (assoc group flags))))
1124 (nnimap-update-info info marks)))))
1126 (defun nnimap-update-info (info marks)
1127 (destructuring-bind (existing flags high low uidnext start-article
1128 permanent-flags uidvalidity
1129 vanished highestmodseq) marks
1131 ;; Ignore groups with no UIDNEXT/marks. This happens for
1132 ;; completely empty groups.
1133 ((and (not existing)
1135 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1137 (gnus-set-active (gnus-info-group info) active))))
1138 ;; We have a mismatch between the old and new UIDVALIDITY
1139 ;; identifiers, so we have to re-request the group info (the next
1140 ;; time). This virtually never happens.
1141 ((let ((old-uidvalidity
1142 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1143 (and old-uidvalidity
1144 (not (equal old-uidvalidity uidvalidity))
1145 (> start-article 1)))
1146 (gnus-group-remove-parameter info 'uidvalidity)
1147 (gnus-group-remove-parameter info 'modseq))
1148 ;; We have the data needed to update.
1150 (let* ((group (gnus-info-group info))
1151 (completep (and start-article
1152 (= start-article 1)))
1153 (active (or (gnus-active group)
1154 (cdr (assq 'active (gnus-info-params info))))))
1156 (setq high (1- uidnext)))
1157 ;; First set the active ranges based on high/low.
1159 (not (gnus-active group)))
1160 (gnus-set-active group
1163 (cons (min (or low (car active))
1165 (max (or high (cdr active))
1170 ;; No articles in this group.
1171 (cons uidnext (1- uidnext)))
1173 (cons start-article (1- start-article)))
1175 ;; No articles and no uidnext.
1180 (or high (1- uidnext)))))
1181 ;; See whether this is a read-only group.
1182 (unless (eq permanent-flags 'not-scanned)
1183 (gnus-group-set-parameter
1184 info 'permanent-flags
1185 (and (or (memq '%* permanent-flags)
1186 (memq '%Seen permanent-flags))
1188 ;; Update marks and read articles if this isn't a
1189 ;; read-only IMAP group.
1190 (when (setq permanent-flags
1191 (cdr (assq 'permanent-flags (gnus-info-params info))))
1192 (if (and highestmodseq
1193 (not start-article))
1194 ;; We've gotten the data by QRESYNCing.
1195 (nnimap-update-qresync-info
1196 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1197 ;; Do normal non-QRESYNC flag updates.
1198 ;; Update the list of read articles.
1200 (gnus-compress-sequence
1201 (gnus-set-difference
1202 (gnus-set-difference
1204 (cdr (assoc '%Seen flags)))
1205 (cdr (assoc '%Flagged flags)))))
1206 (read (gnus-range-difference
1207 (cons start-article high) unread)))
1208 (when (> start-article 1)
1211 (if (> start-article 1)
1212 (gnus-sorted-range-intersection
1213 (cons 1 (1- start-article))
1214 (gnus-info-read info))
1215 (gnus-info-read info))
1217 (when (or (not (listp permanent-flags))
1218 (memq '%Seen permanent-flags))
1219 (gnus-info-set-read info read))
1220 ;; Update the marks.
1221 (setq marks (gnus-info-marks info))
1222 (dolist (type (cdr nnimap-mark-alist))
1223 (when (or (not (listp permanent-flags))
1224 (memq (car (assoc (caddr type) flags))
1226 (memq '%* permanent-flags))
1227 (let ((old-marks (assoc (car type) marks))
1229 (gnus-compress-sequence
1230 (cdr (or (assoc (caddr type) flags) ; %Flagged
1231 (assoc (intern (cadr type) obarray) flags)
1232 (assoc (cadr type) flags)))))) ; "\Flagged"
1233 (setq marks (delq old-marks marks))
1235 (when (and old-marks
1236 (> start-article 1))
1237 (setq old-marks (gnus-range-difference
1239 (cons start-article high)))
1240 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1242 (push (cons (car type) new-marks) marks)))))
1243 (gnus-info-set-marks info marks t))))
1244 ;; Note the active level for the next run-through.
1245 (gnus-group-set-parameter info 'active (gnus-active group))
1246 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1247 (gnus-group-set-parameter info 'modseq highestmodseq)
1248 (nnimap-store-info info (gnus-active group)))))))
1250 (defun nnimap-update-qresync-info (info existing vanished flags)
1251 ;; Add all the vanished articles to the list of read articles.
1256 (gnus-range-add (gnus-info-read info)
1258 (cdr (assq '%Flagged flags)))
1259 (cdr (assq '%Seen flags))))
1260 (let ((marks (gnus-info-marks info)))
1261 (dolist (type (cdr nnimap-mark-alist))
1262 (let ((ticks (assoc (car type) marks))
1264 (cdr (or (assoc (caddr type) flags) ; %Flagged
1265 (assoc (intern (cadr type) obarray) flags)
1266 (assoc (cadr type) flags))))) ; "\Flagged"
1267 (setq marks (delq ticks marks))
1269 ;; Add the new marks we got.
1270 (setq ticks (gnus-add-to-range ticks new-marks))
1271 ;; Remove the marks from messages that don't have them.
1272 (setq ticks (gnus-remove-from-range
1274 (gnus-compress-sequence
1275 (gnus-sorted-complement existing new-marks))))
1277 (push (cons (car type) ticks) marks)))
1278 (gnus-info-set-marks info marks t))))
1280 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1281 (if (zerop (length irange))
1284 (dolist (elem (split-string irange ","))
1286 (if (string-match ":" elem)
1287 (let ((numbers (split-string elem ":")))
1288 (cons (string-to-number (car numbers))
1289 (string-to-number (cadr numbers))))
1290 (string-to-number elem))
1292 (nreverse result))))
1294 (defun nnimap-store-info (info active)
1295 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1296 (entry (assoc group nnimap-current-infos)))
1298 (setcdr entry (list info active))
1299 (push (list group info active) nnimap-current-infos))))
1301 (defun nnimap-flags-to-marks (groups)
1302 (let (data group totalp uidnext articles start-article mark permanent-flags
1303 uidvalidity vanished highestmodseq)
1304 (dolist (elem groups)
1305 (setq group (car elem)
1306 uidnext (nth 1 elem)
1307 start-article (nth 2 elem)
1308 permanent-flags (nth 3 elem)
1309 uidvalidity (nth 4 elem)
1310 vanished (nth 5 elem)
1311 highestmodseq (nth 6 elem)
1312 articles (nthcdr 7 elem))
1313 (let ((high (caar articles))
1315 (dolist (article articles)
1316 (setq low (car article))
1317 (push (car article) existing)
1318 (dolist (flag (cdr article))
1319 (setq mark (assoc flag marks))
1321 (push (list flag (car article)) marks)
1322 (setcdr mark (cons (car article) (cdr mark))))))
1323 (push (list group existing marks high low uidnext start-article
1324 permanent-flags uidvalidity vanished highestmodseq)
1328 (defun nnimap-parse-flags (sequences)
1329 (goto-char (point-min))
1330 ;; Change \Delete etc to %Delete, so that the reader can read it.
1331 (subst-char-in-region (point-min) (point-max)
1333 (let (start end articles groups uidnext elems permanent-flags
1334 uidvalidity vanished highestmodseq)
1335 (dolist (elem sequences)
1336 (destructuring-bind (group-sequence flag-sequence totalp group command)
1338 (setq start (point))
1340 ;; The EXAMINE was successful.
1341 (search-forward (format "\n%d OK " group-sequence) nil t)
1346 (setq permanent-flags
1347 (if (equal command "SELECT")
1348 (and (search-forward "PERMANENTFLAGS "
1349 (or end (point-min)) t)
1350 (read (current-buffer)))
1354 (and (search-forward "UIDNEXT "
1355 (or end (point-min)) t)
1356 (read (current-buffer))))
1359 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1360 (or end (point-min)) t)
1361 ;; Store UIDVALIDITY as a string, as it's
1362 ;; too big for 32-bit Emacsen, usually.
1366 (and (eq flag-sequence 'qresync)
1367 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1368 (or end (point-min)) t)
1372 (and (search-forward "HIGHESTMODSEQ "
1373 (or end (point-min)) t)
1374 (read (current-buffer))))
1377 ;; The UID FETCH FLAGS was successful.
1378 (or (eq flag-sequence 'qresync)
1379 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1380 (if (eq flag-sequence 'qresync)
1384 (setq start (point))
1386 (while (re-search-forward "^\\* [0-9]+ FETCH " start t)
1387 (setq elems (read (current-buffer)))
1388 (push (cons (cadr (memq 'UID elems))
1389 (cadr (memq 'FLAGS elems)))
1391 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1392 vanished highestmodseq)
1396 (setq articles nil))))
1399 (defun nnimap-find-process-buffer (buffer)
1400 (cadr (assoc buffer nnimap-connection-alist)))
1402 (deffoo nnimap-request-post (&optional server)
1403 (setq nnimap-status-string "Read-only server")
1406 (deffoo nnimap-request-thread (id)
1407 (let* ((refs (split-string
1408 (or (mail-header-references (gnus-summary-article-header))
1412 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1414 (dolist (refid refs value)
1416 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1417 refid refid value)))))
1418 (result (with-current-buffer (nnimap-buffer)
1419 (nnimap-command "UID SEARCH %s" cmd))))
1421 (and (car result) (delete 0 (mapcar #'string-to-number
1422 (cdr (assoc "SEARCH" (cdr result))))))
1425 (defun nnimap-possibly-change-group (group server)
1426 (let ((open-result t))
1428 (not (nnimap-server-opened server)))
1429 (setq open-result (nnimap-open-server server)))
1436 (with-current-buffer (nnimap-buffer)
1437 (if (equal group (nnimap-group nnimap-object))
1439 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1441 (setf (nnimap-group nnimap-object) group
1442 (nnimap-select-result nnimap-object) result)
1445 (defun nnimap-find-connection (buffer)
1446 "Find the connection delivering to BUFFER."
1447 (let ((entry (assoc buffer nnimap-connection-alist)))
1449 (if (and (buffer-name (cadr entry))
1450 (get-buffer-process (cadr entry))
1451 (memq (process-status (get-buffer-process (cadr entry)))
1453 (get-buffer-process (cadr entry))
1454 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1457 (defvar nnimap-sequence 0)
1459 (defun nnimap-send-command (&rest args)
1460 (process-send-string
1461 (get-buffer-process (current-buffer))
1464 (incf nnimap-sequence)
1465 (apply #'format args)
1466 (if (nnimap-newlinep nnimap-object)
1469 ;; Some servers apparently can't have many outstanding
1470 ;; commands, so throttle them.
1471 (unless nnimap-streaming
1472 (nnimap-wait-for-response nnimap-sequence))
1475 (defun nnimap-log-command (command)
1476 (with-current-buffer (get-buffer-create "*imap log*")
1477 (goto-char (point-max))
1478 (insert (format-time-string "%H:%M:%S") " " command))
1481 (defun nnimap-command (&rest args)
1483 (setf (nnimap-last-command-time nnimap-object) (current-time))
1484 (let* ((sequence (apply #'nnimap-send-command args))
1485 (response (nnimap-get-response sequence)))
1486 (if (equal (caar response) "OK")
1488 (nnheader-report 'nnimap "%s"
1489 (mapconcat (lambda (a)
1491 (car response) " "))
1494 (defun nnimap-get-response (sequence)
1495 (nnimap-wait-for-response sequence)
1496 (nnimap-parse-response))
1498 (defun nnimap-wait-for-connection (&optional regexp)
1500 (setq regexp "^[*.] .*\n"))
1501 (let ((process (get-buffer-process (current-buffer))))
1502 (goto-char (point-min))
1503 (while (and (memq (process-status process)
1505 (not (re-search-forward regexp nil t)))
1506 (nnheader-accept-process-output process)
1507 (goto-char (point-min)))
1509 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1512 (defun nnimap-wait-for-response (sequence &optional messagep)
1513 (let ((process (get-buffer-process (current-buffer)))
1517 (goto-char (point-max))
1518 (while (and (setq openp (memq (process-status process)
1520 (not (re-search-backward
1521 (format "^%d .*\n" sequence)
1522 (if nnimap-streaming
1523 (max (point-min) (- (point) 500))
1527 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1528 (nnheader-accept-process-output process)
1529 (goto-char (point-max)))
1532 ;; The user hit C-g while we were waiting: kill the process, in case
1533 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1535 (delete-process process)
1538 (defun nnimap-parse-response ()
1539 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1541 (dolist (line lines)
1542 (push (cdr (nnimap-parse-line line)) result))
1543 ;; Return the OK/error code first, and then all the "continuation
1544 ;; lines" afterwards.
1546 (nreverse result))))
1548 ;; Parse an IMAP response line lightly. They look like
1549 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1550 ;; the lines into a list of strings and lists of string.
1551 (defun nnimap-parse-line (line)
1554 (mm-disable-multibyte)
1556 (goto-char (point-min))
1558 (if (eql (setq char (following-char)) ? )
1566 (if (search-forward "]" (line-end-position) 'move)
1573 (if (search-forward ")" (line-end-position) 'move)
1580 (1- (or (search-forward "\"" (line-end-position) 'move)
1583 (buffer-substring (point) (if (search-forward " " nil t)
1585 (goto-char (point-max))))))
1587 (nreverse result))))
1589 (defun nnimap-last-response-string ()
1592 (let ((end (point)))
1596 (while (and (not (bobp))
1597 (eql (following-char) ?*))
1599 (unless (eql (following-char) ?*)
1601 (buffer-substring (point) end))))
1603 (defun nnimap-get-responses (sequences)
1605 (dolist (sequence sequences)
1606 (goto-char (point-min))
1607 (when (re-search-forward (format "^%d " sequence) nil t)
1608 (push (list sequence (nnimap-parse-response))
1612 (defvar nnimap-incoming-split-list nil)
1614 (defun nnimap-fetch-inbox (articles)
1616 (nnimap-wait-for-response
1617 (nnimap-send-command
1619 (nnimap-article-ranges articles)
1620 (format "(UID %s%s)"
1623 "BODY.PEEK[HEADER] BODY.PEEK"
1625 (if nnimap-split-download-body-default
1630 (defun nnimap-split-incoming-mail ()
1631 (with-current-buffer (nnimap-buffer)
1632 (let ((nnimap-incoming-split-list nil)
1633 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1634 nnmail-split-methods
1635 nnimap-split-methods))
1636 (nnmail-split-fancy (or nnimap-split-fancy
1637 nnmail-split-fancy))
1638 (nnmail-inhibit-default-split-group t)
1639 (groups (nnimap-get-groups))
1642 (nnimap-command "SELECT %S" nnimap-inbox)
1643 (setf (nnimap-group nnimap-object) nnimap-inbox)
1644 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1646 (nnimap-fetch-inbox new-articles)
1647 (nnimap-transform-split-mail)
1648 (nnheader-ms-strip-cr)
1650 (nnmail-split-incoming (current-buffer)
1651 #'nnimap-save-mail-spec
1653 #'nnimap-dummy-active-number
1654 #'nnimap-save-mail-spec)
1655 (when nnimap-incoming-split-list
1656 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1657 sequences junk-articles)
1658 ;; Create any groups that doesn't already exist on the
1660 (dolist (spec specs)
1661 (when (and (not (member (car spec) groups))
1662 (not (eq (car spec) 'junk)))
1663 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1664 ;; Then copy over all the messages.
1666 (dolist (spec specs)
1667 (let ((group (car spec))
1668 (ranges (cdr spec)))
1669 (if (eq group 'junk)
1670 (setq junk-articles ranges)
1671 (push (list (nnimap-send-command
1673 (nnimap-article-ranges ranges)
1674 (utf7-encode group t))
1677 ;; Wait for the last COPY response...
1679 (nnimap-wait-for-response (caar sequences))
1680 ;; And then mark the successful copy actions as deleted,
1681 ;; and possibly expunge them.
1682 (nnimap-mark-and-expunge-incoming
1683 (nnimap-parse-copied-articles sequences)))
1684 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1686 (defun nnimap-mark-and-expunge-incoming (range)
1688 (setq range (nnimap-article-ranges range))
1691 (nnimap-send-command
1692 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1694 ;; If the server supports it, we now delete the message we have
1695 ;; just copied over.
1696 ((nnimap-capability "UIDPLUS")
1697 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1698 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1699 ;; user has configured it.
1701 (setq sequence (nnimap-send-command "EXPUNGE"))))
1702 (nnimap-wait-for-response sequence))))
1704 (defun nnimap-parse-copied-articles (sequences)
1705 (let (sequence copied range)
1706 (goto-char (point-min))
1707 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1708 (setq sequence (string-to-number (match-string 1)))
1709 (when (setq range (cadr (assq sequence sequences)))
1710 (push (gnus-uncompress-range range) copied)))
1711 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1713 (defun nnimap-new-articles (flags)
1715 (dolist (elem flags)
1716 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1718 (push (car elem) new)))
1719 (gnus-compress-sequence (nreverse new))))
1721 (defun nnimap-make-split-specs (list)
1725 (destructuring-bind (article spec) elem
1726 (dolist (group (delete nil (mapcar #'car spec)))
1727 (unless (setq entry (assoc group specs))
1728 (push (setq entry (list group)) specs))
1729 (setcdr entry (cons article (cdr entry))))))
1730 (dolist (entry specs)
1731 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1734 (defun nnimap-transform-split-mail ()
1735 (goto-char (point-min))
1736 (let (article bytes)
1739 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1740 (delete-region (point) (progn (forward-line 1) (point)))
1743 (setq article (match-string 1)
1744 bytes (nnimap-get-length))
1745 (delete-region (line-beginning-position) (line-end-position))
1746 ;; Insert MMDF separator, and a way to remember what this
1748 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1749 (forward-char (1+ bytes))
1750 (setq bytes (nnimap-get-length))
1751 (delete-region (line-beginning-position) (line-end-position))
1752 ;; There's a body; skip past that.
1754 (forward-char (1+ bytes))
1755 (delete-region (line-beginning-position) (line-end-position)))))))
1757 (defun nnimap-dummy-active-number (group &optional server)
1760 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1762 (goto-char (point-min))
1763 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1764 (error "Invalid nnimap mail")
1765 (setq article (string-to-number (match-string 1))))
1767 (if (eq group-art 'junk)
1768 (list (cons 'junk 1))
1770 nnimap-incoming-split-list)))
1774 ;;; nnimap.el ends here