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', `network', `starttls' or `shell'.")
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-authenticator nil
66 "How nnimap authenticate itself to the server.
67 Possible choices are nil (use default methods) or `anonymous'.")
69 (defvoo nnimap-fetch-partial-articles nil
70 "If non-nil, nnimap will fetch partial articles.
71 If t, nnimap will fetch only the first part. If a string, it
72 will fetch all parts that have types that match that string. A
73 likely value would be \"text/\" to automatically fetch all
76 (defvoo nnimap-expunge t
77 "If non-nil, expunge articles after deleting them.
78 This is always done if the server supports UID EXPUNGE, but it's
79 not done by default on servers that doesn't support that command.")
82 (defvoo nnimap-connection-alist nil)
84 (defvoo nnimap-current-infos nil)
86 (defvar nnimap-process nil)
88 (defvar nnimap-status-string "")
90 (defvar nnimap-split-download-body-default nil
91 "Internal variable with default value for `nnimap-split-download-body'.")
94 group process commands capabilities select-result newlinep server)
96 (defvar nnimap-object nil)
98 (defvar nnimap-mark-alist
99 '((read "\\Seen" %Seen)
100 (tick "\\Flagged" %Flagged)
101 (reply "\\Answered" %Answered)
102 (expire "gnus-expire")
103 (dormant "gnus-dormant")
106 (download "gnus-download")
107 (forward "gnus-forward")))
109 (defvar nnimap-split-methods nil)
111 (defun nnimap-buffer ()
112 (nnimap-find-process-buffer nntp-server-buffer))
114 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
115 (with-current-buffer nntp-server-buffer
117 (when (nnimap-possibly-change-group group server)
118 (with-current-buffer (nnimap-buffer)
119 (nnimap-send-command "SELECT %S" (utf7-encode group t))
121 (nnimap-wait-for-response
124 (nnimap-article-ranges (gnus-compress-sequence articles))
125 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
127 (if (member "IMAP4REV1"
128 (nnimap-capabilities nnimap-object))
129 "BODY.PEEK[HEADER.FIELDS %s]"
130 "RFC822.HEADER.LINES %s")
131 (append '(Subject From Date Message-Id
132 References In-Reply-To Xref)
133 nnmail-extra-headers))))
135 (nnimap-transform-headers))
136 (insert-buffer-substring
137 (nnimap-find-process-buffer (current-buffer))))
140 (defun nnimap-transform-headers ()
141 (goto-char (point-min))
142 (let (article bytes lines size string)
145 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
146 (delete-region (point) (progn (forward-line 1) (point)))
149 (setq article (match-string 1))
150 ;; Unfold quoted {number} strings.
151 (while (re-search-forward "[^]] {\\([0-9]+\\)}\r\n"
152 (1+ (line-end-position)) t)
153 (setq size (string-to-number (match-string 1)))
154 (delete-region (+ (match-beginning 0) 2) (point))
155 (setq string (delete-region (point) (+ (point) size)))
156 (insert (format "%S" string)))
157 (setq bytes (nnimap-get-length)
161 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
166 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
167 (let ((structure (ignore-errors
168 (read (current-buffer)))))
169 (while (and (consp structure)
170 (not (stringp (car structure))))
171 (setq structure (car structure)))
172 (setq lines (nth 7 structure))))
173 (delete-region (line-beginning-position) (line-end-position))
174 (insert (format "211 %s Article retrieved." article))
177 (insert (format "Chars: %s\n" size)))
179 (insert (format "Lines: %s\n" lines)))
180 (re-search-forward "^\r$")
181 (delete-region (line-beginning-position) (line-end-position))
185 (defun nnimap-get-length ()
186 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
187 (string-to-number (match-string 1))))
189 (defun nnimap-article-ranges (ranges)
193 (number-to-string ranges))
194 ((numberp (cdr ranges))
195 (format "%d:%d" (car ranges) (cdr ranges)))
197 (dolist (elem ranges)
200 (format "%d:%d" (car elem) (cdr elem))
201 (number-to-string elem))
203 (mapconcat #'identity (nreverse result) ",")))))
205 (deffoo nnimap-open-server (server &optional defs)
206 (if (nnimap-server-opened server)
208 (unless (assq 'nnimap-address defs)
209 (setq defs (append defs (list (list 'nnimap-address server)))))
210 (nnoo-change-server 'nnimap server defs)
211 (or (nnimap-find-connection nntp-server-buffer)
212 (nnimap-open-connection nntp-server-buffer))))
214 (defun nnimap-make-process-buffer (buffer)
216 (generate-new-buffer (format "*nnimap %s %s %s*"
217 nnimap-address nnimap-server-port
218 (gnus-buffer-exists-p buffer)))
219 (mm-disable-multibyte)
220 (buffer-disable-undo)
222 (set (make-local-variable 'after-change-functions) nil)
223 (set (make-local-variable 'nnimap-object)
224 (make-nnimap :server (nnoo-current-server 'nnimap)))
225 (push (list buffer (current-buffer)) nnimap-connection-alist)
228 (defun nnimap-open-shell-stream (name buffer host port)
229 (let ((process-connection-type nil))
230 (start-process name buffer shell-file-name
238 (defun nnimap-credentials (address ports)
239 (let (port credentials)
240 ;; Request the credentials from all ports, but only query on the
241 ;; last port if all the previous ones have failed.
242 (while (and (null credentials)
243 (setq port (pop ports)))
245 (auth-source-user-or-password
246 '("login" "password") address port nil (null ports))))
249 (defun nnimap-open-connection (buffer)
250 (with-current-buffer (nnimap-make-process-buffer buffer)
251 (let* ((coding-system-for-read 'binary)
252 (coding-system-for-write 'binary)
255 ((eq nnimap-stream 'network)
257 "*nnimap*" (current-buffer) nnimap-address
258 (or nnimap-server-port
259 (if (netrc-find-service-number "imap")
263 ((eq nnimap-stream 'shell)
264 (nnimap-open-shell-stream
265 "*nnimap*" (current-buffer) nnimap-address
266 (or nnimap-server-port "imap"))
268 ((eq nnimap-stream 'starttls)
269 (starttls-open-stream
270 "*nnimap*" (current-buffer) nnimap-address
271 (or nnimap-server-port "imap"))
273 ((eq nnimap-stream 'ssl)
275 "*nnimap*" (current-buffer) nnimap-address
276 (or nnimap-server-port
277 (if (netrc-find-service-number "imaps")
280 '("143" "993" "imap" "imaps"))))
281 connection-result login-result credentials)
282 (setf (nnimap-process nnimap-object)
283 (get-buffer-process (current-buffer)))
284 (when (and (nnimap-process nnimap-object)
285 (memq (process-status (nnimap-process nnimap-object))
287 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
288 (when (setq connection-result (nnimap-wait-for-connection))
289 (when (eq nnimap-stream 'starttls)
290 (nnimap-send-command "STARTTLS")
291 (starttls-negotiate (nnimap-process nnimap-object)))
292 (unless (equal connection-result "PREAUTH")
293 (if (not (setq credentials
294 (if (eq nnimap-authenticator 'anonymous)
296 (message-make-address))
299 (if nnimap-server-port
300 (cons (format "%s" nnimap-server-port) ports)
302 (setq nnimap-object nil)
303 (setq login-result (nnimap-command "LOGIN %S %S"
306 (unless (car login-result)
307 (delete-process (nnimap-process nnimap-object))
308 (setq nnimap-object nil))))
310 (setf (nnimap-capabilities nnimap-object)
313 (or (nnimap-find-parameter "CAPABILITY" (cdr login-result))
314 (nnimap-find-parameter
315 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
316 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
317 (nnimap-command "ENABLE QRESYNC"))
320 (defun nnimap-find-parameter (parameter elems)
324 ((equal (car elem) parameter)
325 (setq result (cdr elem)))
326 ((and (equal (car elem) "OK")
328 (equal (caadr elem) parameter))
329 (setq result (cdr (cadr elem))))))
332 (deffoo nnimap-close-server (&optional server)
335 (deffoo nnimap-request-close ()
338 (deffoo nnimap-server-opened (&optional server)
339 (and (nnoo-current-server-p 'nnimap server)
341 (gnus-buffer-live-p nntp-server-buffer)
342 (nnimap-find-connection nntp-server-buffer)))
344 (deffoo nnimap-status-message (&optional server)
345 nnimap-status-string)
347 (deffoo nnimap-request-article (article &optional group server to-buffer)
348 (with-current-buffer nntp-server-buffer
349 (let ((result (nnimap-possibly-change-group group server))
351 (when (stringp article)
352 (setq article (nnimap-find-article-by-message-id group article)))
356 (with-current-buffer (nnimap-buffer)
358 (when nnimap-fetch-partial-articles
359 (if (eq nnimap-fetch-partial-articles t)
361 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
362 (goto-char (point-min))
363 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
364 (let ((structure (ignore-errors (read (current-buffer)))))
365 (setq parts (nnimap-find-wanted-parts structure))))))
368 (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object))
369 "UID FETCH %d BODY.PEEK[]"
370 "UID FETCH %d RFC822.PEEK")
372 ;; Check that we really got an article.
373 (goto-char (point-min))
374 (unless (looking-at "\\* [0-9]+ FETCH")
376 (let ((buffer (nnimap-find-process-buffer (current-buffer))))
378 (with-current-buffer (or to-buffer nntp-server-buffer)
379 (insert-buffer-substring buffer)
380 (goto-char (point-min))
381 (let ((bytes (nnimap-get-length)))
382 (delete-region (line-beginning-position)
383 (progn (forward-line 1) (point)))
384 (goto-char (+ (point) bytes))
385 (delete-region (point) (point-max))
386 (nnheader-ms-strip-cr))
387 (cons group article))))))))
389 (defun nnimap-find-wanted-parts (structure)
390 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
392 (defun nnimap-find-wanted-parts-1 (structure prefix)
395 (while (consp (car structure))
396 (let ((sub (pop structure)))
397 (if (consp (car sub))
398 (push (nnimap-find-wanted-parts-1
399 sub (if (string= prefix "")
400 (number-to-string num)
401 (format "%s.%s" prefix num)))
403 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))))
404 (when (string-match nnimap-fetch-partial-articles type)
405 (push (if (string= prefix "")
406 (number-to-string num)
407 (format "%s.%s" prefix num))
412 (deffoo nnimap-request-group (group &optional server dont-check info)
413 (let ((result (nnimap-possibly-change-group group server))
414 articles active marks high low)
415 (with-current-buffer nntp-server-buffer
418 (setq active (nth 2 (assoc group nnimap-current-infos))))
419 (insert (format "211 %d %d %d %S\n"
420 (- (cdr active) (car active))
424 (with-current-buffer (nnimap-buffer)
426 (let ((group-sequence
427 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
429 (nnimap-send-command "UID FETCH 1:* FLAGS")))
430 (nnimap-wait-for-response flag-sequence)
432 (nnimap-flags-to-marks
434 (list (list group-sequence flag-sequence 1 group)))))
436 (nnimap-update-infos marks (list info)))
437 (goto-char (point-max))
438 (let ((uidnext (nth 5 (car marks))))
439 (setq high (if uidnext
442 low (or (nth 4 (car marks)) uidnext)))))
446 "211 %d %d %d %S\n" (1+ (- high low)) low high group)))
449 (deffoo nnimap-request-create-group (group &optional server args)
450 (when (nnimap-possibly-change-group nil server)
451 (with-current-buffer (nnimap-buffer)
452 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
454 (deffoo nnimap-request-delete-group (group &optional force server)
455 (when (nnimap-possibly-change-group nil server)
456 (with-current-buffer (nnimap-buffer)
457 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
459 (deffoo nnimap-request-expunge-group (group &optional server)
460 (when (nnimap-possibly-change-group group server)
461 (with-current-buffer (nnimap-buffer)
462 (car (nnimap-command "EXPUNGE")))))
464 (defun nnimap-get-flags (spec)
467 (with-current-buffer (nnimap-buffer)
469 (nnimap-wait-for-response (nnimap-send-command
470 "UID FETCH %s FLAGS" spec))
471 (goto-char (point-min))
472 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
473 (setq elems (nnimap-parse-line (match-string 1)))
474 (push (cons (string-to-number (cadr (member "UID" elems)))
475 (cadr (member "FLAGS" elems)))
477 (nreverse articles)))
479 (deffoo nnimap-close-group (group &optional server)
482 (deffoo nnimap-request-move-article (article group server accept-form
483 &optional last internal-move-group)
485 (when (nnimap-request-article article group server (current-buffer))
486 ;; If the move is internal (on the same server), just do it the easy
488 (let ((message-id (message-field-value "message-id")))
489 (if internal-move-group
491 (with-current-buffer (nnimap-buffer)
492 (nnimap-command "UID COPY %d %S"
494 (utf7-encode internal-move-group t)))))
496 (nnimap-delete-article article)
497 (cons internal-move-group
498 (nnimap-find-article-by-message-id
499 internal-move-group message-id))))
500 ;; Move the article to a different method.
501 (let ((result (eval accept-form)))
503 (nnimap-delete-article article)
506 (deffoo nnimap-request-expire-articles (articles group &optional server force)
510 ((not (nnimap-possibly-change-group group server))
513 (eq nnmail-expiry-target 'delete))
514 (unless (nnimap-delete-article articles)
515 (message "Article marked for deletion, but not expunged."))
518 (let ((deletable-articles
520 (eq nnmail-expiry-wait 'immediate))
522 (gnus-sorted-intersection
524 (nnimap-find-expired-articles group)))))
525 (if (null deletable-articles)
527 (if (eq nnmail-expiry-target 'delete)
528 (nnimap-delete-article deletable-articles)
529 (setq deletable-articles
530 (nnimap-process-expiry-targets
531 deletable-articles group server)))
532 ;; Return the articles we didn't delete.
533 (gnus-sorted-complement articles deletable-articles))))))
535 (defun nnimap-process-expiry-targets (articles group server)
536 (let ((deleted-articles nil))
537 (dolist (article articles)
538 (let ((target nnmail-expiry-target))
540 (when (nnimap-request-article article group server (current-buffer))
541 (message "Expiring article %s:%d" group article)
542 (when (functionp target)
543 (setq target (funcall target group)))
545 (not (eq target 'delete)))
546 (if (or (gnus-request-group target t)
547 (gnus-request-create-group target))
548 (nnmail-expiry-target-group target group)
551 (push article deleted-articles))))))
552 ;; Change back to the current group again.
553 (nnimap-possibly-change-group group server)
554 (setq deleted-articles (nreverse deleted-articles))
555 (nnimap-delete-article deleted-articles)
558 (defun nnimap-find-expired-articles (group)
559 (let ((cutoff (nnmail-expired-article-p group nil nil)))
560 (with-current-buffer (nnimap-buffer)
563 "UID SEARCH SENTBEFORE %s"
567 (car (rassoc (nth 4 (decode-time cutoff))
568 parse-time-months))))
571 (delete 0 (mapcar #'string-to-number
572 (cdr (assoc "SEARCH" (cdr result))))))))))
575 (defun nnimap-find-article-by-message-id (group message-id)
576 (when (nnimap-possibly-change-group group nil)
577 (with-current-buffer (nnimap-buffer)
579 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
582 ;; Select the last instance of the message in the group.
584 (car (last (assoc "SEARCH" (cdr result)))))
585 (string-to-number article)))))))
587 (defun nnimap-delete-article (articles)
588 (with-current-buffer (nnimap-buffer)
589 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
590 (nnimap-article-ranges articles))
592 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
593 (nnimap-command "UID EXPUNGE %s"
594 (nnimap-article-ranges articles))
597 (nnimap-command "EXPUNGE")
599 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
600 "server doesn't support UIDPLUS, so we won't "
601 "delete this article now"))))))
603 (deffoo nnimap-request-scan (&optional group server)
604 (when (and (nnimap-possibly-change-group nil server)
606 nnimap-split-methods)
607 (message "nnimap %s splitting mail..." server)
608 (nnimap-split-incoming-mail)))
610 (defun nnimap-marks-to-flags (marks)
613 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
617 (deffoo nnimap-request-set-mark (group actions &optional server)
618 (when (nnimap-possibly-change-group group server)
620 (with-current-buffer (nnimap-buffer)
621 ;; Just send all the STORE commands without waiting for
622 ;; response. If they're successful, they're successful.
623 (dolist (action actions)
624 (destructuring-bind (range action marks) action
625 (let ((flags (nnimap-marks-to-flags marks)))
627 (setq sequence (nnimap-send-command
628 "UID STORE %s %sFLAGS.SILENT (%s)"
629 (nnimap-article-ranges range)
633 (mapconcat #'identity flags " ")))))))
634 ;; Wait for the last command to complete to avoid later
635 ;; syncronisation problems with the stream.
637 (nnimap-wait-for-response sequence))))))
639 (deffoo nnimap-request-accept-article (group &optional server last)
640 (when (nnimap-possibly-change-group nil server)
641 (nnmail-check-syntax)
642 (let ((message (buffer-string))
643 (message-id (message-field-value "message-id"))
645 (with-current-buffer (nnimap-buffer)
646 (setq sequence (nnimap-send-command
647 "APPEND %S {%d}" (utf7-encode group t)
649 (process-send-string (get-buffer-process (current-buffer)) message)
650 (process-send-string (get-buffer-process (current-buffer))
651 (if (nnimap-newlinep nnimap-object)
654 (let ((result (nnimap-get-response sequence)))
657 (nnimap-find-article-by-message-id group message-id))))))))
659 (defun nnimap-add-cr ()
660 (goto-char (point-min))
661 (while (re-search-forward "\r?\n" nil t)
662 (replace-match "\r\n" t t)))
664 (defun nnimap-get-groups ()
665 (let ((result (nnimap-command "LIST \"\" \"*\""))
668 (dolist (line (cdr result))
669 (when (and (equal (car line) "LIST")
670 (not (and (caadr line)
671 (string-match "noselect" (caadr line)))))
672 (push (car (last line)) groups)))
675 (deffoo nnimap-request-list (&optional server)
676 (nnimap-possibly-change-group nil server)
677 (with-current-buffer nntp-server-buffer
680 (with-current-buffer (nnimap-buffer)
681 (nnimap-get-groups)))
684 (with-current-buffer (nnimap-buffer)
685 (setf (nnimap-group nnimap-object) nil)
686 (dolist (group groups)
687 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
690 (nnimap-wait-for-response (caar sequences))
692 (nnimap-get-responses (mapcar #'car sequences))))
693 (dolist (response responses)
694 (let* ((sequence (car response))
695 (response (cadr response))
696 (group (cadr (assoc sequence sequences))))
698 (equal (caar response) "OK"))
699 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
701 (dolist (elem response)
702 (when (equal (cadr elem) "EXISTS")
703 (setq exists (string-to-number (car elem)))))
705 (setq highest (1- (string-to-number (car uidnext)))))
708 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
711 (insert (format "%S %d %d y\n"
712 (utf7-decode group t) highest (1+ highest))))
714 ;; Return the widest possible range.
715 (insert (format "%S %d 1 y\n" (utf7-decode group t)
716 (or highest exists)))))))))
719 (deffoo nnimap-retrieve-group-data-early (server infos)
720 (when (nnimap-possibly-change-group nil server)
721 (with-current-buffer (nnimap-buffer)
722 ;; QRESYNC handling isn't implemented.
723 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
724 marks groups sequences)
725 ;; Go through the infos and gather the data needed to know
726 ;; what and how to request the data.
728 (setq marks (gnus-info-marks info))
729 (push (list (gnus-group-real-name (gnus-info-group info))
730 (cdr (assq 'active marks))
731 (cdr (assq 'uid marks)))
733 ;; Then request the data.
735 (setf (nnimap-group nnimap-object) nil)
736 (dolist (elem groups)
741 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
750 ;; Fetch the last 100 flags.
751 (max 1 (- (cdr (nth 1 elem)) 100))
753 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
754 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
760 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
762 (nnimap-possibly-change-group nil server))
763 (with-current-buffer (nnimap-buffer)
764 ;; Wait for the final data to trickle in.
765 (nnimap-wait-for-response (cadar sequences))
766 ;; Now we should have all the data we need, no matter whether
767 ;; we're QRESYNCING, fetching all the flags from scratch, or
768 ;; just fetching the last 100 flags per group.
769 (nnimap-update-infos (nnimap-flags-to-marks
771 (nreverse sequences)))
773 ;; Finally, just return something resembling an active file in
774 ;; the nntp buffer, so that the agent can save the info, too.
775 (with-current-buffer nntp-server-buffer
778 (let* ((group (gnus-info-group info))
779 (active (gnus-active group)))
781 (insert (format "%S %d %d y\n"
782 (gnus-group-real-name group)
784 (car active))))))))))
786 (defun nnimap-update-infos (flags infos)
788 (let ((group (gnus-group-real-name (gnus-info-group info))))
789 (nnimap-update-info info (cdr (assoc group flags))))))
791 (defun nnimap-update-info (info marks)
793 (destructuring-bind (existing flags high low uidnext start-article
794 permanent-flags) marks
795 (let ((group (gnus-info-group info))
796 (completep (and start-article
797 (= start-article 1))))
799 (setq high (1- uidnext)))
800 ;; First set the active ranges based on high/low.
802 (not (gnus-active group)))
803 (gnus-set-active group
806 ;; No articles in this group.
807 (cons uidnext (1- uidnext))))
808 (setcdr (gnus-active group) (or high (1- uidnext))))
810 (setq high (1- uidnext)))
811 ;; Then update the list of read articles.
813 (gnus-compress-sequence
817 (cdr (assoc '%Seen flags)))
818 (cdr (assoc '%Flagged flags)))))
819 (read (gnus-range-difference
820 (cons start-article high) unread)))
821 (when (> start-article 1)
824 (if (> start-article 1)
825 (gnus-sorted-range-intersection
826 (cons 1 (1- start-article))
827 (gnus-info-read info))
828 (gnus-info-read info))
830 (gnus-info-set-read info read)
832 (setq marks (gnus-info-marks info))
833 ;; Note the active level for the next run-through.
834 (let ((active (assq 'active marks)))
836 (setcdr active (gnus-active group))
837 (push (cons 'active (gnus-active group)) marks)))
838 (dolist (type (cdr nnimap-mark-alist))
839 (let ((old-marks (assoc (car type) marks))
841 (gnus-compress-sequence
842 (cdr (or (assoc (caddr type) flags) ; %Flagged
843 (assoc (intern (cadr type) obarray) flags)
844 (assoc (cadr type) flags)))))) ; "\Flagged"
845 (setq marks (delq old-marks marks))
849 (setq old-marks (gnus-range-difference
851 (cons start-article high)))
852 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
854 (push (cons (car type) new-marks) marks)))
855 (gnus-info-set-marks info marks t)
856 (nnimap-store-info info (gnus-active group))))))))
858 (defun nnimap-store-info (info active)
859 (let* ((group (gnus-group-real-name (gnus-info-group info)))
860 (entry (assoc group nnimap-current-infos)))
862 (setcdr entry (list info active))
863 (push (list group info active) nnimap-current-infos))))
865 (defun nnimap-flags-to-marks (groups)
866 (let (data group totalp uidnext articles start-article mark permanent-flags)
867 (dolist (elem groups)
868 (setq group (car elem)
870 start-article (nth 2 elem)
871 permanent-flags (nth 3 elem)
872 articles (nthcdr 4 elem))
873 (let ((high (caar articles))
875 (dolist (article articles)
876 (setq low (car article))
877 (push (car article) existing)
878 (dolist (flag (cdr article))
879 (setq mark (assoc flag marks))
881 (push (list flag (car article)) marks)
882 (setcdr mark (cons (car article) (cdr mark))))))
883 (push (list group existing marks high low uidnext start-article
888 (defun nnimap-parse-flags (sequences)
889 (goto-char (point-min))
890 ;; Change \Delete etc to %Delete, so that the reader can read it.
891 (subst-char-in-region (point-min) (point-max)
893 (let (start end articles groups uidnext elems permanent-flags)
894 (dolist (elem sequences)
895 (destructuring-bind (group-sequence flag-sequence totalp group) elem
897 ;; The EXAMINE was successful.
898 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
903 (setq permanent-flags
904 (and (search-forward "PERMANENTFLAGS "
905 (or end (point-min)) t)
906 (read (current-buffer))))
909 (and (search-forward "UIDNEXT "
910 (or end (point-min)) t)
911 (read (current-buffer))))
914 ;; The UID FETCH FLAGS was successful.
915 (search-forward (format "\n%d OK " flag-sequence) nil t))
918 (while (search-forward " FETCH " start t)
919 (setq elems (read (current-buffer)))
920 (push (cons (cadr (memq 'UID elems))
921 (cadr (memq 'FLAGS elems)))
923 (push (nconc (list group uidnext totalp permanent-flags) articles)
925 (setq articles nil))))
928 (defun nnimap-find-process-buffer (buffer)
929 (cadr (assoc buffer nnimap-connection-alist)))
931 (deffoo nnimap-request-post (&optional server)
932 (setq nnimap-status-string "Read-only server")
935 (defun nnimap-possibly-change-group (group server)
936 (let ((open-result t))
938 (not (nnimap-server-opened server)))
939 (setq open-result (nnimap-open-server server)))
946 (with-current-buffer (nnimap-buffer)
947 (if (equal group (nnimap-group nnimap-object))
949 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
951 (setf (nnimap-group nnimap-object) group
952 (nnimap-select-result nnimap-object) result)
955 (defun nnimap-find-connection (buffer)
956 "Find the connection delivering to BUFFER."
957 (let ((entry (assoc buffer nnimap-connection-alist)))
959 (if (and (buffer-name (cadr entry))
960 (get-buffer-process (cadr entry))
961 (memq (process-status (get-buffer-process (cadr entry)))
963 (get-buffer-process (cadr entry))
964 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
967 (defvar nnimap-sequence 0)
969 (defun nnimap-send-command (&rest args)
971 (get-buffer-process (current-buffer))
974 (incf nnimap-sequence)
975 (apply #'format args)
976 (if (nnimap-newlinep nnimap-object)
981 (defun nnimap-log-command (command)
982 (with-current-buffer (get-buffer-create "*imap log*")
983 (goto-char (point-max))
984 (insert (format-time-string "%H:%M:%S") " " command))
987 (defun nnimap-command (&rest args)
989 (let* ((sequence (apply #'nnimap-send-command args))
990 (response (nnimap-get-response sequence)))
991 (if (equal (caar response) "OK")
993 (nnheader-report 'nnimap "%s"
994 (mapconcat (lambda (a)
999 (defun nnimap-get-response (sequence)
1000 (nnimap-wait-for-response sequence)
1001 (nnimap-parse-response))
1003 (defun nnimap-wait-for-connection ()
1004 (let ((process (get-buffer-process (current-buffer))))
1005 (goto-char (point-min))
1006 (while (and (memq (process-status process)
1008 (not (re-search-forward "^\\* .*\n" nil t)))
1009 (nnheader-accept-process-output process)
1010 (goto-char (point-min)))
1012 (and (looking-at "\\* \\([A-Z0-9]+\\)")
1015 (defun nnimap-wait-for-response (sequence &optional messagep)
1016 (let ((process (get-buffer-process (current-buffer))))
1017 (goto-char (point-max))
1018 (while (and (memq (process-status process)
1020 (not (re-search-backward (format "^%d .*\n" sequence)
1021 (max (point-min) (- (point) 500))
1024 (message "Read %dKB" (/ (buffer-size) 1000)))
1025 (nnheader-accept-process-output process)
1026 (goto-char (point-max)))))
1028 (defun nnimap-parse-response ()
1029 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
1031 (dolist (line lines)
1032 (push (cdr (nnimap-parse-line line)) result))
1033 ;; Return the OK/error code first, and then all the "continuation
1034 ;; lines" afterwards.
1036 (nreverse result))))
1038 ;; Parse an IMAP response line lightly. They look like
1039 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1040 ;; the lines into a list of strings and lists of string.
1041 (defun nnimap-parse-line (line)
1045 (goto-char (point-min))
1047 (if (eql (setq char (following-char)) ? )
1052 (split-string (buffer-substring
1053 (1+ (point)) (1- (search-forward "]")))))
1055 (split-string (buffer-substring
1056 (1+ (point)) (1- (search-forward ")")))))
1059 (buffer-substring (point) (1- (search-forward "\""))))
1061 (buffer-substring (point) (if (search-forward " " nil t)
1063 (goto-char (point-max))))))
1065 (nreverse result))))
1067 (defun nnimap-last-response-string ()
1070 (let ((end (point)))
1074 (while (and (not (bobp))
1075 (eql (following-char) ?*))
1077 (unless (eql (following-char) ?*)
1079 (buffer-substring (point) end))))
1081 (defun nnimap-get-responses (sequences)
1083 (dolist (sequence sequences)
1084 (goto-char (point-min))
1085 (when (re-search-forward (format "^%d " sequence) nil t)
1086 (push (list sequence (nnimap-parse-response))
1090 (defvar nnimap-incoming-split-list nil)
1092 (defun nnimap-fetch-inbox (articles)
1094 (nnimap-wait-for-response
1095 (nnimap-send-command
1097 (nnimap-article-ranges articles)
1098 (format "(UID %s%s)"
1100 (if (member "IMAP4REV1"
1101 (nnimap-capabilities nnimap-object))
1102 "BODY.PEEK[HEADER] BODY.PEEK"
1104 (if nnimap-split-download-body-default
1109 (defun nnimap-split-incoming-mail ()
1110 (with-current-buffer (nnimap-buffer)
1111 (let ((nnimap-incoming-split-list nil)
1112 (nnmail-split-methods nnimap-split-methods)
1113 (nnmail-inhibit-default-split-group t)
1114 (groups (nnimap-get-groups))
1117 (nnimap-command "SELECT %S" nnimap-inbox)
1118 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1120 (nnimap-fetch-inbox new-articles)
1121 (nnimap-transform-split-mail)
1122 (nnheader-ms-strip-cr)
1124 (nnmail-split-incoming (current-buffer)
1125 #'nnimap-save-mail-spec
1127 #'nnimap-dummy-active-number
1128 #'nnimap-save-mail-spec)
1129 (when nnimap-incoming-split-list
1130 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1131 sequences junk-articles)
1132 ;; Create any groups that doesn't already exist on the
1134 (dolist (spec specs)
1135 (when (and (not (member (car spec) groups))
1136 (not (eq (car spec) 'junk)))
1137 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1138 ;; Then copy over all the messages.
1140 (dolist (spec specs)
1141 (let ((group (car spec))
1142 (ranges (cdr spec)))
1143 (if (eq group 'junk)
1144 (setq junk-articles ranges)
1145 (push (list (nnimap-send-command
1147 (nnimap-article-ranges ranges)
1148 (utf7-encode group t))
1151 ;; Wait for the last COPY response...
1153 (nnimap-wait-for-response (caar sequences))
1154 ;; And then mark the successful copy actions as deleted,
1155 ;; and possibly expunge them.
1156 (nnimap-mark-and-expunge-incoming
1157 (nnimap-parse-copied-articles sequences))
1158 (nnimap-mark-and-expunge-incoming junk-articles))))))))
1160 (defun nnimap-mark-and-expunge-incoming (range)
1162 (setq range (nnimap-article-ranges range))
1164 (nnimap-send-command
1165 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)))
1167 ;; If the server supports it, we now delete the message we have
1168 ;; just copied over.
1169 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1170 (setq sequence (nnimap-send-command "UID EXPUNGE %s" range)))
1171 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1172 ;; user has configured it.
1174 (setq sequence (nnimap-send-command "EXPUNGE"))))
1175 (nnimap-wait-for-response sequence))))
1177 (defun nnimap-parse-copied-articles (sequences)
1178 (let (sequence copied range)
1179 (goto-char (point-min))
1180 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1181 (setq sequence (string-to-number (match-string 1)))
1182 (when (setq range (cadr (assq sequence sequences)))
1183 (push (gnus-uncompress-range range) copied)))
1184 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1186 (defun nnimap-new-articles (flags)
1188 (dolist (elem flags)
1189 (when (or (null (cdr elem))
1190 (and (not (memq '%Deleted (cdr elem)))
1191 (not (memq '%Seen (cdr elem)))))
1192 (push (car elem) new)))
1193 (gnus-compress-sequence (nreverse new))))
1195 (defun nnimap-make-split-specs (list)
1199 (destructuring-bind (article spec) elem
1200 (dolist (group (delete nil (mapcar #'car spec)))
1201 (unless (setq entry (assoc group specs))
1202 (push (setq entry (list group)) specs))
1203 (setcdr entry (cons article (cdr entry))))))
1204 (dolist (entry specs)
1205 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1208 (defun nnimap-transform-split-mail ()
1209 (goto-char (point-min))
1210 (let (article bytes)
1213 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1214 (delete-region (point) (progn (forward-line 1) (point)))
1217 (setq article (match-string 1)
1218 bytes (nnimap-get-length))
1219 (delete-region (line-beginning-position) (line-end-position))
1220 ;; Insert MMDF separator, and a way to remember what this
1222 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1223 (forward-char (1+ bytes))
1224 (setq bytes (nnimap-get-length))
1225 (delete-region (line-beginning-position) (line-end-position))
1226 (forward-char (1+ bytes))
1227 (delete-region (line-beginning-position) (line-end-position))))))
1229 (defun nnimap-dummy-active-number (group &optional server)
1232 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1234 (goto-char (point-min))
1235 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1236 (error "Invalid nnimap mail")
1237 (setq article (string-to-number (match-string 1))))
1239 (if (eq group-art 'junk)
1240 (list (cons 'junk 1))
1242 nnimap-incoming-split-list)))
1246 ;;; nnimap.el ends here