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.
39 (defvoo nnimap-address nil
40 "The address of the IMAP server.")
42 (defvoo nnimap-server-port nil
44 If nnimap-stream is `ssl', this will default to `imaps'. If not,
45 it will default to `imap'.")
47 (defvoo nnimap-stream 'ssl
48 "How nnimap will talk to the IMAP server.
49 Values are `ssl' and `network'.")
51 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
52 (if (listp imap-shell-program)
53 (car imap-shell-program)
57 (defvoo nnimap-inbox nil
58 "The mail box where incoming mail arrives and should be split out of.")
60 (defvoo nnimap-expunge-inbox nil
61 "If non-nil, expunge the inbox after fetching mail.
62 This is always done if the server supports UID EXPUNGE, but it's
63 not done by default on servers that doesn't support that command.")
65 (defvoo nnimap-connection-alist nil)
66 (defvar nnimap-process nil)
68 (defvar nnimap-status-string "")
70 (defvar nnimap-split-download-body-default nil
71 "Internal variable with default value for `nnimap-split-download-body'.")
74 group process commands capabilities)
76 (defvar nnimap-object nil)
78 (defvar nnimap-mark-alist
82 (expire "gnus-expire")
83 (dormant "gnus-dormant")
86 (download "gnus-download")
87 (forward "gnus-forward")))
89 (defvar nnimap-split-methods nil)
91 (defun nnimap-buffer ()
92 (nnimap-find-process-buffer nntp-server-buffer))
94 (defun nnimap-retrieve-headers (articles &optional group server fetch-old)
95 (with-current-buffer nntp-server-buffer
97 (when (nnimap-possibly-change-group group server)
98 (with-current-buffer (nnimap-buffer)
99 (nnimap-send-command "SELECT %S" (utf7-encode group t))
101 (nnimap-wait-for-response
104 (nnimap-article-ranges (gnus-compress-sequence articles))
105 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
107 (if (member "IMAP4REV1"
108 (nnimap-capabilities nnimap-object))
109 "BODY.PEEK[HEADER.FIELDS %s]"
110 "RFC822.HEADER.LINES %s")
111 (append '(Subject From Date Message-Id
112 References In-Reply-To Xref)
113 nnmail-extra-headers))))
115 (nnimap-transform-headers))
116 (insert-buffer-substring
117 (nnimap-find-process-buffer (current-buffer))))
120 (defun nnimap-transform-headers ()
121 (goto-char (point-min))
122 (let (article bytes lines)
125 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
126 (delete-region (point) (progn (forward-line 1) (point)))
129 (setq article (match-string 1)
130 bytes (nnimap-get-length)
133 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
134 (let ((structure (ignore-errors (read (current-buffer)))))
135 (while (and (consp structure)
136 (not (stringp (car structure))))
137 (setq structure (car structure)))
138 (setq lines (nth 7 structure))))
139 (delete-region (line-beginning-position) (line-end-position))
140 (insert (format "211 %s Article retrieved." article))
142 (insert (format "Bytes: %d\n" bytes))
144 (insert (format "Lines: %s\n" lines)))
145 (re-search-forward "^\r$")
146 (delete-region (line-beginning-position) (line-end-position))
150 (defun nnimap-get-length ()
151 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
152 (string-to-number (match-string 1))))
154 (defun nnimap-article-ranges (ranges)
158 (number-to-string ranges))
159 ((numberp (cdr ranges))
160 (format "%d:%d" (car ranges) (cdr ranges)))
162 (dolist (elem ranges)
165 (format "%d:%d" (car elem) (cdr elem))
166 (number-to-string elem))
168 (mapconcat #'identity (nreverse result) ",")))))
170 (defun nnimap-open-server (server &optional defs)
171 (if (nnimap-server-opened server)
173 (unless (assq 'nnimap-address defs)
174 (setq defs (append defs (list (list 'nnimap-address server)))))
175 (nnoo-change-server 'nnimap server defs)
176 (or (nnimap-find-connection nntp-server-buffer)
177 (nnimap-open-connection nntp-server-buffer))))
179 (defun nnimap-make-process-buffer (buffer)
181 (generate-new-buffer (format "*nnimap %s %s %s*"
182 nnimap-address nnimap-server-port
183 (gnus-buffer-exists-p buffer)))
184 (mm-disable-multibyte)
185 (buffer-disable-undo)
187 (set (make-local-variable 'after-change-functions) nil)
188 (set (make-local-variable 'nnimap-object) (make-nnimap))
189 (push (list buffer (current-buffer)) nnimap-connection-alist)
192 (defun nnimap-open-shell-stream (name buffer host port)
193 (let ((process (start-process name buffer shell-file-name
202 (defun nnimap-open-connection (buffer)
203 (with-current-buffer (nnimap-make-process-buffer buffer)
204 (let* ((coding-system-for-read 'binary)
205 (coding-system-for-write 'binary)
208 ((eq nnimap-stream 'network)
209 (open-network-stream "*nnimap*" (current-buffer) nnimap-address
210 (or nnimap-server-port
211 (if (netrc-find-service-number "imap")
214 (auth-source-user-or-password
215 '("login" "password") nnimap-address "imap" nil t))
216 ((eq nnimap-stream 'stream)
217 (nnimap-open-shell-stream
218 "*nnimap*" (current-buffer) nnimap-address
219 (or nnimap-server-port "imap"))
220 (auth-source-user-or-password
221 '("login" "password") nnimap-address "imap" nil t))
222 ((eq nnimap-stream 'ssl)
223 (open-tls-stream "*nnimap*" (current-buffer) nnimap-address
224 (or nnimap-server-port
225 (if (netrc-find-service-number "imaps")
229 (auth-source-user-or-password
230 '("login" "password") nnimap-address "imap")
231 (auth-source-user-or-password
232 '("login" "password") nnimap-address "imaps" nil t))))))
233 (setf (nnimap-process nnimap-object)
234 (get-buffer-process (current-buffer)))
236 (delete-process (nnimap-process nnimap-object)))
237 (when (and (nnimap-process nnimap-object)
238 (memq (process-status (nnimap-process nnimap-object))
240 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
241 (let ((result (nnimap-command "LOGIN %S %S"
242 (car credentials) (cadr credentials))))
243 (if (not (car result))
245 (delete-process (nnimap-process nnimap-object))
247 (setf (nnimap-capabilities nnimap-object)
250 (or (nnimap-find-parameter "CAPABILITY" (cdr result))
251 (nnimap-find-parameter
252 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
253 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
254 (nnimap-command "ENABLE QRESYNC"))
257 (defun nnimap-find-parameter (parameter elems)
261 ((equal (car elem) parameter)
262 (setq result (cdr elem)))
263 ((and (equal (car elem) "OK")
265 (equal (caadr elem) parameter))
266 (setq result (cdr (cadr elem))))))
269 (defun nnimap-close-server (&optional server)
272 (defun nnimap-request-close ()
275 (defun nnimap-server-opened (&optional server)
276 (and (nnoo-current-server-p 'nnimap server)
278 (gnus-buffer-live-p nntp-server-buffer)
279 (nnimap-find-connection nntp-server-buffer)))
281 (defun nnimap-status-message (&optional server)
282 nnimap-status-string)
284 (defun nnimap-request-article (article &optional group server to-buffer)
285 (with-current-buffer nntp-server-buffer
286 (let ((result (nnimap-possibly-change-group group server)))
287 (when (stringp article)
288 (setq article (nnimap-find-article-by-message-id group article)))
292 (with-current-buffer (nnimap-buffer)
296 (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object))
297 "UID FETCH %d BODY.PEEK[]"
298 "UID FETCH %d RFC822.PEEK")
300 (let ((buffer (nnimap-find-process-buffer (current-buffer))))
302 (with-current-buffer to-buffer
303 (insert-buffer-substring buffer)
304 (goto-char (point-min))
305 (let ((bytes (nnimap-get-length)))
306 (delete-region (line-beginning-position)
307 (progn (forward-line 1) (point)))
308 (goto-char (+ (point) bytes))
309 (delete-region (point) (point-max))
310 (nnheader-ms-strip-cr))
313 (defun nnimap-request-group (group &optional server dont-check)
314 (with-current-buffer nntp-server-buffer
315 (let ((result (nnimap-possibly-change-group group server))
318 (setq articles (nnimap-get-flags "1:*"))
324 (or (caar articles) 0)
325 (or (caar (last articles)) 0)
329 (defun nnimap-get-flags (spec)
332 (with-current-buffer (nnimap-buffer)
334 (nnimap-wait-for-response (nnimap-send-command
335 "UID FETCH %s FLAGS" spec))
336 (goto-char (point-min))
337 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
338 (setq elems (nnimap-parse-line (match-string 1)))
339 (push (cons (string-to-number (cadr (member "UID" elems)))
340 (cadr (member "FLAGS" elems)))
342 (nreverse articles)))
344 (defun nnimap-close-group (group &optional server)
347 (deffoo nnimap-request-move-article (article group server accept-form
348 &optional last internal-move-group)
349 (when (nnimap-possibly-change-group group server)
350 ;; If the move is internal (on the same server), just do it the easy
352 (let ((message-id (message-field-value "message-id")))
353 (if internal-move-group
355 (with-current-buffer (nnimap-buffer)
356 (nnimap-command "UID COPY %d %S"
358 (utf7-encode internal-move-group t)))))
360 (nnimap-delete-article article)
361 (cons internal-move-group
362 (nnimap-find-article-by-message-id
363 internal-move-group message-id))))
365 (let ((result (eval accept-form)))
367 (nnimap-delete-article article)
370 (deffoo nnimap-request-expire-articles (articles group &optional server force)
372 ((not (nnimap-possibly-change-group group server))
375 (unless (nnimap-delete-article articles)
376 (message "Article marked for deletion, but not expunged."))
381 (defun nnimap-find-article-by-message-id (group message-id)
382 (when (nnimap-possibly-change-group group nil)
383 (with-current-buffer (nnimap-buffer)
385 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
388 ;; Select the last instance of the message in the group.
390 (car (last (assoc "SEARCH" (cdr result)))))
391 (string-to-number article)))))))
393 (defun nnimap-delete-article (articles)
394 (with-current-buffer (nnimap-buffer)
395 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
396 (nnimap-article-ranges articles))
397 (when (member "UIDPLUS" (nnimap-capabilities nnimap-object))
398 (nnimap-send-command "UID EXPUNGE %s"
399 (nnimap-article-ranges articles))
402 (deffoo nnimap-request-scan (&optional group server)
403 (when (and (nnimap-possibly-change-group nil server)
404 (equal group nnimap-inbox)
406 nnimap-split-methods)
407 (nnimap-split-incoming-mail)))
409 (defun nnimap-marks-to-flags (marks)
412 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
416 (defun nnimap-request-set-mark (group actions &optional server)
417 (when (nnimap-possibly-change-group group server)
419 (with-current-buffer (nnimap-buffer)
420 ;; Just send all the STORE commands without waiting for
421 ;; response. If they're successful, they're successful.
422 (dolist (action actions)
423 (destructuring-bind (range action marks) action
424 (let ((flags (nnimap-marks-to-flags marks)))
426 (setq sequence (nnimap-send-command
427 "UID STORE %s %sFLAGS.SILENT (%s)"
428 (nnimap-article-ranges range)
432 (mapconcat #'identity flags " ")))))))
433 ;; Wait for the last command to complete to avoid later
434 ;; syncronisation problems with the stream.
435 (nnimap-wait-for-response sequence)))))
437 (deffoo nnimap-request-accept-article (group &optional server last)
438 (when (nnimap-possibly-change-group nil server)
439 (nnmail-check-syntax)
440 (let ((message (buffer-string))
441 (message-id (message-field-value "message-id"))
443 (with-current-buffer (nnimap-buffer)
444 (setq sequence (nnimap-send-command
445 "APPEND %S {%d}" (utf7-encode group t)
447 (process-send-string (get-buffer-process (current-buffer)) message)
448 (process-send-string (get-buffer-process (current-buffer)) "\r\n")
449 (let ((result (nnimap-get-response sequence)))
452 (nnimap-find-article-by-message-id group message-id))))))))
454 (defun nnimap-add-cr ()
455 (goto-char (point-min))
456 (while (re-search-forward "\r?\n" nil t)
457 (replace-match "\r\n" t t)))
459 (defun nnimap-get-groups ()
460 (let ((result (nnimap-command "LIST \"\" \"*\""))
463 (dolist (line (cdr result))
464 (when (and (equal (car line) "LIST")
465 (not (and (caadr line)
466 (string-match "noselect" (caadr line)))))
467 (push (car (last line)) groups)))
470 (defun nnimap-request-list (&optional server)
471 (nnimap-possibly-change-group nil server)
472 (with-current-buffer nntp-server-buffer
475 (with-current-buffer (nnimap-buffer)
476 (nnimap-get-groups)))
479 (with-current-buffer (nnimap-buffer)
480 (dolist (group groups)
481 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
484 (nnimap-wait-for-response (caar sequences))
486 (nnimap-get-responses (mapcar #'car sequences))))
487 (dolist (response responses)
488 (let* ((sequence (car response))
489 (response (cadr response))
490 (group (cadr (assoc sequence sequences))))
492 (equal (caar response) "OK"))
493 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
495 (dolist (elem response)
496 (when (equal (cadr elem) "EXISTS")
497 (setq exists (string-to-number (car elem)))))
499 (setq highest (1- (string-to-number (car uidnext)))))
502 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
505 (insert (format "%S %d %d y\n"
506 (utf7-decode group t) highest (1+ highest))))
508 ;; Return the widest possible range.
509 (insert (format "%S %d 1 y\n" (utf7-decode group t)
510 (or highest exists)))))))))
513 (defun nnimap-retrieve-group-data-early (server infos)
514 (when (nnimap-possibly-change-group nil server)
515 (with-current-buffer (nnimap-buffer)
516 ;; QRESYNC handling isn't implemented.
517 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
518 marks groups sequences)
519 ;; Go through the infos and gather the data needed to know
520 ;; what and how to request the data.
522 (setq marks (gnus-info-marks info))
523 (push (list (gnus-group-real-name (gnus-info-group info))
524 (cdr (assq 'active marks))
525 (cdr (assq 'uid marks)))
527 ;; Then request the data.
529 (dolist (elem groups)
534 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
543 ;; Fetch the last 100 flags.
544 (max 1 (- (cdr (nth 1 elem)) 100))
546 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
547 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
553 (defun nnimap-finish-retrieve-group-infos (server infos sequences)
555 (nnimap-possibly-change-group nil server))
556 (with-current-buffer (nnimap-buffer)
557 ;; Wait for the final data to trickle in.
558 (nnimap-wait-for-response (cadar sequences))
559 ;; Now we should have all the data we need, no matter whether
560 ;; we're QRESYNCING, fetching all the flags from scratch, or
561 ;; just fetching the last 100 flags per group.
562 (nnimap-update-infos (nnimap-flags-to-marks
564 (nreverse sequences)))
567 (defun nnimap-update-infos (flags infos)
569 (let ((group (gnus-group-real-name (gnus-info-group info))))
570 (nnimap-update-info info (cdr (assoc group flags))))))
572 (defun nnimap-update-info (info marks)
574 (destructuring-bind (existing flags high low uidnext start-article) marks
575 (let ((group (gnus-info-group info))
576 (completep (and start-article
577 (= start-article 1))))
578 ;; First set the active ranges based on high/low.
580 (not (gnus-active group)))
581 (gnus-set-active group
584 ;; No articles in this group.
585 (cons (1- uidnext) uidnext)))
586 (setcdr (gnus-active group) high))
587 ;; Then update the list of read articles.
589 (gnus-compress-sequence
593 (cdr (assoc "\\Seen" flags)))
594 (cdr (assoc "\\Flagged" flags)))))
595 (read (gnus-range-difference
596 (cons start-article high) unread)))
597 (when (> start-article 1)
600 (gnus-sorted-range-intersection
601 (cons 1 start-article)
602 (gnus-info-read info))
604 (gnus-info-set-read info read)
606 (setq marks (gnus-info-marks info))
607 ;; Note the active level for the next run-through.
608 (let ((active (assq 'active marks)))
610 (setcdr active (gnus-active group))
611 (push (cons 'active (gnus-active group)) marks)))
612 (dolist (type (cdr nnimap-mark-alist))
613 (let ((old-marks (assoc (car type) marks))
614 (new-marks (gnus-compress-sequence
615 (cdr (assoc (cadr type) flags)))))
616 (setq marks (delq old-marks marks))
620 (setq old-marks (gnus-range-difference
621 (cons start-article high)
623 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
625 (push (cons (car type) new-marks) marks)))
626 (gnus-info-set-marks info marks)))))))
628 (defun nnimap-flags-to-marks (groups)
629 (let (data group totalp uidnext articles start-article mark)
630 (dolist (elem groups)
631 (setq group (car elem)
633 start-article (caddr elem)
634 articles (cdddr elem))
635 (let ((high (caar articles))
637 (dolist (article articles)
638 (setq low (car article))
639 (push (car article) existing)
640 (dolist (flag (cdr article))
641 (setq mark (assoc flag marks))
643 (push (list flag (car article)) marks)
644 (setcdr mark (cons (car article) (cdr mark)))))
645 (push (list group existing marks high low uidnext start-article)
649 (defun nnimap-parse-flags (sequences)
650 (goto-char (point-min))
651 (let (start end articles groups uidnext elems)
652 (dolist (elem sequences)
653 (destructuring-bind (group-sequence flag-sequence totalp group) elem
654 ;; The EXAMINE was successful.
655 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
659 (if (re-search-backward "UIDNEXT \\([0-9]+\\)"
660 (or end (point-min)) t)
661 (setq uidnext (string-to-number (match-string 1)))
664 ;; The UID FETCH FLAGS was successful.
665 (search-forward (format "\n%d OK " flag-sequence) nil t))
668 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" end t)
669 (setq elems (nnimap-parse-line (match-string 1)))
670 (push (cons (string-to-number (cadr (member "UID" elems)))
671 (cadr (member "FLAGS" elems)))
673 (push (nconc (list group uidnext totalp) articles) groups)
674 (setq articles nil))))
677 (defun nnimap-find-process-buffer (buffer)
678 (cadr (assoc buffer nnimap-connection-alist)))
680 (defun nnimap-request-post (&optional server)
681 (setq nnimap-status-string "Read-only server")
684 (defun nnimap-possibly-change-group (group server)
685 (let ((open-result t))
687 (not (nnimap-server-opened server)))
688 (setq open-result (nnimap-open-server server)))
695 (with-current-buffer (nnimap-buffer)
696 (if (equal group (nnimap-group nnimap-object))
698 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
700 (setf (nnimap-group nnimap-object) group)
703 (defun nnimap-find-connection (buffer)
704 "Find the connection delivering to BUFFER."
705 (let ((entry (assoc buffer nnimap-connection-alist)))
707 (if (and (buffer-name (cadr entry))
708 (get-buffer-process (cadr entry))
709 (memq (process-status (get-buffer-process (cadr entry)))
711 (get-buffer-process (cadr entry))
712 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
715 (defvar nnimap-sequence 0)
717 (defun nnimap-send-command (&rest args)
719 (get-buffer-process (current-buffer))
722 (incf nnimap-sequence)
723 (apply #'format args))))
726 (defun nnimap-log-command (command)
727 (with-current-buffer (get-buffer-create "*imap log*")
728 (goto-char (point-max))
729 (insert (format-time-string "%H:%M:%S") " " command))
732 (defun nnimap-command (&rest args)
734 (let* ((sequence (apply #'nnimap-send-command args))
735 (response (nnimap-get-response sequence)))
736 (if (equal (caar response) "OK")
738 (nnheader-report 'nnimap "%s"
739 (mapconcat #'identity (car response) " "))
742 (defun nnimap-get-response (sequence)
743 (nnimap-wait-for-response sequence)
744 (nnimap-parse-response))
746 (defun nnimap-wait-for-response (sequence &optional messagep)
747 (goto-char (point-max))
751 (not (looking-at (format "^%d .*\n" sequence)))))
753 (message "Read %dKB" (/ (buffer-size) 1000)))
754 (nnheader-accept-process-output (get-buffer-process (current-buffer)))
755 (goto-char (point-max))))
757 (defun nnimap-parse-response ()
758 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
761 (push (cdr (nnimap-parse-line line)) result))
762 ;; Return the OK/error code first, and then all the "continuation
763 ;; lines" afterwards.
767 ;; Parse an IMAP response line lightly. They look like
768 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
769 ;; the lines into a list of strings and lists of string.
770 (defun nnimap-parse-line (line)
774 (goto-char (point-min))
776 (if (eql (setq char (following-char)) ? )
781 (split-string (buffer-substring
782 (1+ (point)) (1- (search-forward "]")))))
784 (split-string (buffer-substring
785 (1+ (point)) (1- (search-forward ")")))))
788 (buffer-substring (point) (1- (search-forward "\""))))
790 (buffer-substring (point) (if (search-forward " " nil t)
792 (goto-char (point-max))))))
796 (defun nnimap-last-response-string ()
803 (while (and (not (bobp))
804 (eql (following-char) ?*))
806 (unless (eql (following-char) ?*)
808 (buffer-substring (point) end))))
810 (defun nnimap-get-responses (sequences)
812 (dolist (sequence sequences)
813 (goto-char (point-min))
814 (when (re-search-forward (format "^%d " sequence) nil t)
815 (push (list sequence (nnimap-parse-response))
819 (defvar nnimap-incoming-split-list nil)
821 (defun nnimap-fetch-inbox (articles)
823 (nnimap-wait-for-response
826 (nnimap-article-ranges articles)
829 (if (member "IMAP4REV1"
830 (nnimap-capabilities nnimap-object))
831 "BODY.PEEK[HEADER] BODY.PEEK"
833 (if nnimap-split-download-body-default
838 (defun nnimap-split-incoming-mail ()
839 (with-current-buffer (nnimap-buffer)
840 (let ((nnimap-incoming-split-list nil)
841 (nnmail-split-methods nnimap-split-methods)
842 (nnmail-inhibit-default-split-group t)
843 (groups (nnimap-get-groups))
846 (nnimap-command "SELECT %S" nnimap-inbox)
847 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
849 (nnimap-fetch-inbox new-articles)
850 (nnimap-transform-split-mail)
851 (nnheader-ms-strip-cr)
853 (nnmail-split-incoming (current-buffer)
854 #'nnimap-save-mail-spec
856 #'nnimap-dummy-active-number)
857 (when nnimap-incoming-split-list
858 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
860 ;; Create any groups that doesn't already exist on the
863 (unless (member (car spec) groups)
864 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
865 ;; Then copy over all the messages.
868 (let ((group (car spec))
870 (push (list (nnimap-send-command "UID COPY %s %S"
871 (nnimap-article-ranges ranges)
872 (utf7-encode group t))
875 ;; Wait for the last COPY response...
877 (nnimap-wait-for-response (caar sequences))
878 ;; And then mark the successful copy actions as deleted,
879 ;; and possibly expunge them.
880 (nnimap-mark-and-expunge-incoming
881 (nnimap-parse-copied-articles sequences)))))))))
883 (defun nnimap-mark-and-expunge-incoming (range)
885 (setq range (nnimap-article-ranges range))
887 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)
889 ;; If the server supports it, we now delete the message we have
891 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
892 (nnimap-send-command "UID EXPUNGE %s" range))
893 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
894 ;; user has configured it.
895 (nnimap-expunge-inbox
896 (nnimap-send-command "EXPUNGE")))))
898 (defun nnimap-parse-copied-articles (sequences)
899 (let (sequence copied range)
900 (goto-char (point-min))
901 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
902 (setq sequence (string-to-number (match-string 1)))
903 (when (setq range (cadr (assq sequence sequences)))
904 (push (gnus-uncompress-range range) copied)))
905 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
907 (defun nnimap-new-articles (flags)
910 (when (or (null (cdr elem))
911 (and (not (member "\\Deleted" (cdr elem)))
912 (not (member "\\Seen" (cdr elem)))))
913 (push (car elem) new)))
914 (gnus-compress-sequence (nreverse new))))
916 (defun nnimap-make-split-specs (list)
920 (destructuring-bind (article spec) elem
921 (dolist (group (delete nil (mapcar #'car spec)))
922 (unless (setq entry (assoc group specs))
923 (push (setq entry (list group)) specs))
924 (setcdr entry (cons article (cdr entry))))))
925 (dolist (entry specs)
926 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
929 (defun nnimap-transform-split-mail ()
930 (goto-char (point-min))
934 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
935 (delete-region (point) (progn (forward-line 1) (point)))
938 (setq article (match-string 1)
939 bytes (nnimap-get-length))
940 (delete-region (line-beginning-position) (line-end-position))
941 ;; Insert MMDF separator, and a way to remember what this
943 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
944 (forward-char (1+ bytes))
945 (setq bytes (nnimap-get-length))
946 (delete-region (line-beginning-position) (line-end-position))
947 (forward-char (1+ bytes))
948 (delete-region (line-beginning-position) (line-end-position))))))
950 (defun nnimap-dummy-active-number (group &optional server)
953 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
955 (goto-char (point-min))
956 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
957 (error "Invalid nnimap mail")
958 (setq article (string-to-number (match-string 1))))
959 (push (list article group-art)
960 nnimap-incoming-split-list)))
964 ;;; nnimap.el ends here