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.
44 (autoload 'auth-source-forget-user-or-password "auth-source")
45 (autoload 'auth-source-user-or-password "auth-source")
49 (defvoo nnimap-address nil
50 "The address of the IMAP server.")
52 (defvoo nnimap-server-port nil
54 If nnimap-stream is `ssl', this will default to `imaps'. If not,
55 it will default to `imap'.")
57 (defvoo nnimap-stream 'ssl
58 "How nnimap will talk to the IMAP server.
59 Values are `ssl', `network', `starttls' or `shell'.")
61 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
62 (if (listp imap-shell-program)
63 (car imap-shell-program)
67 (defvoo nnimap-inbox nil
68 "The mail box where incoming mail arrives and should be split out of.")
70 (defvoo nnimap-split-methods nil
72 Uses the same syntax as nnmail-split-methods")
74 (defvoo nnimap-split-fancy nil
75 "Uses the same syntax as nnmail-split-fancy.")
77 (make-obsolete-variable 'nnimap-split-rule "see `nnimap-split-methods'"
80 (defvoo nnimap-authenticator nil
81 "How nnimap authenticate itself to the server.
82 Possible choices are nil (use default methods) or `anonymous'.")
84 (defvoo nnimap-expunge t
85 "If non-nil, expunge articles after deleting them.
86 This is always done if the server supports UID EXPUNGE, but it's
87 not done by default on servers that doesn't support that command.")
89 (defvoo nnimap-streaming t
90 "If non-nil, try to use streaming commands with IMAP servers.
91 Switching this off will make nnimap slower, but it helps with
94 (defvoo nnimap-connection-alist nil)
96 (defvoo nnimap-current-infos nil)
98 (defvoo nnimap-fetch-partial-articles nil
99 "If non-nil, Gnus will fetch partial articles.
100 If t, nnimap will fetch only the first part. If a string, it
101 will fetch all parts that have types that match that string. A
102 likely value would be \"text/\" to automatically fetch all
105 (defvar nnimap-process nil)
107 (defvar nnimap-status-string "")
109 (defvar nnimap-split-download-body-default nil
110 "Internal variable with default value for `nnimap-split-download-body'.")
112 (defvar nnimap-keepalive-timer nil)
113 (defvar nnimap-process-buffers nil)
116 group process commands capabilities select-result newlinep server
117 last-command-time greeting)
119 (defvar nnimap-object nil)
121 (defvar nnimap-mark-alist
122 '((read "\\Seen" %Seen)
123 (tick "\\Flagged" %Flagged)
124 (reply "\\Answered" %Answered)
125 (expire "gnus-expire")
126 (dormant "gnus-dormant")
129 (download "gnus-download")
130 (forward "gnus-forward")))
132 (defun nnimap-buffer ()
133 (nnimap-find-process-buffer nntp-server-buffer))
135 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
136 (with-current-buffer nntp-server-buffer
138 (when (nnimap-possibly-change-group group server)
139 (with-current-buffer (nnimap-buffer)
141 (nnimap-wait-for-response
144 (nnimap-article-ranges (gnus-compress-sequence articles))
145 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
148 "BODY.PEEK[HEADER.FIELDS %s]"
149 "RFC822.HEADER.LINES %s")
150 (append '(Subject From Date Message-Id
151 References In-Reply-To Xref)
152 nnmail-extra-headers))))
154 (nnimap-transform-headers))
155 (insert-buffer-substring
156 (nnimap-find-process-buffer (current-buffer))))
159 (defun nnimap-transform-headers ()
160 (goto-char (point-min))
161 (let (article bytes lines size string)
164 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
165 (delete-region (point) (progn (forward-line 1) (point)))
168 (setq article (match-string 1))
169 ;; Unfold quoted {number} strings.
170 (while (re-search-forward "[^]] {\\([0-9]+\\)}\r\n"
171 (1+ (line-end-position)) t)
172 (setq size (string-to-number (match-string 1)))
173 (delete-region (+ (match-beginning 0) 2) (point))
174 (setq string (delete-region (point) (+ (point) size)))
175 (insert (format "%S" string)))
176 (setq bytes (nnimap-get-length)
180 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
185 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
186 (let ((structure (ignore-errors
187 (read (current-buffer)))))
188 (while (and (consp structure)
189 (not (stringp (car structure))))
190 (setq structure (car structure)))
191 (setq lines (nth 7 structure))))
192 (delete-region (line-beginning-position) (line-end-position))
193 (insert (format "211 %s Article retrieved." article))
196 (insert (format "Chars: %s\n" size)))
198 (insert (format "Lines: %s\n" lines)))
199 (re-search-forward "^\r$")
200 (delete-region (line-beginning-position) (line-end-position))
204 (defun nnimap-get-length ()
205 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
206 (string-to-number (match-string 1))))
208 (defun nnimap-article-ranges (ranges)
212 (number-to-string ranges))
213 ((numberp (cdr ranges))
214 (format "%d:%d" (car ranges) (cdr ranges)))
216 (dolist (elem ranges)
219 (format "%d:%d" (car elem) (cdr elem))
220 (number-to-string elem))
222 (mapconcat #'identity (nreverse result) ",")))))
224 (deffoo nnimap-open-server (server &optional defs)
225 (if (nnimap-server-opened server)
227 (unless (assq 'nnimap-address defs)
228 (setq defs (append defs (list (list 'nnimap-address server)))))
229 (nnoo-change-server 'nnimap server defs)
230 (or (nnimap-find-connection nntp-server-buffer)
231 (nnimap-open-connection nntp-server-buffer))))
233 (defun nnimap-make-process-buffer (buffer)
235 (generate-new-buffer (format "*nnimap %s %s %s*"
236 nnimap-address nnimap-server-port
237 (gnus-buffer-exists-p buffer)))
238 (mm-disable-multibyte)
239 (buffer-disable-undo)
241 (set (make-local-variable 'after-change-functions) nil)
242 (set (make-local-variable 'nnimap-object)
243 (make-nnimap :server (nnoo-current-server 'nnimap)))
244 (push (list buffer (current-buffer)) nnimap-connection-alist)
245 (push (current-buffer) nnimap-process-buffers)
248 (defun nnimap-open-shell-stream (name buffer host port)
249 (let ((process-connection-type nil))
250 (start-process name buffer shell-file-name
258 (defun nnimap-credentials (address ports &optional inhibit-create)
259 (let (port credentials)
260 ;; Request the credentials from all ports, but only query on the
261 ;; last port if all the previous ones have failed.
262 (while (and (null credentials)
263 (setq port (pop ports)))
265 (auth-source-user-or-password
266 '("login" "password") address port nil
272 (defun nnimap-keepalive ()
273 (let ((now (current-time)))
274 (dolist (buffer nnimap-process-buffers)
275 (when (buffer-name buffer)
276 (with-current-buffer buffer
277 (when (and nnimap-object
278 (nnimap-last-command-time nnimap-object)
282 (nnimap-last-command-time nnimap-object)))
283 ;; More than five minutes since the last command.
285 (nnimap-send-command "NOOP")))))))
287 (defun nnimap-open-connection (buffer)
288 (unless nnimap-keepalive-timer
289 (setq nnimap-keepalive-timer (run-at-time (* 60 15) (* 60 15)
292 (with-current-buffer (nnimap-make-process-buffer buffer)
293 (let* ((coding-system-for-read 'binary)
294 (coding-system-for-write 'binary)
298 ((or (eq nnimap-stream 'network)
299 (and (eq nnimap-stream 'starttls)
300 (fboundp 'open-gnutls-stream)))
302 "*nnimap*" (current-buffer) nnimap-address
304 (or nnimap-server-port
305 (if (netrc-find-service-number "imap")
309 ((eq nnimap-stream 'shell)
310 (nnimap-open-shell-stream
311 "*nnimap*" (current-buffer) nnimap-address
312 (setq port (or nnimap-server-port "imap")))
314 ((eq nnimap-stream 'starttls)
315 (let ((tls-program (nnimap-extend-tls-programs)))
317 "*nnimap*" (current-buffer) nnimap-address
318 (setq port (or nnimap-server-port "imap"))
321 ((memq nnimap-stream '(ssl tls))
322 (funcall (if (fboundp 'open-gnutls-stream)
325 "*nnimap*" (current-buffer) nnimap-address
327 (or nnimap-server-port
328 (if (netrc-find-service-number "imaps")
331 '("143" "993" "imap" "imaps"))
333 (error "Unknown stream type: %s" nnimap-stream))))
334 connection-result login-result credentials)
335 (setf (nnimap-process nnimap-object)
336 (get-buffer-process (current-buffer)))
337 (if (not (and (nnimap-process nnimap-object)
338 (memq (process-status (nnimap-process nnimap-object))
340 (nnheader-report 'nnimap "Unable to contact %s:%s via %s"
341 nnimap-address port nnimap-stream)
342 (gnus-set-process-query-on-exit-flag
343 (nnimap-process nnimap-object) nil)
344 (if (not (setq connection-result (nnimap-wait-for-connection)))
345 (nnheader-report 'nnimap
346 "%s" (buffer-substring
347 (point) (line-end-position)))
348 ;; Store the greeting (for debugging purposes).
349 (setf (nnimap-greeting nnimap-object)
350 (buffer-substring (line-beginning-position)
351 (line-end-position)))
352 ;; Store the capabilities.
353 (setf (nnimap-capabilities nnimap-object)
356 (nnimap-find-parameter
357 "CAPABILITY" (cdr (nnimap-command "CAPABILITY")))))
358 (when nnimap-server-port
359 (push (format "%s" nnimap-server-port) ports))
360 ;; If this is a STARTTLS-capable server, then sever the
361 ;; connection and start a STARTTLS connection instead.
363 ((and (or (and (eq nnimap-stream 'network)
365 (nnimap-capabilities nnimap-object)))
366 (eq nnimap-stream 'starttls))
367 (fboundp 'open-gnutls-stream))
368 (nnimap-command "STARTTLS")
369 (gnutls-negotiate (nnimap-process nnimap-object) nil))
370 ((and (eq nnimap-stream 'network)
371 (member "STARTTLS" (nnimap-capabilities nnimap-object)))
372 (let ((nnimap-stream 'starttls))
374 (nnimap-open-connection buffer)))
375 ;; If the STARTTLS connection was successful, we
376 ;; kill our first non-encrypted connection. If it
377 ;; wasn't successful, we just use our unencrypted
379 (when (memq (process-status tls-process) '(open run))
380 (delete-process (nnimap-process nnimap-object))
381 (kill-buffer (current-buffer))
382 (return tls-process))))))
383 (unless (equal connection-result "PREAUTH")
384 (if (not (setq credentials
385 (if (eq nnimap-authenticator 'anonymous)
387 (message-make-address))
389 ;; First look for the credentials based
390 ;; on the virtual server name.
392 (nnoo-current-server 'nnimap) ports t)
393 ;; Then look them up based on the
395 (nnimap-credentials nnimap-address ports)))))
396 (setq nnimap-object nil)
397 (setq login-result (nnimap-command "LOGIN %S %S"
400 (unless (car login-result)
401 ;; If the login failed, then forget the credentials
402 ;; that are now possibly cached.
403 (dolist (host (list (nnoo-current-server 'nnimap)
406 (dolist (element '("login" "password"))
407 (auth-source-forget-user-or-password
408 element host port))))
409 (delete-process (nnimap-process nnimap-object))
410 (setq nnimap-object nil))))
412 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
413 (nnimap-command "ENABLE QRESYNC"))
414 (nnimap-process nnimap-object))))))))
416 (defun nnimap-extend-tls-programs ()
417 (let ((programs tls-program)
419 (unless (consp programs)
420 (setq programs (list programs)))
421 (dolist (program programs)
422 (when (assoc (car (split-string program)) tls-starttls-switches)
423 (push (if (not (string-match "%s" program))
424 (concat program " " "%s")
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))))
447 (deffoo nnimap-request-close ()
450 (deffoo nnimap-server-opened (&optional server)
451 (and (nnoo-current-server-p 'nnimap server)
453 (gnus-buffer-live-p nntp-server-buffer)
454 (nnimap-find-connection nntp-server-buffer)))
456 (deffoo nnimap-status-message (&optional server)
457 nnimap-status-string)
459 (deffoo nnimap-request-article (article &optional group server to-buffer)
460 (with-current-buffer nntp-server-buffer
461 (let ((result (nnimap-possibly-change-group group server))
463 (when (stringp article)
464 (setq article (nnimap-find-article-by-message-id group article)))
468 (with-current-buffer (nnimap-buffer)
470 (when nnimap-fetch-partial-articles
471 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
472 (goto-char (point-min))
473 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
474 (setq structure (ignore-errors
475 (let ((start (point)))
477 (downcase-region start (point))
479 (read (current-buffer))))
480 parts (nnimap-find-wanted-parts structure))))
482 (nnimap-get-partial-article article parts structure)
483 (nnimap-get-whole-article article))
484 (let ((buffer (current-buffer)))
485 (with-current-buffer (or to-buffer nntp-server-buffer)
487 (insert-buffer-substring buffer)
488 (nnheader-ms-strip-cr)
489 (cons group article)))))))))
491 (defun nnimap-get-whole-article (article)
495 "UID FETCH %d BODY.PEEK[]"
496 "UID FETCH %d RFC822.PEEK")
498 ;; Check that we really got an article.
499 (goto-char (point-min))
500 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t)
503 ;; Remove any data that may have arrived before the FETCH data.
506 (delete-region (point-min) (point)))
507 (let ((bytes (nnimap-get-length)))
508 (delete-region (line-beginning-position)
509 (progn (forward-line 1) (point)))
510 (goto-char (+ (point) bytes))
511 (delete-region (point) (point-max)))
514 (defun nnimap-ver4-p ()
515 (member "IMAP4REV1" (nnimap-capabilities nnimap-object)))
517 (defun nnimap-get-partial-article (article parts structure)
520 "UID FETCH %d (%s %s)"
526 (mapconcat (lambda (part)
527 (format "BODY.PEEK[%s]" part))
529 (mapconcat (lambda (part)
530 (format "RFC822.PEEK[%s]" part))
533 (nnimap-convert-partial-article structure))))
535 (defun nnimap-convert-partial-article (structure)
536 ;; First just skip past the headers.
537 (goto-char (point-min))
538 (let ((bytes (nnimap-get-length))
540 ;; Delete "FETCH" line.
541 (delete-region (line-beginning-position)
542 (progn (forward-line 1) (point)))
543 (goto-char (+ (point) bytes))
544 ;; Collect all the body parts.
545 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
546 (setq id (match-string 1)
547 bytes (nnimap-get-length))
549 (delete-region (point) (progn (forward-line 1) (point)))
550 (push (list id (buffer-substring (point) (+ (point) bytes)))
552 (delete-region (point) (+ (point) bytes)))
553 ;; Delete trailing junk.
554 (delete-region (point) (point-max))
555 ;; Now insert all the parts again where they fit in the structure.
556 (nnimap-insert-partial-structure structure parts)
559 (defun nnimap-insert-partial-structure (structure parts &optional subp)
561 (let ((bstruc structure))
562 (while (consp (car bstruc))
564 (setq type (car bstruc))
565 (setq bstruc (car (cdr bstruc)))
566 (when (and (stringp (car bstruc))
567 (string= (downcase (car bstruc)) "boundary"))
568 (setq boundary (cadr bstruc))))
570 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
571 (downcase type) boundary)))
572 (while (not (stringp (car structure)))
573 (insert "\n--" boundary "\n")
574 (if (consp (caar structure))
575 (nnimap-insert-partial-structure (pop structure) parts t)
576 (let ((bit (pop structure)))
577 (insert (format "Content-type: %s/%s"
578 (downcase (nth 0 bit))
579 (downcase (nth 1 bit))))
580 (if (member "CHARSET" (nth 2 bit))
582 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit)))))
584 (insert (format "Content-transfer-encoding: %s\n"
587 (when (assoc (nth 9 bit) parts)
588 (insert (cadr (assoc (nth 9 bit) parts)))))))
589 (insert "\n--" boundary "--\n")))
591 (defun nnimap-find-wanted-parts (structure)
592 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
594 (defun nnimap-find-wanted-parts-1 (structure prefix)
597 (while (consp (car structure))
598 (let ((sub (pop structure)))
599 (if (consp (car sub))
600 (push (nnimap-find-wanted-parts-1
601 sub (if (string= prefix "")
602 (number-to-string num)
603 (format "%s.%s" prefix num)))
605 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub)))
606 (id (if (string= prefix "")
607 (number-to-string num)
608 (format "%s.%s" prefix num))))
609 (setcar (nthcdr 9 sub) id)
610 (when (if (eq nnimap-fetch-partial-articles t)
612 (string-match nnimap-fetch-partial-articles type))
617 (deffoo nnimap-request-group (group &optional server dont-check info)
618 (let ((result (nnimap-possibly-change-group group server))
619 articles active marks high low)
620 (with-current-buffer nntp-server-buffer
623 (setq active (nth 2 (assoc group nnimap-current-infos))))
624 (insert (format "211 %d %d %d %S\n"
625 (- (cdr active) (car active))
629 (with-current-buffer (nnimap-buffer)
631 (let ((group-sequence
632 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
634 (nnimap-send-command "UID FETCH 1:* FLAGS")))
635 (nnimap-wait-for-response flag-sequence)
637 (nnimap-flags-to-marks
639 (list (list group-sequence flag-sequence
640 1 group "SELECT")))))
643 (nnimap-update-infos marks (list info)))
644 (goto-char (point-max))
645 (let ((uidnext (nth 5 (car marks))))
646 (setq high (or (if uidnext
650 low (or (nth 4 (car marks)) uidnext 1)))))
654 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
657 (deffoo nnimap-request-create-group (group &optional server args)
658 (when (nnimap-possibly-change-group nil server)
659 (with-current-buffer (nnimap-buffer)
660 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
662 (deffoo nnimap-request-delete-group (group &optional force server)
663 (when (nnimap-possibly-change-group nil server)
664 (with-current-buffer (nnimap-buffer)
665 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
667 (deffoo nnimap-request-rename-group (group new-name &optional server)
668 (when (nnimap-possibly-change-group nil server)
669 (with-current-buffer (nnimap-buffer)
670 (car (nnimap-command "RENAME %S %S"
671 (utf7-encode group t) (utf7-encode new-name t))))))
673 (deffoo nnimap-request-expunge-group (group &optional server)
674 (when (nnimap-possibly-change-group group server)
675 (with-current-buffer (nnimap-buffer)
676 (car (nnimap-command "EXPUNGE")))))
678 (defun nnimap-get-flags (spec)
681 (with-current-buffer (nnimap-buffer)
683 (nnimap-wait-for-response (nnimap-send-command
684 "UID FETCH %s FLAGS" spec))
686 (subst-char-in-region (point-min) (point-max)
688 (goto-char (point-min))
689 (while (search-forward " FETCH " end t)
690 (setq elems (read (current-buffer)))
691 (push (cons (cadr (memq 'UID elems))
692 (cadr (memq 'FLAGS elems)))
694 (nreverse articles)))
696 (deffoo nnimap-close-group (group &optional server)
699 (deffoo nnimap-request-move-article (article group server accept-form
700 &optional last internal-move-group)
702 (when (nnimap-request-article article group server (current-buffer))
703 ;; If the move is internal (on the same server), just do it the easy
705 (let ((message-id (message-field-value "message-id")))
706 (if internal-move-group
708 (with-current-buffer (nnimap-buffer)
709 (nnimap-command "UID COPY %d %S"
711 (utf7-encode internal-move-group t)))))
713 (nnimap-delete-article article)
714 (cons internal-move-group
715 (nnimap-find-article-by-message-id
716 internal-move-group message-id))))
717 ;; Move the article to a different method.
718 (let ((result (eval accept-form)))
720 (nnimap-delete-article article)
723 (deffoo nnimap-request-expire-articles (articles group &optional server force)
727 ((not (nnimap-possibly-change-group group server))
730 (eq nnmail-expiry-target 'delete))
731 (unless (nnimap-delete-article (gnus-compress-sequence articles))
732 (message "Article marked for deletion, but not expunged."))
735 (let ((deletable-articles
737 (eq nnmail-expiry-wait 'immediate))
739 (gnus-sorted-intersection
741 (nnimap-find-expired-articles group)))))
742 (if (null deletable-articles)
744 (if (eq nnmail-expiry-target 'delete)
745 (nnimap-delete-article (gnus-compress-sequence deletable-articles))
746 (setq deletable-articles
747 (nnimap-process-expiry-targets
748 deletable-articles group server)))
749 ;; Return the articles we didn't delete.
750 (gnus-sorted-complement articles deletable-articles))))))
752 (defun nnimap-process-expiry-targets (articles group server)
753 (let ((deleted-articles nil))
754 (dolist (article articles)
755 (let ((target nnmail-expiry-target))
757 (when (nnimap-request-article article group server (current-buffer))
758 (message "Expiring article %s:%d" group article)
759 (when (functionp target)
760 (setq target (funcall target group)))
762 (not (eq target 'delete)))
763 (if (or (gnus-request-group target t)
764 (gnus-request-create-group target))
765 (nnmail-expiry-target-group target group)
768 (push article deleted-articles))))))
769 ;; Change back to the current group again.
770 (nnimap-possibly-change-group group server)
771 (setq deleted-articles (nreverse deleted-articles))
772 (nnimap-delete-article (gnus-compress-sequence deleted-articles))
775 (defun nnimap-find-expired-articles (group)
776 (let ((cutoff (nnmail-expired-article-p group nil nil)))
777 (with-current-buffer (nnimap-buffer)
780 "UID SEARCH SENTBEFORE %s"
784 (car (rassoc (nth 4 (decode-time cutoff))
785 parse-time-months))))
788 (delete 0 (mapcar #'string-to-number
789 (cdr (assoc "SEARCH" (cdr result))))))))))
792 (defun nnimap-find-article-by-message-id (group message-id)
793 (with-current-buffer (nnimap-buffer)
795 (setf (nnimap-group nnimap-object) nil)
796 (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
798 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id))
800 (setq result (nnimap-wait-for-response sequence))
802 (car (setq result (nnimap-parse-response))))
803 ;; Select the last instance of the message in the group.
805 (car (last (assoc "SEARCH" (cdr result)))))
806 (string-to-number article))))))
808 (defun nnimap-delete-article (articles)
809 (with-current-buffer (nnimap-buffer)
810 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
811 (nnimap-article-ranges articles))
813 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
814 (nnimap-command "UID EXPUNGE %s"
815 (nnimap-article-ranges articles))
818 (nnimap-command "EXPUNGE")
820 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
821 "server doesn't support UIDPLUS, so we won't "
822 "delete this article now"))))))
824 (deffoo nnimap-request-scan (&optional group server)
825 (when (and (nnimap-possibly-change-group nil server)
827 nnimap-split-methods)
828 (message "nnimap %s splitting mail..." server)
829 (nnimap-split-incoming-mail)))
831 (defun nnimap-marks-to-flags (marks)
834 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
838 (deffoo nnimap-request-set-mark (group actions &optional server)
839 (when (nnimap-possibly-change-group group server)
841 (with-current-buffer (nnimap-buffer)
843 ;; Just send all the STORE commands without waiting for
844 ;; response. If they're successful, they're successful.
845 (dolist (action actions)
846 (destructuring-bind (range action marks) action
847 (let ((flags (nnimap-marks-to-flags marks)))
849 (setq sequence (nnimap-send-command
850 "UID STORE %s %sFLAGS.SILENT (%s)"
851 (nnimap-article-ranges range)
855 (mapconcat #'identity flags " ")))))))
856 ;; Wait for the last command to complete to avoid later
857 ;; syncronisation problems with the stream.
859 (nnimap-wait-for-response sequence))))))
861 (deffoo nnimap-request-accept-article (group &optional server last)
862 (when (nnimap-possibly-change-group nil server)
863 (nnmail-check-syntax)
864 (let ((message-id (message-field-value "message-id"))
867 (setq message (buffer-string))
868 (with-current-buffer (nnimap-buffer)
869 (setq sequence (nnimap-send-command
870 "APPEND %S {%d}" (utf7-encode group t)
872 (process-send-string (get-buffer-process (current-buffer)) message)
873 (process-send-string (get-buffer-process (current-buffer))
874 (if (nnimap-newlinep nnimap-object)
877 (let ((result (nnimap-get-response sequence)))
878 (if (not (car result))
880 (message "%s" (nnheader-get-report-string 'nnimap))
883 (nnimap-find-article-by-message-id group message-id))))))))
885 (defun nnimap-add-cr ()
886 (goto-char (point-min))
887 (while (re-search-forward "\r?\n" nil t)
888 (replace-match "\r\n" t t)))
890 (defun nnimap-get-groups ()
891 (let ((result (nnimap-command "LIST \"\" \"*\""))
894 (dolist (line (cdr result))
895 (when (and (equal (car line) "LIST")
896 (not (and (caadr line)
897 (string-match "noselect" (caadr line)))))
898 (push (car (last line)) groups)))
901 (deffoo nnimap-request-list (&optional server)
902 (nnimap-possibly-change-group nil server)
903 (with-current-buffer nntp-server-buffer
906 (with-current-buffer (nnimap-buffer)
907 (nnimap-get-groups)))
910 (with-current-buffer (nnimap-buffer)
911 (setf (nnimap-group nnimap-object) nil)
912 (dolist (group groups)
913 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
916 (nnimap-wait-for-response (caar sequences))
918 (nnimap-get-responses (mapcar #'car sequences))))
919 (dolist (response responses)
920 (let* ((sequence (car response))
921 (response (cadr response))
922 (group (cadr (assoc sequence sequences))))
924 (equal (caar response) "OK"))
925 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
927 (dolist (elem response)
928 (when (equal (cadr elem) "EXISTS")
929 (setq exists (string-to-number (car elem)))))
931 (setq highest (1- (string-to-number (car uidnext)))))
934 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
937 (insert (format "%S %d %d y\n"
938 (utf7-decode group t) highest (1+ highest))))
940 ;; Return the widest possible range.
941 (insert (format "%S %d 1 y\n" (utf7-decode group t)
942 (or highest exists)))))))))
945 (deffoo nnimap-request-newgroups (date &optional server)
946 (nnimap-possibly-change-group nil server)
947 (with-current-buffer nntp-server-buffer
949 (dolist (group (with-current-buffer (nnimap-buffer)
950 (nnimap-get-groups)))
951 (unless (assoc group nnimap-current-infos)
952 ;; Insert dummy numbers here -- they don't matter.
953 (insert (format "%S 0 1 y\n" group))))
956 (deffoo nnimap-retrieve-group-data-early (server infos)
957 (when (nnimap-possibly-change-group nil server)
958 (with-current-buffer (nnimap-buffer)
960 (setf (nnimap-group nnimap-object) nil)
961 ;; QRESYNC handling isn't implemented.
962 (let ((qresyncp (member "QRESYNC" (nnimap-capabilities nnimap-object)))
963 params groups sequences active uidvalidity modseq group)
964 ;; Go through the infos and gather the data needed to know
965 ;; what and how to request the data.
967 (setq params (gnus-info-params info)
968 group (gnus-group-real-name (gnus-info-group info))
969 active (cdr (assq 'active params))
970 uidvalidity (cdr (assq 'uidvalidity params))
971 modseq (cdr (assq 'modseq params)))
976 (list (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
977 group uidvalidity modseq)
982 (if (and active uidvalidity)
983 ;; Fetch the last 100 flags.
984 (max 1 (- (cdr active) 100))
989 ;; If we don't have a UIDVALIDITY, then this is
990 ;; the first time we've seen the group, so we
991 ;; have to do a SELECT (which is slower than an
992 ;; examine), but will tell us whether the group
993 ;; is read-only or not.
995 (push (list (nnimap-send-command "%s %S" command group)
996 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
999 ;; Some servers apparently can't have many outstanding
1000 ;; commands, so throttle them.
1001 (when (and (not nnimap-streaming)
1003 (nnimap-wait-for-response (caar sequences))))
1006 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
1007 (when (and sequences
1008 (nnimap-possibly-change-group nil server))
1009 (with-current-buffer (nnimap-buffer)
1010 ;; Wait for the final data to trickle in.
1011 (when (nnimap-wait-for-response (if (eq (cadar sequences) 'qresync)
1015 ;; Now we should have most of the data we need, no matter
1016 ;; whether we're QRESYNCING, fetching all the flags from
1017 ;; scratch, or just fetching the last 100 flags per group.
1018 (nnimap-update-infos (nnimap-flags-to-marks
1020 (nreverse sequences)))
1022 ;; Finally, just return something resembling an active file in
1023 ;; the nntp buffer, so that the agent can save the info, too.
1024 (with-current-buffer nntp-server-buffer
1026 (dolist (info infos)
1027 (let* ((group (gnus-info-group info))
1028 (active (gnus-active group)))
1030 (insert (format "%S %d %d y\n"
1031 (gnus-group-real-name group)
1033 (car active)))))))))))
1035 (defun nnimap-update-infos (flags infos)
1036 (dolist (info infos)
1037 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1038 (marks (cdr (assoc group flags))))
1040 (nnimap-update-info info marks)))))
1042 (defun nnimap-update-info (info marks)
1043 (destructuring-bind (existing flags high low uidnext start-article
1044 permanent-flags uidvalidity
1045 vanished highestmodseq) marks
1047 ;; Ignore groups with no UIDNEXT/marks. This happens for
1048 ;; completely empty groups.
1049 ((and (not existing)
1051 (let ((active (cdr (assq 'active (gnus-info-params info)))))
1053 (gnus-set-active (gnus-info-group info) active))))
1054 ;; We have a mismatch between the old and new UIDVALIDITY
1055 ;; identifiers, so we have to re-request the group info (the next
1056 ;; time). This virtually never happens.
1057 ((let ((old-uidvalidity
1058 (cdr (assq 'uidvalidity (gnus-info-params info)))))
1059 (and old-uidvalidity
1060 (not (equal old-uidvalidity uidvalidity))
1061 (> start-article 1)))
1062 (gnus-group-remove-parameter info 'uidvalidity)
1063 (gnus-group-remove-parameter info 'modseq))
1064 ;; We have the data needed to update.
1066 (let* ((group (gnus-info-group info))
1067 (completep (and start-article
1068 (= start-article 1)))
1069 (active (or (gnus-active group)
1070 (cdr (assq 'active (gnus-info-params info))))))
1072 (setq high (1- uidnext)))
1073 ;; First set the active ranges based on high/low.
1075 (not (gnus-active group)))
1076 (gnus-set-active group
1081 ;; No articles in this group.
1082 (cons uidnext (1- uidnext)))
1086 (cons start-article (1- start-article)))
1088 ;; No articles and no uidnext.
1093 (or high (1- uidnext)))))
1094 ;; See whether this is a read-only group.
1095 (unless (eq permanent-flags 'not-scanned)
1096 (gnus-group-set-parameter
1097 info 'permanent-flags
1098 (if (memq '%* permanent-flags)
1101 ;; Update marks and read articles if this isn't a
1102 ;; read-only IMAP group.
1103 (when (cdr (assq 'permanent-flags (gnus-info-params info)))
1104 (if (and highestmodseq
1105 (not start-article))
1106 ;; We've gotten the data by QRESYNCing.
1107 (nnimap-update-qresync-info
1108 info existing (nnimap-imap-ranges-to-gnus-ranges vanished) flags)
1109 ;; Do normal non-QRESYNC flag updates.
1110 ;; Update the list of read articles.
1112 (gnus-compress-sequence
1113 (gnus-set-difference
1114 (gnus-set-difference
1116 (cdr (assoc '%Seen flags)))
1117 (cdr (assoc '%Flagged flags)))))
1118 (read (gnus-range-difference
1119 (cons start-article high) unread)))
1120 (when (> start-article 1)
1123 (if (> start-article 1)
1124 (gnus-sorted-range-intersection
1125 (cons 1 (1- start-article))
1126 (gnus-info-read info))
1127 (gnus-info-read info))
1129 (gnus-info-set-read info read)
1130 ;; Update the marks.
1131 (setq marks (gnus-info-marks info))
1132 (dolist (type (cdr nnimap-mark-alist))
1133 (let ((old-marks (assoc (car type) marks))
1135 (gnus-compress-sequence
1136 (cdr (or (assoc (caddr type) flags) ; %Flagged
1137 (assoc (intern (cadr type) obarray) flags)
1138 (assoc (cadr type) flags)))))) ; "\Flagged"
1139 (setq marks (delq old-marks marks))
1141 (when (and old-marks
1142 (> start-article 1))
1143 (setq old-marks (gnus-range-difference
1145 (cons start-article high)))
1146 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
1148 (push (cons (car type) new-marks) marks)))
1149 (gnus-info-set-marks info marks t)))))
1150 ;; Note the active level for the next run-through.
1151 (gnus-group-set-parameter info 'active (gnus-active group))
1152 (gnus-group-set-parameter info 'uidvalidity uidvalidity)
1153 (gnus-group-set-parameter info 'modseq highestmodseq)
1154 (nnimap-store-info info (gnus-active group)))))))
1156 (defun nnimap-update-qresync-info (info existing vanished flags)
1157 ;; Add all the vanished articles to the list of read articles.
1160 (gnus-range-add (gnus-info-read info)
1162 (let ((marks (gnus-info-marks info)))
1163 (dolist (type (cdr nnimap-mark-alist))
1164 (let ((ticks (assoc (car type) marks))
1166 (cdr (or (assoc (caddr type) flags) ; %Flagged
1167 (assoc (intern (cadr type) obarray) flags)
1168 (assoc (cadr type) flags))))) ; "\Flagged"
1169 (setq marks (delq ticks marks))
1171 ;; Add the new marks we got.
1172 (setq ticks (gnus-add-to-range ticks new-marks))
1173 ;; Remove the marks from messages that don't have them.
1174 (setq ticks (gnus-remove-from-range
1176 (gnus-compress-sequence
1177 (gnus-sorted-complement existing new-marks))))
1179 (push (cons (car type) ticks) marks)))
1180 (gnus-info-set-marks info marks t))))
1182 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1183 (if (zerop (length irange))
1186 (dolist (elem (split-string irange ","))
1188 (if (string-match ":" elem)
1189 (let ((numbers (split-string elem ":")))
1190 (cons (string-to-number (car numbers))
1191 (string-to-number (cadr numbers))))
1192 (string-to-number elem))
1194 (nreverse result))))
1196 (defun nnimap-store-info (info active)
1197 (let* ((group (gnus-group-real-name (gnus-info-group info)))
1198 (entry (assoc group nnimap-current-infos)))
1200 (setcdr entry (list info active))
1201 (push (list group info active) nnimap-current-infos))))
1203 (defun nnimap-flags-to-marks (groups)
1204 (let (data group totalp uidnext articles start-article mark permanent-flags
1205 uidvalidity vanished highestmodseq)
1206 (dolist (elem groups)
1207 (setq group (car elem)
1208 uidnext (nth 1 elem)
1209 start-article (nth 2 elem)
1210 permanent-flags (nth 3 elem)
1211 uidvalidity (nth 4 elem)
1212 vanished (nth 5 elem)
1213 highestmodseq (nth 6 elem)
1214 articles (nthcdr 7 elem))
1215 (let ((high (caar articles))
1217 (dolist (article articles)
1218 (setq low (car article))
1219 (push (car article) existing)
1220 (dolist (flag (cdr article))
1221 (setq mark (assoc flag marks))
1223 (push (list flag (car article)) marks)
1224 (setcdr mark (cons (car article) (cdr mark))))))
1225 (push (list group existing marks high low uidnext start-article
1226 permanent-flags uidvalidity vanished highestmodseq)
1230 (defun nnimap-parse-flags (sequences)
1231 (goto-char (point-min))
1232 ;; Change \Delete etc to %Delete, so that the reader can read it.
1233 (subst-char-in-region (point-min) (point-max)
1235 (let (start end articles groups uidnext elems permanent-flags
1236 uidvalidity vanished highestmodseq)
1237 (dolist (elem sequences)
1238 (destructuring-bind (group-sequence flag-sequence totalp group command)
1240 (setq start (point))
1242 ;; The EXAMINE was successful.
1243 (search-forward (format "\n%d OK " group-sequence) nil t)
1248 (setq permanent-flags
1249 (if (equal command "SELECT")
1250 (and (search-forward "PERMANENTFLAGS "
1251 (or end (point-min)) t)
1252 (read (current-buffer)))
1256 (and (search-forward "UIDNEXT "
1257 (or end (point-min)) t)
1258 (read (current-buffer))))
1261 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1262 (or end (point-min)) t)
1263 ;; Store UIDVALIDITY as a string, as it's
1264 ;; too big for 32-bit Emacsen, usually.
1268 (and (eq flag-sequence 'qresync)
1269 (re-search-forward "VANISHED.* \\([0-9:,]+\\)"
1270 (or end (point-min)) t)
1274 (and (search-forward "HIGHESTMODSEQ "
1275 (or end (point-min)) t)
1276 (read (current-buffer))))
1279 ;; The UID FETCH FLAGS was successful.
1280 (or (eq flag-sequence 'qresync)
1281 (search-forward (format "\n%d OK " flag-sequence) nil t)))
1282 (if (eq flag-sequence 'qresync)
1286 (setq start (point))
1288 (while (search-forward " FETCH " start t)
1289 (setq elems (read (current-buffer)))
1290 (push (cons (cadr (memq 'UID elems))
1291 (cadr (memq 'FLAGS elems)))
1293 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1294 vanished highestmodseq)
1298 (setq articles nil))))
1301 (defun nnimap-find-process-buffer (buffer)
1302 (cadr (assoc buffer nnimap-connection-alist)))
1304 (deffoo nnimap-request-post (&optional server)
1305 (setq nnimap-status-string "Read-only server")
1308 (defun nnimap-possibly-change-group (group server)
1309 (let ((open-result t))
1311 (not (nnimap-server-opened server)))
1312 (setq open-result (nnimap-open-server server)))
1319 (with-current-buffer (nnimap-buffer)
1320 (if (equal group (nnimap-group nnimap-object))
1322 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
1324 (setf (nnimap-group nnimap-object) group
1325 (nnimap-select-result nnimap-object) result)
1328 (defun nnimap-find-connection (buffer)
1329 "Find the connection delivering to BUFFER."
1330 (let ((entry (assoc buffer nnimap-connection-alist)))
1332 (if (and (buffer-name (cadr entry))
1333 (get-buffer-process (cadr entry))
1334 (memq (process-status (get-buffer-process (cadr entry)))
1336 (get-buffer-process (cadr entry))
1337 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
1340 (defvar nnimap-sequence 0)
1342 (defun nnimap-send-command (&rest args)
1343 (process-send-string
1344 (get-buffer-process (current-buffer))
1347 (incf nnimap-sequence)
1348 (apply #'format args)
1349 (if (nnimap-newlinep nnimap-object)
1354 (defun nnimap-log-command (command)
1355 (with-current-buffer (get-buffer-create "*imap log*")
1356 (goto-char (point-max))
1357 (insert (format-time-string "%H:%M:%S") " " command))
1360 (defun nnimap-command (&rest args)
1362 (setf (nnimap-last-command-time nnimap-object) (current-time))
1363 (let* ((sequence (apply #'nnimap-send-command args))
1364 (response (nnimap-get-response sequence)))
1365 (if (equal (caar response) "OK")
1367 (nnheader-report 'nnimap "%s"
1368 (mapconcat (lambda (a)
1370 (car response) " "))
1373 (defun nnimap-get-response (sequence)
1374 (nnimap-wait-for-response sequence)
1375 (nnimap-parse-response))
1377 (defun nnimap-wait-for-connection ()
1378 (let ((process (get-buffer-process (current-buffer))))
1379 (goto-char (point-min))
1380 (while (and (memq (process-status process)
1382 (not (re-search-forward "^[*.] .*\n" nil t)))
1383 (nnheader-accept-process-output process)
1384 (goto-char (point-min)))
1386 (and (looking-at "[*.] \\([A-Z0-9]+\\)")
1389 (defun nnimap-wait-for-response (sequence &optional messagep)
1390 (let ((process (get-buffer-process (current-buffer)))
1394 (goto-char (point-max))
1395 (while (and (setq openp (memq (process-status process)
1397 (not (re-search-backward
1398 (format "^%d .*\n" sequence)
1399 (if nnimap-streaming
1400 (max (point-min) (- (point) 500))
1404 (message "nnimap read %dk" (/ (buffer-size) 1000)))
1405 (nnheader-accept-process-output process)
1406 (goto-char (point-max)))
1409 ;; The user hit C-g while we were waiting: kill the process, in case
1410 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1412 (delete-process process)
1415 (defun nnimap-parse-response ()
1416 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1418 (dolist (line lines)
1419 (push (cdr (nnimap-parse-line line)) result))
1420 ;; Return the OK/error code first, and then all the "continuation
1421 ;; lines" afterwards.
1423 (nreverse result))))
1425 ;; Parse an IMAP response line lightly. They look like
1426 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1427 ;; the lines into a list of strings and lists of string.
1428 (defun nnimap-parse-line (line)
1432 (goto-char (point-min))
1434 (if (eql (setq char (following-char)) ? )
1442 (1- (search-forward "]" (line-end-position) 'move)))))
1447 (1- (search-forward ")" (line-end-position) 'move)))))
1452 (1- (or (search-forward "\"" (line-end-position) 'move)
1455 (buffer-substring (point) (if (search-forward " " nil t)
1457 (goto-char (point-max))))))
1459 (nreverse result))))
1461 (defun nnimap-last-response-string ()
1464 (let ((end (point)))
1468 (while (and (not (bobp))
1469 (eql (following-char) ?*))
1471 (unless (eql (following-char) ?*)
1473 (buffer-substring (point) end))))
1475 (defun nnimap-get-responses (sequences)
1477 (dolist (sequence sequences)
1478 (goto-char (point-min))
1479 (when (re-search-forward (format "^%d " sequence) nil t)
1480 (push (list sequence (nnimap-parse-response))
1484 (defvar nnimap-incoming-split-list nil)
1486 (defun nnimap-fetch-inbox (articles)
1488 (nnimap-wait-for-response
1489 (nnimap-send-command
1491 (nnimap-article-ranges articles)
1492 (format "(UID %s%s)"
1495 "BODY.PEEK[HEADER] BODY.PEEK"
1497 (if nnimap-split-download-body-default
1502 (defun nnimap-split-incoming-mail ()
1503 (with-current-buffer (nnimap-buffer)
1504 (let ((nnimap-incoming-split-list nil)
1505 (nnmail-split-methods (if (eq nnimap-split-methods 'default)
1506 nnmail-split-methods
1507 nnimap-split-methods))
1508 (nnmail-split-fancy (or nnimap-split-fancy
1509 nnmail-split-fancy))
1510 (nnmail-inhibit-default-split-group t)
1511 (groups (nnimap-get-groups))
1514 (nnimap-command "SELECT %S" nnimap-inbox)
1515 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1517 (nnimap-fetch-inbox new-articles)
1518 (nnimap-transform-split-mail)
1519 (nnheader-ms-strip-cr)
1521 (nnmail-split-incoming (current-buffer)
1522 #'nnimap-save-mail-spec
1524 #'nnimap-dummy-active-number
1525 #'nnimap-save-mail-spec)
1526 (when nnimap-incoming-split-list
1527 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1528 sequences junk-articles)
1529 ;; Create any groups that doesn't already exist on the
1531 (dolist (spec specs)
1532 (when (and (not (member (car spec) groups))
1533 (not (eq (car spec) 'junk)))
1534 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1535 ;; Then copy over all the messages.
1537 (dolist (spec specs)
1538 (let ((group (car spec))
1539 (ranges (cdr spec)))
1540 (if (eq group 'junk)
1541 (setq junk-articles ranges)
1542 (push (list (nnimap-send-command
1544 (nnimap-article-ranges ranges)
1545 (utf7-encode group t))
1548 ;; Wait for the last COPY response...
1550 (nnimap-wait-for-response (caar sequences))
1551 ;; And then mark the successful copy actions as deleted,
1552 ;; and possibly expunge them.
1553 (nnimap-mark-and-expunge-incoming
1554 (nnimap-parse-copied-articles sequences)))
1555 (nnimap-mark-and-expunge-incoming junk-articles)))))))
1557 (defun nnimap-mark-and-expunge-incoming (range)
1559 (setq range (nnimap-article-ranges range))
1562 (nnimap-send-command
1563 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1565 ;; If the server supports it, we now delete the message we have
1566 ;; just copied over.
1567 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1568 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1569 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1570 ;; user has configured it.
1572 (setq sequence (nnimap-send-command "EXPUNGE"))))
1573 (nnimap-wait-for-response sequence))))
1575 (defun nnimap-parse-copied-articles (sequences)
1576 (let (sequence copied range)
1577 (goto-char (point-min))
1578 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1579 (setq sequence (string-to-number (match-string 1)))
1580 (when (setq range (cadr (assq sequence sequences)))
1581 (push (gnus-uncompress-range range) copied)))
1582 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1584 (defun nnimap-new-articles (flags)
1586 (dolist (elem flags)
1587 (when (or (null (cdr elem))
1588 (and (not (memq '%Deleted (cdr elem)))
1589 (not (memq '%Seen (cdr elem)))))
1590 (push (car elem) new)))
1591 (gnus-compress-sequence (nreverse new))))
1593 (defun nnimap-make-split-specs (list)
1597 (destructuring-bind (article spec) elem
1598 (dolist (group (delete nil (mapcar #'car spec)))
1599 (unless (setq entry (assoc group specs))
1600 (push (setq entry (list group)) specs))
1601 (setcdr entry (cons article (cdr entry))))))
1602 (dolist (entry specs)
1603 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1606 (defun nnimap-transform-split-mail ()
1607 (goto-char (point-min))
1608 (let (article bytes)
1611 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1612 (delete-region (point) (progn (forward-line 1) (point)))
1615 (setq article (match-string 1)
1616 bytes (nnimap-get-length))
1617 (delete-region (line-beginning-position) (line-end-position))
1618 ;; Insert MMDF separator, and a way to remember what this
1620 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1621 (forward-char (1+ bytes))
1622 (setq bytes (nnimap-get-length))
1623 (delete-region (line-beginning-position) (line-end-position))
1624 (forward-char (1+ bytes))
1625 (delete-region (line-beginning-position) (line-end-position))))))
1627 (defun nnimap-dummy-active-number (group &optional server)
1630 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1632 (goto-char (point-min))
1633 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1634 (error "Invalid nnimap mail")
1635 (setq article (string-to-number (match-string 1))))
1637 (if (eq group-art 'junk)
1638 (list (cons 'junk 1))
1640 nnimap-incoming-split-list)))
1644 ;;; nnimap.el ends here