Comment fix.
[bpt/emacs.git] / lisp / gnus / nnimap.el
CommitLineData
c113de23
GM
1;;; nnimap.el --- imap backend for Gnus
2;; Copyright (C) 1998, 1999, 2000 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?) (unnecessery?)
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
c9171278 62 (require 'cl)
c113de23
GM
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.
83If 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.
89If nil, the first match found will be used.")
90
91(defvar nnimap-split-inbox nil
92 "*Name of mailbox to split mail from.
93
94Mail is read from this mailbox and split according to rules in
95`nnimap-split-rules'.
96
97This can be a string or a list of strings.")
98
99(defvar nnimap-split-rule nil
100 "*Mail will be split according to theese rules.
101
102Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
103
104If you'd like, for instance, one mail group for mail from the
105\"gnus-imap\" mailing list, one group for junk mail and leave
106everything else in the incoming mailbox, you could do something like
107this:
108
109(setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
110 (\"INBOX.junk\" \"Subject:.*buy\")))
111
112As you can see, `nnimap-split-rule' is a list of lists, where the first
113element in each \"rule\" is the name of the IMAP mailbox, and the
114second is a regexp that nnimap will try to match on the header to find
115a fit.
116
117The second element can also be a function. In that case, it will be
118called narrowed to the headers with the first element of the rule as
119the argument. It should return a non-nil value if it thinks that the
120mail belongs in that group.
121
122This variable can also have a function as its value, the function will
123be called with the headers narrowed and should return a group where it
124thinks the article should be splitted to. See `nnimap-split-fancy'.
125
126To allow for different split rules on different virtual servers, and
127even different split rules in different inboxes on the same server,
128the 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
137The virtual server name is in fact a regexp, so that the same rules
138may apply to several servers. In the example, the servers
139\"my3server\" and \"my4server\" both use the same rules. Similarly,
140the inbox string is also a regexp. The actual splitting rules are as
141before, either a function, or a list with group/regexp or
142group/function elements.")
143
144(defvar nnimap-split-predicate "UNSEEN UNDELETED"
145 "The predicate used to find articles to split.
146If you use another IMAP client to peek on articles but always would
147like nnimap to split them once it's started, you could change this to
148\"UNDELETED\". Other available predicates are available in
149RFC2060 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
162The default, nil, will try to use the \"best\" method the server can
163handle.
164
165Change this if
166
1671) you want to connect with SSL. The SSL integration with IMAP is
168 brain-dead so you'll have to tell it specifically.
169
1702) 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
174Possible choices: kerberos4, ssl, network")
175
176(defvoo nnimap-authenticator nil
177 "How nnimap authenticate itself to the server.
178
179The default, nil, will try to use the \"best\" method the server can
180handle.
181
182There is only one reason for fiddling with this variable, and that is
183if your server is more capable than your environment -- i.e. you
184connect to a server that accept Kerberos login's but you haven't
185installed the `imtest' program or your machine isn't configured for
186Kerberos.
187
188Possible 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.
192See also `nnimap-nov-file-name'.")
193
194(defvoo nnimap-nov-file-name "nnimap."
195 "NOV cache base filename.
196The group name and `nnimap-nov-file-name-suffix' will be appended. A
197typical 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.
207Using nov databases will speed up header fetching considerably.
208Unlike other backends, you do not need to take special care if you
209flip this variable.")
210
211(defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
212 "Whether to expunge a group when it is closed.
213When a IMAP group with articles marked for deletion is closed, this
214variable determine if nnimap should actually remove the articles or
215not.
216
217If always, nnimap always perform a expunge when closing the group.
218If never, nnimap never expunges articles marked for deletion.
219If ask, nnimap will ask you if you wish to expunge marked articles.
220
221When setting this variable to `never', you can only expunge articles
222by 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.
226See below for available wildcards.
227
228The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
229REFERENCE will be passed as the first parameter to LIST/LSUB. The
230semantics of this are server specific, on the University of Washington
231server you can specify a directory.
232
233Example:
234 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
235
236There are two wildcards * and %. * matches everything, % matches
237everything 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
242This variable should contain a regexp matching groups where you wish
243replies to be stored to the mailbox directly.
244
245Example:
246 '(\"^[^I][^N][^B][^O][^X].*$\")
247
248This will match all groups not beginning with \"INBOX\".
249
250Note that there is nothing technically different between mail-like and
251news-like mailboxes. If you wish to have a group with todo items or
252similar which you wouldn't want to set up a mailing list for, you can
253use 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.
281If this is 'imap-mailbox-lsub, then use a server-side subscription list to
282restrict visible folders.")
283
284;; Internal variables:
285
286(defvar nnimap-debug nil
287 "Name of buffer to record debugging info.
288For 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.
316If 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))
a2617484
DL
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))))
c113de23
GM
342 (if old-uidvalidity
343 (if (not (equal old-uidvalidity new-uidvalidity))
a2617484
DL
344 ;; uidvalidity clash
345 (gnus-delete-file file)
c113de23
GM
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 nummber in GROUP.
361If 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)
715a2ca2 388 (error "nnimap: Group %s is not uid-valid" group))
c113de23
GM
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 pathname for GROUP on SERVER."
a2617484
DL
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))
c113de23
GM
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 1 (point-max) (nnimap-group-overview-filename group server)
585 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.
637If SERVER is the current virtual server, and the connection to the
638physical server is alive, this function return a non-nil value. If
639SERVER 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.
647Return 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.
661All buffers that have been created by that
662backend should be killed. (Not the nntp-server-buffer, though.) This
663function 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 ((date (format-time-string "%d-%b-%Y"
1092 (nnimap-time-substract
1093 (current-time)
1094 (days-to-time daysago)))))
1095 (if (eq ?0 (string-to-char date))
1096 (substring date 1)
1097 date)))
1098
1099(defun nnimap-request-expire-articles-progress ()
1100 (gnus-message 5 "nnimap: Marking article %d for deletion..."
1101 imap-current-message))
1102
1103;; Notice that we don't actually delete anything, we just mark them deleted.
1104(deffoo nnimap-request-expire-articles (articles group &optional server force)
1105 (let ((artseq (gnus-compress-sequence articles)))
1106 (when (and artseq (nnimap-possibly-change-group group server))
1107 (with-current-buffer nnimap-server-buffer
1108 (if force
1109 (and (imap-message-flags-add
1110 (imap-range-to-message-set artseq) "\\Deleted")
1111 (setq articles nil))
1112 (let ((days (or (and nnmail-expiry-wait-function
1113 (funcall nnmail-expiry-wait-function group))
1114 nnmail-expiry-wait)))
1115 (cond ((eq days 'immediate)
1116 (and (imap-message-flags-add
1117 (imap-range-to-message-set artseq) "\\Deleted")
1118 (setq articles nil)))
1119 ((numberp days)
1120 (let ((oldarts (imap-search
1121 (format "UID %s NOT SINCE %s"
1122 (imap-range-to-message-set artseq)
1123 (nnimap-date-days-ago days))))
1124 (imap-fetch-data-hook
1125 '(nnimap-request-expire-articles-progress)))
1126 (and oldarts
1127 (imap-message-flags-add
1128 (imap-range-to-message-set
1129 (gnus-compress-sequence oldarts))
1130 "\\Deleted")
1131 (setq articles (gnus-set-difference
1132 articles oldarts)))))))))))
1133 ;; return articles not deleted
1134 articles)
1135
1136(deffoo nnimap-request-move-article (article group server
1137 accept-form &optional last)
1138 (when (nnimap-possibly-change-server server)
1139 (save-excursion
1140 (let ((buf (get-buffer-create " *nnimap move*"))
1141 (nnimap-current-move-article article)
1142 (nnimap-current-move-group group)
1143 (nnimap-current-move-server nnimap-current-server)
1144 result)
1145 (and (nnimap-request-article article group server)
1146 (save-excursion
1147 (set-buffer buf)
1148 (buffer-disable-undo (current-buffer))
1149 (insert-buffer-substring nntp-server-buffer)
1150 (setq result (eval accept-form))
1151 (kill-buffer buf)
1152 result)
1153 (nnimap-request-expire-articles (list article) group server t))
1154 result))))
1155
1156(deffoo nnimap-request-accept-article (group &optional server last)
1157 (when (nnimap-possibly-change-server server)
1158 (let (uid)
1159 (if (setq uid
1160 (if (string= nnimap-current-server nnimap-current-move-server)
1161 ;; moving article within same server, speed it up...
1162 (and (nnimap-possibly-change-group
1163 nnimap-current-move-group)
1164 (imap-message-copy (number-to-string
1165 nnimap-current-move-article)
1166 group 'dontcreate nil
1167 nnimap-server-buffer))
c113de23
GM
1168 (with-current-buffer (current-buffer)
1169 (goto-char (point-min))
a2617484
DL
1170 ;; remove any 'From blabla' lines, some IMAP servers
1171 ;; reject the entire message otherwise.
1172 (when (looking-at "^From[^:]")
1173 (kill-region (point) (progn (forward-line) (point))))
1174 ;; turn into rfc822 format (\r\n eol's)
c113de23
GM
1175 (while (search-forward "\n" nil t)
1176 (replace-match "\r\n")))
1177 ;; this 'or' is for Cyrus server bug
1178 (or (null (imap-current-mailbox nnimap-server-buffer))
1179 (imap-mailbox-unselect nnimap-server-buffer))
1180 (imap-message-append group (current-buffer) nil nil
1181 nnimap-server-buffer)))
1182 (cons group (nth 1 uid))
1183 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1184
1185(deffoo nnimap-request-delete-group (group force &optional server)
1186 (when (nnimap-possibly-change-server server)
1187 (with-current-buffer nnimap-server-buffer
1188 (if force
1189 (or (null (imap-mailbox-status group 'uidvalidity))
1190 (imap-mailbox-delete group))
1191 ;; UNSUBSCRIBE?
1192 t))))
1193
1194(deffoo nnimap-request-rename-group (group new-name &optional server)
1195 (when (nnimap-possibly-change-server server)
1196 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1197
1198(defun nnimap-expunge (mailbox server)
1199 (when (nnimap-possibly-change-group mailbox server)
1200 (imap-mailbox-expunge nnimap-server-buffer)))
1201
1202(defun nnimap-acl-get (mailbox server)
1203 (when (nnimap-possibly-change-server server)
a2617484
DL
1204 (and (imap-capability 'ACL nnimap-server-buffer)
1205 (imap-mailbox-acl-get mailbox nnimap-server-buffer))))
c113de23
GM
1206
1207(defun nnimap-acl-edit (mailbox method old-acls new-acls)
1208 (when (nnimap-possibly-change-server (cadr method))
1209 (unless (imap-capability 'ACL nnimap-server-buffer)
1210 (error "Your server does not support ACL editing"))
1211 (with-current-buffer nnimap-server-buffer
1212 ;; delete all removed identifiers
1213 (mapcar (lambda (old-acl)
1214 (unless (assoc (car old-acl) new-acls)
1215 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1216 (error "Can't delete ACL for %s" (car old-acl)))))
1217 old-acls)
1218 ;; set all changed acl's
1219 (mapcar (lambda (new-acl)
1220 (let ((new-rights (cdr new-acl))
1221 (old-rights (cdr (assoc (car new-acl) old-acls))))
1222 (unless (and old-rights new-rights
1223 (string= old-rights new-rights))
1224 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1225 (error "Can't set ACL for %s to %s" (car new-acl)
1226 new-rights)))))
1227 new-acls)
1228 t)))
1229
1230\f
1231;;; Internal functions
1232
1233;;
1234;; This is confusing.
1235;;
1236;; mark => read, tick, draft, reply etc
1237;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1238;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1239;;
1240;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1241;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1242;;
1243
1244(defconst nnimap-mark-to-predicate-alist
1245 (mapcar
1246 (lambda (pair) ; cdr is the mark
1247 (or (assoc (cdr pair)
1248 '((read . "SEEN")
1249 (tick . "FLAGGED")
1250 (draft . "DRAFT")
1251 (reply . "ANSWERED")))
1252 (cons (cdr pair)
1253 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1254 (cons '(read . read) gnus-article-mark-lists)))
1255
1256(defun nnimap-mark-to-predicate (pred)
1257 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1258This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1259to be used within a IMAP SEARCH query."
1260 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1261
1262(defconst nnimap-mark-to-flag-alist
1263 (mapcar
1264 (lambda (pair)
1265 (or (assoc (cdr pair)
1266 '((read . "\\Seen")
1267 (tick . "\\Flagged")
1268 (draft . "\\Draft")
1269 (reply . "\\Answered")))
1270 (cons (cdr pair)
1271 (format "gnus-%s" (symbol-name (cdr pair))))))
1272 (cons '(read . read) gnus-article-mark-lists)))
1273
1274(defun nnimap-mark-to-flag-1 (preds)
1275 (if (and (not (null preds)) (listp preds))
1276 (cons (nnimap-mark-to-flag (car preds))
1277 (nnimap-mark-to-flag (cdr preds)))
1278 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1279
1280(defun nnimap-mark-to-flag (preds &optional always-list make-string)
1281 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1282This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1283be used in a STORE FLAGS command."
1284 (let ((result (nnimap-mark-to-flag-1 preds)))
1285 (setq result (if (and (or make-string always-list)
1286 (not (listp result)))
1287 (list result)
1288 result))
1289 (if make-string
1290 (mapconcat (lambda (flag)
1291 (if (listp flag)
1292 (mapconcat 'identity flag " ")
1293 flag))
1294 result " ")
1295 result)))
1296
1297(defun nnimap-mark-permanent-p (mark &optional group)
1298 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1299 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1300
1301(defun nnimap-remassoc (key alist)
1302 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1303The modified LIST is returned. If the first member
1304of LIST has a car that is `equal' to KEY, there is no way to remove it
1305by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1306sure of changing the value of `foo'."
1307 (when alist
1308 (if (equal key (caar alist))
1309 (cdr alist)
1310 (setcdr alist (nnimap-remassoc key (cdr alist)))
1311 alist)))
1312
1313(defun nnimap-update-alist-soft (key value alist)
1314 (if value
1315 (cons (cons key value) (nnimap-remassoc key alist))
1316 (nnimap-remassoc key alist)))
1317
1318(when nnimap-debug
1319 (require 'trace)
1320 (buffer-disable-undo (get-buffer-create nnimap-debug))
1321 (mapcar (lambda (f) (trace-function-background f nnimap-debug))
1322 '(
1323 nnimap-possibly-change-server
1324 nnimap-verify-uidvalidity
1325 nnimap-find-minmax-uid
1326 nnimap-before-find-minmax-bugworkaround
1327 nnimap-possibly-change-group
1328 ;;nnimap-replace-whitespace
1329 nnimap-retrieve-headers-progress
1330 nnimap-retrieve-which-headers
1331 nnimap-group-overview-filename
1332 nnimap-retrieve-headers-from-file
1333 nnimap-retrieve-headers-from-server
1334 nnimap-retrieve-headers
1335 nnimap-open-connection
1336 nnimap-open-server
1337 nnimap-server-opened
1338 nnimap-close-server
1339 nnimap-request-close
1340 nnimap-status-message
1341 ;;nnimap-demule
1342 nnimap-request-article-part
1343 nnimap-request-article
1344 nnimap-request-head
1345 nnimap-request-body
1346 nnimap-request-group
1347 nnimap-close-group
1348 nnimap-pattern-to-list-arguments
1349 nnimap-request-list
1350 nnimap-request-post
1351 nnimap-retrieve-groups
1352 nnimap-request-update-info-internal
1353 nnimap-request-type
1354 nnimap-request-set-mark
1355 nnimap-split-to-groups
1356 nnimap-split-find-rule
1357 nnimap-split-find-inbox
1358 nnimap-split-articles
1359 nnimap-request-scan
1360 nnimap-request-newgroups
1361 nnimap-request-create-group
1362 nnimap-time-substract
1363 nnimap-date-days-ago
1364 nnimap-request-expire-articles-progress
1365 nnimap-request-expire-articles
1366 nnimap-request-move-article
1367 nnimap-request-accept-article
1368 nnimap-request-delete-group
1369 nnimap-request-rename-group
1370 gnus-group-nnimap-expunge
1371 gnus-group-nnimap-edit-acl
1372 gnus-group-nnimap-edit-acl-done
1373 nnimap-group-mode-hook
1374 nnimap-mark-to-predicate
1375 nnimap-mark-to-flag-1
1376 nnimap-mark-to-flag
1377 nnimap-mark-permanent-p
1378 nnimap-remassoc
1379 nnimap-update-alist-soft
1380 )))
1381
1382(provide 'nnimap)
1383
1384;;; nnimap.el ends here