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-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 (netrc-credentials nnimap-address "imap"))
215 ((eq nnimap-stream 'stream)
216 (nnimap-open-shell-stream
217 "*nnimap*" (current-buffer) nnimap-address
218 (or nnimap-server-port "imap"))
219 (netrc-credentials nnimap-address "imap"))
220 ((eq nnimap-stream 'ssl)
221 (open-tls-stream "*nnimap*" (current-buffer) nnimap-address
222 (or nnimap-server-port
223 (if (netrc-find-service-number "imaps")
226 (netrc-credentials nnimap-address "imaps" "imap")))))
227 (setf (nnimap-process nnimap-object)
228 (get-buffer-process (current-buffer)))
230 (delete-process (nnimap-process nnimap-object))
231 (error "Can't find user name/password for %s" nnimap-address))
232 (when (and (nnimap-process nnimap-object)
233 (memq (process-status (nnimap-process nnimap-object))
235 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
236 (let ((result (nnimap-command "LOGIN %S %S"
237 (car credentials) (cadr credentials))))
239 (delete-process (nnimap-process nnimap-object))
240 (error "Unable to login to the server: %s"
241 (mapconcat #'identity (cadr result) " ")))
242 (setf (nnimap-capabilities nnimap-object)
245 (or (nnimap-find-parameter "CAPABILITY" (cdr result))
246 (nnimap-find-parameter
247 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
248 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
249 (nnimap-command "ENABLE QRESYNC"))
252 (defun nnimap-find-parameter (parameter elems)
256 ((equal (car elem) parameter)
257 (setq result (cdr elem)))
258 ((and (equal (car elem) "OK")
260 (equal (caadr elem) parameter))
261 (setq result (cdr (cadr elem))))))
264 (defun nnimap-close-server (&optional server)
267 (defun nnimap-request-close ()
270 (defun nnimap-server-opened (&optional server)
271 (and (nnoo-current-server-p 'nnimap server)
273 (gnus-buffer-live-p nntp-server-buffer)
274 (nnimap-find-connection nntp-server-buffer)))
276 (defun nnimap-status-message (&optional server)
277 nnimap-status-string)
279 (defun nnimap-request-article (article &optional group server to-buffer)
280 (with-current-buffer nntp-server-buffer
281 (let ((result (nnimap-possibly-change-group group server)))
282 (when (stringp article)
283 (setq article (nnimap-find-article-by-message-id group article)))
287 (with-current-buffer (nnimap-buffer)
291 (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object))
292 "UID FETCH %d BODY.PEEK[]"
293 "UID FETCH %d RFC822.PEEK")
295 (let ((buffer (nnimap-find-process-buffer (current-buffer))))
297 (with-current-buffer to-buffer
298 (insert-buffer-substring buffer)
299 (goto-char (point-min))
300 (let ((bytes (nnimap-get-length)))
301 (delete-region (line-beginning-position)
302 (progn (forward-line 1) (point)))
303 (goto-char (+ (point) bytes))
304 (delete-region (point) (point-max))
305 (nnheader-ms-strip-cr))
308 (defun nnimap-request-group (group &optional server dont-check)
309 (with-current-buffer nntp-server-buffer
310 (let ((result (nnimap-possibly-change-group group server))
313 (setq articles (nnimap-get-flags "1:*"))
319 (or (caar articles) 0)
320 (or (caar (last articles)) 0)
324 (defun nnimap-get-flags (spec)
327 (with-current-buffer (nnimap-buffer)
329 (nnimap-wait-for-response (nnimap-send-command
330 "UID FETCH %s FLAGS" spec))
331 (goto-char (point-min))
332 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
333 (setq elems (nnimap-parse-line (match-string 1)))
334 (push (cons (string-to-number (cadr (member "UID" elems)))
335 (cadr (member "FLAGS" elems)))
337 (nreverse articles)))
339 (defun nnimap-close-group (group &optional server)
342 (deffoo nnimap-request-move-article (article group server accept-form
343 &optional last internal-move-group)
344 (when (nnimap-possibly-change-group group server)
345 ;; If the move is internal (on the same server), just do it the easy
347 (let ((message-id (message-field-value "message-id")))
348 (if internal-move-group
350 (with-current-buffer (nnimap-buffer)
351 (nnimap-command "UID COPY %d %S"
353 (utf7-encode internal-move-group t)))))
355 (nnimap-delete-article article)
356 (cons internal-move-group
357 (nnimap-find-article-by-message-id
358 internal-move-group message-id))))
360 (let ((result (eval accept-form)))
362 (nnimap-delete-article article)
365 (deffoo nnimap-request-expire-articles (articles group &optional server force)
367 ((not (nnimap-possibly-change-group group server))
370 (unless (nnimap-delete-article articles)
371 (message "Article marked for deletion, but not expunged."))
376 (defun nnimap-find-article-by-message-id (group message-id)
377 (when (nnimap-possibly-change-group group nil)
378 (with-current-buffer (nnimap-buffer)
380 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
383 ;; Select the last instance of the message in the group.
385 (car (last (assoc "SEARCH" (cdr result)))))
386 (string-to-number article)))))))
388 (defun nnimap-delete-article (articles)
389 (with-current-buffer (nnimap-buffer)
390 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
391 (nnimap-article-ranges articles))
392 (when (member "UIDPLUS" (nnimap-capabilities nnimap-object))
393 (nnimap-send-command "UID EXPUNGE %s"
394 (nnimap-article-ranges articles))
397 (deffoo nnimap-request-scan (&optional group server)
398 (when (and (nnimap-possibly-change-group nil server)
399 (equal group nnimap-inbox)
401 nnimap-split-methods)
402 (nnimap-split-incoming-mail)))
404 (defun nnimap-marks-to-flags (marks)
407 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
411 (defun nnimap-request-set-mark (group actions &optional server)
412 (when (nnimap-possibly-change-group group server)
414 (with-current-buffer (nnimap-buffer)
415 ;; Just send all the STORE commands without waiting for
416 ;; response. If they're successful, they're successful.
417 (dolist (action actions)
418 (destructuring-bind (range action marks) action
419 (let ((flags (nnimap-marks-to-flags marks)))
421 (setq sequence (nnimap-send-command
422 "UID STORE %s %sFLAGS.SILENT (%s)"
423 (nnimap-article-ranges range)
427 (mapconcat #'identity flags " ")))))))
428 ;; Wait for the last command to complete to avoid later
429 ;; syncronisation problems with the stream.
430 (nnimap-wait-for-response sequence)))))
432 (deffoo nnimap-request-accept-article (group &optional server last)
433 (when (nnimap-possibly-change-group nil server)
434 (nnmail-check-syntax)
435 (let ((message (buffer-string))
436 (message-id (message-field-value "message-id"))
438 (with-current-buffer (nnimap-buffer)
439 (setq sequence (nnimap-send-command
440 "APPEND %S {%d}" (utf7-encode group t)
442 (process-send-string (get-buffer-process (current-buffer)) message)
443 (process-send-string (get-buffer-process (current-buffer)) "\r\n")
444 (let ((result (nnimap-get-response sequence)))
447 (nnimap-find-article-by-message-id group message-id))))))))
449 (defun nnimap-add-cr ()
450 (goto-char (point-min))
451 (while (re-search-forward "\r?\n" nil t)
452 (replace-match "\r\n" t t)))
454 (defun nnimap-get-groups ()
455 (let ((result (nnimap-command "LIST \"\" \"*\""))
458 (dolist (line (cdr result))
459 (when (and (equal (car line) "LIST")
460 (not (and (caadr line)
461 (string-match "noselect" (caadr line)))))
462 (push (car (last line)) groups)))
465 (defun nnimap-request-list (&optional server)
466 (nnimap-possibly-change-group nil server)
467 (with-current-buffer nntp-server-buffer
470 (with-current-buffer (nnimap-buffer)
471 (nnimap-get-groups)))
474 (with-current-buffer (nnimap-buffer)
475 (dolist (group groups)
476 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
479 (nnimap-wait-for-response (caar sequences))
481 (nnimap-get-responses (mapcar #'car sequences))))
482 (dolist (response responses)
483 (let* ((sequence (car response))
484 (response (cadr response))
485 (group (cadr (assoc sequence sequences))))
487 (equal (caar response) "OK"))
488 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
490 (dolist (elem response)
491 (when (equal (cadr elem) "EXISTS")
492 (setq exists (string-to-number (car elem)))))
494 (setq highest (1- (string-to-number (car uidnext)))))
497 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
500 (insert (format "%S %d %d y\n"
501 (utf7-decode group t) highest (1+ highest))))
503 ;; Return the widest possible range.
504 (insert (format "%S %d 1 y\n" (utf7-decode group t)
505 (or highest exists)))))))))
508 (defun nnimap-retrieve-group-data-early (server infos)
509 (when (nnimap-possibly-change-group nil server)
510 (with-current-buffer (nnimap-buffer)
511 ;; QRESYNC handling isn't implemented.
512 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
513 marks groups sequences)
514 ;; Go through the infos and gather the data needed to know
515 ;; what and how to request the data.
517 (setq marks (gnus-info-marks info))
518 (push (list (gnus-group-real-name (gnus-info-group info))
519 (cdr (assq 'active marks))
520 (cdr (assq 'uid marks)))
522 ;; Then request the data.
524 (dolist (elem groups)
529 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
538 ;; Fetch the last 100 flags.
539 (max 1 (- (cdr (nth 1 elem)) 100))
541 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
542 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
548 (defun nnimap-finish-retrieve-group-infos (server infos sequences)
549 (when (nnimap-possibly-change-group nil server)
550 (with-current-buffer (nnimap-buffer)
551 ;; Wait for the final data to trickle in.
552 (nnimap-wait-for-response (cadar sequences))
553 ;; Now we should have all the data we need, no matter whether
554 ;; we're QRESYNCING, fetching all the flags from scratch, or
555 ;; just fetching the last 100 flags per group.
556 (nnimap-update-infos (nnimap-flags-to-marks
558 (nreverse sequences)))
561 (defun nnimap-update-infos (flags infos)
563 (let ((group (gnus-group-real-name (gnus-info-group info))))
564 (nnimap-update-info info (cdr (assoc group flags))))))
566 (defun nnimap-update-info (info marks)
568 (destructuring-bind (existing flags high low uidnext start-article) marks
569 (let ((group (gnus-info-group info))
570 (completep (and start-article
571 (= start-article 1))))
572 ;; First set the active ranges based on high/low.
574 (not (gnus-active group)))
575 (gnus-set-active group
578 ;; No articles in this group.
579 (cons (1- uidnext) uidnext)))
580 (setcdr (gnus-active group) high))
581 ;; Then update the list of read articles.
583 (gnus-compress-sequence
587 (cdr (assoc "\\Seen" flags)))
588 (cdr (assoc "\\Flagged" flags)))))
589 (read (gnus-range-difference
590 (cons start-article high) unread)))
591 (when (> start-article 1)
594 (gnus-sorted-range-intersection
595 (cons 1 start-article)
596 (gnus-info-read info))
598 (gnus-info-set-read info read)
600 (setq marks (gnus-info-marks info))
601 ;; Note the active level for the next run-through.
602 (let ((active (assq 'active marks)))
604 (setcdr active (gnus-active group))
605 (push (cons 'active (gnus-active group)) marks)))
606 (dolist (type (cdr nnimap-mark-alist))
607 (let ((old-marks (assoc (car type) marks))
608 (new-marks (gnus-compress-sequence
609 (cdr (assoc (cadr type) flags)))))
610 (setq marks (delq old-marks marks))
614 (setq old-marks (gnus-range-difference
615 (cons start-article high)
617 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
619 (push (cons (car type) new-marks) marks)))
620 (gnus-info-set-marks info marks)))))))
622 (defun nnimap-flags-to-marks (groups)
623 (let (data group totalp uidnext articles start-article mark)
624 (dolist (elem groups)
625 (setq group (car elem)
627 start-article (caddr elem)
628 articles (cdddr elem))
629 (let ((high (caar articles))
631 (dolist (article articles)
632 (setq low (car article))
633 (push (car article) existing)
634 (dolist (flag (cdr article))
635 (setq mark (assoc flag marks))
637 (push (list flag (car article)) marks)
638 (setcdr mark (cons (car article) (cdr mark)))))
639 (push (list group existing marks high low uidnext start-article)
643 (defun nnimap-parse-flags (sequences)
644 (goto-char (point-min))
645 (let (start end articles groups uidnext elems)
646 (dolist (elem sequences)
647 (destructuring-bind (group-sequence flag-sequence totalp group) elem
648 ;; The EXAMINE was successful.
649 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
653 (if (re-search-backward "UIDNEXT \\([0-9]+\\)"
654 (or end (point-min)) t)
655 (setq uidnext (string-to-number (match-string 1)))
658 ;; The UID FETCH FLAGS was successful.
659 (search-forward (format "\n%d OK " flag-sequence) nil t))
662 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" end t)
663 (setq elems (nnimap-parse-line (match-string 1)))
664 (push (cons (string-to-number (cadr (member "UID" elems)))
665 (cadr (member "FLAGS" elems)))
667 (push (nconc (list group uidnext totalp) articles) groups)
668 (setq articles nil))))
671 (defun nnimap-find-process-buffer (buffer)
672 (cadr (assoc buffer nnimap-connection-alist)))
674 (defun nnimap-request-post (&optional server)
675 (setq nnimap-status-string "Read-only server")
678 (defun nnimap-possibly-change-group (group server)
680 (not (nnimap-server-opened server)))
681 (nnimap-open-server server))
684 (with-current-buffer (nnimap-buffer)
685 (if (equal group (nnimap-group nnimap-object))
687 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
689 (setf (nnimap-group nnimap-object) group)
692 (defun nnimap-find-connection (buffer)
693 "Find the connection delivering to BUFFER."
694 (let ((entry (assoc buffer nnimap-connection-alist)))
696 (if (and (buffer-name (cadr entry))
697 (get-buffer-process (cadr entry))
698 (memq (process-status (get-buffer-process (cadr entry)))
700 (get-buffer-process (cadr entry))
701 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
704 (defvar nnimap-sequence 0)
706 (defun nnimap-send-command (&rest args)
708 (get-buffer-process (current-buffer))
711 (incf nnimap-sequence)
712 (apply #'format args))))
715 (defun nnimap-log-command (command)
716 (with-current-buffer (get-buffer-create "*imap log*")
717 (goto-char (point-max))
718 (insert (format-time-string "%H:%M:%S") " " command))
721 (defun nnimap-command (&rest args)
723 (let* ((sequence (apply #'nnimap-send-command args))
724 (response (nnimap-get-response sequence)))
725 (if (equal (caar response) "OK")
727 (nnheader-report 'nnimap "%s"
728 (mapconcat #'identity (car response) " "))
731 (defun nnimap-get-response (sequence)
732 (nnimap-wait-for-response sequence)
733 (nnimap-parse-response))
735 (defun nnimap-wait-for-response (sequence &optional messagep)
736 (goto-char (point-max))
740 (not (looking-at (format "^%d .*\n" sequence)))))
742 (message "Read %dKB" (/ (buffer-size) 1000)))
743 (nnheader-accept-process-output (get-buffer-process (current-buffer)))
744 (goto-char (point-max))))
746 (defun nnimap-parse-response ()
747 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
750 (push (cdr (nnimap-parse-line line)) result))
751 ;; Return the OK/error code first, and then all the "continuation
752 ;; lines" afterwards.
756 ;; Parse an IMAP response line lightly. They look like
757 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
758 ;; the lines into a list of strings and lists of string.
759 (defun nnimap-parse-line (line)
763 (goto-char (point-min))
765 (if (eql (setq char (following-char)) ? )
770 (split-string (buffer-substring
771 (1+ (point)) (1- (search-forward "]")))))
773 (split-string (buffer-substring
774 (1+ (point)) (1- (search-forward ")")))))
777 (buffer-substring (point) (1- (search-forward "\""))))
779 (buffer-substring (point) (if (search-forward " " nil t)
781 (goto-char (point-max))))))
785 (defun nnimap-last-response-string ()
792 (while (and (not (bobp))
793 (eql (following-char) ?*))
795 (unless (eql (following-char) ?*)
797 (buffer-substring (point) end))))
799 (defun nnimap-get-responses (sequences)
801 (dolist (sequence sequences)
802 (goto-char (point-min))
803 (when (re-search-forward (format "^%d " sequence) nil t)
804 (push (list sequence (nnimap-parse-response))
808 (defvar nnimap-incoming-split-list nil)
810 (defun nnimap-fetch-inbox (articles)
812 (nnimap-wait-for-response
815 (nnimap-article-ranges articles)
818 (if (member "IMAP4REV1"
819 (nnimap-capabilities nnimap-object))
820 "BODY.PEEK[HEADER] BODY.PEEK"
822 (if nnimap-split-download-body-default
827 (defun nnimap-split-incoming-mail ()
828 (with-current-buffer (nnimap-buffer)
829 (let ((nnimap-incoming-split-list nil)
830 (nnmail-split-methods nnimap-split-methods)
831 (nnmail-inhibit-default-split-group t)
832 (groups (nnimap-get-groups))
835 (nnimap-command "SELECT %S" nnimap-inbox)
836 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
838 (nnimap-fetch-inbox new-articles)
839 (nnimap-transform-split-mail)
840 (nnheader-ms-strip-cr)
842 (nnmail-split-incoming (current-buffer)
843 #'nnimap-save-mail-spec
845 #'nnimap-dummy-active-number)
846 (when nnimap-incoming-split-list
847 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
849 ;; Create any groups that doesn't already exist on the
852 (unless (member (car spec) groups)
853 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
854 ;; Then copy over all the messages.
857 (let ((group (car spec))
859 (push (list (nnimap-send-command "UID COPY %s %S"
860 (nnimap-article-ranges ranges)
861 (utf7-encode group t))
864 ;; Wait for the last COPY response...
866 (nnimap-wait-for-response (caar sequences))
867 ;; And then mark the successful copy actions as deleted,
868 ;; and possibly expunge them.
869 (nnimap-mark-and-expunge-incoming
870 (nnimap-parse-copied-articles sequences)))))))))
872 (defun nnimap-mark-and-expunge-incoming (range)
874 (setq range (nnimap-article-ranges range))
876 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)
878 ;; If the server supports it, we now delete the message we have
880 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
881 (nnimap-send-command "UID EXPUNGE %s" range))
882 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
883 ;; user has configured it.
884 (nnimap-expunge-inbox
885 (nnimap-send-command "EXPUNGE")))))
887 (defun nnimap-parse-copied-articles (sequences)
888 (let (sequence copied range)
889 (goto-char (point-min))
890 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
891 (setq sequence (string-to-number (match-string 1)))
892 (when (setq range (cadr (assq sequence sequences)))
893 (push (gnus-uncompress-range range) copied)))
894 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
896 (defun nnimap-new-articles (flags)
899 (when (or (null (cdr elem))
900 (and (not (member "\\Deleted" (cdr elem)))
901 (not (member "\\Seen" (cdr elem)))))
902 (push (car elem) new)))
903 (gnus-compress-sequence (nreverse new))))
905 (defun nnimap-make-split-specs (list)
909 (destructuring-bind (article spec) elem
910 (dolist (group (delete nil (mapcar #'car spec)))
911 (unless (setq entry (assoc group specs))
912 (push (setq entry (list group)) specs))
913 (setcdr entry (cons article (cdr entry))))))
914 (dolist (entry specs)
915 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
918 (defun nnimap-transform-split-mail ()
919 (goto-char (point-min))
923 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
924 (delete-region (point) (progn (forward-line 1) (point)))
927 (setq article (match-string 1)
928 bytes (nnimap-get-length))
929 (delete-region (line-beginning-position) (line-end-position))
930 ;; Insert MMDF separator, and a way to remember what this
932 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
933 (forward-char (1+ bytes))
934 (setq bytes (nnimap-get-length))
935 (delete-region (line-beginning-position) (line-end-position))
936 (forward-char (1+ bytes))
937 (delete-region (line-beginning-position) (line-end-position))))))
939 (defun nnimap-dummy-active-number (group &optional server)
942 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
944 (goto-char (point-min))
945 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
946 (error "Invalid nnimap mail")
947 (setq article (string-to-number (match-string 1))))
948 (push (list article group-art)
949 nnimap-incoming-split-list)))
953 ;;; nnimap.el ends here