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 ;; Authorization / Privacy variables
127 (defvoo nnimap-auth-method nil
130 (defvoo nnimap-stream nil
131 "How nnimap will connect to the server.
133 The default, nil, will try to use the \"best\" method the server can
138 1) you want to connect with SSL. The SSL integration with IMAP is
139 brain-dead so you'll have to tell it specifically.
141 2) your server is more capable than your environment -- i.e. your
142 server accept Kerberos login's but you haven't installed the
143 `imtest' program or your machine isn't configured for Kerberos.
145 Possible choices: kerberos4, ssl, network")
147 (defvoo nnimap-authenticator nil
148 "How nnimap authenticate itself to the server.
150 The default, nil, will try to use the \"best\" method the server can
153 There is only one reason for fiddling with this variable, and that is
154 if your server is more capable than your environment -- i.e. you
155 connect to a server that accept Kerberos login's but you haven't
156 installed the `imtest' program or your machine isn't configured for
159 Possible choices: kerberos4, cram-md5, login, anonymous.")
161 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
162 "Directory to keep NOV cache files for nnimap groups.
163 See also `nnimap-nov-file-name'.")
165 (defvoo nnimap-nov-file-name "nnimap."
166 "NOV cache base filename.
167 The group name and `nnimap-nov-file-name-suffix' will be appended. A
168 typical complete file name would be
169 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
170 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
171 `nnmail-use-long-file-names' is nil")
173 (defvoo nnimap-nov-file-name-suffix ".novcache"
174 "Suffix for NOV cache base filename.")
176 (defvoo nnimap-nov-is-evil nil
177 "If non-nil, nnimap will never generate or use a local nov database for this backend.
178 Using nov databases will speed up header fetching considerably.
179 Unlike other backends, you do not need to take special care if you
180 flip this variable.")
182 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
183 "Whether to expunge a group when it is closed.
184 When a IMAP group with articles marked for deletion is closed, this
185 variable determine if nnimap should actually remove the articles or
188 If always, nnimap always perform a expunge when closing the group.
189 If never, nnimap never expunges articles marked for deletion.
190 If ask, nnimap will ask you if you wish to expunge marked articles.
192 When setting this variable to `never', you can only expunge articles
193 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
195 (defvoo nnimap-list-pattern "*"
196 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
197 See below for available wildcards.
199 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
200 REFERENCE will be passed as the first parameter to LIST/LSUB. The
201 semantics of this are server specific, on the University of Washington
202 server you can specify a directory.
205 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
207 There are two wildcards * and %. * matches everything, % matches
208 everything in the current hierarchy.")
210 (defvoo nnimap-news-groups nil
211 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
213 This variable should contain a regexp matching groups where you wish
214 replies to be stored to the mailbox directly.
217 '(\"^[^I][^N][^B][^O][^X].*$\")
219 This will match all groups not beginning with \"INBOX\".
221 Note that there is nothing technically different between mail-like and
222 news-like mailboxes. If you wish to have a group with todo items or
223 similar which you wouldn't want to set up a mailing list for, you can
224 use this to make replies go directly to the group.")
226 (defvoo nnimap-server-address nil
227 "Obsolete. Use `nnimap-address'.")
229 (defcustom nnimap-authinfo-file "~/.authinfo"
230 "Authorization information for IMAP servers. In .netrc format."
233 (repeat :tag "Entries"
236 :value ("" ("login" . "") ("password" . ""))
240 (const :format "" "login")
241 (string :format "Login: %v"))
243 (const :format "" "password")
244 (string :format "Password: %v")))))))
246 (defcustom nnimap-prune-cache t
247 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
250 (defvar nnimap-request-list-method 'imap-mailbox-list
251 "Method to use to request a list of all folders from the server.
252 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
253 restrict visible folders.")
255 ;; Internal variables:
257 (defvar nnimap-debug nil);; "*nnimap-debug*")
258 (defvar nnimap-current-move-server nil)
259 (defvar nnimap-current-move-group nil)
260 (defvar nnimap-current-move-article nil)
261 (defvar nnimap-length)
262 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
263 (defvar nnimap-progress-how-often 20)
264 (defvar nnimap-counter)
265 (defvar nnimap-callback-callback-function nil
266 "Gnus callback the nnimap asynchronous callback should call.")
267 (defvar nnimap-callback-buffer nil
268 "Which buffer the asynchronous article prefetch callback should work in.")
269 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
270 (defvar nnimap-current-server nil) ;; Current server
271 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
275 (nnoo-define-basics nnimap)
277 ;; Utility functions:
279 (defsubst nnimap-get-server-buffer (server)
280 "Return buffer for SERVER, if nil use current server."
281 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
283 (defun nnimap-possibly-change-server (server)
284 "Return buffer for SERVER, changing the current server as a side-effect.
285 If SERVER is nil, uses the current server."
286 (setq nnimap-current-server (or server nnimap-current-server)
287 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
289 (defun nnimap-verify-uidvalidity (group server)
290 "Verify stored uidvalidity match current one in GROUP on SERVER."
291 (let* ((gnusgroup (gnus-group-prefixed-name
292 group (gnus-server-to-method
293 (format "nnimap:%s" server))))
294 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
295 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity)))
297 (if (not (equal old-uidvalidity new-uidvalidity))
298 nil ;; uidvalidity clash
299 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
301 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
304 (defun nnimap-find-minmax-uid (group &optional examine)
305 "Find lowest and highest active article nummber in GROUP.
306 If EXAMINE is non-nil the group is selected read-only."
307 (with-current-buffer nnimap-server-buffer
308 (when (imap-mailbox-select group examine)
310 (when (> (imap-mailbox-get 'exists) 0)
311 (imap-fetch "1,*" "UID" nil 'nouidfetch)
312 (imap-message-map (lambda (uid Uid)
313 (setq minuid (if minuid (min minuid uid) uid)
314 maxuid (if maxuid (max maxuid uid) uid)))
316 (list (imap-mailbox-get 'exists) minuid maxuid)))))
318 (defun nnimap-possibly-change-group (group &optional server)
319 "Make GROUP the current group, and SERVER the current server."
320 (when (nnimap-possibly-change-server server)
321 (with-current-buffer nnimap-server-buffer
322 (if (or (null group) (imap-current-mailbox-p group))
324 (if (imap-mailbox-select group)
325 (if (or (nnimap-verify-uidvalidity
326 group (or server nnimap-current-server))
327 (zerop (imap-mailbox-get 'exists group))
330 "nnimap: Group %s is not uidvalid. Continue? " group)))
332 (imap-mailbox-unselect)
333 (error "nnimap: Group %s is not uid-valid." group))
334 (nnheader-report 'nnimap (imap-error-text)))))))
336 (defun nnimap-replace-whitespace (string)
337 "Return STRING with all whitespace replaced with space."
339 (while (string-match "[\r\n\t]+" string)
340 (setq string (replace-match " " t t string)))
343 ;; Required backend functions
345 (defun nnimap-retrieve-headers-progress ()
346 "Hook to insert NOV line for current article into `nntp-server-buffer'."
347 (and (numberp nnmail-large-newsgroup)
348 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
349 (> nnimap-length nnmail-large-newsgroup)
350 (nnheader-message 6 "nnimap: Retrieving headers... %c"
351 (nth (/ (% nnimap-counter
352 (* (length nnimap-progress-chars)
353 nnimap-progress-how-often))
354 nnimap-progress-how-often)
355 nnimap-progress-chars)))
356 (with-current-buffer nntp-server-buffer
357 (let (headers lines chars uid)
358 (with-current-buffer nnimap-server-buffer
359 (setq uid imap-current-message
360 headers (if (imap-capability 'IMAP4rev1)
361 ;; xxx don't just use car? alist doesn't contain
362 ;; anything else now, but it might...
363 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
364 (imap-message-get uid 'RFC822.HEADER))
365 lines (imap-body-lines (imap-message-body imap-current-message))
366 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
369 (buffer-disable-undo)
371 (nnheader-ms-strip-cr)
372 (let ((head (nnheader-parse-head 'naked)))
373 (mail-header-set-number head uid)
374 (mail-header-set-chars head chars)
375 (mail-header-set-lines head lines)
378 (defun nnimap-retrieve-which-headers (articles fetch-old)
379 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
380 (with-current-buffer nnimap-server-buffer
381 (if (numberp (car-safe articles))
384 (nnimap-range-to-string
385 (gnus-compress-sequence
386 (append (gnus-uncompress-sequence
388 (cons (if (numberp fetch-old)
389 (max 1 (- (car articles) fetch-old))
391 (1- (car articles)))))
393 (mapcar (lambda (msgid)
395 (format "HEADER Message-Id %s" msgid)))
398 (defun nnimap-group-overview-filename (group server)
399 "Make pathname for GROUP on SERVER."
400 (let ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
401 (file (nnheader-translate-file-chars
402 (concat nnimap-nov-file-name
403 (if (equal server "")
405 server) "." group nnimap-nov-file-name-suffix) t)))
406 (if (or nnmail-use-long-file-names
407 (file-exists-p (concat dir file)))
409 (concat dir (mm-encode-coding-string
410 (nnheader-replace-chars-in-string file ?. ?/)
411 nnmail-pathname-coding-system)))))
413 (defun nnimap-retrieve-headers-from-file (group server)
414 (with-current-buffer nntp-server-buffer
415 (let ((nov (nnimap-group-overview-filename group server)))
416 (when (file-exists-p nov)
417 (mm-insert-file-contents nov)
418 (set-buffer-modified-p nil)
419 (let ((min (progn (goto-char (point-min))
421 (read (current-buffer)))))
422 (max (progn (goto-char (point-max))
425 (read (current-buffer))))))
426 (if (and (numberp min) (numberp max))
428 ;; junk, remove it, it's saved later
432 (defun nnimap-retrieve-headers-from-server (articles group server)
433 (with-current-buffer nnimap-server-buffer
434 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
435 (nnimap-length (gnus-range-length articles))
437 (imap-fetch (nnimap-range-to-string articles)
438 (concat "(UID RFC822.SIZE BODY "
440 (append '(Subject From Date Message-Id
441 References In-Reply-To Xref)
443 nnmail-extra-headers))))
444 (if (imap-capability 'IMAP4rev1)
445 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
446 (format "RFC822.HEADER.LINES %s)" headers)))))
447 (and (numberp nnmail-large-newsgroup)
448 (> nnimap-length nnmail-large-newsgroup)
449 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
451 (defun nnimap-use-nov-p (group server)
452 (or gnus-nov-is-evil nnimap-nov-is-evil
453 (unless (and (gnus-make-directory
455 (nnimap-group-overview-filename group server)))
457 (nnimap-group-overview-filename group server)))
458 (message "nnimap: Nov cache not writable, %s"
459 (nnimap-group-overview-filename group server)))))
461 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
462 (when (nnimap-possibly-change-group group server)
463 (with-current-buffer nntp-server-buffer
465 (if (nnimap-use-nov-p group server)
466 (nnimap-retrieve-headers-from-server
467 (gnus-compress-sequence articles) group server)
468 (let (uids cached low high)
469 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
471 high (car (last uids)))
472 (if (setq cached (nnimap-retrieve-headers-from-file group server))
474 ;; fetch articles with uids before cache block
475 (when (< low (car cached))
476 (goto-char (point-min))
477 (nnimap-retrieve-headers-from-server
478 (cons low (1- (car cached))) group server))
479 ;; fetch articles with uids after cache block
480 (when (> high (cdr cached))
481 (goto-char (point-max))
482 (nnimap-retrieve-headers-from-server
483 (cons (1+ (cdr cached)) high) group server))
484 (when nnimap-prune-cache
485 ;; remove nov's for articles which has expired on server
486 (goto-char (point-min))
487 (dolist (uid (gnus-set-difference articles uids))
488 (when (re-search-forward (format "^%d\t" uid) nil t)
489 (gnus-delete-line)))))
490 ;; nothing cached, fetch whole range from server
491 (nnimap-retrieve-headers-from-server
492 (cons low high) group server))
493 (when (buffer-modified-p)
495 1 (point-max) (nnimap-group-overview-filename group server)
497 (nnheader-nov-delete-outside-range low high))))
500 (defun nnimap-open-connection (server)
501 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
502 nnimap-authenticator nnimap-server-buffer))
503 (nnheader-report 'nnimap "Can't open connection to server %s" server)
504 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
505 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
506 (imap-close nnimap-server-buffer)
507 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
508 (let (list alist user passwd)
509 (and (fboundp 'gnus-parse-netrc)
510 (setq list (gnus-parse-netrc nnimap-authinfo-file)
511 alist (or (and (gnus-netrc-get
512 (gnus-netrc-machine list server) "machine")
513 (gnus-netrc-machine list server))
514 (gnus-netrc-machine list nnimap-address))
515 user (gnus-netrc-get alist "login")
516 passwd (gnus-netrc-get alist "password")))
517 (if (imap-authenticate user passwd nnimap-server-buffer)
519 (push (list server nnimap-server-buffer)
520 nnimap-server-buffer-alist)
521 (nnimap-possibly-change-server server))
522 (imap-close nnimap-server-buffer)
523 (kill-buffer nnimap-server-buffer)
524 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
526 (deffoo nnimap-open-server (server &optional defs)
527 (nnheader-init-server-buffer)
528 (if (nnimap-server-opened server)
530 (unless (assq 'nnimap-server-buffer defs)
531 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
532 ;; translate `nnimap-server-address' to `nnimap-address' in defs
533 ;; for people that configured nnimap with a very old version
534 (unless (assq 'nnimap-address defs)
535 (if (assq 'nnimap-server-address defs)
536 (push (list 'nnimap-address
537 (cadr (assq 'nnimap-server-address defs))) defs)
538 (push (list 'nnimap-address server) defs)))
539 (nnoo-change-server 'nnimap server defs)
540 (or (and nnimap-server-buffer
541 (imap-opened nnimap-server-buffer))
542 (nnimap-open-connection server))))
544 (deffoo nnimap-server-opened (&optional server)
545 "Whether SERVER is opened.
546 If SERVER is the current virtual server, and the connection to the
547 physical server is alive, this function return a non-nil value. If
548 SERVER is nil, it is treated as the current server."
549 ;; clean up autologouts??
550 (and (or server nnimap-current-server)
551 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
552 (imap-opened (nnimap-get-server-buffer server))))
554 (deffoo nnimap-close-server (&optional server)
555 "Close connection to server and free all resources connected to it.
556 Return nil if the server couldn't be closed for some reason."
557 (let ((server (or server nnimap-current-server)))
558 (when (or (nnimap-server-opened server)
559 (imap-opened (nnimap-get-server-buffer server)))
560 (imap-close (nnimap-get-server-buffer server))
561 (kill-buffer (nnimap-get-server-buffer server))
562 (setq nnimap-server-buffer nil
563 nnimap-current-server nil
564 nnimap-server-buffer-alist
565 (delq server nnimap-server-buffer-alist)))
566 (nnoo-close-server 'nnimap server)))
568 (deffoo nnimap-request-close ()
569 "Close connection to all servers and free all resources that the backend have reserved.
570 All buffers that have been created by that
571 backend should be killed. (Not the nntp-server-buffer, though.) This
572 function is generally only called when Gnus is shutting down."
573 (mapcar (lambda (server) (nnimap-close-server (car server)))
574 nnimap-server-buffer-alist)
575 (setq nnimap-server-buffer-alist nil))
577 (deffoo nnimap-status-message (&optional server)
578 "This function returns the last error message from server."
579 (when (nnimap-possibly-change-server server)
580 (nnoo-status-message 'nnimap server)))
582 (defun nnimap-demule (string)
583 (funcall (if (and (fboundp 'string-as-multibyte)
584 (subrp (symbol-function 'string-as-multibyte)))
589 (defun nnimap-callback ()
590 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
591 (with-current-buffer nnimap-callback-buffer
593 (with-current-buffer nnimap-server-buffer
594 (nnimap-demule (imap-message-get (imap-current-message) 'RFC822)))) ;xxx
595 (nnheader-ms-strip-cr)
596 (funcall nnimap-callback-callback-function t)))
598 (defun nnimap-request-article-part (article part prop
599 &optional group server to-buffer)
600 (when (nnimap-possibly-change-group group server)
601 (let ((article (if (stringp article)
602 (car-safe (imap-search
603 (format "HEADER Message-Id %s" article)
604 nnimap-server-buffer))
607 (gnus-message 9 "nnimap: Fetching (part of) article %d..." article)
608 (if (not nnheader-callback-function)
609 (with-current-buffer (or to-buffer nntp-server-buffer)
611 (insert (nnimap-demule (imap-fetch article part prop nil
612 nnimap-server-buffer)))
613 (nnheader-ms-strip-cr)
614 (gnus-message 9 "nnimap: Fetching (part of) article %d...done"
617 (nnheader-report 'nnimap "No such article: %s"
618 (imap-error-text nnimap-server-buffer))
619 (cons group article)))
620 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
621 (setq nnimap-callback-callback-function nnheader-callback-function
622 nnimap-callback-buffer nntp-server-buffer)
623 (imap-fetch-asynch article part nil nnimap-server-buffer)
624 (cons group article))))))
626 (deffoo nnimap-asynchronous-p ()
629 (deffoo nnimap-request-article (article &optional group server to-buffer)
630 (nnimap-request-article-part
631 article "RFC822.PEEK" 'RFC822 group server to-buffer))
633 (deffoo nnimap-request-head (article &optional group server to-buffer)
634 (nnimap-request-article-part
635 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer))
637 (deffoo nnimap-request-body (article &optional group server to-buffer)
638 (nnimap-request-article-part
639 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer))
641 (deffoo nnimap-request-group (group &optional server fast)
642 (nnimap-request-update-info-internal
644 (gnus-get-info (gnus-group-prefixed-name
645 group (gnus-server-to-method (format "nnimap:%s" server))))
647 (when (nnimap-possibly-change-group group server)
650 ((null (setq info (nnimap-find-minmax-uid group t)))
651 (nnheader-report 'nnimap "Could not get active info for %s"
654 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
655 (max 1 (or (nth 1 info) 1))
656 (or (nth 2 info) 0) group)
657 (nnheader-report 'nnimap "Group %s selected" group)
660 (defun nnimap-close-group (group &optional server)
661 (with-current-buffer nnimap-server-buffer
662 (when (and (imap-opened)
663 (nnimap-possibly-change-group group server))
664 (case nnimap-expunge-on-close
665 ('always (imap-mailbox-expunge)
666 (imap-mailbox-close))
667 ('ask (if (and (imap-search "DELETED")
668 (gnus-y-or-n-p (format
669 "Expunge articles in group `%s'? "
670 imap-current-mailbox)))
671 (progn (imap-mailbox-expunge)
672 (imap-mailbox-close))
673 (imap-mailbox-unselect)))
674 (t (imap-mailbox-unselect)))
675 (not imap-current-mailbox))))
677 (defun nnimap-pattern-to-list-arguments (pattern)
679 (cons (car-safe p) (or (cdr-safe p) p)))
680 (if (and (listp pattern)
681 (listp (cdr pattern)))
685 (deffoo nnimap-request-list (&optional server)
686 (when (nnimap-possibly-change-server server)
687 (with-current-buffer nntp-server-buffer
689 (gnus-message 5 "nnimap: Generating active list%s..."
690 (if (> (length server) 0) (concat " for " server) ""))
691 (with-current-buffer nnimap-server-buffer
692 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
693 (dolist (mbx (funcall nnimap-request-list-method
694 (cdr pattern) (car pattern)))
695 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
696 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
698 (with-current-buffer nntp-server-buffer
699 (insert (format "\"%s\" %d %d y\n"
700 mbx (or (nth 2 info) 0)
701 (max 1 (or (nth 1 info) 1)))))))))))
702 (gnus-message 5 "nnimap: Generating active list%s...done"
703 (if (> (length server) 0) (concat " for " server) ""))
706 (deffoo nnimap-request-post (&optional server)
708 (dolist (mbx (message-tokenize-header
709 (message-fetch-field "Newsgroups")) success)
710 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
711 (or (gnus-active to-newsgroup)
712 (gnus-activate-group to-newsgroup)
713 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
715 (or (and (gnus-request-create-group
716 to-newsgroup gnus-command-method)
717 (gnus-activate-group to-newsgroup nil nil
718 gnus-command-method))
719 (error "Couldn't create group %s" to-newsgroup)))
720 (error "No such group: %s" to-newsgroup))
721 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
722 (setq success nil))))))
724 ;; Optional backend functions
726 (deffoo nnimap-retrieve-groups (groups &optional server)
727 (when (nnimap-possibly-change-server server)
728 (gnus-message 5 "nnimap: Checking mailboxes...")
729 (with-current-buffer nntp-server-buffer
731 (dolist (group groups)
732 (gnus-message 7 "nnimap: Checking mailbox %s" group)
733 (or (member "\\NoSelect"
734 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
735 (let ((info (nnimap-find-minmax-uid group 'examine)))
736 (insert (format "\"%s\" %d %d y\n" group
738 (max 1 (or (nth 1 info) 1))))))))
739 (gnus-message 5 "nnimap: Checking mailboxes...done")
742 (deffoo nnimap-request-update-info-internal (group info &optional server)
743 (when (nnimap-possibly-change-group group server)
744 (when info;; xxx what does this mean? should we create a info?
745 (with-current-buffer nnimap-server-buffer
746 (gnus-message 5 "nnimap: Updating info for %s..."
747 (gnus-info-group info))
749 (when (nnimap-mark-permanent-p 'read)
751 ;; read info could contain articles marked unread by other
752 ;; imap clients! we correct this
753 (setq seen (gnus-uncompress-range (gnus-info-read info))
754 unseen (imap-search "UNSEEN UNDELETED")
755 seen (gnus-set-difference seen unseen)
756 ;; seen might lack articles marked as read by other
757 ;; imap clients! we correct this
758 seen (append seen (imap-search "SEEN"))
761 seen (gnus-compress-sequence seen t)
762 ;; we can't return '(1) since this isn't a "list of ranges",
763 ;; and we can't return '((1)) since g-list-of-unread-articles
764 ;; is buggy so we return '((1 . 1)).
765 seen (if (and (integerp (car seen))
767 (list (cons (car seen) (car seen)))
769 (gnus-info-set-read info seen)))
771 (mapcar (lambda (pred)
772 (when (and (nnimap-mark-permanent-p (cdr pred))
773 (member (nnimap-mark-to-flag (cdr pred))
774 (imap-mailbox-get 'flags)))
777 (nnimap-update-alist-soft
779 (gnus-compress-sequence
780 (imap-search (nnimap-mark-to-predicate (cdr pred))))
781 (gnus-info-marks info))
783 gnus-article-mark-lists)
785 (gnus-message 5 "nnimap: Updating info for %s...done"
786 (gnus-info-group info))
790 (deffoo nnimap-request-type (group &optional article)
791 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
795 (deffoo nnimap-request-set-mark (group actions &optional server)
796 (when (nnimap-possibly-change-group group server)
797 (with-current-buffer nnimap-server-buffer
799 (gnus-message 7 "nnimap: Setting marks in %s..." group)
800 (while (setq action (pop actions))
801 (let ((range (nth 0 action))
802 (what (nth 1 action))
803 (cmdmarks (nth 2 action))
805 ;; cache flags are pointless on the server
806 (setq cmdmarks (delq 'cache cmdmarks))
807 ;; flag dormant articles as ticked
808 (if (memq 'dormant cmdmarks)
809 (setq cmdmarks (cons 'tick cmdmarks)))
810 ;; remove stuff we are forbidden to store
811 (mapcar (lambda (mark)
812 (if (imap-message-flag-permanent-p
813 (nnimap-mark-to-flag mark))
814 (setq marks (cons mark marks))))
816 (when (and range marks)
817 (cond ((eq what 'del)
818 (imap-message-flags-del
819 (nnimap-range-to-string range)
820 (nnimap-mark-to-flag marks nil t)))
822 (imap-message-flags-add
823 (nnimap-range-to-string range)
824 (nnimap-mark-to-flag marks nil t)))
826 (imap-message-flags-set
827 (nnimap-range-to-string range)
828 (nnimap-mark-to-flag marks nil t)))))))
829 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
832 (defun nnimap-split-to-groups (rules)
833 ;; tries to match all rules in nnimap-split-rule against content of
834 ;; nntp-server-buffer, returns a list of groups that matched.
835 (with-current-buffer nntp-server-buffer
836 ;; Fold continuation lines.
837 (goto-char (point-min))
838 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
839 (replace-match " " t t))
840 (if (functionp rules)
842 (let (to-groups regrepp)
844 (dolist (rule rules to-groups)
845 (let ((group (car rule))
846 (regexp (cadr rule)))
847 (goto-char (point-min))
848 (when (and (if (stringp regexp)
850 (setq regrepp (string-match "\\\\[0-9&]" group))
851 (re-search-forward regexp nil t))
852 (funcall regexp group))
853 ;; Don't enter the article into the same group twice.
854 (not (assoc group to-groups)))
856 (nnmail-expand-newtext group)
859 (or nnimap-split-crosspost
860 (throw 'split-done to-groups))))))))))
862 (defun nnimap-split-find-rule (server inbox)
865 (defun nnimap-split-find-inbox (server)
866 (if (listp nnimap-split-inbox)
868 (list nnimap-split-inbox)))
870 (defun nnimap-split-articles (&optional group server)
871 (when (nnimap-possibly-change-server server)
872 (with-current-buffer nnimap-server-buffer
873 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
874 ;; iterate over inboxes
875 (while (and (setq inbox (pop inboxes))
876 (nnimap-possibly-change-group inbox));; SELECT
877 ;; find split rule for this server / inbox
878 (when (setq rule (nnimap-split-find-rule server inbox))
879 ;; iterate over articles
880 (dolist (article (imap-search "UNSEEN UNDELETED"))
881 (when (nnimap-request-head article)
882 ;; copy article to right group(s)
883 (setq removeorig nil)
884 (dolist (to-group (nnimap-split-to-groups rule))
885 (if (imap-message-copy (number-to-string article)
886 to-group nil 'nocopyuid)
888 (message "IMAP split moved %s:%s:%d to %s" server inbox
891 ;; Add the group-art list to the history list.
892 (push (list (cons to-group 0)) nnmail-split-history))
893 (message "IMAP split failed to move %s:%s:%d to %s" server
894 inbox article to-group)))
895 ;; remove article if it was successfully copied somewhere
897 (imap-message-flags-add (format "%d" article)
898 "\\Seen \\Deleted")))))
899 (when (imap-mailbox-select inbox);; just in case
900 ;; todo: UID EXPUNGE (if available) to remove splitted articles
901 (imap-mailbox-expunge)
902 (imap-mailbox-close)))
905 (deffoo nnimap-request-scan (&optional group server)
906 (nnimap-split-articles group server))
908 (deffoo nnimap-request-newgroups (date &optional server)
909 (when (nnimap-possibly-change-server server)
910 (with-current-buffer nntp-server-buffer
911 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
912 (if (> (length server) 0) " on " "") server)
914 (dolist (pattern (nnimap-pattern-to-list-arguments
915 nnimap-list-pattern))
916 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
917 nnimap-server-buffer))
918 (or (member-if (lambda (mailbox)
919 (string= (downcase mailbox) "\\noselect"))
920 (imap-mailbox-get 'list-flags mbx
921 nnimap-server-buffer))
922 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
924 (insert (format "\"%s\" %d %d y\n"
925 mbx (or (nth 2 info) 0)
926 (max 1 (or (nth 1 info) 1)))))))))
927 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
928 (if (> (length server) 0) " on " "") server))
931 (deffoo nnimap-request-create-group (group &optional server args)
932 (when (nnimap-possibly-change-server server)
933 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
934 (imap-mailbox-create group nnimap-server-buffer))))
936 (defun nnimap-time-substract (time1 time2)
937 "Return TIME for TIME1 - TIME2."
938 (let* ((ms (- (car time1) (car time2)))
939 (ls (- (nth 1 time1) (nth 1 time2))))
941 (list (- ms 1) (+ (expt 2 16) ls))
944 (defun nnimap-date-days-ago (daysago)
945 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
946 (let ((date (format-time-string "%d-%b-%Y"
947 (nnimap-time-substract
949 (days-to-time daysago)))))
950 (if (eq ?0 (string-to-char date))
954 (defun nnimap-request-expire-articles-progress ()
955 (gnus-message 5 "nnimap: Marking article %d for deletion..."
956 imap-current-message))
958 ;; Notice that we don't actually delete anything, we just mark them deleted.
959 (deffoo nnimap-request-expire-articles (articles group &optional server force)
960 (let ((artseq (gnus-compress-sequence articles)))
961 (when (and artseq (nnimap-possibly-change-group group server))
962 (with-current-buffer nnimap-server-buffer
964 (and (imap-message-flags-add
965 (nnimap-range-to-string artseq) "\\Deleted")
967 (let ((days (or (and nnmail-expiry-wait-function
968 (funcall nnmail-expiry-wait-function group))
969 nnmail-expiry-wait)))
970 (cond ((eq days 'immediate)
971 (and (imap-message-flags-add
972 (nnimap-range-to-string artseq) "\\Deleted")
973 (setq articles nil)))
975 (let ((oldarts (imap-search
976 (format "UID %s NOT SINCE %s"
977 (nnimap-range-to-string artseq)
978 (nnimap-date-days-ago days))))
979 (imap-fetch-data-hook
980 '(nnimap-request-expire-articles-progress)))
982 (imap-message-flags-add
983 (nnimap-range-to-string
984 (gnus-compress-sequence oldarts))
986 (setq articles (gnus-set-difference
987 articles oldarts)))))))))))
988 ;; return articles not deleted
991 (deffoo nnimap-request-move-article (article group server
992 accept-form &optional last)
993 (when (nnimap-possibly-change-server server)
995 (let ((buf (get-buffer-create " *nnimap move*"))
996 (nnimap-current-move-article article)
997 (nnimap-current-move-group group)
998 (nnimap-current-move-server nnimap-current-server)
1000 (and (nnimap-request-article article group server)
1003 (buffer-disable-undo (current-buffer))
1004 (insert-buffer-substring nntp-server-buffer)
1005 (setq result (eval accept-form))
1008 (nnimap-request-expire-articles (list article) group server t))
1011 (deffoo nnimap-request-accept-article (group &optional server last)
1012 (when (nnimap-possibly-change-server server)
1015 (if (string= nnimap-current-server nnimap-current-move-server)
1016 ;; moving article within same server, speed it up...
1017 (and (nnimap-possibly-change-group
1018 nnimap-current-move-group)
1019 (imap-message-copy (number-to-string
1020 nnimap-current-move-article)
1021 group 'dontcreate nil
1022 nnimap-server-buffer))
1023 ;; turn into rfc822 format (\r\n eol's)
1024 (with-current-buffer (current-buffer)
1025 (goto-char (point-min))
1026 (while (search-forward "\n" nil t)
1027 (replace-match "\r\n")))
1028 ;; next line for Cyrus server bug
1029 (imap-mailbox-unselect nnimap-server-buffer)
1030 (imap-message-append group (current-buffer) nil nil
1031 nnimap-server-buffer)))
1032 (cons group (nth 1 uid))
1033 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1035 (deffoo nnimap-request-delete-group (group force &optional server)
1036 (when (nnimap-possibly-change-server server)
1037 (with-current-buffer nnimap-server-buffer
1039 (or (null (imap-mailbox-status group 'uidvalidity))
1040 (imap-mailbox-delete group))
1044 (deffoo nnimap-request-rename-group (group new-name &optional server)
1045 (when (nnimap-possibly-change-server server)
1046 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1048 (defun nnimap-expunge (mailbox server)
1049 (when (nnimap-possibly-change-group mailbox server)
1050 (imap-mailbox-expunge nnimap-server-buffer)))
1052 (defun nnimap-acl-get (mailbox server)
1053 (when (nnimap-possibly-change-server server)
1054 (imap-mailbox-acl-get mailbox nnimap-server-buffer)))
1056 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1057 (when (nnimap-possibly-change-server (cadr method))
1058 (unless (imap-capability 'ACL nnimap-server-buffer)
1059 (error "Your server does not support ACL editing"))
1060 (with-current-buffer nnimap-server-buffer
1061 ;; delete all removed identifiers
1062 (mapcar (lambda (old-acl)
1063 (unless (assoc (car old-acl) new-acls)
1064 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1065 (error "Can't delete ACL for %s" (car old-acl)))))
1067 ;; set all changed acl's
1068 (mapcar (lambda (new-acl)
1069 (let ((new-rights (cdr new-acl))
1070 (old-rights (cdr (assoc (car new-acl) old-acls))))
1071 (unless (and old-rights new-rights
1072 (string= old-rights new-rights))
1073 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1074 (error "Can't set ACL for %s to %s" (car new-acl)
1080 ;;; Internal functions
1083 ;; This is confusing.
1085 ;; mark => read, tick, draft, reply etc
1086 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1087 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1089 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1090 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1093 (defconst nnimap-mark-to-predicate-alist
1095 (lambda (pair) ; cdr is the mark
1096 (or (assoc (cdr pair)
1100 (reply . "ANSWERED")))
1102 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1103 (cons '(read . read) gnus-article-mark-lists)))
1105 (defun nnimap-mark-to-predicate (pred)
1106 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1107 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1108 to be used within a IMAP SEARCH query."
1109 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1111 (defconst nnimap-mark-to-flag-alist
1114 (or (assoc (cdr pair)
1116 (tick . "\\Flagged")
1118 (reply . "\\Answered")))
1120 (format "gnus-%s" (symbol-name (cdr pair))))))
1121 (cons '(read . read) gnus-article-mark-lists)))
1123 (defun nnimap-mark-to-flag-1 (preds)
1124 (if (and (not (null preds)) (listp preds))
1125 (cons (nnimap-mark-to-flag (car preds))
1126 (nnimap-mark-to-flag (cdr preds)))
1127 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1129 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1130 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1131 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1132 be used in a STORE FLAGS command."
1133 (let ((result (nnimap-mark-to-flag-1 preds)))
1134 (setq result (if (and (or make-string always-list)
1135 (not (listp result)))
1139 (mapconcat (lambda (flag)
1141 (mapconcat 'identity flag " ")
1146 (defun nnimap-mark-permanent-p (mark &optional group)
1147 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1148 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1150 (defun nnimap-remassoc (key alist)
1151 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1152 The modified LIST is returned. If the first member
1153 of LIST has a car that is `equal' to KEY, there is no way to remove it
1154 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1155 sure of changing the value of `foo'."
1157 (if (equal key (caar alist))
1159 (setcdr alist (nnimap-remassoc key (cdr alist)))
1162 (defun nnimap-update-alist-soft (key value alist)
1164 (cons (cons key value) (nnimap-remassoc key alist))
1165 (nnimap-remassoc key alist)))
1167 (defun nnimap-range-to-string (range)
1172 (car item) (cdr item))
1173 (format "%d" item)))
1174 (if (and (listp range) (not (listp (cdr range))))
1175 (list range);; make (1 . 2) into ((1 . 2))
1181 (buffer-disable-undo (get-buffer-create nnimap-debug))
1182 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1184 nnimap-possibly-change-server
1185 nnimap-verify-uidvalidity
1186 nnimap-find-minmax-uid
1187 nnimap-possibly-change-group
1188 ;;nnimap-replace-whitespace
1189 nnimap-retrieve-headers-progress
1190 nnimap-retrieve-which-headers
1191 nnimap-group-overview-filename
1192 nnimap-retrieve-headers-from-file
1193 nnimap-retrieve-headers-from-server
1194 nnimap-retrieve-headers
1195 nnimap-open-connection
1197 nnimap-server-opened
1199 nnimap-request-close
1200 nnimap-status-message
1202 nnimap-request-article-part
1203 nnimap-request-article
1206 nnimap-request-group
1208 nnimap-pattern-to-list-arguments
1211 nnimap-retrieve-groups
1212 nnimap-request-update-info-internal
1214 nnimap-request-set-mark
1215 nnimap-split-to-groups
1216 nnimap-split-find-rule
1217 nnimap-split-find-inbox
1218 nnimap-split-articles
1220 nnimap-request-newgroups
1221 nnimap-request-create-group
1222 nnimap-time-substract
1223 nnimap-date-days-ago
1224 nnimap-request-expire-articles-progress
1225 nnimap-request-expire-articles
1226 nnimap-request-move-article
1227 nnimap-request-accept-article
1228 nnimap-request-delete-group
1229 nnimap-request-rename-group
1230 gnus-group-nnimap-expunge
1231 gnus-group-nnimap-edit-acl
1232 gnus-group-nnimap-edit-acl-done
1233 nnimap-group-mode-hook
1234 nnimap-mark-to-predicate
1235 nnimap-mark-to-flag-1
1237 nnimap-mark-permanent-p
1239 nnimap-update-alist-soft
1240 nnimap-range-to-string
1245 ;;; nnimap.el ends here