(nnimap-demule): Revert 2004-08-30 change.
[gnus] / lisp / nnimap.el
1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
6 ;;         Jim Radford <radford@robby.caltech.edu>
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Todo, major things:
29 ;;
30 ;;   o Fix Gnus to view correct number of unread/total articles in group buffer
31 ;;   o Fix Gnus to handle leading '.' in group names (fixed?)
32 ;;   o Finish disconnected mode (moving articles between mailboxes unplugged)
33 ;;   o Sieve
34 ;;   o MIME (partial article fetches)
35 ;;   o Split to other backends, different split rules for different
36 ;;     servers/inboxes
37 ;;
38 ;; Todo, minor things:
39 ;;
40 ;;   o Don't require half of Gnus -- backends should be standalone
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?) (unnecessary?)
47 ;;   o Add support for the following: (if applicable)
48 ;;       request-list-newsgroups, request-regenerate
49 ;;       list-active-group,
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
55 ;;     .newsrc.eld)
56 ;;   o What about Gnus's article editing, can we support it?  NO!
57 ;;   o Use \Draft to support the draft group??
58 ;;   o Duplicate suppression
59 ;;   o Rewrite UID SEARCH UID X as UID FETCH X (UID) for those with slow servers
60
61 ;;; Code:
62
63 (require 'imap)
64 (require 'nnoo)
65 (require 'nnmail)
66 (require 'nnheader)
67 (require 'mm-util)
68 (require 'gnus)
69 (require 'gnus-range)
70 (require 'gnus-start)
71 (require 'gnus-int)
72
73 (eval-when-compile (require 'cl))
74
75 (nnoo-declare nnimap)
76
77 (defconst nnimap-version "nnimap 1.0")
78
79 (defgroup nnimap nil
80   "Reading IMAP mail with Gnus."
81   :group 'gnus)
82
83 (defvoo nnimap-address nil
84   "Address of physical IMAP server.  If nil, use the virtual server's name.")
85
86 (defvoo nnimap-server-port nil
87   "Port number on physical IMAP server.
88 If nil, defaults to 993 for TLS/SSL connections and 143 otherwise.")
89
90 ;; Splitting variables
91
92 (defcustom nnimap-split-crosspost t
93   "If non-nil, do crossposting if several split methods match the mail.
94 If nil, the first match found will be used."
95   :group 'nnimap
96   :type 'boolean)
97
98 (defcustom nnimap-split-inbox nil
99   "Name of mailbox to split mail from.
100
101 Mail is read from this mailbox and split according to rules in
102 `nnimap-split-rule'.
103
104 This can be a string or a list of strings."
105   :group 'nnimap
106   :type '(choice (string)
107                  (repeat string)))
108
109 (define-widget 'nnimap-strict-function 'function
110   "This widget only matches values that are functionp.
111
112 Warning: This means that a value that is the symbol of a not yet
113 loaded function will not match.  Use with care."
114   :match 'nnimap-strict-function-match)
115
116 (defun nnimap-strict-function-match (widget value)
117   "Ignoring WIDGET, match if VALUE is a function."
118   (functionp value))
119
120 (defcustom nnimap-split-rule nil
121   "Mail will be split according to these rules.
122
123 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
124
125 If you'd like, for instance, one mail group for mail from the
126 \"gnus-imap\" mailing list, one group for junk mail and leave
127 everything else in the incoming mailbox, you could do something like
128 this:
129
130 \(setq nnimap-split-rule '((\"INBOX.gnus-imap\"   \"From:.*gnus-imap\")
131                           (\"INBOX.junk\"        \"Subject:.*buy\")))
132
133 As you can see, `nnimap-split-rule' is a list of lists, where the
134 first element in each \"rule\" is the name of the IMAP mailbox (or the
135 symbol `junk' if you want to remove the mail), and the second is a
136 regexp that nnimap will try to match on the header to find a fit.
137
138 The second element can also be a function.  In that case, it will be
139 called narrowed to the headers with the first element of the rule as
140 the argument.  It should return a non-nil value if it thinks that the
141 mail belongs in that group.
142
143 This variable can also have a function as its value, the function will
144 be called with the headers narrowed and should return a group where it
145 thinks the article should be splitted to.  See `nnimap-split-fancy'.
146
147 To allow for different split rules on different virtual servers, and
148 even different split rules in different inboxes on the same server,
149 the syntax of this variable have been extended along the lines of:
150
151 \(setq nnimap-split-rule
152       '((\"my1server\"    (\".*\"    ((\"ding\"    \"ding@gnus.org\")
153                                   (\"junk\"    \"From:.*Simon\")))
154         (\"my2server\"    (\"INBOX\" nnimap-split-fancy))
155         (\"my[34]server\" (\".*\"    ((\"private\" \"To:.*Simon\")
156                                   (\"junk\"    my-junk-func)))))
157
158 The virtual server name is in fact a regexp, so that the same rules
159 may apply to several servers.  In the example, the servers
160 \"my3server\" and \"my4server\" both use the same rules.  Similarly,
161 the inbox string is also a regexp.  The actual splitting rules are as
162 before, either a function, or a list with group/regexp or
163 group/function elements."
164   :group 'nnimap
165   :type '(choice :tag "Rule type"
166                  (repeat :menu-tag "Single-server"
167                          :tag "Single-server list"
168                          (list (string :tag "Mailbox")
169                                (choice :tag "Predicate"
170                                        (regexp :tag "A regexp")
171                                        (nnimap-strict-function :tag "A function"))))
172                  (choice :menu-tag "A function"
173                          :tag "A function"
174                          (function-item nnimap-split-fancy)
175                          (function-item nnmail-split-fancy)
176                          (nnimap-strict-function :tag "User-defined function"))
177                  (repeat :menu-tag "Multi-server (extended)"
178                          :tag "Multi-server list"
179                          (list (regexp :tag "Server regexp")
180                                (list (regexp :tag "Incoming Mailbox regexp")
181                                      (repeat :tag "Rules for matching server(s) and mailbox(es)"
182                                              (list (string :tag "Destination mailbox")
183                                                    (choice :tag "Predicate"
184                                                            (regexp :tag "A Regexp")
185                                                            (nnimap-strict-function :tag "A Function")))))))))
186
187 (defcustom nnimap-split-predicate "UNSEEN UNDELETED"
188   "The predicate used to find articles to split.
189 If you use another IMAP client to peek on articles but always would
190 like nnimap to split them once it's started, you could change this to
191 \"UNDELETED\". Other available predicates are available in
192 RFC2060 section 6.4.4."
193   :group 'nnimap
194   :type 'string)
195
196 (defcustom nnimap-split-fancy nil
197   "Like the variable `nnmail-split-fancy'."
198   :group 'nnimap
199   :type 'sexp)
200
201 (defvar nnimap-split-download-body-default nil
202   "Internal variable with default value for `nnimap-split-download-body'.")
203
204 (defcustom nnimap-split-download-body 'default
205   "Whether to download entire articles during splitting.
206 This is generally not required, and will slow things down considerably.
207 You may need it if you want to use an advanced splitting function that
208 analyses the body before splitting the article.
209 If this variable is nil, bodies will not be downloaded; if this
210 variable is the symbol `default' the default behaviour is
211 used (which currently is nil, unless you use a statistical
212 spam.el test); if this variable is another non-nil value bodies
213 will be downloaded."
214   :group 'nnimap
215   :type '(choice (const :tag "Let system decide" deault)
216                  boolean))
217
218 ;; Performance / bug workaround variables
219
220 (defcustom nnimap-close-asynchronous t
221   "Close mailboxes asynchronously in `nnimap-close-group'.
222 This means that errors cought by nnimap when closing the mailbox will
223 not prevent Gnus from updating the group status, which may be harmful.
224 However, it increases speed."
225   :type 'boolean
226   :group 'nnimap)
227
228 (defcustom nnimap-dont-close t
229   "Never close mailboxes.
230 This increases the speed of closing mailboxes (quiting group) but may
231 decrease the speed of selecting another mailbox later.  Re-selecting
232 the same mailbox will be faster though."
233   :type 'boolean
234   :group 'nnimap)
235
236 (defcustom nnimap-retrieve-groups-asynchronous t
237   "Send asynchronous STATUS commands for each mailbox before checking mail.
238 If you have mailboxes that rarely receives mail, this speeds up new
239 mail checking.  It works by first sending STATUS commands for each
240 mailbox, and then only checking groups which has a modified UIDNEXT
241 more carefully for new mail.
242
243 In summary, the default is O((1-p)*k+p*n) and changing it to nil makes
244 it O(n).  If p is small, then the default is probably faster."
245   :type 'boolean
246   :group 'nnimap)
247
248 (defvoo nnimap-need-unselect-to-notice-new-mail nil
249   "Unselect mailboxes before looking for new mail in them.
250 Some servers seem to need this under some circumstances.")
251
252 ;; Authorization / Privacy variables
253
254 (defvoo nnimap-auth-method nil
255   "Obsolete.")
256
257 (defvoo nnimap-stream nil
258   "How nnimap will connect to the server.
259
260 The default, nil, will try to use the \"best\" method the server can
261 handle.
262
263 Change this if
264
265 1) you want to connect with TLS/SSL.  The TLS/SSL integration
266    with IMAP is suboptimal so you'll have to tell it
267    specifically.
268
269 2) your server is more capable than your environment -- i.e. your
270    server accept Kerberos login's but you haven't installed the
271    `imtest' program or your machine isn't configured for Kerberos.
272
273 Possible choices: gssapi, kerberos4, starttls, tls, ssl, network, shell.
274 See also `imap-streams' and `imap-stream-alist'.")
275
276 (defvoo nnimap-authenticator nil
277   "How nnimap authenticate itself to the server.
278
279 The default, nil, will try to use the \"best\" method the server can
280 handle.
281
282 There is only one reason for fiddling with this variable, and that is
283 if your server is more capable than your environment -- i.e. you
284 connect to a server that accept Kerberos login's but you haven't
285 installed the `imtest' program or your machine isn't configured for
286 Kerberos.
287
288 Possible choices: gssapi, kerberos4, digest-md5, cram-md5, login, anonymous.
289 See also `imap-authenticators' and `imap-authenticator-alist'")
290
291 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
292   "Directory to keep NOV cache files for nnimap groups.
293 See also `nnimap-nov-file-name'.")
294
295 (defvoo nnimap-nov-file-name "nnimap."
296   "NOV cache base filename.
297 The group name and `nnimap-nov-file-name-suffix' will be appended.  A
298 typical complete file name would be
299 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
300 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
301 `nnmail-use-long-file-names' is nil")
302
303 (defvoo nnimap-nov-file-name-suffix ".novcache"
304   "Suffix for NOV cache base filename.")
305
306 (defvoo nnimap-nov-is-evil gnus-agent
307   "If non-nil, never generate or use a local nov database for this backend.
308 Using nov databases should speed up header fetching considerably.
309 However, it will invoke a UID SEARCH UID command on the server, and
310 some servers implement this command inefficiently by opening each and
311 every message in the group, thus making it quite slow.
312 Unlike other backends, you do not need to take special care if you
313 flip this variable.")
314
315 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
316   "Whether to expunge a group when it is closed.
317 When a IMAP group with articles marked for deletion is closed, this
318 variable determine if nnimap should actually remove the articles or
319 not.
320
321 If always, nnimap always perform a expunge when closing the group.
322 If never, nnimap never expunges articles marked for deletion.
323 If ask, nnimap will ask you if you wish to expunge marked articles.
324
325 When setting this variable to `never', you can only expunge articles
326 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
327
328 (defvoo nnimap-list-pattern "*"
329   "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
330 See below for available wildcards.
331
332 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
333 REFERENCE will be passed as the first parameter to LIST/LSUB.  The
334 semantics of this are server specific, on the University of Washington
335 server you can specify a directory.
336
337 Example:
338  '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
339
340 There are two wildcards * and %. * matches everything, % matches
341 everything in the current hierarchy.")
342
343 (defvoo nnimap-news-groups nil
344   "IMAP support a news-like mode, also known as bulletin board mode,
345 where replies is sent via IMAP instead of SMTP.
346
347 This variable should contain a regexp matching groups where you wish
348 replies to be stored to the mailbox directly.
349
350 Example:
351   '(\"^[^I][^N][^B][^O][^X].*$\")
352
353 This will match all groups not beginning with \"INBOX\".
354
355 Note that there is nothing technically different between mail-like and
356 news-like mailboxes.  If you wish to have a group with todo items or
357 similar which you wouldn't want to set up a mailing list for, you can
358 use this to make replies go directly to the group.")
359
360 (defvoo nnimap-expunge-search-string "UID %s NOT SINCE %s"
361   "IMAP search command to use for articles that are to be expired.
362 The first %s is replaced by a UID set of articles to search on,
363 and the second %s is replaced by a date criterium.
364
365 One useful (and perhaps the only useful) value to change this to would
366 be `UID %s NOT SENTSINCE %s' to make nnimap use the Date: header
367 instead of the internal date of messages.  See section 6.4.4 of RFC
368 2060 for more information on valid strings.")
369
370 (defvoo nnimap-importantize-dormant t
371   "If non-nil, mark \"dormant\" articles as \"ticked\" for other IMAP clients.
372 Note that within Gnus, dormant articles will still (only) be
373 marked as ticked.  This is to make \"dormant\" articles stand out,
374 just like \"ticked\" articles, in other IMAP clients.")
375
376 (defvoo nnimap-server-address nil
377   "Obsolete.  Use `nnimap-address'.")
378
379 (defcustom nnimap-authinfo-file "~/.authinfo"
380   "Authorization information for IMAP servers.  In .netrc format."
381   :type
382   '(choice file
383            (repeat :tag "Entries"
384                    :menu-tag "Inline"
385                    (list :format "%v"
386                          :value ("" ("login" . "") ("password" . ""))
387                    &nb