1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
5 ;; Jim Radford <radford@robby.caltech.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Todo, major things:
29 ;; o Fix Gnus to view correct number of unread/total articles in group buffer
30 ;; o Fix Gnus to handle leading '.' in group names (fixed?)
31 ;; o Finish disconnected mode (moving articles between mailboxes unplugged)
33 ;; o MIME (partial article fetches)
34 ;; o Split to other backends, different split rules for different
37 ;; Todo, minor things:
39 ;; o Don't require half of Gnus -- backends should be standalone
40 ;; o Verify that we don't use IMAP4rev1 specific things (RFC2060 App B)
41 ;; o Dont uid fetch 1,* in nnimap-retrive-groups (slow)
42 ;; o Split up big fetches (1,* header especially) in smaller chunks
43 ;; o What do I do with gnus-newsgroup-*?
44 ;; o Tell Gnus about new groups (how can we tell?)
45 ;; o Respooling (fix Gnus?) (unnecessary?)
46 ;; o Add support for the following: (if applicable)
47 ;; request-list-newsgroups, request-regenerate
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
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 ;; o Rewrite UID SEARCH UID X as UID FETCH X (UID) for those with slow servers
74 (defconst nnimap-version "nnimap 1.0")
77 "Reading IMAP mail with Gnus."
80 (defvoo nnimap-address nil
81 "Address of physical IMAP server. If nil, use the virtual server's name.")
83 (defvoo nnimap-server-port nil
84 "Port number on physical IMAP server.
85 If nil, defaults to 993 for SSL connections and 143 otherwise.")
87 ;; Splitting variables
89 (defcustom nnimap-split-crosspost t
90 "If non-nil, do crossposting if several split methods match the mail.
91 If nil, the first match found will be used."
95 (defcustom nnimap-split-inbox nil
96 "Name of mailbox to split mail from.
98 Mail is read from this mailbox and split according to rules in
101 This can be a string or a list of strings."
103 :type '(choice (string)
106 (define-widget 'nnimap-strict-function 'function
107 "This widget only matches values that are functionp.
109 Warning: This means that a value that is the symbol of a not yet
110 loaded function will not match. Use with care."
111 :match 'nnimap-strict-function-match)
113 (defun nnimap-strict-function-match (widget value)
114 "Ignoring WIDGET, match if VALUE is a function."
117 (defcustom nnimap-split-rule nil
118 "Mail will be split according to these rules.
120 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
122 If you'd like, for instance, one mail group for mail from the
123 \"gnus-imap\" mailing list, one group for junk mail and leave
124 everything else in the incoming mailbox, you could do something like
127 \(setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
128 (\"INBOX.junk\" \"Subject:.*buy\")))
130 As you can see, `nnimap-split-rule' is a list of lists, where the
131 first element in each \"rule\" is the name of the IMAP mailbox (or the
132 symbol `junk' if you want to remove the mail), and the second is a
133 regexp that nnimap will try to match on the header to find a fit.
135 The second element can also be a function. In that case, it will be
136 called narrowed to the headers with the first element of the rule as
137 the argument. It should return a non-nil value if it thinks that the
138 mail belongs in that group.
140 This variable can also have a function as its value, the function will
141 be called with the headers narrowed and should return a group where it
142 thinks the article should be splitted to. See `nnimap-split-fancy'.
144 To allow for different split rules on different virtual servers, and
145 even different split rules in different inboxes on the same server,
146 the syntax of this variable have been extended along the lines of:
148 \(setq nnimap-split-rule
149 '((\"my1server\" (\".*\" ((\"ding\" \"ding@gnus.org\")
150 (\"junk\" \"From:.*Simon\")))
151 (\"my2server\" (\"INBOX\" nnimap-split-fancy))
152 (\"my[34]server\" (\".*\" ((\"private\" \"To:.*Simon\")
153 (\"junk\" my-junk-func)))))
155 The virtual server name is in fact a regexp, so that the same rules
156 may apply to several servers. In the example, the servers
157 \"my3server\" and \"my4server\" both use the same rules. Similarly,
158 the inbox string is also a regexp. The actual splitting rules are as
159 before, either a function, or a list with group/regexp or
160 group/function elements."
162 :type '(choice :tag "Rule type"
163 (repeat :menu-tag "Single-server"
164 :tag "Single-server list"
165 (list (string :tag "Mailbox")
166 (choice :tag "Predicate"
167 (regexp :tag "A regexp")
168 (nnimap-strict-function :tag "A function"))))
169 (choice :menu-tag "A function"
171 (function-item nnimap-split-fancy)
172 (function-item nnmail-split-fancy)
173 (nnimap-strict-function :tag "User-defined function"))
174 (repeat :menu-tag "Multi-server (extended)"
175 :tag "Multi-server list"
176 (list (regexp :tag "Server regexp")
177 (list (regexp :tag "Incoming Mailbox regexp")
178 (repeat :tag "Rules for matching server(s) and mailbox(es)"
179 (list (string :tag "Destination mailbox")
180 (choice :tag "Predicate"
181 (regexp :tag "A Regexp")
182 (nnimap-strict-function :tag "A Function")))))))))
184 (defcustom nnimap-split-predicate "UNSEEN UNDELETED"
185 "The predicate used to find articles to split.
186 If you use another IMAP client to peek on articles but always would
187 like nnimap to split them once it's started, you could change this to
188 \"UNDELETED\". Other available predicates are available in
189 RFC2060 section 6.4.4."
193 (defcustom nnimap-split-fancy nil
194 "Like the variable `nnmail-split-fancy', which see."
198 ;; Performance / bug workaround variables
200 (defcustom nnimap-close-asynchronous t
201 "Close mailboxes asynchronously in `nnimap-close-group'.
202 This means that errors cought by nnimap when closing the mailbox will
203 not prevent Gnus from updating the group status, which may be harmful.
204 However, it increases speed."
208 (defcustom nnimap-dont-close t
209 "Never close mailboxes.
210 This increases the speed of closing mailboxes (quiting group) but may
211 decrease the speed of selecting another mailbox later. Re-selecting
212 the same mailbox will be faster though."
216 (defcustom nnimap-retrieve-groups-asynchronous t
217 "Send asynchronous STATUS commands for each mailbox before checking mail.
218 If you have mailboxes that rarely receives mail, this speeds up new
219 mail checking. It works by first sending STATUS commands for each
220 mailbox, and then only checking groups which has a modified UIDNEXT
221 more carefully for new mail.
223 In summary, the default is O((1-p)*k+p*n) and changing it to nil makes
224 it O(n). If p is small, then the default is probably faster."
228 (defvoo nnimap-need-unselect-to-notice-new-mail nil
229 "Unselect mailboxes before looking for new mail in them.
230 Some servers seem to need this under some circumstances.")
232 ;; Authorization / Privacy variables
234 (defvoo nnimap-auth-method nil
237 (defvoo nnimap-stream nil
238 "How nnimap will connect to the server.
240 The default, nil, will try to use the \"best\" method the server can
245 1) you want to connect with SSL. The SSL integration with IMAP is
246 brain-dead so you'll have to tell it specifically.
248 2) your server is more capable than your environment -- i.e. your
249 server accept Kerberos login's but you haven't installed the
250 `imtest' program or your machine isn't configured for Kerberos.
252 Possible choices: kerberos4, ssl, network")
254 (defvoo nnimap-authenticator nil
255 "How nnimap authenticate itself to the server.
257 The default, nil, will try to use the \"best\" method the server can
260 There is only one reason for fiddling with this variable, and that is
261 if your server is more capable than your environment -- i.e. you
262 connect to a server that accept Kerberos login's but you haven't
263 installed the `imtest' program or your machine isn't configured for
266 Possible choices: kerberos4, cram-md5, login, anonymous.")
268 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
269 "Directory to keep NOV cache files for nnimap groups.
270 See also `nnimap-nov-file-name'.")
272 (defvoo nnimap-nov-file-name "nnimap."
273 "NOV cache base filename.
274 The group name and `nnimap-nov-file-name-suffix' will be appended. A
275 typical complete file name would be
276 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
277 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
278 `nnmail-use-long-file-names' is nil")
280 (defvoo nnimap-nov-file-name-suffix ".novcache"
281 "Suffix for NOV cache base filename.")
283 (defvoo nnimap-nov-is-evil gnus-agent
284 "If non-nil, never generate or use a local nov database for this backend.
285 Using nov databases should speed up header fetching considerably.
286 However, it will invoke a UID SEARCH UID command on the server, and
287 some servers implement this command inefficiently by opening each and
288 every message in the group, thus making it quite slow.
289 Unlike other backends, you do not need to take special care if you
290 flip this variable.")
292 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
293 "Whether to expunge a group when it is closed.
294 When a IMAP group with articles marked for deletion is closed, this
295 variable determine if nnimap should actually remove the articles or
298 If always, nnimap always perform a expunge when closing the group.
299 If never, nnimap never expunges articles marked for deletion.
300 If ask, nnimap will ask you if you wish to expunge marked articles.
302 When setting this variable to `never', you can only expunge articles
303 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
305 (defvoo nnimap-list-pattern "*"
306 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
307 See below for available wildcards.
309 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
310 REFERENCE will be passed as the first parameter to LIST/LSUB. The
311 semantics of this are server specific, on the University of Washington
312 server you can specify a directory.
315 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
317 There are two wildcards * and %. * matches everything, % matches
318 everything in the current hierarchy.")
320 (defvoo nnimap-news-groups nil
321 "IMAP support a news-like mode, also known as bulletin board mode,
322 where replies is sent via IMAP instead of SMTP.
324 This variable should contain a regexp matching groups where you wish
325 replies to be stored to the mailbox directly.
328 '(\"^[^I][^N][^B][^O][^X].*$\")
330 This will match all groups not beginning with \"INBOX\".
332 Note that there is nothing technically different between mail-like and
333 news-like mailboxes. If you wish to have a group with todo items or
334 similar which you wouldn't want to set up a mailing list for, you can
335 use this to make replies go directly to the group.")
337 (defvoo nnimap-expunge-search-string "UID %s NOT SINCE %s"
338 "IMAP search command to use for articles that are to be expired.
339 The first %s is replaced by a UID set of articles to search on,
340 and the second %s is replaced by a date criterium.
342 One useful (and perhaps the only useful) value to change this to would
343 be `UID %s NOT SENTSINCE %s' to make nnimap use the Date: header
344 instead of the internal date of messages. See section 6.4.4 of RFC
345 2060 for more information on valid strings.")
347 (defvoo nnimap-importantize-dormant t
348 "If non-nil, mark \"dormant\" articles as \"ticked\" for other IMAP clients.
349 Note that within Gnus, dormant articles will still (only) be
350 marked as ticked. This is to make \"dormant\" articles stand out,
351 just like \"ticked\" articles, in other IMAP clients.")
353 (defvoo nnimap-server-address nil
354 "Obsolete. Use `nnimap-address'.")
356 (defcustom nnimap-authinfo-file "~/.authinfo"
357 "Authorization information for IMAP servers. In .netrc format."
360 (repeat :tag "Entries"
363 :value ("" ("login" . "") ("password" . ""))
367 (const :format "" "login")
368 (string :format "Login: %v"))
370 (const :format "" "password")
371 (string :format "Password: %v")))))))
373 (defcustom nnimap-prune-cache t
374 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
377 (defvar nnimap-request-list-method 'imap-mailbox-list
378 "Method to use to request a list of all folders from the server.
379 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
380 restrict visible folders.")
382 (defcustom nnimap-debug nil
383 "If non-nil, random debug spews are placed in *nnimap-debug* buffer."
387 ;; Internal variables:
389 (defvar nnimap-debug-buffer "*nnimap-debug*")
390 (defvar nnimap-mailbox-info (gnus-make-hashtable 997))
391 (defvar nnimap-current-move-server nil)
392 (defvar nnimap-current-move-group nil)
393 (defvar nnimap-current-move-article nil)
394 (defvar nnimap-length)
395 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
396 (defvar nnimap-progress-how-often 20)
397 (defvar nnimap-counter)
398 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
399 (defvar nnimap-current-server nil) ;; Current server
400 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
404 (nnoo-define-basics nnimap)
406 ;; Utility functions:
408 (defsubst nnimap-get-server-buffer (server)
409 "Return buffer for SERVER, if nil use current server."
410 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
412 (defun nnimap-possibly-change-server (server)
413 "Return buffer for SERVER, changing the current server as a side-effect.
414 If SERVER is nil, uses the current server."
415 (setq nnimap-current-server (or server nnimap-current-server)
416 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
418 (defun nnimap-verify-uidvalidity (group server)
419 "Verify stored uidvalidity match current one in GROUP on SERVER."
420 (let* ((gnusgroup (gnus-group-prefixed-name
421 group (gnus-server-to-method
422 (format "nnimap:%s" server))))
423 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
424 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity))
425 (dir (file-name-as-directory (expand-file-name nnimap-directory)))
426 (nameuid (nnheader-translate-file-chars
427 (concat nnimap-nov-file-name
428 (if (equal server "")
430 server) "." group "." old-uidvalidity
431 nnimap-nov-file-name-suffix) t))
432 (file (if (or nnmail-use-long-file-names
433 (file-exists-p (expand-file-name nameuid dir)))
434 (expand-file-name nameuid dir)
436 (mm-encode-coding-string
437 (nnheader-replace-chars-in-string nameuid ?. ?/)
438 nnmail-pathname-coding-system)
441 (if (not (equal old-uidvalidity new-uidvalidity))
443 (gnus-delete-file file)
444 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
446 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
449 (defun nnimap-before-find-minmax-bugworkaround ()
450 "Function called before iterating through mailboxes with
451 `nnimap-find-minmax-uid'."
452 (when nnimap-need-unselect-to-notice-new-mail
453 ;; XXX this is for UoW imapd problem, it doesn't notice new mail in
454 ;; currently selected mailbox without a re-select/examine.
455 (or (null (imap-current-mailbox nnimap-server-buffer))
456 (imap-mailbox-unselect nnimap-server-buffer))))
458 (defun nnimap-find-minmax-uid (group &optional examine)
459 "Find lowest and highest active article number in GROUP.
460 If EXAMINE is non-nil the group is selected read-only."
461 (with-current-buffer nnimap-server-buffer
462 (when (or (string= group (imap-current-mailbox))
463 (imap-mailbox-select group examine))
465 (when (> (imap-mailbox-get 'exists) 0)
466 (imap-fetch "1,*" "UID" nil 'nouidfetch)
467 (imap-message-map (lambda (uid Uid)
468 (setq minuid (if minuid (min minuid uid) uid)
469 maxuid (if maxuid (max maxuid uid) uid)))
471 (list (imap-mailbox-get 'exists) minuid maxuid)))))
473 (defun nnimap-possibly-change-group (group &optional server)
474 "Make GROUP the current group, and SERVER the current server."
475 (when (nnimap-possibly-change-server server)
476 (with-current-buffer nnimap-server-buffer
477 (if (or (null group) (imap-current-mailbox-p group))
479 (if (imap-mailbox-select group)
480 (if (or (nnimap-verify-uidvalidity
481 group (or server nnimap-current-server))
482 (zerop (imap-mailbox-get 'exists group))
483 t ;; for OGnus to see if ignoring uidvalidity
484 ;; changes has any bad effects.
487 "nnimap: Group %s is not uidvalid. Continue? " group)))
489 (imap-mailbox-unselect)
490 (error "nnimap: Group %s is not uid-valid" group))
491 (nnheader-report 'nnimap (imap-error-text)))))))
493 (defun nnimap-replace-whitespace (string)
494 "Return STRING with all whitespace replaced with space."
496 (while (string-match "[\r\n\t]+" string)
497 (setq string (replace-match " " t t string)))
500 ;; Required backend functions
502 (defun nnimap-retrieve-headers-progress ()
503 "Hook to insert NOV line for current article into `nntp-server-buffer'."
504 (and (numberp nnmail-large-newsgroup)
505 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
506 (> nnimap-length nnmail-large-newsgroup)
507 (nnheader-message 6 "nnimap: Retrieving headers... %c"
508 (nth (/ (% nnimap-counter
509 (* (length nnimap-progress-chars)
510 nnimap-progress-how-often))
511 nnimap-progress-how-often)
512 nnimap-progress-chars)))
513 (with-current-buffer nntp-server-buffer
514 (let (headers lines chars uid mbx)
515 (with-current-buffer nnimap-server-buffer
516 (setq uid imap-current-message
517 mbx imap-current-mailbox
518 headers (nnimap-demule
519 (if (imap-capability 'IMAP4rev1)
520 ;; xxx don't just use car? alist doesn't contain
521 ;; anything else now, but it might...
522 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
523 (imap-message-get uid 'RFC822.HEADER)))
524 lines (imap-body-lines (imap-message-body imap-current-message))
525 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
528 (buffer-disable-undo)
530 (let ((head (nnheader-parse-naked-head)))
531 (mail-header-set-number head uid)
532 (mail-header-set-chars head chars)
533 (mail-header-set-lines head lines)
534 (mail-header-set-xref
535 head (format "%s %s:%d" (system-name) mbx uid))
538 (defun nnimap-retrieve-which-headers (articles fetch-old)
539 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
540 (with-current-buffer nnimap-server-buffer
541 (if (numberp (car-safe articles))
544 (imap-range-to-message-set
545 (gnus-compress-sequence
546 (append (gnus-uncompress-sequence
548 (cons (if (numberp fetch-old)
549 (max 1 (- (car articles) fetch-old))
551 (1- (car articles)))))
553 (mapcar (lambda (msgid)
555 (format "HEADER Message-Id \"%s\"" msgid)))
558 (defun nnimap-group-overview-filename (group server)
559 "Make pathname for GROUP on SERVER."
560 (let* ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
561 (uidvalidity (gnus-group-get-parameter
562 (gnus-group-prefixed-name
563 group (gnus-server-to-method
564 (format "nnimap:%s" server)))
566 (name (nnheader-translate-file-chars
567 (concat nnimap-nov-file-name
568 (if (equal server "")
570 server) "." group nnimap-nov-file-name-suffix) t))
571 (nameuid (nnheader-translate-file-chars
572 (concat nnimap-nov-file-name
573 (if (equal server "")
575 server) "." group "." uidvalidity
576 nnimap-nov-file-name-suffix) t))
577 (oldfile (if (or nnmail-use-long-file-names
578 (file-exists-p (expand-file-name name dir)))
579 (expand-file-name name dir)
581 (mm-encode-coding-string
582 (nnheader-replace-chars-in-string name ?. ?/)
583 nnmail-pathname-coding-system)
585 (newfile (if (or nnmail-use-long-file-names
586 (file-exists-p (expand-file-name nameuid dir)))
587 (expand-file-name nameuid dir)
589 (mm-encode-coding-string
590 (nnheader-replace-chars-in-string nameuid ?. ?/)
591 nnmail-pathname-coding-system)
593 (when (and (file-exists-p oldfile) (not (file-exists-p newfile)))
594 (message "nnimap: Upgrading novcache filename...")
596 (gnus-make-directory (file-name-directory newfile))
597 (unless (ignore-errors (rename-file oldfile newfile) t)
598 (if (ignore-errors (copy-file oldfile newfile) t)
599 (delete-file oldfile)
600 (error "Can't rename `%s' to `%s'" oldfile newfile))))
603 (defun nnimap-retrieve-headers-from-file (group server)
604 (with-current-buffer nntp-server-buffer
605 (let ((nov (nnimap-group-overview-filename group server)))
606 (when (file-exists-p nov)
607 (mm-insert-file-contents nov)
608 (set-buffer-modified-p nil)
609 (let ((min (ignore-errors (goto-char (point-min))
610 (read (current-buffer))))
611 (max (ignore-errors (goto-char (point-max))
613 (read (current-buffer)))))
614 (if (and (numberp min) (numberp max))
616 ;; junk, remove it, it's saved later
620 (defun nnimap-retrieve-headers-from-server (articles group server)
621 (with-current-buffer nnimap-server-buffer
622 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
623 (nnimap-length (gnus-range-length articles))
625 (imap-fetch (imap-range-to-message-set articles)
626 (concat "(UID RFC822.SIZE BODY "
628 (append '(Subject From Date Message-Id
629 References In-Reply-To Xref)
631 nnmail-extra-headers))))
632 (if (imap-capability 'IMAP4rev1)
633 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
634 (format "RFC822.HEADER.LINES %s)" headers)))))
635 (and (numberp nnmail-large-newsgroup)
636 (> nnimap-length nnmail-large-newsgroup)
637 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
639 (defun nnimap-dont-use-nov-p (group server)
640 (or gnus-nov-is-evil nnimap-nov-is-evil
641 (unless (and (gnus-make-directory
643 (nnimap-group-overview-filename group server)))
645 (nnimap-group-overview-filename group server)))
646 (message "nnimap: Nov cache not writable, %s"
647 (nnimap-group-overview-filename group server)))))
649 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
650 (when (nnimap-possibly-change-group group server)
651 (with-current-buffer nntp-server-buffer
653 (if (nnimap-dont-use-nov-p group server)
654 (nnimap-retrieve-headers-from-server
655 (gnus-compress-sequence articles) group server)
656 (let (uids cached low high)
657 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
659 high (car (last uids)))
660 (if (setq cached (nnimap-retrieve-headers-from-file group server))
662 ;; fetch articles with uids before cache block
663 (when (< low (car cached))
664 (goto-char (point-min))
665 (nnimap-retrieve-headers-from-server
666 (cons low (1- (car cached))) group server))
667 ;; fetch articles with uids after cache block
668 (when (> high (cdr cached))
669 (goto-char (point-max))
670 (nnimap-retrieve-headers-from-server
671 (cons (1+ (cdr cached)) high) group server))
672 (when nnimap-prune-cache
673 ;; remove nov's for articles which has expired on server
674 (goto-char (point-min))
675 (dolist (uid (gnus-set-difference articles uids))
676 (when (re-search-forward (format "^%d\t" uid) nil t)
677 (gnus-delete-line)))))
678 ;; nothing cached, fetch whole range from server
679 (nnimap-retrieve-headers-from-server
680 (cons low high) group server))
681 (when (buffer-modified-p)
683 (point-min) (point-max)
684 (nnimap-group-overview-filename group server) nil 'nomesg))
685 (nnheader-nov-delete-outside-range low high))))
688 (defun nnimap-open-connection (server)
689 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
690 nnimap-authenticator nnimap-server-buffer))
691 (nnheader-report 'nnimap "Can't open connection to server %s" server)
692 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
693 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
694 (imap-close nnimap-server-buffer)
695 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
696 (let* ((list (gnus-parse-netrc nnimap-authinfo-file))
697 (port (if nnimap-server-port
698 (int-to-string nnimap-server-port)
700 (alist (gnus-netrc-machine list (or nnimap-server-address
701 nnimap-address server)
703 (user (gnus-netrc-get alist "login"))
704 (passwd (gnus-netrc-get alist "password")))
705 (if (imap-authenticate user passwd nnimap-server-buffer)
707 (push (list server nnimap-server-buffer)
708 nnimap-server-buffer-alist)
709 (nnimap-possibly-change-server server))
710 (imap-close nnimap-server-buffer)
711 (kill-buffer nnimap-server-buffer)
712 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
714 (deffoo nnimap-open-server (server &optional defs)
715 (nnheader-init-server-buffer)
716 (if (nnimap-server-opened server)
718 (unless (assq 'nnimap-server-buffer defs)
719 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
720 ;; translate `nnimap-server-address' to `nnimap-address' in defs
721 ;; for people that configured nnimap with a very old version
722 (unless (assq 'nnimap-address defs)
723 (if (assq 'nnimap-server-address defs)
724 (push (list 'nnimap-address
725 (cadr (assq 'nnimap-server-address defs))) defs)
726 (push (list 'nnimap-address server) defs)))
727 (nnoo-change-server 'nnimap server defs)
728 (or nnimap-server-buffer
729 (setq nnimap-server-buffer (cadr (assq 'nnimap-server-buffer defs))))
730 (with-current-buffer (get-buffer-create nnimap-server-buffer)
731 (nnoo-change-server 'nnimap server defs))
732 (or (and nnimap-server-buffer
733 (imap-opened nnimap-server-buffer)
734 (if (with-current-buffer nnimap-server-buffer
735 (memq imap-state '(auth select examine)))
737 (imap-close nnimap-server-buffer)
738 (nnimap-open-connection server)))
739 (nnimap-open-connection server))))
741 (deffoo nnimap-server-opened (&optional server)
742 "Whether SERVER is opened.
743 If SERVER is the current virtual server, and the connection to the
744 physical server is alive, this function return a non-nil value. If
745 SERVER is nil, it is treated as the current server."
746 ;; clean up autologouts??
747 (and (or server nnimap-current-server)
748 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
749 (imap-opened (nnimap-get-server-buffer server))))
751 (deffoo nnimap-close-server (&optional server)
752 "Close connection to server and free all resources connected to it.
753 Return nil if the server couldn't be closed for some reason."
754 (let ((server (or server nnimap-current-server)))
755 (when (or (nnimap-server-opened server)
756 (imap-opened (nnimap-get-server-buffer server)))
757 (imap-close (nnimap-get-server-buffer server))
758 (kill-buffer (nnimap-get-server-buffer server))
759 (setq nnimap-server-buffer nil
760 nnimap-current-server nil
761 nnimap-server-buffer-alist
762 (delq server nnimap-server-buffer-alist)))
763 (nnoo-close-server 'nnimap server)))
765 (deffoo nnimap-request-close ()
766 "Close connection to all servers and free all resources that the backend have reserved.
767 All buffers that have been created by that
768 backend should be killed. (Not the nntp-server-buffer, though.) This
769 function is generally only called when Gnus is shutting down."
770 (mapcar (lambda (server) (nnimap-close-server (car server)))
771 nnimap-server-buffer-alist)
772 (setq nnimap-server-buffer-alist nil))
774 (deffoo nnimap-status-message (&optional server)
775 "This function returns the last error message from server."
776 (when (nnimap-possibly-change-server server)
777 (nnoo-status-message 'nnimap server)))
779 (defun nnimap-demule (string)
780 (funcall (if (and (fboundp 'string-as-multibyte)
781 (subrp (symbol-function 'string-as-multibyte)))
786 (defun nnimap-make-callback (article gnus-callback buffer)
787 "Return a callback function."
789 (nnimap-callback ,article ,gnus-callback ,buffer)))
791 (defun nnimap-callback (article gnus-callback buffer)
792 (when (eq article (imap-current-message))
793 (remove-hook 'imap-fetch-data-hook
794 (nnimap-make-callback article gnus-callback buffer))
795 (with-current-buffer buffer
797 (with-current-buffer nnimap-server-buffer
799 (if (imap-capability 'IMAP4rev1)
800 ;; xxx don't just use car? alist doesn't contain
801 ;; anything else now, but it might...
802 (nth 2 (car (imap-message-get article 'BODYDETAIL)))
803 (imap-message-get article 'RFC822)))))
804 (nnheader-ms-strip-cr)
805 (funcall gnus-callback t))))
807 (defun nnimap-request-article-part (article part prop &optional
808 group server to-buffer detail)
809 (when (nnimap-possibly-change-group group server)
810 (let ((article (if (stringp article)
811 (car-safe (imap-search
812 (format "HEADER Message-Id \"%s\"" article)
813 nnimap-server-buffer))
816 (gnus-message 10 "nnimap: Fetching (part of) article %d from %s..."
817 article (or group imap-current-mailbox
818 gnus-newsgroup-name))
819 (if (not nnheader-callback-function)
820 (with-current-buffer (or to-buffer nntp-server-buffer)
822 (let ((data (imap-fetch article part prop nil
823 nnimap-server-buffer)))
824 (insert (nnimap-demule (if detail
827 (nnheader-ms-strip-cr)
829 10 "nnimap: Fetching (part of) article %d from %s...done"
830 article (or group imap-current-mailbox gnus-newsgroup-name))
832 (nnheader-report 'nnimap "No such article %d in %s: %s"
833 article (or group imap-current-mailbox
835 (imap-error-text nnimap-server-buffer))
836 (cons group article)))
837 (add-hook 'imap-fetch-data-hook
838 (nnimap-make-callback article
839 nnheader-callback-function
841 (imap-fetch-asynch article part nil nnimap-server-buffer)
842 (cons group article))))))
844 (deffoo nnimap-asynchronous-p ()
847 (deffoo nnimap-request-article (article &optional group server to-buffer)
848 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
849 (nnimap-request-article-part
850 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
851 (nnimap-request-article-part
852 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
854 (deffoo nnimap-request-head (article &optional group server to-buffer)
855 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
856 (nnimap-request-article-part
857 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
858 (nnimap-request-article-part
859 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
861 (deffoo nnimap-request-body (article &optional group server to-buffer)
862 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
863 (nnimap-request-article-part
864 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
865 (nnimap-request-article-part
866 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
868 (deffoo nnimap-request-group (group &optional server fast)
869 (nnimap-request-update-info-internal
871 (gnus-get-info (gnus-group-prefixed-name
872 group (gnus-server-to-method (format "nnimap:%s" server))))
874 (when (nnimap-possibly-change-group group server)
875 (nnimap-before-find-minmax-bugworkaround)
878 ((null (setq info (nnimap-find-minmax-uid group t)))
879 (nnheader-report 'nnimap "Could not get active info for %s"
882 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
883 (max 1 (or (nth 1 info) 1))
884 (or (nth 2 info) 0) group)
885 (nnheader-report 'nnimap "Group %s selected" group)
888 (defun nnimap-update-unseen (group &optional server)
889 "Update the unseen count in `nnimap-mailbox-info'."
891 (gnus-group-prefixed-name group server)
892 (let ((old (gnus-gethash-safe (gnus-group-prefixed-name group server)
893 nnimap-mailbox-info)))
894 (list (nth 0 old) (nth 1 old)
895 (imap-mailbox-status group 'unseen nnimap-server-buffer)
897 nnimap-mailbox-info))
899 (defun nnimap-close-group (group &optional server)
900 (with-current-buffer nnimap-server-buffer
901 (when (and (imap-opened)
902 (nnimap-possibly-change-group group server))
903 (nnimap-update-unseen group server)
904 (case nnimap-expunge-on-close
906 (imap-mailbox-expunge nnimap-close-asynchronous)
907 (unless nnimap-dont-close
908 (imap-mailbox-close nnimap-close-asynchronous))))
909 (ask (if (and (imap-search "DELETED")
910 (gnus-y-or-n-p (format "Expunge articles in group `%s'? "
911 imap-current-mailbox)))
913 (imap-mailbox-expunge nnimap-close-asynchronous)
914 (unless nnimap-dont-close
915 (imap-mailbox-close nnimap-close-asynchronous)))
916 (imap-mailbox-unselect)))
917 (t (imap-mailbox-unselect)))
918 (not imap-current-mailbox))))
920 (defun nnimap-pattern-to-list-arguments (pattern)
922 (cons (car-safe p) (or (cdr-safe p) p)))
923 (if (and (listp pattern)
924 (listp (cdr pattern)))
928 (deffoo nnimap-request-list (&optional server)
929 (when (nnimap-possibly-change-server server)
930 (with-current-buffer nntp-server-buffer
932 (gnus-message 5 "nnimap: Generating active list%s..."
933 (if (> (length server) 0) (concat " for " server) ""))
934 (nnimap-before-find-minmax-bugworkaround)
935 (with-current-buffer nnimap-server-buffer
936 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
937 (dolist (mbx (funcall nnimap-request-list-method
938 (cdr pattern) (car pattern)))
939 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
940 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
942 (with-current-buffer nntp-server-buffer
943 (insert (format "\"%s\" %d %d y\n"
944 mbx (or (nth 2 info) 0)
945 (max 1 (or (nth 1 info) 1)))))))))))
946 (gnus-message 5 "nnimap: Generating active list%s...done"
947 (if (> (length server) 0) (concat " for " server) ""))
950 (deffoo nnimap-request-post (&optional server)
952 (dolist (mbx (message-unquote-tokens
953 (message-tokenize-header
954 (message-fetch-field "Newsgroups") ", ")) success)
955 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
956 (or (gnus-active to-newsgroup)
957 (gnus-activate-group to-newsgroup)
958 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
960 (or (and (gnus-request-create-group
961 to-newsgroup gnus-command-method)
962 (gnus-activate-group to-newsgroup nil nil
963 gnus-command-method))
964 (error "Couldn't create group %s" to-newsgroup)))
965 (error "No such group: %s" to-newsgroup))
966 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
967 (setq success nil))))))
969 ;; Optional backend functions
971 (defun nnimap-string-lessp-numerical (s1 s2)
972 "Return t if first arg string is less than second in numerical order."
973 (cond ((string= s1 s2)
975 ((> (length s1) (length s2))
977 ((< (length s1) (length s2))
979 ((< (string-to-number (substring s1 0 1))
980 (string-to-number (substring s2 0 1)))
982 ((> (string-to-number (substring s1 0 1))
983 (string-to-number (substring s2 0 1)))
986 (nnimap-string-lessp-numerical (substring s1 1) (substring s2 1)))))
988 (deffoo nnimap-retrieve-groups (groups &optional server)
989 (when (nnimap-possibly-change-server server)
990 (gnus-message 5 "nnimap: Checking mailboxes...")
991 (with-current-buffer nntp-server-buffer
993 (nnimap-before-find-minmax-bugworkaround)
994 (let (asyncgroups slowgroups)
995 (if (null nnimap-retrieve-groups-asynchronous)
996 (setq slowgroups groups)
997 (dolist (group groups)
998 (gnus-message 9 "nnimap: Quickly checking mailbox %s" group)
999 (add-to-list (if (gnus-gethash-safe
1000 (gnus-group-prefixed-name group server)
1001 nnimap-mailbox-info)
1004 (list group (imap-mailbox-status-asynch
1005 group '(uidvalidity uidnext unseen)
1006 nnimap-server-buffer))))
1007 (dolist (asyncgroup asyncgroups)
1008 (let ((group (nth 0 asyncgroup))
1009 (tag (nth 1 asyncgroup))
1011 (when (imap-ok-p (imap-wait-for-tag tag nnimap-server-buffer))
1012 (if (or (not (string=
1013 (nth 0 (gnus-gethash (gnus-group-prefixed-name
1015 nnimap-mailbox-info))
1016 (imap-mailbox-get 'uidvalidity group
1017 nnimap-server-buffer)))
1019 (nth 1 (gnus-gethash (gnus-group-prefixed-name
1021 nnimap-mailbox-info))
1022 (imap-mailbox-get 'uidnext group
1023 nnimap-server-buffer))))
1024 (push (list group) slowgroups)
1025 (insert (nth 3 (gnus-gethash (gnus-group-prefixed-name
1027 nnimap-mailbox-info))))))))
1028 (dolist (group slowgroups)
1029 (if nnimap-retrieve-groups-asynchronous
1030 (setq group (car group)))
1031 (gnus-message 7 "nnimap: Mailbox %s modified" group)
1032 (imap-mailbox-put 'uidnext nil group nnimap-server-buffer)
1033 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags group
1034 nnimap-server-buffer))
1035 (let* ((info (nnimap-find-minmax-uid group 'examine))
1036 (str (format "\"%s\" %d %d y\n" group
1038 (max 1 (or (nth 1 info) 1)))))
1039 (when (> (or (imap-mailbox-get 'recent group
1040 nnimap-server-buffer) 0)
1042 (push (list (cons group 0)) nnmail-split-history))
1044 (when nnimap-retrieve-groups-asynchronous
1046 (gnus-group-prefixed-name group server)
1047 (list (or (imap-mailbox-get
1048 'uidvalidity group nnimap-server-buffer)
1049 (imap-mailbox-status
1050 group 'uidvalidity nnimap-server-buffer))
1051 (or (imap-mailbox-get
1052 'uidnext group nnimap-server-buffer)
1053 (imap-mailbox-status
1054 group 'uidnext nnimap-server-buffer))
1055 (or (imap-mailbox-get
1056 'unseen group nnimap-server-buffer)
1057 (imap-mailbox-status
1058 group 'unseen nnimap-server-buffer))
1060 nnimap-mailbox-info)))))))
1061 (gnus-message 5 "nnimap: Checking mailboxes...done")
1064 (deffoo nnimap-request-update-info-internal (group info &optional server)
1065 (when (nnimap-possibly-change-group group server)
1066 (when info ;; xxx what does this mean? should we create a info?
1067 (with-current-buffer nnimap-server-buffer
1068 (gnus-message 5 "nnimap: Updating info for %s..."
1069 (gnus-info-group info))
1071 (when (nnimap-mark-permanent-p 'read)
1073 ;; read info could contain articles marked unread by other
1074 ;; imap clients! we correct this
1075 (setq seen (gnus-uncompress-range (gnus-info-read info))
1076 unseen (imap-search "UNSEEN UNDELETED")
1077 seen (gnus-set-difference seen unseen)
1078 ;; seen might lack articles marked as read by other
1079 ;; imap clients! we correct this
1080 seen (append seen (imap-search "SEEN"))
1083 seen (gnus-compress-sequence seen t)
1084 ;; we can't return '(1) since this isn't a "list of ranges",
1085 ;; and we can't return '((1)) since g-list-of-unread-articles
1086 ;; is buggy so we return '((1 . 1)).
1087 seen (if (and (integerp (car seen))
1089 (list (cons (car seen) (car seen)))
1091 (gnus-info-set-read info seen)))
1093 (mapcar (lambda (pred)
1094 (when (or (eq (cdr pred) 'recent)
1095 (and (nnimap-mark-permanent-p (cdr pred))
1096 (member (nnimap-mark-to-flag (cdr pred))
1097 (imap-mailbox-get 'flags))))
1098 (gnus-info-set-marks
1100 (gnus-update-alist-soft
1102 (gnus-compress-sequence
1103 (imap-search (nnimap-mark-to-predicate (cdr pred))))
1104 (gnus-info-marks info))
1106 gnus-article-mark-lists)
1108 (when nnimap-importantize-dormant
1109 ;; nnimap mark dormant article as ticked too (for other clients)
1110 ;; so we remove that mark for gnus since we support dormant
1111 (gnus-info-set-marks
1113 (gnus-update-alist-soft
1115 (gnus-remove-from-range
1116 (cdr-safe (assoc 'tick (gnus-info-marks info)))
1117 (cdr-safe (assoc 'dormant (gnus-info-marks info))))
1118 (gnus-info-marks info))
1121 (gnus-message 5 "nnimap: Updating info for %s...done"
1122 (gnus-info-group info))
1126 (deffoo nnimap-request-type (group &optional article)
1127 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
1131 (deffoo nnimap-request-set-mark (group actions &optional server)
1132 (when (nnimap-possibly-change-group group server)
1133 (with-current-buffer nnimap-server-buffer
1135 (gnus-message 7 "nnimap: Setting marks in %s..." group)
1136 (while (setq action (pop actions))
1137 (let ((range (nth 0 action))
1138 (what (nth 1 action))
1139 (cmdmarks (nth 2 action))
1141 ;; bookmark can't be stored (not list/range
1142 (setq cmdmarks (delq 'bookmark cmdmarks))
1143 ;; killed can't be stored (not list/range
1144 (setq cmdmarks (delq 'killed cmdmarks))
1145 ;; unsent are for nndraft groups only
1146 (setq cmdmarks (delq 'unsent cmdmarks))
1147 ;; cache flags are pointless on the server
1148 (setq cmdmarks (delq 'cache cmdmarks))
1149 ;; seen flags are local to each gnus
1150 (setq cmdmarks (delq 'seen cmdmarks))
1151 ;; recent marks can't be set
1152 (setq cmdmarks (delq 'recent cmdmarks))
1153 (when nnimap-importantize-dormant
1154 ;; flag dormant articles as ticked
1155 (if (memq 'dormant cmdmarks)
1156 (setq cmdmarks (cons 'tick cmdmarks))))
1157 ;; remove stuff we are forbidden to store
1158 (mapcar (lambda (mark)
1159 (if (imap-message-flag-permanent-p
1160 (nnimap-mark-to-flag mark))
1161 (setq marks (cons mark marks))))
1163 (when (and range marks)
1164 (cond ((eq what 'del)
1165 (imap-message-flags-del
1166 (imap-range-to-message-set range)
1167 (nnimap-mark-to-flag marks nil t)))
1169 (imap-message-flags-add
1170 (imap-range-to-message-set range)
1171 (nnimap-mark-to-flag marks nil t)))
1173 (imap-message-flags-set
1174 (imap-range-to-message-set range)
1175 (nnimap-mark-to-flag marks nil t)))))))
1176 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
1179 (defun nnimap-split-fancy ()
1180 "Like the function `nnmail-split-fancy', but uses `nnimap-split-fancy'."
1181 (let ((nnmail-split-fancy nnimap-split-fancy))
1182 (nnmail-split-fancy)))
1184 (defun nnimap-split-to-groups (rules)
1185 ;; tries to match all rules in nnimap-split-rule against content of
1186 ;; nntp-server-buffer, returns a list of groups that matched.
1187 (with-current-buffer nntp-server-buffer
1188 ;; Fold continuation lines.
1189 (goto-char (point-min))
1190 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1191 (replace-match " " t t))
1192 (if (functionp rules)
1194 (let (to-groups regrepp)
1196 (dolist (rule rules to-groups)
1197 (let ((group (car rule))
1198 (regexp (cadr rule)))
1199 (goto-char (point-min))
1200 (when (and (if (stringp regexp)
1202 (if (not (stringp group))
1203 (setq group (eval group))
1205 (string-match "\\\\[0-9&]" group)))
1206 (re-search-forward regexp nil t))
1207 (funcall regexp group))
1208 ;; Don't enter the article into the same group twice.
1209 (not (assoc group to-groups)))
1211 (nnmail-expand-newtext group)
1214 (or nnimap-split-crosspost
1215 (throw 'split-done to-groups))))))))))
1217 (defun nnimap-assoc-match (key alist)
1219 (while (and alist (not element))
1220 (if (string-match (car (car alist)) key)
1221 (setq element (car alist)))
1222 (setq alist (cdr alist)))
1225 (defun nnimap-split-find-rule (server inbox)
1226 (if (and (listp nnimap-split-rule) (listp (car nnimap-split-rule))
1227 (list (cdar nnimap-split-rule)) (listp (cadar nnimap-split-rule)))
1229 (cadr (nnimap-assoc-match inbox (cdr (nnimap-assoc-match
1230 server nnimap-split-rule))))
1233 (defun nnimap-split-find-inbox (server)
1234 (if (listp nnimap-split-inbox)
1236 (list nnimap-split-inbox)))
1238 (defun nnimap-split-articles (&optional group server)
1239 (when (nnimap-possibly-change-server server)
1240 (with-current-buffer nnimap-server-buffer
1241 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
1242 ;; iterate over inboxes
1243 (while (and (setq inbox (pop inboxes))
1244 (nnimap-possibly-change-group inbox)) ;; SELECT
1245 ;; find split rule for this server / inbox
1246 (when (setq rule (nnimap-split-find-rule server inbox))
1247 ;; iterate over articles
1248 (dolist (article (imap-search nnimap-split-predicate))
1249 (when (nnimap-request-head article)
1250 ;; copy article to right group(s)
1251 (setq removeorig nil)
1252 (dolist (to-group (nnimap-split-to-groups rule))
1253 (cond ((eq to-group 'junk)
1254 (message "IMAP split removed %s:%s:%d" server inbox
1256 (setq removeorig t))
1257 ((imap-message-copy (number-to-string article)
1258 to-group nil 'nocopyuid)
1259 (message "IMAP split moved %s:%s:%d to %s" server
1260 inbox article to-group)
1262 (when nnmail-cache-accepted-message-ids
1263 (with-current-buffer nntp-server-buffer
1266 (nnmail-fetch-field "message-id"))
1267 (nnmail-cache-insert msgid to-group)))))
1268 ;; Add the group-art list to the history list.
1269 (push (list (cons to-group 0)) nnmail-split-history))
1271 (message "IMAP split failed to move %s:%s:%d to %s"
1272 server inbox article to-group))))
1273 ;; remove article if it was successfully copied somewhere
1275 (imap-message-flags-add (format "%d" article)
1276 "\\Seen \\Deleted")))))
1277 (when (imap-mailbox-select inbox) ;; just in case
1278 ;; todo: UID EXPUNGE (if available) to remove splitted articles
1279 (imap-mailbox-expunge)
1280 (imap-mailbox-close)))
1281 (when nnmail-cache-accepted-message-ids
1282 (nnmail-cache-close))
1285 (deffoo nnimap-request-scan (&optional group server)
1286 (nnimap-split-articles group server))
1288 (deffoo nnimap-request-newgroups (date &optional server)
1289 (when (nnimap-possibly-change-server server)
1290 (with-current-buffer nntp-server-buffer
1291 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
1292 (if (> (length server) 0) " on " "") server)
1294 (nnimap-before-find-minmax-bugworkaround)
1295 (dolist (pattern (nnimap-pattern-to-list-arguments
1296 nnimap-list-pattern))
1297 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
1298 nnimap-server-buffer))
1300 (dolist (mailbox (imap-mailbox-get 'list-flags mbx
1301 nnimap-server-buffer))
1302 (if (string= (downcase mailbox) "\\noselect")
1305 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
1307 (insert (format "\"%s\" %d %d y\n"
1308 mbx (or (nth 2 info) 0)
1309 (max 1 (or (nth 1 info) 1)))))))))
1310 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
1311 (if (> (length server) 0) " on " "") server))
1314 (deffoo nnimap-request-create-group (group &optional server args)
1315 (when (nnimap-possibly-change-server server)
1316 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
1317 (imap-mailbox-create group nnimap-server-buffer))))
1319 (defun nnimap-time-substract (time1 time2)
1320 "Return TIME for TIME1 - TIME2."
1321 (let* ((ms (- (car time1) (car time2)))
1322 (ls (- (nth 1 time1) (nth 1 time2))))
1324 (list (- ms 1) (+ (expt 2 16) ls))
1327 (defun nnimap-date-days-ago (daysago)
1328 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
1329 (let* ((time (nnimap-time-substract (current-time) (days-to-time daysago)))
1330 (date (format-time-string
1331 (format "%%d-%s-%%Y"
1332 (capitalize (car (rassoc (nth 4 (decode-time time))
1333 parse-time-months))))
1335 (if (eq ?0 (string-to-char date))
1339 (defun nnimap-request-expire-articles-progress ()
1340 (gnus-message 5 "nnimap: Marking article %d for deletion..."
1341 imap-current-message))
1343 (defun nnimap-expiry-target (arts group server)
1344 (unless (eq nnmail-expiry-target 'delete)
1347 (nnimap-request-article art group server (current-buffer))
1348 ;; hints for optimization in `nnimap-request-accept-article'
1349 (let ((nnimap-current-move-article art)
1350 (nnimap-current-move-group group)
1351 (nnimap-current-move-server server))
1352 (nnmail-expiry-target-group nnmail-expiry-target group))))
1353 ;; It is not clear if `nnmail-expiry-target' somehow cause the
1354 ;; current group to be changed or not, so we make sure here.
1355 (nnimap-possibly-change-group group server)))
1357 ;; Notice that we don't actually delete anything, we just mark them deleted.
1358 (deffoo nnimap-request-expire-articles (articles group &optional server force)
1359 (let ((artseq (gnus-compress-sequence articles)))
1360 (when (and artseq (nnimap-possibly-change-group group server))
1361 (with-current-buffer nnimap-server-buffer
1362 (let ((days (or (and nnmail-expiry-wait-function
1363 (funcall nnmail-expiry-wait-function group))
1364 nnmail-expiry-wait)))
1365 (cond ((or force (eq days 'immediate))
1366 (let ((oldarts (imap-search
1368 (imap-range-to-message-set artseq)))))
1370 (nnimap-expiry-target oldarts group server)
1371 (when (imap-message-flags-add
1372 (imap-range-to-message-set
1373 (gnus-compress-sequence oldarts)) "\\Deleted")
1374 (setq articles (gnus-set-difference
1375 articles oldarts))))))
1377 (let ((oldarts (imap-search
1378 (format nnimap-expunge-search-string
1379 (imap-range-to-message-set artseq)
1380 (nnimap-date-days-ago days))))
1381 (imap-fetch-data-hook
1382 '(nnimap-request-expire-articles-progress)))
1384 (nnimap-expiry-target oldarts group server)
1385 (when (imap-message-flags-add
1386 (imap-range-to-message-set
1387 (gnus-compress-sequence oldarts)) "\\Deleted")
1388 (setq articles (gnus-set-difference
1389 articles oldarts)))))))))))
1390 ;; return articles not deleted
1393 (deffoo nnimap-request-move-article (article group server
1394 accept-form &optional last)
1395 (when (nnimap-possibly-change-server server)
1397 (let ((buf (get-buffer-create " *nnimap move*"))
1398 (nnimap-current-move-article article)
1399 (nnimap-current-move-group group)
1400 (nnimap-current-move-server nnimap-current-server)
1402 (and (nnimap-request-article article group server)
1405 (buffer-disable-undo (current-buffer))
1406 (insert-buffer-substring nntp-server-buffer)
1407 (setq result (eval accept-form))
1410 (imap-message-flags-add
1411 (imap-range-to-message-set (list article))
1412 "\\Deleted" 'silent nnimap-server-buffer))
1415 (deffoo nnimap-request-accept-article (group &optional server last)
1416 (when (nnimap-possibly-change-server server)
1419 (if (string= nnimap-current-server nnimap-current-move-server)
1420 ;; moving article within same server, speed it up...
1421 (and (nnimap-possibly-change-group
1422 nnimap-current-move-group)
1423 (imap-message-copy (number-to-string
1424 nnimap-current-move-article)
1425 group 'dontcreate nil
1426 nnimap-server-buffer))
1427 (with-current-buffer (current-buffer)
1428 (goto-char (point-min))
1429 ;; remove any 'From blabla' lines, some IMAP servers
1430 ;; reject the entire message otherwise.
1431 (when (looking-at "^From[^:]")
1432 (kill-region (point) (progn (forward-line) (point))))
1433 ;; turn into rfc822 format (\r\n eol's)
1434 (while (search-forward "\n" nil t)
1435 (replace-match "\r\n"))
1436 (when nnmail-cache-accepted-message-ids
1437 (nnmail-cache-insert (nnmail-fetch-field "message-id")
1439 (when (and last nnmail-cache-accepted-message-ids)
1440 (nnmail-cache-close))
1441 ;; this 'or' is for Cyrus server bug
1442 (or (null (imap-current-mailbox nnimap-server-buffer))
1443 (imap-mailbox-unselect nnimap-server-buffer))
1444 (imap-message-append group (current-buffer) nil nil
1445 nnimap-server-buffer)))
1446 (cons group (nth 1 uid))
1447 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1449 (deffoo nnimap-request-delete-group (group force &optional server)
1450 (when (nnimap-possibly-change-server server)
1451 (with-current-buffer nnimap-server-buffer
1453 (or (null (imap-mailbox-status group 'uidvalidity))
1454 (imap-mailbox-delete group))
1458 (deffoo nnimap-request-rename-group (group new-name &optional server)
1459 (when (nnimap-possibly-change-server server)
1460 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1462 (defun nnimap-expunge (mailbox server)
1463 (when (nnimap-possibly-change-group mailbox server)
1464 (imap-mailbox-expunge nil nnimap-server-buffer)))
1466 (defun nnimap-acl-get (mailbox server)
1467 (when (nnimap-possibly-change-server server)
1468 (and (imap-capability 'ACL nnimap-server-buffer)
1469 (imap-mailbox-acl-get mailbox nnimap-server-buffer))))
1471 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1472 (when (nnimap-possibly-change-server (cadr method))
1473 (unless (imap-capability 'ACL nnimap-server-buffer)
1474 (error "Your server does not support ACL editing"))
1475 (with-current-buffer nnimap-server-buffer
1476 ;; delete all removed identifiers
1477 (mapcar (lambda (old-acl)
1478 (unless (assoc (car old-acl) new-acls)
1479 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1480 (error "Can't delete ACL for %s" (car old-acl)))))
1482 ;; set all changed acl's
1483 (mapcar (lambda (new-acl)
1484 (let ((new-rights (cdr new-acl))
1485 (old-rights (cdr (assoc (car new-acl) old-acls))))
1486 (unless (and old-rights new-rights
1487 (string= old-rights new-rights))
1488 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1489 (error "Can't set ACL for %s to %s" (car new-acl)
1495 ;;; Internal functions
1498 ;; This is confusing.
1500 ;; mark => read, tick, draft, reply etc
1501 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1502 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1504 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1505 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1508 (defconst nnimap-mark-to-predicate-alist
1510 (lambda (pair) ; cdr is the mark
1511 (or (assoc (cdr pair)
1516 (reply . "ANSWERED")))
1518 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1519 (cons '(read . read) gnus-article-mark-lists)))
1521 (defun nnimap-mark-to-predicate (pred)
1522 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1523 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1524 to be used within a IMAP SEARCH query."
1525 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1527 (defconst nnimap-mark-to-flag-alist
1530 (or (assoc (cdr pair)
1532 (tick . "\\Flagged")
1534 (recent . "\\Recent")
1535 (reply . "\\Answered")))
1537 (format "gnus-%s" (symbol-name (cdr pair))))))
1538 (cons '(read . read) gnus-article-mark-lists)))
1540 (defun nnimap-mark-to-flag-1 (preds)
1541 (if (and (not (null preds)) (listp preds))
1542 (cons (nnimap-mark-to-flag (car preds))
1543 (nnimap-mark-to-flag (cdr preds)))
1544 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1546 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1547 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1548 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1549 be used in a STORE FLAGS command."
1550 (let ((result (nnimap-mark-to-flag-1 preds)))
1551 (setq result (if (and (or make-string always-list)
1552 (not (listp result)))
1556 (mapconcat (lambda (flag)
1558 (mapconcat 'identity flag " ")
1563 (defun nnimap-mark-permanent-p (mark &optional group)
1564 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1565 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1569 (buffer-disable-undo (get-buffer-create nnimap-debug-buffer))
1570 (mapcar (lambda (f) (trace-function-background f nnimap-debug-buffer))
1572 nnimap-possibly-change-server
1573 nnimap-verify-uidvalidity
1574 nnimap-find-minmax-uid
1575 nnimap-before-find-minmax-bugworkaround
1576 nnimap-possibly-change-group
1577 ;;nnimap-replace-whitespace
1578 nnimap-retrieve-headers-progress
1579 nnimap-retrieve-which-headers
1580 nnimap-group-overview-filename
1581 nnimap-retrieve-headers-from-file
1582 nnimap-retrieve-headers-from-server
1583 nnimap-retrieve-headers
1584 nnimap-open-connection
1586 nnimap-server-opened
1588 nnimap-request-close
1589 nnimap-status-message
1591 nnimap-request-article-part
1592 nnimap-request-article
1595 nnimap-request-group
1597 nnimap-pattern-to-list-arguments
1600 nnimap-retrieve-groups
1601 nnimap-request-update-info-internal
1603 nnimap-request-set-mark
1604 nnimap-split-to-groups
1605 nnimap-split-find-rule
1606 nnimap-split-find-inbox
1607 nnimap-split-articles
1609 nnimap-request-newgroups
1610 nnimap-request-create-group
1611 nnimap-time-substract
1612 nnimap-date-days-ago
1613 nnimap-request-expire-articles-progress
1614 nnimap-request-expire-articles
1615 nnimap-request-move-article
1616 nnimap-request-accept-article
1617 nnimap-request-delete-group
1618 nnimap-request-rename-group
1619 gnus-group-nnimap-expunge
1620 gnus-group-nnimap-edit-acl
1621 gnus-group-nnimap-edit-acl-done
1622 nnimap-group-mode-hook
1623 nnimap-mark-to-predicate
1624 nnimap-mark-to-flag-1
1626 nnimap-mark-permanent-p
1631 ;;; nnimap.el ends here