1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2011 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.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
35 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
36 ;; `make-network-stream'.
37 (unless (fboundp 'open-protocol-stream
)
38 (require 'proto-stream
)))
53 (autoload 'auth-source-forget
+ "auth-source")
54 (autoload 'auth-source-search
"auth-source")
58 (defvoo nnimap-address nil
59 "The address of the IMAP server.")
61 (defvoo nnimap-user nil
62 "Username to use for authentication to the IMAP server.")
64 (defvoo nnimap-server-port nil
66 If nnimap-stream is `ssl', this will default to `imaps'. If not,
67 it will default to `imap'.")
69 (defvoo nnimap-stream
'undecided
70 "How nnimap talks to the IMAP server.
71 The value should be either `undecided', `ssl' or `tls',
72 `network', `starttls', `plain', or `shell'.
74 If the value is `undecided', nnimap tries `ssl' first, then falls
77 (defvoo nnimap-shell-program
(if (boundp 'imap-shell-program
)
78 (if (listp imap-shell-program
)
79 (car imap-shell-program
)
83 (defvoo nnimap-inbox nil
84 "The mail box where incoming mail arrives and should be split out of.
85 For example, \"INBOX\".")
87 (defvoo nnimap-split-methods nil
89 Uses the same syntax as `nnmail-split-methods'.")
91 (defvoo nnimap-split-fancy nil
92 "Uses the same syntax as `nnmail-split-fancy'.")
94 (defvoo nnimap-unsplittable-articles
'(%Deleted %Seen
)
95 "Articles with the flags in the list will not be considered when splitting.")
97 (make-obsolete-variable 'nnimap-split-rule
"see `nnimap-split-methods'"
100 (defvoo nnimap-authenticator nil
101 "How nnimap authenticate itself to the server.
102 Possible choices are nil (use default methods) or `anonymous'.")
104 (defvoo nnimap-expunge t
105 "If non-nil, expunge articles after deleting them.
106 This is always done if the server supports UID EXPUNGE, but it's
107 not done by default on servers that doesn't support that command.")
109 (defvoo nnimap-streaming t
110 "If non-nil, try to use streaming commands with IMAP servers.
111 Switching this off will make nnimap slower, but it helps with
114 (defvoo nnimap-connection-alist nil
)
116 (defvoo nnimap-current-infos nil
)
118 (defvoo nnimap-fetch-partial-articles nil
119 "If non-nil, Gnus will fetch partial articles.
120 If t, nnimap will fetch only the first part. If a string, it
121 will fetch all parts that have types that match that string. A
122 likely value would be \"text/\" to automatically fetch all
125 (defvar nnimap-process nil
)
127 (defvar nnimap-status-string
"")
129 (defvar nnimap-split-download-body-default nil
130 "Internal variable with default value for `nnimap-split-download-body'.")
132 (defvar nnimap-keepalive-timer nil
)
133 (defvar nnimap-process-buffers nil
)
136 group process commands capabilities select-result newlinep server
137 last-command-time greeting examined stream-type
)
139 (defvar nnimap-object nil
)
141 (defvar nnimap-mark-alist
142 '((read "\\Seen" %Seen
)
143 (tick "\\Flagged" %Flagged
)
144 (reply "\\Answered" %Answered
)
145 (expire "gnus-expire")
146 (dormant "gnus-dormant")
149 (download "gnus-download")
150 (forward "gnus-forward")))
152 (defvar nnimap-quirks
153 '(("QRESYNC" "Zimbra" "QRESYNC ")))
155 (defvar nnimap-inhibit-logging nil
)
157 (defun nnimap-buffer ()
158 (nnimap-find-process-buffer nntp-server-buffer
))
160 (defun nnimap-header-parameters ()
161 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
164 "BODY.PEEK[HEADER.FIELDS %s]"
165 "RFC822.HEADER.LINES %s")
166 (append '(Subject From Date Message-Id
167 References In-Reply-To Xref
)
168 nnmail-extra-headers
))))
170 (deffoo nnimap-retrieve-headers
(articles &optional group server fetch-old
)
171 (with-current-buffer nntp-server-buffer
173 (when (nnimap-possibly-change-group group server
)
174 (with-current-buffer (nnimap-buffer)
176 (nnimap-wait-for-response
179 (nnimap-article-ranges (gnus-compress-sequence articles
))
180 (nnimap-header-parameters))
182 (nnimap-transform-headers)
183 (nnheader-remove-cr-followed-by-lf))
184 (insert-buffer-substring
185 (nnimap-find-process-buffer (current-buffer))))
188 (defun nnimap-transform-headers ()
189 (goto-char (point-min))
190 (let (article bytes lines size string
)
193 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
194 (delete-region (point) (progn (forward-line 1) (point)))
197 (setq article
(match-string 1))
198 ;; Unfold quoted {number} strings.
199 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
200 (1+ (line-end-position)) t
)
201 (setq size
(string-to-number (match-string 1)))
202 (delete-region (+ (match-beginning 0) 2) (point))
203 (setq string
(buffer-substring (point) (+ (point) size
)))
204 (delete-region (point) (+ (point) size
))
205 (insert (format "%S" string
)))
206 (setq bytes
(nnimap-get-length)
210 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
215 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
216 (let ((structure (ignore-errors
217 (read (current-buffer)))))
218 (while (and (consp structure
)
219 (not (stringp (car structure
))))
220 (setq structure
(car structure
)))
221 (setq lines
(nth 7 structure
))))
222 (delete-region (line-beginning-position) (line-end-position))
223 (insert (format "211 %s Article retrieved." article
))
226 (insert (format "Chars: %s\n" size
)))
228 (insert (format "Lines: %s\n" lines
)))
229 (unless (re-search-forward "^\r$" nil t
)
230 (goto-char (point-max)))
231 (delete-region (line-beginning-position) (line-end-position))
235 (defun nnimap-unfold-quoted-lines ()
236 ;; Unfold quoted {number} strings.
238 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
239 (setq size
(string-to-number (match-string 1)))
240 (delete-region (1+ (match-beginning 0)) (point))
241 (setq string
(buffer-substring (point) (+ (point) size
)))
242 (delete-region (point) (+ (point) size
))
243 (insert (format "%S" string
)))))
245 (defun nnimap-get-length ()
246 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
247 (string-to-number (match-string 1))))
249 (defun nnimap-article-ranges (ranges)
253 (number-to-string ranges
))
254 ((numberp (cdr ranges
))
255 (format "%d:%d" (car ranges
) (cdr ranges
)))
257 (dolist (elem ranges
)
260 (format "%d:%d" (car elem
) (cdr elem
))
261 (number-to-string elem
))
263 (mapconcat #'identity
(nreverse result
) ",")))))
265 (deffoo nnimap-open-server
(server &optional defs
)
266 (if (nnimap-server-opened server
)
268 (unless (assq 'nnimap-address defs
)
269 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
270 (nnoo-change-server 'nnimap server defs
)
271 (or (nnimap-find-connection nntp-server-buffer
)
272 (nnimap-open-connection nntp-server-buffer
))))
274 (defun nnimap-make-process-buffer (buffer)
276 (generate-new-buffer (format "*nnimap %s %s %s*"
277 nnimap-address nnimap-server-port
278 (gnus-buffer-exists-p buffer
)))
279 (mm-disable-multibyte)
280 (buffer-disable-undo)
282 (set (make-local-variable 'after-change-functions
) nil
)
283 (set (make-local-variable 'nnimap-object
)
284 (make-nnimap :server
(nnoo-current-server 'nnimap
)))
285 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
286 (push (current-buffer) nnimap-process-buffers
)
289 (defun nnimap-credentials (address ports user
)
290 (let* ((auth-source-creation-prompts
291 '((user .
"IMAP user at %h: ")
292 (secret .
"IMAP password for %u@%h: ")))
293 (found (nth 0 (auth-source-search :max
1
297 :require
'(:user
:secret
)
300 (list (plist-get found
:user
)
301 (let ((secret (plist-get found
:secret
)))
302 (if (functionp secret
)
305 (plist-get found
:save-function
))
308 (defun nnimap-keepalive ()
309 (let ((now (current-time)))
310 (dolist (buffer nnimap-process-buffers
)
311 (when (buffer-name buffer
)
312 (with-current-buffer buffer
313 (when (and nnimap-object
314 (nnimap-last-command-time nnimap-object
)
318 (nnimap-last-command-time nnimap-object
)))
319 ;; More than five minutes since the last command.
321 (nnimap-send-command "NOOP")))))))
323 (defun nnimap-open-connection (buffer)
324 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
325 ;; `ssl' when nnimap-server-port was nil. Sort of.
326 (when (and nnimap-server-port
327 (eq nnimap-stream
'undecided
))
328 (setq nnimap-stream
'ssl
))
330 (if (eq nnimap-stream
'undecided
)
331 (loop for type in
'(ssl network
)
332 for stream
= (let ((nnimap-stream type
))
333 (nnimap-open-connection-1 buffer
))
334 while
(eq stream
'no-connect
)
335 finally
(return stream
))
336 (nnimap-open-connection-1 buffer
))))
337 (if (eq stream
'no-connect
)
341 (defun nnimap-open-connection-1 (buffer)
342 (unless nnimap-keepalive-timer
343 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
345 (with-current-buffer (nnimap-make-process-buffer buffer
)
346 (let* ((coding-system-for-read 'binary
)
347 (coding-system-for-write 'binary
)
351 ((memq nnimap-stream
'(network plain starttls
))
352 (nnheader-message 7 "Opening connection to %s..."
355 ((eq nnimap-stream
'shell
)
356 (nnheader-message 7 "Opening connection to %s via shell..."
359 ((memq nnimap-stream
'(ssl tls
))
360 (nnheader-message 7 "Opening connection to %s via tls..."
362 '("imaps" "imap" "993" "143"))
364 (error "Unknown stream type: %s" nnimap-stream
))))
365 login-result credentials
)
366 (when nnimap-server-port
367 (push nnimap-server-port ports
))
369 (open-protocol-stream
370 "*nnimap*" (current-buffer) nnimap-address
(car ports
)
373 :shell-command nnimap-shell-program
374 :capability-command
"1 CAPABILITY\r\n"
375 :end-of-command
"\r\n"
378 (lambda (capabilities)
379 (when (gnus-string-match-p "STARTTLS" capabilities
)
381 (stream (car stream-list
))
382 (props (cdr stream-list
))
383 (greeting (plist-get props
:greeting
))
384 (capabilities (plist-get props
:capabilities
))
385 (stream-type (plist-get props
:type
)))
386 (when (and stream
(not (memq (process-status stream
) '(open run
))))
388 (setf (nnimap-process nnimap-object
) stream
)
389 (setf (nnimap-stream-type nnimap-object
) stream-type
)
392 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
393 nnimap-address port nnimap-stream
)
395 (gnus-set-process-query-on-exit-flag stream nil
)
396 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
397 (nnheader-report 'nnimap
"%s" greeting
)
398 ;; Store the greeting (for debugging purposes).
399 (setf (nnimap-greeting nnimap-object
) greeting
)
400 (setf (nnimap-capabilities nnimap-object
)
402 (split-string capabilities
)))
403 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
404 (if (not (setq credentials
405 (if (eq nnimap-authenticator
'anonymous
)
407 (message-make-address))
408 ;; Look for the credentials based on
409 ;; the virtual server name and the address
411 (gnus-delete-duplicates
414 (nnoo-current-server 'nnimap
)))
417 (setq nnimap-object nil
)
418 (let ((nnimap-inhibit-logging t
))
420 (nnimap-login (car credentials
) (cadr credentials
))))
421 (if (car login-result
)
423 ;; Save the credentials if a save function exists
424 ;; (such a function will only be passed if a new
425 ;; token was created).
426 (when (functionp (nth 2 credentials
))
427 (funcall (nth 2 credentials
)))
428 ;; See if CAPABILITY is set as part of login
430 (dolist (response (cddr login-result
))
431 (when (string= "CAPABILITY" (upcase (car response
)))
432 (setf (nnimap-capabilities nnimap-object
)
433 (mapcar #'upcase
(cdr response
))))))
434 ;; If the login failed, then forget the credentials
435 ;; that are now possibly cached.
436 (dolist (host (list (nnoo-current-server 'nnimap
)
439 (auth-source-forget+ :host host
:port port
)))
440 (delete-process (nnimap-process nnimap-object
))
441 (setq nnimap-object nil
))))
443 (when (nnimap-capability "QRESYNC")
444 (nnimap-command "ENABLE QRESYNC"))
445 (nnimap-process nnimap-object
))))))))
447 (autoload 'rfc2104-hash
"rfc2104")
449 (defun nnimap-login (user password
)
451 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
452 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
453 ;; and we're using an encrypted connection.
454 ((and (not (nnimap-capability "LOGINDISABLED"))
455 (eq (nnimap-stream-type nnimap-object
) 'tls
))
456 (nnimap-command "LOGIN %S %S" user password
))
457 ((nnimap-capability "AUTH=CRAM-MD5")
459 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
460 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
462 (get-buffer-process (current-buffer))
464 (base64-encode-string
466 (rfc2104-hash 'md5
64 16 password
467 (base64-decode-string challenge
))))
469 (nnimap-wait-for-response sequence
)))
470 ((not (nnimap-capability "LOGINDISABLED"))
471 (nnimap-command "LOGIN %S %S" user password
))
472 ((nnimap-capability "AUTH=PLAIN")
474 "AUTHENTICATE PLAIN %s"
475 (base64-encode-string
476 (format "\000%s\000%s"
477 (nnimap-quote-specials user
)
478 (nnimap-quote-specials password
)))))))
480 (defun nnimap-quote-specials (string)
483 (goto-char (point-min))
484 (while (re-search-forward "[\\\"]" nil t
)
490 (defun nnimap-find-parameter (parameter elems
)
494 ((equal (car elem
) parameter
)
495 (setq result
(cdr elem
)))
496 ((and (equal (car elem
) "OK")
498 (equal (caadr elem
) parameter
))
499 (setq result
(cdr (cadr elem
))))))
502 (deffoo nnimap-close-server
(&optional server
)
503 (when (nnoo-change-server 'nnimap server nil
)
505 (delete-process (get-buffer-process (nnimap-buffer))))
506 (nnoo-close-server 'nnimap server
)
509 (deffoo nnimap-request-close
()
512 (deffoo nnimap-server-opened
(&optional server
)
513 (and (nnoo-current-server-p 'nnimap server
)
515 (gnus-buffer-live-p nntp-server-buffer
)
516 (nnimap-find-connection nntp-server-buffer
)))
518 (deffoo nnimap-status-message
(&optional server
)
519 nnimap-status-string
)
521 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
522 (with-current-buffer nntp-server-buffer
523 (let ((result (nnimap-possibly-change-group group server
))
525 (when (stringp article
)
526 (setq article
(nnimap-find-article-by-message-id group article
)))
530 (with-current-buffer (nnimap-buffer)
532 (when nnimap-fetch-partial-articles
533 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
534 (goto-char (point-min))
535 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
536 (setq structure
(ignore-errors
537 (let ((start (point)))
539 (downcase-region start
(point))
541 (read (current-buffer))))
542 parts
(nnimap-find-wanted-parts structure
))))
544 (nnimap-get-partial-article article parts structure
)
545 (nnimap-get-whole-article article
))
546 (let ((buffer (current-buffer)))
547 (with-current-buffer (or to-buffer nntp-server-buffer
)
548 (nnheader-insert-buffer-substring buffer
)
549 (nnheader-ms-strip-cr)))
550 (cons group article
)))))))
552 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
553 (when (nnimap-possibly-change-group group server
)
554 (with-current-buffer (nnimap-buffer)
555 (when (stringp article
)
556 (setq article
(nnimap-find-article-by-message-id group article
)))
559 (nnimap-get-whole-article
560 article
(format "UID FETCH %%d %s"
561 (nnimap-header-parameters)))
562 (let ((buffer (current-buffer)))
563 (with-current-buffer (or to-buffer nntp-server-buffer
)
565 (insert-buffer-substring buffer
)
566 (nnheader-ms-strip-cr)
567 (cons group article
)))))))
569 (defun nnimap-get-whole-article (article &optional command
)
574 "UID FETCH %d BODY.PEEK[]"
575 "UID FETCH %d RFC822.PEEK"))
577 ;; Check that we really got an article.
578 (goto-char (point-min))
579 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
582 ;; Remove any data that may have arrived before the FETCH data.
585 (delete-region (point-min) (point)))
586 (let ((bytes (nnimap-get-length)))
587 (delete-region (line-beginning-position)
588 (progn (forward-line 1) (point)))
589 (goto-char (+ (point) bytes
))
590 (delete-region (point) (point-max)))
593 (defun nnimap-capability (capability)
594 (member capability
(nnimap-capabilities nnimap-object
)))
596 (defun nnimap-ver4-p ()
597 (nnimap-capability "IMAP4REV1"))
599 (defun nnimap-get-partial-article (article parts structure
)
602 "UID FETCH %d (%s %s)"
608 (mapconcat (lambda (part)
609 (format "BODY.PEEK[%s]" part
))
611 (mapconcat (lambda (part)
612 (format "RFC822.PEEK[%s]" part
))
615 (nnimap-convert-partial-article structure
))))
617 (defun nnimap-convert-partial-article (structure)
618 ;; First just skip past the headers.
619 (goto-char (point-min))
620 (let ((bytes (nnimap-get-length))
622 ;; Delete "FETCH" line.
623 (delete-region (line-beginning-position)
624 (progn (forward-line 1) (point)))
625 (goto-char (+ (point) bytes
))
626 ;; Collect all the body parts.
627 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
628 (setq id
(match-string 1)
629 bytes
(or (nnimap-get-length) 0))
631 (delete-region (point) (progn (forward-line 1) (point)))
632 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
634 (delete-region (point) (+ (point) bytes
)))
635 ;; Delete trailing junk.
636 (delete-region (point) (point-max))
637 ;; Now insert all the parts again where they fit in the structure.
638 (nnimap-insert-partial-structure structure parts
)
641 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
643 (let ((bstruc structure
))
644 (while (consp (car bstruc
))
646 (setq type
(car bstruc
))
647 (setq bstruc
(car (cdr bstruc
)))
648 (let ((has-boundary (member "boundary" bstruc
)))
650 (setq boundary
(cadr has-boundary
)))))
652 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
653 (downcase type
) boundary
)))
654 (while (not (stringp (car structure
)))
655 (insert "\n--" boundary
"\n")
656 (if (consp (caar structure
))
657 (nnimap-insert-partial-structure (pop structure
) parts t
)
658 (let ((bit (pop structure
)))
659 (insert (format "Content-type: %s/%s"
660 (downcase (nth 0 bit
))
661 (downcase (nth 1 bit
))))
662 (if (member "CHARSET" (nth 2 bit
))
664 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit
)))))
666 (insert (format "Content-transfer-encoding: %s\n"
669 (when (assoc (nth 9 bit
) parts
)
670 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
671 (insert "\n--" boundary
"--\n")))
673 (defun nnimap-find-wanted-parts (structure)
674 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
676 (defun nnimap-find-wanted-parts-1 (structure prefix
)
679 (while (consp (car structure
))
680 (let ((sub (pop structure
)))
681 (if (consp (car sub
))
682 (push (nnimap-find-wanted-parts-1
683 sub
(if (string= prefix
"")
684 (number-to-string num
)
685 (format "%s.%s" prefix num
)))
687 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
688 (id (if (string= prefix
"")
689 (number-to-string num
)
690 (format "%s.%s" prefix num
))))
691 (setcar (nthcdr 9 sub
) id
)
692 (when (if (eq nnimap-fetch-partial-articles t
)
694 (string-match nnimap-fetch-partial-articles type
))
699 (deffoo nnimap-request-group
(group &optional server dont-check info
)
700 (let ((result (nnimap-possibly-change-group
701 ;; Don't SELECT the group if we're going to select it
703 (if (and (not dont-check
)
704 (assoc group nnimap-current-infos
))
708 articles active marks high low
)
709 (with-current-buffer nntp-server-buffer
712 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
713 (insert (format "211 %d %d %d %S\n"
714 (- (cdr active
) (car active
))
718 (with-current-buffer (nnimap-buffer)
720 (let ((group-sequence
721 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
723 (nnimap-send-command "UID FETCH 1:* FLAGS")))
724 (setf (nnimap-group nnimap-object
) group
)
725 (nnimap-wait-for-response flag-sequence
)
727 (nnimap-flags-to-marks
729 (list (list group-sequence flag-sequence
730 1 group
"SELECT")))))
733 (nnimap-update-infos marks
(list info
))
734 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
735 (goto-char (point-max))
736 (let ((uidnext (nth 5 (car marks
))))
737 (setq high
(or (if uidnext
741 low
(or (nth 4 (car marks
)) uidnext
1)))))
745 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
748 (deffoo nnimap-request-create-group
(group &optional server args
)
749 (when (nnimap-possibly-change-group nil server
)
750 (with-current-buffer (nnimap-buffer)
751 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
753 (deffoo nnimap-request-delete-group
(group &optional force server
)
754 (when (nnimap-possibly-change-group nil server
)
755 (with-current-buffer (nnimap-buffer)
756 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
758 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
759 (when (nnimap-possibly-change-group nil server
)
760 (with-current-buffer (nnimap-buffer)
761 (nnimap-unselect-group)
762 (car (nnimap-command "RENAME %S %S"
763 (utf7-encode group t
) (utf7-encode new-name t
))))))
765 (defun nnimap-unselect-group ()
766 ;; Make sure we don't have this group open read/write by asking
767 ;; to examine a mailbox that doesn't exist. This seems to be
768 ;; the only way that allows us to reliably go back to unselected
770 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
772 (deffoo nnimap-request-expunge-group
(group &optional server
)
773 (when (nnimap-possibly-change-group group server
)
774 (with-current-buffer (nnimap-buffer)
775 (car (nnimap-command "EXPUNGE")))))
777 (defun nnimap-get-flags (spec)
780 (with-current-buffer (nnimap-buffer)
782 (nnimap-wait-for-response (nnimap-send-command
783 "UID FETCH %s FLAGS" spec
))
785 (subst-char-in-region (point-min) (point-max)
787 (goto-char (point-min))
788 (while (search-forward " FETCH " end t
)
789 (setq elems
(read (current-buffer)))
790 (push (cons (cadr (memq 'UID elems
))
791 (cadr (memq 'FLAGS elems
)))
793 (nreverse articles
)))
795 (deffoo nnimap-close-group
(group &optional server
)
798 (deffoo nnimap-request-move-article
(article group server accept-form
799 &optional last internal-move-group
)
801 (mm-disable-multibyte)
802 (when (funcall (if internal-move-group
804 'nnimap-request-article
)
805 article group server
(current-buffer))
806 ;; If the move is internal (on the same server), just do it the easy
808 (let ((message-id (message-field-value "message-id")))
809 (if internal-move-group
811 (with-current-buffer (nnimap-buffer)
812 (nnimap-command "UID COPY %d %S"
814 (utf7-encode internal-move-group t
)))))
816 (nnimap-delete-article article
)
817 (cons internal-move-group
818 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
819 (nnimap-find-article-by-message-id
820 internal-move-group message-id
)))))
821 ;; Move the article to a different method.
822 (let ((result (eval accept-form
)))
824 (nnimap-delete-article article
)
827 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
831 ((not (nnimap-possibly-change-group group server
))
834 (eq nnmail-expiry-target
'delete
))
835 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
836 (nnheader-message 7 "Article marked for deletion, but not expunged."))
839 (let ((deletable-articles
841 (eq nnmail-expiry-wait
'immediate
))
843 (gnus-sorted-intersection
845 (nnimap-find-expired-articles group
)))))
846 (if (null deletable-articles
)
848 (if (eq nnmail-expiry-target
'delete
)
849 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
850 (setq deletable-articles
851 (nnimap-process-expiry-targets
852 deletable-articles group server
)))
853 ;; Return the articles we didn't delete.
854 (gnus-sorted-complement articles deletable-articles
))))))
856 (defun nnimap-process-expiry-targets (articles group server
)
857 (let ((deleted-articles nil
))
859 ;; shortcut further processing if we're going to delete the articles
860 ((eq nnmail-expiry-target
'delete
)
861 (setq deleted-articles articles
)
863 ;; or just move them to another folder on the same IMAP server
864 ((and (not (functionp nnmail-expiry-target
))
865 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
866 (gnus-server-to-method
867 (format "nnimap:%s" server
))))
868 (and (nnimap-possibly-change-group group server
)
869 (with-current-buffer (nnimap-buffer)
870 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
873 (nnimap-article-ranges (gnus-compress-sequence articles
))
874 (utf7-encode (gnus-group-real-name nnmail-expiry-target
) t
))
875 (setq deleted-articles articles
)))
878 (dolist (article articles
)
879 (let ((target nnmail-expiry-target
))
881 (mm-disable-multibyte)
882 (when (nnimap-request-article article group server
(current-buffer))
883 (when (functionp target
)
884 (setq target
(funcall target group
)))
886 (not (eq target
'delete
)))
887 (if (or (gnus-request-group target t
)
888 (gnus-request-create-group target
))
890 (nnmail-expiry-target-group target group
)
891 (nnheader-message 7 "Expiring article %s:%d to %s"
892 group article target
))
894 (nnheader-message 7 "Expiring article %s:%d" group article
))
896 (push article deleted-articles
))))))))
897 ;; Change back to the current group again.
898 (nnimap-possibly-change-group group server
)
899 (setq deleted-articles
(nreverse deleted-articles
))
900 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
903 (defun nnimap-find-expired-articles (group)
904 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
905 (with-current-buffer (nnimap-buffer)
908 "UID SEARCH SENTBEFORE %s"
912 (car (rassoc (nth 4 (decode-time cutoff
))
913 parse-time-months
))))
916 (delete 0 (mapcar #'string-to-number
917 (cdr (assoc "SEARCH" (cdr result
))))))))))
920 (defun nnimap-find-article-by-message-id (group message-id
)
921 (with-current-buffer (nnimap-buffer)
923 (unless (equal group
(nnimap-group nnimap-object
))
924 (setf (nnimap-group nnimap-object
) nil
)
925 (setf (nnimap-examined nnimap-object
) group
)
926 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
928 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
930 (setq result
(nnimap-wait-for-response sequence
))
932 (car (setq result
(nnimap-parse-response))))
933 ;; Select the last instance of the message in the group.
935 (car (last (cdr (assoc "SEARCH" (cdr result
))))))
936 (string-to-number article
))))))
938 (defun nnimap-delete-article (articles)
939 (with-current-buffer (nnimap-buffer)
940 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
941 (nnimap-article-ranges articles
))
943 ((nnimap-capability "UIDPLUS")
944 (nnimap-command "UID EXPUNGE %s"
945 (nnimap-article-ranges articles
))
948 (nnimap-command "EXPUNGE")
950 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
951 "server doesn't support UIDPLUS, so we won't "
952 "delete this article now"))))))
954 (deffoo nnimap-request-scan
(&optional group server
)
955 (when (and (nnimap-possibly-change-group nil server
)
957 nnimap-split-methods
)
958 (nnheader-message 7 "nnimap %s splitting mail..." server
)
959 (nnimap-split-incoming-mail)
960 (nnheader-message 7 "nnimap %s splitting mail...done" server
)))
962 (defun nnimap-marks-to-flags (marks)
965 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
969 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
970 (when (nnimap-possibly-change-group nil server
)
971 (let ((command (assoc
973 '((subscribe "SUBSCRIBE")
974 (unsubscribe "UNSUBSCRIBE")))))
976 (with-current-buffer (nnimap-buffer)
977 (nnimap-command "%s %S" (cadr command
) (utf7-encode group t
)))))))
979 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
980 (when (nnimap-possibly-change-group group server
)
982 (with-current-buffer (nnimap-buffer)
984 ;; Just send all the STORE commands without waiting for
985 ;; response. If they're successful, they're successful.
986 (dolist (action actions
)
987 (destructuring-bind (range action marks
) action
988 (let ((flags (nnimap-marks-to-flags marks
)))
990 (setq sequence
(nnimap-send-command
991 "UID STORE %s %sFLAGS.SILENT (%s)"
992 (nnimap-article-ranges range
)
994 ((eq action
'del
) "-")
995 ((eq action
'add
) "+")
996 ((eq action
'set
) ""))
997 (mapconcat #'identity flags
" ")))))))
998 ;; Wait for the last command to complete to avoid later
999 ;; syncronisation problems with the stream.
1001 (nnimap-wait-for-response sequence
))))))
1003 (deffoo nnimap-request-accept-article
(group &optional server last
)
1004 (when (nnimap-possibly-change-group nil server
)
1005 (nnmail-check-syntax)
1006 (let ((message-id (message-field-value "message-id"))
1009 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
1010 (with-current-buffer (nnimap-buffer)
1011 (when (setq message
(or (nnimap-process-quirk "OK Gimap " 'append message
)
1013 ;; If we have this group open read-only, then unselect it
1014 ;; before appending to it.
1015 (when (equal (nnimap-examined nnimap-object
) group
)
1016 (nnimap-unselect-group))
1018 (setq sequence
(nnimap-send-command
1019 "APPEND %S {%d}" (utf7-encode group t
)
1021 (unless nnimap-streaming
1022 (nnimap-wait-for-connection "^[+]"))
1023 (process-send-string (get-buffer-process (current-buffer)) message
)
1024 (process-send-string (get-buffer-process (current-buffer))
1025 (if (nnimap-newlinep nnimap-object
)
1028 (let ((result (nnimap-get-response sequence
)))
1029 (if (not (nnimap-ok-p result
))
1031 (nnheader-report 'nnimap
"%s" result
)
1034 (or (nnimap-find-uid-response "APPENDUID" (car result
))
1035 (nnimap-find-article-by-message-id
1036 group message-id
))))))))))
1038 (defun nnimap-process-quirk (greeting-match type data
)
1039 (when (and (nnimap-greeting nnimap-object
)
1040 (string-match greeting-match
(nnimap-greeting nnimap-object
))
1042 (string-match "\000" data
))
1043 (let ((choice (gnus-multiple-choice
1044 "Message contains NUL characters. Delete, continue, abort? "
1045 '((?d
"Delete NUL characters")
1046 (?c
"Try to APPEND the message as is")
1050 (nnheader-report 'nnimap
"Aborted APPEND due to NUL characters"))
1056 (goto-char (point-min))
1057 (while (search-forward "\000" nil t
)
1058 (replace-match "" t t
))
1059 (buffer-string)))))))
1061 (defun nnimap-ok-p (value)
1064 (equal (caar value
) "OK")))
1066 (defun nnimap-find-uid-response (name list
)
1067 (let ((result (car (last (nnimap-find-response-element name list
)))))
1069 (string-to-number result
))))
1071 (defun nnimap-find-response-element (name list
)
1074 (when (and (consp elem
)
1075 (equal name
(car elem
)))
1076 (setq result elem
)))
1079 (deffoo nnimap-request-replace-article
(article group buffer
)
1081 (when (and (nnimap-possibly-change-group group nil
)
1082 ;; Put the article into the group.
1083 (with-current-buffer buffer
1085 (nnimap-request-accept-article group nil t
))))
1086 (nnimap-delete-article (list article
))
1087 ;; Return the new article number.
1090 (defun nnimap-add-cr ()
1091 (goto-char (point-min))
1092 (while (re-search-forward "\r?\n" nil t
)
1093 (replace-match "\r\n" t t
)))
1095 (defun nnimap-get-groups ()
1097 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1099 (nnimap-wait-for-response sequence
)
1100 (subst-char-in-region (point-min) (point-max)
1102 (goto-char (point-min))
1103 (nnimap-unfold-quoted-lines)
1104 (goto-char (point-min))
1105 (while (search-forward "* LIST " nil t
)
1106 (let ((flags (read (current-buffer)))
1107 (separator (read (current-buffer)))
1108 (group (read (current-buffer))))
1109 (unless (member '%NoSelect flags
)
1110 (push (if (stringp group
)
1112 (format "%s" group
))
1116 (deffoo nnimap-request-list
(&optional server
)
1117 (when (nnimap-possibly-change-group nil server
)
1118 (with-current-buffer nntp-server-buffer
1121 (with-current-buffer (nnimap-buffer)
1122 (nnimap-get-groups)))
1123 sequences responses
)
1125 (with-current-buffer (nnimap-buffer)
1126 (setf (nnimap-group nnimap-object
) nil
)
1127 (dolist (group groups
)
1128 (setf (nnimap-examined nnimap-object
) group
)
1129 (push (list (nnimap-send-command "EXAMINE %S"
1130 (utf7-encode group t
))
1133 (nnimap-wait-for-response (caar sequences
))
1135 (nnimap-get-responses (mapcar #'car sequences
))))
1136 (dolist (response responses
)
1137 (let* ((sequence (car response
))
1138 (response (cadr response
))
1139 (group (cadr (assoc sequence sequences
))))
1141 (equal (caar response
) "OK"))
1142 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1144 (dolist (elem response
)
1145 (when (equal (cadr elem
) "EXISTS")
1146 (setq exists
(string-to-number (car elem
)))))
1148 (setq highest
(1- (string-to-number (car uidnext
)))))
1151 (insert (format "%S 0 1 y\n" (utf7-decode group t
))))
1154 (insert (format "%S %d %d y\n"
1155 (utf7-decode group t
)
1156 highest
(1+ highest
))))
1158 ;; Return the widest possible range.
1159 (insert (format "%S %d 1 y\n" (utf7-decode group t
)
1160 (or highest exists
)))))))))
1163 (deffoo nnimap-request-newgroups
(date &optional server
)
1164 (when (nnimap-possibly-change-group nil server
)
1165 (with-current-buffer nntp-server-buffer
1167 (dolist (group (with-current-buffer (nnimap-buffer)
1168 (nnimap-get-groups)))
1169 (unless (assoc group nnimap-current-infos
)
1170 ;; Insert dummy numbers here -- they don't matter.
1171 (insert (format "%S 0 1 y\n" group
))))
1174 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1175 (when (nnimap-possibly-change-group nil server
)
1176 (with-current-buffer (nnimap-buffer)
1178 (setf (nnimap-group nnimap-object
) nil
)
1179 (let ((qresyncp (nnimap-capability "QRESYNC"))
1180 params groups sequences active uidvalidity modseq group
)
1181 ;; Go through the infos and gather the data needed to know
1182 ;; what and how to request the data.
1183 (dolist (info infos
)
1184 (setq params
(gnus-info-params info
)
1185 group
(gnus-group-real-name (gnus-info-group info
))
1186 active
(cdr (assq 'active params
))
1187 uidvalidity
(cdr (assq 'uidvalidity params
))
1188 modseq
(cdr (assq 'modseq params
)))
1189 (setf (nnimap-examined nnimap-object
) group
)
1195 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1196 (utf7-encode group t
)
1197 (nnimap-quirk "QRESYNC")
1203 (if (and active uidvalidity
)
1204 ;; Fetch the last 100 flags.
1205 (max 1 (- (cdr active
) 100))
1210 ;; If we don't have a UIDVALIDITY, then this is
1211 ;; the first time we've seen the group, so we
1212 ;; have to do a SELECT (which is slower than an
1213 ;; examine), but will tell us whether the group
1214 ;; is read-only or not.
1216 (push (list (nnimap-send-command "%s %S" command
1217 (utf7-encode group t
))
1218 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1219 start group command
)
1223 (defun nnimap-quirk (command)
1224 (let ((quirk (assoc command nnimap-quirks
)))
1225 ;; If this server is of a type that matches a quirk, then return
1226 ;; the "quirked" command instead of the proper one.
1227 (if (or (null quirk
)
1228 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1232 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1233 (when (and sequences
1234 ;; Check that the process is still alive.
1235 (get-buffer-process (nnimap-buffer))
1236 (memq (process-status (get-buffer-process (nnimap-buffer)))
1238 (nnimap-possibly-change-group nil server
))
1239 (with-current-buffer (nnimap-buffer)
1240 ;; Wait for the final data to trickle in.
1241 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1245 ;; Now we should have most of the data we need, no matter
1246 ;; whether we're QRESYNCING, fetching all the flags from
1247 ;; scratch, or just fetching the last 100 flags per group.
1248 (nnimap-update-infos (nnimap-flags-to-marks
1250 (nreverse sequences
)))
1252 ;; Finally, just return something resembling an active file in
1253 ;; the nntp buffer, so that the agent can save the info, too.
1254 (with-current-buffer nntp-server-buffer
1256 (dolist (info infos
)
1257 (let* ((group (gnus-info-group info
))
1258 (active (gnus-active group
)))
1260 (insert (format "%S %d %d y\n"
1261 (gnus-group-real-name group
)
1263 (car active
)))))))))))
1265 (defun nnimap-update-infos (flags infos
)
1266 (dolist (info infos
)
1267 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1268 (marks (cdr (assoc group flags
))))
1270 (nnimap-update-info info marks
)))))
1272 (defun nnimap-update-info (info marks
)
1273 (destructuring-bind (existing flags high low uidnext start-article
1274 permanent-flags uidvalidity
1275 vanished highestmodseq
) marks
1277 ;; Ignore groups with no UIDNEXT/marks. This happens for
1278 ;; completely empty groups.
1279 ((and (not existing
)
1281 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1283 (gnus-set-active (gnus-info-group info
) active
))))
1284 ;; We have a mismatch between the old and new UIDVALIDITY
1285 ;; identifiers, so we have to re-request the group info (the next
1286 ;; time). This virtually never happens.
1287 ((let ((old-uidvalidity
1288 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1289 (and old-uidvalidity
1290 (not (equal old-uidvalidity uidvalidity
))
1291 (> start-article
1)))
1292 (gnus-group-remove-parameter info
'uidvalidity
)
1293 (gnus-group-remove-parameter info
'modseq
))
1294 ;; We have the data needed to update.
1296 (let* ((group (gnus-info-group info
))
1297 (completep (and start-article
1298 (= start-article
1)))
1299 (active (or (gnus-active group
)
1300 (cdr (assq 'active
(gnus-info-params info
))))))
1302 (setq high
(1- uidnext
)))
1303 ;; First set the active ranges based on high/low.
1305 (not (gnus-active group
)))
1306 (gnus-set-active group
1309 (cons (min (or low
(car active
))
1311 (max (or high
(cdr active
))
1316 ;; No articles in this group.
1317 (cons uidnext
(1- uidnext
)))
1319 (cons start-article
(1- start-article
)))
1321 ;; No articles and no uidnext.
1323 (gnus-set-active group
1325 (or high
(1- uidnext
)))))
1326 ;; See whether this is a read-only group.
1327 (unless (eq permanent-flags
'not-scanned
)
1328 (gnus-group-set-parameter
1329 info
'permanent-flags
1330 (and (or (memq '%
* permanent-flags
)
1331 (memq '%Seen permanent-flags
))
1333 ;; Update marks and read articles if this isn't a
1334 ;; read-only IMAP group.
1335 (when (setq permanent-flags
1336 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1337 (if (and highestmodseq
1338 (not start-article
))
1339 ;; We've gotten the data by QRESYNCing.
1340 (nnimap-update-qresync-info
1341 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1342 ;; Do normal non-QRESYNC flag updates.
1343 ;; Update the list of read articles.
1345 (gnus-compress-sequence
1346 (gnus-set-difference
1347 (gnus-set-difference
1349 (cdr (assoc '%Seen flags
)))
1350 (cdr (assoc '%Flagged flags
)))))
1351 (read (gnus-range-difference
1352 (cons start-article high
) unread
)))
1353 (when (> start-article
1)
1356 (if (> start-article
1)
1357 (gnus-sorted-range-intersection
1358 (cons 1 (1- start-article
))
1359 (gnus-info-read info
))
1360 (gnus-info-read info
))
1362 (when (or (not (listp permanent-flags
))
1363 (memq '%Seen permanent-flags
))
1364 (gnus-info-set-read info read
))
1365 ;; Update the marks.
1366 (setq marks
(gnus-info-marks info
))
1367 (dolist (type (cdr nnimap-mark-alist
))
1368 (when (or (not (listp permanent-flags
))
1369 (memq (car (assoc (caddr type
) flags
))
1371 (memq '%
* permanent-flags
))
1372 (let ((old-marks (assoc (car type
) marks
))
1374 (gnus-compress-sequence
1375 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1376 (assoc (intern (cadr type
) obarray
) flags
)
1377 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1378 (setq marks
(delq old-marks marks
))
1380 (when (and old-marks
1381 (> start-article
1))
1382 (setq old-marks
(gnus-range-difference
1384 (cons start-article high
)))
1385 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1387 (push (cons (car type
) new-marks
) marks
)))))
1388 (gnus-info-set-marks info marks t
))))
1389 ;; Tell Gnus whether there are any \Recent messages in any of
1391 (let ((recent (cdr (assoc '%Recent flags
))))
1394 (> (car (last recent
)) (cdr active
)))
1395 (push (list (cons (gnus-group-real-name group
) 0))
1396 nnmail-split-history
)))
1397 ;; Note the active level for the next run-through.
1398 (gnus-group-set-parameter info
'active
(gnus-active group
))
1399 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1400 (gnus-group-set-parameter info
'modseq highestmodseq
)
1401 (nnimap-store-info info
(gnus-active group
)))))))
1403 (defun nnimap-update-qresync-info (info existing vanished flags
)
1404 ;; Add all the vanished articles to the list of read articles.
1409 (gnus-range-add (gnus-info-read info
)
1411 (cdr (assq '%Flagged flags
)))
1412 (cdr (assq '%Seen flags
))))
1413 (let ((marks (gnus-info-marks info
)))
1414 (dolist (type (cdr nnimap-mark-alist
))
1415 (let ((ticks (assoc (car type
) marks
))
1417 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1418 (assoc (intern (cadr type
) obarray
) flags
)
1419 (assoc (cadr type
) flags
))))) ; "\Flagged"
1420 (setq marks
(delq ticks marks
))
1422 ;; Add the new marks we got.
1423 (setq ticks
(gnus-add-to-range ticks new-marks
))
1424 ;; Remove the marks from messages that don't have them.
1425 (setq ticks
(gnus-remove-from-range
1427 (gnus-compress-sequence
1428 (gnus-sorted-complement existing new-marks
))))
1430 (push (cons (car type
) ticks
) marks
)))
1431 (gnus-info-set-marks info marks t
))))
1433 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1434 (if (zerop (length irange
))
1437 (dolist (elem (split-string irange
","))
1439 (if (string-match ":" elem
)
1440 (let ((numbers (split-string elem
":")))
1441 (cons (string-to-number (car numbers
))
1442 (string-to-number (cadr numbers
))))
1443 (string-to-number elem
))
1445 (nreverse result
))))
1447 (defun nnimap-store-info (info active
)
1448 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1449 (entry (assoc group nnimap-current-infos
)))
1451 (setcdr entry
(list info active
))
1452 (push (list group info active
) nnimap-current-infos
))))
1454 (defun nnimap-flags-to-marks (groups)
1455 (let (data group totalp uidnext articles start-article mark permanent-flags
1456 uidvalidity vanished highestmodseq
)
1457 (dolist (elem groups
)
1458 (setq group
(car elem
)
1459 uidnext
(nth 1 elem
)
1460 start-article
(nth 2 elem
)
1461 permanent-flags
(nth 3 elem
)
1462 uidvalidity
(nth 4 elem
)
1463 vanished
(nth 5 elem
)
1464 highestmodseq
(nth 6 elem
)
1465 articles
(nthcdr 7 elem
))
1466 (let ((high (caar articles
))
1468 (dolist (article articles
)
1469 (setq low
(car article
))
1470 (push (car article
) existing
)
1471 (dolist (flag (cdr article
))
1472 (setq mark
(assoc flag marks
))
1474 (push (list flag
(car article
)) marks
)
1475 (setcdr mark
(cons (car article
) (cdr mark
))))))
1476 (push (list group existing marks high low uidnext start-article
1477 permanent-flags uidvalidity vanished highestmodseq
)
1481 (defun nnimap-parse-flags (sequences)
1482 (goto-char (point-min))
1483 ;; Change \Delete etc to %Delete, so that the reader can read it.
1484 (subst-char-in-region (point-min) (point-max)
1486 ;; Remove any MODSEQ entries in the buffer, because they may contain
1487 ;; numbers that are too large for 32-bit Emacsen.
1488 (while (re-search-forward " MODSEQ ([0-9]+)" nil t
)
1489 (replace-match "" t t
))
1490 (goto-char (point-min))
1491 (let (start end articles groups uidnext elems permanent-flags
1492 uidvalidity vanished highestmodseq
)
1493 (dolist (elem sequences
)
1494 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1496 (setq start
(point))
1498 ;; The EXAMINE was successful.
1499 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1504 (setq permanent-flags
1505 (if (equal command
"SELECT")
1506 (and (search-forward "PERMANENTFLAGS "
1507 (or end
(point-min)) t
)
1508 (read (current-buffer)))
1512 (and (search-forward "UIDNEXT "
1513 (or end
(point-min)) t
)
1514 (read (current-buffer))))
1517 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1518 (or end
(point-min)) t
)
1519 ;; Store UIDVALIDITY as a string, as it's
1520 ;; too big for 32-bit Emacsen, usually.
1524 (and (eq flag-sequence
'qresync
)
1525 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1526 (or end
(point-min)) t
)
1530 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1531 (or end
(point-min)) t
)
1535 ;; The UID FETCH FLAGS was successful.
1536 (or (eq flag-sequence
'qresync
)
1537 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1538 (if (eq flag-sequence
'qresync
)
1542 (setq start
(point))
1544 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1546 (setq elems
(read (current-buffer)))
1547 (push (cons (cadr (memq 'UID elems
))
1548 (cadr (memq 'FLAGS elems
)))
1550 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1551 vanished highestmodseq
)
1555 (setq articles nil
))))
1558 (defun nnimap-find-process-buffer (buffer)
1559 (cadr (assoc buffer nnimap-connection-alist
)))
1561 (deffoo nnimap-request-post
(&optional server
)
1562 (setq nnimap-status-string
"Read-only server")
1565 (declare-function gnus-fetch-headers
"gnus-sum"
1566 (articles &optional limit force-new dependencies
))
1568 (deffoo nnimap-request-thread
(header)
1569 (let* ((id (mail-header-id header
))
1571 (or (mail-header-references header
)
1575 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1577 (dolist (refid refs value
)
1579 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1580 refid refid value
)))))
1581 (result (with-current-buffer (nnimap-buffer)
1582 (nnimap-command "UID SEARCH %s" cmd
))))
1585 (and (car result
) (delete 0 (mapcar #'string-to-number
1586 (cdr (assoc "SEARCH" (cdr result
))))))
1589 (defun nnimap-possibly-change-group (group server
)
1590 (let ((open-result t
))
1592 (not (nnimap-server-opened server
)))
1593 (setq open-result
(nnimap-open-server server
)))
1600 (with-current-buffer (nnimap-buffer)
1601 (if (equal group
(nnimap-group nnimap-object
))
1603 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1605 (setf (nnimap-group nnimap-object
) group
1606 (nnimap-select-result nnimap-object
) result
)
1609 (defun nnimap-find-connection (buffer)
1610 "Find the connection delivering to BUFFER."
1611 (let ((entry (assoc buffer nnimap-connection-alist
)))
1613 (if (and (buffer-name (cadr entry
))
1614 (get-buffer-process (cadr entry
))
1615 (memq (process-status (get-buffer-process (cadr entry
)))
1617 (get-buffer-process (cadr entry
))
1618 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1621 (defvar nnimap-sequence
0)
1623 (defun nnimap-send-command (&rest args
)
1624 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1625 (process-send-string
1626 (get-buffer-process (current-buffer))
1629 (incf nnimap-sequence
)
1630 (apply #'format args
)
1631 (if (nnimap-newlinep nnimap-object
)
1634 ;; Some servers apparently can't have many outstanding
1635 ;; commands, so throttle them.
1636 (unless nnimap-streaming
1637 (nnimap-wait-for-response nnimap-sequence
))
1640 (defun nnimap-log-command (command)
1641 (with-current-buffer (get-buffer-create "*imap log*")
1642 (goto-char (point-max))
1643 (insert (format-time-string "%H:%M:%S") " "
1644 (if nnimap-inhibit-logging
1649 (defun nnimap-command (&rest args
)
1651 (let* ((sequence (apply #'nnimap-send-command args
))
1652 (response (nnimap-get-response sequence
)))
1653 (if (equal (caar response
) "OK")
1655 (nnheader-report 'nnimap
"%s"
1656 (mapconcat (lambda (a)
1658 (car response
) " "))
1661 (defun nnimap-get-response (sequence)
1662 (nnimap-wait-for-response sequence
)
1663 (nnimap-parse-response))
1665 (defun nnimap-wait-for-connection (&optional regexp
)
1666 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1668 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1669 (let ((process (get-buffer-process (current-buffer))))
1670 (goto-char (point-min))
1671 (while (and (memq (process-status process
)
1673 (not (re-search-forward regexp nil t
)))
1674 (nnheader-accept-process-output process
)
1675 (goto-char (point-min)))
1677 (and (looking-at (or response-regexp regexp
))
1680 (defun nnimap-wait-for-response (sequence &optional messagep
)
1681 (let ((process (get-buffer-process (current-buffer)))
1685 (goto-char (point-max))
1686 (while (and (setq openp
(memq (process-status process
)
1689 ;; Skip past any "*" lines that the server has
1691 (while (and (not (bobp))
1694 (looking-at "\\*"))))
1695 (not (looking-at (format "%d .*\n" sequence
)))))
1697 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1698 (nnheader-accept-process-output process
)
1699 (goto-char (point-max)))
1704 ;; The user hit C-g while we were waiting: kill the process, in case
1705 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1707 (delete-process process
)
1710 (defun nnimap-parse-response ()
1711 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1713 (dolist (line lines
)
1714 (push (cdr (nnimap-parse-line line
)) result
))
1715 ;; Return the OK/error code first, and then all the "continuation
1716 ;; lines" afterwards.
1718 (nreverse result
))))
1720 ;; Parse an IMAP response line lightly. They look like
1721 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1722 ;; the lines into a list of strings and lists of string.
1723 (defun nnimap-parse-line (line)
1726 (mm-disable-multibyte)
1728 (goto-char (point-min))
1730 (if (eql (setq char
(following-char)) ?
)
1738 (if (search-forward "]" (line-end-position) 'move
)
1745 (if (search-forward ")" (line-end-position) 'move
)
1752 (1- (or (search-forward "\"" (line-end-position) 'move
)
1755 (buffer-substring (point) (if (search-forward " " nil t
)
1757 (goto-char (point-max))))))
1759 (nreverse result
))))
1761 (defun nnimap-last-response-string ()
1764 (let ((end (point)))
1768 (while (and (not (bobp))
1769 (eql (following-char) ?
*))
1771 (unless (eql (following-char) ?
*)
1773 (buffer-substring (point) end
))))
1775 (defun nnimap-get-responses (sequences)
1777 (dolist (sequence sequences
)
1778 (goto-char (point-min))
1779 (when (re-search-forward (format "^%d " sequence
) nil t
)
1780 (push (list sequence
(nnimap-parse-response))
1784 (defvar nnimap-incoming-split-list nil
)
1786 (defun nnimap-fetch-inbox (articles)
1788 (nnimap-wait-for-response
1789 (nnimap-send-command
1791 (nnimap-article-ranges articles
)
1792 (format "(UID %s%s)"
1798 (nnimap-split-download-body-default
1806 (defun nnimap-split-incoming-mail ()
1807 (with-current-buffer (nnimap-buffer)
1808 (let ((nnimap-incoming-split-list nil
)
1809 (nnmail-split-methods
1811 ((eq nnimap-split-methods
'default
)
1812 nnmail-split-methods
)
1813 (nnimap-split-methods
1814 nnimap-split-methods
)
1816 'nnmail-split-fancy
)))
1817 (nnmail-split-fancy (or nnimap-split-fancy
1818 nnmail-split-fancy
))
1819 (nnmail-inhibit-default-split-group t
)
1820 (groups (nnimap-get-groups))
1823 (nnimap-command "SELECT %S" nnimap-inbox
)
1824 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1825 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1827 (nnimap-fetch-inbox new-articles
)
1828 (nnimap-transform-split-mail)
1829 (nnheader-ms-strip-cr)
1831 (nnmail-split-incoming (current-buffer)
1832 #'nnimap-save-mail-spec
1834 #'nnimap-dummy-active-number
1835 #'nnimap-save-mail-spec
)
1836 (when nnimap-incoming-split-list
1837 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1838 sequences junk-articles
)
1839 ;; Create any groups that doesn't already exist on the
1841 (dolist (spec specs
)
1842 (when (and (not (member (car spec
) groups
))
1843 (not (eq (car spec
) 'junk
)))
1844 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1845 ;; Then copy over all the messages.
1847 (dolist (spec specs
)
1848 (let ((group (car spec
))
1849 (ranges (cdr spec
)))
1850 (if (eq group
'junk
)
1851 (setq junk-articles ranges
)
1852 (push (list (nnimap-send-command
1854 (nnimap-article-ranges ranges
)
1855 (utf7-encode group t
))
1858 ;; Wait for the last COPY response...
1860 (nnimap-wait-for-response (caar sequences
))
1861 ;; And then mark the successful copy actions as deleted,
1862 ;; and possibly expunge them.
1863 (nnimap-mark-and-expunge-incoming
1864 (nnimap-parse-copied-articles sequences
)))
1865 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1867 (defun nnimap-mark-and-expunge-incoming (range)
1869 (setq range
(nnimap-article-ranges range
))
1872 (nnimap-send-command
1873 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1875 ;; If the server supports it, we now delete the message we have
1876 ;; just copied over.
1877 ((nnimap-capability "UIDPLUS")
1878 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1879 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1880 ;; user has configured it.
1882 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1883 (nnimap-wait-for-response sequence
))))
1885 (defun nnimap-parse-copied-articles (sequences)
1886 (let (sequence copied range
)
1887 (goto-char (point-min))
1888 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t
)
1889 (setq sequence
(string-to-number (match-string 1)))
1890 (when (setq range
(cadr (assq sequence sequences
)))
1891 (push (gnus-uncompress-range range
) copied
)))
1892 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1894 (defun nnimap-new-articles (flags)
1896 (dolist (elem flags
)
1897 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1899 (push (car elem
) new
)))
1900 (gnus-compress-sequence (nreverse new
))))
1902 (defun nnimap-make-split-specs (list)
1906 (destructuring-bind (article spec
) elem
1907 (dolist (group (delete nil
(mapcar #'car spec
)))
1908 (unless (setq entry
(assoc group specs
))
1909 (push (setq entry
(list group
)) specs
))
1910 (setcdr entry
(cons article
(cdr entry
))))))
1911 (dolist (entry specs
)
1912 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
1915 (defun nnimap-transform-split-mail ()
1916 (goto-char (point-min))
1917 (let (article bytes
)
1920 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
1921 (delete-region (point) (progn (forward-line 1) (point)))
1924 (setq article
(match-string 1)
1925 bytes
(nnimap-get-length))
1926 (delete-region (line-beginning-position) (line-end-position))
1927 ;; Insert MMDF separator, and a way to remember what this
1929 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
1930 (forward-char (1+ bytes
))
1931 (setq bytes
(nnimap-get-length))
1932 (delete-region (line-beginning-position) (line-end-position))
1933 ;; There's a body; skip past that.
1935 (forward-char (1+ bytes
))
1936 (delete-region (line-beginning-position) (line-end-position)))))))
1938 (defun nnimap-dummy-active-number (group &optional server
)
1941 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
1943 (goto-char (point-min))
1944 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
1945 (error "Invalid nnimap mail")
1946 (setq article
(string-to-number (match-string 1))))
1948 (if (eq group-art
'junk
)
1949 (list (cons 'junk
1))
1951 nnimap-incoming-split-list
)))
1955 ;;; nnimap.el ends here