1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998-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 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-predicate "UNSEEN UNDELETED"
130 "The predicate used to find articles to split.
131 If you use another IMAP client to peek on articles but always would
132 like nnimap to split them once it's started, you could change this to
133 \"UNDELETED\". Other available predicates are available in
134 RFC2060 section 6.4.4.")
136 (defvar nnimap-split-fancy nil
137 "Like `nnmail-split-fancy', which see.")
139 ;; Authorization / Privacy variables
141 (defvoo nnimap-auth-method nil
144 (defvoo nnimap-stream nil
145 "How nnimap will connect to the server.
147 The default, nil, will try to use the \"best\" method the server can
152 1) you want to connect with SSL. The SSL integration with IMAP is
153 brain-dead so you'll have to tell it specifically.
155 2) your server is more capable than your environment -- i.e. your
156 server accept Kerberos login's but you haven't installed the
157 `imtest' program or your machine isn't configured for Kerberos.
159 Possible choices: kerberos4, ssl, network")
161 (defvoo nnimap-authenticator nil
162 "How nnimap authenticate itself to the server.
164 The default, nil, will try to use the \"best\" method the server can
167 There is only one reason for fiddling with this variable, and that is
168 if your server is more capable than your environment -- i.e. you
169 connect to a server that accept Kerberos login's but you haven't
170 installed the `imtest' program or your machine isn't configured for
173 Possible choices: kerberos4, cram-md5, login, anonymous.")
175 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
176 "Directory to keep NOV cache files for nnimap groups.
177 See also `nnimap-nov-file-name'.")
179 (defvoo nnimap-nov-file-name "nnimap."
180 "NOV cache base filename.
181 The group name and `nnimap-nov-file-name-suffix' will be appended. A
182 typical complete file name would be
183 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
184 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
185 `nnmail-use-long-file-names' is nil")
187 (defvoo nnimap-nov-file-name-suffix ".novcache"
188 "Suffix for NOV cache base filename.")
190 (defvoo nnimap-nov-is-evil nil
191 "If non-nil, nnimap will never generate or use a local nov database for this backend.
192 Using nov databases will speed up header fetching considerably.
193 Unlike other backends, you do not need to take special care if you
194 flip this variable.")
196 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
197 "Whether to expunge a group when it is closed.
198 When a IMAP group with articles marked for deletion is closed, this
199 variable determine if nnimap should actually remove the articles or
202 If always, nnimap always perform a expunge when closing the group.
203 If never, nnimap never expunges articles marked for deletion.
204 If ask, nnimap will ask you if you wish to expunge marked articles.
206 When setting this variable to `never', you can only expunge articles
207 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
209 (defvoo nnimap-list-pattern "*"
210 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
211 See below for available wildcards.
213 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
214 REFERENCE will be passed as the first parameter to LIST/LSUB. The
215 semantics of this are server specific, on the University of Washington
216 server you can specify a directory.
219 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
221 There are two wildcards * and %. * matches everything, % matches
222 everything in the current hierarchy.")
224 (defvoo nnimap-news-groups nil
225 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
227 This variable should contain a regexp matching groups where you wish
228 replies to be stored to the mailbox directly.
231 '(\"^[^I][^N][^B][^O][^X].*$\")
233 This will match all groups not beginning with \"INBOX\".
235 Note that there is nothing technically different between mail-like and
236 news-like mailboxes. If you wish to have a group with todo items or
237 similar which you wouldn't want to set up a mailing list for, you can
238 use this to make replies go directly to the group.")
240 (defvoo nnimap-server-address nil
241 "Obsolete. Use `nnimap-address'.")
243 (defcustom nnimap-authinfo-file "~/.authinfo"
244 "Authorization information for IMAP servers. In .netrc format."
247 (repeat :tag "Entries"
250 :value ("" ("login" . "") ("password" . ""))
254 (const :format "" "login")
255 (string :format "Login: %v"))
257 (const :format "" "password")
258 (string :format "Password: %v")))))))
260 (defcustom nnimap-prune-cache t
261 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
264 (defvar nnimap-request-list-method 'imap-mailbox-list
265 "Method to use to request a list of all folders from the server.
266 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
267 restrict visible folders.")
269 ;; Internal variables:
271 (defvar nnimap-debug nil);; "*nnimap-debug*")
272 (defvar nnimap-current-move-server nil)
273 (defvar nnimap-current-move-group nil)
274 (defvar nnimap-current-move-article nil)
275 (defvar nnimap-length)
276 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
277 (defvar nnimap-progress-how-often 20)
278 (defvar nnimap-counter)
279 (defvar nnimap-callback-callback-function nil
280 "Gnus callback the nnimap asynchronous callback should call.")
281 (defvar nnimap-callback-buffer nil
282 "Which buffer the asynchronous article prefetch callback should work in.")
283 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
284 (defvar nnimap-current-server nil) ;; Current server
285 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
289 (nnoo-define-basics nnimap)
291 ;; Utility functions:
293 (defsubst nnimap-get-server-buffer (server)
294 "Return buffer for SERVER, if nil use current server."
295 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
297 (defun nnimap-possibly-change-server (server)
298 "Return buffer for SERVER, changing the current server as a side-effect.
299 If SERVER is nil, uses the current server."
300 (setq nnimap-current-server (or server nnimap-current-server)
301 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
303 (defun nnimap-verify-uidvalidity (group server)
304 "Verify stored uidvalidity match current one in GROUP on SERVER."
305 (let* ((gnusgroup (gnus-group-prefixed-name
306 group (gnus-server-to-method
307 (format "nnimap:%s" server))))
308 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
309 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity)))
311 (if (not (equal old-uidvalidity new-uidvalidity))
312 nil ;; uidvalidity clash
313 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
315 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
318 (defun nnimap-find-minmax-uid (group &optional examine)
319 "Find lowest and highest active article nummber in GROUP.
320 If EXAMINE is non-nil the group is selected read-only."
321 (with-current-buffer nnimap-server-buffer
322 (when (imap-mailbox-select group examine)
324 (when (> (imap-mailbox-get 'exists) 0)
325 (imap-fetch "1,*" "UID" nil 'nouidfetch)
326 (imap-message-map (lambda (uid Uid)
327 (setq minuid (if minuid (min minuid uid) uid)
328 maxuid (if maxuid (max maxuid uid) uid)))
330 (list (imap-mailbox-get 'exists) minuid maxuid)))))
332 (defun nnimap-possibly-change-group (group &optional server)
333 "Make GROUP the current group, and SERVER the current server."
334 (when (nnimap-possibly-change-server server)
335 (with-current-buffer nnimap-server-buffer
336 (if (or (null group) (imap-current-mailbox-p group))
338 (if (imap-mailbox-select group)
339 (if (or (nnimap-verify-uidvalidity
340 group (or server nnimap-current-server))
341 (zerop (imap-mailbox-get 'exists group))
344 "nnimap: Group %s is not uidvalid. Continue? " group)))
346 (imap-mailbox-unselect)
347 (error "nnimap: Group %s is not uid-valid." group))
348 (nnheader-report 'nnimap (imap-error-text)))))))
350 (defun nnimap-replace-whitespace (string)
351 "Return STRING with all whitespace replaced with space."
353 (while (string-match "[\r\n\t]+" string)
354 (setq string (replace-match " " t t string)))
357 ;; Required backend functions
359 (defun nnimap-retrieve-headers-progress ()
360 "Hook to insert NOV line for current article into `nntp-server-buffer'."
361 (and (numberp nnmail-large-newsgroup)
362 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
363 (> nnimap-length nnmail-large-newsgroup)
364 (nnheader-message 6 "nnimap: Retrieving headers... %c"
365 (nth (/ (% nnimap-counter
366 (* (length nnimap-progress-chars)
367 nnimap-progress-how-often))
368 nnimap-progress-how-often)
369 nnimap-progress-chars)))
370 (with-current-buffer nntp-server-buffer
371 (let (headers lines chars uid mbx)
372 (with-current-buffer nnimap-server-buffer
373 (setq uid imap-current-message
374 mbx imap-current-mailbox
375 headers (if (imap-capability 'IMAP4rev1)
376 ;; xxx don't just use car? alist doesn't contain
377 ;; anything else now, but it might...
378 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
379 (imap-message-get uid 'RFC822.HEADER))
380 lines (imap-body-lines (imap-message-body imap-current-message))
381 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
384 (buffer-disable-undo)
386 (nnheader-ms-strip-cr)
387 (nnheader-fold-continuation-lines)
388 (subst-char-in-region (point-min) (point-max) ?\t ? )
389 (let ((head (nnheader-parse-head 'naked)))
390 (mail-header-set-number head uid)
391 (mail-header-set-chars head chars)
392 (mail-header-set-lines head lines)
393 (mail-header-set-xref
394 head (format "%s %s:%d" (system-name) mbx uid))
397 (defun nnimap-retrieve-which-headers (articles fetch-old)
398 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
399 (with-current-buffer nnimap-server-buffer
400 (if (numberp (car-safe articles))
403 (nnimap-range-to-string
404 (gnus-compress-sequence
405 (append (gnus-uncompress-sequence
407 (cons (if (numberp fetch-old)
408 (max 1 (- (car articles) fetch-old))
410 (1- (car articles)))))
412 (mapcar (lambda (msgid)
414 (format "HEADER Message-Id %s" msgid)))
417 (defun nnimap-group-overview-filename (group server)
418 "Make pathname for GROUP on SERVER."
419 (let ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
420 (file (nnheader-translate-file-chars
421 (concat nnimap-nov-file-name
422 (if (equal server "")
424 server) "." group nnimap-nov-file-name-suffix) t)))
425 (if (or nnmail-use-long-file-names
426 (file-exists-p (concat dir file)))
428 (concat dir (mm-encode-coding-string
429 (nnheader-replace-chars-in-string file ?. ?/)
430 nnmail-pathname-coding-system)))))
432 (defun nnimap-retrieve-headers-from-file (group server)
433 (with-current-buffer nntp-server-buffer
434 (let ((nov (nnimap-group-overview-filename group server)))
435 (when (file-exists-p nov)
436 (mm-insert-file-contents nov)
437 (set-buffer-modified-p nil)
438 (let ((min (progn (goto-char (point-min))
440 (read (current-buffer)))))
441 (max (progn (goto-char (point-max))
444 (read (current-buffer))))))
445 (if (and (numberp min) (numberp max))
447 ;; junk, remove it, it's saved later
451 (defun nnimap-retrieve-headers-from-server (articles group server)
452 (with-current-buffer nnimap-server-buffer
453 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
454 (nnimap-length (gnus-range-length articles))
456 (imap-fetch (nnimap-range-to-string articles)
457 (concat "(UID RFC822.SIZE BODY "
459 (append '(Subject From Date Message-Id
460 References In-Reply-To Xref)
462 nnmail-extra-headers))))
463 (if (imap-capability 'IMAP4rev1)
464 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
465 (format "RFC822.HEADER.LINES %s)" headers)))))
466 (and (numberp nnmail-large-newsgroup)
467 (> nnimap-length nnmail-large-newsgroup)
468 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
470 (defun nnimap-use-nov-p (group server)
471 (or gnus-nov-is-evil nnimap-nov-is-evil
472 (unless (and (gnus-make-directory
474 (nnimap-group-overview-filename group server)))
476 (nnimap-group-overview-filename group server)))
477 (message "nnimap: Nov cache not writable, %s"
478 (nnimap-group-overview-filename group server)))))
480 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
481 (when (nnimap-possibly-change-group group server)
482 (with-current-buffer nntp-server-buffer
484 (if (nnimap-use-nov-p group server)
485 (nnimap-retrieve-headers-from-server
486 (gnus-compress-sequence articles) group server)
487 (let (uids cached low high)
488 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
490 high (car (last uids)))
491 (if (setq cached (nnimap-retrieve-headers-from-file group server))
493 ;; fetch articles with uids before cache block
494 (when (< low (car cached))
495 (goto-char (point-min))
496 (nnimap-retrieve-headers-from-server
497 (cons low (1- (car cached))) group server))
498 ;; fetch articles with uids after cache block
499 (when (> high (cdr cached))
500 (goto-char (point-max))
501 (nnimap-retrieve-headers-from-server
502 (cons (1+ (cdr cached)) high) group server))
503 (when nnimap-prune-cache
504 ;; remove nov's for articles which has expired on server
505 (goto-char (point-min))
506 (dolist (uid (gnus-set-difference articles uids))
507 (when (re-search-forward (format "^%d\t" uid) nil t)
508 (gnus-delete-line)))))
509 ;; nothing cached, fetch whole range from server
510 (nnimap-retrieve-headers-from-server
511 (cons low high) group server))
512 (when (buffer-modified-p)
514 1 (point-max) (nnimap-group-overview-filename group server)
516 (nnheader-nov-delete-outside-range low high))))
519 (defun nnimap-open-connection (server)
520 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
521 nnimap-authenticator nnimap-server-buffer))
522 (nnheader-report 'nnimap "Can't open connection to server %s" server)
523 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
524 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
525 (imap-close nnimap-server-buffer)
526 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
527 (let (list alist user passwd)
528 (and (fboundp 'gnus-parse-netrc)
529 (setq list (gnus-parse-netrc nnimap-authinfo-file)
530 alist (or (and (gnus-netrc-get
531 (gnus-netrc-machine list server) "machine")
532 (gnus-netrc-machine list server))
533 (gnus-netrc-machine list nnimap-address))
534 user (gnus-netrc-get alist "login")
535 passwd (gnus-netrc-get alist "password")))
536 (if (imap-authenticate user passwd nnimap-server-buffer)
538 (push (list server nnimap-server-buffer)
539 nnimap-server-buffer-alist)
540 (nnimap-possibly-change-server server))
541 (imap-close nnimap-server-buffer)
542 (kill-buffer nnimap-server-buffer)
543 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
545 (deffoo nnimap-open-server (server &optional defs)
546 (nnheader-init-server-buffer)
547 (if (nnimap-server-opened server)
549 (unless (assq 'nnimap-server-buffer defs)
550 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
551 ;; translate `nnimap-server-address' to `nnimap-address' in defs
552 ;; for people that configured nnimap with a very old version
553 (unless (assq 'nnimap-address defs)
554 (if (assq 'nnimap-server-address defs)
555 (push (list 'nnimap-address
556 (cadr (assq 'nnimap-server-address defs))) defs)
557 (push (list 'nnimap-address server) defs)))
558 (nnoo-change-server 'nnimap server defs)
559 (or (and nnimap-server-buffer
560 (imap-opened nnimap-server-buffer))
561 (nnimap-open-connection server))))
563 (deffoo nnimap-server-opened (&optional server)
564 "Whether SERVER is opened.
565 If SERVER is the current virtual server, and the connection to the
566 physical server is alive, this function return a non-nil value. If
567 SERVER is nil, it is treated as the current server."
568 ;; clean up autologouts??
569 (and (or server nnimap-current-server)
570 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
571 (imap-opened (nnimap-get-server-buffer server))))
573 (deffoo nnimap-close-server (&optional server)
574 "Close connection to server and free all resources connected to it.
575 Return nil if the server couldn't be closed for some reason."
576 (let ((server (or server nnimap-current-server)))
577 (when (or (nnimap-server-opened server)
578 (imap-opened (nnimap-get-server-buffer server)))
579 (imap-close (nnimap-get-server-buffer server))
580 (kill-buffer (nnimap-get-server-buffer server))
581 (setq nnimap-server-buffer nil
582 nnimap-current-server nil
583 nnimap-server-buffer-alist
584 (delq server nnimap-server-buffer-alist)))
585 (nnoo-close-server 'nnimap server)))
587 (deffoo nnimap-request-close ()
588 "Close connection to all servers and free all resources that the backend have reserved.
589 All buffers that have been created by that
590 backend should be killed. (Not the nntp-server-buffer, though.) This
591 function is generally only called when Gnus is shutting down."
592 (mapcar (lambda (server) (nnimap-close-server (car server)))
593 nnimap-server-buffer-alist)
594 (setq nnimap-server-buffer-alist nil))
596 (deffoo nnimap-status-message (&optional server)
597 "This function returns the last error message from server."
598 (when (nnimap-possibly-change-server server)
599 (nnoo-status-message 'nnimap server)))
601 (defun nnimap-demule (string)
602 (funcall (if (and (fboundp 'string-as-multibyte)
603 (subrp (symbol-function 'string-as-multibyte)))
608 (defun nnimap-callback ()
609 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
610 (with-current-buffer nnimap-callback-buffer
612 (with-current-buffer nnimap-server-buffer
613 (nnimap-demule (imap-message-get (imap-current-message) 'RFC822)))) ;xxx
614 (nnheader-ms-strip-cr)
615 (funcall nnimap-callback-callback-function t)))
617 (defun nnimap-request-article-part (article part prop &optional
618 group server to-buffer detail)
619 (when (nnimap-possibly-change-group group server)
620 (let ((article (if (stringp article)
621 (car-safe (imap-search
622 (format "HEADER Message-Id %s" article)
623 nnimap-server-buffer))
626 (gnus-message 9 "nnimap: Fetching (part of) article %d..." article)
627 (if (not nnheader-callback-function)
628 (with-current-buffer (or to-buffer nntp-server-buffer)
630 (let ((data (imap-fetch article part prop nil
631 nnimap-server-buffer)))
632 (insert (nnimap-demule (if detail
635 (nnheader-ms-strip-cr)
636 (gnus-message 9 "nnimap: Fetching (part of) article %d...done"
639 (nnheader-report 'nnimap "No such article: %s"
640 (imap-error-text nnimap-server-buffer))
641 (cons group article)))
642 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
643 (setq nnimap-callback-callback-function nnheader-callback-function
644 nnimap-callback-buffer nntp-server-buffer)
645 (imap-fetch-asynch article part nil nnimap-server-buffer)
646 (cons group article))))))
648 (deffoo nnimap-asynchronous-p ()
651 (deffoo nnimap-request-article (article &optional group server to-buffer)
652 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
653 (nnimap-request-article-part
654 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
655 (nnimap-request-article-part
656 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
658 (deffoo nnimap-request-head (article &optional group server to-buffer)
659 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
660 (nnimap-request-article-part
661 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
662 (nnimap-request-article-part
663 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
665 (deffoo nnimap-request-body (article &optional group server to-buffer)
666 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
667 (nnimap-request-article-part
668 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
669 (nnimap-request-article-part
670 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
672 (deffoo nnimap-request-group (group &optional server fast)
673 (nnimap-request-update-info-internal
675 (gnus-get-info (gnus-group-prefixed-name
676 group (gnus-server-to-method (format "nnimap:%s" server))))
678 (when (nnimap-possibly-change-group group server)
681 ((null (setq info (nnimap-find-minmax-uid group t)))
682 (nnheader-report 'nnimap "Could not get active info for %s"
685 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
686 (max 1 (or (nth 1 info) 1))
687 (or (nth 2 info) 0) group)
688 (nnheader-report 'nnimap "Group %s selected" group)
691 (defun nnimap-close-group (group &optional server)
692 (with-current-buffer nnimap-server-buffer
693 (when (and (imap-opened)
694 (nnimap-possibly-change-group group server))
695 (case nnimap-expunge-on-close
696 ('always (imap-mailbox-expunge)
697 (imap-mailbox-close))
698 ('ask (if (and (imap-search "DELETED")
699 (gnus-y-or-n-p (format
700 "Expunge articles in group `%s'? "
701 imap-current-mailbox)))
702 (progn (imap-mailbox-expunge)
703 (imap-mailbox-close))
704 (imap-mailbox-unselect)))
705 (t (imap-mailbox-unselect)))
706 (not imap-current-mailbox))))
708 (defun nnimap-pattern-to-list-arguments (pattern)
710 (cons (car-safe p) (or (cdr-safe p) p)))
711 (if (and (listp pattern)
712 (listp (cdr pattern)))
716 (deffoo nnimap-request-list (&optional server)
717 (when (nnimap-possibly-change-server server)
718 (with-current-buffer nntp-server-buffer
720 (gnus-message 5 "nnimap: Generating active list%s..."
721 (if (> (length server) 0) (concat " for " server) ""))
722 (with-current-buffer nnimap-server-buffer
723 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
724 (dolist (mbx (funcall nnimap-request-list-method
725 (cdr pattern) (car pattern)))
726 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
727 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
729 (with-current-buffer nntp-server-buffer
730 (insert (format "\"%s\" %d %d y\n"
731 mbx (or (nth 2 info) 0)
732 (max 1 (or (nth 1 info) 1)))))))))))
733 (gnus-message 5 "nnimap: Generating active list%s...done"
734 (if (> (length server) 0) (concat " for " server) ""))
737 (deffoo nnimap-request-post (&optional server)
739 (dolist (mbx (message-tokenize-header
740 (message-fetch-field "Newsgroups")) success)
741 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
742 (or (gnus-active to-newsgroup)
743 (gnus-activate-group to-newsgroup)
744 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
746 (or (and (gnus-request-create-group
747 to-newsgroup gnus-command-method)
748 (gnus-activate-group to-newsgroup nil nil
749 gnus-command-method))
750 (error "Couldn't create group %s" to-newsgroup)))
751 (error "No such group: %s" to-newsgroup))
752 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
753 (setq success nil))))))
755 ;; Optional backend functions
757 (deffoo nnimap-retrieve-groups (groups &optional server)
758 (when (nnimap-possibly-change-server server)
759 (gnus-message 5 "nnimap: Checking mailboxes...")
760 (with-current-buffer nntp-server-buffer
762 (dolist (group groups)
763 (gnus-message 7 "nnimap: Checking mailbox %s" group)
764 (or (member "\\NoSelect"
765 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
766 (let ((info (nnimap-find-minmax-uid group 'examine)))
767 (insert (format "\"%s\" %d %d y\n" group
769 (max 1 (or (nth 1 info) 1))))))))
770 (gnus-message 5 "nnimap: Checking mailboxes...done")
773 (deffoo nnimap-request-update-info-internal (group info &optional server)
774 (when (nnimap-possibly-change-group group server)
775 (when info;; xxx what does this mean? should we create a info?
776 (with-current-buffer nnimap-server-buffer
777 (gnus-message 5 "nnimap: Updating info for %s..."
778 (gnus-info-group info))
780 (when (nnimap-mark-permanent-p 'read)
782 ;; read info could contain articles marked unread by other
783 ;; imap clients! we correct this
784 (setq seen (gnus-uncompress-range (gnus-info-read info))
785 unseen (imap-search "UNSEEN UNDELETED")
786 seen (gnus-set-difference seen unseen)
787 ;; seen might lack articles marked as read by other
788 ;; imap clients! we correct this
789 seen (append seen (imap-search "SEEN"))
792 seen (gnus-compress-sequence seen t)
793 ;; we can't return '(1) since this isn't a "list of ranges",
794 ;; and we can't return '((1)) since g-list-of-unread-articles
795 ;; is buggy so we return '((1 . 1)).
796 seen (if (and (integerp (car seen))
798 (list (cons (car seen) (car seen)))
800 (gnus-info-set-read info seen)))
802 (mapcar (lambda (pred)
803 (when (and (nnimap-mark-permanent-p (cdr pred))
804 (member (nnimap-mark-to-flag (cdr pred))
805 (imap-mailbox-get 'flags)))
808 (nnimap-update-alist-soft
810 (gnus-compress-sequence
811 (imap-search (nnimap-mark-to-predicate (cdr pred))))
812 (gnus-info-marks info))
814 gnus-article-mark-lists)
816 (gnus-message 5 "nnimap: Updating info for %s...done"
817 (gnus-info-group info))
821 (deffoo nnimap-request-type (group &optional article)
822 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
826 (deffoo nnimap-request-set-mark (group actions &optional server)
827 (when (nnimap-possibly-change-group group server)
828 (with-current-buffer nnimap-server-buffer
830 (gnus-message 7 "nnimap: Setting marks in %s..." group)
831 (while (setq action (pop actions))
832 (let ((range (nth 0 action))
833 (what (nth 1 action))
834 (cmdmarks (nth 2 action))
836 ;; cache flags are pointless on the server
837 (setq cmdmarks (delq 'cache cmdmarks))
838 ;; flag dormant articles as ticked
839 (if (memq 'dormant cmdmarks)
840 (setq cmdmarks (cons 'tick cmdmarks)))
841 ;; remove stuff we are forbidden to store
842 (mapcar (lambda (mark)
843 (if (imap-message-flag-permanent-p
844 (nnimap-mark-to-flag mark))
845 (setq marks (cons mark marks))))
847 (when (and range marks)
848 (cond ((eq what 'del)
849 (imap-message-flags-del
850 (nnimap-range-to-string range)
851 (nnimap-mark-to-flag marks nil t)))
853 (imap-message-flags-add
854 (nnimap-range-to-string range)
855 (nnimap-mark-to-flag marks nil t)))
857 (imap-message-flags-set
858 (nnimap-range-to-string range)
859 (nnimap-mark-to-flag marks nil t)))))))
860 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
863 (defun nnimap-split-fancy ()
864 "Like nnmail-split-fancy, but uses nnimap-split-fancy."
865 (let ((nnmail-split-fancy nnimap-split-fancy))
866 (nnmail-split-fancy)))
868 (defun nnimap-split-to-groups (rules)
869 ;; tries to match all rules in nnimap-split-rule against content of
870 ;; nntp-server-buffer, returns a list of groups that matched.
871 (with-current-buffer nntp-server-buffer
872 ;; Fold continuation lines.
873 (goto-char (point-min))
874 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
875 (replace-match " " t t))
876 (if (functionp rules)
878 (let (to-groups regrepp)
880 (dolist (rule rules to-groups)
881 (let ((group (car rule))
882 (regexp (cadr rule)))
883 (goto-char (point-min))
884 (when (and (if (stringp regexp)
886 (setq regrepp (string-match "\\\\[0-9&]" group))
887 (re-search-forward regexp nil t))
888 (funcall regexp group))
889 ;; Don't enter the article into the same group twice.
890 (not (assoc group to-groups)))
892 (nnmail-expand-newtext group)
895 (or nnimap-split-crosspost
896 (throw 'split-done to-groups))))))))))
898 (defun nnimap-split-find-rule (server inbox)
901 (defun nnimap-split-find-inbox (server)
902 (if (listp nnimap-split-inbox)
904 (list nnimap-split-inbox)))
906 (defun nnimap-split-articles (&optional group server)
907 (when (nnimap-possibly-change-server server)
908 (with-current-buffer nnimap-server-buffer
909 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
910 ;; iterate over inboxes
911 (while (and (setq inbox (pop inboxes))
912 (nnimap-possibly-change-group inbox));; SELECT
913 ;; find split rule for this server / inbox
914 (when (setq rule (nnimap-split-find-rule server inbox))
915 ;; iterate over articles
916 (dolist (article (imap-search nnimap-split-predicate))
917 (when (nnimap-request-head article)
918 ;; copy article to right group(s)
919 (setq removeorig nil)
920 (dolist (to-group (nnimap-split-to-groups rule))
921 (if (imap-message-copy (number-to-string article)
922 to-group nil 'nocopyuid)
924 (message "IMAP split moved %s:%s:%d to %s" server inbox
927 ;; Add the group-art list to the history list.
928 (push (list (cons to-group 0)) nnmail-split-history))
929 (message "IMAP split failed to move %s:%s:%d to %s" server
930 inbox article to-group)))
931 ;; remove article if it was successfully copied somewhere
933 (imap-message-flags-add (format "%d" article)
934 "\\Seen \\Deleted")))))
935 (when (imap-mailbox-select inbox);; just in case
936 ;; todo: UID EXPUNGE (if available) to remove splitted articles
937 (imap-mailbox-expunge)
938 (imap-mailbox-close)))
941 (deffoo nnimap-request-scan (&optional group server)
942 (nnimap-split-articles group server))
944 (deffoo nnimap-request-newgroups (date &optional server)
945 (when (nnimap-possibly-change-server server)
946 (with-current-buffer nntp-server-buffer
947 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
948 (if (> (length server) 0) " on " "") server)
950 (dolist (pattern (nnimap-pattern-to-list-arguments
951 nnimap-list-pattern))
952 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
953 nnimap-server-buffer))
954 (or (member-if (lambda (mailbox)
955 (string= (downcase mailbox) "\\noselect"))
956 (imap-mailbox-get 'list-flags mbx
957 nnimap-server-buffer))
958 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
960 (insert (format "\"%s\" %d %d y\n"
961 mbx (or (nth 2 info) 0)
962 (max 1 (or (nth 1 info) 1)))))))))
963 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
964 (if (> (length server) 0) " on " "") server))
967 (deffoo nnimap-request-create-group (group &optional server args)
968 (when (nnimap-possibly-change-server server)
969 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
970 (imap-mailbox-create group nnimap-server-buffer))))
972 (defun nnimap-time-substract (time1 time2)
973 "Return TIME for TIME1 - TIME2."
974 (let* ((ms (- (car time1) (car time2)))
975 (ls (- (nth 1 time1) (nth 1 time2))))
977 (list (- ms 1) (+ (expt 2 16) ls))
980 (defun nnimap-date-days-ago (daysago)
981 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
982 (let ((date (format-time-string "%d-%b-%Y"
983 (nnimap-time-substract
985 (days-to-time daysago)))))
986 (if (eq ?0 (string-to-char date))
990 (defun nnimap-request-expire-articles-progress ()
991 (gnus-message 5 "nnimap: Marking article %d for deletion..."
992 imap-current-message))
994 ;; Notice that we don't actually delete anything, we just mark them deleted.
995 (deffoo nnimap-request-expire-articles (articles group &optional server force)
996 (let ((artseq (gnus-compress-sequence articles)))
997 (when (and artseq (nnimap-possibly-change-group group server))
998 (with-current-buffer nnimap-server-buffer
1000 (and (imap-message-flags-add
1001 (nnimap-range-to-string artseq) "\\Deleted")
1002 (setq articles nil))
1003 (let ((days (or (and nnmail-expiry-wait-function
1004 (funcall nnmail-expiry-wait-function group))
1005 nnmail-expiry-wait)))
1006 (cond ((eq days 'immediate)
1007 (and (imap-message-flags-add
1008 (nnimap-range-to-string artseq) "\\Deleted")
1009 (setq articles nil)))
1011 (let ((oldarts (imap-search
1012 (format "UID %s NOT SINCE %s"
1013 (nnimap-range-to-string artseq)
1014 (nnimap-date-days-ago days))))
1015 (imap-fetch-data-hook
1016 '(nnimap-request-expire-articles-progress)))
1018 (imap-message-flags-add
1019 (nnimap-range-to-string
1020 (gnus-compress-sequence oldarts))
1022 (setq articles (gnus-set-difference
1023 articles oldarts)))))))))))
1024 ;; return articles not deleted
1027 (deffoo nnimap-request-move-article (article group server
1028 accept-form &optional last)
1029 (when (nnimap-possibly-change-server server)
1031 (let ((buf (get-buffer-create " *nnimap move*"))
1032 (nnimap-current-move-article article)
1033 (nnimap-current-move-group group)
1034 (nnimap-current-move-server nnimap-current-server)
1036 (and (nnimap-request-article article group server)
1039 (buffer-disable-undo (current-buffer))
1040 (insert-buffer-substring nntp-server-buffer)
1041 (setq result (eval accept-form))
1044 (nnimap-request-expire-articles (list article) group server t))
1047 (deffoo nnimap-request-accept-article (group &optional server last)
1048 (when (nnimap-possibly-change-server server)
1051 (if (string= nnimap-current-server nnimap-current-move-server)
1052 ;; moving article within same server, speed it up...
1053 (and (nnimap-possibly-change-group
1054 nnimap-current-move-group)
1055 (imap-message-copy (number-to-string
1056 nnimap-current-move-article)
1057 group 'dontcreate nil
1058 nnimap-server-buffer))
1059 ;; turn into rfc822 format (\r\n eol's)
1060 (with-current-buffer (current-buffer)
1061 (goto-char (point-min))
1062 (while (search-forward "\n" nil t)
1063 (replace-match "\r\n")))
1064 ;; next line for Cyrus server bug
1065 (imap-mailbox-unselect nnimap-server-buffer)
1066 (imap-message-append group (current-buffer) nil nil
1067 nnimap-server-buffer)))
1068 (cons group (nth 1 uid))
1069 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1071 (deffoo nnimap-request-delete-group (group force &optional server)
1072 (when (nnimap-possibly-change-server server)
1073 (with-current-buffer nnimap-server-buffer
1075 (or (null (imap-mailbox-status group 'uidvalidity))
1076 (imap-mailbox-delete group))
1080 (deffoo nnimap-request-rename-group (group new-name &optional server)
1081 (when (nnimap-possibly-change-server server)
1082 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1084 (defun nnimap-expunge (mailbox server)
1085 (when (nnimap-possibly-change-group mailbox server)
1086 (imap-mailbox-expunge nnimap-server-buffer)))
1088 (defun nnimap-acl-get (mailbox server)
1089 (when (nnimap-possibly-change-server server)
1090 (imap-mailbox-acl-get mailbox nnimap-server-buffer)))
1092 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1093 (when (nnimap-possibly-change-server (cadr method))
1094 (unless (imap-capability 'ACL nnimap-server-buffer)
1095 (error "Your server does not support ACL editing"))
1096 (with-current-buffer nnimap-server-buffer
1097 ;; delete all removed identifiers
1098 (mapcar (lambda (old-acl)
1099 (unless (assoc (car old-acl) new-acls)
1100 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1101 (error "Can't delete ACL for %s" (car old-acl)))))
1103 ;; set all changed acl's
1104 (mapcar (lambda (new-acl)
1105 (let ((new-rights (cdr new-acl))
1106 (old-rights (cdr (assoc (car new-acl) old-acls))))
1107 (unless (and old-rights new-rights
1108 (string= old-rights new-rights))
1109 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1110 (error "Can't set ACL for %s to %s" (car new-acl)
1116 ;;; Internal functions
1119 ;; This is confusing.
1121 ;; mark => read, tick, draft, reply etc
1122 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1123 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1125 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1126 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1129 (defconst nnimap-mark-to-predicate-alist
1131 (lambda (pair) ; cdr is the mark
1132 (or (assoc (cdr pair)
1136 (reply . "ANSWERED")))
1138 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1139 (cons '(read . read) gnus-article-mark-lists)))
1141 (defun nnimap-mark-to-predicate (pred)
1142 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1143 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1144 to be used within a IMAP SEARCH query."
1145 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1147 (defconst nnimap-mark-to-flag-alist
1150 (or (assoc (cdr pair)
1152 (tick . "\\Flagged")
1154 (reply . "\\Answered")))
1156 (format "gnus-%s" (symbol-name (cdr pair))))))
1157 (cons '(read . read) gnus-article-mark-lists)))
1159 (defun nnimap-mark-to-flag-1 (preds)
1160 (if (and (not (null preds)) (listp preds))
1161 (cons (nnimap-mark-to-flag (car preds))
1162 (nnimap-mark-to-flag (cdr preds)))
1163 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1165 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1166 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1167 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1168 be used in a STORE FLAGS command."
1169 (let ((result (nnimap-mark-to-flag-1 preds)))
1170 (setq result (if (and (or make-string always-list)
1171 (not (listp result)))
1175 (mapconcat (lambda (flag)
1177 (mapconcat 'identity flag " ")
1182 (defun nnimap-mark-permanent-p (mark &optional group)
1183 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1184 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1186 (defun nnimap-remassoc (key alist)
1187 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1188 The modified LIST is returned. If the first member
1189 of LIST has a car that is `equal' to KEY, there is no way to remove it
1190 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1191 sure of changing the value of `foo'."
1193 (if (equal key (caar alist))
1195 (setcdr alist (nnimap-remassoc key (cdr alist)))
1198 (defun nnimap-update-alist-soft (key value alist)
1200 (cons (cons key value) (nnimap-remassoc key alist))
1201 (nnimap-remassoc key alist)))
1203 (defun nnimap-range-to-string (range)
1208 (car item) (cdr item))
1209 (format "%d" item)))
1210 (if (and (listp range) (not (listp (cdr range))))
1211 (list range);; make (1 . 2) into ((1 . 2))
1217 (buffer-disable-undo (get-buffer-create nnimap-debug))
1218 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1220 nnimap-possibly-change-server
1221 nnimap-verify-uidvalidity
1222 nnimap-find-minmax-uid
1223 nnimap-possibly-change-group
1224 ;;nnimap-replace-whitespace
1225 nnimap-retrieve-headers-progress
1226 nnimap-retrieve-which-headers
1227 nnimap-group-overview-filename
1228 nnimap-retrieve-headers-from-file
1229 nnimap-retrieve-headers-from-server
1230 nnimap-retrieve-headers
1231 nnimap-open-connection
1233 nnimap-server-opened
1235 nnimap-request-close
1236 nnimap-status-message
1238 nnimap-request-article-part
1239 nnimap-request-article
1242 nnimap-request-group
1244 nnimap-pattern-to-list-arguments
1247 nnimap-retrieve-groups
1248 nnimap-request-update-info-internal
1250 nnimap-request-set-mark
1251 nnimap-split-to-groups
1252 nnimap-split-find-rule
1253 nnimap-split-find-inbox
1254 nnimap-split-articles
1256 nnimap-request-newgroups
1257 nnimap-request-create-group
1258 nnimap-time-substract
1259 nnimap-date-days-ago
1260 nnimap-request-expire-articles-progress
1261 nnimap-request-expire-articles
1262 nnimap-request-move-article
1263 nnimap-request-accept-article
1264 nnimap-request-delete-group
1265 nnimap-request-rename-group
1266 gnus-group-nnimap-expunge
1267 gnus-group-nnimap-edit-acl
1268 gnus-group-nnimap-edit-acl-done
1269 nnimap-group-mode-hook
1270 nnimap-mark-to-predicate
1271 nnimap-mark-to-flag-1
1273 nnimap-mark-permanent-p
1275 nnimap-update-alist-soft
1276 nnimap-range-to-string
1281 ;;; nnimap.el ends here