1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998, 1999, 2000 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 Verify that we don't use IMAP4rev1 specific things (RFC2060 App B)
41 ;; o Dont uid fetch 1,* in nnimap-retrive-groups (slow)
42 ;; o Split up big fetches (1,* header especially) in smaller chunks
43 ;; o What do I do with gnus-newsgroup-*?
44 ;; o Tell Gnus about new groups (how can we tell?)
45 ;; o Respooling (fix Gnus?) (unnecessery?)
46 ;; o Add support for the following: (if applicable)
47 ;; request-list-newsgroups, request-regenerate
49 ;; request-associate-buffer, request-restore-buffer,
50 ;; o Do The Right Thing when UIDVALIDITY changes (what's the right thing?)
51 ;; o Support RFC2221 (Login referrals)
52 ;; o IMAP2BIS compatibility? (RFC2061)
53 ;; o ACAP stuff (perhaps a different project, would be nice to ACAPify
55 ;; o What about Gnus's article editing, can we support it? NO!
56 ;; o Use \Draft to support the draft group??
74 (defconst nnimap-version "nnimap 0.131")
76 (defvoo nnimap-address nil
77 "Address of physical IMAP server. If nil, use the virtual server's name.")
79 (defvoo nnimap-server-port nil
80 "Port number on physical IMAP server.
81 If nil, defaults to 993 for SSL connections and 143 otherwise.")
83 ;; Splitting variables
85 (defvar nnimap-split-crosspost t
86 "If non-nil, do crossposting if several split methods match the mail.
87 If nil, the first match found will be used.")
89 (defvar nnimap-split-inbox nil
90 "*Name of mailbox to split mail from.
92 Mail is read from this mailbox and split according to rules in
95 This can be a string or a list of strings.")
97 (defvar nnimap-split-rule nil
98 "*Mail will be split according to theese rules.
100 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
102 If you'd like, for instance, one mail group for mail from the
103 \"gnus-imap\" mailing list, one group for junk mail and leave
104 everything else in the incoming mailbox, you could do something like
107 (setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
108 (\"INBOX.junk\" \"Subject:.*buy\")))
110 As you can see, `nnimap-split-rule' is a list of lists, where the first
111 element in each \"rule\" is the name of the IMAP mailbox, and the
112 second is a regexp that nnimap will try to match on the header to find
115 The first element can also be a list. In that case, the first element
116 is the server the second element is the group on that server in which
117 the matching article will be stored.
119 The second element can also be a function. In that case, it will be
120 called narrowed to the headers with the first element of the rule as
121 the argument. It should return a non-nil value if it thinks that the
122 mail belongs in that group.
124 This variable can also have a function as its value, the function will
125 be called with the headers narrowed and should return a group where it
126 thinks the article should be splitted to.")
128 (defvar nnimap-split-predicate "UNSEEN UNDELETED"
129 "The predicate used to find articles to split.
130 If you use another IMAP client to peek on articles but always would
131 like nnimap to split them once it's started, you could change this to
132 \"UNDELETED\". Other available predicates are available in
133 RFC2060 section 6.4.4.")
135 (defvar nnimap-split-fancy nil
136 "Like `nnmail-split-fancy', which see.")
138 ;; Authorization / Privacy variables
140 (defvoo nnimap-auth-method nil
143 (defvoo nnimap-stream nil
144 "How nnimap will connect to the server.
146 The default, nil, will try to use the \"best\" method the server can
151 1) you want to connect with SSL. The SSL integration with IMAP is
152 brain-dead so you'll have to tell it specifically.
154 2) your server is more capable than your environment -- i.e. your
155 server accept Kerberos login's but you haven't installed the
156 `imtest' program or your machine isn't configured for Kerberos.
158 Possible choices: kerberos4, ssl, network")
160 (defvoo nnimap-authenticator nil
161 "How nnimap authenticate itself to the server.
163 The default, nil, will try to use the \"best\" method the server can
166 There is only one reason for fiddling with this variable, and that is
167 if your server is more capable than your environment -- i.e. you
168 connect to a server that accept Kerberos login's but you haven't
169 installed the `imtest' program or your machine isn't configured for
172 Possible choices: kerberos4, cram-md5, login, anonymous.")
174 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
175 "Directory to keep NOV cache files for nnimap groups.
176 See also `nnimap-nov-file-name'.")
178 (defvoo nnimap-nov-file-name "nnimap."
179 "NOV cache base filename.
180 The group name and `nnimap-nov-file-name-suffix' will be appended. A
181 typical complete file name would be
182 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
183 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
184 `nnmail-use-long-file-names' is nil")
186 (defvoo nnimap-nov-file-name-suffix ".novcache"
187 "Suffix for NOV cache base filename.")
189 (defvoo nnimap-nov-is-evil nil
190 "If non-nil, nnimap will never generate or use a local nov database for this backend.
191 Using nov databases will speed up header fetching considerably.
192 Unlike other backends, you do not need to take special care if you
193 flip this variable.")
195 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
196 "Whether to expunge a group when it is closed.
197 When a IMAP group with articles marked for deletion is closed, this
198 variable determine if nnimap should actually remove the articles or
201 If always, nnimap always perform a expunge when closing the group.
202 If never, nnimap never expunges articles marked for deletion.
203 If ask, nnimap will ask you if you wish to expunge marked articles.
205 When setting this variable to `never', you can only expunge articles
206 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
208 (defvoo nnimap-list-pattern "*"
209 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
210 See below for available wildcards.
212 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
213 REFERENCE will be passed as the first parameter to LIST/LSUB. The
214 semantics of this are server specific, on the University of Washington
215 server you can specify a directory.
218 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
220 There are two wildcards * and %. * matches everything, % matches
221 everything in the current hierarchy.")
223 (defvoo nnimap-news-groups nil
224 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
226 This variable should contain a regexp matching groups where you wish
227 replies to be stored to the mailbox directly.
230 '(\"^[^I][^N][^B][^O][^X].*$\")
232 This will match all groups not beginning with \"INBOX\".
234 Note that there is nothing technically different between mail-like and
235 news-like mailboxes. If you wish to have a group with todo items or
236 similar which you wouldn't want to set up a mailing list for, you can
237 use this to make replies go directly to the group.")
239 (defvoo nnimap-server-address nil
240 "Obsolete. Use `nnimap-address'.")
242 (defcustom nnimap-authinfo-file "~/.authinfo"
243 "Authorization information for IMAP servers. In .netrc format."
246 (repeat :tag "Entries"
249 :value ("" ("login" . "") ("password" . ""))
253 (const :format "" "login")
254 (string :format "Login: %v"))
256 (const :format "" "password")
257 (string :format "Password: %v")))))))
259 (defcustom nnimap-prune-cache t
260 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
263 (defvar nnimap-request-list-method 'imap-mailbox-list
264 "Method to use to request a list of all folders from the server.
265 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
266 restrict visible folders.")
268 ;; Internal variables:
270 (defvar nnimap-debug nil);; "*nnimap-debug*")
271 (defvar nnimap-current-move-server nil)
272 (defvar nnimap-current-move-group nil)
273 (defvar nnimap-current-move-article nil)
274 (defvar nnimap-length)
275 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
276 (defvar nnimap-progress-how-often 20)
277 (defvar nnimap-counter)
278 (defvar nnimap-callback-callback-function nil
279 "Gnus callback the nnimap asynchronous callback should call.")
280 (defvar nnimap-callback-buffer nil
281 "Which buffer the asynchronous article prefetch callback should work in.")
282 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
283 (defvar nnimap-current-server nil) ;; Current server
284 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
288 (nnoo-define-basics nnimap)
290 ;; Utility functions:
292 (defsubst nnimap-get-server-buffer (server)
293 "Return buffer for SERVER, if nil use current server."
294 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
296 (defun nnimap-possibly-change-server (server)
297 "Return buffer for SERVER, changing the current server as a side-effect.
298 If SERVER is nil, uses the current server."
299 (setq nnimap-current-server (or server nnimap-current-server)
300 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
302 (defun nnimap-verify-uidvalidity (group server)
303 "Verify stored uidvalidity match current one in GROUP on SERVER."
304 (let* ((gnusgroup (gnus-group-prefixed-name
305 group (gnus-server-to-method
306 (format "nnimap:%s" server))))
307 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
308 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity)))
310 (if (not (equal old-uidvalidity new-uidvalidity))
311 nil ;; uidvalidity clash
312 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
314 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
317 (defun nnimap-find-minmax-uid (group &optional examine)
318 "Find lowest and highest active article nummber in GROUP.
319 If EXAMINE is non-nil the group is selected read-only."
320 (with-current-buffer nnimap-server-buffer
321 (when (imap-mailbox-select group examine)
323 (when (> (imap-mailbox-get 'exists) 0)
324 (imap-fetch "1,*" "UID" nil 'nouidfetch)
325 (imap-message-map (lambda (uid Uid)
326 (setq minuid (if minuid (min minuid uid) uid)
327 maxuid (if maxuid (max maxuid uid) uid)))
329 (list (imap-mailbox-get 'exists) minuid maxuid)))))
331 (defun nnimap-possibly-change-group (group &optional server)
332 "Make GROUP the current group, and SERVER the current server."
333 (when (nnimap-possibly-change-server server)
334 (with-current-buffer nnimap-server-buffer
335 (if (or (null group) (imap-current-mailbox-p group))
337 (if (imap-mailbox-select group)
338 (if (or (nnimap-verify-uidvalidity
339 group (or server nnimap-current-server))
340 (zerop (imap-mailbox-get 'exists group))
343 "nnimap: Group %s is not uidvalid. Continue? " group)))
345 (imap-mailbox-unselect)
346 (error "nnimap: Group %s is not uid-valid." group))
347 (nnheader-report 'nnimap (imap-error-text)))))))
349 (defun nnimap-replace-whitespace (string)
350 "Return STRING with all whitespace replaced with space."
352 (while (string-match "[\r\n\t]+" string)
353 (setq string (replace-match " " t t string)))
356 ;; Required backend functions
358 (defun nnimap-retrieve-headers-progress ()
359 "Hook to insert NOV line for current article into `nntp-server-buffer'."
360 (and (numberp nnmail-large-newsgroup)
361 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
362 (> nnimap-length nnmail-large-newsgroup)
363 (nnheader-message 6 "nnimap: Retrieving headers... %c"
364 (nth (/ (% nnimap-counter
365 (* (length nnimap-progress-chars)
366 nnimap-progress-how-often))
367 nnimap-progress-how-often)
368 nnimap-progress-chars)))
369 (with-current-buffer nntp-server-buffer
370 (let (headers lines chars uid mbx)
371 (with-current-buffer nnimap-server-buffer
372 (setq uid imap-current-message
373 mbx imap-current-mailbox
374 headers (if (imap-capability 'IMAP4rev1)
375 ;; xxx don't just use car? alist doesn't contain
376 ;; anything else now, but it might...
377 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
378 (imap-message-get uid 'RFC822.HEADER))
379 lines (imap-body-lines (imap-message-body imap-current-message))
380 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
383 (buffer-disable-undo)
385 (nnheader-ms-strip-cr)
386 (nnheader-fold-continuation-lines)
387 (subst-char-in-region (point-min) (point-max) ?\t ? )
388 (let ((head (nnheader-parse-head 'naked)))
389 (mail-header-set-number head uid)
390 (mail-header-set-chars head chars)
391 (mail-header-set-lines head lines)
392 (mail-header-set-xref
393 head (format "%s %s:%d" (system-name) mbx uid))
396 (defun nnimap-retrieve-which-headers (articles fetch-old)
397 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
398 (with-current-buffer nnimap-server-buffer
399 (if (numberp (car-safe articles))
402 (nnimap-range-to-string
403 (gnus-compress-sequence
404 (append (gnus-uncompress-sequence
406 (cons (if (numberp fetch-old)
407 (max 1 (- (car articles) fetch-old))
409 (1- (car articles)))))
411 (mapcar (lambda (msgid)
413 (format "HEADER Message-Id %s" msgid)))
416 (defun nnimap-group-overview-filename (group server)
417 "Make pathname for GROUP on SERVER."
418 (let ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
419 (file (nnheader-translate-file-chars
420 (concat nnimap-nov-file-name
421 (if (equal server "")
423 server) "." group nnimap-nov-file-name-suffix) t)))
424 (if (or nnmail-use-long-file-names
425 (file-exists-p (concat dir file)))
427 (concat dir (mm-encode-coding-string
428 (nnheader-replace-chars-in-string file ?. ?/)
429 nnmail-pathname-coding-system)))))
431 (defun nnimap-retrieve-headers-from-file (group server)
432 (with-current-buffer nntp-server-buffer
433 (let ((nov (nnimap-group-overview-filename group server)))
434 (when (file-exists-p nov)
435 (mm-insert-file-contents nov)
436 (set-buffer-modified-p nil)
437 (let ((min (progn (goto-char (point-min))
439 (read (current-buffer)))))
440 (max (progn (goto-char (point-max))
443 (read (current-buffer))))))
444 (if (and (numberp min) (numberp max))
446 ;; junk, remove it, it's saved later
450 (defun nnimap-retrieve-headers-from-server (articles group server)
451 (with-current-buffer nnimap-server-buffer
452 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
453 (nnimap-length (gnus-range-length articles))
455 (imap-fetch (nnimap-range-to-string articles)
456 (concat "(UID RFC822.SIZE BODY "
458 (append '(Subject From Date Message-Id
459 References In-Reply-To Xref)
461 nnmail-extra-headers))))
462 (if (imap-capability 'IMAP4rev1)
463 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
464 (format "RFC822.HEADER.LINES %s)" headers)))))
465 (and (numberp nnmail-large-newsgroup)
466 (> nnimap-length nnmail-large-newsgroup)
467 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
469 (defun nnimap-use-nov-p (group server)
470 (or gnus-nov-is-evil nnimap-nov-is-evil
471 (unless (and (gnus-make-directory
473 (nnimap-group-overview-filename group server)))
475 (nnimap-group-overview-filename group server)))
476 (message "nnimap: Nov cache not writable, %s"
477 (nnimap-group-overview-filename group server)))))
479 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
480 (when (nnimap-possibly-change-group group server)
481 (with-current-buffer nntp-server-buffer
483 (if (nnimap-use-nov-p group server)
484 (nnimap-retrieve-headers-from-server
485 (gnus-compress-sequence articles) group server)
486 (let (uids cached low high)
487 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
489 high (car (last uids)))
490 (if (setq cached (nnimap-retrieve-headers-from-file group server))
492 ;; fetch articles with uids before cache block
493 (when (< low (car cached))
494 (goto-char (point-min))
495 (nnimap-retrieve-headers-from-server
496 (cons low (1- (car cached))) group server))
497 ;; fetch articles with uids after cache block
498 (when (> high (cdr cached))
499 (goto-char (point-max))
500 (nnimap-retrieve-headers-from-server
501 (cons (1+ (cdr cached)) high) group server))
502 (when nnimap-prune-cache
503 ;; remove nov's for articles which has expired on server
504 (goto-char (point-min))
505 (dolist (uid (gnus-set-difference articles uids))
506 (when (re-search-forward (format "^%d\t" uid) nil t)
507 (gnus-delete-line)))))
508 ;; nothing cached, fetch whole range from server
509 (nnimap-retrieve-headers-from-server
510 (cons low high) group server))
511 (when (buffer-modified-p)
513 1 (point-max) (nnimap-group-overview-filename group server)
515 (nnheader-nov-delete-outside-range low high))))
518 (defun nnimap-open-connection (server)
519 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
520 nnimap-authenticator nnimap-server-buffer))
521 (nnheader-report 'nnimap "Can't open connection to server %s" server)
522 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
523 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
524 (imap-close nnimap-server-buffer)
525 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
526 (let (list alist user passwd)
527 (and (fboundp 'gnus-parse-netrc)
528 (setq list (gnus-parse-netrc nnimap-authinfo-file)
529 alist (or (and (gnus-netrc-get
530 (gnus-netrc-machine list server) "machine")
531 (gnus-netrc-machine list server))
532 (gnus-netrc-machine list nnimap-address))
533 user (gnus-netrc-get alist "login")
534 passwd (gnus-netrc-get alist "password")))
535 (if (imap-authenticate user passwd nnimap-server-buffer)
537 (push (list server nnimap-server-buffer)
538 nnimap-server-buffer-alist)
539 (nnimap-possibly-change-server server))
540 (imap-close nnimap-server-buffer)
541 (kill-buffer nnimap-server-buffer)
542 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
544 (deffoo nnimap-open-server (server &optional defs)
545 (nnheader-init-server-buffer)
546 (if (nnimap-server-opened server)
548 (unless (assq 'nnimap-server-buffer defs)
549 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
550 ;; translate `nnimap-server-address' to `nnimap-address' in defs
551 ;; for people that configured nnimap with a very old version
552 (unless (assq 'nnimap-address defs)
553 (if (assq 'nnimap-server-address defs)
554 (push (list 'nnimap-address
555 (cadr (assq 'nnimap-server-address defs))) defs)
556 (push (list 'nnimap-address server) defs)))
557 (nnoo-change-server 'nnimap server defs)
558 (or (and nnimap-server-buffer
559 (imap-opened nnimap-server-buffer))
560 (nnimap-open-connection server))))
562 (deffoo nnimap-server-opened (&optional server)
563 "Whether SERVER is opened.
564 If SERVER is the current virtual server, and the connection to the
565 physical server is alive, this function return a non-nil value. If
566 SERVER is nil, it is treated as the current server."
567 ;; clean up autologouts??
568 (and (or server nnimap-current-server)
569 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
570 (imap-opened (nnimap-get-server-buffer server))))
572 (deffoo nnimap-close-server (&optional server)
573 "Close connection to server and free all resources connected to it.
574 Return nil if the server couldn't be closed for some reason."
575 (let ((server (or server nnimap-current-server)))
576 (when (or (nnimap-server-opened server)
577 (imap-opened (nnimap-get-server-buffer server)))
578 (imap-close (nnimap-get-server-buffer server))
579 (kill-buffer (nnimap-get-server-buffer server))
580 (setq nnimap-server-buffer nil
581 nnimap-current-server nil
582 nnimap-server-buffer-alist
583 (delq server nnimap-server-buffer-alist)))
584 (nnoo-close-server 'nnimap server)))
586 (deffoo nnimap-request-close ()
587 "Close connection to all servers and free all resources that the backend have reserved.
588 All buffers that have been created by that
589 backend should be killed. (Not the nntp-server-buffer, though.) This
590 function is generally only called when Gnus is shutting down."
591 (mapcar (lambda (server) (nnimap-close-server (car server)))
592 nnimap-server-buffer-alist)
593 (setq nnimap-server-buffer-alist nil))
595 (deffoo nnimap-status-message (&optional server)
596 "This function returns the last error message from server."
597 (when (nnimap-possibly-change-server server)
598 (nnoo-status-message 'nnimap server)))
600 (defun nnimap-demule (string)
601 (funcall (if (and (fboundp 'string-as-multibyte)
602 (subrp (symbol-function 'string-as-multibyte)))
607 (defun nnimap-callback ()
608 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
609 (with-current-buffer nnimap-callback-buffer
611 (with-current-buffer nnimap-server-buffer
613 (if (imap-capability 'IMAP4rev1)
614 ;; xxx don't just use car? alist doesn't contain
615 ;; anything else now, but it might...
616 (nth 2 (car (imap-message-get (imap-current-message) 'BODYDETAIL)))
617 (imap-message-get (imap-current-message) 'RFC822)))))
618 (nnheader-ms-strip-cr)
619 (funcall nnimap-callback-callback-function t)))
621 (defun nnimap-request-article-part (article part prop &optional
622 group server to-buffer detail)
623 (when (nnimap-possibly-change-group group server)
624 (let ((article (if (stringp article)
625 (car-safe (imap-search
626 (format "HEADER Message-Id %s" article)
627 nnimap-server-buffer))
630 (gnus-message 9 "nnimap: Fetching (part of) article %d..." article)
631 (if (not nnheader-callback-function)
632 (with-current-buffer (or to-buffer nntp-server-buffer)
634 (let ((data (imap-fetch article part prop nil
635 nnimap-server-buffer)))
636 (insert (nnimap-demule (if detail
639 (nnheader-ms-strip-cr)
640 (gnus-message 9 "nnimap: Fetching (part of) article %d...done"
643 (nnheader-report 'nnimap "No such article: %s"
644 (imap-error-text nnimap-server-buffer))
645 (cons group article)))
646 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
647 (setq nnimap-callback-callback-function nnheader-callback-function
648 nnimap-callback-buffer nntp-server-buffer)
649 (imap-fetch-asynch article part nil nnimap-server-buffer)
650 (cons group article))))))
652 (deffoo nnimap-asynchronous-p ()
655 (deffoo nnimap-request-article (article &optional group server to-buffer)
656 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
657 (nnimap-request-article-part
658 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
659 (nnimap-request-article-part
660 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
662 (deffoo nnimap-request-head (article &optional group server to-buffer)
663 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
664 (nnimap-request-article-part
665 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
666 (nnimap-request-article-part
667 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
669 (deffoo nnimap-request-body (article &optional group server to-buffer)
670 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
671 (nnimap-request-article-part
672 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
673 (nnimap-request-article-part
674 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
676 (deffoo nnimap-request-group (group &optional server fast)
677 (nnimap-request-update-info-internal
679 (gnus-get-info (gnus-group-prefixed-name
680 group (gnus-server-to-method (format "nnimap:%s" server))))
682 (when (nnimap-possibly-change-group group server)
685 ((null (setq info (nnimap-find-minmax-uid group t)))
686 (nnheader-report 'nnimap "Could not get active info for %s"
689 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
690 (max 1 (or (nth 1 info) 1))
691 (or (nth 2 info) 0) group)
692 (nnheader-report 'nnimap "Group %s selected" group)
695 (defun nnimap-close-group (group &optional server)
696 (with-current-buffer nnimap-server-buffer
697 (when (and (imap-opened)
698 (nnimap-possibly-change-group group server))
699 (case nnimap-expunge-on-close
700 ('always (imap-mailbox-expunge)
701 (imap-mailbox-close))
702 ('ask (if (and (imap-search "DELETED")
703 (gnus-y-or-n-p (format
704 "Expunge articles in group `%s'? "
705 imap-current-mailbox)))
706 (progn (imap-mailbox-expunge)
707 (imap-mailbox-close))
708 (imap-mailbox-unselect)))
709 (t (imap-mailbox-unselect)))
710 (not imap-current-mailbox))))
712 (defun nnimap-pattern-to-list-arguments (pattern)
714 (cons (car-safe p) (or (cdr-safe p) p)))
715 (if (and (listp pattern)
716 (listp (cdr pattern)))
720 (deffoo nnimap-request-list (&optional server)
721 (when (nnimap-possibly-change-server server)
722 (with-current-buffer nntp-server-buffer
724 (gnus-message 5 "nnimap: Generating active list%s..."
725 (if (> (length server) 0) (concat " for " server) ""))
726 (with-current-buffer nnimap-server-buffer
727 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
728 (dolist (mbx (funcall nnimap-request-list-method
729 (cdr pattern) (car pattern)))
730 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
731 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
733 (with-current-buffer nntp-server-buffer
734 (insert (format "\"%s\" %d %d y\n"
735 mbx (or (nth 2 info) 0)
736 (max 1 (or (nth 1 info) 1)))))))))))
737 (gnus-message 5 "nnimap: Generating active list%s...done"
738 (if (> (length server) 0) (concat " for " server) ""))
741 (deffoo nnimap-request-post (&optional server)
743 (dolist (mbx (message-unquote-tokens
744 (message-tokenize-header
745 (message-fetch-field "Newsgroups") ", ")) success)
746 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
747 (or (gnus-active to-newsgroup)
748 (gnus-activate-group to-newsgroup)
749 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
751 (or (and (gnus-request-create-group
752 to-newsgroup gnus-command-method)
753 (gnus-activate-group to-newsgroup nil nil
754 gnus-command-method))
755 (error "Couldn't create group %s" to-newsgroup)))
756 (error "No such group: %s" to-newsgroup))
757 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
758 (setq success nil))))))
760 ;; Optional backend functions
762 (deffoo nnimap-retrieve-groups (groups &optional server)
763 (when (nnimap-possibly-change-server server)
764 (gnus-message 5 "nnimap: Checking mailboxes...")
765 (with-current-buffer nntp-server-buffer
767 (dolist (group groups)
768 (gnus-message 7 "nnimap: Checking mailbox %s" group)
769 (or (member "\\NoSelect"
770 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
771 (let ((info (nnimap-find-minmax-uid group 'examine)))
772 (insert (format "\"%s\" %d %d y\n" group
774 (max 1 (or (nth 1 info) 1))))))))
775 (gnus-message 5 "nnimap: Checking mailboxes...done")
778 (deffoo nnimap-request-update-info-internal (group info &optional server)
779 (when (nnimap-possibly-change-group group server)
780 (when info;; xxx what does this mean? should we create a info?
781 (with-current-buffer nnimap-server-buffer
782 (gnus-message 5 "nnimap: Updating info for %s..."
783 (gnus-info-group info))
785 (when (nnimap-mark-permanent-p 'read)
787 ;; read info could contain articles marked unread by other
788 ;; imap clients! we correct this
789 (setq seen (gnus-uncompress-range (gnus-info-read info))
790 unseen (imap-search "UNSEEN UNDELETED")
791 seen (gnus-set-difference seen unseen)
792 ;; seen might lack articles marked as read by other
793 ;; imap clients! we correct this
794 seen (append seen (imap-search "SEEN"))
797 seen (gnus-compress-sequence seen t)
798 ;; we can't return '(1) since this isn't a "list of ranges",
799 ;; and we can't return '((1)) since g-list-of-unread-articles
800 ;; is buggy so we return '((1 . 1)).
801 seen (if (and (integerp (car seen))
803 (list (cons (car seen) (car seen)))
805 (gnus-info-set-read info seen)))
807 (mapcar (lambda (pred)
808 (when (and (nnimap-mark-permanent-p (cdr pred))
809 (member (nnimap-mark-to-flag (cdr pred))
810 (imap-mailbox-get 'flags)))
813 (nnimap-update-alist-soft
815 (gnus-compress-sequence
816 (imap-search (nnimap-mark-to-predicate (cdr pred))))
817 (gnus-info-marks info))
819 gnus-article-mark-lists)
821 (gnus-message 5 "nnimap: Updating info for %s...done"
822 (gnus-info-group info))
826 (deffoo nnimap-request-type (group &optional article)
827 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
831 (deffoo nnimap-request-set-mark (group actions &optional server)
832 (when (nnimap-possibly-change-group group server)
833 (with-current-buffer nnimap-server-buffer
835 (gnus-message 7 "nnimap: Setting marks in %s..." group)
836 (while (setq action (pop actions))
837 (let ((range (nth 0 action))
838 (what (nth 1 action))
839 (cmdmarks (nth 2 action))
841 ;; cache flags are pointless on the server
842 (setq cmdmarks (delq 'cache cmdmarks))
843 ;; flag dormant articles as ticked
844 (if (memq 'dormant cmdmarks)
845 (setq cmdmarks (cons 'tick cmdmarks)))
846 ;; remove stuff we are forbidden to store
847 (mapcar (lambda (mark)
848 (if (imap-message-flag-permanent-p
849 (nnimap-mark-to-flag mark))
850 (setq marks (cons mark marks))))
852 (when (and range marks)
853 (cond ((eq what 'del)
854 (imap-message-flags-del
855 (nnimap-range-to-string range)
856 (nnimap-mark-to-flag marks nil t)))
858 (imap-message-flags-add
859 (nnimap-range-to-string range)
860 (nnimap-mark-to-flag marks nil t)))
862 (imap-message-flags-set
863 (nnimap-range-to-string range)
864 (nnimap-mark-to-flag marks nil t)))))))
865 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
868 (defun nnimap-split-fancy ()
869 "Like nnmail-split-fancy, but uses nnimap-split-fancy."
870 (let ((nnmail-split-fancy nnimap-split-fancy))
871 (nnmail-split-fancy)))
873 (defun nnimap-split-to-groups (rules)
874 ;; tries to match all rules in nnimap-split-rule against content of
875 ;; nntp-server-buffer, returns a list of groups that matched.
876 (with-current-buffer nntp-server-buffer
877 ;; Fold continuation lines.
878 (goto-char (point-min))
879 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
880 (replace-match " " t t))
881 (if (functionp rules)
883 (let (to-groups regrepp)
885 (dolist (rule rules to-groups)
886 (let ((group (car rule))
887 (regexp (cadr rule)))
888 (goto-char (point-min))
889 (when (and (if (stringp regexp)
891 (setq regrepp (string-match "\\\\[0-9&]" group))
892 (re-search-forward regexp nil t))
893 (funcall regexp group))
894 ;; Don't enter the article into the same group twice.
895 (not (assoc group to-groups)))
897 (nnmail-expand-newtext group)
900 (or nnimap-split-crosspost
901 (throw 'split-done to-groups))))))))))
903 (defun nnimap-split-find-rule (server inbox)
906 (defun nnimap-split-find-inbox (server)
907 (if (listp nnimap-split-inbox)
909 (list nnimap-split-inbox)))
911 (defun nnimap-split-articles (&optional group server)
912 (when (nnimap-possibly-change-server server)
913 (with-current-buffer nnimap-server-buffer
914 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
915 ;; iterate over inboxes
916 (while (and (setq inbox (pop inboxes))
917 (nnimap-possibly-change-group inbox));; SELECT
918 ;; find split rule for this server / inbox
919 (when (setq rule (nnimap-split-find-rule server inbox))
920 ;; iterate over articles
921 (dolist (article (imap-search nnimap-split-predicate))
922 (when (nnimap-request-head article)
923 ;; copy article to right group(s)
924 (setq removeorig nil)
925 (dolist (to-group (nnimap-split-to-groups rule))
926 (if (imap-message-copy (number-to-string article)
927 to-group nil 'nocopyuid)
929 (message "IMAP split moved %s:%s:%d to %s" server inbox
932 ;; Add the group-art list to the history list.
933 (push (list (cons to-group 0)) nnmail-split-history))
934 (message "IMAP split failed to move %s:%s:%d to %s" server
935 inbox article to-group)))
936 ;; remove article if it was successfully copied somewhere
938 (imap-message-flags-add (format "%d" article)
939 "\\Seen \\Deleted")))))
940 (when (imap-mailbox-select inbox);; just in case
941 ;; todo: UID EXPUNGE (if available) to remove splitted articles
942 (imap-mailbox-expunge)
943 (imap-mailbox-close)))
946 (deffoo nnimap-request-scan (&optional group server)
947 (nnimap-split-articles group server))
949 (deffoo nnimap-request-newgroups (date &optional server)
950 (when (nnimap-possibly-change-server server)
951 (with-current-buffer nntp-server-buffer
952 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
953 (if (> (length server) 0) " on " "") server)
955 (dolist (pattern (nnimap-pattern-to-list-arguments
956 nnimap-list-pattern))
957 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
958 nnimap-server-buffer))
959 (or (member-if (lambda (mailbox)
960 (string= (downcase mailbox) "\\noselect"))
961 (imap-mailbox-get 'list-flags mbx
962 nnimap-server-buffer))
963 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
965 (insert (format "\"%s\" %d %d y\n"
966 mbx (or (nth 2 info) 0)
967 (max 1 (or (nth 1 info) 1)))))))))
968 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
969 (if (> (length server) 0) " on " "") server))
972 (deffoo nnimap-request-create-group (group &optional server args)
973 (when (nnimap-possibly-change-server server)
974 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
975 (imap-mailbox-create group nnimap-server-buffer))))
977 (defun nnimap-time-substract (time1 time2)
978 "Return TIME for TIME1 - TIME2."
979 (let* ((ms (- (car time1) (car time2)))
980 (ls (- (nth 1 time1) (nth 1 time2))))
982 (list (- ms 1) (+ (expt 2 16) ls))
985 (defun nnimap-date-days-ago (daysago)
986 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
987 (let ((date (format-time-string "%d-%b-%Y"
988 (nnimap-time-substract
990 (days-to-time daysago)))))
991 (if (eq ?0 (string-to-char date))
995 (defun nnimap-request-expire-articles-progress ()
996 (gnus-message 5 "nnimap: Marking article %d for deletion..."
997 imap-current-message))
999 ;; Notice that we don't actually delete anything, we just mark them deleted.
1000 (deffoo nnimap-request-expire-articles (articles group &optional server force)
1001 (let ((artseq (gnus-compress-sequence articles)))
1002 (when (and artseq (nnimap-possibly-change-group group server))
1003 (with-current-buffer nnimap-server-buffer
1005 (and (imap-message-flags-add
1006 (nnimap-range-to-string artseq) "\\Deleted")
1007 (setq articles nil))
1008 (let ((days (or (and nnmail-expiry-wait-function
1009 (funcall nnmail-expiry-wait-function group))
1010 nnmail-expiry-wait)))
1011 (cond ((eq days 'immediate)
1012 (and (imap-message-flags-add
1013 (nnimap-range-to-string artseq) "\\Deleted")
1014 (setq articles nil)))
1016 (let ((oldarts (imap-search
1017 (format "UID %s NOT SINCE %s"
1018 (nnimap-range-to-string artseq)
1019 (nnimap-date-days-ago days))))
1020 (imap-fetch-data-hook
1021 '(nnimap-request-expire-articles-progress)))
1023 (imap-message-flags-add
1024 (nnimap-range-to-string
1025 (gnus-compress-sequence oldarts))
1027 (setq articles (gnus-set-difference
1028 articles oldarts)))))))))))
1029 ;; return articles not deleted
1032 (deffoo nnimap-request-move-article (article group server
1033 accept-form &optional last)
1034 (when (nnimap-possibly-change-server server)
1036 (let ((buf (get-buffer-create " *nnimap move*"))
1037 (nnimap-current-move-article article)
1038 (nnimap-current-move-group group)
1039 (nnimap-current-move-server nnimap-current-server)
1041 (and (nnimap-request-article article group server)
1044 (buffer-disable-undo (current-buffer))
1045 (insert-buffer-substring nntp-server-buffer)
1046 (setq result (eval accept-form))
1049 (nnimap-request-expire-articles (list article) group server t))
1052 (deffoo nnimap-request-accept-article (group &optional server last)
1053 (when (nnimap-possibly-change-server server)
1056 (if (string= nnimap-current-server nnimap-current-move-server)
1057 ;; moving article within same server, speed it up...
1058 (and (nnimap-possibly-change-group
1059 nnimap-current-move-group)
1060 (imap-message-copy (number-to-string
1061 nnimap-current-move-article)
1062 group 'dontcreate nil
1063 nnimap-server-buffer))
1064 ;; turn into rfc822 format (\r\n eol's)
1065 (with-current-buffer (current-buffer)
1066 (goto-char (point-min))
1067 (while (search-forward "\n" nil t)
1068 (replace-match "\r\n")))
1069 ;; this 'or' is for Cyrus server bug
1070 (or (null (imap-current-mailbox nnimap-server-buffer))
1071 (imap-mailbox-unselect nnimap-server-buffer))
1072 (imap-message-append group (current-buffer) nil nil
1073 nnimap-server-buffer)))
1074 (cons group (nth 1 uid))
1075 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1077 (deffoo nnimap-request-delete-group (group force &optional server)
1078 (when (nnimap-possibly-change-server server)
1079 (with-current-buffer nnimap-server-buffer
1081 (or (null (imap-mailbox-status group 'uidvalidity))
1082 (imap-mailbox-delete group))
1086 (deffoo nnimap-request-rename-group (group new-name &optional server)
1087 (when (nnimap-possibly-change-server server)
1088 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1090 (defun nnimap-expunge (mailbox server)
1091 (when (nnimap-possibly-change-group mailbox server)
1092 (imap-mailbox-expunge nnimap-server-buffer)))
1094 (defun nnimap-acl-get (mailbox server)
1095 (when (nnimap-possibly-change-server server)
1096 (imap-mailbox-acl-get mailbox nnimap-server-buffer)))
1098 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1099 (when (nnimap-possibly-change-server (cadr method))
1100 (unless (imap-capability 'ACL nnimap-server-buffer)
1101 (error "Your server does not support ACL editing"))
1102 (with-current-buffer nnimap-server-buffer
1103 ;; delete all removed identifiers
1104 (mapcar (lambda (old-acl)
1105 (unless (assoc (car old-acl) new-acls)
1106 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1107 (error "Can't delete ACL for %s" (car old-acl)))))
1109 ;; set all changed acl's
1110 (mapcar (lambda (new-acl)
1111 (let ((new-rights (cdr new-acl))
1112 (old-rights (cdr (assoc (car new-acl) old-acls))))
1113 (unless (and old-rights new-rights
1114 (string= old-rights new-rights))
1115 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1116 (error "Can't set ACL for %s to %s" (car new-acl)
1122 ;;; Internal functions
1125 ;; This is confusing.
1127 ;; mark => read, tick, draft, reply etc
1128 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1129 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1131 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1132 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1135 (defconst nnimap-mark-to-predicate-alist
1137 (lambda (pair) ; cdr is the mark
1138 (or (assoc (cdr pair)
1142 (reply . "ANSWERED")))
1144 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1145 (cons '(read . read) gnus-article-mark-lists)))
1147 (defun nnimap-mark-to-predicate (pred)
1148 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1149 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1150 to be used within a IMAP SEARCH query."
1151 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1153 (defconst nnimap-mark-to-flag-alist
1156 (or (assoc (cdr pair)
1158 (tick . "\\Flagged")
1160 (reply . "\\Answered")))
1162 (format "gnus-%s" (symbol-name (cdr pair))))))
1163 (cons '(read . read) gnus-article-mark-lists)))
1165 (defun nnimap-mark-to-flag-1 (preds)
1166 (if (and (not (null preds)) (listp preds))
1167 (cons (nnimap-mark-to-flag (car preds))
1168 (nnimap-mark-to-flag (cdr preds)))
1169 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1171 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1172 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1173 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1174 be used in a STORE FLAGS command."
1175 (let ((result (nnimap-mark-to-flag-1 preds)))
1176 (setq result (if (and (or make-string always-list)
1177 (not (listp result)))
1181 (mapconcat (lambda (flag)
1183 (mapconcat 'identity flag " ")
1188 (defun nnimap-mark-permanent-p (mark &optional group)
1189 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1190 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1192 (defun nnimap-remassoc (key alist)
1193 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1194 The modified LIST is returned. If the first member
1195 of LIST has a car that is `equal' to KEY, there is no way to remove it
1196 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1197 sure of changing the value of `foo'."
1199 (if (equal key (caar alist))
1201 (setcdr alist (nnimap-remassoc key (cdr alist)))
1204 (defun nnimap-update-alist-soft (key value alist)
1206 (cons (cons key value) (nnimap-remassoc key alist))
1207 (nnimap-remassoc key alist)))
1209 (defun nnimap-range-to-string (range)
1214 (car item) (cdr item))
1215 (format "%d" item)))
1216 (if (and (listp range) (not (listp (cdr range))))
1217 (list range);; make (1 . 2) into ((1 . 2))
1223 (buffer-disable-undo (get-buffer-create nnimap-debug))
1224 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1226 nnimap-possibly-change-server
1227 nnimap-verify-uidvalidity
1228 nnimap-find-minmax-uid
1229 nnimap-possibly-change-group
1230 ;;nnimap-replace-whitespace
1231 nnimap-retrieve-headers-progress
1232 nnimap-retrieve-which-headers
1233 nnimap-group-overview-filename
1234 nnimap-retrieve-headers-from-file
1235 nnimap-retrieve-headers-from-server
1236 nnimap-retrieve-headers
1237 nnimap-open-connection
1239 nnimap-server-opened
1241 nnimap-request-close
1242 nnimap-status-message
1244 nnimap-request-article-part
1245 nnimap-request-article
1248 nnimap-request-group
1250 nnimap-pattern-to-list-arguments
1253 nnimap-retrieve-groups
1254 nnimap-request-update-info-internal
1256 nnimap-request-set-mark
1257 nnimap-split-to-groups
1258 nnimap-split-find-rule
1259 nnimap-split-find-inbox
1260 nnimap-split-articles
1262 nnimap-request-newgroups
1263 nnimap-request-create-group
1264 nnimap-time-substract
1265 nnimap-date-days-ago
1266 nnimap-request-expire-articles-progress
1267 nnimap-request-expire-articles
1268 nnimap-request-move-article
1269 nnimap-request-accept-article
1270 nnimap-request-delete-group
1271 nnimap-request-rename-group
1272 gnus-group-nnimap-expunge
1273 gnus-group-nnimap-edit-acl
1274 gnus-group-nnimap-edit-acl-done
1275 nnimap-group-mode-hook
1276 nnimap-mark-to-predicate
1277 nnimap-mark-to-flag-1
1279 nnimap-mark-permanent-p
1281 nnimap-update-alist-soft
1282 nnimap-range-to-string
1287 ;;; nnimap.el ends here