New directory
[bpt/emacs.git] / lisp / gnus / nnimap.el
1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
5 ;; Jim Radford <radford@robby.caltech.edu>
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; Todo, major things:
28 ;;
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)
32 ;; o Sieve
33 ;; o MIME (partial article fetches)
34 ;; o Split to other backends, different split rules for different
35 ;; servers/inboxes
36 ;;
37 ;; Todo, minor things:
38 ;;
39 ;; o Don't require half of Gnus -- backends should be standalone
40 ;; o Verify that we don't use IMAP4rev1 specific things (RFC2060 App B)
41 ;; o Dont uid fetch 1,* in nnimap-retrive-groups (slow)
42 ;; o Split up big fetches (1,* header especially) in smaller chunks
43 ;; o What do I do with gnus-newsgroup-*?
44 ;; o Tell Gnus about new groups (how can we tell?)
45 ;; o Respooling (fix Gnus?) (unnecessary?)
46 ;; o Add support for the following: (if applicable)
47 ;; request-list-newsgroups, request-regenerate
48 ;; list-active-group,
49 ;; request-associate-buffer, request-restore-buffer,
50 ;; o Do The Right Thing when UIDVALIDITY changes (what's the right thing?)
51 ;; o Support RFC2221 (Login referrals)
52 ;; o IMAP2BIS compatibility? (RFC2061)
53 ;; o ACAP stuff (perhaps a different project, would be nice to ACAPify
54 ;; .newsrc.eld)
55 ;; o What about Gnus's article editing, can we support it? NO!
56 ;; o Use \Draft to support the draft group??
57 ;; o Duplicate suppression
58
59 ;;; Code:
60
61 (eval-and-compile
62 (require 'cl)
63 (require 'imap))
64
65 (require 'nnoo)
66 (require 'nnmail)
67 (require 'nnheader)
68 (require 'mm-util)
69 (require 'gnus)
70 (require 'gnus-range)
71 (require 'gnus-start)
72 (require 'gnus-int)
73
74 (nnoo-declare nnimap)
75
76 (defconst nnimap-version "nnimap 0.131")
77
78 (defvoo nnimap-address nil
79 "Address of physical IMAP server. If nil, use the virtual server's name.")
80
81 (defvoo nnimap-server-port nil
82 "Port number on physical IMAP server.
83 If nil, defaults to 993 for SSL connections and 143 otherwise.")
84
85 ;; Splitting variables
86
87 (defvar nnimap-split-crosspost t
88 "If non-nil, do crossposting if several split methods match the mail.
89 If nil, the first match found will be used.")
90
91 (defvar nnimap-split-inbox nil
92 "*Name of mailbox to split mail from.
93
94 Mail is read from this mailbox and split according to rules in
95 `nnimap-split-rules'.
96
97 This can be a string or a list of strings.")
98
99 (defvar nnimap-split-rule nil
100 "*Mail will be split according to these rules.
101
102 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
103
104 If you'd like, for instance, one mail group for mail from the
105 \"gnus-imap\" mailing list, one group for junk mail and leave
106 everything else in the incoming mailbox, you could do something like
107 this:
108
109 \(setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
110 (\"INBOX.junk\" \"Subject:.*buy\")))
111
112 As you can see, `nnimap-split-rule' is a list of lists, where the first
113 element in each \"rule\" is the name of the IMAP mailbox, and the
114 second is a regexp that nnimap will try to match on the header to find
115 a fit.
116
117 The second element can also be a function. In that case, it will be
118 called narrowed to the headers with the first element of the rule as
119 the argument. It should return a non-nil value if it thinks that the
120 mail belongs in that group.
121
122 This variable can also have a function as its value, the function will
123 be called with the headers narrowed and should return a group where it
124 thinks the article should be splitted to. See `nnimap-split-fancy'.
125
126 To allow for different split rules on different virtual servers, and
127 even different split rules in different inboxes on the same server,
128 the syntax of this variable have been extended along the lines of:
129
130 \(setq nnimap-split-rule
131 '((\"my1server\" (\".*\" ((\"ding\" \"ding@gnus.org\")
132 (\"junk\" \"From:.*Simon\")))
133 (\"my2server\" (\"INBOX\" nnimap-split-fancy))
134 (\"my[34]server\" (\".*\" ((\"private\" \"To:.*Simon\")
135 (\"junk\" my-junk-func)))))
136
137 The virtual server name is in fact a regexp, so that the same rules
138 may apply to several servers. In the example, the servers
139 \"my3server\" and \"my4server\" both use the same rules. Similarly,
140 the inbox string is also a regexp. The actual splitting rules are as
141 before, either a function, or a list with group/regexp or
142 group/function elements.")
143
144 (defvar nnimap-split-predicate "UNSEEN UNDELETED"
145 "The predicate used to find articles to split.
146 If you use another IMAP client to peek on articles but always would
147 like nnimap to split them once it's started, you could change this to
148 \"UNDELETED\". Other available predicates are available in
149 RFC2060 section 6.4.4.")
150
151 (defvar nnimap-split-fancy nil
152 "Like `nnmail-split-fancy', which see.")
153
154 ;; Authorization / Privacy variables
155
156 (defvoo nnimap-auth-method nil
157 "Obsolete.")
158
159 (defvoo nnimap-stream nil
160 "How nnimap will connect to the server.
161
162 The default, nil, will try to use the \"best\" method the server can
163 handle.
164
165 Change this if
166
167 1) you want to connect with SSL. The SSL integration with IMAP is
168 brain-dead so you'll have to tell it specifically.
169
170 2) your server is more capable than your environment -- i.e. your
171 server accept Kerberos login's but you haven't installed the
172 `imtest' program or your machine isn't configured for Kerberos.
173
174 Possible choices: kerberos4, ssl, network")
175
176 (defvoo nnimap-authenticator nil
177 "How nnimap authenticate itself to the server.
178
179 The default, nil, will try to use the \"best\" method the server can
180 handle.
181
182 There is only one reason for fiddling with this variable, and that is
183 if your server is more capable than your environment -- i.e. you
184 connect to a server that accept Kerberos login's but you haven't
185 installed the `imtest' program or your machine isn't configured for
186 Kerberos.
187
188 Possible choices: kerberos4, cram-md5, login, anonymous.")
189
190 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
191 "Directory to keep NOV cache files for nnimap groups.
192 See also `nnimap-nov-file-name'.")
193
194 (defvoo nnimap-nov-file-name "nnimap."
195 "NOV cache base filename.
196 The group name and `nnimap-nov-file-name-suffix' will be appended. A
197 typical complete file name would be
198 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
199 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
200 `nnmail-use-long-file-names' is nil")
201
202 (defvoo nnimap-nov-file-name-suffix ".novcache"
203 "Suffix for NOV cache base filename.")
204
205 (defvoo nnimap-nov-is-evil nil
206 "If non-nil, nnimap will never generate or use a local nov database for this backend.
207 Using nov databases will speed up header fetching considerably.
208 Unlike other backends, you do not need to take special care if you
209 flip this variable.")
210
211 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
212 "Whether to expunge a group when it is closed.
213 When a IMAP group with articles marked for deletion is closed, this
214 variable determine if nnimap should actually remove the articles or
215 not.
216
217 If always, nnimap always perform a expunge when closing the group.
218 If never, nnimap never expunges articles marked for deletion.
219 If ask, nnimap will ask you if you wish to expunge marked articles.
220
221 When setting this variable to `never', you can only expunge articles
222 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
223
224 (defvoo nnimap-list-pattern "*"
225 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
226 See below for available wildcards.
227
228 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
229 REFERENCE will be passed as the first parameter to LIST/LSUB. The
230 semantics of this are server specific, on the University of Washington
231 server you can specify a directory.
232
233 Example:
234 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
235
236 There are two wildcards * and %. * matches everything, % matches
237 everything in the current hierarchy.")
238
239 (defvoo nnimap-news-groups nil
240 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
241
242 This variable should contain a regexp matching groups where you wish
243 replies to be stored to the mailbox directly.
244
245 Example:
246 '(\"^[^I][^N][^B][^O][^X].*$\")
247
248 This will match all groups not beginning with \"INBOX\".
249
250 Note that there is nothing technically different between mail-like and
251 news-like mailboxes. If you wish to have a group with todo items or
252 similar which you wouldn't want to set up a mailing list for, you can
253 use this to make replies go directly to the group.")
254
255 (defvoo nnimap-server-address nil
256 "Obsolete. Use `nnimap-address'.")
257
258 (defcustom nnimap-authinfo-file "~/.authinfo"
259 "Authorization information for IMAP servers. In .netrc format."
260 :type
261 '(choice file
262 (repeat :tag "Entries"
263 :menu-tag "Inline"
264 (list :format "%v"
265 :value ("" ("login" . "") ("password" . ""))
266 (string :tag "Host")
267 (checklist :inline t
268 (cons :format "%v"
269 (const :format "" "login")
270 (string :format "Login: %v"))
271 (cons :format "%v"
272 (const :format "" "password")
273 (string :format "Password: %v")))))))
274
275 (defcustom nnimap-prune-cache t
276 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
277 :type 'boolean)
278
279 (defvar nnimap-request-list-method 'imap-mailbox-list
280 "Method to use to request a list of all folders from the server.
281 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
282 restrict visible folders.")
283
284 ;; Internal variables:
285
286 (defvar nnimap-debug nil
287 "Name of buffer to record debugging info.
288 For example: (setq nnimap-debug \"*nnimap-debug*\")")
289 (defvar nnimap-current-move-server nil)
290 (defvar nnimap-current-move-group nil)
291 (defvar nnimap-current-move-article nil)
292 (defvar nnimap-length)
293 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
294 (defvar nnimap-progress-how-often 20)
295 (defvar nnimap-counter)
296 (defvar nnimap-callback-callback-function nil
297 "Gnus callback the nnimap asynchronous callback should call.")
298 (defvar nnimap-callback-buffer nil
299 "Which buffer the asynchronous article prefetch callback should work in.")
300 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
301 (defvar nnimap-current-server nil) ;; Current server
302 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
303
304 \f
305
306 (nnoo-define-basics nnimap)
307
308 ;; Utility functions:
309
310 (defsubst nnimap-get-server-buffer (server)
311 "Return buffer for SERVER, if nil use current server."
312 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
313
314 (defun nnimap-possibly-change-server (server)
315 "Return buffer for SERVER, changing the current server as a side-effect.
316 If SERVER is nil, uses the current server."
317 (setq nnimap-current-server (or server nnimap-current-server)
318 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
319
320 (defun nnimap-verify-uidvalidity (group server)
321 "Verify stored uidvalidity match current one in GROUP on SERVER."
322 (let* ((gnusgroup (gnus-group-prefixed-name
323 group (gnus-server-to-method
324 (format "nnimap:%s" server))))
325 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
326 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity))
327 (dir (file-name-as-directory (expand-file-name nnimap-directory)))
328 (nameuid (nnheader-translate-file-chars
329 (concat nnimap-nov-file-name
330 (if (equal server "")
331 "unnamed"
332 server) "." group "." old-uidvalidity
333 nnimap-nov-file-name-suffix) t))
334 (file (if (or nnmail-use-long-file-names
335 (file-exists-p (expand-file-name nameuid dir)))
336 (expand-file-name nameuid dir)
337 (expand-file-name
338 (mm-encode-coding-string
339 (nnheader-replace-chars-in-string nameuid ?. ?/)
340 nnmail-pathname-coding-system)
341 dir))))
342 (if old-uidvalidity
343 (if (not (equal old-uidvalidity new-uidvalidity))
344 ;; uidvalidity clash
345 (gnus-delete-file file)
346 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
347 t)
348 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
349 t)))
350
351 (defun nnimap-before-find-minmax-bugworkaround ()
352 "Function called before iterating through mailboxes with
353 `nnimap-find-minmax-uid'."
354 ;; XXX this is for UoW imapd problem, it doesn't notice new mail in
355 ;; currently selected mailbox without a re-select/examine.
356 (or (null (imap-current-mailbox nnimap-server-buffer))
357 (imap-mailbox-unselect nnimap-server-buffer)))
358
359 (defun nnimap-find-minmax-uid (group &optional examine)
360 "Find lowest and highest active article number in GROUP.
361 If EXAMINE is non-nil the group is selected read-only."
362 (with-current-buffer nnimap-server-buffer
363 (when (imap-mailbox-select group examine)
364 (let (minuid maxuid)
365 (when (> (imap-mailbox-get 'exists) 0)
366 (imap-fetch "1,*" "UID" nil 'nouidfetch)
367 (imap-message-map (lambda (uid Uid)
368 (setq minuid (if minuid (min minuid uid) uid)
369 maxuid (if maxuid (max maxuid uid) uid)))
370 'UID))
371 (list (imap-mailbox-get 'exists) minuid maxuid)))))
372
373 (defun nnimap-possibly-change-group (group &optional server)
374 "Make GROUP the current group, and SERVER the current server."
375 (when (nnimap-possibly-change-server server)
376 (with-current-buffer nnimap-server-buffer
377 (if (or (null group) (imap-current-mailbox-p group))
378 imap-current-mailbox
379 (if (imap-mailbox-select group)
380 (if (or (nnimap-verify-uidvalidity
381 group (or server nnimap-current-server))
382 (zerop (imap-mailbox-get 'exists group))
383 (yes-or-no-p
384 (format
385 "nnimap: Group %s is not uidvalid. Continue? " group)))
386 imap-current-mailbox
387 (imap-mailbox-unselect)
388 (error "nnimap: Group %s is not uid-valid" group))
389 (nnheader-report 'nnimap (imap-error-text)))))))
390
391 (defun nnimap-replace-whitespace (string)
392 "Return STRING with all whitespace replaced with space."
393 (when string
394 (while (string-match "[\r\n\t]+" string)
395 (setq string (replace-match " " t t string)))
396 string))
397
398 ;; Required backend functions
399
400 (defun nnimap-retrieve-headers-progress ()
401 "Hook to insert NOV line for current article into `nntp-server-buffer'."
402 (and (numberp nnmail-large-newsgroup)
403 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
404 (> nnimap-length nnmail-large-newsgroup)
405 (nnheader-message 6 "nnimap: Retrieving headers... %c"
406 (nth (/ (% nnimap-counter
407 (* (length nnimap-progress-chars)
408 nnimap-progress-how-often))
409 nnimap-progress-how-often)
410 nnimap-progress-chars)))
411 (with-current-buffer nntp-server-buffer
412 (let (headers lines chars uid mbx)
413 (with-current-buffer nnimap-server-buffer
414 (setq uid imap-current-message
415 mbx imap-current-mailbox
416 headers (nnimap-demule
417 (if (imap-capability 'IMAP4rev1)
418 ;; xxx don't just use car? alist doesn't contain
419 ;; anything else now, but it might...
420 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
421 (imap-message-get uid 'RFC822.HEADER)))
422 lines (imap-body-lines (imap-message-body imap-current-message))
423 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
424 (nnheader-insert-nov
425 (with-temp-buffer
426 (buffer-disable-undo)
427 (insert headers)
428 (nnheader-ms-strip-cr)
429 (nnheader-fold-continuation-lines)
430 (subst-char-in-region (point-min) (point-max) ?\t ? )
431 (let ((head (nnheader-parse-head 'naked)))
432 (mail-header-set-number head uid)
433 (mail-header-set-chars head chars)
434 (mail-header-set-lines head lines)
435 (mail-header-set-xref
436 head (format "%s %s:%d" (system-name) mbx uid))
437 head))))))
438
439 (defun nnimap-retrieve-which-headers (articles fetch-old)
440 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
441 (with-current-buffer nnimap-server-buffer
442 (if (numberp (car-safe articles))
443 (imap-search
444 (concat "UID "
445 (imap-range-to-message-set
446 (gnus-compress-sequence
447 (append (gnus-uncompress-sequence
448 (and fetch-old
449 (cons (if (numberp fetch-old)
450 (max 1 (- (car articles) fetch-old))
451 1)
452 (1- (car articles)))))
453 articles)))))
454 (mapcar (lambda (msgid)
455 (imap-search
456 (format "HEADER Message-Id %s" msgid)))
457 articles))))
458
459 (defun nnimap-group-overview-filename (group server)
460 "Make file name for GROUP on SERVER."
461 (let* ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
462 (uidvalidity (gnus-group-get-parameter
463 (gnus-group-prefixed-name
464 group (gnus-server-to-method
465 (format "nnimap:%s" server)))
466 'uidvalidity))
467 (name (nnheader-translate-file-chars
468 (concat nnimap-nov-file-name
469 (if (equal server "")
470 "unnamed"
471 server) "." group nnimap-nov-file-name-suffix) t))
472 (nameuid (nnheader-translate-file-chars
473 (concat nnimap-nov-file-name
474 (if (equal server "")
475 "unnamed"
476 server) "." group "." uidvalidity
477 nnimap-nov-file-name-suffix) t))
478 (oldfile (if (or nnmail-use-long-file-names
479 (file-exists-p (expand-file-name name dir)))
480 (expand-file-name name dir)
481 (expand-file-name
482 (mm-encode-coding-string
483 (nnheader-replace-chars-in-string name ?. ?/)
484 nnmail-pathname-coding-system)
485 dir)))
486 (newfile (if (or nnmail-use-long-file-names
487 (file-exists-p (expand-file-name nameuid dir)))
488 (expand-file-name nameuid dir)
489 (expand-file-name
490 (mm-encode-coding-string
491 (nnheader-replace-chars-in-string nameuid ?. ?/)
492 nnmail-pathname-coding-system)
493 dir))))
494 (when (and (file-exists-p oldfile) (not (file-exists-p newfile)))
495 (message "nnimap: Upgrading novcache filename...")
496 (sit-for 1)
497 (gnus-make-directory (file-name-directory newfile))
498 (unless (ignore-errors (rename-file oldfile newfile) t)
499 (if (ignore-errors (copy-file oldfile newfile) t)
500 (delete-file oldfile)
501 (error "Can't rename `%s' to `%s'" oldfile newfile))))
502 newfile))
503
504 (defun nnimap-retrieve-headers-from-file (group server)
505 (with-current-buffer nntp-server-buffer
506 (let ((nov (nnimap-group-overview-filename group server)))
507 (when (file-exists-p nov)
508 (mm-insert-file-contents nov)
509 (set-buffer-modified-p nil)
510 (let ((min (ignore-errors (goto-char (point-min))
511 (read (current-buffer))))
512 (max (ignore-errors (goto-char (point-max))
513 (forward-line -1)
514 (read (current-buffer)))))
515 (if (and (numberp min) (numberp max))
516 (cons min max)
517 ;; junk, remove it, it's saved later
518 (erase-buffer)
519 nil))))))
520
521 (defun nnimap-retrieve-headers-from-server (articles group server)
522 (with-current-buffer nnimap-server-buffer
523 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
524 (nnimap-length (gnus-range-length articles))
525 (nnimap-counter 0))
526 (imap-fetch (imap-range-to-message-set articles)
527 (concat "(UID RFC822.SIZE BODY "
528 (let ((headers
529 (append '(Subject From Date Message-Id
530 References In-Reply-To Xref)
531 (copy-sequence
532 nnmail-extra-headers))))
533 (if (imap-capability 'IMAP4rev1)
534 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
535 (format "RFC822.HEADER.LINES %s)" headers)))))
536 (and (numberp nnmail-large-newsgroup)
537 (> nnimap-length nnmail-large-newsgroup)
538 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
539
540 (defun nnimap-use-nov-p (group server)
541 (or gnus-nov-is-evil nnimap-nov-is-evil
542 (unless (and (gnus-make-directory
543 (file-name-directory
544 (nnimap-group-overview-filename group server)))
545 (file-writable-p
546 (nnimap-group-overview-filename group server)))
547 (message "nnimap: Nov cache not writable, %s"
548 (nnimap-group-overview-filename group server)))))
549
550 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
551 (when (nnimap-possibly-change-group group server)
552 (with-current-buffer nntp-server-buffer
553 (erase-buffer)
554 (if (nnimap-use-nov-p group server)
555 (nnimap-retrieve-headers-from-server
556 (gnus-compress-sequence articles) group server)
557 (let (uids cached low high)
558 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
559 low (car uids)
560 high (car (last uids)))
561 (if (setq cached (nnimap-retrieve-headers-from-file group server))
562 (progn
563 ;; fetch articles with uids before cache block
564 (when (< low (car cached))
565 (goto-char (point-min))
566 (nnimap-retrieve-headers-from-server
567 (cons low (1- (car cached))) group server))
568 ;; fetch articles with uids after cache block
569 (when (> high (cdr cached))
570 (goto-char (point-max))
571 (nnimap-retrieve-headers-from-server
572 (cons (1+ (cdr cached)) high) group server))
573 (when nnimap-prune-cache
574 ;; remove nov's for articles which has expired on server
575 (goto-char (point-min))
576 (dolist (uid (gnus-set-difference articles uids))
577 (when (re-search-forward (format "^%d\t" uid) nil t)
578 (gnus-delete-line)))))
579 ;; nothing cached, fetch whole range from server
580 (nnimap-retrieve-headers-from-server
581 (cons low high) group server))
582 (when (buffer-modified-p)
583 (nnmail-write-region
584 (point-min) (point-max)
585 (nnimap-group-overview-filename group server) nil 'nomesg))
586 (nnheader-nov-delete-outside-range low high))))
587 'nov)))
588
589 (defun nnimap-open-connection (server)
590 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
591 nnimap-authenticator nnimap-server-buffer))
592 (nnheader-report 'nnimap "Can't open connection to server %s" server)
593 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
594 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
595 (imap-close nnimap-server-buffer)
596 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
597 (let* ((list (gnus-parse-netrc nnimap-authinfo-file))
598 (port (if nnimap-server-port
599 (int-to-string nnimap-server-port)
600 "imap"))
601 (alist (gnus-netrc-machine list (or nnimap-server-address
602 nnimap-address server)
603 port "imap"))
604 (user (gnus-netrc-get alist "login"))
605 (passwd (gnus-netrc-get alist "password")))
606 (if (imap-authenticate user passwd nnimap-server-buffer)
607 (prog1
608 (push (list server nnimap-server-buffer)
609 nnimap-server-buffer-alist)
610 (nnimap-possibly-change-server server))
611 (imap-close nnimap-server-buffer)
612 (kill-buffer nnimap-server-buffer)
613 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
614
615 (deffoo nnimap-open-server (server &optional defs)
616 (nnheader-init-server-buffer)
617 (if (nnimap-server-opened server)
618 t
619 (unless (assq 'nnimap-server-buffer defs)
620 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
621 ;; translate `nnimap-server-address' to `nnimap-address' in defs
622 ;; for people that configured nnimap with a very old version
623 (unless (assq 'nnimap-address defs)
624 (if (assq 'nnimap-server-address defs)
625 (push (list 'nnimap-address
626 (cadr (assq 'nnimap-server-address defs))) defs)
627 (push (list 'nnimap-address server) defs)))
628 (nnoo-change-server 'nnimap server defs)
629 (with-current-buffer (get-buffer-create nnimap-server-buffer)
630 (nnoo-change-server 'nnimap server defs))
631 (or (and nnimap-server-buffer
632 (imap-opened nnimap-server-buffer))
633 (nnimap-open-connection server))))
634
635 (deffoo nnimap-server-opened (&optional server)
636 "Whether SERVER is opened.
637 If SERVER is the current virtual server, and the connection to the
638 physical server is alive, this function return a non-nil value. If
639 SERVER is nil, it is treated as the current server."
640 ;; clean up autologouts??
641 (and (or server nnimap-current-server)
642 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
643 (imap-opened (nnimap-get-server-buffer server))))
644
645 (deffoo nnimap-close-server (&optional server)
646 "Close connection to server and free all resources connected to it.
647 Return nil if the server couldn't be closed for some reason."
648 (let ((server (or server nnimap-current-server)))
649 (when (or (nnimap-server-opened server)
650 (imap-opened (nnimap-get-server-buffer server)))
651 (imap-close (nnimap-get-server-buffer server))
652 (kill-buffer (nnimap-get-server-buffer server))
653 (setq nnimap-server-buffer nil
654 nnimap-current-server nil
655 nnimap-server-buffer-alist
656 (delq server nnimap-server-buffer-alist)))
657 (nnoo-close-server 'nnimap server)))
658
659 (deffoo nnimap-request-close ()
660 "Close connection to all servers and free all resources that the backend have reserved.
661 All buffers that have been created by that
662 backend should be killed. (Not the nntp-server-buffer, though.) This
663 function is generally only called when Gnus is shutting down."
664 (mapcar (lambda (server) (nnimap-close-server (car server)))
665 nnimap-server-buffer-alist)
666 (setq nnimap-server-buffer-alist nil))
667
668 (deffoo nnimap-status-message (&optional server)
669 "This function returns the last error message from server."
670 (when (nnimap-possibly-change-server server)
671 (nnoo-status-message 'nnimap server)))
672
673 (defun nnimap-demule (string)
674 (funcall (if (and (fboundp 'string-as-multibyte)
675 (subrp (symbol-function 'string-as-multibyte)))
676 'string-as-multibyte
677 'identity)
678 (or string "")))
679
680 (defun nnimap-callback ()
681 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
682 (with-current-buffer nnimap-callback-buffer
683 (insert
684 (with-current-buffer nnimap-server-buffer
685 (nnimap-demule
686 (if (imap-capability 'IMAP4rev1)
687 ;; xxx don't just use car? alist doesn't contain
688 ;; anything else now, but it might...
689 (nth 2 (car (imap-message-get (imap-current-message) 'BODYDETAIL)))
690 (imap-message-get (imap-current-message) 'RFC822)))))
691 (nnheader-ms-strip-cr)
692 (funcall nnimap-callback-callback-function t)))
693
694 (defun nnimap-request-article-part (article part prop &optional
695 group server to-buffer detail)
696 (when (nnimap-possibly-change-group group server)
697 (let ((article (if (stringp article)
698 (car-safe (imap-search
699 (format "HEADER Message-Id %s" article)
700 nnimap-server-buffer))
701 article)))
702 (when article
703 (gnus-message 10 "nnimap: Fetching (part of) article %d..." article)
704 (if (not nnheader-callback-function)
705 (with-current-buffer (or to-buffer nntp-server-buffer)
706 (erase-buffer)
707 (let ((data (imap-fetch article part prop nil
708 nnimap-server-buffer)))
709 (insert (nnimap-demule (if detail
710 (nth 2 (car data))
711 data))))
712 (nnheader-ms-strip-cr)
713 (gnus-message 10 "nnimap: Fetching (part of) article %d...done"
714 article)
715 (if (bobp)
716 (nnheader-report 'nnimap "No such article: %s"
717 (imap-error-text nnimap-server-buffer))
718 (cons group article)))
719 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
720 (setq nnimap-callback-callback-function nnheader-callback-function
721 nnimap-callback-buffer nntp-server-buffer)
722 (imap-fetch-asynch article part nil nnimap-server-buffer)
723 (cons group article))))))
724
725 (deffoo nnimap-asynchronous-p ()
726 t)
727
728 (deffoo nnimap-request-article (article &optional group server to-buffer)
729 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
730 (nnimap-request-article-part
731 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
732 (nnimap-request-article-part
733 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
734
735 (deffoo nnimap-request-head (article &optional group server to-buffer)
736 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
737 (nnimap-request-article-part
738 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
739 (nnimap-request-article-part
740 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
741
742 (deffoo nnimap-request-body (article &optional group server to-buffer)
743 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
744 (nnimap-request-article-part
745 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
746 (nnimap-request-article-part
747 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
748
749 (deffoo nnimap-request-group (group &optional server fast)
750 (nnimap-request-update-info-internal
751 group
752 (gnus-get-info (gnus-group-prefixed-name
753 group (gnus-server-to-method (format "nnimap:%s" server))))
754 server)
755 (when (nnimap-possibly-change-group group server)
756 (nnimap-before-find-minmax-bugworkaround)
757 (let (info)
758 (cond (fast group)
759 ((null (setq info (nnimap-find-minmax-uid group t)))
760 (nnheader-report 'nnimap "Could not get active info for %s"
761 group))
762 (t
763 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
764 (max 1 (or (nth 1 info) 1))
765 (or (nth 2 info) 0) group)
766 (nnheader-report 'nnimap "Group %s selected" group)
767 t)))))
768
769 (defun nnimap-close-group (group &optional server)
770 (with-current-buffer nnimap-server-buffer
771 (when (and (imap-opened)
772 (nnimap-possibly-change-group group server))
773 (case nnimap-expunge-on-close
774 ('always (imap-mailbox-expunge)
775 (imap-mailbox-close))
776 ('ask (if (and (imap-search "DELETED")
777 (gnus-y-or-n-p (format
778 "Expunge articles in group `%s'? "
779 imap-current-mailbox)))
780 (progn (imap-mailbox-expunge)
781 (imap-mailbox-close))
782 (imap-mailbox-unselect)))
783 (t (imap-mailbox-unselect)))
784 (not imap-current-mailbox))))
785
786 (defun nnimap-pattern-to-list-arguments (pattern)
787 (mapcar (lambda (p)
788 (cons (car-safe p) (or (cdr-safe p) p)))
789 (if (and (listp pattern)
790 (listp (cdr pattern)))
791 pattern
792 (list pattern))))
793
794 (deffoo nnimap-request-list (&optional server)
795 (when (nnimap-possibly-change-server server)
796 (with-current-buffer nntp-server-buffer
797 (erase-buffer))
798 (gnus-message 5 "nnimap: Generating active list%s..."
799 (if (> (length server) 0) (concat " for " server) ""))
800 (nnimap-before-find-minmax-bugworkaround)
801 (with-current-buffer nnimap-server-buffer
802 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
803 (dolist (mbx (funcall nnimap-request-list-method
804 (cdr pattern) (car pattern)))
805 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
806 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
807 (when info
808 (with-current-buffer nntp-server-buffer
809 (insert (format "\"%s\" %d %d y\n"
810 mbx (or (nth 2 info) 0)
811 (max 1 (or (nth 1 info) 1)))))))))))
812 (gnus-message 5 "nnimap: Generating active list%s...done"
813 (if (> (length server) 0) (concat " for " server) ""))
814 t))
815
816 (deffoo nnimap-request-post (&optional server)
817 (let ((success t))
818 (dolist (mbx (message-unquote-tokens
819 (message-tokenize-header
820 (message-fetch-field "Newsgroups") ", ")) success)
821 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
822 (or (gnus-active to-newsgroup)
823 (gnus-activate-group to-newsgroup)
824 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
825 to-newsgroup))
826 (or (and (gnus-request-create-group
827 to-newsgroup gnus-command-method)
828 (gnus-activate-group to-newsgroup nil nil
829 gnus-command-method))
830 (error "Couldn't create group %s" to-newsgroup)))
831 (error "No such group: %s" to-newsgroup))
832 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
833 (setq success nil))))))
834
835 ;; Optional backend functions
836
837 (deffoo nnimap-retrieve-groups (groups &optional server)
838 (when (nnimap-possibly-change-server server)
839 (gnus-message 5 "nnimap: Checking mailboxes...")
840 (with-current-buffer nntp-server-buffer
841 (erase-buffer)
842 (nnimap-before-find-minmax-bugworkaround)
843 (dolist (group groups)
844 (gnus-message 7 "nnimap: Checking mailbox %s" group)
845 (or (member "\\NoSelect"
846 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
847 (let ((info (nnimap-find-minmax-uid group 'examine)))
848 (insert (format "\"%s\" %d %d y\n" group
849 (or (nth 2 info) 0)
850 (max 1 (or (nth 1 info) 1))))))))
851 (gnus-message 5 "nnimap: Checking mailboxes...done")
852 'active))
853
854 (deffoo nnimap-request-update-info-internal (group info &optional server)
855 (when (nnimap-possibly-change-group group server)
856 (when info;; xxx what does this mean? should we create a info?
857 (with-current-buffer nnimap-server-buffer
858 (gnus-message 5 "nnimap: Updating info for %s..."
859 (gnus-info-group info))
860
861 (when (nnimap-mark-permanent-p 'read)
862 (let (seen unseen)
863 ;; read info could contain articles marked unread by other
864 ;; imap clients! we correct this
865 (setq seen (gnus-uncompress-range (gnus-info-read info))
866 unseen (imap-search "UNSEEN UNDELETED")
867 seen (gnus-set-difference seen unseen)
868 ;; seen might lack articles marked as read by other
869 ;; imap clients! we correct this
870 seen (append seen (imap-search "SEEN"))
871 ;; remove dupes
872 seen (sort seen '<)
873 seen (gnus-compress-sequence seen t)
874 ;; we can't return '(1) since this isn't a "list of ranges",
875 ;; and we can't return '((1)) since g-list-of-unread-articles
876 ;; is buggy so we return '((1 . 1)).
877 seen (if (and (integerp (car seen))
878 (null (cdr seen)))
879 (list (cons (car seen) (car seen)))
880 seen))
881 (gnus-info-set-read info seen)))
882
883 (mapcar (lambda (pred)
884 (when (and (nnimap-mark-permanent-p (cdr pred))
885 (member (nnimap-mark-to-flag (cdr pred))
886 (imap-mailbox-get 'flags)))
887 (gnus-info-set-marks
888 info
889 (nnimap-update-alist-soft
890 (cdr pred)
891 (gnus-compress-sequence
892 (imap-search (nnimap-mark-to-predicate (cdr pred))))
893 (gnus-info-marks info))
894 t)))
895 gnus-article-mark-lists)
896
897 ;; nnimap mark dormant article as ticked too (for other clients)
898 ;; so we remove that mark for gnus since we support dormant
899 (gnus-info-set-marks
900 info
901 (nnimap-update-alist-soft
902 'tick
903 (gnus-remove-from-range
904 (cdr-safe (assoc 'tick (gnus-info-marks info)))
905 (cdr-safe (assoc 'dormant (gnus-info-marks info))))
906 (gnus-info-marks info))
907 t)
908
909 (gnus-message 5 "nnimap: Updating info for %s...done"
910 (gnus-info-group info))
911
912 info))))
913
914 (deffoo nnimap-request-type (group &optional article)
915 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
916 'news
917 'mail))
918
919 (deffoo nnimap-request-set-mark (group actions &optional server)
920 (when (nnimap-possibly-change-group group server)
921 (with-current-buffer nnimap-server-buffer
922 (let (action)
923 (gnus-message 7 "nnimap: Setting marks in %s..." group)
924 (while (setq action (pop actions))
925 (let ((range (nth 0 action))
926 (what (nth 1 action))
927 (cmdmarks (nth 2 action))
928 marks)
929 ;; cache flags are pointless on the server
930 (setq cmdmarks (delq 'cache cmdmarks))
931 ;; flag dormant articles as ticked
932 (if (memq 'dormant cmdmarks)
933 (setq cmdmarks (cons 'tick cmdmarks)))
934 ;; remove stuff we are forbidden to store
935 (mapcar (lambda (mark)
936 (if (imap-message-flag-permanent-p
937 (nnimap-mark-to-flag mark))
938 (setq marks (cons mark marks))))
939 cmdmarks)
940 (when (and range marks)
941 (cond ((eq what 'del)
942 (imap-message-flags-del
943 (imap-range-to-message-set range)
944 (nnimap-mark-to-flag marks nil t)))
945 ((eq what 'add)
946 (imap-message-flags-add
947 (imap-range-to-message-set range)
948 (nnimap-mark-to-flag marks nil t)))
949 ((eq what 'set)
950 (imap-message-flags-set
951 (imap-range-to-message-set range)
952 (nnimap-mark-to-flag marks nil t)))))))
953 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
954 nil)
955
956 (defun nnimap-split-fancy ()
957 "Like nnmail-split-fancy, but uses nnimap-split-fancy."
958 (let ((nnmail-split-fancy nnimap-split-fancy))
959 (nnmail-split-fancy)))
960
961 (defun nnimap-split-to-groups (rules)
962 ;; tries to match all rules in nnimap-split-rule against content of
963 ;; nntp-server-buffer, returns a list of groups that matched.
964 (with-current-buffer nntp-server-buffer
965 ;; Fold continuation lines.
966 (goto-char (point-min))
967 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
968 (replace-match " " t t))
969 (if (functionp rules)
970 (funcall rules)
971 (let (to-groups regrepp)
972 (catch 'split-done
973 (dolist (rule rules to-groups)
974 (let ((group (car rule))
975 (regexp (cadr rule)))
976 (goto-char (point-min))
977 (when (and (if (stringp regexp)
978 (progn
979 (setq regrepp (string-match "\\\\[0-9&]" group))
980 (re-search-forward regexp nil t))
981 (funcall regexp group))
982 ;; Don't enter the article into the same group twice.
983 (not (assoc group to-groups)))
984 (push (if regrepp
985 (nnmail-expand-newtext group)
986 group)
987 to-groups)
988 (or nnimap-split-crosspost
989 (throw 'split-done to-groups))))))))))
990
991 (defun nnimap-assoc-match (key alist)
992 (let (element)
993 (while (and alist (not element))
994 (if (string-match (car (car alist)) key)
995 (setq element (car alist)))
996 (setq alist (cdr alist)))
997 element))
998
999 (defun nnimap-split-find-rule (server inbox)
1000 (if (and (listp nnimap-split-rule) (listp (car nnimap-split-rule))
1001 (list (cdar nnimap-split-rule)) (listp (cadar nnimap-split-rule)))
1002 ;; extended format
1003 (cadr (nnimap-assoc-match inbox (cdr (nnimap-assoc-match
1004 server nnimap-split-rule))))
1005 nnimap-split-rule))
1006
1007 (defun nnimap-split-find-inbox (server)
1008 (if (listp nnimap-split-inbox)
1009 nnimap-split-inbox
1010 (list nnimap-split-inbox)))
1011
1012 (defun nnimap-split-articles (&optional group server)
1013 (when (nnimap-possibly-change-server server)
1014 (with-current-buffer nnimap-server-buffer
1015 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
1016 ;; iterate over inboxes
1017 (while (and (setq inbox (pop inboxes))
1018 (nnimap-possibly-change-group inbox));; SELECT
1019 ;; find split rule for this server / inbox
1020 (when (setq rule (nnimap-split-find-rule server inbox))
1021 ;; iterate over articles
1022 (dolist (article (imap-search nnimap-split-predicate))
1023 (when (nnimap-request-head article)
1024 ;; copy article to right group(s)
1025 (setq removeorig nil)
1026 (dolist (to-group (nnimap-split-to-groups rule))
1027 (if (imap-message-copy (number-to-string article)
1028 to-group nil 'nocopyuid)
1029 (progn
1030 (message "IMAP split moved %s:%s:%d to %s" server inbox
1031 article to-group)
1032 (setq removeorig t)
1033 ;; Add the group-art list to the history list.
1034 (push (list (cons to-group 0)) nnmail-split-history))
1035 (message "IMAP split failed to move %s:%s:%d to %s" server
1036 inbox article to-group)))
1037 ;; remove article if it was successfully copied somewhere
1038 (and removeorig
1039 (imap-message-flags-add (format "%d" article)
1040 "\\Seen \\Deleted")))))
1041 (when (imap-mailbox-select inbox);; just in case
1042 ;; todo: UID EXPUNGE (if available) to remove splitted articles
1043 (imap-mailbox-expunge)
1044 (imap-mailbox-close)))
1045 t))))
1046
1047 (deffoo nnimap-request-scan (&optional group server)
1048 (nnimap-split-articles group server))
1049
1050 (deffoo nnimap-request-newgroups (date &optional server)
1051 (when (nnimap-possibly-change-server server)
1052 (with-current-buffer nntp-server-buffer
1053 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
1054 (if (> (length server) 0) " on " "") server)
1055 (erase-buffer)
1056 (nnimap-before-find-minmax-bugworkaround)
1057 (dolist (pattern (nnimap-pattern-to-list-arguments
1058 nnimap-list-pattern))
1059 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
1060 nnimap-server-buffer))
1061 (or (catch 'found
1062 (dolist (mailbox (imap-mailbox-get 'list-flags mbx
1063 nnimap-server-buffer))
1064 (if (string= (downcase mailbox) "\\noselect")
1065 (throw 'found t)))
1066 nil)
1067 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
1068 (when info
1069 (insert (format "\"%s\" %d %d y\n"
1070 mbx (or (nth 2 info) 0)
1071 (max 1 (or (nth 1 info) 1)))))))))
1072 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
1073 (if (> (length server) 0) " on " "") server))
1074 t))
1075
1076 (deffoo nnimap-request-create-group (group &optional server args)
1077 (when (nnimap-possibly-change-server server)
1078 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
1079 (imap-mailbox-create group nnimap-server-buffer))))
1080
1081 (defun nnimap-time-substract (time1 time2)
1082 "Return TIME for TIME1 - TIME2."
1083 (let* ((ms (- (car time1) (car time2)))
1084 (ls (- (nth 1 time1) (nth 1 time2))))
1085 (if (< ls 0)
1086 (list (- ms 1) (+ (expt 2 16) ls))
1087 (list ms ls))))
1088
1089 (defun nnimap-date-days-ago (daysago)
1090 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
1091 (let* ((time (nnimap-time-substract (current-time) (days-to-time daysago)))
1092 (date (format-time-string
1093 (format "%%d-%s-%%Y"
1094 (capitalize (car (rassoc (nth 4 (decode-time time))
1095 parse-time-months))))
1096 time)))
1097 (if (eq ?0 (string-to-char date))
1098 (substring date 1)
1099 date)))
1100
1101 (defun nnimap-request-expire-articles-progress ()
1102 (gnus-message 5 "nnimap: Marking article %d for deletion..."
1103 imap-current-message))
1104
1105 ;; Notice that we don't actually delete anything, we just mark them deleted.
1106 (deffoo nnimap-request-expire-articles (articles group &optional server force)
1107 (let ((artseq (gnus-compress-sequence articles)))
1108 (when (and artseq (nnimap-possibly-change-group group server))
1109 (with-current-buffer nnimap-server-buffer
1110 (if force
1111 (and (imap-message-flags-add
1112 (imap-range-to-message-set artseq) "\\Deleted")
1113 (setq articles nil))
1114 (let ((days (or (and nnmail-expiry-wait-function
1115 (funcall nnmail-expiry-wait-function group))
1116 nnmail-expiry-wait)))
1117 (cond ((eq days 'immediate)
1118 (and (imap-message-flags-add
1119 (imap-range-to-message-set artseq) "\\Deleted")
1120 (setq articles nil)))
1121 ((numberp days)
1122 (let ((oldarts (imap-search
1123 (format "UID %s NOT SINCE %s"
1124 (imap-range-to-message-set artseq)
1125 (nnimap-date-days-ago days))))
1126 (imap-fetch-data-hook
1127 '(nnimap-request-expire-articles-progress)))
1128 (and oldarts
1129 (imap-message-flags-add
1130 (imap-range-to-message-set
1131 (gnus-compress-sequence oldarts))
1132 "\\Deleted")
1133 (setq articles (gnus-set-difference
1134 articles oldarts)))))))))))
1135 ;; return articles not deleted
1136 articles)
1137
1138 (deffoo nnimap-request-move-article (article group server
1139 accept-form &optional last)
1140 (when (nnimap-possibly-change-server server)
1141 (save-excursion
1142 (let ((buf (get-buffer-create " *nnimap move*"))
1143 (nnimap-current-move-article article)
1144 (nnimap-current-move-group group)
1145 (nnimap-current-move-server nnimap-current-server)
1146 result)
1147 (and (nnimap-request-article article group server)
1148 (save-excursion
1149 (set-buffer buf)
1150 (buffer-disable-undo (current-buffer))
1151 (insert-buffer-substring nntp-server-buffer)
1152 (setq result (eval accept-form))
1153 (kill-buffer buf)
1154 result)
1155 (nnimap-request-expire-articles (list article) group server t))
1156 result))))
1157
1158 (deffoo nnimap-request-accept-article (group &optional server last)
1159 (when (nnimap-possibly-change-server server)
1160 (let (uid)
1161 (if (setq uid
1162 (if (string= nnimap-current-server nnimap-current-move-server)
1163 ;; moving article within same server, speed it up...
1164 (and (nnimap-possibly-change-group
1165 nnimap-current-move-group)
1166 (imap-message-copy (number-to-string
1167 nnimap-current-move-article)
1168 group 'dontcreate nil
1169 nnimap-server-buffer))
1170 (with-current-buffer (current-buffer)
1171 (goto-char (point-min))
1172 ;; remove any 'From blabla' lines, some IMAP servers
1173 ;; reject the entire message otherwise.
1174 (when (looking-at "^From[^:]")
1175 (kill-region (point) (progn (forward-line) (point))))
1176 ;; turn into rfc822 format (\r\n eol's)
1177 (while (search-forward "\n" nil t)
1178 (replace-match "\r\n")))
1179 ;; this 'or' is for Cyrus server bug
1180 (or (null (imap-current-mailbox nnimap-server-buffer))
1181 (imap-mailbox-unselect nnimap-server-buffer))
1182 (imap-message-append group (current-buffer) nil nil
1183 nnimap-server-buffer)))
1184 (cons group (nth 1 uid))
1185 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1186
1187 (deffoo nnimap-request-delete-group (group force &optional server)
1188 (when (nnimap-possibly-change-server server)
1189 (with-current-buffer nnimap-server-buffer
1190 (if force
1191 (or (null (imap-mailbox-status group 'uidvalidity))
1192 (imap-mailbox-delete group))
1193 ;; UNSUBSCRIBE?
1194 t))))
1195
1196 (deffoo nnimap-request-rename-group (group new-name &optional server)
1197 (when (nnimap-possibly-change-server server)
1198 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1199
1200 (defun nnimap-expunge (mailbox server)
1201 (when (nnimap-possibly-change-group mailbox server)
1202 (imap-mailbox-expunge nnimap-server-buffer)))
1203
1204 (defun nnimap-acl-get (mailbox server)
1205 (when (nnimap-possibly-change-server server)
1206 (and (imap-capability 'ACL nnimap-server-buffer)
1207 (imap-mailbox-acl-get mailbox nnimap-server-buffer))))
1208
1209 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1210 (when (nnimap-possibly-change-server (cadr method))
1211 (unless (imap-capability 'ACL nnimap-server-buffer)
1212 (error "Your server does not support ACL editing"))
1213 (with-current-buffer nnimap-server-buffer
1214 ;; delete all removed identifiers
1215 (mapcar (lambda (old-acl)
1216 (unless (assoc (car old-acl) new-acls)
1217 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1218 (error "Can't delete ACL for %s" (car old-acl)))))
1219 old-acls)
1220 ;; set all changed acl's
1221 (mapcar (lambda (new-acl)
1222 (let ((new-rights (cdr new-acl))
1223 (old-rights (cdr (assoc (car new-acl) old-acls))))
1224 (unless (and old-rights new-rights
1225 (string= old-rights new-rights))
1226 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1227 (error "Can't set ACL for %s to %s" (car new-acl)
1228 new-rights)))))
1229 new-acls)
1230 t)))
1231
1232 \f
1233 ;;; Internal functions
1234
1235 ;;
1236 ;; This is confusing.
1237 ;;
1238 ;; mark => read, tick, draft, reply etc
1239 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1240 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1241 ;;
1242 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1243 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1244 ;;
1245
1246 (defconst nnimap-mark-to-predicate-alist
1247 (mapcar
1248 (lambda (pair) ; cdr is the mark
1249 (or (assoc (cdr pair)
1250 '((read . "SEEN")
1251 (tick . "FLAGGED")
1252 (draft . "DRAFT")
1253 (reply . "ANSWERED")))
1254 (cons (cdr pair)
1255 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1256 (cons '(read . read) gnus-article-mark-lists)))
1257
1258 (defun nnimap-mark-to-predicate (pred)
1259 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1260 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1261 to be used within a IMAP SEARCH query."
1262 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1263
1264 (defconst nnimap-mark-to-flag-alist
1265 (mapcar
1266 (lambda (pair)
1267 (or (assoc (cdr pair)
1268 '((read . "\\Seen")
1269 (tick . "\\Flagged")
1270 (draft . "\\Draft")
1271 (reply . "\\Answered")))
1272 (cons (cdr pair)
1273 (format "gnus-%s" (symbol-name (cdr pair))))))
1274 (cons '(read . read) gnus-article-mark-lists)))
1275
1276 (defun nnimap-mark-to-flag-1 (preds)
1277 (if (and (not (null preds)) (listp preds))
1278 (cons (nnimap-mark-to-flag (car preds))
1279 (nnimap-mark-to-flag (cdr preds)))
1280 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1281
1282 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1283 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1284 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1285 be used in a STORE FLAGS command."
1286 (let ((result (nnimap-mark-to-flag-1 preds)))
1287 (setq result (if (and (or make-string always-list)
1288 (not (listp result)))
1289 (list result)
1290 result))
1291 (if make-string
1292 (mapconcat (lambda (flag)
1293 (if (listp flag)
1294 (mapconcat 'identity flag " ")
1295 flag))
1296 result " ")
1297 result)))
1298
1299 (defun nnimap-mark-permanent-p (mark &optional group)
1300 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1301 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1302
1303 (defun nnimap-remassoc (key alist)
1304 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1305 The modified LIST is returned. If the first member
1306 of LIST has a car that is `equal' to KEY, there is no way to remove it
1307 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1308 sure of changing the value of `foo'."
1309 (when alist
1310 (if (equal key (caar alist))
1311 (cdr alist)
1312 (setcdr alist (nnimap-remassoc key (cdr alist)))
1313 alist)))
1314
1315 (defun nnimap-update-alist-soft (key value alist)
1316 (if value
1317 (cons (cons key value) (nnimap-remassoc key alist))
1318 (nnimap-remassoc key alist)))
1319
1320 (when nnimap-debug
1321 (require 'trace)
1322 (buffer-disable-undo (get-buffer-create nnimap-debug))
1323 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1324 '(
1325 nnimap-possibly-change-server
1326 nnimap-verify-uidvalidity
1327 nnimap-find-minmax-uid
1328 nnimap-before-find-minmax-bugworkaround
1329 nnimap-possibly-change-group
1330 ;;nnimap-replace-whitespace
1331 nnimap-retrieve-headers-progress
1332 nnimap-retrieve-which-headers
1333 nnimap-group-overview-filename
1334 nnimap-retrieve-headers-from-file
1335 nnimap-retrieve-headers-from-server
1336 nnimap-retrieve-headers
1337 nnimap-open-connection
1338 nnimap-open-server
1339 nnimap-server-opened
1340 nnimap-close-server
1341 nnimap-request-close
1342 nnimap-status-message
1343 ;;nnimap-demule
1344 nnimap-request-article-part
1345 nnimap-request-article
1346 nnimap-request-head
1347 nnimap-request-body
1348 nnimap-request-group
1349 nnimap-close-group
1350 nnimap-pattern-to-list-arguments
1351 nnimap-request-list
1352 nnimap-request-post
1353 nnimap-retrieve-groups
1354 nnimap-request-update-info-internal
1355 nnimap-request-type
1356 nnimap-request-set-mark
1357 nnimap-split-to-groups
1358 nnimap-split-find-rule
1359 nnimap-split-find-inbox
1360 nnimap-split-articles
1361 nnimap-request-scan
1362 nnimap-request-newgroups
1363 nnimap-request-create-group
1364 nnimap-time-substract
1365 nnimap-date-days-ago
1366 nnimap-request-expire-articles-progress
1367 nnimap-request-expire-articles
1368 nnimap-request-move-article
1369 nnimap-request-accept-article
1370 nnimap-request-delete-group
1371 nnimap-request-rename-group
1372 gnus-group-nnimap-expunge
1373 gnus-group-nnimap-edit-acl
1374 gnus-group-nnimap-edit-acl-done
1375 nnimap-group-mode-hook
1376 nnimap-mark-to-predicate
1377 nnimap-mark-to-flag-1
1378 nnimap-mark-to-flag
1379 nnimap-mark-permanent-p
1380 nnimap-remassoc
1381 nnimap-update-alist-soft
1382 )))
1383
1384 (provide 'nnimap)
1385
1386 ;;; nnimap.el ends here