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
)
172 (setq group
(nnimap-decode-gnus-group group
)))
173 (with-current-buffer nntp-server-buffer
175 (when (nnimap-possibly-change-group group server
)
176 (with-current-buffer (nnimap-buffer)
178 (nnimap-wait-for-response
181 (nnimap-article-ranges (gnus-compress-sequence articles
))
182 (nnimap-header-parameters))
184 (nnimap-transform-headers)
185 (nnheader-remove-cr-followed-by-lf))
186 (insert-buffer-substring
187 (nnimap-find-process-buffer (current-buffer))))
190 (defun nnimap-transform-headers ()
191 (goto-char (point-min))
192 (let (article bytes lines size string
)
195 (while (not (looking-at "\\* [0-9]+ FETCH.+?UID \\([0-9]+\\)"))
196 (delete-region (point) (progn (forward-line 1) (point)))
199 (setq article
(match-string 1))
200 ;; Unfold quoted {number} strings.
201 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
202 (1+ (line-end-position)) t
)
203 (setq size
(string-to-number (match-string 1)))
204 (delete-region (+ (match-beginning 0) 2) (point))
205 (setq string
(buffer-substring (point) (+ (point) size
)))
206 (delete-region (point) (+ (point) size
))
207 (insert (format "%S" string
)))
208 (setq bytes
(nnimap-get-length)
212 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
217 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
218 (let ((structure (ignore-errors
219 (read (current-buffer)))))
220 (while (and (consp structure
)
221 (not (atom (car structure
))))
222 (setq structure
(car structure
)))
224 (stringp (car structure
))
225 (equal (upcase (nth 0 structure
)) "MESSAGE")
226 (equal (upcase (nth 1 structure
)) "RFC822"))
228 (nth 7 structure
)))))
229 (delete-region (line-beginning-position) (line-end-position))
230 (insert (format "211 %s Article retrieved." article
))
233 (insert (format "Chars: %s\n" size
)))
235 (insert (format "Lines: %s\n" lines
)))
236 (unless (re-search-forward "^\r$" nil t
)
237 (goto-char (point-max)))
238 (delete-region (line-beginning-position) (line-end-position))
242 (defun nnimap-unfold-quoted-lines ()
243 ;; Unfold quoted {number} strings.
245 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
246 (setq size
(string-to-number (match-string 1)))
247 (delete-region (1+ (match-beginning 0)) (point))
248 (setq string
(buffer-substring (point) (+ (point) size
)))
249 (delete-region (point) (+ (point) size
))
250 (insert (format "%S" string
)))))
252 (defun nnimap-get-length ()
253 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
254 (string-to-number (match-string 1))))
256 (defun nnimap-article-ranges (ranges)
260 (number-to-string ranges
))
261 ((numberp (cdr ranges
))
262 (format "%d:%d" (car ranges
) (cdr ranges
)))
264 (dolist (elem ranges
)
267 (format "%d:%d" (car elem
) (cdr elem
))
268 (number-to-string elem
))
270 (mapconcat #'identity
(nreverse result
) ",")))))
272 (deffoo nnimap-open-server
(server &optional defs
)
273 (if (nnimap-server-opened server
)
275 (unless (assq 'nnimap-address defs
)
276 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
277 (nnoo-change-server 'nnimap server defs
)
278 (or (nnimap-find-connection nntp-server-buffer
)
279 (nnimap-open-connection nntp-server-buffer
))))
281 (defun nnimap-make-process-buffer (buffer)
283 (generate-new-buffer (format "*nnimap %s %s %s*"
284 nnimap-address nnimap-server-port
285 (gnus-buffer-exists-p buffer
)))
286 (mm-disable-multibyte)
287 (buffer-disable-undo)
289 (set (make-local-variable 'after-change-functions
) nil
)
290 (set (make-local-variable 'nnimap-object
)
291 (make-nnimap :server
(nnoo-current-server 'nnimap
)))
292 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
293 (push (current-buffer) nnimap-process-buffers
)
296 (defun nnimap-credentials (address ports user
)
297 (let* ((auth-source-creation-prompts
298 '((user .
"IMAP user at %h: ")
299 (secret .
"IMAP password for %u@%h: ")))
300 (found (nth 0 (auth-source-search :max
1
304 :require
'(:user
:secret
)
307 (list (plist-get found
:user
)
308 (let ((secret (plist-get found
:secret
)))
309 (if (functionp secret
)
312 (plist-get found
:save-function
))
315 (defun nnimap-keepalive ()
316 (let ((now (current-time)))
317 (dolist (buffer nnimap-process-buffers
)
318 (when (buffer-name buffer
)
319 (with-current-buffer buffer
320 (when (and nnimap-object
321 (nnimap-last-command-time nnimap-object
)
325 (nnimap-last-command-time nnimap-object
)))
326 ;; More than five minutes since the last command.
328 (nnimap-send-command "NOOP")))))))
330 (defun nnimap-open-connection (buffer)
331 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
332 ;; `ssl' when nnimap-server-port was nil. Sort of.
333 (when (and nnimap-server-port
334 (eq nnimap-stream
'undecided
))
335 (setq nnimap-stream
'ssl
))
337 (if (eq nnimap-stream
'undecided
)
338 (loop for type in
'(ssl network
)
339 for stream
= (let ((nnimap-stream type
))
340 (nnimap-open-connection-1 buffer
))
341 while
(eq stream
'no-connect
)
342 finally
(return stream
))
343 (nnimap-open-connection-1 buffer
))))
344 (if (eq stream
'no-connect
)
348 (defun nnimap-map-port (port)
349 (if (equal port
"imaps")
353 (defun nnimap-open-connection-1 (buffer)
354 (unless nnimap-keepalive-timer
355 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
357 (with-current-buffer (nnimap-make-process-buffer buffer
)
358 (let* ((coding-system-for-read 'binary
)
359 (coding-system-for-write 'binary
)
362 ((memq nnimap-stream
'(network plain starttls
))
363 (nnheader-message 7 "Opening connection to %s..."
366 ((eq nnimap-stream
'shell
)
367 (nnheader-message 7 "Opening connection to %s via shell..."
370 ((memq nnimap-stream
'(ssl tls
))
371 (nnheader-message 7 "Opening connection to %s via tls..."
373 '("imaps" "imap" "993" "143"))
375 (error "Unknown stream type: %s" nnimap-stream
))))
376 login-result credentials
)
377 (when nnimap-server-port
378 (push nnimap-server-port ports
))
380 (open-protocol-stream
381 "*nnimap*" (current-buffer) nnimap-address
382 (nnimap-map-port (car ports
))
385 :shell-command nnimap-shell-program
386 :capability-command
"1 CAPABILITY\r\n"
387 :end-of-command
"\r\n"
390 (lambda (capabilities)
391 (when (gnus-string-match-p "STARTTLS" capabilities
)
393 (stream (car stream-list
))
394 (props (cdr stream-list
))
395 (greeting (plist-get props
:greeting
))
396 (capabilities (plist-get props
:capabilities
))
397 (stream-type (plist-get props
:type
)))
398 (when (and stream
(not (memq (process-status stream
) '(open run
))))
400 (setf (nnimap-process nnimap-object
) stream
)
401 (setf (nnimap-stream-type nnimap-object
) stream-type
)
404 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
405 nnimap-address
(car ports
) nnimap-stream
)
407 (gnus-set-process-query-on-exit-flag stream nil
)
408 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
409 (nnheader-report 'nnimap
"%s" greeting
)
410 ;; Store the greeting (for debugging purposes).
411 (setf (nnimap-greeting nnimap-object
) greeting
)
412 (setf (nnimap-capabilities nnimap-object
)
414 (split-string capabilities
)))
415 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
416 (if (not (setq credentials
417 (if (eq nnimap-authenticator
'anonymous
)
419 (message-make-address))
420 ;; Look for the credentials based on
421 ;; the virtual server name and the address
423 (gnus-delete-duplicates
426 (nnoo-current-server 'nnimap
)))
429 (setq nnimap-object nil
)
430 (let ((nnimap-inhibit-logging t
))
432 (nnimap-login (car credentials
) (cadr credentials
))))
433 (if (car login-result
)
435 ;; Save the credentials if a save function exists
436 ;; (such a function will only be passed if a new
437 ;; token was created).
438 (when (functionp (nth 2 credentials
))
439 (funcall (nth 2 credentials
)))
440 ;; See if CAPABILITY is set as part of login
442 (dolist (response (cddr login-result
))
443 (when (string= "CAPABILITY" (upcase (car response
)))
444 (setf (nnimap-capabilities nnimap-object
)
445 (mapcar #'upcase
(cdr response
))))))
446 ;; If the login failed, then forget the credentials
447 ;; that are now possibly cached.
448 (dolist (host (list (nnoo-current-server 'nnimap
)
451 (auth-source-forget+ :host host
:port port
)))
452 (delete-process (nnimap-process nnimap-object
))
453 (setq nnimap-object nil
))))
455 (when (nnimap-capability "QRESYNC")
456 (nnimap-command "ENABLE QRESYNC"))
457 (nnimap-process nnimap-object
))))))))
459 (autoload 'rfc2104-hash
"rfc2104")
461 (defun nnimap-login (user password
)
463 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
464 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
465 ;; and we're using an encrypted connection.
466 ((and (not (nnimap-capability "LOGINDISABLED"))
467 (eq (nnimap-stream-type nnimap-object
) 'tls
))
468 (nnimap-command "LOGIN %S %S" user password
))
469 ((nnimap-capability "AUTH=CRAM-MD5")
471 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
472 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
474 (get-buffer-process (current-buffer))
476 (base64-encode-string
478 (rfc2104-hash 'md5
64 16 password
479 (base64-decode-string challenge
))))
481 (nnimap-wait-for-response sequence
)))
482 ((not (nnimap-capability "LOGINDISABLED"))
483 (nnimap-command "LOGIN %S %S" user password
))
484 ((nnimap-capability "AUTH=PLAIN")
486 "AUTHENTICATE PLAIN %s"
487 (base64-encode-string
488 (format "\000%s\000%s"
489 (nnimap-quote-specials user
)
490 (nnimap-quote-specials password
)))))))
492 (defun nnimap-quote-specials (string)
495 (goto-char (point-min))
496 (while (re-search-forward "[\\\"]" nil t
)
502 (defun nnimap-find-parameter (parameter elems
)
506 ((equal (car elem
) parameter
)
507 (setq result
(cdr elem
)))
508 ((and (equal (car elem
) "OK")
510 (equal (caadr elem
) parameter
))
511 (setq result
(cdr (cadr elem
))))))
514 (deffoo nnimap-close-server
(&optional server
)
515 (when (nnoo-change-server 'nnimap server nil
)
517 (delete-process (get-buffer-process (nnimap-buffer))))
518 (nnoo-close-server 'nnimap server
)
521 (deffoo nnimap-request-close
()
524 (deffoo nnimap-server-opened
(&optional server
)
525 (and (nnoo-current-server-p 'nnimap server
)
527 (gnus-buffer-live-p nntp-server-buffer
)
528 (nnimap-find-connection nntp-server-buffer
)))
530 (deffoo nnimap-status-message
(&optional server
)
531 nnimap-status-string
)
533 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
535 (setq group
(nnimap-decode-gnus-group group
)))
536 (with-current-buffer nntp-server-buffer
537 (let ((result (nnimap-possibly-change-group group server
))
539 (when (stringp article
)
540 (setq article
(nnimap-find-article-by-message-id group article
)))
544 (with-current-buffer (nnimap-buffer)
546 (when nnimap-fetch-partial-articles
547 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
548 (goto-char (point-min))
549 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
550 (setq structure
(ignore-errors
551 (let ((start (point)))
553 (downcase-region start
(point))
555 (read (current-buffer))))
556 parts
(nnimap-find-wanted-parts structure
))))
558 (nnimap-get-partial-article article parts structure
)
559 (nnimap-get-whole-article article
))
560 (let ((buffer (current-buffer)))
561 (with-current-buffer (or to-buffer nntp-server-buffer
)
562 (nnheader-insert-buffer-substring buffer
)
563 (nnheader-ms-strip-cr)))
564 (cons group article
)))))))
566 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
568 (setq group
(nnimap-decode-gnus-group group
)))
569 (when (nnimap-possibly-change-group group server
)
570 (with-current-buffer (nnimap-buffer)
571 (when (stringp article
)
572 (setq article
(nnimap-find-article-by-message-id group article
)))
575 (nnimap-get-whole-article
576 article
(format "UID FETCH %%d %s"
577 (nnimap-header-parameters)))
578 (let ((buffer (current-buffer)))
579 (with-current-buffer (or to-buffer nntp-server-buffer
)
581 (insert-buffer-substring buffer
)
582 (nnheader-ms-strip-cr)
583 (cons group article
)))))))
585 (defun nnimap-get-whole-article (article &optional command
)
590 "UID FETCH %d BODY.PEEK[]"
591 "UID FETCH %d RFC822.PEEK"))
593 ;; Check that we really got an article.
594 (goto-char (point-min))
595 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
598 ;; Remove any data that may have arrived before the FETCH data.
601 (delete-region (point-min) (point)))
602 (let ((bytes (nnimap-get-length)))
603 (delete-region (line-beginning-position)
604 (progn (forward-line 1) (point)))
605 (goto-char (+ (point) bytes
))
606 (delete-region (point) (point-max)))
609 (defun nnimap-capability (capability)
610 (member capability
(nnimap-capabilities nnimap-object
)))
612 (defun nnimap-ver4-p ()
613 (nnimap-capability "IMAP4REV1"))
615 (defun nnimap-get-partial-article (article parts structure
)
618 "UID FETCH %d (%s %s)"
624 (mapconcat (lambda (part)
625 (format "BODY.PEEK[%s]" part
))
627 (mapconcat (lambda (part)
628 (format "RFC822.PEEK[%s]" part
))
631 (nnimap-convert-partial-article structure
))))
633 (defun nnimap-convert-partial-article (structure)
634 ;; First just skip past the headers.
635 (goto-char (point-min))
636 (let ((bytes (nnimap-get-length))
638 ;; Delete "FETCH" line.
639 (delete-region (line-beginning-position)
640 (progn (forward-line 1) (point)))
641 (goto-char (+ (point) bytes
))
642 ;; Collect all the body parts.
643 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
644 (setq id
(match-string 1)
645 bytes
(or (nnimap-get-length) 0))
647 (delete-region (point) (progn (forward-line 1) (point)))
648 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
650 (delete-region (point) (+ (point) bytes
)))
651 ;; Delete trailing junk.
652 (delete-region (point) (point-max))
653 ;; Now insert all the parts again where they fit in the structure.
654 (nnimap-insert-partial-structure structure parts
)
657 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
659 (let ((bstruc structure
))
660 (while (consp (car bstruc
))
662 (setq type
(car bstruc
))
663 (setq bstruc
(car (cdr bstruc
)))
664 (let ((has-boundary (member "boundary" bstruc
)))
666 (setq boundary
(cadr has-boundary
)))))
668 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
669 (downcase type
) boundary
)))
670 (while (not (stringp (car structure
)))
671 (insert "\n--" boundary
"\n")
672 (if (consp (caar structure
))
673 (nnimap-insert-partial-structure (pop structure
) parts t
)
674 (let ((bit (pop structure
)))
675 (insert (format "Content-type: %s/%s"
676 (downcase (nth 0 bit
))
677 (downcase (nth 1 bit
))))
678 (if (member "CHARSET" (nth 2 bit
))
680 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit
)))))
682 (insert (format "Content-transfer-encoding: %s\n"
685 (when (assoc (nth 9 bit
) parts
)
686 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
687 (insert "\n--" boundary
"--\n")))
689 (defun nnimap-find-wanted-parts (structure)
690 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
692 (defun nnimap-find-wanted-parts-1 (structure prefix
)
695 (while (consp (car structure
))
696 (let ((sub (pop structure
)))
697 (if (consp (car sub
))
698 (push (nnimap-find-wanted-parts-1
699 sub
(if (string= prefix
"")
700 (number-to-string num
)
701 (format "%s.%s" prefix num
)))
703 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
704 (id (if (string= prefix
"")
705 (number-to-string num
)
706 (format "%s.%s" prefix num
))))
707 (setcar (nthcdr 9 sub
) id
)
708 (when (if (eq nnimap-fetch-partial-articles t
)
710 (string-match nnimap-fetch-partial-articles type
))
715 (defun nnimap-decode-gnus-group (group)
716 (decode-coding-string group
'utf-8
))
718 (deffoo nnimap-request-group
(group &optional server dont-check info
)
719 (setq group
(nnimap-decode-gnus-group group
))
720 (let ((result (nnimap-possibly-change-group
721 ;; Don't SELECT the group if we're going to select it
723 (if (and (not dont-check
)
724 (assoc group nnimap-current-infos
))
728 articles active marks high low
)
729 (with-current-buffer nntp-server-buffer
732 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
733 (insert (format "211 %d %d %d %S\n"
734 (- (cdr active
) (car active
))
738 (with-current-buffer (nnimap-buffer)
740 (let ((group-sequence
741 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
743 (nnimap-send-command "UID FETCH 1:* FLAGS")))
744 (setf (nnimap-group nnimap-object
) group
)
745 (nnimap-wait-for-response flag-sequence
)
747 (nnimap-flags-to-marks
749 (list (list group-sequence flag-sequence
750 1 group
"SELECT")))))
753 (nnimap-update-infos marks
(list info
))
754 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
755 (goto-char (point-max))
756 (let ((uidnext (nth 5 (car marks
))))
757 (setq high
(or (if uidnext
761 low
(or (nth 4 (car marks
)) uidnext
1)))))
765 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
768 (deffoo nnimap-request-create-group
(group &optional server args
)
769 (setq group
(nnimap-decode-gnus-group group
))
770 (when (nnimap-possibly-change-group nil server
)
771 (with-current-buffer (nnimap-buffer)
772 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
774 (deffoo nnimap-request-delete-group
(group &optional force server
)
775 (setq group
(nnimap-decode-gnus-group group
))
776 (when (nnimap-possibly-change-group nil server
)
777 (with-current-buffer (nnimap-buffer)
778 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
780 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
781 (setq group
(nnimap-decode-gnus-group group
))
782 (when (nnimap-possibly-change-group nil server
)
783 (with-current-buffer (nnimap-buffer)
784 (nnimap-unselect-group)
785 (car (nnimap-command "RENAME %S %S"
786 (utf7-encode group t
) (utf7-encode new-name t
))))))
788 (defun nnimap-unselect-group ()
789 ;; Make sure we don't have this group open read/write by asking
790 ;; to examine a mailbox that doesn't exist. This seems to be
791 ;; the only way that allows us to reliably go back to unselected
793 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
795 (deffoo nnimap-request-expunge-group
(group &optional server
)
796 (setq group
(nnimap-decode-gnus-group group
))
797 (when (nnimap-possibly-change-group group server
)
798 (with-current-buffer (nnimap-buffer)
799 (car (nnimap-command "EXPUNGE")))))
801 (defun nnimap-get-flags (spec)
804 (with-current-buffer (nnimap-buffer)
806 (nnimap-wait-for-response (nnimap-send-command
807 "UID FETCH %s FLAGS" spec
))
809 (subst-char-in-region (point-min) (point-max)
811 (goto-char (point-min))
812 (while (search-forward " FETCH " end t
)
813 (setq elems
(read (current-buffer)))
814 (push (cons (cadr (memq 'UID elems
))
815 (cadr (memq 'FLAGS elems
)))
817 (nreverse articles
)))
819 (deffoo nnimap-close-group
(group &optional server
)
822 (deffoo nnimap-request-move-article
(article group server accept-form
823 &optional last internal-move-group
)
824 (setq group
(nnimap-decode-gnus-group group
))
826 (mm-disable-multibyte)
827 (when (funcall (if internal-move-group
829 'nnimap-request-article
)
830 article group server
(current-buffer))
831 ;; If the move is internal (on the same server), just do it the easy
833 (let ((message-id (message-field-value "message-id")))
834 (if internal-move-group
836 (with-current-buffer (nnimap-buffer)
837 (nnimap-command "UID COPY %d %S"
839 (utf7-encode internal-move-group t
)))))
841 (nnimap-delete-article article
)
842 (cons internal-move-group
843 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
844 (nnimap-find-article-by-message-id
845 internal-move-group message-id
)))))
846 ;; Move the article to a different method.
847 (let ((result (eval accept-form
)))
849 (nnimap-delete-article article
)
852 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
853 (setq group
(nnimap-decode-gnus-group group
))
857 ((not (nnimap-possibly-change-group group server
))
860 (eq nnmail-expiry-target
'delete
))
861 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
862 (nnheader-message 7 "Article marked for deletion, but not expunged."))
865 (let ((deletable-articles
867 (eq nnmail-expiry-wait
'immediate
))
869 (gnus-sorted-intersection
871 (nnimap-find-expired-articles group
)))))
872 (if (null deletable-articles
)
874 (if (eq nnmail-expiry-target
'delete
)
875 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
876 (setq deletable-articles
877 (nnimap-process-expiry-targets
878 deletable-articles group server
)))
879 ;; Return the articles we didn't delete.
880 (gnus-sorted-complement articles deletable-articles
))))))
882 (defun nnimap-process-expiry-targets (articles group server
)
883 (let ((deleted-articles nil
))
885 ;; shortcut further processing if we're going to delete the articles
886 ((eq nnmail-expiry-target
'delete
)
887 (setq deleted-articles articles
)
889 ;; or just move them to another folder on the same IMAP server
890 ((and (not (functionp nnmail-expiry-target
))
891 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
892 (gnus-server-to-method
893 (format "nnimap:%s" server
))))
894 (and (nnimap-possibly-change-group group server
)
895 (with-current-buffer (nnimap-buffer)
896 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
899 (nnimap-article-ranges (gnus-compress-sequence articles
))
900 (utf7-encode (gnus-group-real-name nnmail-expiry-target
) t
))
901 (setq deleted-articles articles
)))
904 (dolist (article articles
)
905 (let ((target nnmail-expiry-target
))
907 (mm-disable-multibyte)
908 (when (nnimap-request-article article group server
(current-buffer))
909 (when (functionp target
)
910 (setq target
(funcall target group
)))
912 (not (eq target
'delete
)))
913 (if (or (gnus-request-group target t
)
914 (gnus-request-create-group target
))
916 (nnmail-expiry-target-group target group
)
917 (nnheader-message 7 "Expiring article %s:%d to %s"
918 group article target
))
920 (nnheader-message 7 "Expiring article %s:%d" group article
))
922 (push article deleted-articles
))))))))
923 ;; Change back to the current group again.
924 (nnimap-possibly-change-group group server
)
925 (setq deleted-articles
(nreverse deleted-articles
))
926 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
929 (defun nnimap-find-expired-articles (group)
930 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
931 (with-current-buffer (nnimap-buffer)
934 "UID SEARCH SENTBEFORE %s"
938 (car (rassoc (nth 4 (decode-time cutoff
))
939 parse-time-months
))))
942 (delete 0 (mapcar #'string-to-number
943 (cdr (assoc "SEARCH" (cdr result
))))))))))
946 (defun nnimap-find-article-by-message-id (group message-id
)
947 (with-current-buffer (nnimap-buffer)
949 (unless (equal group
(nnimap-group nnimap-object
))
950 (setf (nnimap-group nnimap-object
) nil
)
951 (setf (nnimap-examined nnimap-object
) group
)
952 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
954 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
956 (setq result
(nnimap-wait-for-response sequence
))
958 (car (setq result
(nnimap-parse-response))))
959 ;; Select the last instance of the message in the group.
961 (car (last (cdr (assoc "SEARCH" (cdr result
))))))
962 (string-to-number article
))))))
964 (defun nnimap-delete-article (articles)
965 (with-current-buffer (nnimap-buffer)
966 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
967 (nnimap-article-ranges articles
))
969 ((nnimap-capability "UIDPLUS")
970 (nnimap-command "UID EXPUNGE %s"
971 (nnimap-article-ranges articles
))
974 (nnimap-command "EXPUNGE")
976 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
977 "server doesn't support UIDPLUS, so we won't "
978 "delete this article now"))))))
980 (deffoo nnimap-request-scan
(&optional group server
)
982 (setq group
(nnimap-decode-gnus-group group
)))
983 (when (and (nnimap-possibly-change-group nil server
)
985 nnimap-split-methods
)
986 (nnheader-message 7 "nnimap %s splitting mail..." server
)
987 (nnimap-split-incoming-mail)
988 (nnheader-message 7 "nnimap %s splitting mail...done" server
)))
990 (defun nnimap-marks-to-flags (marks)
993 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
997 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
998 (setq group
(nnimap-decode-gnus-group group
))
999 (when (nnimap-possibly-change-group nil server
)
1000 (let ((command (assoc
1002 '((subscribe "SUBSCRIBE")
1003 (unsubscribe "UNSUBSCRIBE")))))
1005 (with-current-buffer (nnimap-buffer)
1006 (nnimap-command "%s %S" (cadr command
) (utf7-encode group t
)))))))
1008 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
1009 (setq group
(nnimap-decode-gnus-group group
))
1010 (when (nnimap-possibly-change-group group server
)
1012 (with-current-buffer (nnimap-buffer)
1014 ;; Just send all the STORE commands without waiting for
1015 ;; response. If they're successful, they're successful.
1016 (dolist (action actions
)
1017 (destructuring-bind (range action marks
) action
1018 (let ((flags (nnimap-marks-to-flags marks
)))
1020 (setq sequence
(nnimap-send-command
1021 "UID STORE %s %sFLAGS.SILENT (%s)"
1022 (nnimap-article-ranges range
)
1024 ((eq action
'del
) "-")
1025 ((eq action
'add
) "+")
1026 ((eq action
'set
) ""))
1027 (mapconcat #'identity flags
" ")))))))
1028 ;; Wait for the last command to complete to avoid later
1029 ;; syncronisation problems with the stream.
1031 (nnimap-wait-for-response sequence
))))))
1033 (deffoo nnimap-request-accept-article
(group &optional server last
)
1034 (setq group
(nnimap-decode-gnus-group group
))
1035 (when (nnimap-possibly-change-group nil server
)
1036 (nnmail-check-syntax)
1037 (let ((message-id (message-field-value "message-id"))
1040 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
1041 (with-current-buffer (nnimap-buffer)
1042 (when (setq message
(or (nnimap-process-quirk "OK Gimap " 'append message
)
1044 ;; If we have this group open read-only, then unselect it
1045 ;; before appending to it.
1046 (when (equal (nnimap-examined nnimap-object
) group
)
1047 (nnimap-unselect-group))
1049 (setq sequence
(nnimap-send-command
1050 "APPEND %S {%d}" (utf7-encode group t
)
1052 (unless nnimap-streaming
1053 (nnimap-wait-for-connection "^[+]"))
1054 (process-send-string (get-buffer-process (current-buffer)) message
)
1055 (process-send-string (get-buffer-process (current-buffer))
1056 (if (nnimap-newlinep nnimap-object
)
1059 (let ((result (nnimap-get-response sequence
)))
1060 (if (not (nnimap-ok-p result
))
1062 (nnheader-report 'nnimap
"%s" result
)
1065 (or (nnimap-find-uid-response "APPENDUID" (car result
))
1066 (nnimap-find-article-by-message-id
1067 group message-id
))))))))))
1069 (defun nnimap-process-quirk (greeting-match type data
)
1070 (when (and (nnimap-greeting nnimap-object
)
1071 (string-match greeting-match
(nnimap-greeting nnimap-object
))
1073 (string-match "\000" data
))
1074 (let ((choice (gnus-multiple-choice
1075 "Message contains NUL characters. Delete, continue, abort? "
1076 '((?d
"Delete NUL characters")
1077 (?c
"Try to APPEND the message as is")
1081 (nnheader-report 'nnimap
"Aborted APPEND due to NUL characters"))
1087 (goto-char (point-min))
1088 (while (search-forward "\000" nil t
)
1089 (replace-match "" t t
))
1090 (buffer-string)))))))
1092 (defun nnimap-ok-p (value)
1095 (equal (caar value
) "OK")))
1097 (defun nnimap-find-uid-response (name list
)
1098 (let ((result (car (last (nnimap-find-response-element name list
)))))
1100 (string-to-number result
))))
1102 (defun nnimap-find-response-element (name list
)
1105 (when (and (consp elem
)
1106 (equal name
(car elem
)))
1107 (setq result elem
)))
1110 (deffoo nnimap-request-replace-article
(article group buffer
)
1111 (setq group
(nnimap-decode-gnus-group group
))
1113 (when (and (nnimap-possibly-change-group group nil
)
1114 ;; Put the article into the group.
1115 (with-current-buffer buffer
1117 (nnimap-request-accept-article group nil t
))))
1118 (nnimap-delete-article (list article
))
1119 ;; Return the new article number.
1122 (defun nnimap-add-cr ()
1123 (goto-char (point-min))
1124 (while (re-search-forward "\r?\n" nil t
)
1125 (replace-match "\r\n" t t
)))
1127 (defun nnimap-get-groups ()
1129 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1131 (nnimap-wait-for-response sequence
)
1132 (subst-char-in-region (point-min) (point-max)
1134 (goto-char (point-min))
1135 (nnimap-unfold-quoted-lines)
1136 (goto-char (point-min))
1137 (while (search-forward "* LIST " nil t
)
1138 (let ((flags (read (current-buffer)))
1139 (separator (read (current-buffer)))
1140 (group (read (current-buffer))))
1141 (unless (member '%NoSelect flags
)
1142 (push (utf7-decode (if (stringp group
)
1144 (format "%s" group
)) t
)
1148 (deffoo nnimap-request-list
(&optional server
)
1149 (when (nnimap-possibly-change-group nil server
)
1150 (with-current-buffer nntp-server-buffer
1153 (with-current-buffer (nnimap-buffer)
1154 (nnimap-get-groups)))
1155 sequences responses
)
1157 (with-current-buffer (nnimap-buffer)
1158 (setf (nnimap-group nnimap-object
) nil
)
1159 (dolist (group groups
)
1160 (setf (nnimap-examined nnimap-object
) group
)
1161 (push (list (nnimap-send-command "EXAMINE %S"
1162 (utf7-encode group t
))
1165 (nnimap-wait-for-response (caar sequences
))
1167 (nnimap-get-responses (mapcar #'car sequences
))))
1168 (dolist (response responses
)
1169 (let* ((sequence (car response
))
1170 (response (cadr response
))
1171 (group (cadr (assoc sequence sequences
))))
1173 (equal (caar response
) "OK"))
1174 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1176 (dolist (elem response
)
1177 (when (equal (cadr elem
) "EXISTS")
1178 (setq exists
(string-to-number (car elem
)))))
1180 (setq highest
(1- (string-to-number (car uidnext
)))))
1183 (insert (format "%S 0 1 y\n" (utf7-decode group t
))))
1186 (insert (format "%S %d %d y\n"
1187 (utf7-decode group t
)
1188 highest
(1+ highest
))))
1190 ;; Return the widest possible range.
1191 (insert (format "%S %d 1 y\n" (utf7-decode group t
)
1192 (or highest exists
)))))))))
1195 (deffoo nnimap-request-newgroups
(date &optional server
)
1196 (when (nnimap-possibly-change-group nil server
)
1197 (with-current-buffer nntp-server-buffer
1199 (dolist (group (with-current-buffer (nnimap-buffer)
1200 (nnimap-get-groups)))
1201 (unless (assoc group nnimap-current-infos
)
1202 ;; Insert dummy numbers here -- they don't matter.
1203 (insert (format "%S 0 1 y\n" (utf7-encode group
)))))
1206 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1207 (when (nnimap-possibly-change-group nil server
)
1208 (with-current-buffer (nnimap-buffer)
1210 (setf (nnimap-group nnimap-object
) nil
)
1211 (let ((qresyncp (nnimap-capability "QRESYNC"))
1212 params groups sequences active uidvalidity modseq group
)
1213 ;; Go through the infos and gather the data needed to know
1214 ;; what and how to request the data.
1215 (dolist (info infos
)
1216 (setq params
(gnus-info-params info
)
1217 group
(nnimap-decode-gnus-group
1218 (gnus-group-real-name (gnus-info-group info
)))
1219 active
(cdr (assq 'active params
))
1220 uidvalidity
(cdr (assq 'uidvalidity params
))
1221 modseq
(cdr (assq 'modseq params
)))
1222 (setf (nnimap-examined nnimap-object
) group
)
1228 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1229 (utf7-encode group t
)
1230 (nnimap-quirk "QRESYNC")
1236 (if (and active uidvalidity
)
1237 ;; Fetch the last 100 flags.
1238 (max 1 (- (cdr active
) 100))
1243 ;; If we don't have a UIDVALIDITY, then this is
1244 ;; the first time we've seen the group, so we
1245 ;; have to do a SELECT (which is slower than an
1246 ;; examine), but will tell us whether the group
1247 ;; is read-only or not.
1249 (push (list (nnimap-send-command "%s %S" command
1250 (utf7-encode group t
))
1251 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1252 start group command
)
1256 (defun nnimap-quirk (command)
1257 (let ((quirk (assoc command nnimap-quirks
)))
1258 ;; If this server is of a type that matches a quirk, then return
1259 ;; the "quirked" command instead of the proper one.
1260 (if (or (null quirk
)
1261 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1265 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1266 (when (and sequences
1267 ;; Check that the process is still alive.
1268 (get-buffer-process (nnimap-buffer))
1269 (memq (process-status (get-buffer-process (nnimap-buffer)))
1271 (nnimap-possibly-change-group nil server
))
1272 (with-current-buffer (nnimap-buffer)
1273 ;; Wait for the final data to trickle in.
1274 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1278 ;; Now we should have most of the data we need, no matter
1279 ;; whether we're QRESYNCING, fetching all the flags from
1280 ;; scratch, or just fetching the last 100 flags per group.
1281 (nnimap-update-infos (nnimap-flags-to-marks
1283 (nreverse sequences
)))
1285 ;; Finally, just return something resembling an active file in
1286 ;; the nntp buffer, so that the agent can save the info, too.
1287 (with-current-buffer nntp-server-buffer
1289 (dolist (info infos
)
1290 (let* ((group (gnus-info-group info
))
1291 (active (gnus-active group
)))
1293 (insert (format "%S %d %d y\n"
1294 (decode-coding-string
1295 (gnus-group-real-name group
) 'utf-8
)
1297 (car active
)))))))))))
1299 (defun nnimap-update-infos (flags infos
)
1300 (dolist (info infos
)
1301 (let* ((group (nnimap-decode-gnus-group
1302 (gnus-group-real-name (gnus-info-group info
))))
1303 (marks (cdr (assoc group flags
))))
1305 (nnimap-update-info info marks
)))))
1307 (defun nnimap-update-info (info marks
)
1308 (destructuring-bind (existing flags high low uidnext start-article
1309 permanent-flags uidvalidity
1310 vanished highestmodseq
) marks
1312 ;; Ignore groups with no UIDNEXT/marks. This happens for
1313 ;; completely empty groups.
1314 ((and (not existing
)
1316 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1318 (gnus-set-active (gnus-info-group info
) active
))))
1319 ;; We have a mismatch between the old and new UIDVALIDITY
1320 ;; identifiers, so we have to re-request the group info (the next
1321 ;; time). This virtually never happens.
1322 ((let ((old-uidvalidity
1323 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1324 (and old-uidvalidity
1325 (not (equal old-uidvalidity uidvalidity
))
1326 (> start-article
1)))
1327 (gnus-group-remove-parameter info
'uidvalidity
)
1328 (gnus-group-remove-parameter info
'modseq
))
1329 ;; We have the data needed to update.
1331 (let* ((group (gnus-info-group info
))
1332 (completep (and start-article
1333 (= start-article
1)))
1334 (active (or (gnus-active group
)
1335 (cdr (assq 'active
(gnus-info-params info
))))))
1337 (setq high
(1- uidnext
)))
1338 ;; First set the active ranges based on high/low.
1340 (not (gnus-active group
)))
1341 (gnus-set-active group
1344 (cons (min (or low
(car active
))
1346 (max (or high
(cdr active
))
1351 ;; No articles in this group.
1352 (cons uidnext
(1- uidnext
)))
1354 (cons start-article
(1- start-article
)))
1356 ;; No articles and no uidnext.
1358 (gnus-set-active group
1360 (or high
(1- uidnext
)))))
1361 ;; See whether this is a read-only group.
1362 (unless (eq permanent-flags
'not-scanned
)
1363 (gnus-group-set-parameter
1364 info
'permanent-flags
1365 (and (or (memq '%
* permanent-flags
)
1366 (memq '%Seen permanent-flags
))
1368 ;; Update marks and read articles if this isn't a
1369 ;; read-only IMAP group.
1370 (when (setq permanent-flags
1371 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1372 (if (and highestmodseq
1373 (not start-article
))
1374 ;; We've gotten the data by QRESYNCing.
1375 (nnimap-update-qresync-info
1376 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1377 ;; Do normal non-QRESYNC flag updates.
1378 ;; Update the list of read articles.
1380 (gnus-compress-sequence
1381 (gnus-set-difference
1382 (gnus-set-difference
1384 (cdr (assoc '%Seen flags
)))
1385 (cdr (assoc '%Flagged flags
)))))
1386 (read (gnus-range-difference
1387 (cons start-article high
) unread
)))
1388 (when (> start-article
1)
1391 (if (> start-article
1)
1392 (gnus-sorted-range-intersection
1393 (cons 1 (1- start-article
))
1394 (gnus-info-read info
))
1395 (gnus-info-read info
))
1397 (when (or (not (listp permanent-flags
))
1398 (memq '%Seen permanent-flags
))
1399 (gnus-info-set-read info read
))
1400 ;; Update the marks.
1401 (setq marks
(gnus-info-marks info
))
1402 (dolist (type (cdr nnimap-mark-alist
))
1403 (when (or (not (listp permanent-flags
))
1404 (memq (car (assoc (caddr type
) flags
))
1406 (memq '%
* permanent-flags
))
1407 (let ((old-marks (assoc (car type
) marks
))
1409 (gnus-compress-sequence
1410 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1411 (assoc (intern (cadr type
) obarray
) flags
)
1412 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1413 (setq marks
(delq old-marks marks
))
1415 (when (and old-marks
1416 (> start-article
1))
1417 (setq old-marks
(gnus-range-difference
1419 (cons start-article high
)))
1420 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1422 (push (cons (car type
) new-marks
) marks
)))))
1423 (gnus-info-set-marks info marks t
))))
1424 ;; Tell Gnus whether there are any \Recent messages in any of
1426 (let ((recent (cdr (assoc '%Recent flags
))))
1429 (> (car (last recent
)) (cdr active
)))
1430 (push (list (cons (gnus-group-real-name group
) 0))
1431 nnmail-split-history
)))
1432 ;; Note the active level for the next run-through.
1433 (gnus-group-set-parameter info
'active
(gnus-active group
))
1434 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1435 (gnus-group-set-parameter info
'modseq highestmodseq
)
1436 (nnimap-store-info info
(gnus-active group
)))))))
1438 (defun nnimap-update-qresync-info (info existing vanished flags
)
1439 ;; Add all the vanished articles to the list of read articles.
1444 (gnus-range-add (gnus-info-read info
)
1446 (cdr (assq '%Flagged flags
)))
1447 (cdr (assq '%Seen flags
))))
1448 (let ((marks (gnus-info-marks info
)))
1449 (dolist (type (cdr nnimap-mark-alist
))
1450 (let ((ticks (assoc (car type
) marks
))
1452 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1453 (assoc (intern (cadr type
) obarray
) flags
)
1454 (assoc (cadr type
) flags
))))) ; "\Flagged"
1455 (setq marks
(delq ticks marks
))
1457 ;; Add the new marks we got.
1458 (setq ticks
(gnus-add-to-range ticks new-marks
))
1459 ;; Remove the marks from messages that don't have them.
1460 (setq ticks
(gnus-remove-from-range
1462 (gnus-compress-sequence
1463 (gnus-sorted-complement existing new-marks
))))
1465 (push (cons (car type
) ticks
) marks
)))
1466 (gnus-info-set-marks info marks t
))))
1468 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1469 (if (zerop (length irange
))
1472 (dolist (elem (split-string irange
","))
1474 (if (string-match ":" elem
)
1475 (let ((numbers (split-string elem
":")))
1476 (cons (string-to-number (car numbers
))
1477 (string-to-number (cadr numbers
))))
1478 (string-to-number elem
))
1480 (nreverse result
))))
1482 (defun nnimap-store-info (info active
)
1483 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1484 (entry (assoc group nnimap-current-infos
)))
1486 (setcdr entry
(list info active
))
1487 (push (list group info active
) nnimap-current-infos
))))
1489 (defun nnimap-flags-to-marks (groups)
1490 (let (data group totalp uidnext articles start-article mark permanent-flags
1491 uidvalidity vanished highestmodseq
)
1492 (dolist (elem groups
)
1493 (setq group
(car elem
)
1494 uidnext
(nth 1 elem
)
1495 start-article
(nth 2 elem
)
1496 permanent-flags
(nth 3 elem
)
1497 uidvalidity
(nth 4 elem
)
1498 vanished
(nth 5 elem
)
1499 highestmodseq
(nth 6 elem
)
1500 articles
(nthcdr 7 elem
))
1501 (let ((high (caar articles
))
1503 (dolist (article articles
)
1504 (setq low
(car article
))
1505 (push (car article
) existing
)
1506 (dolist (flag (cdr article
))
1507 (setq mark
(assoc flag marks
))
1509 (push (list flag
(car article
)) marks
)
1510 (setcdr mark
(cons (car article
) (cdr mark
))))))
1511 (push (list group existing marks high low uidnext start-article
1512 permanent-flags uidvalidity vanished highestmodseq
)
1516 (defun nnimap-parse-flags (sequences)
1517 (goto-char (point-min))
1518 ;; Change \Delete etc to %Delete, so that the reader can read it.
1519 (subst-char-in-region (point-min) (point-max)
1521 ;; Remove any MODSEQ entries in the buffer, because they may contain
1522 ;; numbers that are too large for 32-bit Emacsen.
1523 (while (re-search-forward " MODSEQ ([0-9]+)" nil t
)
1524 (replace-match "" t t
))
1525 (goto-char (point-min))
1526 (let (start end articles groups uidnext elems permanent-flags
1527 uidvalidity vanished highestmodseq
)
1528 (dolist (elem sequences
)
1529 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1531 (setq start
(point))
1533 ;; The EXAMINE was successful.
1534 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1539 (setq permanent-flags
1540 (if (equal command
"SELECT")
1541 (and (search-forward "PERMANENTFLAGS "
1542 (or end
(point-min)) t
)
1543 (read (current-buffer)))
1547 (and (search-forward "UIDNEXT "
1548 (or end
(point-min)) t
)
1549 (read (current-buffer))))
1552 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1553 (or end
(point-min)) t
)
1554 ;; Store UIDVALIDITY as a string, as it's
1555 ;; too big for 32-bit Emacsen, usually.
1559 (and (eq flag-sequence
'qresync
)
1560 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1561 (or end
(point-min)) t
)
1565 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1566 (or end
(point-min)) t
)
1570 ;; The UID FETCH FLAGS was successful.
1571 (or (eq flag-sequence
'qresync
)
1572 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1573 (if (eq flag-sequence
'qresync
)
1577 (setq start
(point))
1579 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1581 (setq elems
(read (current-buffer)))
1582 (push (cons (cadr (memq 'UID elems
))
1583 (cadr (memq 'FLAGS elems
)))
1585 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1586 vanished highestmodseq
)
1590 (setq articles nil
))))
1593 (defun nnimap-find-process-buffer (buffer)
1594 (cadr (assoc buffer nnimap-connection-alist
)))
1596 (deffoo nnimap-request-post
(&optional server
)
1597 (setq nnimap-status-string
"Read-only server")
1600 (declare-function gnus-fetch-headers
"gnus-sum"
1601 (articles &optional limit force-new dependencies
))
1603 (deffoo nnimap-request-thread
(header &optional group server
)
1605 (setq group
(nnimap-decode-gnus-group group
)))
1606 (if gnus-refer-thread-use-nnir
1607 (nnir-search-thread header
)
1608 (when (nnimap-possibly-change-group group server
)
1609 (let* ((cmd (nnimap-make-thread-query header
))
1610 (result (with-current-buffer (nnimap-buffer)
1611 (nnimap-command "UID SEARCH %s" cmd
))))
1614 (and (car result
) (delete 0 (mapcar #'string-to-number
1615 (cdr (assoc "SEARCH" (cdr result
))))))
1618 (defun nnimap-possibly-change-group (group server
)
1619 (let ((open-result t
))
1621 (not (nnimap-server-opened server
)))
1622 (setq open-result
(nnimap-open-server server
)))
1629 (with-current-buffer (nnimap-buffer)
1630 (if (equal group
(nnimap-group nnimap-object
))
1632 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1634 (setf (nnimap-group nnimap-object
) group
1635 (nnimap-select-result nnimap-object
) result
)
1638 (defun nnimap-find-connection (buffer)
1639 "Find the connection delivering to BUFFER."
1640 (let ((entry (assoc buffer nnimap-connection-alist
)))
1642 (if (and (buffer-name (cadr entry
))
1643 (get-buffer-process (cadr entry
))
1644 (memq (process-status (get-buffer-process (cadr entry
)))
1646 (get-buffer-process (cadr entry
))
1647 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1650 (defvar nnimap-sequence
0)
1652 (defun nnimap-send-command (&rest args
)
1653 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1654 (process-send-string
1655 (get-buffer-process (current-buffer))
1658 (incf nnimap-sequence
)
1659 (apply #'format args
)
1660 (if (nnimap-newlinep nnimap-object
)
1663 ;; Some servers apparently can't have many outstanding
1664 ;; commands, so throttle them.
1665 (unless nnimap-streaming
1666 (nnimap-wait-for-response nnimap-sequence
))
1669 (defun nnimap-log-command (command)
1670 (with-current-buffer (get-buffer-create "*imap log*")
1671 (goto-char (point-max))
1672 (insert (format-time-string "%H:%M:%S") " "
1673 (if nnimap-inhibit-logging
1678 (defun nnimap-command (&rest args
)
1680 (let* ((sequence (apply #'nnimap-send-command args
))
1681 (response (nnimap-get-response sequence
)))
1682 (if (equal (caar response
) "OK")
1684 (nnheader-report 'nnimap
"%s"
1685 (mapconcat (lambda (a)
1687 (car response
) " "))
1690 (defun nnimap-get-response (sequence)
1691 (nnimap-wait-for-response sequence
)
1692 (nnimap-parse-response))
1694 (defun nnimap-wait-for-connection (&optional regexp
)
1695 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1697 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1698 (let ((process (get-buffer-process (current-buffer))))
1699 (goto-char (point-min))
1700 (while (and (memq (process-status process
)
1702 (not (re-search-forward regexp nil t
)))
1703 (nnheader-accept-process-output process
)
1704 (goto-char (point-min)))
1706 (and (looking-at (or response-regexp regexp
))
1709 (defun nnimap-wait-for-response (sequence &optional messagep
)
1710 (let ((process (get-buffer-process (current-buffer)))
1714 (goto-char (point-max))
1715 (while (and (setq openp
(memq (process-status process
)
1718 ;; Skip past any "*" lines that the server has
1720 (while (and (not (bobp))
1723 (looking-at "\\*"))))
1724 (not (looking-at (format "%d .*\n" sequence
)))))
1726 (nnheader-message-maybe
1727 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1728 (nnheader-accept-process-output process
)
1729 (goto-char (point-max)))
1734 ;; The user hit C-g while we were waiting: kill the process, in case
1735 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1737 (delete-process process
)
1740 (defun nnimap-parse-response ()
1741 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1743 (dolist (line lines
)
1744 (push (cdr (nnimap-parse-line line
)) result
))
1745 ;; Return the OK/error code first, and then all the "continuation
1746 ;; lines" afterwards.
1748 (nreverse result
))))
1750 ;; Parse an IMAP response line lightly. They look like
1751 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1752 ;; the lines into a list of strings and lists of string.
1753 (defun nnimap-parse-line (line)
1756 (mm-disable-multibyte)
1758 (goto-char (point-min))
1760 (if (eql (setq char
(following-char)) ?
)
1768 (if (search-forward "]" (line-end-position) 'move
)
1775 (if (search-forward ")" (line-end-position) 'move
)
1782 (1- (or (search-forward "\"" (line-end-position) 'move
)
1785 (buffer-substring (point) (if (search-forward " " nil t
)
1787 (goto-char (point-max))))))
1789 (nreverse result
))))
1791 (defun nnimap-last-response-string ()
1794 (let ((end (point)))
1798 (while (and (not (bobp))
1799 (eql (following-char) ?
*))
1801 (unless (eql (following-char) ?
*)
1803 (buffer-substring (point) end
))))
1805 (defun nnimap-get-responses (sequences)
1807 (dolist (sequence sequences
)
1808 (goto-char (point-min))
1809 (when (re-search-forward (format "^%d " sequence
) nil t
)
1810 (push (list sequence
(nnimap-parse-response))
1814 (defvar nnimap-incoming-split-list nil
)
1816 (defun nnimap-fetch-inbox (articles)
1818 (nnimap-wait-for-response
1819 (nnimap-send-command
1821 (nnimap-article-ranges articles
)
1822 (format "(UID %s%s)"
1828 (nnimap-split-download-body-default
1836 (defun nnimap-split-incoming-mail ()
1837 (with-current-buffer (nnimap-buffer)
1838 (let ((nnimap-incoming-split-list nil
)
1839 (nnmail-split-methods
1841 ((eq nnimap-split-methods
'default
)
1842 nnmail-split-methods
)
1843 (nnimap-split-methods
1844 nnimap-split-methods
)
1846 'nnmail-split-fancy
)))
1847 (nnmail-split-fancy (or nnimap-split-fancy
1848 nnmail-split-fancy
))
1849 (nnmail-inhibit-default-split-group t
)
1850 (groups (nnimap-get-groups))
1853 (nnimap-command "SELECT %S" nnimap-inbox
)
1854 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1855 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1857 (nnimap-fetch-inbox new-articles
)
1858 (nnimap-transform-split-mail)
1859 (nnheader-ms-strip-cr)
1861 (nnmail-split-incoming (current-buffer)
1862 #'nnimap-save-mail-spec
1864 #'nnimap-dummy-active-number
1865 #'nnimap-save-mail-spec
)
1866 (when nnimap-incoming-split-list
1867 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1868 sequences junk-articles
)
1869 ;; Create any groups that doesn't already exist on the
1871 (dolist (spec specs
)
1872 (when (and (not (member (car spec
) groups
))
1873 (not (eq (car spec
) 'junk
)))
1874 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1875 ;; Then copy over all the messages.
1877 (dolist (spec specs
)
1878 (let ((group (car spec
))
1879 (ranges (cdr spec
)))
1880 (if (eq group
'junk
)
1881 (setq junk-articles ranges
)
1882 (push (list (nnimap-send-command
1884 (nnimap-article-ranges ranges
)
1885 (utf7-encode group t
))
1888 ;; Wait for the last COPY response...
1890 (nnimap-wait-for-response (caar sequences
))
1891 ;; And then mark the successful copy actions as deleted,
1892 ;; and possibly expunge them.
1893 (nnimap-mark-and-expunge-incoming
1894 (nnimap-parse-copied-articles sequences
)))
1895 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1897 (defun nnimap-mark-and-expunge-incoming (range)
1899 (setq range
(nnimap-article-ranges range
))
1902 (nnimap-send-command
1903 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1905 ;; If the server supports it, we now delete the message we have
1906 ;; just copied over.
1907 ((nnimap-capability "UIDPLUS")
1908 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1909 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1910 ;; user has configured it.
1912 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1913 (nnimap-wait-for-response sequence
))))
1915 (defun nnimap-parse-copied-articles (sequences)
1916 (let (sequence copied range
)
1917 (goto-char (point-min))
1918 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t
)
1919 (setq sequence
(string-to-number (match-string 1)))
1920 (when (setq range
(cadr (assq sequence sequences
)))
1921 (push (gnus-uncompress-range range
) copied
)))
1922 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1924 (defun nnimap-new-articles (flags)
1926 (dolist (elem flags
)
1927 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1929 (push (car elem
) new
)))
1930 (gnus-compress-sequence (nreverse new
))))
1932 (defun nnimap-make-split-specs (list)
1936 (destructuring-bind (article spec
) elem
1937 (dolist (group (delete nil
(mapcar #'car spec
)))
1938 (unless (setq entry
(assoc group specs
))
1939 (push (setq entry
(list group
)) specs
))
1940 (setcdr entry
(cons article
(cdr entry
))))))
1941 (dolist (entry specs
)
1942 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
1945 (defun nnimap-transform-split-mail ()
1946 (goto-char (point-min))
1947 (let (article bytes
)
1950 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
1951 (delete-region (point) (progn (forward-line 1) (point)))
1954 (setq article
(match-string 1)
1955 bytes
(nnimap-get-length))
1956 (delete-region (line-beginning-position) (line-end-position))
1957 ;; Insert MMDF separator, and a way to remember what this
1959 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
1960 (forward-char (1+ bytes
))
1961 (setq bytes
(nnimap-get-length))
1962 (delete-region (line-beginning-position) (line-end-position))
1963 ;; There's a body; skip past that.
1965 (forward-char (1+ bytes
))
1966 (delete-region (line-beginning-position) (line-end-position)))))))
1968 (defun nnimap-dummy-active-number (group &optional server
)
1971 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
1973 (goto-char (point-min))
1974 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
1975 (error "Invalid nnimap mail")
1976 (setq article
(string-to-number (match-string 1))))
1978 (if (eq group-art
'junk
)
1979 (list (cons 'junk
1))
1981 nnimap-incoming-split-list
)))
1983 (defun nnimap-make-thread-query (header)
1984 (let* ((id (mail-header-id header
))
1986 (or (mail-header-references header
)
1990 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
1992 (dolist (refid refs value
)
1994 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
1995 refid refid value
)))))
2000 ;;; nnimap.el ends here