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 (defvoo nnimap-address nil
45 "The address of the IMAP server.")
47 (defvoo nnimap-server-port nil
49 If nnimap-stream is `ssl', this will default to `imaps'. If not,
50 it will default to `imap'.")
52 (defvoo nnimap-stream 'ssl
53 "How nnimap will talk to the IMAP server.
54 Values are `ssl' and `network'.")
56 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
57 (if (listp imap-shell-program)
58 (car imap-shell-program)
62 (defvoo nnimap-inbox nil
63 "The mail box where incoming mail arrives and should be split out of.")
65 (defvoo nnimap-expunge-inbox nil
66 "If non-nil, expunge the inbox after fetching mail.
67 This is always done if the server supports UID EXPUNGE, but it's
68 not done by default on servers that doesn't support that command.")
70 (defvoo nnimap-authenticator nil
71 "How nnimap authenticate itself to the server.
72 Possible choices are nil (use default methods) or `anonymous'.")
74 (defvoo nnimap-fetch-partial-articles nil
75 "If non-nil, nnimap will fetch partial articles.
76 If t, nnimap will fetch only the first part. If a string, it
77 will fetch all parts that have types that match that string. A
78 likely value would be \"text/\" to automatically fetch all
81 (defvoo nnimap-expunge nil)
83 (defvoo nnimap-connection-alist nil)
85 (defvoo nnimap-current-infos nil)
87 (defvar nnimap-process nil)
89 (defvar nnimap-status-string "")
91 (defvar nnimap-split-download-body-default nil
92 "Internal variable with default value for `nnimap-split-download-body'.")
95 group process commands capabilities select-result newlinep)
97 (defvar nnimap-object nil)
99 (defvar nnimap-mark-alist
103 (expire "gnus-expire")
104 (dormant "gnus-dormant")
107 (download "gnus-download")
108 (forward "gnus-forward")))
110 (defvar nnimap-split-methods nil)
112 (defun nnimap-buffer ()
113 (nnimap-find-process-buffer nntp-server-buffer))
115 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
116 (with-current-buffer nntp-server-buffer
118 (when (nnimap-possibly-change-group group server)
119 (with-current-buffer (nnimap-buffer)
120 (nnimap-send-command "SELECT %S" (utf7-encode group t))
122 (nnimap-wait-for-response
125 (nnimap-article-ranges (gnus-compress-sequence articles))
126 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
128 (if (member "IMAP4REV1"
129 (nnimap-capabilities nnimap-object))
130 "BODY.PEEK[HEADER.FIELDS %s]"
131 "RFC822.HEADER.LINES %s")
132 (append '(Subject From Date Message-Id
133 References In-Reply-To Xref)
134 nnmail-extra-headers))))
136 (nnimap-transform-headers))
137 (insert-buffer-substring
138 (nnimap-find-process-buffer (current-buffer))))
141 (defun nnimap-transform-headers ()
142 (goto-char (point-min))
143 (let (article bytes lines size)
146 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
147 (delete-region (point) (progn (forward-line 1) (point)))
150 (setq article (match-string 1)
151 bytes (nnimap-get-length)
155 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
160 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
161 (let ((structure (ignore-errors (read (current-buffer)))))
162 (while (and (consp structure)
163 (not (stringp (car structure))))
164 (setq structure (car structure)))
165 (setq lines (nth 7 structure))))
166 (delete-region (line-beginning-position) (line-end-position))
167 (insert (format "211 %s Article retrieved." article))
170 (insert (format "Chars: %s\n" size)))
172 (insert (format "Lines: %s\n" lines)))
173 (re-search-forward "^\r$")
174 (delete-region (line-beginning-position) (line-end-position))
178 (defun nnimap-get-length ()
179 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
180 (string-to-number (match-string 1))))
182 (defun nnimap-article-ranges (ranges)
186 (number-to-string ranges))
187 ((numberp (cdr ranges))
188 (format "%d:%d" (car ranges) (cdr ranges)))
190 (dolist (elem ranges)
193 (format "%d:%d" (car elem) (cdr elem))
194 (number-to-string elem))
196 (mapconcat #'identity (nreverse result) ",")))))
198 (deffoo nnimap-open-server (server &optional defs)
199 (if (nnimap-server-opened server)
201 (unless (assq 'nnimap-address defs)
202 (setq defs (append defs (list (list 'nnimap-address server)))))
203 (nnoo-change-server 'nnimap server defs)
204 (or (nnimap-find-connection nntp-server-buffer)
205 (nnimap-open-connection nntp-server-buffer))))
207 (defun nnimap-make-process-buffer (buffer)
209 (generate-new-buffer (format "*nnimap %s %s %s*"
210 nnimap-address nnimap-server-port
211 (gnus-buffer-exists-p buffer)))
212 (mm-disable-multibyte)
213 (buffer-disable-undo)
215 (set (make-local-variable 'after-change-functions) nil)
216 (set (make-local-variable 'nnimap-object) (make-nnimap))
217 (push (list buffer (current-buffer)) nnimap-connection-alist)
220 (defun nnimap-open-shell-stream (name buffer host port)
221 (let ((process-connection-type nil))
222 (start-process name buffer shell-file-name
230 (defun nnimap-credentials (address ports)
231 (let (port credentials)
232 ;; Request the credentials from all ports, but only query on the
233 ;; last port if all the previous ones have failed.
234 (while (and (null credentials)
235 (setq port (pop ports)))
237 (auth-source-user-or-password
238 '("login" "password") address port nil (null ports))))
241 (defun nnimap-open-connection (buffer)
242 (with-current-buffer (nnimap-make-process-buffer buffer)
243 (let* ((coding-system-for-read 'binary)
244 (coding-system-for-write 'binary)
247 ((eq nnimap-stream 'network)
249 "*nnimap*" (current-buffer) nnimap-address
250 (or nnimap-server-port
251 (if (netrc-find-service-number "imap")
255 ((eq nnimap-stream 'shell)
256 (nnimap-open-shell-stream
257 "*nnimap*" (current-buffer) nnimap-address
258 (or nnimap-server-port "imap"))
260 ((eq nnimap-stream 'ssl)
262 "*nnimap*" (current-buffer) nnimap-address
263 (or nnimap-server-port
264 (if (netrc-find-service-number "imaps")
267 '("143" "993" "imap" "imaps"))))
268 connection-result login-result credentials)
269 (setf (nnimap-process nnimap-object)
270 (get-buffer-process (current-buffer)))
271 (when (and (nnimap-process nnimap-object)
272 (memq (process-status (nnimap-process nnimap-object))
274 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
275 (when (setq connection-result (nnimap-wait-for-connection))
276 (unless (equal connection-result "PREAUTH")
277 (if (not (setq credentials
278 (if (eq nnimap-authenticator 'anonymous)
280 (message-make-address))
283 (if nnimap-server-port
284 (cons (format "%s" nnimap-server-port) ports)
286 (setq nnimap-object nil)
287 (setq login-result (nnimap-command "LOGIN %S %S"
290 (unless (car login-result)
291 (delete-process (nnimap-process nnimap-object))
292 (setq nnimap-object nil))))
294 (setf (nnimap-capabilities nnimap-object)
297 (or (nnimap-find-parameter "CAPABILITY" (cdr login-result))
298 (nnimap-find-parameter
299 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
300 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
301 (nnimap-command "ENABLE QRESYNC"))
304 (defun nnimap-find-parameter (parameter elems)
308 ((equal (car elem) parameter)
309 (setq result (cdr elem)))
310 ((and (equal (car elem) "OK")
312 (equal (caadr elem) parameter))
313 (setq result (cdr (cadr elem))))))
316 (deffoo nnimap-close-server (&optional server)
319 (deffoo nnimap-request-close ()
322 (deffoo nnimap-server-opened (&optional server)
323 (and (nnoo-current-server-p 'nnimap server)
325 (gnus-buffer-live-p nntp-server-buffer)
326 (nnimap-find-connection nntp-server-buffer)))
328 (deffoo nnimap-status-message (&optional server)
329 nnimap-status-string)
331 (deffoo nnimap-request-article (article &optional group server to-buffer)
332 (with-current-buffer nntp-server-buffer
333 (let ((result (nnimap-possibly-change-group group server))
335 (when (stringp article)
336 (setq article (nnimap-find-article-by-message-id group article)))
340 (with-current-buffer (nnimap-buffer)
342 (when nnimap-fetch-partial-articles
343 (if (eq nnimap-fetch-partial-articles t)
345 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
346 (goto-char (point-min))
347 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
348 (let ((structure (ignore-errors (read (current-buffer)))))
349 (setq parts (nnimap-find-wanted-parts structure))))))
352 (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object))
353 "UID FETCH %d BODY.PEEK[]"
354 "UID FETCH %d RFC822.PEEK")
356 ;; Check that we really got an article.
357 (goto-char (point-min))
358 (unless (looking-at "\\* [0-9]+ FETCH")
360 (let ((buffer (nnimap-find-process-buffer (current-buffer))))
362 (with-current-buffer (or to-buffer nntp-server-buffer)
363 (insert-buffer-substring buffer)
364 (goto-char (point-min))
365 (let ((bytes (nnimap-get-length)))
366 (delete-region (line-beginning-position)
367 (progn (forward-line 1) (point)))
368 (goto-char (+ (point) bytes))
369 (delete-region (point) (point-max))
370 (nnheader-ms-strip-cr))
371 (cons group article))))))))
373 (defun nnimap-find-wanted-parts (structure)
374 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
376 (defun nnimap-find-wanted-parts-1 (structure prefix)
379 (while (consp (car structure))
380 (let ((sub (pop structure)))
381 (if (consp (car sub))
382 (push (nnimap-find-wanted-parts-1
383 sub (if (string= prefix "")
384 (number-to-string num)
385 (format "%s.%s" prefix num)))
387 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))))
388 (when (string-match nnimap-fetch-partial-articles type)
389 (push (if (string= prefix "")
390 (number-to-string num)
391 (format "%s.%s" prefix num))
396 (deffoo nnimap-request-group (group &optional server dont-check info)
397 (let ((result (nnimap-possibly-change-group group server))
398 articles active marks high low)
399 (with-current-buffer nntp-server-buffer
402 (setq active (nth 2 (assoc group nnimap-current-infos))))
403 (insert (format "211 %d %d %d %S\n"
404 (- (cdr active) (car active))
408 (with-current-buffer (nnimap-buffer)
410 (let ((group-sequence
411 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
413 (nnimap-send-command "UID FETCH 1:* FLAGS")))
414 (nnimap-wait-for-response flag-sequence)
416 (nnimap-flags-to-marks
418 (list (list group-sequence flag-sequence 1 group)))))
420 (nnimap-update-infos marks (list info)))
421 (goto-char (point-max))
424 (setq high (nth 3 (car marks))
425 low (nth 4 (car marks))))
426 ((re-search-backward "UIDNEXT \\([0-9]+\\)" nil t)
427 (setq high (1- (string-to-number (match-string 1)))
432 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
435 (deffoo nnimap-request-create-group (group &optional server args)
436 (when (nnimap-possibly-change-group nil server)
437 (with-current-buffer (nnimap-buffer)
438 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
440 (deffoo nnimap-request-delete-group (group &optional force server)
441 (when (nnimap-possibly-change-group nil server)
442 (with-current-buffer (nnimap-buffer)
443 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
445 (deffoo nnimap-request-expunge-group (group &optional server)
446 (when (nnimap-possibly-change-group group server)
447 (with-current-buffer (nnimap-buffer)
448 (car (nnimap-command "EXPUNGE")))))
450 (defun nnimap-get-flags (spec)
453 (with-current-buffer (nnimap-buffer)
455 (nnimap-wait-for-response (nnimap-send-command
456 "UID FETCH %s FLAGS" spec))
457 (goto-char (point-min))
458 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
459 (setq elems (nnimap-parse-line (match-string 1)))
460 (push (cons (string-to-number (cadr (member "UID" elems)))
461 (cadr (member "FLAGS" elems)))
463 (nreverse articles)))
465 (deffoo nnimap-close-group (group &optional server)
468 (deffoo nnimap-request-move-article (article group server accept-form
469 &optional last internal-move-group)
471 (when (nnimap-request-article article group server (current-buffer))
472 ;; If the move is internal (on the same server), just do it the easy
474 (let ((message-id (message-field-value "message-id")))
475 (if internal-move-group
477 (with-current-buffer (nnimap-buffer)
478 (nnimap-command "UID COPY %d %S"
480 (utf7-encode internal-move-group t)))))
482 (nnimap-delete-article article)
483 (cons internal-move-group
484 (nnimap-find-article-by-message-id
485 internal-move-group message-id))))
486 ;; Move the article to a different method.
487 (let ((result (eval accept-form)))
489 (nnimap-delete-article article)
492 (deffoo nnimap-request-expire-articles (articles group &optional server force)
496 ((not (nnimap-possibly-change-group group server))
499 (eq nnmail-expiry-target 'delete))
500 (unless (nnimap-delete-article articles)
501 (message "Article marked for deletion, but not expunged."))
504 (let ((deletable-articles
507 (gnus-sorted-intersection
509 (nnimap-find-expired-articles group)))))
510 (if (null deletable-articles)
512 (if (eq nnmail-expiry-target 'delete)
513 (nnimap-delete-article deletable-articles)
514 (setq deletable-articles
515 (nnimap-process-expiry-targets
516 deletable-articles group server)))
517 ;; Return the articles we didn't delete.
518 (gnus-sorted-complement articles deletable-articles))))))
520 (defun nnimap-process-expiry-targets (articles group server)
521 (let ((deleted-articles nil))
522 (dolist (article articles)
523 (let ((target nnmail-expiry-target))
525 (when (nnimap-request-article article group server (current-buffer))
526 (message "Expiring article %s:%d" group article)
527 (when (functionp target)
528 (setq target (funcall target group)))
530 (not (eq target 'delete)))
531 (if (or (gnus-request-group target t)
532 (gnus-request-create-group target))
533 (nnmail-expiry-target-group target group)
536 (push article deleted-articles))))))
537 ;; Change back to the current group again.
538 (nnimap-possibly-change-group group server)
539 (setq deleted-articles (nreverse deleted-articles))
540 (nnimap-delete-article deleted-articles)
543 (defun nnimap-find-expired-articles (group)
544 (let ((cutoff (nnmail-expired-article-p group nil nil)))
545 (with-current-buffer (nnimap-buffer)
548 "UID SEARCH SENTBEFORE %s"
552 (car (rassoc (nth 4 (decode-time cutoff))
553 parse-time-months))))
556 (delete 0 (mapcar #'string-to-number
557 (cdr (assoc "SEARCH" (cdr result))))))))))
560 (defun nnimap-find-article-by-message-id (group message-id)
561 (when (nnimap-possibly-change-group group nil)
562 (with-current-buffer (nnimap-buffer)
564 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
567 ;; Select the last instance of the message in the group.
569 (car (last (assoc "SEARCH" (cdr result)))))
570 (string-to-number article)))))))
572 (defun nnimap-delete-article (articles)
573 (with-current-buffer (nnimap-buffer)
574 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
575 (nnimap-article-ranges articles))
577 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
578 (nnimap-command "UID EXPUNGE %s"
579 (nnimap-article-ranges articles))
582 (nnimap-command "EXPUNGE")
585 (deffoo nnimap-request-scan (&optional group server)
586 (when (and (nnimap-possibly-change-group nil server)
587 (equal group nnimap-inbox)
589 nnimap-split-methods)
590 (nnimap-split-incoming-mail)))
592 (defun nnimap-marks-to-flags (marks)
595 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
599 (deffoo nnimap-request-set-mark (group actions &optional server)
600 (when (nnimap-possibly-change-group group server)
602 (with-current-buffer (nnimap-buffer)
603 ;; Just send all the STORE commands without waiting for
604 ;; response. If they're successful, they're successful.
605 (dolist (action actions)
606 (destructuring-bind (range action marks) action
607 (let ((flags (nnimap-marks-to-flags marks)))
609 (setq sequence (nnimap-send-command
610 "UID STORE %s %sFLAGS.SILENT (%s)"
611 (nnimap-article-ranges range)
615 (mapconcat #'identity flags " ")))))))
616 ;; Wait for the last command to complete to avoid later
617 ;; syncronisation problems with the stream.
619 (nnimap-wait-for-response sequence))))))
621 (deffoo nnimap-request-accept-article (group &optional server last)
622 (when (nnimap-possibly-change-group nil server)
623 (nnmail-check-syntax)
624 (let ((message (buffer-string))
625 (message-id (message-field-value "message-id"))
627 (with-current-buffer (nnimap-buffer)
628 (setq sequence (nnimap-send-command
629 "APPEND %S {%d}" (utf7-encode group t)
631 (process-send-string (get-buffer-process (current-buffer)) message)
632 (process-send-string (get-buffer-process (current-buffer))
633 (if (nnimap-newlinep nnimap-object)
636 (let ((result (nnimap-get-response sequence)))
639 (nnimap-find-article-by-message-id group message-id))))))))
641 (defun nnimap-add-cr ()
642 (goto-char (point-min))
643 (while (re-search-forward "\r?\n" nil t)
644 (replace-match "\r\n" t t)))
646 (defun nnimap-get-groups ()
647 (let ((result (nnimap-command "LIST \"\" \"*\""))
650 (dolist (line (cdr result))
651 (when (and (equal (car line) "LIST")
652 (not (and (caadr line)
653 (string-match "noselect" (caadr line)))))
654 (push (car (last line)) groups)))
657 (deffoo nnimap-request-list (&optional server)
658 (nnimap-possibly-change-group nil server)
659 (with-current-buffer nntp-server-buffer
662 (with-current-buffer (nnimap-buffer)
663 (nnimap-get-groups)))
666 (with-current-buffer (nnimap-buffer)
667 (dolist (group groups)
668 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
671 (nnimap-wait-for-response (caar sequences))
673 (nnimap-get-responses (mapcar #'car sequences))))
674 (dolist (response responses)
675 (let* ((sequence (car response))
676 (response (cadr response))
677 (group (cadr (assoc sequence sequences))))
679 (equal (caar response) "OK"))
680 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
682 (dolist (elem response)
683 (when (equal (cadr elem) "EXISTS")
684 (setq exists (string-to-number (car elem)))))
686 (setq highest (1- (string-to-number (car uidnext)))))
689 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
692 (insert (format "%S %d %d y\n"
693 (utf7-decode group t) highest (1+ highest))))
695 ;; Return the widest possible range.
696 (insert (format "%S %d 1 y\n" (utf7-decode group t)
697 (or highest exists)))))))))
700 (deffoo nnimap-retrieve-group-data-early (server infos)
701 (when (nnimap-possibly-change-group nil server)
702 (with-current-buffer (nnimap-buffer)
703 ;; QRESYNC handling isn't implemented.
704 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
705 marks groups sequences)
706 ;; Go through the infos and gather the data needed to know
707 ;; what and how to request the data.
709 (setq marks (gnus-info-marks info))
710 (push (list (gnus-group-real-name (gnus-info-group info))
711 (cdr (assq 'active marks))
712 (cdr (assq 'uid marks)))
714 ;; Then request the data.
716 (dolist (elem groups)
721 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
730 ;; Fetch the last 100 flags.
731 (max 1 (- (cdr (nth 1 elem)) 100))
733 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
734 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
740 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
742 (nnimap-possibly-change-group nil server))
743 (with-current-buffer (nnimap-buffer)
744 ;; Wait for the final data to trickle in.
745 (nnimap-wait-for-response (cadar sequences))
746 ;; Now we should have all the data we need, no matter whether
747 ;; we're QRESYNCING, fetching all the flags from scratch, or
748 ;; just fetching the last 100 flags per group.
749 (nnimap-update-infos (nnimap-flags-to-marks
751 (nreverse sequences)))
753 ;; Finally, just return something resembling an active file in
754 ;; the nntp buffer, so that the agent can save the info, too.
755 (with-current-buffer nntp-server-buffer
758 (let* ((group (gnus-info-group info))
759 (active (gnus-active group)))
761 (insert (format "%S %d %d y\n"
762 (gnus-group-real-name group)
764 (car active))))))))))
766 (defun nnimap-update-infos (flags infos)
768 (let ((group (gnus-group-real-name (gnus-info-group info))))
769 (nnimap-update-info info (cdr (assoc group flags))))))
771 (defun nnimap-update-info (info marks)
773 (destructuring-bind (existing flags high low uidnext start-article) marks
774 (let ((group (gnus-info-group info))
775 (completep (and start-article
776 (= start-article 1))))
777 ;; First set the active ranges based on high/low.
779 (not (gnus-active group)))
780 (gnus-set-active group
783 ;; No articles in this group.
784 (cons (1- uidnext) uidnext)))
785 (setcdr (gnus-active group) high))
786 ;; Then update the list of read articles.
788 (gnus-compress-sequence
792 (cdr (assoc "\\Seen" flags)))
793 (cdr (assoc "\\Flagged" flags)))))
794 (read (gnus-range-difference
795 (cons start-article high) unread)))
796 (when (> start-article 1)
799 (if (> start-article 1)
800 (gnus-sorted-range-intersection
801 (cons 1 (1- start-article))
802 (gnus-info-read info))
803 (gnus-info-read info))
805 (gnus-info-set-read info read)
807 (setq marks (gnus-info-marks info))
808 ;; Note the active level for the next run-through.
809 (let ((active (assq 'active marks)))
811 (setcdr active (gnus-active group))
812 (push (cons 'active (gnus-active group)) marks)))
813 (dolist (type (cdr nnimap-mark-alist))
814 (let ((old-marks (assoc (car type) marks))
815 (new-marks (gnus-compress-sequence
816 (cdr (assoc (cadr type) flags)))))
817 (setq marks (delq old-marks marks))
821 (setq old-marks (gnus-range-difference
823 (cons start-article high)))
824 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
826 (push (cons (car type) new-marks) marks)))
827 (gnus-info-set-marks info marks t)
828 (nnimap-store-info info (gnus-active group))))))))
830 (defun nnimap-store-info (info active)
831 (let* ((group (gnus-group-real-name (gnus-info-group info)))
832 (entry (assoc group nnimap-current-infos)))
834 (setcdr entry (list info active))
835 (push (list group info active) nnimap-current-infos))))
837 (defun nnimap-flags-to-marks (groups)
838 (let (data group totalp uidnext articles start-article mark)
839 (dolist (elem groups)
840 (setq group (car elem)
842 start-article (caddr elem)
843 articles (cdddr elem))
844 (let ((high (caar articles))
846 (dolist (article articles)
847 (setq low (car article))
848 (push (car article) existing)
849 (dolist (flag (cdr article))
850 (setq mark (assoc flag marks))
852 (push (list flag (car article)) marks)
853 (setcdr mark (cons (car article) (cdr mark)))))
854 (push (list group existing marks high low uidnext start-article)
858 (defun nnimap-parse-flags (sequences)
859 (goto-char (point-min))
860 (let (start end articles groups uidnext elems)
861 (dolist (elem sequences)
862 (destructuring-bind (group-sequence flag-sequence totalp group) elem
863 ;; The EXAMINE was successful.
864 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
868 (if (re-search-backward "UIDNEXT \\([0-9]+\\)"
869 (or end (point-min)) t)
870 (setq uidnext (string-to-number (match-string 1)))
873 ;; The UID FETCH FLAGS was successful.
874 (search-forward (format "\n%d OK " flag-sequence) nil t))
877 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" end t)
878 (setq elems (nnimap-parse-line (match-string 1)))
879 (push (cons (string-to-number (cadr (member "UID" elems)))
880 (cadr (member "FLAGS" elems)))
882 (push (nconc (list group uidnext totalp) articles) groups)
883 (setq articles nil))))
886 (defun nnimap-find-process-buffer (buffer)
887 (cadr (assoc buffer nnimap-connection-alist)))
889 (deffoo nnimap-request-post (&optional server)
890 (setq nnimap-status-string "Read-only server")
893 (defun nnimap-possibly-change-group (group server)
894 (let ((open-result t))
896 (not (nnimap-server-opened server)))
897 (setq open-result (nnimap-open-server server)))
904 (with-current-buffer (nnimap-buffer)
905 (if (equal group (nnimap-group nnimap-object))
907 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
909 (setf (nnimap-group nnimap-object) group
910 (nnimap-select-result nnimap-object) result)
913 (defun nnimap-find-connection (buffer)
914 "Find the connection delivering to BUFFER."
915 (let ((entry (assoc buffer nnimap-connection-alist)))
917 (if (and (buffer-name (cadr entry))
918 (get-buffer-process (cadr entry))
919 (memq (process-status (get-buffer-process (cadr entry)))
921 (get-buffer-process (cadr entry))
922 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
925 (defvar nnimap-sequence 0)
927 (defun nnimap-send-command (&rest args)
929 (get-buffer-process (current-buffer))
932 (incf nnimap-sequence)
933 (apply #'format args)
934 (if (nnimap-newlinep nnimap-object)
939 (defun nnimap-log-command (command)
940 (with-current-buffer (get-buffer-create "*imap log*")
941 (goto-char (point-max))
942 (insert (format-time-string "%H:%M:%S") " " command))
945 (defun nnimap-command (&rest args)
947 (let* ((sequence (apply #'nnimap-send-command args))
948 (response (nnimap-get-response sequence)))
949 (if (equal (caar response) "OK")
951 (nnheader-report 'nnimap "%s"
952 (mapconcat (lambda (a)
957 (defun nnimap-get-response (sequence)
958 (nnimap-wait-for-response sequence)
959 (nnimap-parse-response))
961 (defun nnimap-wait-for-connection ()
962 (let ((process (get-buffer-process (current-buffer))))
963 (goto-char (point-min))
964 (while (and (memq (process-status process)
966 (not (re-search-forward "^\\* .*\n" nil t)))
967 (nnheader-accept-process-output process)
968 (goto-char (point-min)))
970 (and (looking-at "\\* \\([A-Z0-9]+\\)")
973 (defun nnimap-wait-for-response (sequence &optional messagep)
974 (let ((process (get-buffer-process (current-buffer))))
975 (goto-char (point-max))
976 (while (and (memq (process-status process)
978 (not (re-search-backward (format "^%d .*\n" sequence)
979 (max (point-min) (- (point) 500))
982 (message "Read %dKB" (/ (buffer-size) 1000)))
983 (nnheader-accept-process-output process)
984 (goto-char (point-max)))))
986 (defun nnimap-parse-response ()
987 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
990 (push (cdr (nnimap-parse-line line)) result))
991 ;; Return the OK/error code first, and then all the "continuation
992 ;; lines" afterwards.
996 ;; Parse an IMAP response line lightly. They look like
997 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
998 ;; the lines into a list of strings and lists of string.
999 (defun nnimap-parse-line (line)
1003 (goto-char (point-min))
1005 (if (eql (setq char (following-char)) ? )
1010 (split-string (buffer-substring
1011 (1+ (point)) (1- (search-forward "]")))))
1013 (split-string (buffer-substring
1014 (1+ (point)) (1- (search-forward ")")))))
1017 (buffer-substring (point) (1- (search-forward "\""))))
1019 (buffer-substring (point) (if (search-forward " " nil t)
1021 (goto-char (point-max))))))
1023 (nreverse result))))
1025 (defun nnimap-last-response-string ()
1028 (let ((end (point)))
1032 (while (and (not (bobp))
1033 (eql (following-char) ?*))
1035 (unless (eql (following-char) ?*)
1037 (buffer-substring (point) end))))
1039 (defun nnimap-get-responses (sequences)
1041 (dolist (sequence sequences)
1042 (goto-char (point-min))
1043 (when (re-search-forward (format "^%d " sequence) nil t)
1044 (push (list sequence (nnimap-parse-response))
1048 (defvar nnimap-incoming-split-list nil)
1050 (defun nnimap-fetch-inbox (articles)
1052 (nnimap-wait-for-response
1053 (nnimap-send-command
1055 (nnimap-article-ranges articles)
1056 (format "(UID %s%s)"
1058 (if (member "IMAP4REV1"
1059 (nnimap-capabilities nnimap-object))
1060 "BODY.PEEK[HEADER] BODY.PEEK"
1062 (if nnimap-split-download-body-default
1067 (defun nnimap-split-incoming-mail ()
1068 (with-current-buffer (nnimap-buffer)
1069 (let ((nnimap-incoming-split-list nil)
1070 (nnmail-split-methods nnimap-split-methods)
1071 (nnmail-inhibit-default-split-group t)
1072 (groups (nnimap-get-groups))
1075 (nnimap-command "SELECT %S" nnimap-inbox)
1076 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1078 (nnimap-fetch-inbox new-articles)
1079 (nnimap-transform-split-mail)
1080 (nnheader-ms-strip-cr)
1082 (nnmail-split-incoming (current-buffer)
1083 #'nnimap-save-mail-spec
1085 #'nnimap-dummy-active-number)
1086 (when nnimap-incoming-split-list
1087 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1089 ;; Create any groups that doesn't already exist on the
1091 (dolist (spec specs)
1092 (unless (member (car spec) groups)
1093 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1094 ;; Then copy over all the messages.
1096 (dolist (spec specs)
1097 (let ((group (car spec))
1098 (ranges (cdr spec)))
1099 (push (list (nnimap-send-command "UID COPY %s %S"
1100 (nnimap-article-ranges ranges)
1101 (utf7-encode group t))
1104 ;; Wait for the last COPY response...
1106 (nnimap-wait-for-response (caar sequences))
1107 ;; And then mark the successful copy actions as deleted,
1108 ;; and possibly expunge them.
1109 (nnimap-mark-and-expunge-incoming
1110 (nnimap-parse-copied-articles sequences)))))))))
1112 (defun nnimap-mark-and-expunge-incoming (range)
1114 (setq range (nnimap-article-ranges range))
1116 (nnimap-send-command
1117 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1119 ;; If the server supports it, we now delete the message we have
1120 ;; just copied over.
1121 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1122 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1123 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1124 ;; user has configured it.
1125 (nnimap-expunge-inbox
1126 (setq sequence (nnimap-send-command "EXPUNGE"))))
1127 (nnimap-wait-for-response sequence))))
1129 (defun nnimap-parse-copied-articles (sequences)
1130 (let (sequence copied range)
1131 (goto-char (point-min))
1132 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1133 (setq sequence (string-to-number (match-string 1)))
1134 (when (setq range (cadr (assq sequence sequences)))
1135 (push (gnus-uncompress-range range) copied)))
1136 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1138 (defun nnimap-new-articles (flags)
1140 (dolist (elem flags)
1141 (when (or (null (cdr elem))
1142 (and (not (member "\\Deleted" (cdr elem)))
1143 (not (member "\\Seen" (cdr elem)))))
1144 (push (car elem) new)))
1145 (gnus-compress-sequence (nreverse new))))
1147 (defun nnimap-make-split-specs (list)
1151 (destructuring-bind (article spec) elem
1152 (dolist (group (delete nil (mapcar #'car spec)))
1153 (unless (setq entry (assoc group specs))
1154 (push (setq entry (list group)) specs))
1155 (setcdr entry (cons article (cdr entry))))))
1156 (dolist (entry specs)
1157 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1160 (defun nnimap-transform-split-mail ()
1161 (goto-char (point-min))
1162 (let (article bytes)
1165 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1166 (delete-region (point) (progn (forward-line 1) (point)))
1169 (setq article (match-string 1)
1170 bytes (nnimap-get-length))
1171 (delete-region (line-beginning-position) (line-end-position))
1172 ;; Insert MMDF separator, and a way to remember what this
1174 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1175 (forward-char (1+ bytes))
1176 (setq bytes (nnimap-get-length))
1177 (delete-region (line-beginning-position) (line-end-position))
1178 (forward-char (1+ bytes))
1179 (delete-region (line-beginning-position) (line-end-position))))))
1181 (defun nnimap-dummy-active-number (group &optional server)
1184 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1186 (goto-char (point-min))
1187 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1188 (error "Invalid nnimap mail")
1189 (setq article (string-to-number (match-string 1))))
1190 (push (list article group-art)
1191 nnimap-incoming-split-list)))
1195 ;;; nnimap.el ends here