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