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.
43 (defvoo nnimap-address nil
44 "The address of the IMAP server.")
46 (defvoo nnimap-server-port nil
48 If nnimap-stream is `ssl', this will default to `imaps'. If not,
49 it will default to `imap'.")
51 (defvoo nnimap-stream 'ssl
52 "How nnimap will talk to the IMAP server.
53 Values are `ssl' and `network'.")
55 (defvoo nnimap-shell-program (if (boundp 'imap-shell-program)
56 (if (listp imap-shell-program)
57 (car imap-shell-program)
61 (defvoo nnimap-inbox nil
62 "The mail box where incoming mail arrives and should be split out of.")
64 (defvoo nnimap-expunge-inbox nil
65 "If non-nil, expunge the inbox after fetching mail.
66 This is always done if the server supports UID EXPUNGE, but it's
67 not done by default on servers that doesn't support that command.")
69 (defvoo nnimap-authenticator nil
70 "How nnimap authenticate itself to the server.
71 Possible choices are nil (use default methods) or `anonymous'.")
73 (defvoo nnimap-fetch-partial-articles nil
74 "If non-nil, nnimap will fetch partial articles.
75 If t, nnimap will fetch only the first part. If a string, it
76 will fetch all parts that have types that match that string. A
77 likely value would be \"text/\" to automatically fetch all
80 (defvoo nnimap-connection-alist nil)
82 (defvoo nnimap-current-infos nil)
84 (defvar nnimap-process nil)
86 (defvar nnimap-status-string "")
88 (defvar nnimap-split-download-body-default nil
89 "Internal variable with default value for `nnimap-split-download-body'.")
92 group process commands capabilities select-result newlinep)
94 (defvar nnimap-object nil)
96 (defvar nnimap-mark-alist
100 (expire "gnus-expire")
101 (dormant "gnus-dormant")
104 (download "gnus-download")
105 (forward "gnus-forward")))
107 (defvar nnimap-split-methods nil)
109 (defun nnimap-buffer ()
110 (nnimap-find-process-buffer nntp-server-buffer))
112 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
113 (with-current-buffer nntp-server-buffer
115 (when (nnimap-possibly-change-group group server)
116 (with-current-buffer (nnimap-buffer)
117 (nnimap-send-command "SELECT %S" (utf7-encode group t))
119 (nnimap-wait-for-response
122 (nnimap-article-ranges (gnus-compress-sequence articles))
123 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
125 (if (member "IMAP4REV1"
126 (nnimap-capabilities nnimap-object))
127 "BODY.PEEK[HEADER.FIELDS %s]"
128 "RFC822.HEADER.LINES %s")
129 (append '(Subject From Date Message-Id
130 References In-Reply-To Xref)
131 nnmail-extra-headers))))
133 (nnimap-transform-headers))
134 (insert-buffer-substring
135 (nnimap-find-process-buffer (current-buffer))))
138 (defun nnimap-transform-headers ()
139 (goto-char (point-min))
140 (let (article bytes lines size)
143 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
144 (delete-region (point) (progn (forward-line 1) (point)))
147 (setq article (match-string 1)
148 bytes (nnimap-get-length)
152 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
157 (when (search-forward "BODYSTRUCTURE" (line-end-position) t)
158 (let ((structure (ignore-errors (read (current-buffer)))))
159 (while (and (consp structure)
160 (not (stringp (car structure))))
161 (setq structure (car structure)))
162 (setq lines (nth 7 structure))))
163 (delete-region (line-beginning-position) (line-end-position))
164 (insert (format "211 %s Article retrieved." article))
167 (insert (format "Chars: %s\n" size)))
169 (insert (format "Lines: %s\n" lines)))
170 (re-search-forward "^\r$")
171 (delete-region (line-beginning-position) (line-end-position))
175 (defun nnimap-get-length ()
176 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t)
177 (string-to-number (match-string 1))))
179 (defun nnimap-article-ranges (ranges)
183 (number-to-string ranges))
184 ((numberp (cdr ranges))
185 (format "%d:%d" (car ranges) (cdr ranges)))
187 (dolist (elem ranges)
190 (format "%d:%d" (car elem) (cdr elem))
191 (number-to-string elem))
193 (mapconcat #'identity (nreverse result) ",")))))
195 (deffoo nnimap-open-server (server &optional defs)
196 (if (nnimap-server-opened server)
198 (unless (assq 'nnimap-address defs)
199 (setq defs (append defs (list (list 'nnimap-address server)))))
200 (nnoo-change-server 'nnimap server defs)
201 (or (nnimap-find-connection nntp-server-buffer)
202 (nnimap-open-connection nntp-server-buffer))))
204 (defun nnimap-make-process-buffer (buffer)
206 (generate-new-buffer (format "*nnimap %s %s %s*"
207 nnimap-address nnimap-server-port
208 (gnus-buffer-exists-p buffer)))
209 (mm-disable-multibyte)
210 (buffer-disable-undo)
212 (set (make-local-variable 'after-change-functions) nil)
213 (set (make-local-variable 'nnimap-object) (make-nnimap))
214 (push (list buffer (current-buffer)) nnimap-connection-alist)
217 (defun nnimap-open-shell-stream (name buffer host port)
218 (let ((process-connection-type nil))
219 (start-process name buffer shell-file-name
227 (defun nnimap-credentials (address ports)
228 (let (port credentials)
229 ;; Request the credentials from all ports, but only query on the
230 ;; last port if all the previous ones have failed.
231 (while (and (null credentials)
232 (setq port (pop ports)))
234 (auth-source-user-or-password
235 '("login" "password") address port nil (null ports))))
238 (defun nnimap-open-connection (buffer)
239 (with-current-buffer (nnimap-make-process-buffer buffer)
240 (let* ((coding-system-for-read 'binary)
241 (coding-system-for-write 'binary)
244 ((eq nnimap-stream 'network)
246 "*nnimap*" (current-buffer) nnimap-address
247 (or nnimap-server-port
248 (if (netrc-find-service-number "imap")
252 ((eq nnimap-stream 'shell)
253 (nnimap-open-shell-stream
254 "*nnimap*" (current-buffer) nnimap-address
255 (or nnimap-server-port "imap"))
257 ((eq nnimap-stream 'ssl)
259 "*nnimap*" (current-buffer) nnimap-address
260 (or nnimap-server-port
261 (if (netrc-find-service-number "imaps")
264 '("143" "993" "imap" "imaps"))))
265 connection-result login-result credentials)
266 (setf (nnimap-process nnimap-object)
267 (get-buffer-process (current-buffer)))
268 (when (and (nnimap-process nnimap-object)
269 (memq (process-status (nnimap-process nnimap-object))
271 (gnus-set-process-query-on-exit-flag (nnimap-process nnimap-object) nil)
272 (when (setq connection-result (nnimap-wait-for-connection))
273 (unless (equal connection-result "PREAUTH")
274 (if (not (setq credentials
275 (if (eq nnimap-authenticator 'anonymous)
277 (message-make-address))
280 (if nnimap-server-port
281 (cons (format "%s" nnimap-server-port) ports)
283 (setq nnimap-object nil)
284 (setq login-result (nnimap-command "LOGIN %S %S"
287 (unless (car login-result)
288 (delete-process (nnimap-process nnimap-object))
289 (setq nnimap-object nil))))
291 (setf (nnimap-capabilities nnimap-object)
294 (or (nnimap-find-parameter "CAPABILITY" (cdr login-result))
295 (nnimap-find-parameter
296 "CAPABILITY" (cdr (nnimap-command "CAPABILITY"))))))
297 (when (member "QRESYNC" (nnimap-capabilities nnimap-object))
298 (nnimap-command "ENABLE QRESYNC"))
301 (defun nnimap-find-parameter (parameter elems)
305 ((equal (car elem) parameter)
306 (setq result (cdr elem)))
307 ((and (equal (car elem) "OK")
309 (equal (caadr elem) parameter))
310 (setq result (cdr (cadr elem))))))
313 (deffoo nnimap-close-server (&optional server)
316 (deffoo nnimap-request-close ()
319 (deffoo nnimap-server-opened (&optional server)
320 (and (nnoo-current-server-p 'nnimap server)
322 (gnus-buffer-live-p nntp-server-buffer)
323 (nnimap-find-connection nntp-server-buffer)))
325 (deffoo nnimap-status-message (&optional server)
326 nnimap-status-string)
328 (deffoo nnimap-request-article (article &optional group server to-buffer)
329 (with-current-buffer nntp-server-buffer
330 (let ((result (nnimap-possibly-change-group group server))
332 (when (stringp article)
333 (setq article (nnimap-find-article-by-message-id group article)))
337 (with-current-buffer (nnimap-buffer)
339 (when nnimap-fetch-partial-articles
340 (if (eq nnimap-fetch-partial-articles t)
342 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article)
343 (goto-char (point-min))
344 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t)
345 (let ((structure (ignore-errors (read (current-buffer)))))
346 (setq parts (nnimap-find-wanted-parts structure))))))
349 (if (member "IMAP4REV1" (nnimap-capabilities nnimap-object))
350 "UID FETCH %d BODY.PEEK[]"
351 "UID FETCH %d RFC822.PEEK")
353 ;; Check that we really got an article.
354 (goto-char (point-min))
355 (unless (looking-at "\\* [0-9]+ FETCH")
357 (let ((buffer (nnimap-find-process-buffer (current-buffer))))
359 (with-current-buffer (or to-buffer nntp-server-buffer)
360 (insert-buffer-substring buffer)
361 (goto-char (point-min))
362 (let ((bytes (nnimap-get-length)))
363 (delete-region (line-beginning-position)
364 (progn (forward-line 1) (point)))
365 (goto-char (+ (point) bytes))
366 (delete-region (point) (point-max))
367 (nnheader-ms-strip-cr))
368 (cons group article))))))))
370 (defun nnimap-find-wanted-parts (structure)
371 (message-flatten-list (nnimap-find-wanted-parts-1 structure "")))
373 (defun nnimap-find-wanted-parts-1 (structure prefix)
376 (while (consp (car structure))
377 (let ((sub (pop structure)))
378 (if (consp (car sub))
379 (push (nnimap-find-wanted-parts-1
380 sub (if (string= prefix "")
381 (number-to-string num)
382 (format "%s.%s" prefix num)))
384 (let ((type (format "%s/%s" (nth 0 sub) (nth 1 sub))))
385 (when (string-match nnimap-fetch-partial-articles type)
386 (push (if (string= prefix "")
387 (number-to-string num)
388 (format "%s.%s" prefix num))
393 (deffoo nnimap-request-group (group &optional server dont-check info)
394 (let ((result (nnimap-possibly-change-group group server))
395 articles active marks high low)
396 (with-current-buffer nntp-server-buffer
399 (setq active (nth 2 (assoc group nnimap-current-infos))))
400 (insert (format "211 %d %d %d %S\n"
401 (- (cdr active) (car active))
405 (with-current-buffer (nnimap-buffer)
407 (let ((group-sequence
408 (nnimap-send-command "SELECT %S" (utf7-encode group t)))
410 (nnimap-send-command "UID FETCH 1:* FLAGS")))
411 (nnimap-wait-for-response flag-sequence)
413 (nnimap-flags-to-marks
415 (list (list group-sequence flag-sequence 1 group)))))
417 (nnimap-update-infos marks (list info)))
418 (goto-char (point-max))
421 (setq high (nth 3 (car marks))
422 low (nth 4 (car marks))))
423 ((re-search-backward "UIDNEXT \\([0-9]+\\)" nil t)
424 (setq high (string-to-number (match-string 1))
434 (deffoo nnimap-request-create-group (group &optional server args)
435 (when (nnimap-possibly-change-group nil server)
436 (with-current-buffer (nnimap-buffer)
437 (car (nnimap-command "CREATE %S" (utf7-encode group t))))))
439 (deffoo nnimap-request-delete-group (group &optional force server)
440 (when (nnimap-possibly-change-group nil server)
441 (with-current-buffer (nnimap-buffer)
442 (car (nnimap-command "DELETE %S" (utf7-encode group t))))))
444 (deffoo nnimap-request-expunge-group (group &optional server)
445 (when (nnimap-possibly-change-group group server)
446 (with-current-buffer (nnimap-buffer)
447 (car (nnimap-command "EXPUNGE")))))
449 (defun nnimap-get-flags (spec)
452 (with-current-buffer (nnimap-buffer)
454 (nnimap-wait-for-response (nnimap-send-command
455 "UID FETCH %s FLAGS" spec))
456 (goto-char (point-min))
457 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" nil t)
458 (setq elems (nnimap-parse-line (match-string 1)))
459 (push (cons (string-to-number (cadr (member "UID" elems)))
460 (cadr (member "FLAGS" elems)))
462 (nreverse articles)))
464 (deffoo nnimap-close-group (group &optional server)
467 (deffoo nnimap-request-move-article (article group server accept-form
468 &optional last internal-move-group)
469 (when (nnimap-possibly-change-group group server)
470 ;; If the move is internal (on the same server), just do it the easy
472 (let ((message-id (message-field-value "message-id")))
473 (if internal-move-group
475 (with-current-buffer (nnimap-buffer)
476 (nnimap-command "UID COPY %d %S"
478 (utf7-encode internal-move-group t)))))
480 (nnimap-delete-article article)
481 (cons internal-move-group
482 (nnimap-find-article-by-message-id
483 internal-move-group message-id))))
485 (when (nnimap-request-article article group server (current-buffer))
486 (let ((result (eval accept-form)))
488 (nnimap-delete-article article)
491 (deffoo nnimap-request-expire-articles (articles group &optional server force)
493 ((not (nnimap-possibly-change-group group server))
496 (unless (nnimap-delete-article articles)
497 (message "Article marked for deletion, but not expunged."))
502 (defun nnimap-find-article-by-message-id (group message-id)
503 (when (nnimap-possibly-change-group group nil)
504 (with-current-buffer (nnimap-buffer)
506 (nnimap-command "UID SEARCH HEADER Message-Id %S" message-id))
509 ;; Select the last instance of the message in the group.
511 (car (last (assoc "SEARCH" (cdr result)))))
512 (string-to-number article)))))))
514 (defun nnimap-delete-article (articles)
515 (with-current-buffer (nnimap-buffer)
516 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
517 (nnimap-article-ranges articles))
518 (when (member "UIDPLUS" (nnimap-capabilities nnimap-object))
519 (nnimap-send-command "UID EXPUNGE %s"
520 (nnimap-article-ranges articles))
523 (deffoo nnimap-request-scan (&optional group server)
524 (when (and (nnimap-possibly-change-group nil server)
525 (equal group nnimap-inbox)
527 nnimap-split-methods)
528 (nnimap-split-incoming-mail)))
530 (defun nnimap-marks-to-flags (marks)
533 (when (setq flag (cadr (assq mark nnimap-mark-alist)))
537 (deffoo nnimap-request-set-mark (group actions &optional server)
538 (when (nnimap-possibly-change-group group server)
540 (with-current-buffer (nnimap-buffer)
541 ;; Just send all the STORE commands without waiting for
542 ;; response. If they're successful, they're successful.
543 (dolist (action actions)
544 (destructuring-bind (range action marks) action
545 (let ((flags (nnimap-marks-to-flags marks)))
547 (setq sequence (nnimap-send-command
548 "UID STORE %s %sFLAGS.SILENT (%s)"
549 (nnimap-article-ranges range)
553 (mapconcat #'identity flags " ")))))))
554 ;; Wait for the last command to complete to avoid later
555 ;; syncronisation problems with the stream.
557 (nnimap-wait-for-response sequence))))))
559 (deffoo nnimap-request-accept-article (group &optional server last)
560 (when (nnimap-possibly-change-group nil server)
561 (nnmail-check-syntax)
562 (let ((message (buffer-string))
563 (message-id (message-field-value "message-id"))
565 (with-current-buffer (nnimap-buffer)
566 (setq sequence (nnimap-send-command
567 "APPEND %S {%d}" (utf7-encode group t)
569 (process-send-string (get-buffer-process (current-buffer)) message)
570 (process-send-string (get-buffer-process (current-buffer))
571 (if (nnimap-newlinep nnimap-object)
574 (let ((result (nnimap-get-response sequence)))
577 (nnimap-find-article-by-message-id group message-id))))))))
579 (defun nnimap-add-cr ()
580 (goto-char (point-min))
581 (while (re-search-forward "\r?\n" nil t)
582 (replace-match "\r\n" t t)))
584 (defun nnimap-get-groups ()
585 (let ((result (nnimap-command "LIST \"\" \"*\""))
588 (dolist (line (cdr result))
589 (when (and (equal (car line) "LIST")
590 (not (and (caadr line)
591 (string-match "noselect" (caadr line)))))
592 (push (car (last line)) groups)))
595 (deffoo nnimap-request-list (&optional server)
596 (nnimap-possibly-change-group nil server)
597 (with-current-buffer nntp-server-buffer
600 (with-current-buffer (nnimap-buffer)
601 (nnimap-get-groups)))
604 (with-current-buffer (nnimap-buffer)
605 (dolist (group groups)
606 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t))
609 (nnimap-wait-for-response (caar sequences))
611 (nnimap-get-responses (mapcar #'car sequences))))
612 (dolist (response responses)
613 (let* ((sequence (car response))
614 (response (cadr response))
615 (group (cadr (assoc sequence sequences))))
617 (equal (caar response) "OK"))
618 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response))
620 (dolist (elem response)
621 (when (equal (cadr elem) "EXISTS")
622 (setq exists (string-to-number (car elem)))))
624 (setq highest (1- (string-to-number (car uidnext)))))
627 (insert (format "%S 0 1 y\n" (utf7-decode group t))))
630 (insert (format "%S %d %d y\n"
631 (utf7-decode group t) highest (1+ highest))))
633 ;; Return the widest possible range.
634 (insert (format "%S %d 1 y\n" (utf7-decode group t)
635 (or highest exists)))))))))
638 (deffoo nnimap-retrieve-group-data-early (server infos)
639 (when (nnimap-possibly-change-group nil server)
640 (with-current-buffer (nnimap-buffer)
641 ;; QRESYNC handling isn't implemented.
642 (let ((qresyncp (member "notQRESYNC" (nnimap-capabilities nnimap-object)))
643 marks groups sequences)
644 ;; Go through the infos and gather the data needed to know
645 ;; what and how to request the data.
647 (setq marks (gnus-info-marks info))
648 (push (list (gnus-group-real-name (gnus-info-group info))
649 (cdr (assq 'active marks))
650 (cdr (assq 'uid marks)))
652 ;; Then request the data.
654 (dolist (elem groups)
659 (nnimap-send-command "EXAMINE %S (QRESYNC (%s %s))"
668 ;; Fetch the last 100 flags.
669 (max 1 (- (cdr (nth 1 elem)) 100))
671 (push (list (nnimap-send-command "EXAMINE %S" (car elem))
672 (nnimap-send-command "UID FETCH %d:* FLAGS" start)
678 (deffoo nnimap-finish-retrieve-group-infos (server infos sequences)
680 (nnimap-possibly-change-group nil server))
681 (with-current-buffer (nnimap-buffer)
682 ;; Wait for the final data to trickle in.
683 (nnimap-wait-for-response (cadar sequences))
684 ;; Now we should have all the data we need, no matter whether
685 ;; we're QRESYNCING, fetching all the flags from scratch, or
686 ;; just fetching the last 100 flags per group.
687 (nnimap-update-infos (nnimap-flags-to-marks
689 (nreverse sequences)))
691 ;; Finally, just return something resembling an active file in
692 ;; the nntp buffer, so that the agent can save the info, too.
693 (with-current-buffer nntp-server-buffer
696 (let* ((group (gnus-info-group info))
697 (active (gnus-active group)))
699 (insert (format "%S %d %d y\n"
700 (gnus-group-real-name group)
702 (car active))))))))))
704 (defun nnimap-update-infos (flags infos)
706 (let ((group (gnus-group-real-name (gnus-info-group info))))
707 (nnimap-update-info info (cdr (assoc group flags))))))
709 (defun nnimap-update-info (info marks)
711 (destructuring-bind (existing flags high low uidnext start-article) marks
712 (let ((group (gnus-info-group info))
713 (completep (and start-article
714 (= start-article 1))))
715 ;; First set the active ranges based on high/low.
717 (not (gnus-active group)))
718 (gnus-set-active group
721 ;; No articles in this group.
722 (cons (1- uidnext) uidnext)))
723 (setcdr (gnus-active group) high))
724 ;; Then update the list of read articles.
726 (gnus-compress-sequence
730 (cdr (assoc "\\Seen" flags)))
731 (cdr (assoc "\\Flagged" flags)))))
732 (read (gnus-range-difference
733 (cons start-article high) unread)))
734 (when (> start-article 1)
737 (if (> start-article 1)
738 (gnus-sorted-range-intersection
739 (cons 1 (1- start-article))
740 (gnus-info-read info))
741 (gnus-info-read info))
743 (gnus-info-set-read info read)
745 (setq marks (gnus-info-marks info))
746 ;; Note the active level for the next run-through.
747 (let ((active (assq 'active marks)))
749 (setcdr active (gnus-active group))
750 (push (cons 'active (gnus-active group)) marks)))
751 (dolist (type (cdr nnimap-mark-alist))
752 (let ((old-marks (assoc (car type) marks))
753 (new-marks (gnus-compress-sequence
754 (cdr (assoc (cadr type) flags)))))
755 (setq marks (delq old-marks marks))
759 (setq old-marks (gnus-range-difference
761 (cons start-article high)))
762 (setq new-marks (gnus-range-nconcat old-marks new-marks)))
764 (push (cons (car type) new-marks) marks)))
765 (gnus-info-set-marks info marks t)
766 (nnimap-store-info info (gnus-active group))))))))
768 (defun nnimap-store-info (info active)
769 (let* ((group (gnus-group-real-name (gnus-info-group info)))
770 (entry (assoc group nnimap-current-infos)))
772 (setcdr entry (list info active))
773 (push (list group info active) nnimap-current-infos))))
775 (defun nnimap-flags-to-marks (groups)
776 (let (data group totalp uidnext articles start-article mark)
777 (dolist (elem groups)
778 (setq group (car elem)
780 start-article (caddr elem)
781 articles (cdddr elem))
782 (let ((high (caar articles))
784 (dolist (article articles)
785 (setq low (car article))
786 (push (car article) existing)
787 (dolist (flag (cdr article))
788 (setq mark (assoc flag marks))
790 (push (list flag (car article)) marks)
791 (setcdr mark (cons (car article) (cdr mark)))))
792 (push (list group existing marks high low uidnext start-article)
796 (defun nnimap-parse-flags (sequences)
797 (goto-char (point-min))
798 (let (start end articles groups uidnext elems)
799 (dolist (elem sequences)
800 (destructuring-bind (group-sequence flag-sequence totalp group) elem
801 ;; The EXAMINE was successful.
802 (when (and (search-forward (format "\n%d OK " group-sequence) nil t)
806 (if (re-search-backward "UIDNEXT \\([0-9]+\\)"
807 (or end (point-min)) t)
808 (setq uidnext (string-to-number (match-string 1)))
811 ;; The UID FETCH FLAGS was successful.
812 (search-forward (format "\n%d OK " flag-sequence) nil t))
815 (while (re-search-forward "^\\* [0-9]+ FETCH (\\(.*\\))" end t)
816 (setq elems (nnimap-parse-line (match-string 1)))
817 (push (cons (string-to-number (cadr (member "UID" elems)))
818 (cadr (member "FLAGS" elems)))
820 (push (nconc (list group uidnext totalp) articles) groups)
821 (setq articles nil))))
824 (defun nnimap-find-process-buffer (buffer)
825 (cadr (assoc buffer nnimap-connection-alist)))
827 (deffoo nnimap-request-post (&optional server)
828 (setq nnimap-status-string "Read-only server")
831 (defun nnimap-possibly-change-group (group server)
832 (let ((open-result t))
834 (not (nnimap-server-opened server)))
835 (setq open-result (nnimap-open-server server)))
842 (with-current-buffer (nnimap-buffer)
843 (if (equal group (nnimap-group nnimap-object))
845 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t))))
847 (setf (nnimap-group nnimap-object) group
848 (nnimap-select-result nnimap-object) result)
851 (defun nnimap-find-connection (buffer)
852 "Find the connection delivering to BUFFER."
853 (let ((entry (assoc buffer nnimap-connection-alist)))
855 (if (and (buffer-name (cadr entry))
856 (get-buffer-process (cadr entry))
857 (memq (process-status (get-buffer-process (cadr entry)))
859 (get-buffer-process (cadr entry))
860 (setq nnimap-connection-alist (delq entry nnimap-connection-alist))
863 (defvar nnimap-sequence 0)
865 (defun nnimap-send-command (&rest args)
867 (get-buffer-process (current-buffer))
870 (incf nnimap-sequence)
871 (apply #'format args)
872 (if (nnimap-newlinep nnimap-object)
877 (defun nnimap-log-command (command)
878 (with-current-buffer (get-buffer-create "*imap log*")
879 (goto-char (point-max))
880 (insert (format-time-string "%H:%M:%S") " " command))
883 (defun nnimap-command (&rest args)
885 (let* ((sequence (apply #'nnimap-send-command args))
886 (response (nnimap-get-response sequence)))
887 (if (equal (caar response) "OK")
889 (nnheader-report 'nnimap "%s"
890 (mapconcat (lambda (a)
895 (defun nnimap-get-response (sequence)
896 (nnimap-wait-for-response sequence)
897 (nnimap-parse-response))
899 (defun nnimap-wait-for-connection ()
900 (let ((process (get-buffer-process (current-buffer))))
901 (goto-char (point-min))
902 (while (and (memq (process-status process)
904 (not (re-search-forward "^\\* .*\n" nil t)))
905 (nnheader-accept-process-output process)
906 (goto-char (point-min)))
908 (and (looking-at "\\* \\([A-Z0-9]+\\)")
911 (defun nnimap-wait-for-response (sequence &optional messagep)
912 (let ((process (get-buffer-process (current-buffer))))
913 (goto-char (point-max))
914 (while (and (memq (process-status process)
916 (not (re-search-backward (format "^%d .*\n" sequence)
917 (max (point-min) (- (point) 500))
920 (message "Read %dKB" (/ (buffer-size) 1000)))
921 (nnheader-accept-process-output process)
922 (goto-char (point-max)))))
924 (defun nnimap-parse-response ()
925 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t))
928 (push (cdr (nnimap-parse-line line)) result))
929 ;; Return the OK/error code first, and then all the "continuation
930 ;; lines" afterwards.
934 ;; Parse an IMAP response line lightly. They look like
935 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
936 ;; the lines into a list of strings and lists of string.
937 (defun nnimap-parse-line (line)
941 (goto-char (point-min))
943 (if (eql (setq char (following-char)) ? )
948 (split-string (buffer-substring
949 (1+ (point)) (1- (search-forward "]")))))
951 (split-string (buffer-substring
952 (1+ (point)) (1- (search-forward ")")))))
955 (buffer-substring (point) (1- (search-forward "\""))))
957 (buffer-substring (point) (if (search-forward " " nil t)
959 (goto-char (point-max))))))
963 (defun nnimap-last-response-string ()
970 (while (and (not (bobp))
971 (eql (following-char) ?*))
973 (unless (eql (following-char) ?*)
975 (buffer-substring (point) end))))
977 (defun nnimap-get-responses (sequences)
979 (dolist (sequence sequences)
980 (goto-char (point-min))
981 (when (re-search-forward (format "^%d " sequence) nil t)
982 (push (list sequence (nnimap-parse-response))
986 (defvar nnimap-incoming-split-list nil)
988 (defun nnimap-fetch-inbox (articles)
990 (nnimap-wait-for-response
993 (nnimap-article-ranges articles)
996 (if (member "IMAP4REV1"
997 (nnimap-capabilities nnimap-object))
998 "BODY.PEEK[HEADER] BODY.PEEK"
1000 (if nnimap-split-download-body-default
1005 (defun nnimap-split-incoming-mail ()
1006 (with-current-buffer (nnimap-buffer)
1007 (let ((nnimap-incoming-split-list nil)
1008 (nnmail-split-methods nnimap-split-methods)
1009 (nnmail-inhibit-default-split-group t)
1010 (groups (nnimap-get-groups))
1013 (nnimap-command "SELECT %S" nnimap-inbox)
1014 (setq new-articles (nnimap-new-articles (nnimap-get-flags "1:*")))
1016 (nnimap-fetch-inbox new-articles)
1017 (nnimap-transform-split-mail)
1018 (nnheader-ms-strip-cr)
1020 (nnmail-split-incoming (current-buffer)
1021 #'nnimap-save-mail-spec
1023 #'nnimap-dummy-active-number)
1024 (when nnimap-incoming-split-list
1025 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list))
1027 ;; Create any groups that doesn't already exist on the
1029 (dolist (spec specs)
1030 (unless (member (car spec) groups)
1031 (nnimap-command "CREATE %S" (utf7-encode (car spec) t))))
1032 ;; Then copy over all the messages.
1034 (dolist (spec specs)
1035 (let ((group (car spec))
1036 (ranges (cdr spec)))
1037 (push (list (nnimap-send-command "UID COPY %s %S"
1038 (nnimap-article-ranges ranges)
1039 (utf7-encode group t))
1042 ;; Wait for the last COPY response...
1044 (nnimap-wait-for-response (caar sequences))
1045 ;; And then mark the successful copy actions as deleted,
1046 ;; and possibly expunge them.
1047 (nnimap-mark-and-expunge-incoming
1048 (nnimap-parse-copied-articles sequences)))))))))
1050 (defun nnimap-mark-and-expunge-incoming (range)
1052 (setq range (nnimap-article-ranges range))
1053 (nnimap-send-command
1054 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range)
1056 ;; If the server supports it, we now delete the message we have
1057 ;; just copied over.
1058 ((member "UIDPLUS" (nnimap-capabilities nnimap-object))
1059 (nnimap-send-command "UID EXPUNGE %s" range))
1060 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1061 ;; user has configured it.
1062 (nnimap-expunge-inbox
1063 (nnimap-send-command "EXPUNGE")))))
1065 (defun nnimap-parse-copied-articles (sequences)
1066 (let (sequence copied range)
1067 (goto-char (point-min))
1068 (while (re-search-forward "^\\([0-9]+\\) OK " nil t)
1069 (setq sequence (string-to-number (match-string 1)))
1070 (when (setq range (cadr (assq sequence sequences)))
1071 (push (gnus-uncompress-range range) copied)))
1072 (gnus-compress-sequence (sort (apply #'nconc copied) #'<))))
1074 (defun nnimap-new-articles (flags)
1076 (dolist (elem flags)
1077 (when (or (null (cdr elem))
1078 (and (not (member "\\Deleted" (cdr elem)))
1079 (not (member "\\Seen" (cdr elem)))))
1080 (push (car elem) new)))
1081 (gnus-compress-sequence (nreverse new))))
1083 (defun nnimap-make-split-specs (list)
1087 (destructuring-bind (article spec) elem
1088 (dolist (group (delete nil (mapcar #'car spec)))
1089 (unless (setq entry (assoc group specs))
1090 (push (setq entry (list group)) specs))
1091 (setcdr entry (cons article (cdr entry))))))
1092 (dolist (entry specs)
1093 (setcdr entry (gnus-compress-sequence (sort (cdr entry) #'<))))
1096 (defun nnimap-transform-split-mail ()
1097 (goto-char (point-min))
1098 (let (article bytes)
1101 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1102 (delete-region (point) (progn (forward-line 1) (point)))
1105 (setq article (match-string 1)
1106 bytes (nnimap-get-length))
1107 (delete-region (line-beginning-position) (line-end-position))
1108 ;; Insert MMDF separator, and a way to remember what this
1110 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article))
1111 (forward-char (1+ bytes))
1112 (setq bytes (nnimap-get-length))
1113 (delete-region (line-beginning-position) (line-end-position))
1114 (forward-char (1+ bytes))
1115 (delete-region (line-beginning-position) (line-end-position))))))
1117 (defun nnimap-dummy-active-number (group &optional server)
1120 (defun nnimap-save-mail-spec (group-art &optional server full-nov)
1122 (goto-char (point-min))
1123 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t))
1124 (error "Invalid nnimap mail")
1125 (setq article (string-to-number (match-string 1))))
1126 (push (list article group-art)
1127 nnimap-incoming-split-list)))
1131 ;;; nnimap.el ends here