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
))))
48 (require 'proto-stream
)
50 (autoload 'auth-source-forget-user-or-password
"auth-source")
51 (autoload 'auth-source-user-or-password
"auth-source")
55 (defvoo nnimap-address nil
56 "The address of the IMAP server.")
58 (defvoo nnimap-server-port nil
60 If nnimap-stream is `ssl', this will default to `imaps'. If not,
61 it will default to `imap'.")
63 (defvoo nnimap-stream
'undecided
64 "How nnimap will talk to the IMAP server.
65 Values are `ssl', `network', `starttls' or `shell'.
66 The default is to try `ssl' first, and then `network'.")
68 (defvoo nnimap-shell-program
(if (boundp 'imap-shell-program
)
69 (if (listp imap-shell-program
)
70 (car imap-shell-program
)
74 (defvoo nnimap-inbox nil
75 "The mail box where incoming mail arrives and should be split out of.")
77 (defvoo nnimap-split-methods nil
79 Uses the same syntax as nnmail-split-methods")
81 (defvoo nnimap-split-fancy nil
82 "Uses the same syntax as nnmail-split-fancy.")
84 (defvoo nnimap-unsplittable-articles
'(%Deleted %Seen
)
85 "Articles with the flags in the list will not be considered when splitting.")
87 (make-obsolete-variable 'nnimap-split-rule
"see `nnimap-split-methods'"
90 (defvoo nnimap-authenticator nil
91 "How nnimap authenticate itself to the server.
92 Possible choices are nil (use default methods) or `anonymous'.")
94 (defvoo nnimap-expunge t
95 "If non-nil, expunge articles after deleting them.
96 This is always done if the server supports UID EXPUNGE, but it's
97 not done by default on servers that doesn't support that command.")
99 (defvoo nnimap-streaming t
100 "If non-nil, try to use streaming commands with IMAP servers.
101 Switching this off will make nnimap slower, but it helps with
104 (defvoo nnimap-connection-alist nil
)
106 (defvoo nnimap-current-infos nil
)
108 (defvoo nnimap-fetch-partial-articles nil
109 "If non-nil, Gnus will fetch partial articles.
110 If t, nnimap will fetch only the first part. If a string, it
111 will fetch all parts that have types that match that string. A
112 likely value would be \"text/\" to automatically fetch all
115 (defvar nnimap-process nil
)
117 (defvar nnimap-status-string
"")
119 (defvar nnimap-split-download-body-default nil
120 "Internal variable with default value for `nnimap-split-download-body'.")
122 (defvar nnimap-keepalive-timer nil
)
123 (defvar nnimap-process-buffers nil
)
126 group process commands capabilities select-result newlinep server
127 last-command-time greeting examined
)
129 (defvar nnimap-object nil
)
131 (defvar nnimap-mark-alist
132 '((read "\\Seen" %Seen
)
133 (tick "\\Flagged" %Flagged
)
134 (reply "\\Answered" %Answered
)
135 (expire "gnus-expire")
136 (dormant "gnus-dormant")
139 (download "gnus-download")
140 (forward "gnus-forward")))
142 (defvar nnimap-quirks
143 '(("QRESYNC" "Zimbra" "QRESYNC ")))
145 (defun nnimap-buffer ()
146 (nnimap-find-process-buffer nntp-server-buffer
))
148 (defun nnimap-header-parameters ()
149 (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
152 "BODY.PEEK[HEADER.FIELDS %s]"
153 "RFC822.HEADER.LINES %s")
154 (append '(Subject From Date Message-Id
155 References In-Reply-To Xref
)
156 nnmail-extra-headers
))))
158 (deffoo nnimap-retrieve-headers
(articles &optional group server fetch-old
)
159 (with-current-buffer nntp-server-buffer
161 (when (nnimap-possibly-change-group group server
)
162 (with-current-buffer (nnimap-buffer)
164 (nnimap-wait-for-response
167 (nnimap-article-ranges (gnus-compress-sequence articles
))
168 (nnimap-header-parameters))
170 (nnimap-transform-headers)
171 (nnheader-remove-cr-followed-by-lf))
172 (insert-buffer-substring
173 (nnimap-find-process-buffer (current-buffer))))
176 (defun nnimap-transform-headers ()
177 (goto-char (point-min))
178 (let (article bytes lines size string
)
181 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
182 (delete-region (point) (progn (forward-line 1) (point)))
185 (setq article
(match-string 1))
186 ;; Unfold quoted {number} strings.
187 (while (re-search-forward "[^]][ (]{\\([0-9]+\\)}\r?\n"
188 (1+ (line-end-position)) t
)
189 (setq size
(string-to-number (match-string 1)))
190 (delete-region (+ (match-beginning 0) 2) (point))
191 (setq string
(buffer-substring (point) (+ (point) size
)))
192 (delete-region (point) (+ (point) size
))
193 (insert (format "%S" string
)))
194 (setq bytes
(nnimap-get-length)
198 (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
203 (when (search-forward "BODYSTRUCTURE" (line-end-position) t
)
204 (let ((structure (ignore-errors
205 (read (current-buffer)))))
206 (while (and (consp structure
)
207 (not (stringp (car structure
))))
208 (setq structure
(car structure
)))
209 (setq lines
(nth 7 structure
))))
210 (delete-region (line-beginning-position) (line-end-position))
211 (insert (format "211 %s Article retrieved." article
))
214 (insert (format "Chars: %s\n" size
)))
216 (insert (format "Lines: %s\n" lines
)))
217 (unless (re-search-forward "^\r$" nil t
)
218 (goto-char (point-max)))
219 (delete-region (line-beginning-position) (line-end-position))
223 (defun nnimap-unfold-quoted-lines ()
224 ;; Unfold quoted {number} strings.
226 (while (re-search-forward " {\\([0-9]+\\)}\r?\n" nil t
)
227 (setq size
(string-to-number (match-string 1)))
228 (delete-region (1+ (match-beginning 0)) (point))
229 (setq string
(buffer-substring (point) (+ (point) size
)))
230 (delete-region (point) (+ (point) size
))
231 (insert (format "%S" string
)))))
233 (defun nnimap-get-length ()
234 (and (re-search-forward "{\\([0-9]+\\)}" (line-end-position) t
)
235 (string-to-number (match-string 1))))
237 (defun nnimap-article-ranges (ranges)
241 (number-to-string ranges
))
242 ((numberp (cdr ranges
))
243 (format "%d:%d" (car ranges
) (cdr ranges
)))
245 (dolist (elem ranges
)
248 (format "%d:%d" (car elem
) (cdr elem
))
249 (number-to-string elem
))
251 (mapconcat #'identity
(nreverse result
) ",")))))
253 (deffoo nnimap-open-server
(server &optional defs
)
254 (if (nnimap-server-opened server
)
256 (unless (assq 'nnimap-address defs
)
257 (setq defs
(append defs
(list (list 'nnimap-address server
)))))
258 (nnoo-change-server 'nnimap server defs
)
259 (or (nnimap-find-connection nntp-server-buffer
)
260 (nnimap-open-connection nntp-server-buffer
))))
262 (defun nnimap-make-process-buffer (buffer)
264 (generate-new-buffer (format "*nnimap %s %s %s*"
265 nnimap-address nnimap-server-port
266 (gnus-buffer-exists-p buffer
)))
267 (mm-disable-multibyte)
268 (buffer-disable-undo)
270 (set (make-local-variable 'after-change-functions
) nil
)
271 (set (make-local-variable 'nnimap-object
)
272 (make-nnimap :server
(nnoo-current-server 'nnimap
)))
273 (push (list buffer
(current-buffer)) nnimap-connection-alist
)
274 (push (current-buffer) nnimap-process-buffers
)
277 (defun nnimap-credentials (address ports
&optional inhibit-create
)
278 (let (port credentials
)
279 ;; Request the credentials from all ports, but only query on the
280 ;; last port if all the previous ones have failed.
281 (while (and (null credentials
)
282 (setq port
(pop ports
)))
284 (auth-source-user-or-password
285 '("login" "password") address port nil
291 (defun nnimap-keepalive ()
292 (let ((now (current-time)))
293 (dolist (buffer nnimap-process-buffers
)
294 (when (buffer-name buffer
)
295 (with-current-buffer buffer
296 (when (and nnimap-object
297 (nnimap-last-command-time nnimap-object
)
301 (nnimap-last-command-time nnimap-object
)))
302 ;; More than five minutes since the last command.
304 (nnimap-send-command "NOOP")))))))
306 (defun nnimap-open-connection (buffer)
307 ;; Be backwards-compatible -- the earlier value of nnimap-stream was
308 ;; `ssl' when nnimap-server-port was nil. Sort of.
309 (when (and nnimap-server-port
310 (eq nnimap-stream
'undecided
))
311 (setq nnimap-stream
'ssl
))
313 (if (eq nnimap-stream
'undecided
)
314 (loop for type in
'(ssl network
)
315 for stream
= (let ((nnimap-stream type
))
316 (nnimap-open-connection-1 buffer
))
317 while
(eq stream
'no-connect
)
318 finally
(return stream
))
319 (nnimap-open-connection-1 buffer
))))
320 (if (eq stream
'no-connect
)
324 (defun nnimap-open-connection-1 (buffer)
325 (unless nnimap-keepalive-timer
326 (setq nnimap-keepalive-timer
(run-at-time (* 60 15) (* 60 15)
328 (with-current-buffer (nnimap-make-process-buffer buffer
)
329 (let* ((coding-system-for-read 'binary
)
330 (coding-system-for-write 'binary
)
334 ((or (eq nnimap-stream
'network
)
335 (eq nnimap-stream
'starttls
))
336 (nnheader-message 7 "Opening connection to %s..."
339 ((eq nnimap-stream
'shell
)
340 (nnheader-message 7 "Opening connection to %s via shell..."
343 ((memq nnimap-stream
'(ssl tls
))
344 (nnheader-message 7 "Opening connection to %s via tls..."
346 '("143" "993" "imap" "imaps"))
348 (error "Unknown stream type: %s" nnimap-stream
))))
349 (proto-stream-always-use-starttls t
)
350 login-result credentials
)
351 (when nnimap-server-port
352 (setq ports
(append ports
(list nnimap-server-port
))))
353 (destructuring-bind (stream greeting capabilities
)
354 (open-protocol-stream
355 "*nnimap*" (current-buffer) nnimap-address
(car (last ports
))
357 :shell-command nnimap-shell-program
358 :capability-command
"1 CAPABILITY\r\n"
361 (lambda (capabilities)
362 (when (gnus-string-match-p "STARTTLS" capabilities
)
364 (setf (nnimap-process nnimap-object
) stream
)
367 (nnheader-report 'nnimap
"Unable to contact %s:%s via %s"
368 nnimap-address port nnimap-stream
)
370 (gnus-set-process-query-on-exit-flag stream nil
)
371 (if (not (gnus-string-match-p "[*.] \\(OK\\|PREAUTH\\)" greeting
))
372 (nnheader-report 'nnimap
"%s" greeting
)
373 ;; Store the greeting (for debugging purposes).
374 (setf (nnimap-greeting nnimap-object
) greeting
)
375 (setf (nnimap-capabilities nnimap-object
)
377 (split-string capabilities
)))
378 (unless (gnus-string-match-p "[*.] PREAUTH" greeting
)
379 (if (not (setq credentials
380 (if (eq nnimap-authenticator
'anonymous
)
382 (message-make-address))
384 ;; First look for the credentials based
385 ;; on the virtual server name.
387 (nnoo-current-server 'nnimap
) ports t
)
388 ;; Then look them up based on the
390 (nnimap-credentials nnimap-address ports
)))))
391 (setq nnimap-object nil
)
393 (nnimap-login (car credentials
) (cadr credentials
)))
394 (unless (car login-result
)
395 ;; If the login failed, then forget the credentials
396 ;; that are now possibly cached.
397 (dolist (host (list (nnoo-current-server 'nnimap
)
400 (dolist (element '("login" "password"))
401 (auth-source-forget-user-or-password
402 element host port
))))
403 (delete-process (nnimap-process nnimap-object
))
404 (setq nnimap-object nil
))))
406 (when (nnimap-capability "QRESYNC")
407 (nnimap-command "ENABLE QRESYNC"))
408 (nnimap-process nnimap-object
))))))))
410 (autoload 'rfc2104-hash
"rfc2104")
412 (defun nnimap-login (user password
)
414 ((not (nnimap-capability "LOGINDISABLED"))
415 (nnimap-command "LOGIN %S %S" user password
))
416 ((nnimap-capability "AUTH=PLAIN")
418 "AUTHENTICATE PLAIN %s"
419 (base64-encode-string
420 (format "\000%s\000%s"
421 (nnimap-quote-specials user
)
422 (nnimap-quote-specials password
)))))
423 ((nnimap-capability "AUTH=CRAM-MD5")
425 (let ((sequence (nnimap-send-command "AUTHENTICATE CRAM-MD5"))
426 (challenge (nnimap-wait-for-line "^\\+\\(.*\\)\n")))
428 (get-buffer-process (current-buffer))
430 (base64-encode-string
432 (rfc2104-hash 'md5
64 16 password
433 (base64-decode-string challenge
))))
435 (nnimap-wait-for-response sequence
)))))
437 (defun nnimap-quote-specials (string)
440 (goto-char (point-min))
441 (while (re-search-forward "[\\\"]" nil t
)
447 (defun nnimap-find-parameter (parameter elems
)
451 ((equal (car elem
) parameter
)
452 (setq result
(cdr elem
)))
453 ((and (equal (car elem
) "OK")
455 (equal (caadr elem
) parameter
))
456 (setq result
(cdr (cadr elem
))))))
459 (deffoo nnimap-close-server
(&optional server
)
460 (when (nnoo-change-server 'nnimap server nil
)
462 (delete-process (get-buffer-process (nnimap-buffer))))
463 (nnoo-close-server 'nnimap server
)
466 (deffoo nnimap-request-close
()
469 (deffoo nnimap-server-opened
(&optional server
)
470 (and (nnoo-current-server-p 'nnimap server
)
472 (gnus-buffer-live-p nntp-server-buffer
)
473 (nnimap-find-connection nntp-server-buffer
)))
475 (deffoo nnimap-status-message
(&optional server
)
476 nnimap-status-string
)
478 (deffoo nnimap-request-article
(article &optional group server to-buffer
)
479 (with-current-buffer nntp-server-buffer
480 (let ((result (nnimap-possibly-change-group group server
))
482 (when (stringp article
)
483 (setq article
(nnimap-find-article-by-message-id group article
)))
487 (with-current-buffer (nnimap-buffer)
489 (when nnimap-fetch-partial-articles
490 (nnimap-command "UID FETCH %d (BODYSTRUCTURE)" article
)
491 (goto-char (point-min))
492 (when (re-search-forward "FETCH.*BODYSTRUCTURE" nil t
)
493 (setq structure
(ignore-errors
494 (let ((start (point)))
496 (downcase-region start
(point))
498 (read (current-buffer))))
499 parts
(nnimap-find-wanted-parts structure
))))
501 (nnimap-get-partial-article article parts structure
)
502 (nnimap-get-whole-article article
))
503 (let ((buffer (current-buffer)))
504 (with-current-buffer (or to-buffer nntp-server-buffer
)
506 (insert-buffer-substring buffer
)
507 (nnheader-ms-strip-cr)
508 (cons group article
)))))))))
510 (deffoo nnimap-request-head
(article &optional group server to-buffer
)
511 (when (nnimap-possibly-change-group group server
)
512 (with-current-buffer (nnimap-buffer)
513 (when (stringp article
)
514 (setq article
(nnimap-find-article-by-message-id group article
)))
515 (nnimap-get-whole-article
516 article
(format "UID FETCH %%d %s"
517 (nnimap-header-parameters)))
518 (let ((buffer (current-buffer)))
519 (with-current-buffer (or to-buffer nntp-server-buffer
)
521 (insert-buffer-substring buffer
)
522 (nnheader-ms-strip-cr)
523 (cons group article
))))))
525 (defun nnimap-get-whole-article (article &optional command
)
530 "UID FETCH %d BODY.PEEK[]"
531 "UID FETCH %d RFC822.PEEK"))
533 ;; Check that we really got an article.
534 (goto-char (point-min))
535 (unless (re-search-forward "\\* [0-9]+ FETCH" nil t
)
538 ;; Remove any data that may have arrived before the FETCH data.
541 (delete-region (point-min) (point)))
542 (let ((bytes (nnimap-get-length)))
543 (delete-region (line-beginning-position)
544 (progn (forward-line 1) (point)))
545 (goto-char (+ (point) bytes
))
546 (delete-region (point) (point-max)))
549 (defun nnimap-capability (capability)
550 (member capability
(nnimap-capabilities nnimap-object
)))
552 (defun nnimap-ver4-p ()
553 (nnimap-capability "IMAP4REV1"))
555 (defun nnimap-get-partial-article (article parts structure
)
558 "UID FETCH %d (%s %s)"
564 (mapconcat (lambda (part)
565 (format "BODY.PEEK[%s]" part
))
567 (mapconcat (lambda (part)
568 (format "RFC822.PEEK[%s]" part
))
571 (nnimap-convert-partial-article structure
))))
573 (defun nnimap-convert-partial-article (structure)
574 ;; First just skip past the headers.
575 (goto-char (point-min))
576 (let ((bytes (nnimap-get-length))
578 ;; Delete "FETCH" line.
579 (delete-region (line-beginning-position)
580 (progn (forward-line 1) (point)))
581 (goto-char (+ (point) bytes
))
582 ;; Collect all the body parts.
583 (while (looking-at ".*BODY\\[\\([.0-9]+\\)\\]")
584 (setq id
(match-string 1)
585 bytes
(nnimap-get-length))
587 (delete-region (point) (progn (forward-line 1) (point)))
588 (push (list id
(buffer-substring (point) (+ (point) bytes
)))
590 (delete-region (point) (+ (point) bytes
)))
591 ;; Delete trailing junk.
592 (delete-region (point) (point-max))
593 ;; Now insert all the parts again where they fit in the structure.
594 (nnimap-insert-partial-structure structure parts
)
597 (defun nnimap-insert-partial-structure (structure parts
&optional subp
)
599 (let ((bstruc structure
))
600 (while (consp (car bstruc
))
602 (setq type
(car bstruc
))
603 (setq bstruc
(car (cdr bstruc
)))
604 (let ((has-boundary (member "boundary" bstruc
)))
606 (setq boundary
(cadr has-boundary
)))))
608 (insert (format "Content-type: multipart/%s; boundary=%S\n\n"
609 (downcase type
) boundary
)))
610 (while (not (stringp (car structure
)))
611 (insert "\n--" boundary
"\n")
612 (if (consp (caar structure
))
613 (nnimap-insert-partial-structure (pop structure
) parts t
)
614 (let ((bit (pop structure
)))
615 (insert (format "Content-type: %s/%s"
616 (downcase (nth 0 bit
))
617 (downcase (nth 1 bit
))))
618 (if (member "CHARSET" (nth 2 bit
))
620 "; charset=%S\n" (cadr (member "CHARSET" (nth 2 bit
)))))
622 (insert (format "Content-transfer-encoding: %s\n"
625 (when (assoc (nth 9 bit
) parts
)
626 (insert (cadr (assoc (nth 9 bit
) parts
)))))))
627 (insert "\n--" boundary
"--\n")))
629 (defun nnimap-find-wanted-parts (structure)
630 (message-flatten-list (nnimap-find-wanted-parts-1 structure
"")))
632 (defun nnimap-find-wanted-parts-1 (structure prefix
)
635 (while (consp (car structure
))
636 (let ((sub (pop structure
)))
637 (if (consp (car sub
))
638 (push (nnimap-find-wanted-parts-1
639 sub
(if (string= prefix
"")
640 (number-to-string num
)
641 (format "%s.%s" prefix num
)))
643 (let ((type (format "%s/%s" (nth 0 sub
) (nth 1 sub
)))
644 (id (if (string= prefix
"")
645 (number-to-string num
)
646 (format "%s.%s" prefix num
))))
647 (setcar (nthcdr 9 sub
) id
)
648 (when (if (eq nnimap-fetch-partial-articles t
)
650 (string-match nnimap-fetch-partial-articles type
))
655 (deffoo nnimap-request-group
(group &optional server dont-check info
)
656 (let ((result (nnimap-possibly-change-group
657 ;; Don't SELECT the group if we're going to select it
659 (if (and (not dont-check
)
660 (assoc group nnimap-current-infos
))
664 articles active marks high low
)
665 (with-current-buffer nntp-server-buffer
668 (setq active
(nth 2 (assoc group nnimap-current-infos
))))
669 (insert (format "211 %d %d %d %S\n"
670 (- (cdr active
) (car active
))
674 (with-current-buffer (nnimap-buffer)
676 (let ((group-sequence
677 (nnimap-send-command "SELECT %S" (utf7-encode group t
)))
679 (nnimap-send-command "UID FETCH 1:* FLAGS")))
680 (setf (nnimap-group nnimap-object
) group
)
681 (nnimap-wait-for-response flag-sequence
)
683 (nnimap-flags-to-marks
685 (list (list group-sequence flag-sequence
686 1 group
"SELECT")))))
689 (nnimap-update-infos marks
(list info
))
690 (nnimap-store-info info
(gnus-active (gnus-info-group info
))))
691 (goto-char (point-max))
692 (let ((uidnext (nth 5 (car marks
))))
693 (setq high
(or (if uidnext
697 low
(or (nth 4 (car marks
)) uidnext
1)))))
701 "211 %d %d %d %S\n" (1+ (- high low
)) low high group
)))
704 (deffoo nnimap-request-create-group
(group &optional server args
)
705 (when (nnimap-possibly-change-group nil server
)
706 (with-current-buffer (nnimap-buffer)
707 (car (nnimap-command "CREATE %S" (utf7-encode group t
))))))
709 (deffoo nnimap-request-delete-group
(group &optional force server
)
710 (when (nnimap-possibly-change-group nil server
)
711 (with-current-buffer (nnimap-buffer)
712 (car (nnimap-command "DELETE %S" (utf7-encode group t
))))))
714 (deffoo nnimap-request-rename-group
(group new-name
&optional server
)
715 (when (nnimap-possibly-change-group nil server
)
716 (with-current-buffer (nnimap-buffer)
717 (nnimap-unselect-group)
718 (car (nnimap-command "RENAME %S %S"
719 (utf7-encode group t
) (utf7-encode new-name t
))))))
721 (defun nnimap-unselect-group ()
722 ;; Make sure we don't have this group open read/write by asking
723 ;; to examine a mailbox that doesn't exist. This seems to be
724 ;; the only way that allows us to reliably go back to unselected
726 (nnimap-command "EXAMINE DOES.NOT.EXIST"))
728 (deffoo nnimap-request-expunge-group
(group &optional server
)
729 (when (nnimap-possibly-change-group group server
)
730 (with-current-buffer (nnimap-buffer)
731 (car (nnimap-command "EXPUNGE")))))
733 (defun nnimap-get-flags (spec)
736 (with-current-buffer (nnimap-buffer)
738 (nnimap-wait-for-response (nnimap-send-command
739 "UID FETCH %s FLAGS" spec
))
741 (subst-char-in-region (point-min) (point-max)
743 (goto-char (point-min))
744 (while (search-forward " FETCH " end t
)
745 (setq elems
(read (current-buffer)))
746 (push (cons (cadr (memq 'UID elems
))
747 (cadr (memq 'FLAGS elems
)))
749 (nreverse articles
)))
751 (deffoo nnimap-close-group
(group &optional server
)
754 (deffoo nnimap-request-move-article
(article group server accept-form
755 &optional last internal-move-group
)
757 (mm-disable-multibyte)
758 (when (funcall (if internal-move-group
760 'nnimap-request-article
)
761 article group server
(current-buffer))
762 ;; If the move is internal (on the same server), just do it the easy
764 (let ((message-id (message-field-value "message-id")))
765 (if internal-move-group
767 (with-current-buffer (nnimap-buffer)
768 (nnimap-command "UID COPY %d %S"
770 (utf7-encode internal-move-group t
)))))
772 (nnimap-delete-article article
)
773 (cons internal-move-group
774 (or (nnimap-find-uid-response "COPYUID" (cadr result
))
775 (nnimap-find-article-by-message-id
776 internal-move-group message-id
)))))
777 ;; Move the article to a different method.
778 (let ((result (eval accept-form
)))
780 (nnimap-delete-article article
)
783 (deffoo nnimap-request-expire-articles
(articles group
&optional server force
)
787 ((not (nnimap-possibly-change-group group server
))
790 (eq nnmail-expiry-target
'delete
))
791 (unless (nnimap-delete-article (gnus-compress-sequence articles
))
792 (nnheader-message 7 "Article marked for deletion, but not expunged."))
795 (let ((deletable-articles
797 (eq nnmail-expiry-wait
'immediate
))
799 (gnus-sorted-intersection
801 (nnimap-find-expired-articles group
)))))
802 (if (null deletable-articles
)
804 (if (eq nnmail-expiry-target
'delete
)
805 (nnimap-delete-article (gnus-compress-sequence deletable-articles
))
806 (setq deletable-articles
807 (nnimap-process-expiry-targets
808 deletable-articles group server
)))
809 ;; Return the articles we didn't delete.
810 (gnus-sorted-complement articles deletable-articles
))))))
812 (defun nnimap-process-expiry-targets (articles group server
)
813 (let ((deleted-articles nil
))
815 ;; shortcut further processing if we're going to delete the articles
816 ((eq nnmail-expiry-target
'delete
)
817 (setq deleted-articles articles
)
819 ;; or just move them to another folder on the same IMAP server
820 ((and (not (functionp nnmail-expiry-target
))
821 (gnus-server-equal (gnus-group-method nnmail-expiry-target
)
822 (gnus-server-to-method
823 (format "nnimap:%s" server
))))
824 (and (nnimap-possibly-change-group group server
)
825 (with-current-buffer (nnimap-buffer)
826 (nnheader-message 7 "Expiring articles from %s: %s" group articles
)
829 (nnimap-article-ranges (gnus-compress-sequence articles
))
830 (utf7-encode (gnus-group-real-name nnmail-expiry-target
) t
))
831 (setq deleted-articles articles
)))
834 (dolist (article articles
)
835 (let ((target nnmail-expiry-target
))
837 (mm-disable-multibyte)
838 (when (nnimap-request-article article group server
(current-buffer))
839 (nnheader-message 7 "Expiring article %s:%d" group article
)
840 (when (functionp target
)
841 (setq target
(funcall target group
)))
843 (not (eq target
'delete
)))
844 (if (or (gnus-request-group target t
)
845 (gnus-request-create-group target
))
846 (nnmail-expiry-target-group target group
)
849 (push article deleted-articles
))))))))
850 ;; Change back to the current group again.
851 (nnimap-possibly-change-group group server
)
852 (setq deleted-articles
(nreverse deleted-articles
))
853 (nnimap-delete-article (gnus-compress-sequence deleted-articles
))
856 (defun nnimap-find-expired-articles (group)
857 (let ((cutoff (nnmail-expired-article-p group nil nil
)))
858 (with-current-buffer (nnimap-buffer)
861 "UID SEARCH SENTBEFORE %s"
865 (car (rassoc (nth 4 (decode-time cutoff
))
866 parse-time-months
))))
869 (delete 0 (mapcar #'string-to-number
870 (cdr (assoc "SEARCH" (cdr result
))))))))))
873 (defun nnimap-find-article-by-message-id (group message-id
)
874 (with-current-buffer (nnimap-buffer)
876 (unless (equal group
(nnimap-group nnimap-object
))
877 (setf (nnimap-group nnimap-object
) nil
)
878 (setf (nnimap-examined nnimap-object
) group
)
879 (nnimap-send-command "EXAMINE %S" (utf7-encode group t
)))
881 (nnimap-send-command "UID SEARCH HEADER Message-Id %S" message-id
))
883 (setq result
(nnimap-wait-for-response sequence
))
885 (car (setq result
(nnimap-parse-response))))
886 ;; Select the last instance of the message in the group.
888 (car (last (assoc "SEARCH" (cdr result
)))))
889 (string-to-number article
))))))
891 (defun nnimap-delete-article (articles)
892 (with-current-buffer (nnimap-buffer)
893 (nnimap-command "UID STORE %s +FLAGS.SILENT (\\Deleted)"
894 (nnimap-article-ranges articles
))
896 ((nnimap-capability "UIDPLUS")
897 (nnimap-command "UID EXPUNGE %s"
898 (nnimap-article-ranges articles
))
901 (nnimap-command "EXPUNGE")
903 (t (gnus-message 7 (concat "nnimap: nnimap-expunge is not set and the "
904 "server doesn't support UIDPLUS, so we won't "
905 "delete this article now"))))))
907 (deffoo nnimap-request-scan
(&optional group server
)
908 (when (and (nnimap-possibly-change-group nil server
)
910 nnimap-split-methods
)
911 (nnheader-message 7 "nnimap %s splitting mail..." server
)
912 (nnimap-split-incoming-mail)))
914 (defun nnimap-marks-to-flags (marks)
917 (when (setq flag
(cadr (assq mark nnimap-mark-alist
)))
921 (deffoo nnimap-request-update-group-status
(group status
&optional server
)
922 (when (nnimap-possibly-change-group nil server
)
923 (let ((command (assoc
925 '((subscribe "SUBSCRIBE")
926 (unsubscribe "UNSUBSCRIBE")))))
928 (with-current-buffer (nnimap-buffer)
929 (nnimap-command "%s %S" (cadr command
) (utf7-encode group t
)))))))
931 (deffoo nnimap-request-set-mark
(group actions
&optional server
)
932 (when (nnimap-possibly-change-group group server
)
934 (with-current-buffer (nnimap-buffer)
936 ;; Just send all the STORE commands without waiting for
937 ;; response. If they're successful, they're successful.
938 (dolist (action actions
)
939 (destructuring-bind (range action marks
) action
940 (let ((flags (nnimap-marks-to-flags marks
)))
942 (setq sequence
(nnimap-send-command
943 "UID STORE %s %sFLAGS.SILENT (%s)"
944 (nnimap-article-ranges range
)
946 ((eq action
'del
) "-")
947 ((eq action
'add
) "+")
948 ((eq action
'set
) ""))
949 (mapconcat #'identity flags
" ")))))))
950 ;; Wait for the last command to complete to avoid later
951 ;; syncronisation problems with the stream.
953 (nnimap-wait-for-response sequence
))))))
955 (deffoo nnimap-request-accept-article
(group &optional server last
)
956 (when (nnimap-possibly-change-group nil server
)
957 (nnmail-check-syntax)
958 (let ((message-id (message-field-value "message-id"))
961 (setq message
(buffer-substring-no-properties (point-min) (point-max)))
962 (with-current-buffer (nnimap-buffer)
963 ;; If we have this group open read-only, then unselect it
964 ;; before appending to it.
965 (when (equal (nnimap-examined nnimap-object
) group
)
966 (nnimap-unselect-group))
968 (setq sequence
(nnimap-send-command
969 "APPEND %S {%d}" (utf7-encode group t
)
971 (unless nnimap-streaming
972 (nnimap-wait-for-connection "^[+]"))
973 (process-send-string (get-buffer-process (current-buffer)) message
)
974 (process-send-string (get-buffer-process (current-buffer))
975 (if (nnimap-newlinep nnimap-object
)
978 (let ((result (nnimap-get-response sequence
)))
979 (if (not (car result
))
981 (nnheader-message 7 "%s" (nnheader-get-report-string 'nnimap
))
984 (or (nnimap-find-uid-response "APPENDUID" (car result
))
985 (nnimap-find-article-by-message-id
986 group message-id
)))))))))
988 (defun nnimap-find-uid-response (name list
)
989 (let ((result (car (last (nnimap-find-response-element name list
)))))
991 (string-to-number result
))))
993 (defun nnimap-find-response-element (name list
)
996 (when (and (consp elem
)
997 (equal name
(car elem
)))
1001 (deffoo nnimap-request-replace-article
(article group buffer
)
1003 (when (and (nnimap-possibly-change-group group nil
)
1004 ;; Put the article into the group.
1005 (with-current-buffer buffer
1007 (nnimap-request-accept-article group nil t
))))
1008 (nnimap-delete-article (list article
))
1009 ;; Return the new article number.
1012 (defun nnimap-add-cr ()
1013 (goto-char (point-min))
1014 (while (re-search-forward "\r?\n" nil t
)
1015 (replace-match "\r\n" t t
)))
1017 (defun nnimap-get-groups ()
1019 (let ((sequence (nnimap-send-command "LIST \"\" \"*\""))
1021 (nnimap-wait-for-response sequence
)
1022 (subst-char-in-region (point-min) (point-max)
1024 (goto-char (point-min))
1025 (nnimap-unfold-quoted-lines)
1026 (goto-char (point-min))
1027 (while (search-forward "* LIST " nil t
)
1028 (let ((flags (read (current-buffer)))
1029 (separator (read (current-buffer)))
1030 (group (read (current-buffer))))
1031 (unless (member '%NoSelect flags
)
1032 (push (if (stringp group
)
1034 (format "%s" group
))
1038 (deffoo nnimap-request-list
(&optional server
)
1039 (nnimap-possibly-change-group nil server
)
1040 (with-current-buffer nntp-server-buffer
1043 (with-current-buffer (nnimap-buffer)
1044 (nnimap-get-groups)))
1045 sequences responses
)
1047 (with-current-buffer (nnimap-buffer)
1048 (setf (nnimap-group nnimap-object
) nil
)
1049 (dolist (group groups
)
1050 (setf (nnimap-examined nnimap-object
) group
)
1051 (push (list (nnimap-send-command "EXAMINE %S" (utf7-encode group t
))
1054 (nnimap-wait-for-response (caar sequences
))
1056 (nnimap-get-responses (mapcar #'car sequences
))))
1057 (dolist (response responses
)
1058 (let* ((sequence (car response
))
1059 (response (cadr response
))
1060 (group (cadr (assoc sequence sequences
))))
1062 (equal (caar response
) "OK"))
1063 (let ((uidnext (nnimap-find-parameter "UIDNEXT" response
))
1065 (dolist (elem response
)
1066 (when (equal (cadr elem
) "EXISTS")
1067 (setq exists
(string-to-number (car elem
)))))
1069 (setq highest
(1- (string-to-number (car uidnext
)))))
1072 (insert (format "%S 0 1 y\n" (utf7-decode group t
))))
1075 (insert (format "%S %d %d y\n"
1076 (utf7-decode group t
) highest
(1+ highest
))))
1078 ;; Return the widest possible range.
1079 (insert (format "%S %d 1 y\n" (utf7-decode group t
)
1080 (or highest exists
)))))))))
1083 (deffoo nnimap-request-newgroups
(date &optional server
)
1084 (nnimap-possibly-change-group nil server
)
1085 (with-current-buffer nntp-server-buffer
1087 (dolist (group (with-current-buffer (nnimap-buffer)
1088 (nnimap-get-groups)))
1089 (unless (assoc group nnimap-current-infos
)
1090 ;; Insert dummy numbers here -- they don't matter.
1091 (insert (format "%S 0 1 y\n" group
))))
1094 (deffoo nnimap-retrieve-group-data-early
(server infos
)
1095 (when (nnimap-possibly-change-group nil server
)
1096 (with-current-buffer (nnimap-buffer)
1098 (setf (nnimap-group nnimap-object
) nil
)
1099 (let ((qresyncp (nnimap-capability "QRESYNC"))
1100 params groups sequences active uidvalidity modseq group
)
1101 ;; Go through the infos and gather the data needed to know
1102 ;; what and how to request the data.
1103 (dolist (info infos
)
1104 (setq params
(gnus-info-params info
)
1105 group
(gnus-group-real-name (gnus-info-group info
))
1106 active
(cdr (assq 'active params
))
1107 uidvalidity
(cdr (assq 'uidvalidity params
))
1108 modseq
(cdr (assq 'modseq params
)))
1109 (setf (nnimap-examined nnimap-object
) group
)
1114 (list (nnimap-send-command "EXAMINE %S (%s (%s %s))"
1115 (utf7-encode group t
)
1116 (nnimap-quirk "QRESYNC")
1122 (if (and active uidvalidity
)
1123 ;; Fetch the last 100 flags.
1124 (max 1 (- (cdr active
) 100))
1129 ;; If we don't have a UIDVALIDITY, then this is
1130 ;; the first time we've seen the group, so we
1131 ;; have to do a SELECT (which is slower than an
1132 ;; examine), but will tell us whether the group
1133 ;; is read-only or not.
1135 (push (list (nnimap-send-command "%s %S" command
1136 (utf7-encode group t
))
1137 (nnimap-send-command "UID FETCH %d:* FLAGS" start
)
1138 start group command
)
1142 (defun nnimap-quirk (command)
1143 (let ((quirk (assoc command nnimap-quirks
)))
1144 ;; If this server is of a type that matches a quirk, then return
1145 ;; the "quirked" command instead of the proper one.
1146 (if (or (null quirk
)
1147 (not (string-match (nth 1 quirk
) (nnimap-greeting nnimap-object
))))
1151 (deffoo nnimap-finish-retrieve-group-infos
(server infos sequences
)
1152 (when (and sequences
1153 (nnimap-possibly-change-group nil server
))
1154 (with-current-buffer (nnimap-buffer)
1155 ;; Wait for the final data to trickle in.
1156 (when (nnimap-wait-for-response (if (eq (cadar sequences
) 'qresync
)
1160 ;; Now we should have most of the data we need, no matter
1161 ;; whether we're QRESYNCING, fetching all the flags from
1162 ;; scratch, or just fetching the last 100 flags per group.
1163 (nnimap-update-infos (nnimap-flags-to-marks
1165 (nreverse sequences
)))
1167 ;; Finally, just return something resembling an active file in
1168 ;; the nntp buffer, so that the agent can save the info, too.
1169 (with-current-buffer nntp-server-buffer
1171 (dolist (info infos
)
1172 (let* ((group (gnus-info-group info
))
1173 (active (gnus-active group
)))
1175 (insert (format "%S %d %d y\n"
1176 (gnus-group-real-name group
)
1178 (car active
)))))))))))
1180 (defun nnimap-update-infos (flags infos
)
1181 (dolist (info infos
)
1182 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1183 (marks (cdr (assoc group flags
))))
1185 (nnimap-update-info info marks
)))))
1187 (defun nnimap-update-info (info marks
)
1188 (destructuring-bind (existing flags high low uidnext start-article
1189 permanent-flags uidvalidity
1190 vanished highestmodseq
) marks
1192 ;; Ignore groups with no UIDNEXT/marks. This happens for
1193 ;; completely empty groups.
1194 ((and (not existing
)
1196 (let ((active (cdr (assq 'active
(gnus-info-params info
)))))
1198 (gnus-set-active (gnus-info-group info
) active
))))
1199 ;; We have a mismatch between the old and new UIDVALIDITY
1200 ;; identifiers, so we have to re-request the group info (the next
1201 ;; time). This virtually never happens.
1202 ((let ((old-uidvalidity
1203 (cdr (assq 'uidvalidity
(gnus-info-params info
)))))
1204 (and old-uidvalidity
1205 (not (equal old-uidvalidity uidvalidity
))
1206 (> start-article
1)))
1207 (gnus-group-remove-parameter info
'uidvalidity
)
1208 (gnus-group-remove-parameter info
'modseq
))
1209 ;; We have the data needed to update.
1211 (let* ((group (gnus-info-group info
))
1212 (completep (and start-article
1213 (= start-article
1)))
1214 (active (or (gnus-active group
)
1215 (cdr (assq 'active
(gnus-info-params info
))))))
1217 (setq high
(1- uidnext
)))
1218 ;; First set the active ranges based on high/low.
1220 (not (gnus-active group
)))
1221 (gnus-set-active group
1224 (cons (min (or low
(car active
))
1226 (max (or high
(cdr active
))
1231 ;; No articles in this group.
1232 (cons uidnext
(1- uidnext
)))
1234 (cons start-article
(1- start-article
)))
1236 ;; No articles and no uidnext.
1241 (or high
(1- uidnext
)))))
1242 ;; See whether this is a read-only group.
1243 (unless (eq permanent-flags
'not-scanned
)
1244 (gnus-group-set-parameter
1245 info
'permanent-flags
1246 (and (or (memq '%
* permanent-flags
)
1247 (memq '%Seen permanent-flags
))
1249 ;; Update marks and read articles if this isn't a
1250 ;; read-only IMAP group.
1251 (when (setq permanent-flags
1252 (cdr (assq 'permanent-flags
(gnus-info-params info
))))
1253 (if (and highestmodseq
1254 (not start-article
))
1255 ;; We've gotten the data by QRESYNCing.
1256 (nnimap-update-qresync-info
1257 info existing
(nnimap-imap-ranges-to-gnus-ranges vanished
) flags
)
1258 ;; Do normal non-QRESYNC flag updates.
1259 ;; Update the list of read articles.
1261 (gnus-compress-sequence
1262 (gnus-set-difference
1263 (gnus-set-difference
1265 (cdr (assoc '%Seen flags
)))
1266 (cdr (assoc '%Flagged flags
)))))
1267 (read (gnus-range-difference
1268 (cons start-article high
) unread
)))
1269 (when (> start-article
1)
1272 (if (> start-article
1)
1273 (gnus-sorted-range-intersection
1274 (cons 1 (1- start-article
))
1275 (gnus-info-read info
))
1276 (gnus-info-read info
))
1278 (when (or (not (listp permanent-flags
))
1279 (memq '%Seen permanent-flags
))
1280 (gnus-info-set-read info read
))
1281 ;; Update the marks.
1282 (setq marks
(gnus-info-marks info
))
1283 (dolist (type (cdr nnimap-mark-alist
))
1284 (when (or (not (listp permanent-flags
))
1285 (memq (car (assoc (caddr type
) flags
))
1287 (memq '%
* permanent-flags
))
1288 (let ((old-marks (assoc (car type
) marks
))
1290 (gnus-compress-sequence
1291 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1292 (assoc (intern (cadr type
) obarray
) flags
)
1293 (assoc (cadr type
) flags
)))))) ; "\Flagged"
1294 (setq marks
(delq old-marks marks
))
1296 (when (and old-marks
1297 (> start-article
1))
1298 (setq old-marks
(gnus-range-difference
1300 (cons start-article high
)))
1301 (setq new-marks
(gnus-range-nconcat old-marks new-marks
)))
1303 (push (cons (car type
) new-marks
) marks
)))))
1304 (gnus-info-set-marks info marks t
))))
1305 ;; Note the active level for the next run-through.
1306 (gnus-group-set-parameter info
'active
(gnus-active group
))
1307 (gnus-group-set-parameter info
'uidvalidity uidvalidity
)
1308 (gnus-group-set-parameter info
'modseq highestmodseq
)
1309 (nnimap-store-info info
(gnus-active group
)))))))
1311 (defun nnimap-update-qresync-info (info existing vanished flags
)
1312 ;; Add all the vanished articles to the list of read articles.
1317 (gnus-range-add (gnus-info-read info
)
1319 (cdr (assq '%Flagged flags
)))
1320 (cdr (assq '%Seen flags
))))
1321 (let ((marks (gnus-info-marks info
)))
1322 (dolist (type (cdr nnimap-mark-alist
))
1323 (let ((ticks (assoc (car type
) marks
))
1325 (cdr (or (assoc (caddr type
) flags
) ; %Flagged
1326 (assoc (intern (cadr type
) obarray
) flags
)
1327 (assoc (cadr type
) flags
))))) ; "\Flagged"
1328 (setq marks
(delq ticks marks
))
1330 ;; Add the new marks we got.
1331 (setq ticks
(gnus-add-to-range ticks new-marks
))
1332 ;; Remove the marks from messages that don't have them.
1333 (setq ticks
(gnus-remove-from-range
1335 (gnus-compress-sequence
1336 (gnus-sorted-complement existing new-marks
))))
1338 (push (cons (car type
) ticks
) marks
)))
1339 (gnus-info-set-marks info marks t
))))
1341 (defun nnimap-imap-ranges-to-gnus-ranges (irange)
1342 (if (zerop (length irange
))
1345 (dolist (elem (split-string irange
","))
1347 (if (string-match ":" elem
)
1348 (let ((numbers (split-string elem
":")))
1349 (cons (string-to-number (car numbers
))
1350 (string-to-number (cadr numbers
))))
1351 (string-to-number elem
))
1353 (nreverse result
))))
1355 (defun nnimap-store-info (info active
)
1356 (let* ((group (gnus-group-real-name (gnus-info-group info
)))
1357 (entry (assoc group nnimap-current-infos
)))
1359 (setcdr entry
(list info active
))
1360 (push (list group info active
) nnimap-current-infos
))))
1362 (defun nnimap-flags-to-marks (groups)
1363 (let (data group totalp uidnext articles start-article mark permanent-flags
1364 uidvalidity vanished highestmodseq
)
1365 (dolist (elem groups
)
1366 (setq group
(car elem
)
1367 uidnext
(nth 1 elem
)
1368 start-article
(nth 2 elem
)
1369 permanent-flags
(nth 3 elem
)
1370 uidvalidity
(nth 4 elem
)
1371 vanished
(nth 5 elem
)
1372 highestmodseq
(nth 6 elem
)
1373 articles
(nthcdr 7 elem
))
1374 (let ((high (caar articles
))
1376 (dolist (article articles
)
1377 (setq low
(car article
))
1378 (push (car article
) existing
)
1379 (dolist (flag (cdr article
))
1380 (setq mark
(assoc flag marks
))
1382 (push (list flag
(car article
)) marks
)
1383 (setcdr mark
(cons (car article
) (cdr mark
))))))
1384 (push (list group existing marks high low uidnext start-article
1385 permanent-flags uidvalidity vanished highestmodseq
)
1389 (defun nnimap-parse-flags (sequences)
1390 (goto-char (point-min))
1391 ;; Change \Delete etc to %Delete, so that the reader can read it.
1392 (subst-char-in-region (point-min) (point-max)
1394 (let (start end articles groups uidnext elems permanent-flags
1395 uidvalidity vanished highestmodseq
)
1396 (dolist (elem sequences
)
1397 (destructuring-bind (group-sequence flag-sequence totalp group command
)
1399 (setq start
(point))
1401 ;; The EXAMINE was successful.
1402 (search-forward (format "\n%d OK " group-sequence
) nil t
)
1407 (setq permanent-flags
1408 (if (equal command
"SELECT")
1409 (and (search-forward "PERMANENTFLAGS "
1410 (or end
(point-min)) t
)
1411 (read (current-buffer)))
1415 (and (search-forward "UIDNEXT "
1416 (or end
(point-min)) t
)
1417 (read (current-buffer))))
1420 (and (re-search-forward "UIDVALIDITY \\([0-9]+\\)"
1421 (or end
(point-min)) t
)
1422 ;; Store UIDVALIDITY as a string, as it's
1423 ;; too big for 32-bit Emacsen, usually.
1427 (and (eq flag-sequence
'qresync
)
1428 (re-search-forward "^\\* VANISHED .* \\([0-9:,]+\\)"
1429 (or end
(point-min)) t
)
1433 (and (search-forward "HIGHESTMODSEQ "
1434 (or end
(point-min)) t
)
1435 (read (current-buffer))))
1438 ;; The UID FETCH FLAGS was successful.
1439 (or (eq flag-sequence
'qresync
)
1440 (search-forward (format "\n%d OK " flag-sequence
) nil t
)))
1441 (if (eq flag-sequence
'qresync
)
1445 (setq start
(point))
1447 (while (re-search-forward "^\\* [0-9]+ FETCH " start t
)
1448 (setq elems
(read (current-buffer)))
1449 (push (cons (cadr (memq 'UID elems
))
1450 (cadr (memq 'FLAGS elems
)))
1452 (push (nconc (list group uidnext totalp permanent-flags uidvalidity
1453 vanished highestmodseq
)
1457 (setq articles nil
))))
1460 (defun nnimap-find-process-buffer (buffer)
1461 (cadr (assoc buffer nnimap-connection-alist
)))
1463 (deffoo nnimap-request-post
(&optional server
)
1464 (setq nnimap-status-string
"Read-only server")
1467 (deffoo nnimap-request-thread
(header)
1468 (let* ((id (mail-header-id header
))
1470 (or (mail-header-references header
)
1474 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
1476 (dolist (refid refs value
)
1478 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
1479 refid refid value
)))))
1480 (result (with-current-buffer (nnimap-buffer)
1481 (nnimap-command "UID SEARCH %s" cmd
))))
1483 (and (car result
) (delete 0 (mapcar #'string-to-number
1484 (cdr (assoc "SEARCH" (cdr result
))))))
1487 (defun nnimap-possibly-change-group (group server
)
1488 (let ((open-result t
))
1490 (not (nnimap-server-opened server
)))
1491 (setq open-result
(nnimap-open-server server
)))
1498 (with-current-buffer (nnimap-buffer)
1499 (if (equal group
(nnimap-group nnimap-object
))
1501 (let ((result (nnimap-command "SELECT %S" (utf7-encode group t
))))
1503 (setf (nnimap-group nnimap-object
) group
1504 (nnimap-select-result nnimap-object
) result
)
1507 (defun nnimap-find-connection (buffer)
1508 "Find the connection delivering to BUFFER."
1509 (let ((entry (assoc buffer nnimap-connection-alist
)))
1511 (if (and (buffer-name (cadr entry
))
1512 (get-buffer-process (cadr entry
))
1513 (memq (process-status (get-buffer-process (cadr entry
)))
1515 (get-buffer-process (cadr entry
))
1516 (setq nnimap-connection-alist
(delq entry nnimap-connection-alist
))
1519 (defvar nnimap-sequence
0)
1521 (defun nnimap-send-command (&rest args
)
1522 (process-send-string
1523 (get-buffer-process (current-buffer))
1526 (incf nnimap-sequence
)
1527 (apply #'format args
)
1528 (if (nnimap-newlinep nnimap-object
)
1531 ;; Some servers apparently can't have many outstanding
1532 ;; commands, so throttle them.
1533 (unless nnimap-streaming
1534 (nnimap-wait-for-response nnimap-sequence
))
1537 (defun nnimap-log-command (command)
1538 (with-current-buffer (get-buffer-create "*imap log*")
1539 (goto-char (point-max))
1540 (insert (format-time-string "%H:%M:%S") " " command
))
1543 (defun nnimap-command (&rest args
)
1545 (setf (nnimap-last-command-time nnimap-object
) (current-time))
1546 (let* ((sequence (apply #'nnimap-send-command args
))
1547 (response (nnimap-get-response sequence
)))
1548 (if (equal (caar response
) "OK")
1550 (nnheader-report 'nnimap
"%s"
1551 (mapconcat (lambda (a)
1553 (car response
) " "))
1556 (defun nnimap-get-response (sequence)
1557 (nnimap-wait-for-response sequence
)
1558 (nnimap-parse-response))
1560 (defun nnimap-wait-for-connection (&optional regexp
)
1561 (nnimap-wait-for-line (or regexp
"^[*.] .*\n") "[*.] \\([A-Z0-9]+\\)"))
1563 (defun nnimap-wait-for-line (regexp &optional response-regexp
)
1564 (let ((process (get-buffer-process (current-buffer))))
1565 (goto-char (point-min))
1566 (while (and (memq (process-status process
)
1568 (not (re-search-forward regexp nil t
)))
1569 (nnheader-accept-process-output process
)
1570 (goto-char (point-min)))
1572 (and (looking-at (or response-regexp regexp
))
1575 (defun nnimap-wait-for-response (sequence &optional messagep
)
1576 (let ((process (get-buffer-process (current-buffer)))
1580 (goto-char (point-max))
1581 (while (and (setq openp
(memq (process-status process
)
1583 (not (re-search-backward
1584 (format "^%d .*\n" sequence
)
1585 (if nnimap-streaming
1595 (nnheader-message 7 "nnimap read %dk" (/ (buffer-size) 1000)))
1596 (nnheader-accept-process-output process
)
1597 (goto-char (point-max)))
1600 ;; The user hit C-g while we were waiting: kill the process, in case
1601 ;; it's a gnutls-cli process that's stuck (tends to happen a lot behind
1603 (delete-process process
)
1606 (defun nnimap-parse-response ()
1607 (let ((lines (split-string (nnimap-last-response-string) "\r\n" t
))
1609 (dolist (line lines
)
1610 (push (cdr (nnimap-parse-line line
)) result
))
1611 ;; Return the OK/error code first, and then all the "continuation
1612 ;; lines" afterwards.
1614 (nreverse result
))))
1616 ;; Parse an IMAP response line lightly. They look like
1617 ;; "* OK [UIDVALIDITY 1164213559] UIDs valid", typically, so parse
1618 ;; the lines into a list of strings and lists of string.
1619 (defun nnimap-parse-line (line)
1622 (mm-disable-multibyte)
1624 (goto-char (point-min))
1626 (if (eql (setq char
(following-char)) ?
)
1634 (if (search-forward "]" (line-end-position) 'move
)
1641 (if (search-forward ")" (line-end-position) 'move
)
1648 (1- (or (search-forward "\"" (line-end-position) 'move
)
1651 (buffer-substring (point) (if (search-forward " " nil t
)
1653 (goto-char (point-max))))))
1655 (nreverse result
))))
1657 (defun nnimap-last-response-string ()
1660 (let ((end (point)))
1664 (while (and (not (bobp))
1665 (eql (following-char) ?
*))
1667 (unless (eql (following-char) ?
*)
1669 (buffer-substring (point) end
))))
1671 (defun nnimap-get-responses (sequences)
1673 (dolist (sequence sequences
)
1674 (goto-char (point-min))
1675 (when (re-search-forward (format "^%d " sequence
) nil t
)
1676 (push (list sequence
(nnimap-parse-response))
1680 (defvar nnimap-incoming-split-list nil
)
1682 (defun nnimap-fetch-inbox (articles)
1684 (nnimap-wait-for-response
1685 (nnimap-send-command
1687 (nnimap-article-ranges articles
)
1688 (format "(UID %s%s)"
1691 "BODY.PEEK[HEADER] BODY.PEEK"
1693 (if nnimap-split-download-body-default
1698 (defun nnimap-split-incoming-mail ()
1699 (with-current-buffer (nnimap-buffer)
1700 (let ((nnimap-incoming-split-list nil
)
1701 (nnmail-split-methods (if (eq nnimap-split-methods
'default
)
1702 nnmail-split-methods
1703 nnimap-split-methods
))
1704 (nnmail-split-fancy (or nnimap-split-fancy
1705 nnmail-split-fancy
))
1706 (nnmail-inhibit-default-split-group t
)
1707 (groups (nnimap-get-groups))
1710 (nnimap-command "SELECT %S" nnimap-inbox
)
1711 (setf (nnimap-group nnimap-object
) nnimap-inbox
)
1712 (setq new-articles
(nnimap-new-articles (nnimap-get-flags "1:*")))
1714 (nnimap-fetch-inbox new-articles
)
1715 (nnimap-transform-split-mail)
1716 (nnheader-ms-strip-cr)
1718 (nnmail-split-incoming (current-buffer)
1719 #'nnimap-save-mail-spec
1721 #'nnimap-dummy-active-number
1722 #'nnimap-save-mail-spec
)
1723 (when nnimap-incoming-split-list
1724 (let ((specs (nnimap-make-split-specs nnimap-incoming-split-list
))
1725 sequences junk-articles
)
1726 ;; Create any groups that doesn't already exist on the
1728 (dolist (spec specs
)
1729 (when (and (not (member (car spec
) groups
))
1730 (not (eq (car spec
) 'junk
)))
1731 (nnimap-command "CREATE %S" (utf7-encode (car spec
) t
))))
1732 ;; Then copy over all the messages.
1734 (dolist (spec specs
)
1735 (let ((group (car spec
))
1736 (ranges (cdr spec
)))
1737 (if (eq group
'junk
)
1738 (setq junk-articles ranges
)
1739 (push (list (nnimap-send-command
1741 (nnimap-article-ranges ranges
)
1742 (utf7-encode group t
))
1745 ;; Wait for the last COPY response...
1747 (nnimap-wait-for-response (caar sequences
))
1748 ;; And then mark the successful copy actions as deleted,
1749 ;; and possibly expunge them.
1750 (nnimap-mark-and-expunge-incoming
1751 (nnimap-parse-copied-articles sequences
)))
1752 (nnimap-mark-and-expunge-incoming junk-articles
)))))))
1754 (defun nnimap-mark-and-expunge-incoming (range)
1756 (setq range
(nnimap-article-ranges range
))
1759 (nnimap-send-command
1760 "UID STORE %s +FLAGS.SILENT (\\Deleted)" range
)))
1762 ;; If the server supports it, we now delete the message we have
1763 ;; just copied over.
1764 ((nnimap-capability "UIDPLUS")
1765 (setq sequence
(nnimap-send-command "UID EXPUNGE %s" range
)))
1766 ;; If it doesn't support UID EXPUNGE, then we only expunge if the
1767 ;; user has configured it.
1769 (setq sequence
(nnimap-send-command "EXPUNGE"))))
1770 (nnimap-wait-for-response sequence
))))
1772 (defun nnimap-parse-copied-articles (sequences)
1773 (let (sequence copied range
)
1774 (goto-char (point-min))
1775 (while (re-search-forward "^\\([0-9]+\\) OK " nil t
)
1776 (setq sequence
(string-to-number (match-string 1)))
1777 (when (setq range
(cadr (assq sequence sequences
)))
1778 (push (gnus-uncompress-range range
) copied
)))
1779 (gnus-compress-sequence (sort (apply #'nconc copied
) #'<))))
1781 (defun nnimap-new-articles (flags)
1783 (dolist (elem flags
)
1784 (unless (gnus-list-memq-of-list nnimap-unsplittable-articles
1786 (push (car elem
) new
)))
1787 (gnus-compress-sequence (nreverse new
))))
1789 (defun nnimap-make-split-specs (list)
1793 (destructuring-bind (article spec
) elem
1794 (dolist (group (delete nil
(mapcar #'car spec
)))
1795 (unless (setq entry
(assoc group specs
))
1796 (push (setq entry
(list group
)) specs
))
1797 (setcdr entry
(cons article
(cdr entry
))))))
1798 (dolist (entry specs
)
1799 (setcdr entry
(gnus-compress-sequence (sort (cdr entry
) #'<))))
1802 (defun nnimap-transform-split-mail ()
1803 (goto-char (point-min))
1804 (let (article bytes
)
1807 (while (not (looking-at "^\\* [0-9]+ FETCH.*UID \\([0-9]+\\)"))
1808 (delete-region (point) (progn (forward-line 1) (point)))
1811 (setq article
(match-string 1)
1812 bytes
(nnimap-get-length))
1813 (delete-region (line-beginning-position) (line-end-position))
1814 ;; Insert MMDF separator, and a way to remember what this
1816 (insert (format "\^A\^A\^A\^A\n\nX-nnimap-article: %s" article
))
1817 (forward-char (1+ bytes
))
1818 (setq bytes
(nnimap-get-length))
1819 (delete-region (line-beginning-position) (line-end-position))
1820 ;; There's a body; skip past that.
1822 (forward-char (1+ bytes
))
1823 (delete-region (line-beginning-position) (line-end-position)))))))
1825 (defun nnimap-dummy-active-number (group &optional server
)
1828 (defun nnimap-save-mail-spec (group-art &optional server full-nov
)
1830 (goto-char (point-min))
1831 (if (not (re-search-forward "X-nnimap-article: \\([0-9]+\\)" nil t
))
1832 (error "Invalid nnimap mail")
1833 (setq article
(string-to-number (match-string 1))))
1835 (if (eq group-art
'junk
)
1836 (list (cons 'junk
1))
1838 nnimap-incoming-split-list
)))
1842 ;;; nnimap.el ends here