1 ;;; nnimap.el --- IMAP interface for Gnus
3 ;; Copyright (C) 2010-2013 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), `anonymous',
103 `login', `plain' and `cram-md5'.")
105 (defvoo nnimap-expunge t
106 "If non-nil, expunge articles after deleting them.
107 This is always done if the server supports UID EXPUNGE, but it's
108 not done by default on servers that doesn't support that command.")
110 (defvoo nnimap-streaming t
111 "If non-nil, try to use streaming commands with IMAP servers.
112 Switching this off will make nnimap slower, but it helps with
115 (defvoo nnimap-connection-alist nil
)
117 (defvoo nnimap-current-infos nil
)
119 (defvoo nnimap-fetch-partial-articles nil
120 "If non-nil, Gnus will fetch partial articles.
121 If t, Gnus will fetch only the first part. If a string, it
122 will fetch all parts that have types that match that string. A
123 likely value would be \"text/\" to automatically fetch all
126 (defvar nnimap-process nil
)
128 (defvar nnimap-status-string
"")
130 (defvar nnimap-split-download-body-default nil
131 "Internal variable with default value for `nnimap-split-download-body'.")
133 (defvar nnimap-keepalive-timer nil
)
134 (defvar nnimap-process-buffers nil
)
137 group process commands capabilities select-result newlinep server
138 last-command-time greeting examined stream-type initial-resync
)
140 (defvar nnimap-object nil
)
142 (defvar nnimap-mark-alist
143 '((read "\\Seen" %Seen
)
144 (tick "\\Flagged" %Flagged
)
145 (reply "\\Answered" %Answered
)
146 (expire "gnus-expire")
147 (dormant "gnus-dormant")
150 (download "gnus-download")
151 (forward "gnus-forward")))
153 (defvar nnimap-quirks
154 '(("QRESYNC" "Zimbra" "QRESYNC ")))
156 (defvar nnimap-inhibit-logging nil
)
158 (defun nnimap-buffer ()
159 (nnimap-find-process-buffer nntp-server-buffer
))
161 (defun nnimap-header-parameters ()
162 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
165 "BODY.PEEK[HEADER.FIELDS %s]"
166 "RFC822.HEADER.LINES %s")
167 (append '(Subject From Date Message-Id
168 References In-Reply-To Xref
)
169 nnmail-extra-headers
))))
171 (deffoo nnimap-retrieve-headers
(articles &optional group server fetch-old
)
173 (setq group
(nnimap-decode-gnus-group group
)))
174 (with-current-buffer nntp-server-buffer
176 (when (nnimap-possibly-change-group group server
)
177 (with-current-buffer (nnimap-buffer)
179 (nnimap-wait-for-response
182 (nnimap-article-ranges (gnus-compress-sequence articles
))
183 (nnimap-header-parameters))
185 (nnimap-transform-headers)
186 (nnheader-remove-cr-followed-by-lf))
187 (insert-buffer-substring
188 (nnimap-find-process-buffer (current-buffer))))
191 (defun nnimap-transform-headers ()
192 (goto-char (point-min))
193 (let (article lines size string
)
196 (while (not (looking-at "\\* [0-9]+ FETCH"))
197 (delete-region (point) (progn (forward-line 1) (point)))
200 (goto-char (match-end 0))
201 ;; Unfold quoted {number} strings.
202 (while (re-search-forward
203 "[^]][ (]{\\([0-9]+\\)}\r?\n"
205 ;; Start of the header section.
206 (or (re-search-forward "] {[0-9]+}\r?\n" nil t
)
207 ;; Start of the next FETCH.
208 (re-search-forward "\\* [0-9]+ FETCH" nil t
)
211 (setq size
(string-to-number (match-string 1)))
212 (delete-region (+ (match-beginning 0) 2) (point))
213 (setq string
(buffer-substring (point) (+ (point) size
)))
214 (delete-region (point) (+ (point) size
))
215 (insert (format "%S" (mm-subst-char-in-string ?
\n ?\s string
))))
218 (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
223 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
228 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
229 (let ((structure (ignore-errors
230 (read (current-buffer)))))
231 (while (and (consp structure
)
232 (not (atom (car structure
))))
233 (setq structure
(car structure
)))
235 (stringp (car structure
))
236 (equal (upcase (nth 0 structure
)) "MESSAGE")
237 (equal (upcase (nth 1 structure
)) "RFC822"))
239 (nth 7 structure
)))))
240 (delete-region (line-beginning-position) (line-end-position))
241 (insert (format "211 %s Article retrieved." article
))
244 (insert (format "Chars: %s\n" size
)))
246 (insert (format "Lines: %s\n" lines
)))
247 (unless (re-search-forward "^\r$" nil t
)
248 (goto-char (point-max)))
249 (delete-region (line-beginning-position) (line-end-position))
253 (defun nnimap-unfold-quoted-lines ()
254 ;; Unfold quoted {number} strings.
256 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
257 (setq size
(string-to-number (match-string 1)))
258 (delete-region (1+ (match-beginning 0)) (point))
259 (setq string
(buffer-substring (point) (+ (point) size
)))
260 (delete-region (point) (+ (point) size
))
261 (insert (format "%S" string
)))))
263 (defun nnimap-get-length ()
264 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
265 (string-to-number (match-string 1))))
267 (defun nnimap-article-ranges (ranges)
271 (number-to-string ranges
))
272 ((numberp (cdr ranges
))
273 (format "%d:%d" (car ranges
) (cdr ranges
)))
275 (dolist (elem ranges
)
278 (format "%d:%d" (car elem
) (cdr elem
))
279 (number-to-string elem
))
281 (mapconcat #'identity
(nreverse result
) ",")))))
283 (deffoo nnimap-open-server
(server &optional defs no-reconnect
)
284 (if (nnimap-server-opened server
)
286 (unless (assq 'nnimap-address defs
)
287 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
288 (nnoo-change-server 'nnimap server defs
)
290 (nnimap-find-connection nntp-server-buffer
)
291 (or (nnimap-find-connection nntp-server-buffer
)
292 (nnimap-open-connection nntp-server-buffer
)))))
294 (defun nnimap-make-process-buffer (buffer)
296 (generate-new-buffer (format " *nnimap %s %s %s*"
297 nnimap-address nnimap-server-port
298 (gnus-buffer-exists-p buffer
)))
299 (mm-disable-multibyte)
300 (buffer-disable-undo)
302 (set (make-local-variable 'after-change-functions
) nil
)
303 (set (make-local-variable 'nnimap-object
)
304 (make-nnimap :server
(nnoo-current-server 'nnimap
)
306 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
307 (push (current-buffer) nnimap-process-buffers
)
310 (defun nnimap-credentials (address ports user
)
311 (let* ((auth-source-creation-prompts
312 '((user .
"IMAP user at %h: ")
313 (secret .
"IMAP password for %u@%h: ")))
314 (found (nth 0 (auth-source-search :max
1
318 :require
'(:user
:secret
)
321 (list (plist-get found
:user
)
322 (let ((secret (plist-get found
:secret
)))
323 (if (functionp secret
)
326 (plist-get found
:save-function
))
329 (defun nnimap-keepalive ()
330 (let ((now (current-time)))
331 (dolist (buffer nnimap-process-buffers
)
332 (when (buffer-name buffer
)
333 (with-current-buffer buffer
334 (when (and nnimap-object
335 (nnimap-last-command-time nnimap-object
)
339 (nnimap-last-command-time nnimap-object
)))
340 ;; More than five minutes since the last command.
342 (ignore-errors ;E.g. "buffer foo has no process".
343 (nnimap-send-command "NOOP"))))))))
345 (defun nnimap-open-connection (buffer)
346 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
347 ;; `ssl' when nnimap-server-port was nil. Sort of.
348 (when (and nnimap-server-port
349 (eq nnimap-stream
'undecided
))
350 (setq nnimap-stream
'ssl
))
352 (if (eq nnimap-stream
'undecided
)
353 (loop for type in
'(ssl network
)
354 for stream
= (let ((nnimap-stream type
))
355 (nnimap-open-connection-1 buffer
))
356 while
(eq stream
'no-connect
)
357 finally
(return stream
))
358 (nnimap-open-connection-1 buffer
))))
359 (if (eq stream
'no-connect
)
363 (defun nnimap-map-port (port)
364 (if (equal port
"imaps")
368 (defun nnimap-open-connection-1 (buffer)
369 (unless nnimap-keepalive-timer
370 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
371 #'nnimap-keepalive
)))
372 (with-current-buffer (nnimap-make-process-buffer buffer
)
373 (let* ((coding-system-for-read 'binary
)
374 (coding-system-for-write 'binary
)
377 ((memq nnimap-stream
'(network plain starttls
))
378 (nnheader-message 7 "Opening connection to %s..."
381 ((eq nnimap-stream
'shell
)
382 (nnheader-message 7 "Opening connection to %s via shell..."
385 ((memq nnimap-stream
'(ssl tls
))
386 (nnheader-message 7 "Opening connection to %s via tls..."
388 '("imaps" "imap" "993" "143"))
390 (error "Unknown stream type: %s" nnimap-stream
))))
391 login-result credentials
)
392 (when nnimap-server-port
393 (push nnimap-server-port ports
))
395 (open-protocol-stream
396 "*nnimap*" (current-buffer) nnimap-address
397 (nnimap-map-port (car ports
))
400 :shell-command nnimap-shell-program
401 :capability-command
"1 CAPABILITY\r\n"
402 :end-of-command
"\r\n"
405 (lambda (capabilities)
406 (when (gnus-string-match-p "STARTTLS" capabilities
)
408 (stream (car stream-list
))
409 (props (cdr stream-list
))
410 (greeting (plist-get props
:greeting
))
411 (capabilities (plist-get props
:capabilities
))
412 (stream-type (plist-get props
:type
)))
413 (when (and stream
(not (memq (process-status stream
) '(open run
))))
416 (when (and (fboundp 'set-network-process-option
) ;; Not in XEmacs.
417 (fboundp 'process-type
) ;; Emacs 22 doesn't provide it.
418 (eq (process-type stream
) 'network
))
419 ;; Use TCP-keepalive so that connections that pass through a NAT
420 ;; router don't hang when left idle.
421 (set-network-process-option stream
:keepalive t
))
423 (setf (nnimap-process nnimap-object
) stream
)
424 (setf (nnimap-stream-type nnimap-object
) stream-type
)
427 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
428 nnimap-address
(car ports
) nnimap-stream
)
430 (gnus-set-process-query-on-exit-flag stream nil
)
431 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
432 (nnheader-report 'nnimap
"%s" greeting
)
433 ;; Store the greeting (for debugging purposes).
434 (setf (nnimap-greeting nnimap-object
) greeting
)
435 (setf (nnimap-capabilities nnimap-object
)
437 (split-string capabilities
)))
438 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
439 (if (not (setq credentials
440 (if (eq nnimap-authenticator
'anonymous
)
442 (message-make-address))
443 ;; Look for the credentials based on
444 ;; the virtual server name and the address
446 (gnus-delete-duplicates
449 (nnoo-current-server 'nnimap
)))
452 (setq nnimap-object nil
)
453 (let ((nnimap-inhibit-logging t
))
455 (nnimap-login (car credentials
) (cadr credentials
))))
456 (if (car login-result
)
458 ;; Save the credentials if a save function exists
459 ;; (such a function will only be passed if a new
460 ;; token was created).
461 (when (functionp (nth 2 credentials
))
462 (funcall (nth 2 credentials
)))
463 ;; See if CAPABILITY is set as part of login
465 (dolist (response (cddr login-result
))
466 (when (string= "CAPABILITY" (upcase (car response
)))
467 (setf (nnimap-capabilities nnimap-object
)
468 (mapcar #'upcase
(cdr response
))))))
469 ;; If the login failed, then forget the credentials
470 ;; that are now possibly cached.
471 (dolist (host (list (nnoo-current-server 'nnimap
)
474 (auth-source-forget+ :host host
:port port
)))
475 (delete-process (nnimap-process nnimap-object
))
476 (setq nnimap-object nil
))))
478 (when (nnimap-capability "QRESYNC")
479 (nnimap-command "ENABLE QRESYNC"))
480 (nnheader-message 7 "Opening connection to %s...done"
482 (nnimap-process nnimap-object
))))))))
484 (autoload 'rfc2104-hash
"rfc2104")
486 (defun nnimap-login (user password
)
488 ;; Prefer plain LOGIN if it's enabled (since it requires fewer
489 ;; round trips than CRAM-MD5, and it's less likely to be buggy),
490 ;; and we're using an encrypted connection.
491 ((and (not (nnimap-capability "LOGINDISABLED"))
492 (eq (nnimap-stream-type nnimap-object
) 'tls
)
493 (or (null nnimap-authenticator
)
494 (eq nnimap-authenticator
'login
)))
495 (nnimap-command "LOGIN %S %S" user password
))
496 ((and (nnimap-capability "AUTH=CRAM-MD5")
497 (or (null nnimap-authenticator
)
498 (eq nnimap-authenticator
'cram-md5
)))
500 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
501 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
503 (get-buffer-process (current-buffer))
505 (base64-encode-string
507 (rfc2104-hash 'md5
64 16 password
508 (base64-decode-string challenge
))))
510 (nnimap-wait-for-response sequence
)))
511 ((and (not (nnimap-capability "LOGINDISABLED"))
512 (or (null nnimap-authenticator
)
513 (eq nnimap-authenticator
'login
)))
514 (nnimap-command "LOGIN %S %S" user password
))
515 ((and (nnimap-capability "AUTH=PLAIN")
516 (or (null nnimap-authenticator
)
517 (eq nnimap-authenticator
'plain
)))
519 "AUTHENTICATE PLAIN %s"
520 (base64-encode-string
521 (format "\000%s\000%s"
522 (nnimap-quote-specials user
)
523 (nnimap-quote-specials password
)))))))
525 (defun nnimap-quote-specials (string)
528 (goto-char (point-min))
529 (while (re-search-forward "[\\\"]" nil t
)
535 (defun nnimap-find-parameter (parameter elems
)
539 ((equal (car elem
) parameter
)
540 (setq result
(cdr elem
)))
541 ((and (equal (car elem
) "OK")
543 (equal (caadr elem
) parameter
))
544 (setq result
(cdr (cadr elem
))))))
547 (deffoo nnimap-close-server
(&optional server
)
548 (when (nnoo-change-server 'nnimap server nil
)
550 (delete-process (get-buffer-process (nnimap-buffer))))
551 (nnoo-close-server 'nnimap server
)
554 (deffoo nnimap-request-close
()
557 (deffoo nnimap-server-opened
(&optional server
)
558 (and (nnoo-current-server-p 'nnimap server
)
560 (gnus-buffer-live-p nntp-server-buffer
)
561 (nnimap-find-connection nntp-server-buffer
)))
563 (deffoo nnimap-status-message
(&optional server
)
564 nnimap-status-string
)
566 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
568 (setq group
(nnimap-decode-gnus-group group
)))
569 (with-current-buffer nntp-server-buffer
570 (let ((result (nnimap-possibly-change-group group server
))
572 (when (stringp article
)
573 (setq article
(nnimap-find-article-by-message-id group article
)))
577 (with-current-buffer (nnimap-buffer)
579 (when nnimap-fetch-partial-articles
580 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
581 (goto-char (point-min))
582 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
583 (setq structure
(ignore-errors
584 (let ((start (point)))
586 (downcase-region start
(point))
588 (read (current-buffer))))
589 parts
(nnimap-find-wanted-parts structure
))))
591 (nnimap-get-partial-article article parts structure
)
592 (nnimap-get-whole-article article
))
593 (let ((buffer (current-buffer)))
594 (with-current-buffer (or to-buffer nntp-server-buffer
)
595 (nnheader-insert-buffer-substring buffer
)
596 (nnheader-ms-strip-cr)))
597 (cons group article
)))))))
599 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
601 (setq group
(nnimap-decode-gnus-group group
)))
602 (when (nnimap-possibly-change-group group server
)
603 (with-current-buffer (nnimap-buffer)
604 (when (stringp article
)
605 (setq article
(nnimap-find-article-by-message-id group article
)))
608 (nnimap-get-whole-article
609 article
(format "UID FETCH %%d %s"
610 (nnimap-header-parameters)))
611 (let ((buffer (current-buffer)))
612 (with-current-buffer (or to-buffer nntp-server-buffer
)
614 (insert-buffer-substring buffer
)
615 (nnheader-ms-strip-cr)
616 (cons group article
)))))))
618 (defun nnimap-get-whole-article (article &optional command
)
623 "UID FETCH %d BODY.PEEK[]"
624 "UID FETCH %d RFC822.PEEK"))
626 ;; Check that we really got an article.
627 (goto-char (point-min))
628 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
631 ;; Remove any data that may have arrived before the FETCH data.
634 (delete-region (point-min) (point)))
635 (let ((bytes (nnimap-get-length)))
636 (delete-region (line-beginning-position)
637 (progn (forward-line 1) (point)))
638 (goto-char (+ (point) bytes
))
639 (delete-region (point) (point-max)))
642 (defun nnimap-capability (capability)
643 (member capability
(nnimap-capabilities nnimap-object
)))
645 (defun nnimap-ver4-p ()
646 (nnimap-capability "IMAP4REV1"))
648 (defun nnimap-get-partial-article (article parts structure
)
651 "UID FETCH %d (%s %s)"
657 (mapconcat (lambda (part)
658 (format "BODY.PEEK[%s]" part
))
660 (mapconcat (lambda (part)
661 (format "RFC822.PEEK[%s]" part
))
664 (nnimap-convert-partial-article structure
))))
666 (defun nnimap-convert-partial-article (structure)
667 ;; First just skip past the headers.
668 (goto-char (point-min))
669 (let ((bytes (nnimap-get-length))
671 ;; Delete "FETCH" line.
672 (delete-region (line-beginning-position)
673 (progn (forward-line 1) (point)))
674 (goto-char (+ (point) bytes
))
675 ;; Collect all the body parts.
676 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
677 (setq id
(match-string 1)
678 bytes
(or (nnimap-get-length) 0))
680 (delete-region (point) (progn (forward-line 1) (point)))
681 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
683 (delete-region (point) (+ (point) bytes
)))
684 ;; Delete trailing junk.
685 (delete-region (point) (point-max))
686 ;; Now insert all the parts again where they fit in the structure.
687 (nnimap-insert-partial-structure structure parts
)
690 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
692 (let ((bstruc structure
))
693 (while (consp (car bstruc
))
695 (setq type
(car bstruc
))
696 (setq bstruc
(car (cdr bstruc
)))
697 (let ((has-boundary (member "boundary" bstruc
)))
699 (setq boundary
(cadr has-boundary
)))))
701 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
702 (downcase type
) boundary
)))
703 (while (not (stringp (car structure
)))
704 (insert "\n--" boundary
"\n")
705 (if (consp (caar structure
))
706 (nnimap-insert-partial-structure (pop structure
) parts t
)
707 (let ((bit (pop structure
)))
708 (insert (format "Content-type: %s/%s"
709 (downcase (nth 0 bit
))
710 (downcase (nth 1 bit
))))
711 (if (member-ignore-case "CHARSET" (nth 2 bit
))
714 (cadr (member-ignore-case "CHARSET" (nth 2 bit
)))))
716 (insert (format "Content-transfer-encoding: %s\n"
719 (when (assoc (nth 9 bit
) parts
)
720 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
721 (insert "\n--" boundary
"--\n")))
723 (defun nnimap-find-wanted-parts (structure)
724 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
726 (defun nnimap-find-wanted-parts-1 (structure prefix
)
729 (while (consp (car structure
))
730 (let ((sub (pop structure
)))
731 (if (consp (car sub
))
732 (push (nnimap-find-wanted-parts-1
733 sub
(if (string= prefix
"")
734 (number-to-string num
)
735 (format "%s.%s" prefix num
)))
737 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
738 (id (if (string= prefix
"")
739 (number-to-string num
)
740 (format "%s.%s" prefix num
))))
741 (setcar (nthcdr 9 sub
) id
)
742 (when (if (eq nnimap-fetch-partial-articles t
)
744 (string-match nnimap-fetch-partial-articles type
))
749 (defun nnimap-decode-gnus-group (group)
750 (decode-coding-string group
'utf-8
))
752 (deffoo nnimap-request-group
(group &optional server dont-check info
)
753 (setq group
(nnimap-decode-gnus-group group
))
754 (let ((result (nnimap-possibly-change-group
755 ;; Don't SELECT the group if we're going to select it
757 (if (and (not dont-check
)
758 (assoc group nnimap-current-infos
))
762 articles active marks high low
)
763 (with-current-buffer nntp-server-buffer
766 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
767 (insert (format "211 %d %d %d %S\n"
768 (- (cdr active
) (car active
))
772 (with-current-buffer (nnimap-buffer)
774 (let ((group-sequence
775 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
777 (nnimap-send-command "UID FETCH 1:* FLAGS")))
778 (setf (nnimap-group nnimap-object
) group
)
779 (nnimap-wait-for-response flag-sequence
)
781 (nnimap-flags-to-marks
783 (list (list group-sequence flag-sequence
784 1 group
"SELECT")))))
787 (nnimap-update-infos marks
(list info
))
788 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
789 (goto-char (point-max))
790 (let ((uidnext (nth 5 (car marks
))))
791 (setq high
(or (if uidnext
795 low
(or (nth 4 (car marks
)) uidnext
1)))))
799 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
802 (deffoo nnimap-request-create-group
(group &optional server args
)
803 (setq group
(nnimap-decode-gnus-group group
))
804 (when (nnimap-possibly-change-group nil server
)
805 (with-current-buffer (nnimap-buffer)
806 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
808 (deffoo nnimap-request-delete-group
(group &optional force server
)
809 (setq group
(nnimap-decode-gnus-group group
))
810 (when (nnimap-possibly-change-group nil server
)
811 (with-current-buffer (nnimap-buffer)
812 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
814 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
815 (setq group
(nnimap-decode-gnus-group group
))
816 (when (nnimap-possibly-change-group nil server
)
817 (with-current-buffer (nnimap-buffer)
818 (nnimap-unselect-group)
819 (car (nnimap-command "RENAME %S %S"
820 (utf7-encode group t
) (utf7-encode new-name t
))))))
822 (defun nnimap-unselect-group ()
823 ;; Make sure we don't have this group open read/write by asking
824 ;; to examine a mailbox that doesn't exist. This seems to be
825 ;; the only way that allows us to reliably go back to unselected
827 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
829 (deffoo nnimap-request-expunge-group
(group &optional server
)
830 (setq group
(nnimap-decode-gnus-group group
))
831 (when (nnimap-possibly-change-group group server
)
832 (with-current-buffer (nnimap-buffer)
833 (car (nnimap-command "EXPUNGE")))))
835 (defun nnimap-get-flags (spec)
838 (with-current-buffer (nnimap-buffer)
840 (nnimap-wait-for-response (nnimap-send-command
841 "UID FETCH %s FLAGS" spec
))
843 (subst-char-in-region (point-min) (point-max)
845 (goto-char (point-min))
846 (while (search-forward " FETCH " end t
)
847 (setq elems
(read (current-buffer)))
848 (push (cons (cadr (memq 'UID elems
))
849 (cadr (memq 'FLAGS elems
)))
851 (nreverse articles
)))
853 (deffoo nnimap-close-group
(group &optional server
)
856 (deffoo nnimap-request-move-article
(article group server accept-form
857 &optional last internal-move-group
)
858 (setq group
(nnimap-decode-gnus-group group
))
860 (mm-disable-multibyte)
861 (when (funcall (if internal-move-group
863 'nnimap-request-article
)
864 article group server
(current-buffer))
865 ;; If the move is internal (on the same server), just do it the easy
867 (let ((message-id (message-field-value "message-id")))
868 (if internal-move-group
870 (with-current-buffer (nnimap-buffer)
871 (nnimap-command "UID COPY %d %S"
873 (utf7-encode internal-move-group t
)))))
875 (nnimap-delete-article article
)
876 (cons internal-move-group
877 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
878 (nnimap-find-article-by-message-id
879 internal-move-group message-id
)))))
880 ;; Move the article to a different method.
881 (let ((result (eval accept-form
)))
883 (nnimap-possibly-change-group group server
)
884 (nnimap-delete-article article
)
887 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
888 (setq group
(nnimap-decode-gnus-group group
))
892 ((not (nnimap-possibly-change-group group server
))
895 (eq nnmail-expiry-target
'delete
))
896 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
897 (nnheader-message 7 "Article marked for deletion, but not expunged."))
900 (let ((deletable-articles
902 (eq nnmail-expiry-wait
'immediate
))
904 (gnus-sorted-intersection
906 (nnimap-find-expired-articles group
)))))
907 (if (null deletable-articles
)
909 (if (eq nnmail-expiry-target
'delete
)
910 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
911 (setq deletable-articles
912 (nnimap-process-expiry-targets
913 deletable-articles group server
)))
914 ;; Return the articles we didn't delete.
915 (gnus-sorted-complement articles deletable-articles
))))))
917 (defun nnimap-process-expiry-targets (articles group server
)
918 (let ((deleted-articles nil
))
920 ;; shortcut further processing if we're going to delete the articles
921 ((eq nnmail-expiry-target
'delete
)
922 (setq deleted-articles articles
)
924 ;; or just move them to another folder on the same IMAP server
925 ((and (not (functionp nnmail-expiry-target
))
926 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
927 (gnus-server-to-method
928 (format "nnimap:%s" server
))))
929 (and (nnimap-possibly-change-group group server
)
930 (with-current-buffer (nnimap-buffer)
931 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
934 (nnimap-article-ranges (gnus-compress-sequence articles
))
935 (utf7-encode (gnus-group-real-name nnmail-expiry-target
) t
))
936 (setq deleted-articles articles
)))
939 (dolist (article articles
)
940 (let ((target nnmail-expiry-target
))
942 (mm-disable-multibyte)
943 (when (nnimap-request-article article group server
(current-buffer))
944 (when (functionp target
)
945 (setq target
(funcall target group
)))
947 (not (eq target
'delete
)))
948 (if (or (gnus-request-group target t
)
949 (gnus-request-create-group target
))
951 (nnmail-expiry-target-group target group
)
952 (nnheader-message 7 "Expiring article %s:%d to %s"
953 group article target
))
955 (nnheader-message 7 "Expiring article %s:%d" group article
))
957 (push article deleted-articles
))))))))
958 ;; Change back to the current group again.
959 (nnimap-possibly-change-group group server
)
960 (setq deleted-articles
(nreverse deleted-articles
))
961 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
964 (defun nnimap-find-expired-articles (group)
965 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
966 (with-current-buffer (nnimap-buffer)
969 "UID SEARCH SENTBEFORE %s"
973 (car (rassoc (nth 4 (decode-time cutoff
))
974 parse-time-months
))))
977 (delete 0 (mapcar #'string-to-number
978 (cdr (assoc "SEARCH" (cdr result
))))))))))
981 (defun nnimap-find-article-by-message-id (group message-id
)
982 (with-current-buffer (nnimap-buffer)
984 (unless (or (not group
) (equal group
(nnimap-group nnimap-object
)))
985 (setf (nnimap-group nnimap-object
) nil
)
986 (setf (nnimap-examined nnimap-object
) group
)
987 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
989 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
991 (setq result
(nnimap-wait-for-response sequence
))
993 (car (setq result
(nnimap-parse-response))))
994 ;; Select the last instance of the message in the group.
996 (car (last (cdr (assoc "SEARCH" (cdr result
))))))
997 (string-to-number article
))))))
999 (defun nnimap-delete-article (articles)
1000 (with-current-buffer (nnimap-buffer)
1001 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
1002 (nnimap-article-ranges articles
))
1004 ((nnimap-capability "UIDPLUS")
1005 (nnimap-command "UID EXPUNGE %s"
1006 (nnimap-article-ranges articles
))
1009 (nnimap-command "EXPUNGE")
1011 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
1012 "server doesn't support UIDPLUS, so we won't "
1013 "delete this article now"))))))
1015 (deffoo nnimap-request-scan
(&optional group server
)
1017 (setq group
(nnimap-decode-gnus-group group
)))
1018 (when (and (nnimap-possibly-change-group nil server
)
1020 nnimap-split-methods
)
1021 (nnheader-message 7 "nnimap %s splitting mail..." server
)
1022 (nnimap-split-incoming-mail)
1023 (nnheader-message 7 "nnimap %s splitting mail...done" server
)))
1025 (defun nnimap-marks-to-flags (marks)
1027 (dolist (mark marks
)
1028 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
1032 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
1033 (setq group
(nnimap-decode-gnus-group group
))
1034 (when (nnimap-possibly-change-group nil server
)
1035 (let ((command (assoc
1037 '((subscribe "SUBSCRIBE")
1038 (unsubscribe "UNSUBSCRIBE")))))
1040 (with-current-buffer (nnimap-buffer)
1041 (nnimap-command "%s %S" (cadr command
) (utf7-encode group t
)))))))
1043 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
1044 (setq group
(nnimap-decode-gnus-group group
))
1045 (when (nnimap-possibly-change-group group server
)
1047 (with-current-buffer (nnimap-buffer)
1049 ;; Just send all the STORE commands without waiting for
1050 ;; response. If they're successful, they're successful.
1051 (dolist (action actions
)
1052 (destructuring-bind (range action marks
) action
1053 (let ((flags (nnimap-marks-to-flags marks
)))
1055 (setq sequence
(nnimap-send-command
1056 "UID STORE %s %sFLAGS.SILENT (%s)"
1057 (nnimap-article-ranges range
)
1059 ((eq action
'del
) "-")
1060 ((eq action
'add
) "+")
1061 ((eq action
'set
) ""))
1062 (mapconcat #'identity flags
" ")))))))
1063 ;; Wait for the last command to complete to avoid later
1064 ;; synchronization problems with the stream.
1066 (nnimap-wait-for-response sequence
))))))
1068 (deffoo nnimap-request-accept-article
(group &optional server last
)
1069 (setq group
(nnimap-decode-gnus-group group
))
1070 (when (nnimap-possibly-change-group nil server
)
1071 (nnmail-check-syntax)
1072 (let ((message-id (message-field-value "message-id"))
1075 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
1076 (with-current-buffer (nnimap-buffer)
1077 (when (setq message
(or (nnimap-process-quirk "OK Gimap " 'append message
)
1079 ;; If we have this group open read-only, then unselect it
1080 ;; before appending to it.
1081 (when (equal (nnimap-examined nnimap-object
) group
)
1082 (nnimap-unselect-group))
1084 (setq sequence
(nnimap-send-command
1085 "APPEND %S {%d}" (utf7-encode group t
)
1087 (unless nnimap-streaming
1088 (nnimap-wait-for-connection "^[+]"))
1089 (process-send-string (get-buffer-process (current-buffer)) message
)
1090 (process-send-string (get-buffer-process (current-buffer))
1091 (if (nnimap-newlinep nnimap-object
)
1094 (let ((result (nnimap-get-response sequence
)))
1095 (if (not (nnimap-ok-p result
))
1097 (nnheader-report 'nnimap
"%s" result
)
1100 (or (nnimap-find-uid-response "APPENDUID" (car result
))
1101 (nnimap-find-article-by-message-id
1102 group message-id
))))))))))
1104 (defun nnimap-process-quirk (greeting-match type data
)
1105 (when (and (nnimap-greeting nnimap-object
)
1106 (string-match greeting-match
(nnimap-greeting nnimap-object
))
1108 (string-match "\000" data
))
1109 (let ((choice (gnus-multiple-choice
1110 "Message contains NUL characters. Delete, continue, abort? "
1111 '((?d
"Delete NUL characters")
1112 (?c
"Try to APPEND the message as is")
1116 (nnheader-report 'nnimap
"Aborted APPEND due to NUL characters"))
1122 (goto-char (point-min))
1123 (while (search-forward "\000" nil t
)
1124 (replace-match "" t t
))
1125 (buffer-string)))))))
1127 (defun nnimap-ok-p (value)
1130 (equal (caar value
) "OK")))
1132 (defun nnimap-find-uid-response (name list
)
1133 (let ((result (car (last (nnimap-find-response-element name list
)))))
1135 (string-to-number result
))))
1137 (defun nnimap-find-response-element (name list
)
1140 (when (and (consp elem
)
1141 (equal name
(car elem
)))
1142 (setq result elem
)))
1145 (deffoo nnimap-request-replace-article
(article group buffer
)
1146 (setq group
(nnimap-decode-gnus-group group
))
1148 (when (and (nnimap-possibly-change-group group nil
)
1149 ;; Put the article into the group.
1150 (with-current-buffer buffer
1152 (nnimap-request-accept-article group nil t
))))
1153 (nnimap-delete-article (list article
))
1154 ;; Return the new article number.
1157 (defun nnimap-add-cr ()
1158 (goto-char (point-min))
1159 (while (re-search-forward "\r?\n" nil t
)
1160 (replace-match "\r\n" t t
)))
1162 (defun nnimap-get-groups ()
1164 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1166 (nnimap-wait-for-response sequence
)
1167 (subst-char-in-region (point-min) (point-max)
1169 (goto-char (point-min))
1170 (nnimap-unfold-quoted-lines)
1171 (goto-char (point-min))
1172 (while (search-forward "* LIST " nil t
)
1173 (let ((flags (read (current-buffer)))
1174 (separator (read (current-buffer)))
1175 (group (read (current-buffer))))
1176 (unless (member '%NoSelect flags
)
1177 (push (utf7-decode (if (stringp group
)
1179 (format "%s" group
)) t
)
1183 (deffoo nnimap-request-list
(&optional server
)
1184 (when (nnimap-possibly-change-group nil server
)
1185 (with-current-buffer nntp-server-buffer
1188 (with-current-buffer (nnimap-buffer)
1189 (nnimap-get-groups)))
1190 sequences responses
)
1192 (with-current-buffer (nnimap-buffer)
1193 (setf (nnimap-group nnimap-object
) nil
)
1194 (dolist (group groups
)
1195 (setf (nnimap-examined nnimap-object
) group
)
1196 (push (list (nnimap-send-command "EXAMINE %S"
1197 (utf7-encode group t
))
1200 (nnimap-wait-for-response (caar sequences
))
1202 (nnimap-get-responses (mapcar #'car sequences
))))
1203 (dolist (response responses
)
1204 (let* ((sequence (car response
))
1205 (response (cadr response
))
1206 (group (cadr (assoc sequence sequences
)))
1207 (egroup (encode-coding-string group
'utf-8
)))
1209 (equal (caar response
) "OK"))
1210 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1212 (dolist (elem response
)
1213 (when (equal (cadr elem
) "EXISTS")
1214 (setq exists
(string-to-number (car elem
)))))
1216 (setq highest
(1- (string-to-number (car uidnext
)))))
1219 (insert (format "%S 0 1 y\n" egroup
)))
1222 (insert (format "%S %d %d y\n" egroup
1223 highest
(1+ highest
))))
1225 ;; Return the widest possible range.
1226 (insert (format "%S %d 1 y\n" egroup
1227 (or highest exists
)))))))))
1230 (deffoo nnimap-request-newgroups
(date &optional server
)
1231 (when (nnimap-possibly-change-group nil server
)
1232 (with-current-buffer nntp-server-buffer
1234 (dolist (group (with-current-buffer (nnimap-buffer)
1235 (nnimap-get-groups)))
1236 (unless (assoc group nnimap-current-infos
)
1237 ;; Insert dummy numbers here -- they don't matter.
1238 (insert (format "%S 0 1 y\n" (encode-coding-string group
'utf-8
)))))
1241 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1242 (when (and (nnimap-possibly-change-group nil server
)
1244 (with-current-buffer (nnimap-buffer)
1246 (setf (nnimap-group nnimap-object
) nil
)
1247 (setf (nnimap-initial-resync nnimap-object
) 0)
1248 (let ((qresyncp (nnimap-capability "QRESYNC"))
1249 params groups sequences active uidvalidity modseq group
)
1250 ;; Go through the infos and gather the data needed to know
1251 ;; what and how to request the data.
1252 (dolist (info infos
)
1253 (setq params
(gnus-info-params info
)
1254 group
(nnimap-decode-gnus-group
1255 (gnus-group-real-name (gnus-info-group info
)))
1256 active
(cdr (assq 'active params
))
1257 uidvalidity
(cdr (assq 'uidvalidity params
))
1258 modseq
(cdr (assq 'modseq params
)))
1259 (setf (nnimap-examined nnimap-object
) group
)
1265 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1266 (utf7-encode group t
)
1267 (nnimap-quirk "QRESYNC")
1275 ;; If we don't have a UIDVALIDITY, then this is
1276 ;; the first time we've seen the group, so we
1277 ;; have to do a SELECT (which is slower than an
1278 ;; examine), but will tell us whether the group
1279 ;; is read-only or not.
1282 (if (and active uidvalidity
)
1283 ;; Fetch the last 100 flags.
1284 (setq start
(max 1 (- (cdr active
) 100)))
1285 (setf (nnimap-initial-resync nnimap-object
)
1286 (1+ (nnimap-initial-resync nnimap-object
)))
1288 (push (list (nnimap-send-command "%s %S" command
1289 (utf7-encode group t
))
1290 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1291 start group command
)
1295 (defun nnimap-quirk (command)
1296 (let ((quirk (assoc command nnimap-quirks
)))
1297 ;; If this server is of a type that matches a quirk, then return
1298 ;; the "quirked" command instead of the proper one.
1299 (if (or (null quirk
)
1300 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1304 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1305 (when (and sequences
1306 (nnimap-possibly-change-group nil server t
)
1307 ;; Check that the process is still alive.
1308 (get-buffer-process (nnimap-buffer))
1309 (memq (process-status (get-buffer-process (nnimap-buffer)))
1311 (with-current-buffer (nnimap-buffer)
1312 ;; Wait for the final data to trickle in.
1313 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1317 ;; Now we should have most of the data we need, no matter
1318 ;; whether we're QRESYNCING, fetching all the flags from
1319 ;; scratch, or just fetching the last 100 flags per group.
1320 (nnimap-update-infos (nnimap-flags-to-marks
1322 (nreverse sequences
)))
1324 ;; Finally, just return something resembling an active file in
1325 ;; the nntp buffer, so that the agent can save the info, too.
1326 (with-current-buffer nntp-server-buffer
1328 (dolist (info infos
)
1329 (let* ((group (gnus-info-group info
))
1330 (active (gnus-active group
)))
1332 (insert (format "%S %d %d y\n"
1333 (decode-coding-string
1334 (gnus-group-real-name group
) 'utf-8
)
1336 (car active
)))))))))))
1338 (defun nnimap-update-infos (flags infos
)
1339 (dolist (info infos
)
1340 (let* ((group (nnimap-decode-gnus-group
1341 (gnus-group-real-name (gnus-info-group info
))))
1342 (marks (cdr (assoc group flags
))))
1344 (nnimap-update-info info marks
)))))
1346 (defun nnimap-update-info (info marks
)
1347 (destructuring-bind (existing flags high low uidnext start-article
1348 permanent-flags uidvalidity
1349 vanished highestmodseq
) marks
1351 ;; Ignore groups with no UIDNEXT/marks. This happens for
1352 ;; completely empty groups.
1353 ((and (not existing
)
1355 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1357 (gnus-set-active (gnus-info-group info
) active
))))
1358 ;; We have a mismatch between the old and new UIDVALIDITY
1359 ;; identifiers, so we have to re-request the group info (the next
1360 ;; time). This virtually never happens.
1361 ((let ((old-uidvalidity
1362 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1363 (and old-uidvalidity
1364 (not (equal old-uidvalidity uidvalidity
))
1365 (or (not start-article
)
1366 (> start-article
1))))
1367 (gnus-group-remove-parameter info
'uidvalidity
)
1368 (gnus-group-remove-parameter info
'modseq
))
1369 ;; We have the data needed to update.
1371 (let* ((group (gnus-info-group info
))
1372 (completep (and start-article
1373 (= start-article
1)))
1374 (active (or (gnus-active group
)
1375 (cdr (assq 'active
(gnus-info-params info
))))))
1377 (setq high
(1- uidnext
)))
1378 ;; First set the active ranges based on high/low.
1380 (not (gnus-active group
)))
1381 (gnus-set-active group
1384 (cons (min (or low
(car active
))
1386 (max (or high
(cdr active
))
1391 ;; No articles in this group.
1392 (cons uidnext
(1- uidnext
)))
1394 (cons start-article
(1- start-article
)))
1396 ;; No articles and no uidnext.
1398 (gnus-set-active group
1400 (or high
(1- uidnext
)))))
1401 ;; See whether this is a read-only group.
1402 (unless (eq permanent-flags
'not-scanned
)
1403 (gnus-group-set-parameter
1404 info
'permanent-flags
1405 (and (or (memq '%
* permanent-flags
)
1406 (memq '%Seen permanent-flags
))
1408 ;; Update marks and read articles if this isn't a
1409 ;; read-only IMAP group.
1410 (when (setq permanent-flags
1411 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1412 (if (and highestmodseq
1413 (not start-article
))
1414 ;; We've gotten the data by QRESYNCing.
1415 (nnimap-update-qresync-info
1416 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1417 ;; Do normal non-QRESYNC flag updates.
1418 ;; Update the list of read articles.
1420 (gnus-compress-sequence
1421 (gnus-set-difference
1422 (gnus-set-difference
1425 (cdr (assoc '%Seen flags
))
1426 (cdr (assoc '%Deleted flags
))))
1427 (cdr (assoc '%Flagged flags
)))))
1428 (read (gnus-range-difference
1429 (cons start-article high
) unread
)))
1430 (when (> start-article
1)
1433 (if (> start-article
1)
1434 (gnus-sorted-range-intersection
1435 (cons 1 (1- start-article
))
1436 (gnus-info-read info
))
1437 (gnus-info-read info
))
1439 (when (or (not (listp permanent-flags
))
1440 (memq '%Seen permanent-flags
))
1441 (gnus-info-set-read info read
))
1442 ;; Update the marks.
1443 (setq marks
(gnus-info-marks info
))
1444 (dolist (type (cdr nnimap-mark-alist
))
1445 (when (or (not (listp permanent-flags
))
1446 (memq (car (assoc (caddr type
) flags
))
1448 (memq '%
* permanent-flags
))
1449 (let ((old-marks (assoc (car type
) marks
))
1451 (gnus-compress-sequence
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 old-marks marks
))
1457 (when (and old-marks
1458 (> start-article
1))
1459 (setq old-marks
(gnus-range-difference
1461 (cons start-article high
)))
1462 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1464 (push (cons (car type
) new-marks
) marks
)))))
1465 (gnus-info-set-marks info marks t
))))
1466 ;; Tell Gnus whether there are any \Recent messages in any of
1468 (let ((recent (cdr (assoc '%Recent flags
))))
1471 (> (car (last recent
)) (cdr active
)))
1472 (push (list (cons (gnus-group-real-name group
) 0))
1473 nnmail-split-history
)))
1474 ;; Note the active level for the next run-through.
1475 (gnus-group-set-parameter info
'active
(gnus-active group
))
1476 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1477 (gnus-group-set-parameter info
'modseq highestmodseq
)
1478 (nnimap-store-info info
(gnus-active group
)))))))
1480 (defun nnimap-update-qresync-info (info existing vanished flags
)
1481 ;; Add all the vanished articles to the list of read articles.
1486 (gnus-range-add (gnus-info-read info
)
1488 (cdr (assq '%Flagged flags
)))
1489 (cdr (assq '%Seen flags
))))
1490 (let ((marks (gnus-info-marks info
)))
1491 (dolist (type (cdr nnimap-mark-alist
))
1492 (let ((ticks (assoc (car type
) marks
))
1494 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1495 (assoc (intern (cadr type
) obarray
) flags
)
1496 (assoc (cadr type
) flags
))))) ; "\Flagged"
1497 (setq marks
(delq ticks marks
))
1499 ;; Add the new marks we got.
1500 (setq ticks
(gnus-add-to-range ticks new-marks
))
1501 ;; Remove the marks from messages that don't have them.
1502 (setq ticks
(gnus-remove-from-range
1504 (gnus-compress-sequence
1505 (gnus-sorted-complement existing new-marks
))))
1507 (push (cons (car type
) ticks
) marks
)))
1508 (gnus-info-set-marks info marks t
))))
1510 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1511 (if (zerop (length irange
))
1514 (dolist (elem (split-string irange
","))
1516 (if (string-match ":" elem
)
1517 (let ((numbers (split-string elem
":")))
1518 (cons (string-to-number (car numbers
))
1519 (string-to-number (cadr numbers
))))
1520 (string-to-number elem
))
1522 (nreverse result
))))
1524 (defun nnimap-store-info (info active
)
1525 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1526 (entry (assoc group nnimap-current-infos
)))
1528 (setcdr entry
(list info active
))
1529 (push (list group info active
) nnimap-current-infos
))))
1531 (defun nnimap-flags-to-marks (groups)
1532 (let (data group totalp uidnext articles start-article mark permanent-flags
1533 uidvalidity vanished highestmodseq
)
1534 (dolist (elem groups
)
1535 (setq group
(car elem
)
1536 uidnext
(nth 1 elem
)
1537 start-article
(nth 2 elem
)
1538 permanent-flags
(nth 3 elem
)
1539 uidvalidity
(nth 4 elem
)
1540 vanished
(nth 5 elem
)
1541 highestmodseq
(nth 6 elem
)
1542 articles
(nthcdr 7 elem
))
1543 (let ((high (caar articles
))
1545 (dolist (article articles
)
1546 (setq low
(car article
))
1547 (push (car article
) existing
)
1548 (dolist (flag (cdr article
))
1549 (setq mark
(assoc flag marks
))
1551 (push (list flag
(car article
)) marks
)
1552 (setcdr mark
(cons (car article
) (cdr mark
))))))
1553 (push (list group existing marks high low uidnext start-article
1554 permanent-flags uidvalidity vanished highestmodseq
)
1558 (defun nnimap-parse-flags (sequences)
1559 (goto-char (point-min))
1560 ;; Change \Delete etc to %Delete, so that the Emacs Lisp reader can
1562 (subst-char-in-region (point-min) (point-max)
1564 ;; Remove any MODSEQ entries in the buffer, because they may contain
1565 ;; numbers that are too large for 32-bit Emacsen.
1566 (while (re-search-forward " MODSEQ ([0-9]+)" nil t
)
1567 (replace-match "" t t
))
1568 (goto-char (point-min))
1569 (let (start end articles groups uidnext elems permanent-flags
1570 uidvalidity vanished highestmodseq
)
1571 (dolist (elem sequences
)
1572 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1574 (setq start
(point))
1576 ;; The EXAMINE was successful.
1577 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1582 (setq permanent-flags
1583 (if (equal command
"SELECT")
1584 (and (search-forward "PERMANENTFLAGS "
1585 (or end
(point-min)) t
)
1586 (read (current-buffer)))
1590 (and (search-forward "UIDNEXT "
1591 (or end
(point-min)) t
)
1592 (read (current-buffer))))
1595 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1596 (or end
(point-min)) t
)
1597 ;; Store UIDVALIDITY as a string, as it's
1598 ;; too big for 32-bit Emacsen, usually.
1602 (and (eq flag-sequence
'qresync
)
1603 (re-search-forward "^\\* VANISHED .*? \\([0-9:,]+\\)"
1604 (or end
(point-min)) t
)
1608 (and (re-search-forward "HIGHESTMODSEQ \\([0-9]+\\)"
1609 (or end
(point-min)) t
)
1613 ;; The UID FETCH FLAGS was successful.
1614 (or (eq flag-sequence
'qresync
)
1615 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1616 (if (eq flag-sequence
'qresync
)
1620 (setq start
(point))
1622 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1624 (setq elems
(read (current-buffer)))
1625 (push (cons (cadr (memq 'UID elems
))
1626 (cadr (memq 'FLAGS elems
)))
1628 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1629 vanished highestmodseq
)
1632 (if (eq flag-sequence
'qresync
)
1635 (setq articles nil
))))
1638 (defun nnimap-find-process-buffer (buffer)
1639 (cadr (assoc buffer nnimap-connection-alist
)))
1641 (deffoo nnimap-request-post
(&optional server
)
1642 (setq nnimap-status-string
"Read-only server")
1645 (declare-function gnus-fetch-headers
"gnus-sum"
1646 (articles &optional limit force-new dependencies
))
1648 (autoload 'nnir-search-thread
"nnir")
1650 (deffoo nnimap-request-thread
(header &optional group server
)
1652 (setq group
(nnimap-decode-gnus-group group
)))
1653 (if gnus-refer-thread-use-nnir
1654 (nnir-search-thread header
)
1655 (when (nnimap-possibly-change-group group server
)
1656 (let* ((cmd (nnimap-make-thread-query header
))
1657 (result (with-current-buffer (nnimap-buffer)
1658 (nnimap-command "UID SEARCH %s" cmd
))))
1662 (delete 0 (mapcar #'string-to-number
1663 (cdr (assoc "SEARCH" (cdr result
))))))
1666 (defun nnimap-possibly-change-group (group server
&optional no-reconnect
)
1667 (let ((open-result t
))
1669 (not (nnimap-server-opened server
)))
1670 (setq open-result
(nnimap-open-server server nil no-reconnect
)))
1677 (with-current-buffer (nnimap-buffer)
1678 (if (equal group
(nnimap-group nnimap-object
))
1680 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1682 (setf (nnimap-group nnimap-object
) group
1683 (nnimap-select-result nnimap-object
) result
)
1686 (defun nnimap-find-connection (buffer)
1687 "Find the connection delivering to BUFFER."
1688 (let ((entry (assoc buffer nnimap-connection-alist
)))
1690 (if (and (buffer-name (cadr entry
))
1691 (get-buffer-process (cadr entry
))
1692 (memq (process-status (get-buffer-process (cadr entry
)))
1694 (get-buffer-process (cadr entry
))
1695 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1698 (defvar nnimap-sequence
0)
1700 (defun nnimap-send-command (&rest args
)
1701 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1702 (process-send-string
1703 (get-buffer-process (current-buffer))
1706 (incf nnimap-sequence
)
1707 (apply #'format args
)
1708 (if (nnimap-newlinep nnimap-object
)
1711 ;; Some servers apparently can't have many outstanding
1712 ;; commands, so throttle them.
1713 (unless nnimap-streaming
1714 (nnimap-wait-for-response nnimap-sequence
))
1717 (defvar nnimap-record-commands nil
1718 "If non-nil, log commands to the \"*imap log*\" buffer.")
1720 (defun nnimap-log-command (command)
1721 (when nnimap-record-commands
1722 (with-current-buffer (get-buffer-create "*imap log*")
1723 (goto-char (point-max))
1724 (insert (format-time-string "%H:%M:%S")
1725 " [" nnimap-address
"] "
1726 (if nnimap-inhibit-logging
1731 (defun nnimap-command (&rest args
)
1733 (let* ((sequence (apply #'nnimap-send-command args
))
1734 (response (nnimap-get-response sequence
)))
1735 (if (equal (caar response
) "OK")
1737 (nnheader-report 'nnimap
"%s"
1738 (mapconcat (lambda (a)
1740 (car response
) " "))
1743 (defun nnimap-get-response (sequence)
1744 (nnimap-wait-for-response sequence
)
1745 (nnimap-parse-response))
1747 (defun nnimap-wait-for-connection (&optional regexp
)
1748 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1750 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1751 (let ((process (get-buffer-process (current-buffer))))
1752 (goto-char (point-min))
1753 (while (and (memq (process-status process
)
1755 (not (re-search-forward regexp nil t
)))
1756 (nnheader-accept-process-output process
)
1757 (goto-char (point-min)))
1759 (and (looking-at (or response-regexp regexp
))
1762 (defun nnimap-wait-for-response (sequence &optional messagep
)
1763 (let ((process (get-buffer-process (current-buffer)))
1767 (goto-char (point-max))
1768 (while (and (setq openp
(memq (process-status process
)
1771 ;; Skip past any "*" lines that the server has
1773 (while (and (not (bobp))
1776 (looking-at "\\*"))))
1777 (not (looking-at (format "%d .*\n" sequence
)))))
1779 (nnheader-message-maybe
1780 7 "nnimap read %dk from %s%s" (/ (buffer-size) 1000)
1782 (if (not (zerop (nnimap-initial-resync nnimap-object
)))
1783 (format " (initial sync of %d group%s; please wait)"
1784 (nnimap-initial-resync nnimap-object
)
1785 (if (= (nnimap-initial-resync nnimap-object
) 1)
1789 (nnheader-accept-process-output process
)
1790 (goto-char (point-max)))
1791 (setf (nnimap-initial-resync nnimap-object
) 0)
1796 ;; The user hit C-g while we were waiting: kill the process, in case
1797 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1799 (delete-process process
)
1802 (defun nnimap-parse-response ()
1803 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1805 (dolist (line lines
)
1806 (push (cdr (nnimap-parse-line line
)) result
))
1807 ;; Return the OK/error code first, and then all the "continuation
1808 ;; lines" afterwards.
1810 (nreverse result
))))
1812 ;; Parse an IMAP response line lightly. They look like
1813 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1814 ;; the lines into a list of strings and lists of string.
1815 (defun nnimap-parse-line (line)
1818 (mm-disable-multibyte)
1820 (goto-char (point-min))
1822 (if (eql (setq char
(following-char)) ?
)
1830 (if (search-forward "]" (line-end-position) 'move
)
1837 (if (search-forward ")" (line-end-position) 'move
)
1844 (1- (or (search-forward "\"" (line-end-position) 'move
)
1847 (buffer-substring (point) (if (search-forward " " nil t
)
1849 (goto-char (point-max))))))
1851 (nreverse result
))))
1853 (defun nnimap-last-response-string ()
1856 (let ((end (point)))
1860 (while (and (not (bobp))
1861 (eql (following-char) ?
*))
1863 (unless (eql (following-char) ?
*)
1865 (buffer-substring (point) end
))))
1867 (defun nnimap-get-responses (sequences)
1869 (dolist (sequence sequences
)
1870 (goto-char (point-min))
1871 (when (re-search-forward (format "^%d " sequence
) nil t
)
1872 (push (list sequence
(nnimap-parse-response))
1876 (defvar nnimap-incoming-split-list nil
)
1878 (defun nnimap-fetch-inbox (articles)
1880 (nnimap-wait-for-response
1881 (nnimap-send-command
1883 (nnimap-article-ranges articles
)
1884 (format "(UID %s%s)"
1890 (nnimap-split-download-body-default
1898 (defun nnimap-split-incoming-mail ()
1899 (with-current-buffer (nnimap-buffer)
1900 (let ((nnimap-incoming-split-list nil
)
1901 (nnmail-split-methods
1903 ((eq nnimap-split-methods
'default
)
1904 nnmail-split-methods
)
1905 (nnimap-split-methods
1906 nnimap-split-methods
)
1908 'nnmail-split-fancy
)))
1909 (nnmail-split-fancy (or nnimap-split-fancy
1910 nnmail-split-fancy
))
1911 (nnmail-inhibit-default-split-group t
)
1912 (groups (nnimap-get-groups))
1915 (nnimap-command "SELECT %S" nnimap-inbox
)
1916 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1917 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1919 (nnimap-fetch-inbox new-articles
)
1920 (nnimap-transform-split-mail)
1921 (nnheader-ms-strip-cr)
1923 (nnmail-split-incoming (current-buffer)
1924 #'nnimap-save-mail-spec
1926 #'nnimap-dummy-active-number
1927 #'nnimap-save-mail-spec
)
1928 (when nnimap-incoming-split-list
1929 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1930 sequences junk-articles
)
1931 ;; Create any groups that doesn't already exist on the
1933 (dolist (spec specs
)
1934 (when (and (not (member (car spec
) groups
))
1935 (not (eq (car spec
) 'junk
)))
1936 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1937 ;; Then copy over all the messages.
1939 (dolist (spec specs
)
1940 (let ((group (car spec
))
1941 (ranges (cdr spec
)))
1942 (if (eq group
'junk
)
1943 (setq junk-articles ranges
)
1944 (push (list (nnimap-send-command
1946 (nnimap-article-ranges ranges
)
1947 (utf7-encode group t
))
1950 ;; Wait for the last COPY response...
1952 (nnimap-wait-for-response (caar sequences
))
1953 ;; And then mark the successful copy actions as deleted,
1954 ;; and possibly expunge them.
1955 (nnimap-mark-and-expunge-incoming
1956 (nnimap-parse-copied-articles sequences
)))
1957 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1959 (defun nnimap-mark-and-expunge-incoming (range)
1961 (setq range
(nnimap-article-ranges range
))
1964 (nnimap-send-command
1965 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1967 ;; If the server supports it, we now delete the message we have
1968 ;; just copied over.
1969 ((nnimap-capability "UIDPLUS")
1970 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1971 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1972 ;; user has configured it.
1974 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1975 (nnimap-wait-for-response sequence
))))
1977 (defun nnimap-parse-copied-articles (sequences)
1978 (let (sequence copied range
)
1979 (goto-char (point-min))
1980 (while (re-search-forward "^\\([0-9]+\\) OK\\b" nil t
)
1981 (setq sequence
(string-to-number (match-string 1)))
1982 (when (setq range
(cadr (assq sequence sequences
)))
1983 (push (gnus-uncompress-range range
) copied
)))
1984 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1986 (defun nnimap-new-articles (flags)
1988 (dolist (elem flags
)
1989 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1991 (push (car elem
) new
)))
1992 (gnus-compress-sequence (nreverse new
))))
1994 (defun nnimap-make-split-specs (list)
1998 (destructuring-bind (article spec
) elem
1999 (dolist (group (delete nil
(mapcar #'car spec
)))
2000 (unless (setq entry
(assoc group specs
))
2001 (push (setq entry
(list group
)) specs
))
2002 (setcdr entry
(cons article
(cdr entry
))))))
2003 (dolist (entry specs
)
2004 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
2007 (defun nnimap-transform-split-mail ()
2008 (goto-char (point-min))
2009 (let (article bytes
)
2012 (while (not (looking-at "\\* [0-9]+ FETCH.+UID \\([0-9]+\\)"))
2013 (delete-region (point) (progn (forward-line 1) (point)))
2016 (setq article
(match-string 1)
2017 bytes
(nnimap-get-length))
2018 (delete-region (line-beginning-position) (line-end-position))
2019 ;; Insert MMDF separator, and a way to remember what this
2021 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
2022 (forward-char (1+ bytes
))
2023 (setq bytes
(nnimap-get-length))
2024 (delete-region (line-beginning-position) (line-end-position))
2025 ;; There's a body; skip past that.
2027 (forward-char (1+ bytes
))
2028 (delete-region (line-beginning-position) (line-end-position)))))))
2030 (defun nnimap-dummy-active-number (group &optional server
)
2033 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
2035 (goto-char (point-min))
2036 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
2037 (error "Invalid nnimap mail")
2038 (setq article
(string-to-number (match-string 1))))
2040 (if (eq group-art
'junk
)
2041 (list (cons 'junk
1))
2043 nnimap-incoming-split-list
)))
2045 (defun nnimap-make-thread-query (header)
2046 (let* ((id (mail-header-id header
))
2048 (or (mail-header-references header
)
2052 "(OR HEADER REFERENCES %S HEADER Message-Id %S)"
2054 (dolist (refid refs value
)
2056 "(OR (OR HEADER Message-Id %S HEADER REFERENCES %S) %s)"
2057 refid refid value
)))))
2062 ;;; nnimap.el ends here