1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
5 ;; Jim Radford <radford@robby.caltech.edu>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Todo, major things:
29 ;; o Fix Gnus to view correct number of unread/total articles in group buffer
30 ;; o Fix Gnus to handle leading '.' in group names (fixed?)
31 ;; o Finish disconnected mode (moving articles between mailboxes unplugged)
33 ;; o MIME (partial article fetches)
34 ;; o Split to other backends, different split rules for different
37 ;; Todo, minor things:
39 ;; o Don't require half of Gnus -- backends should be standalone
40 ;; o Support escape characters in `message-tokenize-header'
41 ;; o Verify that we don't use IMAP4rev1 specific things (RFC2060 App B)
42 ;; o Dont uid fetch 1,* in nnimap-retrive-groups (slow)
43 ;; o Split up big fetches (1,* header especially) in smaller chunks
44 ;; o What do I do with gnus-newsgroup-*?
45 ;; o Tell Gnus about new groups (how can we tell?)
46 ;; o Respooling (fix Gnus?) (unnecessery?)
47 ;; o Add support for the following: (if applicable)
48 ;; request-list-newsgroups, request-regenerate
50 ;; request-associate-buffer, request-restore-buffer,
51 ;; o Do The Right Thing when UIDVALIDITY changes (what's the right thing?)
52 ;; o Support RFC2221 (Login referrals)
53 ;; o IMAP2BIS compatibility? (RFC2061)
54 ;; o ACAP stuff (perhaps a different project, would be nice to ACAPify
56 ;; o What about Gnus's article editing, can we support it? NO!
57 ;; o Use \Draft to support the draft group??
75 (defconst nnimap-version "nnimap 0.131")
77 (defvoo nnimap-address nil
78 "Address of physical IMAP server. If nil, use the virtual server's name.")
80 (defvoo nnimap-server-port nil
81 "Port number on physical IMAP server.
82 If nil, defaults to 993 for SSL connections and 143 otherwise.")
84 ;; Splitting variables
86 (defvar nnimap-split-crosspost t
87 "If non-nil, do crossposting if several split methods match the mail.
88 If nil, the first match found will be used.")
90 (defvar nnimap-split-inbox nil
91 "*Name of mailbox to split mail from.
93 Mail is read from this mailbox and split according to rules in
96 This can be a string or a list of strings.")
98 (defvar nnimap-split-rule nil
99 "*Mail will be split according to theese rules.
101 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
103 If you'd like, for instance, one mail group for mail from the
104 \"gnus-imap\" mailing list, one group for junk mail and leave
105 everything else in the incoming mailbox, you could do something like
108 (setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
109 (\"INBOX.junk\" \"Subject:.*buy\")))
111 As you can see, `nnimap-split-rule' is a list of lists, where the first
112 element in each \"rule\" is the name of the IMAP mailbox, and the
113 second is a regexp that nnimap will try to match on the header to find
116 The first element can also be a list. In that case, the first element
117 is the server the second element is the group on that server in which
118 the matching article will be stored.
120 The second element can also be a function. In that case, it will be
121 called narrowed to the headers with the first element of the rule as
122 the argument. It should return a non-nil value if it thinks that the
123 mail belongs in that group.
125 This variable can also have a function as its value, the function will
126 be called with the headers narrowed and should return a group where it
127 thinks the article should be splitted to.")
129 (defvar nnimap-split-fancy nil
130 "Like `nnmail-split-fancy', which see.")
132 ;; Authorization / Privacy variables
134 (defvoo nnimap-auth-method nil
137 (defvoo nnimap-stream nil
138 "How nnimap will connect to the server.
140 The default, nil, will try to use the \"best\" method the server can
145 1) you want to connect with SSL. The SSL integration with IMAP is
146 brain-dead so you'll have to tell it specifically.
148 2) your server is more capable than your environment -- i.e. your
149 server accept Kerberos login's but you haven't installed the
150 `imtest' program or your machine isn't configured for Kerberos.
152 Possible choices: kerberos4, ssl, network")
154 (defvoo nnimap-authenticator nil
155 "How nnimap authenticate itself to the server.
157 The default, nil, will try to use the \"best\" method the server can
160 There is only one reason for fiddling with this variable, and that is
161 if your server is more capable than your environment -- i.e. you
162 connect to a server that accept Kerberos login's but you haven't
163 installed the `imtest' program or your machine isn't configured for
166 Possible choices: kerberos4, cram-md5, login, anonymous.")
168 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
169 "Directory to keep NOV cache files for nnimap groups.
170 See also `nnimap-nov-file-name'.")
172 (defvoo nnimap-nov-file-name "nnimap."
173 "NOV cache base filename.
174 The group name and `nnimap-nov-file-name-suffix' will be appended. A
175 typical complete file name would be
176 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
177 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
178 `nnmail-use-long-file-names' is nil")
180 (defvoo nnimap-nov-file-name-suffix ".novcache"
181 "Suffix for NOV cache base filename.")
183 (defvoo nnimap-nov-is-evil nil
184 "If non-nil, nnimap will never generate or use a local nov database for this backend.
185 Using nov databases will speed up header fetching considerably.
186 Unlike other backends, you do not need to take special care if you
187 flip this variable.")
189 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
190 "Whether to expunge a group when it is closed.
191 When a IMAP group with articles marked for deletion is closed, this
192 variable determine if nnimap should actually remove the articles or
195 If always, nnimap always perform a expunge when closing the group.
196 If never, nnimap never expunges articles marked for deletion.
197 If ask, nnimap will ask you if you wish to expunge marked articles.
199 When setting this variable to `never', you can only expunge articles
200 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
202 (defvoo nnimap-list-pattern "*"
203 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
204 See below for available wildcards.
206 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
207 REFERENCE will be passed as the first parameter to LIST/LSUB. The
208 semantics of this are server specific, on the University of Washington
209 server you can specify a directory.
212 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
214 There are two wildcards * and %. * matches everything, % matches
215 everything in the current hierarchy.")
217 (defvoo nnimap-news-groups nil
218 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
220 This variable should contain a regexp matching groups where you wish
221 replies to be stored to the mailbox directly.
224 '(\"^[^I][^N][^B][^O][^X].*$\")
226 This will match all groups not beginning with \"INBOX\".
228 Note that there is nothing technically different between mail-like and
229 news-like mailboxes. If you wish to have a group with todo items or
230 similar which you wouldn't want to set up a mailing list for, you can
231 use this to make replies go directly to the group.")
233 (defvoo nnimap-server-address nil
234 "Obsolete. Use `nnimap-address'.")
236 (defcustom nnimap-authinfo-file "~/.authinfo"
237 "Authorization information for IMAP servers. In .netrc format."
240 (repeat :tag "Entries"
243 :value ("" ("login" . "") ("password" . ""))
247 (const :format "" "login")
248 (string :format "Login: %v"))
250 (const :format "" "password")
251 (string :format "Password: %v")))))))
253 (defcustom nnimap-prune-cache t
254 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
257 (defvar nnimap-request-list-method 'imap-mailbox-list
258 "Method to use to request a list of all folders from the server.
259 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
260 restrict visible folders.")
262 ;; Internal variables:
264 (defvar nnimap-debug nil);; "*nnimap-debug*")
265 (defvar nnimap-current-move-server nil)
266 (defvar nnimap-current-move-group nil)
267 (defvar nnimap-current-move-article nil)
268 (defvar nnimap-length)
269 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
270 (defvar nnimap-progress-how-often 20)
271 (defvar nnimap-counter)
272 (defvar nnimap-callback-callback-function nil
273 "Gnus callback the nnimap asynchronous callback should call.")
274 (defvar nnimap-callback-buffer nil
275 "Which buffer the asynchronous article prefetch callback should work in.")
276 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
277 (defvar nnimap-current-server nil) ;; Current server
278 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
282 (nnoo-define-basics nnimap)
284 ;; Utility functions:
286 (defsubst nnimap-get-server-buffer (server)
287 "Return buffer for SERVER, if nil use current server."
288 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
290 (defun nnimap-possibly-change-server (server)
291 "Return buffer for SERVER, changing the current server as a side-effect.
292 If SERVER is nil, uses the current server."
293 (setq nnimap-current-server (or server nnimap-current-server)
294 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
296 (defun nnimap-verify-uidvalidity (group server)
297 "Verify stored uidvalidity match current one in GROUP on SERVER."
298 (let* ((gnusgroup (gnus-group-prefixed-name
299 group (gnus-server-to-method
300 (format "nnimap:%s" server))))
301 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
302 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity)))
304 (if (not (equal old-uidvalidity new-uidvalidity))
305 nil ;; uidvalidity clash
306 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
308 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
311 (defun nnimap-find-minmax-uid (group &optional examine)
312 "Find lowest and highest active article nummber in GROUP.
313 If EXAMINE is non-nil the group is selected read-only."
314 (with-current-buffer nnimap-server-buffer
315 (when (imap-mailbox-select group examine)
317 (when (> (imap-mailbox-get 'exists) 0)
318 (imap-fetch "1,*" "UID" nil 'nouidfetch)
319 (imap-message-map (lambda (uid Uid)
320 (setq minuid (if minuid (min minuid uid) uid)
321 maxuid (if maxuid (max maxuid uid) uid)))
323 (list (imap-mailbox-get 'exists) minuid maxuid)))))
325 (defun nnimap-possibly-change-group (group &optional server)
326 "Make GROUP the current group, and SERVER the current server."
327 (when (nnimap-possibly-change-server server)
328 (with-current-buffer nnimap-server-buffer
329 (if (or (null group) (imap-current-mailbox-p group))
331 (if (imap-mailbox-select group)
332 (if (or (nnimap-verify-uidvalidity
333 group (or server nnimap-current-server))
334 (zerop (imap-mailbox-get 'exists group))
337 "nnimap: Group %s is not uidvalid. Continue? " group)))
339 (imap-mailbox-unselect)
340 (error "nnimap: Group %s is not uid-valid." group))
341 (nnheader-report 'nnimap (imap-error-text)))))))
343 (defun nnimap-replace-whitespace (string)
344 "Return STRING with all whitespace replaced with space."
346 (while (string-match "[\r\n\t]+" string)
347 (setq string (replace-match " " t t string)))
350 ;; Required backend functions
352 (defun nnimap-retrieve-headers-progress ()
353 "Hook to insert NOV line for current article into `nntp-server-buffer'."
354 (and (numberp nnmail-large-newsgroup)
355 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
356 (> nnimap-length nnmail-large-newsgroup)
357 (nnheader-message 6 "nnimap: Retrieving headers... %c"
358 (nth (/ (% nnimap-counter
359 (* (length nnimap-progress-chars)
360 nnimap-progress-how-often))
361 nnimap-progress-how-often)
362 nnimap-progress-chars)))
363 (with-current-buffer nntp-server-buffer
364 (let (headers lines chars uid mbx)
365 (with-current-buffer nnimap-server-buffer
366 (setq uid imap-current-message
367 mbx imap-current-mailbox
368 headers (if (imap-capability 'IMAP4rev1)
369 ;; xxx don't just use car? alist doesn't contain
370 ;; anything else now, but it might...
371 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
372 (imap-message-get uid 'RFC822.HEADER))
373 lines (imap-body-lines (imap-message-body imap-current-message))
374 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
377 (buffer-disable-undo)
379 (nnheader-ms-strip-cr)
380 (let ((head (nnheader-parse-head 'naked)))
381 (mail-header-set-number head uid)
382 (mail-header-set-chars head chars)
383 (mail-header-set-lines head lines)
384 (mail-header-set-xref
385 head (format "%s %s:%d" (system-name) mbx uid))
388 (defun nnimap-retrieve-which-headers (articles fetch-old)
389 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
390 (with-current-buffer nnimap-server-buffer
391 (if (numberp (car-safe articles))
394 (nnimap-range-to-string
395 (gnus-compress-sequence
396 (append (gnus-uncompress-sequence
398 (cons (if (numberp fetch-old)
399 (max 1 (- (car articles) fetch-old))
401 (1- (car articles)))))
403 (mapcar (lambda (msgid)
405 (format "HEADER Message-Id %s" msgid)))
408 (defun nnimap-group-overview-filename (group server)
409 "Make pathname for GROUP on SERVER."
410 (let ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
411 (file (nnheader-translate-file-chars
412 (concat nnimap-nov-file-name
413 (if (equal server "")
415 server) "." group nnimap-nov-file-name-suffix) t)))
416 (if (or nnmail-use-long-file-names
417 (file-exists-p (concat dir file)))
419 (concat dir (mm-encode-coding-string
420 (nnheader-replace-chars-in-string file ?. ?/)
421 nnmail-pathname-coding-system)))))
423 (defun nnimap-retrieve-headers-from-file (group server)
424 (with-current-buffer nntp-server-buffer
425 (let ((nov (nnimap-group-overview-filename group server)))
426 (when (file-exists-p nov)
427 (mm-insert-file-contents nov)
428 (set-buffer-modified-p nil)
429 (let ((min (progn (goto-char (point-min))
431 (read (current-buffer)))))
432 (max (progn (goto-char (point-max))
435 (read (current-buffer))))))
436 (if (and (numberp min) (numberp max))
438 ;; junk, remove it, it's saved later
442 (defun nnimap-retrieve-headers-from-server (articles group server)
443 (with-current-buffer nnimap-server-buffer
444 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
445 (nnimap-length (gnus-range-length articles))
447 (imap-fetch (nnimap-range-to-string articles)
448 (concat "(UID RFC822.SIZE BODY "
450 (append '(Subject From Date Message-Id
451 References In-Reply-To Xref)
453 nnmail-extra-headers))))
454 (if (imap-capability 'IMAP4rev1)
455 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
456 (format "RFC822.HEADER.LINES %s)" headers)))))
457 (and (numberp nnmail-large-newsgroup)
458 (> nnimap-length nnmail-large-newsgroup)
459 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
461 (defun nnimap-use-nov-p (group server)
462 (or gnus-nov-is-evil nnimap-nov-is-evil
463 (unless (and (gnus-make-directory
465 (nnimap-group-overview-filename group server)))
467 (nnimap-group-overview-filename group server)))
468 (message "nnimap: Nov cache not writable, %s"
469 (nnimap-group-overview-filename group server)))))
471 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
472 (when (nnimap-possibly-change-group group server)
473 (with-current-buffer nntp-server-buffer
475 (if (nnimap-use-nov-p group server)
476 (nnimap-retrieve-headers-from-server
477 (gnus-compress-sequence articles) group server)
478 (let (uids cached low high)
479 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
481 high (car (last uids)))
482 (if (setq cached (nnimap-retrieve-headers-from-file group server))
484 ;; fetch articles with uids before cache block
485 (when (< low (car cached))
486 (goto-char (point-min))
487 (nnimap-retrieve-headers-from-server
488 (cons low (1- (car cached))) group server))
489 ;; fetch articles with uids after cache block
490 (when (> high (cdr cached))
491 (goto-char (point-max))
492 (nnimap-retrieve-headers-from-server
493 (cons (1+ (cdr cached)) high) group server))
494 (when nnimap-prune-cache
495 ;; remove nov's for articles which has expired on server
496 (goto-char (point-min))
497 (dolist (uid (gnus-set-difference articles uids))
498 (when (re-search-forward (format "^%d\t" uid) nil t)
499 (gnus-delete-line)))))
500 ;; nothing cached, fetch whole range from server
501 (nnimap-retrieve-headers-from-server
502 (cons low high) group server))
503 (when (buffer-modified-p)
505 1 (point-max) (nnimap-group-overview-filename group server)
507 (nnheader-nov-delete-outside-range low high))))
510 (defun nnimap-open-connection (server)
511 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
512 nnimap-authenticator nnimap-server-buffer))
513 (nnheader-report 'nnimap "Can't open connection to server %s" server)
514 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
515 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
516 (imap-close nnimap-server-buffer)
517 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
518 (let (list alist user passwd)
519 (and (fboundp 'gnus-parse-netrc)
520 (setq list (gnus-parse-netrc nnimap-authinfo-file)
521 alist (or (and (gnus-netrc-get
522 (gnus-netrc-machine list server) "machine")
523 (gnus-netrc-machine list server))
524 (gnus-netrc-machine list nnimap-address))
525 user (gnus-netrc-get alist "login")
526 passwd (gnus-netrc-get alist "password")))
527 (if (imap-authenticate user passwd nnimap-server-buffer)
529 (push (list server nnimap-server-buffer)
530 nnimap-server-buffer-alist)
531 (nnimap-possibly-change-server server))
532 (imap-close nnimap-server-buffer)
533 (kill-buffer nnimap-server-buffer)
534 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
536 (deffoo nnimap-open-server (server &optional defs)
537 (nnheader-init-server-buffer)
538 (if (nnimap-server-opened server)
540 (unless (assq 'nnimap-server-buffer defs)
541 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
542 ;; translate `nnimap-server-address' to `nnimap-address' in defs
543 ;; for people that configured nnimap with a very old version
544 (unless (assq 'nnimap-address defs)
545 (if (assq 'nnimap-server-address defs)
546 (push (list 'nnimap-address
547 (cadr (assq 'nnimap-server-address defs))) defs)
548 (push (list 'nnimap-address server) defs)))
549 (nnoo-change-server 'nnimap server defs)
550 (or (and nnimap-server-buffer
551 (imap-opened nnimap-server-buffer))
552 (nnimap-open-connection server))))
554 (deffoo nnimap-server-opened (&optional server)
555 "Whether SERVER is opened.
556 If SERVER is the current virtual server, and the connection to the
557 physical server is alive, this function return a non-nil value. If
558 SERVER is nil, it is treated as the current server."
559 ;; clean up autologouts??
560 (and (or server nnimap-current-server)
561 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
562 (imap-opened (nnimap-get-server-buffer server))))
564 (deffoo nnimap-close-server (&optional server)
565 "Close connection to server and free all resources connected to it.
566 Return nil if the server couldn't be closed for some reason."
567 (let ((server (or server nnimap-current-server)))
568 (when (or (nnimap-server-opened server)
569 (imap-opened (nnimap-get-server-buffer server)))
570 (imap-close (nnimap-get-server-buffer server))
571 (kill-buffer (nnimap-get-server-buffer server))
572 (setq nnimap-server-buffer nil
573 nnimap-current-server nil
574 nnimap-server-buffer-alist
575 (delq server nnimap-server-buffer-alist)))
576 (nnoo-close-server 'nnimap server)))
578 (deffoo nnimap-request-close ()
579 "Close connection to all servers and free all resources that the backend have reserved.
580 All buffers that have been created by that
581 backend should be killed. (Not the nntp-server-buffer, though.) This
582 function is generally only called when Gnus is shutting down."
583 (mapcar (lambda (server) (nnimap-close-server (car server)))
584 nnimap-server-buffer-alist)
585 (setq nnimap-server-buffer-alist nil))
587 (deffoo nnimap-status-message (&optional server)
588 "This function returns the last error message from server."
589 (when (nnimap-possibly-change-server server)
590 (nnoo-status-message 'nnimap server)))
592 (defun nnimap-demule (string)
593 (funcall (if (and (fboundp 'string-as-multibyte)
594 (subrp (symbol-function 'string-as-multibyte)))
599 (defun nnimap-callback ()
600 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
601 (with-current-buffer nnimap-callback-buffer
603 (with-current-buffer nnimap-server-buffer
604 (nnimap-demule (imap-message-get (imap-current-message) 'RFC822)))) ;xxx
605 (nnheader-ms-strip-cr)
606 (funcall nnimap-callback-callback-function t)))
608 (defun nnimap-request-article-part (article part prop
609 &optional group server to-buffer)
610 (when (nnimap-possibly-change-group group server)
611 (let ((article (if (stringp article)
612 (car-safe (imap-search
613 (format "HEADER Message-Id %s" article)
614 nnimap-server-buffer))
617 (gnus-message 9 "nnimap: Fetching (part of) article %d..." article)
618 (if (not nnheader-callback-function)
619 (with-current-buffer (or to-buffer nntp-server-buffer)
621 (insert (nnimap-demule (imap-fetch article part prop nil
622 nnimap-server-buffer)))
623 (nnheader-ms-strip-cr)
624 (gnus-message 9 "nnimap: Fetching (part of) article %d...done"
627 (nnheader-report 'nnimap "No such article: %s"
628 (imap-error-text nnimap-server-buffer))
629 (cons group article)))
630 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
631 (setq nnimap-callback-callback-function nnheader-callback-function
632 nnimap-callback-buffer nntp-server-buffer)
633 (imap-fetch-asynch article part nil nnimap-server-buffer)
634 (cons group article))))))
636 (deffoo nnimap-asynchronous-p ()
639 (deffoo nnimap-request-article (article &optional group server to-buffer)
640 (nnimap-request-article-part
641 article "RFC822.PEEK" 'RFC822 group server to-buffer))
643 (deffoo nnimap-request-head (article &optional group server to-buffer)
644 (nnimap-request-article-part
645 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer))
647 (deffoo nnimap-request-body (article &optional group server to-buffer)
648 (nnimap-request-article-part
649 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer))
651 (deffoo nnimap-request-group (group &optional server fast)
652 (nnimap-request-update-info-internal
654 (gnus-get-info (gnus-group-prefixed-name
655 group (gnus-server-to-method (format "nnimap:%s" server))))
657 (when (nnimap-possibly-change-group group server)
660 ((null (setq info (nnimap-find-minmax-uid group t)))
661 (nnheader-report 'nnimap "Could not get active info for %s"
664 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
665 (max 1 (or (nth 1 info) 1))
666 (or (nth 2 info) 0) group)
667 (nnheader-report 'nnimap "Group %s selected" group)
670 (defun nnimap-close-group (group &optional server)
671 (with-current-buffer nnimap-server-buffer
672 (when (and (imap-opened)
673 (nnimap-possibly-change-group group server))
674 (case nnimap-expunge-on-close
675 ('always (imap-mailbox-expunge)
676 (imap-mailbox-close))
677 ('ask (if (and (imap-search "DELETED")
678 (gnus-y-or-n-p (format
679 "Expunge articles in group `%s'? "
680 imap-current-mailbox)))
681 (progn (imap-mailbox-expunge)
682 (imap-mailbox-close))
683 (imap-mailbox-unselect)))
684 (t (imap-mailbox-unselect)))
685 (not imap-current-mailbox))))
687 (defun nnimap-pattern-to-list-arguments (pattern)
689 (cons (car-safe p) (or (cdr-safe p) p)))
690 (if (and (listp pattern)
691 (listp (cdr pattern)))
695 (deffoo nnimap-request-list (&optional server)
696 (when (nnimap-possibly-change-server server)
697 (with-current-buffer nntp-server-buffer
699 (gnus-message 5 "nnimap: Generating active list%s..."
700 (if (> (length server) 0) (concat " for " server) ""))
701 (with-current-buffer nnimap-server-buffer
702 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
703 (dolist (mbx (funcall nnimap-request-list-method
704 (cdr pattern) (car pattern)))
705 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
706 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
708 (with-current-buffer nntp-server-buffer
709 (insert (format "\"%s\" %d %d y\n"
710 mbx (or (nth 2 info) 0)
711 (max 1 (or (nth 1 info) 1)))))))))))
712 (gnus-message 5 "nnimap: Generating active list%s...done"
713 (if (> (length server) 0) (concat " for " server) ""))
716 (deffoo nnimap-request-post (&optional server)
718 (dolist (mbx (message-tokenize-header
719 (message-fetch-field "Newsgroups")) success)
720 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
721 (or (gnus-active to-newsgroup)
722 (gnus-activate-group to-newsgroup)
723 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
725 (or (and (gnus-request-create-group
726 to-newsgroup gnus-command-method)
727 (gnus-activate-group to-newsgroup nil nil
728 gnus-command-method))
729 (error "Couldn't create group %s" to-newsgroup)))
730 (error "No such group: %s" to-newsgroup))
731 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
732 (setq success nil))))))
734 ;; Optional backend functions
736 (deffoo nnimap-retrieve-groups (groups &optional server)
737 (when (nnimap-possibly-change-server server)
738 (gnus-message 5 "nnimap: Checking mailboxes...")
739 (with-current-buffer nntp-server-buffer
741 (dolist (group groups)
742 (gnus-message 7 "nnimap: Checking mailbox %s" group)
743 (or (member "\\NoSelect"
744 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
745 (let ((info (nnimap-find-minmax-uid group 'examine)))
746 (insert (format "\"%s\" %d %d y\n" group
748 (max 1 (or (nth 1 info) 1))))))))
749 (gnus-message 5 "nnimap: Checking mailboxes...done")
752 (deffoo nnimap-request-update-info-internal (group info &optional server)
753 (when (nnimap-possibly-change-group group server)
754 (when info;; xxx what does this mean? should we create a info?
755 (with-current-buffer nnimap-server-buffer
756 (gnus-message 5 "nnimap: Updating info for %s..."
757 (gnus-info-group info))
759 (when (nnimap-mark-permanent-p 'read)
761 ;; read info could contain articles marked unread by other
762 ;; imap clients! we correct this
763 (setq seen (gnus-uncompress-range (gnus-info-read info))
764 unseen (imap-search "UNSEEN UNDELETED")
765 seen (gnus-set-difference seen unseen)
766 ;; seen might lack articles marked as read by other
767 ;; imap clients! we correct this
768 seen (append seen (imap-search "SEEN"))
771 seen (gnus-compress-sequence seen t)
772 ;; we can't return '(1) since this isn't a "list of ranges",
773 ;; and we can't return '((1)) since g-list-of-unread-articles
774 ;; is buggy so we return '((1 . 1)).
775 seen (if (and (integerp (car seen))
777 (list (cons (car seen) (car seen)))
779 (gnus-info-set-read info seen)))
781 (mapcar (lambda (pred)
782 (when (and (nnimap-mark-permanent-p (cdr pred))
783 (member (nnimap-mark-to-flag (cdr pred))
784 (imap-mailbox-get 'flags)))
787 (nnimap-update-alist-soft
789 (gnus-compress-sequence
790 (imap-search (nnimap-mark-to-predicate (cdr pred))))
791 (gnus-info-marks info))
793 gnus-article-mark-lists)
795 (gnus-message 5 "nnimap: Updating info for %s...done"
796 (gnus-info-group info))
800 (deffoo nnimap-request-type (group &optional article)
801 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
805 (deffoo nnimap-request-set-mark (group actions &optional server)
806 (when (nnimap-possibly-change-group group server)
807 (with-current-buffer nnimap-server-buffer
809 (gnus-message 7 "nnimap: Setting marks in %s..." group)
810 (while (setq action (pop actions))
811 (let ((range (nth 0 action))
812 (what (nth 1 action))
813 (cmdmarks (nth 2 action))
815 ;; cache flags are pointless on the server
816 (setq cmdmarks (delq 'cache cmdmarks))
817 ;; flag dormant articles as ticked
818 (if (memq 'dormant cmdmarks)
819 (setq cmdmarks (cons 'tick cmdmarks)))
820 ;; remove stuff we are forbidden to store
821 (mapcar (lambda (mark)
822 (if (imap-message-flag-permanent-p
823 (nnimap-mark-to-flag mark))
824 (setq marks (cons mark marks))))
826 (when (and range marks)
827 (cond ((eq what 'del)
828 (imap-message-flags-del
829 (nnimap-range-to-string range)
830 (nnimap-mark-to-flag marks nil t)))
832 (imap-message-flags-add
833 (nnimap-range-to-string range)
834 (nnimap-mark-to-flag marks nil t)))
836 (imap-message-flags-set
837 (nnimap-range-to-string range)
838 (nnimap-mark-to-flag marks nil t)))))))
839 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
842 (defun nnimap-split-fancy ()
843 "Like nnmail-split-fancy, but uses nnimap-split-fancy."
844 (let ((nnmail-split-fancy nnimap-split-fancy))
845 (nnmail-split-fancy)))
847 (defun nnimap-split-to-groups (rules)
848 ;; tries to match all rules in nnimap-split-rule against content of
849 ;; nntp-server-buffer, returns a list of groups that matched.
850 (with-current-buffer nntp-server-buffer
851 ;; Fold continuation lines.
852 (goto-char (point-min))
853 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
854 (replace-match " " t t))
855 (if (functionp rules)
857 (let (to-groups regrepp)
859 (dolist (rule rules to-groups)
860 (let ((group (car rule))
861 (regexp (cadr rule)))
862 (goto-char (point-min))
863 (when (and (if (stringp regexp)
865 (setq regrepp (string-match "\\\\[0-9&]" group))
866 (re-search-forward regexp nil t))
867 (funcall regexp group))
868 ;; Don't enter the article into the same group twice.
869 (not (assoc group to-groups)))
871 (nnmail-expand-newtext group)
874 (or nnimap-split-crosspost
875 (throw 'split-done to-groups))))))))))
877 (defun nnimap-split-find-rule (server inbox)
880 (defun nnimap-split-find-inbox (server)
881 (if (listp nnimap-split-inbox)
883 (list nnimap-split-inbox)))
885 (defun nnimap-split-articles (&optional group server)
886 (when (nnimap-possibly-change-server server)
887 (with-current-buffer nnimap-server-buffer
888 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
889 ;; iterate over inboxes
890 (while (and (setq inbox (pop inboxes))
891 (nnimap-possibly-change-group inbox));; SELECT
892 ;; find split rule for this server / inbox
893 (when (setq rule (nnimap-split-find-rule server inbox))
894 ;; iterate over articles
895 (dolist (article (imap-search "UNSEEN UNDELETED"))
896 (when (nnimap-request-head article)
897 ;; copy article to right group(s)
898 (setq removeorig nil)
899 (dolist (to-group (nnimap-split-to-groups rule))
900 (if (imap-message-copy (number-to-string article)
901 to-group nil 'nocopyuid)
903 (message "IMAP split moved %s:%s:%d to %s" server inbox
906 ;; Add the group-art list to the history list.
907 (push (list (cons to-group 0)) nnmail-split-history))
908 (message "IMAP split failed to move %s:%s:%d to %s" server
909 inbox article to-group)))
910 ;; remove article if it was successfully copied somewhere
912 (imap-message-flags-add (format "%d" article)
913 "\\Seen \\Deleted")))))
914 (when (imap-mailbox-select inbox);; just in case
915 ;; todo: UID EXPUNGE (if available) to remove splitted articles
916 (imap-mailbox-expunge)
917 (imap-mailbox-close)))
920 (deffoo nnimap-request-scan (&optional group server)
921 (nnimap-split-articles group server))
923 (deffoo nnimap-request-newgroups (date &optional server)
924 (when (nnimap-possibly-change-server server)
925 (with-current-buffer nntp-server-buffer
926 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
927 (if (> (length server) 0) " on " "") server)
929 (dolist (pattern (nnimap-pattern-to-list-arguments
930 nnimap-list-pattern))
931 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
932 nnimap-server-buffer))
933 (or (member-if (lambda (mailbox)
934 (string= (downcase mailbox) "\\noselect"))
935 (imap-mailbox-get 'list-flags mbx
936 nnimap-server-buffer))
937 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
939 (insert (format "\"%s\" %d %d y\n"
940 mbx (or (nth 2 info) 0)
941 (max 1 (or (nth 1 info) 1)))))))))
942 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
943 (if (> (length server) 0) " on " "") server))
946 (deffoo nnimap-request-create-group (group &optional server args)
947 (when (nnimap-possibly-change-server server)
948 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
949 (imap-mailbox-create group nnimap-server-buffer))))
951 (defun nnimap-time-substract (time1 time2)
952 "Return TIME for TIME1 - TIME2."
953 (let* ((ms (- (car time1) (car time2)))
954 (ls (- (nth 1 time1) (nth 1 time2))))
956 (list (- ms 1) (+ (expt 2 16) ls))
959 (defun nnimap-date-days-ago (daysago)
960 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
961 (let ((date (format-time-string "%d-%b-%Y"
962 (nnimap-time-substract
964 (days-to-time daysago)))))
965 (if (eq ?0 (string-to-char date))
969 (defun nnimap-request-expire-articles-progress ()
970 (gnus-message 5 "nnimap: Marking article %d for deletion..."
971 imap-current-message))
973 ;; Notice that we don't actually delete anything, we just mark them deleted.
974 (deffoo nnimap-request-expire-articles (articles group &optional server force)
975 (let ((artseq (gnus-compress-sequence articles)))
976 (when (and artseq (nnimap-possibly-change-group group server))
977 (with-current-buffer nnimap-server-buffer
979 (and (imap-message-flags-add
980 (nnimap-range-to-string artseq) "\\Deleted")
982 (let ((days (or (and nnmail-expiry-wait-function
983 (funcall nnmail-expiry-wait-function group))
984 nnmail-expiry-wait)))
985 (cond ((eq days 'immediate)
986 (and (imap-message-flags-add
987 (nnimap-range-to-string artseq) "\\Deleted")
988 (setq articles nil)))
990 (let ((oldarts (imap-search
991 (format "UID %s NOT SINCE %s"
992 (nnimap-range-to-string artseq)
993 (nnimap-date-days-ago days))))
994 (imap-fetch-data-hook
995 '(nnimap-request-expire-articles-progress)))
997 (imap-message-flags-add
998 (nnimap-range-to-string
999 (gnus-compress-sequence oldarts))
1001 (setq articles (gnus-set-difference
1002 articles oldarts)))))))))))
1003 ;; return articles not deleted
1006 (deffoo nnimap-request-move-article (article group server
1007 accept-form &optional last)
1008 (when (nnimap-possibly-change-server server)
1010 (let ((buf (get-buffer-create " *nnimap move*"))
1011 (nnimap-current-move-article article)
1012 (nnimap-current-move-group group)
1013 (nnimap-current-move-server nnimap-current-server)
1015 (and (nnimap-request-article article group server)
1018 (buffer-disable-undo (current-buffer))
1019 (insert-buffer-substring nntp-server-buffer)
1020 (setq result (eval accept-form))
1023 (nnimap-request-expire-articles (list article) group server t))
1026 (deffoo nnimap-request-accept-article (group &optional server last)
1027 (when (nnimap-possibly-change-server server)
1030 (if (string= nnimap-current-server nnimap-current-move-server)
1031 ;; moving article within same server, speed it up...
1032 (and (nnimap-possibly-change-group
1033 nnimap-current-move-group)
1034 (imap-message-copy (number-to-string
1035 nnimap-current-move-article)
1036 group 'dontcreate nil
1037 nnimap-server-buffer))
1038 ;; turn into rfc822 format (\r\n eol's)
1039 (with-current-buffer (current-buffer)
1040 (goto-char (point-min))
1041 (while (search-forward "\n" nil t)
1042 (replace-match "\r\n")))
1043 ;; next line for Cyrus server bug
1044 (imap-mailbox-unselect nnimap-server-buffer)
1045 (imap-message-append group (current-buffer) nil nil
1046 nnimap-server-buffer)))
1047 (cons group (nth 1 uid))
1048 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1050 (deffoo nnimap-request-delete-group (group force &optional server)
1051 (when (nnimap-possibly-change-server server)
1052 (with-current-buffer nnimap-server-buffer
1054 (or (null (imap-mailbox-status group 'uidvalidity))
1055 (imap-mailbox-delete group))
1059 (deffoo nnimap-request-rename-group (group new-name &optional server)
1060 (when (nnimap-possibly-change-server server)
1061 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1063 (defun nnimap-expunge (mailbox server)
1064 (when (nnimap-possibly-change-group mailbox server)
1065 (imap-mailbox-expunge nnimap-server-buffer)))
1067 (defun nnimap-acl-get (mailbox server)
1068 (when (nnimap-possibly-change-server server)
1069 (imap-mailbox-acl-get mailbox nnimap-server-buffer)))
1071 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1072 (when (nnimap-possibly-change-server (cadr method))
1073 (unless (imap-capability 'ACL nnimap-server-buffer)
1074 (error "Your server does not support ACL editing"))
1075 (with-current-buffer nnimap-server-buffer
1076 ;; delete all removed identifiers
1077 (mapcar (lambda (old-acl)
1078 (unless (assoc (car old-acl) new-acls)
1079 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1080 (error "Can't delete ACL for %s" (car old-acl)))))
1082 ;; set all changed acl's
1083 (mapcar (lambda (new-acl)
1084 (let ((new-rights (cdr new-acl))
1085 (old-rights (cdr (assoc (car new-acl) old-acls))))
1086 (unless (and old-rights new-rights
1087 (string= old-rights new-rights))
1088 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1089 (error "Can't set ACL for %s to %s" (car new-acl)
1095 ;;; Internal functions
1098 ;; This is confusing.
1100 ;; mark => read, tick, draft, reply etc
1101 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1102 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1104 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1105 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1108 (defconst nnimap-mark-to-predicate-alist
1110 (lambda (pair) ; cdr is the mark
1111 (or (assoc (cdr pair)
1115 (reply . "ANSWERED")))
1117 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1118 (cons '(read . read) gnus-article-mark-lists)))
1120 (defun nnimap-mark-to-predicate (pred)
1121 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1122 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1123 to be used within a IMAP SEARCH query."
1124 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1126 (defconst nnimap-mark-to-flag-alist
1129 (or (assoc (cdr pair)
1131 (tick . "\\Flagged")
1133 (reply . "\\Answered")))
1135 (format "gnus-%s" (symbol-name (cdr pair))))))
1136 (cons '(read . read) gnus-article-mark-lists)))
1138 (defun nnimap-mark-to-flag-1 (preds)
1139 (if (and (not (null preds)) (listp preds))
1140 (cons (nnimap-mark-to-flag (car preds))
1141 (nnimap-mark-to-flag (cdr preds)))
1142 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1144 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1145 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1146 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1147 be used in a STORE FLAGS command."
1148 (let ((result (nnimap-mark-to-flag-1 preds)))
1149 (setq result (if (and (or make-string always-list)
1150 (not (listp result)))
1154 (mapconcat (lambda (flag)
1156 (mapconcat 'identity flag " ")
1161 (defun nnimap-mark-permanent-p (mark &optional group)
1162 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1163 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1165 (defun nnimap-remassoc (key alist)
1166 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1167 The modified LIST is returned. If the first member
1168 of LIST has a car that is `equal' to KEY, there is no way to remove it
1169 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1170 sure of changing the value of `foo'."
1172 (if (equal key (caar alist))
1174 (setcdr alist (nnimap-remassoc key (cdr alist)))
1177 (defun nnimap-update-alist-soft (key value alist)
1179 (cons (cons key value) (nnimap-remassoc key alist))
1180 (nnimap-remassoc key alist)))
1182 (defun nnimap-range-to-string (range)
1187 (car item) (cdr item))
1188 (format "%d" item)))
1189 (if (and (listp range) (not (listp (cdr range))))
1190 (list range);; make (1 . 2) into ((1 . 2))
1196 (buffer-disable-undo (get-buffer-create nnimap-debug))
1197 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1199 nnimap-possibly-change-server
1200 nnimap-verify-uidvalidity
1201 nnimap-find-minmax-uid
1202 nnimap-possibly-change-group
1203 ;;nnimap-replace-whitespace
1204 nnimap-retrieve-headers-progress
1205 nnimap-retrieve-which-headers
1206 nnimap-group-overview-filename
1207 nnimap-retrieve-headers-from-file
1208 nnimap-retrieve-headers-from-server
1209 nnimap-retrieve-headers
1210 nnimap-open-connection
1212 nnimap-server-opened
1214 nnimap-request-close
1215 nnimap-status-message
1217 nnimap-request-article-part
1218 nnimap-request-article
1221 nnimap-request-group
1223 nnimap-pattern-to-list-arguments
1226 nnimap-retrieve-groups
1227 nnimap-request-update-info-internal
1229 nnimap-request-set-mark
1230 nnimap-split-to-groups
1231 nnimap-split-find-rule
1232 nnimap-split-find-inbox
1233 nnimap-split-articles
1235 nnimap-request-newgroups
1236 nnimap-request-create-group
1237 nnimap-time-substract
1238 nnimap-date-days-ago
1239 nnimap-request-expire-articles-progress
1240 nnimap-request-expire-articles
1241 nnimap-request-move-article
1242 nnimap-request-accept-article
1243 nnimap-request-delete-group
1244 nnimap-request-rename-group
1245 gnus-group-nnimap-expunge
1246 gnus-group-nnimap-edit-acl
1247 gnus-group-nnimap-edit-acl-done
1248 nnimap-group-mode-hook
1249 nnimap-mark-to-predicate
1250 nnimap-mark-to-flag-1
1252 nnimap-mark-permanent-p
1254 nnimap-update-alist-soft
1255 nnimap-range-to-string
1260 ;;; nnimap.el ends here