1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <jas@pdc.kth.se>
6 ;; Jim Radford <radford@robby.caltech.edu>
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)
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.
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)
34 ;; o MIME (partial article fetches)
35 ;; o Split to other backends, different split rules for different
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
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
56 ;; o What about Gnus's article editing, can we support it? NO!
57 ;; o Use \Draft to support the draft group??
58 ;; o Duplicate suppression
59 ;; o Rewrite UID SEARCH UID X as UID FETCH X (UID) for those with slow servers
73 (eval-when-compile (require 'cl))
77 (defconst nnimap-version "nnimap 1.0")
80 "Reading IMAP mail with Gnus."
83 (defvoo nnimap-address nil
84 "Address of physical IMAP server. If nil, use the virtual server's name.")
86 (defvoo nnimap-server-port nil
87 "Port number on physical IMAP server.
88 If nil, defaults to 993 for TLS/SSL connections and 143 otherwise.")
90 ;; Splitting variables
92 (defcustom nnimap-split-crosspost t
93 "If non-nil, do crossposting if several split methods match the mail.
94 If nil, the first match found will be used."
98 (defcustom nnimap-split-inbox nil
99 "Name of mailbox to split mail from.
101 Mail is read from this mailbox and split according to rules in
104 This can be a string or a list of strings."
106 :type '(choice (string)
109 (define-widget 'nnimap-strict-function 'function
110 "This widget only matches values that are functionp.
112 Warning: This means that a value that is the symbol of a not yet
113 loaded function will not match. Use with care."
114 :match 'nnimap-strict-function-match)
116 (defun nnimap-strict-function-match (widget value)
117 "Ignoring WIDGET, match if VALUE is a function."
120 (defcustom nnimap-split-rule nil
121 "Mail will be split according to these rules.
123 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
125 If you'd like, for instance, one mail group for mail from the
126 \"gnus-imap\" mailing list, one group for junk mail and leave
127 everything else in the incoming mailbox, you could do something like
130 \(setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
131 (\"INBOX.junk\" \"Subject:.*buy\")))
133 As you can see, `nnimap-split-rule' is a list of lists, where the
134 first element in each \"rule\" is the name of the IMAP mailbox (or the
135 symbol `junk' if you want to remove the mail), and the second is a
136 regexp that nnimap will try to match on the header to find a fit.
138 The second element can also be a function. In that case, it will be
139 called narrowed to the headers with the first element of the rule as
140 the argument. It should return a non-nil value if it thinks that the
141 mail belongs in that group.
143 This variable can also have a function as its value, the function will
144 be called with the headers narrowed and should return a group where it
145 thinks the article should be splitted to. See `nnimap-split-fancy'.
147 To allow for different split rules on different virtual servers, and
148 even different split rules in different inboxes on the same server,
149 the syntax of this variable have been extended along the lines of:
151 \(setq nnimap-split-rule
152 '((\"my1server\" (\".*\" ((\"ding\" \"ding@gnus.org\")
153 (\"junk\" \"From:.*Simon\")))
154 (\"my2server\" (\"INBOX\" nnimap-split-fancy))
155 (\"my[34]server\" (\".*\" ((\"private\" \"To:.*Simon\")
156 (\"junk\" my-junk-func)))))
158 The virtual server name is in fact a regexp, so that the same rules
159 may apply to several servers. In the example, the servers
160 \"my3server\" and \"my4server\" both use the same rules. Similarly,
161 the inbox string is also a regexp. The actual splitting rules are as
162 before, either a function, or a list with group/regexp or
163 group/function elements."
165 :type '(choice :tag "Rule type"
166 (repeat :menu-tag "Single-server"
167 :tag "Single-server list"
168 (list (string :tag "Mailbox")
169 (choice :tag "Predicate"
170 (regexp :tag "A regexp")
171 (nnimap-strict-function :tag "A function"))))
172 (choice :menu-tag "A function"
174 (function-item nnimap-split-fancy)
175 (function-item nnmail-split-fancy)
176 (nnimap-strict-function :tag "User-defined function"))
177 (repeat :menu-tag "Multi-server (extended)"
178 :tag "Multi-server list"
179 (list (regexp :tag "Server regexp")
180 (list (regexp :tag "Incoming Mailbox regexp")
181 (repeat :tag "Rules for matching server(s) and mailbox(es)"
182 (list (string :tag "Destination mailbox")
183 (choice :tag "Predicate"
184 (regexp :tag "A Regexp")
185 (nnimap-strict-function :tag "A Function")))))))))
187 (defcustom nnimap-split-predicate "UNSEEN UNDELETED"
188 "The predicate used to find articles to split.
189 If you use another IMAP client to peek on articles but always would
190 like nnimap to split them once it's started, you could change this to
191 \"UNDELETED\". Other available predicates are available in
192 RFC2060 section 6.4.4."
196 (defcustom nnimap-split-fancy nil
197 "Like the variable `nnmail-split-fancy'."
201 (defvar nnimap-split-download-body-default nil
202 "Internal variable with default value for `nnimap-split-download-body'.")
204 (defcustom nnimap-split-download-body 'default
205 "Whether to download entire articles during splitting.
206 This is generally not required, and will slow things down considerably.
207 You may need it if you want to use an advanced splitting function that
208 analyses the body before splitting the article.
209 If this variable is nil, bodies will not be downloaded; if this
210 variable is the symbol `default' the default behaviour is
211 used (which currently is nil, unless you use a statistical
212 spam.el test); if this variable is another non-nil value bodies
216 :type '(choice (const :tag "Let system decide" deault)
219 ;; Performance / bug workaround variables
221 (defcustom nnimap-close-asynchronous t
222 "Close mailboxes asynchronously in `nnimap-close-group'.
223 This means that errors caught by nnimap when closing the mailbox will
224 not prevent Gnus from updating the group status, which may be harmful.
225 However, it increases speed."
230 (defcustom nnimap-dont-close t
231 "Never close mailboxes.
232 This increases the speed of closing mailboxes (quiting group) but may
233 decrease the speed of selecting another mailbox later. Re-selecting
234 the same mailbox will be faster though."
239 (defcustom nnimap-retrieve-groups-asynchronous t
240 "Send asynchronous STATUS commands for each mailbox before checking mail.
241 If you have mailboxes that rarely receives mail, this speeds up new
242 mail checking. It works by first sending STATUS commands for each
243 mailbox, and then only checking groups which has a modified UIDNEXT
244 more carefully for new mail.
246 In summary, the default is O((1-p)*k+p*n) and changing it to nil makes
247 it O(n). If p is small, then the default is probably faster."
252 (defvoo nnimap-need-unselect-to-notice-new-mail nil
253 "Unselect mailboxes before looking for new mail in them.
254 Some servers seem to need this under some circumstances.")
256 ;; Authorization / Privacy variables
258 (defvoo nnimap-auth-method nil
261 (defvoo nnimap-stream nil
262 "How nnimap will connect to the server.
264 The default, nil, will try to use the \"best\" method the server can
269 1) you want to connect with TLS/SSL. The TLS/SSL integration
270 with IMAP is suboptimal so you'll have to tell it
273 2) your server is more capable than your environment -- i.e. your
274 server accept Kerberos login's but you haven't installed the
275 `imtest' program or your machine isn't configured for Kerberos.
277 Possible choices: gssapi, kerberos4, starttls, tls, ssl, network, shell.
278 See also `imap-streams' and `imap-stream-alist'.")
280 (defvoo nnimap-authenticator nil
281 "How nnimap authenticate itself to the server.
283 The default, nil, will try to use the \"best\" method the server can
286 There is only one reason for fiddling with this variable, and that is
287 if your server is more capable than your environment -- i.e. you
288 connect to a server that accept Kerberos login's but you haven't
289 installed the `imtest' program or your machine isn't configured for
292 Possible choices: gssapi, kerberos4, digest-md5, cram-md5, login, anonymous.
293 See also `imap-authenticators' and `imap-authenticator-alist'")
295 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
296 "Directory to keep NOV cache files for nnimap groups.
297 See also `nnimap-nov-file-name'.")
299 (defvoo nnimap-nov-file-name "nnimap."
300 "NOV cache base filename.
301 The group name and `nnimap-nov-file-name-suffix' will be appended. A
302 typical complete file name would be
303 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
304 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
305 `nnmail-use-long-file-names' is nil")
307 (defvoo nnimap-nov-file-name-suffix ".novcache"
308 "Suffix for NOV cache base filename.")
310 (defvoo nnimap-nov-is-evil gnus-agent
311 "If non-nil, never generate or use a local nov database for this backend.
312 Using nov databases should speed up header fetching considerably.
313 However, it will invoke a UID SEARCH UID command on the server, and
314 some servers implement this command inefficiently by opening each and
315 every message in the group, thus making it quite slow.
316 Unlike other backends, you do not need to take special care if you
317 flip this variable.")
319 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
320 "Whether to expunge a group when it is closed.
321 When a IMAP group with articles marked for deletion is closed, this
322 variable determine if nnimap should actually remove the articles or
325 If always, nnimap always perform a expunge when closing the group.
326 If never, nnimap never expunges articles marked for deletion.
327 If ask, nnimap will ask you if you wish to expunge marked articles.
329 When setting this variable to `never', you can only expunge articles
330 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
332 (defvoo nnimap-list-pattern "*"
333 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
334 See below for available wildcards.
336 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
337 REFERENCE will be passed as the first parameter to LIST/LSUB. The
338 semantics of this are server specific, on the University of Washington
339 server you can specify a directory.
342 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
344 There are two wildcards * and %. * matches everything, % matches
345 everything in the current hierarchy.")
347 (defvoo nnimap-news-groups nil
348 "IMAP support a news-like mode, also known as bulletin board mode,
349 where replies is sent via IMAP instead of SMTP.
351 This variable should contain a regexp matching groups where you wish
352 replies to be stored to the mailbox directly.
355 '(\"^[^I][^N][^B][^O][^X].*$\")
357 This will match all groups not beginning with \"INBOX\".
359 Note that there is nothing technically different between mail-like and
360 news-like mailboxes. If you wish to have a group with todo items or
361 similar which you wouldn't want to set up a mailing list for, you can
362 use this to make replies go directly to the group.")
364 (defvoo nnimap-expunge-search-string "UID %s NOT SINCE %s"
365 "IMAP search command to use for articles that are to be expired.
366 The first %s is replaced by a UID set of articles to search on,
367 and the second %s is replaced by a date criterium.
369 One useful (and perhaps the only useful) value to change this to would
370 be `UID %s NOT SENTSINCE %s' to make nnimap use the Date: header
371 instead of the internal date of messages. See section 6.4.4 of RFC
372 2060 for more information on valid strings.")
374 (defvoo nnimap-importantize-dormant t
375 "If non-nil, mark \"dormant\" articles as \"ticked\" for other IMAP clients.
376 Note that within Gnus, dormant articles will still (only) be
377 marked as ticked. This is to make \"dormant\" articles stand out,
378 just like \"ticked\" articles, in other IMAP clients.")
380 (defvoo nnimap-server-address nil
381 "Obsolete. Use `nnimap-address'.")
383 (defcustom nnimap-authinfo-file "~/.authinfo"
384 "Authorization information for IMAP servers. In .netrc format."
387 (repeat :tag "Entries"
390 :value ("" ("login" . "") ("password" . ""))
394 (const :format "" "login")
395 (string :format "Login: %v"))
397 (const :format "" "password")
398 (string :format "Password: %v"))))))
401 (defcustom nnimap-prune-cache t
402 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
406 (defvar nnimap-request-list-method 'imap-mailbox-list
407 "Method to use to request a list of all folders from the server.
408 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
409 restrict visible folders.")
411 (defcustom nnimap-debug nil
412 "If non-nil, random debug spews are placed in *nnimap-debug* buffer.
413 Note that username, passwords and other privacy sensitive
414 information (such as e-mail) may be stored in the *nnimap-debug*
415 buffer. It is not written to disk, however. Do not enable this
416 variable unless you are comfortable with that."
420 ;; Internal variables:
422 (defvar nnimap-debug-buffer "*nnimap-debug*")
423 (defvar nnimap-mailbox-info (gnus-make-hashtable 997))
424 (defvar nnimap-current-move-server nil)
425 (defvar nnimap-current-move-group nil)
426 (defvar nnimap-current-move-article nil)
427 (defvar nnimap-length)
428 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
429 (defvar nnimap-progress-how-often 20)
430 (defvar nnimap-counter)
431 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
432 (defvar nnimap-current-server nil) ;; Current server
433 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
437 (nnoo-define-basics nnimap)
439 ;; Utility functions:
441 (defsubst nnimap-get-server-buffer (server)
442 "Return buffer for SERVER, if nil use current server."
443 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
445 (defun nnimap-possibly-change-server (server)
446 "Return buffer for SERVER, changing the current server as a side-effect.
447 If SERVER is nil, uses the current server."
448 (setq nnimap-current-server (or server nnimap-current-server)
449 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
451 (defun nnimap-verify-uidvalidity (group server)
452 "Verify stored uidvalidity match current one in GROUP on SERVER."
453 (let* ((gnusgroup (gnus-group-prefixed-name
454 group (gnus-server-to-method
455 (format "nnimap:%s" server))))
456 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
457 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity))
458 (dir (file-name-as-directory (expand-file-name nnimap-directory)))
459 (nameuid (nnheader-translate-file-chars
460 (concat nnimap-nov-file-name
461 (if (equal server "")
463 server) "." group "." old-uidvalidity
464 nnimap-nov-file-name-suffix) t))
465 (file (if (or nnmail-use-long-file-names
466 (file-exists-p (expand-file-name nameuid dir)))
467 (expand-file-name nameuid dir)
469 (mm-encode-coding-string
470 (nnheader-replace-chars-in-string nameuid ?. ?/)
471 nnmail-pathname-coding-system)
474 (if (not (equal old-uidvalidity new-uidvalidity))
476 (gnus-delete-file file)
477 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
479 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
482 (defun nnimap-before-find-minmax-bugworkaround ()
483 "Function called before iterating through mailboxes with
484 `nnimap-find-minmax-uid'."
485 (when nnimap-need-unselect-to-notice-new-mail
486 ;; XXX this is for UoW imapd problem, it doesn't notice new mail in
487 ;; currently selected mailbox without a re-select/examine.
488 (or (null (imap-current-mailbox nnimap-server-buffer))
489 (imap-mailbox-unselect nnimap-server-buffer))))
491 (defun nnimap-find-minmax-uid (group &optional examine)
492 "Find lowest and highest active article number in GROUP.
493 If EXAMINE is non-nil the group is selected read-only."
494 (with-current-buffer nnimap-server-buffer
495 (when (or (string= group (imap-current-mailbox))
496 (imap-mailbox-select group examine))
498 (when (> (imap-mailbox-get 'exists) 0)
499 (imap-fetch "1,*" "UID" nil 'nouidfetch)
500 (imap-message-map (lambda (uid Uid)
501 (setq minuid (if minuid (min minuid uid) uid)
502 maxuid (if maxuid (max maxuid uid) uid)))
504 (list (imap-mailbox-get 'exists) minuid maxuid)))))
506 (defun nnimap-possibly-change-group (group &optional server)
507 "Make GROUP the current group, and SERVER the current server."
508 (when (nnimap-possibly-change-server server)
509 (with-current-buffer nnimap-server-buffer
510 (if (or (null group) (imap-current-mailbox-p group))
512 (if (imap-mailbox-select group)
513 (if (or (nnimap-verify-uidvalidity
514 group (or server nnimap-current-server))
515 (zerop (imap-mailbox-get 'exists group))
516 t ;; for OGnus to see if ignoring uidvalidity
517 ;; changes has any bad effects.
520 "nnimap: Group %s is not uidvalid. Continue? " group)))
522 (imap-mailbox-unselect)
523 (error "nnimap: Group %s is not uid-valid" group))
524 (nnheader-report 'nnimap (imap-error-text)))))))
526 (defun nnimap-replace-whitespace (string)
527 "Return STRING with all whitespace replaced with space."
529 (while (string-match "[\r\n\t]+" string)
530 (setq string (replace-match " " t t string)))
533 ;; Required backend functions
535 (defun nnimap-retrieve-headers-progress ()
536 "Hook to insert NOV line for current article into `nntp-server-buffer'."
537 (and (numberp nnmail-large-newsgroup)
538 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
539 (> nnimap-length nnmail-large-newsgroup)
540 (nnheader-message 6 "nnimap: Retrieving headers... %c"
541 (nth (/ (% nnimap-counter
542 (* (length nnimap-progress-chars)
543 nnimap-progress-how-often))
544 nnimap-progress-how-often)
545 nnimap-progress-chars)))
546 (with-current-buffer nntp-server-buffer
547 (let (headers lines chars uid mbx)
548 (with-current-buffer nnimap-server-buffer
549 (setq uid imap-current-message
550 mbx imap-current-mailbox
551 headers (nnimap-demule
552 (if (imap-capability 'IMAP4rev1)
553 ;; xxx don't just use car? alist doesn't contain
554 ;; anything else now, but it might...
555 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
556 (imap-message-get uid 'RFC822.HEADER)))
557 lines (imap-body-lines (imap-message-body imap-current-message))
558 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
561 (buffer-disable-undo)
563 (let ((head (nnheader-parse-naked-head)))
564 (mail-header-set-number head uid)
565 (mail-header-set-chars head chars)
566 (mail-header-set-lines head lines)
567 (mail-header-set-xref
568 head (format "%s %s:%d" (system-name) mbx uid))
571 (defun nnimap-retrieve-which-headers (articles fetch-old)
572 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
573 (with-current-buffer nnimap-server-buffer
574 (if (numberp (car-safe articles))
577 (imap-range-to-message-set
578 (gnus-compress-sequence
579 (append (gnus-uncompress-sequence
581 (cons (if (numberp fetch-old)
582 (max 1 (- (car articles) fetch-old))
584 (1- (car articles)))))
586 (mapcar (lambda (msgid)
588 (format "HEADER Message-Id \"%s\"" msgid)))
591 (defun nnimap-group-overview-filename (group server)
592 "Make file name for GROUP on SERVER."
593 (let* ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
594 (uidvalidity (gnus-group-get-parameter
595 (gnus-group-prefixed-name
596 group (gnus-server-to-method
597 (format "nnimap:%s" server)))
599 (name (nnheader-translate-file-chars
600 (concat nnimap-nov-file-name
601 (if (equal server "")
603 server) "." group nnimap-nov-file-name-suffix) t))
604 (nameuid (nnheader-translate-file-chars
605 (concat nnimap-nov-file-name
606 (if (equal server "")
608 server) "." group "." uidvalidity
609 nnimap-nov-file-name-suffix) t))
610 (oldfile (if (or nnmail-use-long-file-names
611 (file-exists-p (expand-file-name name dir)))
612 (expand-file-name name dir)
614 (mm-encode-coding-string
615 (nnheader-replace-chars-in-string name ?. ?/)
616 nnmail-pathname-coding-system)
618 (newfile (if (or nnmail-use-long-file-names
619 (file-exists-p (expand-file-name nameuid dir)))
620 (expand-file-name nameuid dir)
622 (mm-encode-coding-string
623 (nnheader-replace-chars-in-string nameuid ?. ?/)
624 nnmail-pathname-coding-system)
626 (when (and (file-exists-p oldfile) (not (file-exists-p newfile)))
627 (message "nnimap: Upgrading novcache filename...")
629 (gnus-make-directory (file-name-directory newfile))
630 (unless (ignore-errors (rename-file oldfile newfile) t)
631 (if (ignore-errors (copy-file oldfile newfile) t)
632 (delete-file oldfile)
633 (error "Can't rename `%s' to `%s'" oldfile newfile))))
636 (defun nnimap-retrieve-headers-from-file (group server)
637 (with-current-buffer nntp-server-buffer
638 (let ((nov (nnimap-group-overview-filename group server)))
639 (when (file-exists-p nov)
640 (mm-insert-file-contents nov)
641 (set-buffer-modified-p nil)
642 (let ((min (ignore-errors (goto-char (point-min))
643 (read (current-buffer))))
644 (max (ignore-errors (goto-char (point-max))
646 (read (current-buffer)))))
647 (if (and (numberp min) (numberp max))
649 ;; junk, remove it, it's saved later
653 (defun nnimap-retrieve-headers-from-server (articles group server)
654 (with-current-buffer nnimap-server-buffer
655 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
656 (nnimap-length (gnus-range-length articles))
658 (imap-fetch (imap-range-to-message-set articles)
659 (concat "(UID RFC822.SIZE BODY "
661 (append '(Subject From Date Message-Id
662 References In-Reply-To Xref)
664 nnmail-extra-headers))))
665 (if (imap-capability 'IMAP4rev1)
666 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
667 (format "RFC822.HEADER.LINES %s)" headers)))))
668 (with-current-buffer nntp-server-buffer
669 (sort-numeric-fields 1 (point-min) (point-max)))
670 (and (numberp nnmail-large-newsgroup)
671 (> nnimap-length nnmail-large-newsgroup)
672 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
674 (defun nnimap-dont-use-nov-p (group server)
675 (or gnus-nov-is-evil nnimap-nov-is-evil
676 (unless (and (gnus-make-directory
678 (nnimap-group-overview-filename group server)))
680 (nnimap-group-overview-filename group server)))
681 (message "nnimap: Nov cache not writable, %s"
682 (nnimap-group-overview-filename group server)))))
684 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
685 (when (nnimap-possibly-change-group group server)
686 (with-current-buffer nntp-server-buffer
688 (if (nnimap-dont-use-nov-p group server)
689 (nnimap-retrieve-headers-from-server
690 (gnus-compress-sequence articles) group server)
691 (let (uids cached low high)
692 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
694 high (car (last uids)))
695 (if (setq cached (nnimap-retrieve-headers-from-file group server))
697 ;; fetch articles with uids before cache block
698 (when (< low (car cached))
699 (goto-char (point-min))
700 (nnimap-retrieve-headers-from-server
701 (cons low (1- (car cached))) group server))
702 ;; fetch articles with uids after cache block
703 (when (> high (cdr cached))
704 (goto-char (point-max))
705 (nnimap-retrieve-headers-from-server
706 (cons (1+ (cdr cached)) high) group server))
707 (when nnimap-prune-cache
708 ;; remove nov's for articles which has expired on server
709 (goto-char (point-min))
710 (dolist (uid (gnus-set-difference articles uids))
711 (when (re-search-forward (format "^%d\t" uid) nil t)
712 (gnus-delete-line)))))
713 ;; nothing cached, fetch whole range from server
714 (nnimap-retrieve-headers-from-server
715 (cons low high) group server))
716 (when (buffer-modified-p)
718 (point-min) (point-max)
719 (nnimap-group-overview-filename group server) nil 'nomesg))
720 (nnheader-nov-delete-outside-range low high))))
723 (defun nnimap-open-connection (server)
724 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
725 nnimap-authenticator nnimap-server-buffer))
726 (nnheader-report 'nnimap "Can't open connection to server %s" server)
727 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
728 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
729 (imap-close nnimap-server-buffer)
730 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
731 (let* ((list (progn (gnus-message 7 "Parsing authinfo file `%s'."
732 nnimap-authinfo-file)
733 (gnus-parse-netrc nnimap-authinfo-file)))
734 (port (if nnimap-server-port
735 (int-to-string nnimap-server-port)
737 (alist (or (gnus-netrc-machine list server port "imap")
738 (gnus-netrc-machine list server port "imaps")
739 (gnus-netrc-machine list
740 (or nnimap-server-address
743 (gnus-netrc-machine list
744 (or nnimap-server-address
747 (user (gnus-netrc-get alist "login"))
748 (passwd (gnus-netrc-get alist "password")))
749 (if (imap-authenticate user passwd nnimap-server-buffer)
751 (push (list server nnimap-server-buffer)
752 nnimap-server-buffer-alist)
753 (nnimap-possibly-change-server server))
754 (imap-close nnimap-server-buffer)
755 (kill-buffer nnimap-server-buffer)
756 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
758 (deffoo nnimap-open-server (server &optional defs)
759 (nnheader-init-server-buffer)
760 (if (nnimap-server-opened server)
762 (unless (assq 'nnimap-server-buffer defs)
763 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
764 ;; translate `nnimap-server-address' to `nnimap-address' in defs
765 ;; for people that configured nnimap with a very old version
766 (unless (assq 'nnimap-address defs)
767 (if (assq 'nnimap-server-address defs)
768 (push (list 'nnimap-address
769 (cadr (assq 'nnimap-server-address defs))) defs)
770 (push (list 'nnimap-address server) defs)))
771 (nnoo-change-server 'nnimap server defs)
772 (or nnimap-server-buffer
773 (setq nnimap-server-buffer (cadr (assq 'nnimap-server-buffer defs))))
774 (with-current-buffer (get-buffer-create nnimap-server-buffer)
775 (nnoo-change-server 'nnimap server defs))
776 (or (and nnimap-server-buffer
777 (imap-opened nnimap-server-buffer)
778 (if (with-current-buffer nnimap-server-buffer
779 (memq imap-state '(auth select examine)))
781 (imap-close nnimap-server-buffer)
782 (nnimap-open-connection server)))
783 (nnimap-open-connection server))))
785 (deffoo nnimap-server-opened (&optional server)
786 "Whether SERVER is opened.
787 If SERVER is the current virtual server, and the connection to the
788 physical server is alive, this function return a non-nil value. If
789 SERVER is nil, it is treated as the current server."
790 ;; clean up autologouts??
791 (and (or server nnimap-current-server)
792 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
793 (imap-opened (nnimap-get-server-buffer server))))
795 (deffoo nnimap-close-server (&optional server)
796 "Close connection to server and free all resources connected to it.
797 Return nil if the server couldn't be closed for some reason."
798 (let ((server (or server nnimap-current-server)))
799 (when (or (nnimap-server-opened server)
800 (imap-opened (nnimap-get-server-buffer server)))
801 (imap-close (nnimap-get-server-buffer server))
802 (kill-buffer (nnimap-get-server-buffer server))
803 (setq nnimap-server-buffer nil
804 nnimap-current-server nil
805 nnimap-server-buffer-alist
806 (delq server nnimap-server-buffer-alist)))
807 (nnoo-close-server 'nnimap server)))
809 (deffoo nnimap-request-close ()
810 "Close connection to all servers and free all resources that the backend have reserved.
811 All buffers that have been created by that
812 backend should be killed. (Not the nntp-server-buffer, though.) This
813 function is generally only called when Gnus is shutting down."
814 (mapcar (lambda (server) (nnimap-close-server (car server)))
815 nnimap-server-buffer-alist)
816 (setq nnimap-server-buffer-alist nil))
818 (deffoo nnimap-status-message (&optional server)
819 "This function returns the last error message from server."
820 (when (nnimap-possibly-change-server server)
821 (nnoo-status-message 'nnimap server)))
823 (defun nnimap-demule (string)
824 ;; BEWARE: we used to use string-as-multibyte here which is braindead
825 ;; because it will turn accidental emacs-mule-valid byte sequences
826 ;; into multibyte chars. --Stef
827 ;; Reverted, braindead got 7.5 out of 10 on imdb, so it can't be
829 (funcall (if (and (fboundp 'string-as-multibyte)
830 (subrp (symbol-function 'string-as-multibyte)))
835 (defun nnimap-make-callback (article gnus-callback buffer)
836 "Return a callback function."
838 (nnimap-callback ,article ,gnus-callback ,buffer)))
840 (defun nnimap-callback (article gnus-callback buffer)
841 (when (eq article (imap-current-message))
842 (remove-hook 'imap-fetch-data-hook
843 (nnimap-make-callback article gnus-callback buffer))
844 (with-current-buffer buffer
846 (with-current-buffer nnimap-server-buffer
848 (if (imap-capability 'IMAP4rev1)
849 ;; xxx don't just use car? alist doesn't contain
850 ;; anything else now, but it might...
851 (nth 2 (car (imap-message-get article 'BODYDETAIL)))
852 (imap-message-get article 'RFC822)))))
853 (nnheader-ms-strip-cr)
854 (funcall gnus-callback t))))
856 (defun nnimap-request-article-part (article part prop &optional
857 group server to-buffer detail)
858 (when (nnimap-possibly-change-group group server)
859 (let ((article (if (stringp article)
860 (car-safe (imap-search
861 (format "HEADER Message-Id \"%s\"" article)
862 nnimap-server-buffer))
865 (gnus-message 10 "nnimap: Fetching (part of) article %d from %s..."
866 article (or group imap-current-mailbox
867 gnus-newsgroup-name))
868 (if (not nnheader-callback-function)
869 (with-current-buffer (or to-buffer nntp-server-buffer)
871 (let ((data (imap-fetch article part prop nil
872 nnimap-server-buffer)))
873 (insert (nnimap-demule (if detail
876 (nnheader-ms-strip-cr)
878 10 "nnimap: Fetching (part of) article %d from %s...done"
879 article (or group imap-current-mailbox gnus-newsgroup-name))
881 (nnheader-report 'nnimap "No such article %d in %s: %s"
882 article (or group imap-current-mailbox
884 (imap-error-text nnimap-server-buffer))
885 (cons group article)))
886 (add-hook 'imap-fetch-data-hook
887 (nnimap-make-callback article
888 nnheader-callback-function
890 (imap-fetch-asynch article part nil nnimap-server-buffer)
891 (cons group article))))))
893 (deffoo nnimap-asynchronous-p ()
896 (deffoo nnimap-request-article (article &optional group server to-buffer)
897 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
898 (nnimap-request-article-part
899 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
900 (nnimap-request-article-part
901 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
903 (deffoo nnimap-request-head (article &optional group server to-buffer)
904 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
905 (nnimap-request-article-part
906 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
907 (nnimap-request-article-part
908 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
910 (deffoo nnimap-request-body (article &optional group server to-buffer)
911 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
912 (nnimap-request-article-part
913 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
914 (nnimap-request-article-part
915 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
917 (deffoo nnimap-request-group (group &optional server fast)
918 (nnimap-request-update-info-internal
920 (gnus-get-info (gnus-group-prefixed-name
921 group (gnus-server-to-method (format "nnimap:%s" server))))
923 (when (nnimap-possibly-change-group group server)
924 (nnimap-before-find-minmax-bugworkaround)
927 ((null (setq info (nnimap-find-minmax-uid group t)))
928 (nnheader-report 'nnimap "Could not get active info for %s"
931 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
932 (max 1 (or (nth 1 info) 1))
933 (or (nth 2 info) 0) group)
934 (nnheader-report 'nnimap "Group %s selected" group)
937 (defun nnimap-update-unseen (group &optional server)
938 "Update the unseen count in `nnimap-mailbox-info'."
940 (gnus-group-prefixed-name group server)
941 (let ((old (gnus-gethash-safe (gnus-group-prefixed-name group server)
942 nnimap-mailbox-info)))
943 (list (nth 0 old) (nth 1 old)
944 (imap-mailbox-status group 'unseen nnimap-server-buffer)
946 nnimap-mailbox-info))
948 (defun nnimap-close-group (group &optional server)
949 (with-current-buffer nnimap-server-buffer
950 (when (and (imap-opened)
951 (nnimap-possibly-change-group group server))
952 (nnimap-update-unseen group server)
953 (case nnimap-expunge-on-close
955 (imap-mailbox-expunge nnimap-close-asynchronous)
956 (unless nnimap-dont-close
957 (imap-mailbox-close nnimap-close-asynchronous))))
958 (ask (if (and (imap-search "DELETED")
959 (gnus-y-or-n-p (format "Expunge articles in group `%s'? "
960 imap-current-mailbox)))
962 (imap-mailbox-expunge nnimap-close-asynchronous)
963 (unless nnimap-dont-close
964 (imap-mailbox-close nnimap-close-asynchronous)))
965 (imap-mailbox-unselect)))
966 (t (imap-mailbox-unselect)))
967 (not imap-current-mailbox))))
969 (defun nnimap-pattern-to-list-arguments (pattern)
971 (cons (car-safe p) (or (cdr-safe p) p)))
972 (if (and (listp pattern)
973 (listp (cdr pattern)))
977 (deffoo nnimap-request-list (&optional server)
978 (when (nnimap-possibly-change-server server)
979 (with-current-buffer nntp-server-buffer
981 (gnus-message 5 "nnimap: Generating active list%s..."
982 (if (> (length server) 0) (concat " for " server) ""))
983 (nnimap-before-find-minmax-bugworkaround)
984 (with-current-buffer nnimap-server-buffer
985 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
986 (dolist (mbx (funcall nnimap-request-list-method
987 (cdr pattern) (car pattern)))
988 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
989 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
991 (with-current-buffer nntp-server-buffer
992 (insert (format "\"%s\" %d %d y\n"
993 mbx (or (nth 2 info) 0)
994 (max 1 (or (nth 1 info) 1)))))))))))
995 (gnus-message 5 "nnimap: Generating active list%s...done"
996 (if (> (length server) 0) (concat " for " server) ""))
999 (deffoo nnimap-request-post (&optional server)
1001 (dolist (mbx (message-unquote-tokens
1002 (message-tokenize-header
1003 (message-fetch-field "Newsgroups") ", ")) success)
1004 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1005 (or (gnus-active to-newsgroup)
1006 (gnus-activate-group to-newsgroup)
1007 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1009 (or (and (gnus-request-create-group
1010 to-newsgroup gnus-command-method)
1011 (gnus-activate-group to-newsgroup nil nil
1012 gnus-command-method))
1013 (error "Couldn't create group %s" to-newsgroup)))
1014 (error "No such group: %s" to-newsgroup))
1015 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
1016 (setq success nil))))))
1018 ;; Optional backend functions
1020 (defun nnimap-string-lessp-numerical (s1 s2)
1021 "Return t if first arg string is less than second in numerical order."
1022 (cond ((string= s1 s2)
1024 ((> (length s1) (length s2))
1026 ((< (length s1) (length s2))
1028 ((< (string-to-number (substring s1 0 1))
1029 (string-to-number (substring s2 0 1)))
1031 ((> (string-to-number (substring s1 0 1))
1032 (string-to-number (substring s2 0 1)))
1035 (nnimap-string-lessp-numerical (substring s1 1) (substring s2 1)))))
1037 (deffoo nnimap-retrieve-groups (groups &optional server)
1038 (when (nnimap-possibly-change-server server)
1039 (gnus-message 5 "nnimap: Checking mailboxes...")
1040 (with-current-buffer nntp-server-buffer
1042 (nnimap-before-find-minmax-bugworkaround)
1043 (let (asyncgroups slowgroups)
1044 (if (null nnimap-retrieve-groups-asynchronous)
1045 (setq slowgroups groups)
1046 (dolist (group groups)
1047 (gnus-message 9 "nnimap: Quickly checking mailbox %s" group)
1048 (add-to-list (if (gnus-gethash-safe
1049 (gnus-group-prefixed-name group server)
1050 nnimap-mailbox-info)
1053 (list group (imap-mailbox-status-asynch
1054 group '(uidvalidity uidnext unseen)
1055 nnimap-server-buffer))))
1056 (dolist (asyncgroup asyncgroups)
1057 (let ((group (nth 0 asyncgroup))
1058 (tag (nth 1 asyncgroup))
1060 (when (imap-ok-p (imap-wait-for-tag tag nnimap-server-buffer))
1061 (if (or (not (string=
1062 (nth 0 (gnus-gethash (gnus-group-prefixed-name
1064 nnimap-mailbox-info))
1065 (imap-mailbox-get 'uidvalidity group
1066 nnimap-server-buffer)))
1068 (nth 1 (gnus-gethash (gnus-group-prefixed-name
1070 nnimap-mailbox-info))
1071 (imap-mailbox-get 'uidnext group
1072 nnimap-server-buffer))))
1073 (push (list group) slowgroups)
1074 (insert (nth 3 (gnus-gethash (gnus-group-prefixed-name
1076 nnimap-mailbox-info))))))))
1077 (dolist (group slowgroups)
1078 (if nnimap-retrieve-groups-asynchronous
1079 (setq group (car group)))
1080 (gnus-message 7 "nnimap: Mailbox %s modified" group)
1081 (imap-mailbox-put 'uidnext nil group nnimap-server-buffer)
1082 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags group
1083 nnimap-server-buffer))
1084 (let* ((info (nnimap-find-minmax-uid group 'examine))
1085 (str (format "\"%s\" %d %d y\n" group
1087 (max 1 (or (nth 1 info) 1)))))
1088 (when (> (or (imap-mailbox-get 'recent group
1089 nnimap-server-buffer) 0)
1091 (push (list (cons group 0)) nnmail-split-history))
1093 (when nnimap-retrieve-groups-asynchronous
1095 (gnus-group-prefixed-name group server)
1096 (list (or (imap-mailbox-get
1097 'uidvalidity group nnimap-server-buffer)
1098 (imap-mailbox-status
1099 group 'uidvalidity nnimap-server-buffer))
1100 (or (imap-mailbox-get
1101 'uidnext group nnimap-server-buffer)
1102 (imap-mailbox-status
1103 group 'uidnext nnimap-server-buffer))
1104 (or (imap-mailbox-get
1105 'unseen group nnimap-server-buffer)
1106 (imap-mailbox-status
1107 group 'unseen nnimap-server-buffer))
1109 nnimap-mailbox-info)))))))
1110 (gnus-message 5 "nnimap: Checking mailboxes...done")
1113 (deffoo nnimap-request-update-info-internal (group info &optional server)
1114 (when (nnimap-possibly-change-group group server)
1115 (when info ;; xxx what does this mean? should we create a info?
1116 (with-current-buffer nnimap-server-buffer
1117 (gnus-message 5 "nnimap: Updating info for %s..."
1118 (gnus-info-group info))
1120 (when (nnimap-mark-permanent-p 'read)
1122 ;; read info could contain articles marked unread by other
1123 ;; imap clients! we correct this
1124 (setq seen (gnus-uncompress-range (gnus-info-read info))
1125 unseen (imap-search "UNSEEN UNDELETED")
1126 seen (gnus-set-difference seen unseen)
1127 ;; seen might lack articles marked as read by other
1128 ;; imap clients! we correct this
1129 seen (append seen (imap-search "SEEN"))
1132 seen (gnus-compress-sequence seen t)
1133 ;; we can't return '(1) since this isn't a "list of ranges",
1134 ;; and we can't return '((1)) since g-list-of-unread-articles
1135 ;; is buggy so we return '((1 . 1)).
1136 seen (if (and (integerp (car seen))
1138 (list (cons (car seen) (car seen)))
1140 (gnus-info-set-read info seen)))
1142 (mapcar (lambda (pred)
1143 (when (or (eq (cdr pred) 'recent)
1144 (and (nnimap-mark-permanent-p (cdr pred))
1145 (member (nnimap-mark-to-flag (cdr pred))
1146 (imap-mailbox-get 'flags))))
1147 (gnus-info-set-marks
1149 (gnus-update-alist-soft
1151 (gnus-compress-sequence
1152 (imap-search (nnimap-mark-to-predicate (cdr pred))))
1153 (gnus-info-marks info))
1155 gnus-article-mark-lists)
1157 (when nnimap-importantize-dormant
1158 ;; nnimap mark dormant article as ticked too (for other clients)
1159 ;; so we remove that mark for gnus since we support dormant
1160 (gnus-info-set-marks
1162 (gnus-update-alist-soft
1164 (gnus-remove-from-range
1165 (cdr-safe (assoc 'tick (gnus-info-marks info)))
1166 (cdr-safe (assoc 'dormant (gnus-info-marks info))))
1167 (gnus-info-marks info))
1170 (gnus-message 5 "nnimap: Updating info for %s...done"
1171 (gnus-info-group info))
1175 (deffoo nnimap-request-type (group &optional article)
1176 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
1180 (deffoo nnimap-request-set-mark (group actions &optional server)
1181 (when (nnimap-possibly-change-group group server)
1182 (with-current-buffer nnimap-server-buffer
1184 (gnus-message 7 "nnimap: Setting marks in %s..." group)
1185 (while (setq action (pop actions))
1186 (let ((range (nth 0 action))
1187 (what (nth 1 action))
1188 (cmdmarks (nth 2 action))
1190 ;; bookmark can't be stored (not list/range
1191 (setq cmdmarks (delq 'bookmark cmdmarks))
1192 ;; killed can't be stored (not list/range
1193 (setq cmdmarks (delq 'killed cmdmarks))
1194 ;; unsent are for nndraft groups only
1195 (setq cmdmarks (delq 'unsent cmdmarks))
1196 ;; cache flags are pointless on the server
1197 (setq cmdmarks (delq 'cache cmdmarks))
1198 ;; seen flags are local to each gnus
1199 (setq cmdmarks (delq 'seen cmdmarks))
1200 ;; recent marks can't be set
1201 (setq cmdmarks (delq 'recent cmdmarks))
1202 (when nnimap-importantize-dormant
1203 ;; flag dormant articles as ticked
1204 (if (memq 'dormant cmdmarks)
1205 (setq cmdmarks (cons 'tick cmdmarks))))
1206 ;; remove stuff we are forbidden to store
1207 (mapcar (lambda (mark)
1208 (if (imap-message-flag-permanent-p
1209 (nnimap-mark-to-flag mark))
1210 (setq marks (cons mark marks))))
1212 (when (and range marks)
1213 (cond ((eq what 'del)
1214 (imap-message-flags-del
1215 (imap-range-to-message-set range)
1216 (nnimap-mark-to-flag marks nil t)))
1218 (imap-message-flags-add
1219 (imap-range-to-message-set range)
1220 (nnimap-mark-to-flag marks nil t)))
1222 (imap-message-flags-set
1223 (imap-range-to-message-set range)
1224 (nnimap-mark-to-flag marks nil t)))))))
1225 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
1228 (defun nnimap-split-fancy ()
1229 "Like the function `nnmail-split-fancy', but uses `nnimap-split-fancy'."
1230 (let ((nnmail-split-fancy nnimap-split-fancy))
1231 (nnmail-split-fancy)))
1233 (defun nnimap-split-to-groups (rules)
1234 ;; tries to match all rules in nnimap-split-rule against content of
1235 ;; nntp-server-buffer, returns a list of groups that matched.
1236 (with-current-buffer nntp-server-buffer
1237 ;; Fold continuation lines.
1238 (goto-char (point-min))
1239 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1240 (replace-match " " t t))
1241 (if (functionp rules)
1243 (let (to-groups regrepp)
1245 (dolist (rule rules to-groups)
1246 (let ((group (car rule))
1247 (regexp (cadr rule)))
1248 (goto-char (point-min))
1249 (when (and (if (stringp regexp)
1251 (if (not (stringp group))
1252 (setq group (eval group))
1254 (string-match "\\\\[0-9&]" group)))
1255 (re-search-forward regexp nil t))
1256 (funcall regexp group))
1257 ;; Don't enter the article into the same group twice.
1258 (not (assoc group to-groups)))
1260 (nnmail-expand-newtext group)
1263 (or nnimap-split-crosspost
1264 (throw 'split-done to-groups))))))))))
1266 (defun nnimap-assoc-match (key alist)
1268 (while (and alist (not element))
1269 (if (string-match (car (car alist)) key)
1270 (setq element (car alist)))
1271 (setq alist (cdr alist)))
1274 (defun nnimap-split-find-rule (server inbox)
1275 (if (and (listp nnimap-split-rule) (listp (car nnimap-split-rule))
1276 (list (cdar nnimap-split-rule)) (listp (cadar nnimap-split-rule)))
1278 (cadr (nnimap-assoc-match inbox (cdr (nnimap-assoc-match
1279 server nnimap-split-rule))))
1282 (defun nnimap-split-find-inbox (server)
1283 (if (listp nnimap-split-inbox)
1285 (list nnimap-split-inbox)))
1287 (defun nnimap-split-articles (&optional group server)
1288 (when (nnimap-possibly-change-server server)
1289 (with-current-buffer nnimap-server-buffer
1290 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
1291 ;; iterate over inboxes
1292 (while (and (setq inbox (pop inboxes))
1293 (nnimap-possibly-change-group inbox)) ;; SELECT
1294 ;; find split rule for this server / inbox
1295 (when (setq rule (nnimap-split-find-rule server inbox))
1296 ;; iterate over articles
1297 (dolist (article (imap-search nnimap-split-predicate))
1298 (when (if (if (eq nnimap-split-download-body 'default)
1299 nnimap-split-download-body-default
1300 nnimap-split-download-body)
1301 (and (nnimap-request-article article)
1302 (with-current-buffer nntp-server-buffer (mail-narrow-to-head)))
1303 (nnimap-request-head article))
1304 ;; copy article to right group(s)
1305 (setq removeorig nil)
1306 (dolist (to-group (nnimap-split-to-groups rule))
1307 (cond ((eq to-group 'junk)
1308 (message "IMAP split removed %s:%s:%d" server inbox
1310 (setq removeorig t))
1311 ((imap-message-copy (number-to-string article)
1312 to-group nil 'nocopyuid)
1313 (message "IMAP split moved %s:%s:%d to %s" server
1314 inbox article to-group)
1316 (when nnmail-cache-accepted-message-ids
1317 (with-current-buffer nntp-server-buffer
1320 (nnmail-fetch-field "message-id"))
1321 (nnmail-cache-insert msgid
1323 (nnmail-fetch-field "subject"))))))
1324 ;; Add the group-art list to the history list.
1325 (push (list (cons to-group 0)) nnmail-split-history))
1327 (message "IMAP split failed to move %s:%s:%d to %s"
1328 server inbox article to-group))))
1329 (if (if (eq nnimap-split-download-body 'default)
1330 nnimap-split-download-body-default
1331 nnimap-split-download-body)
1333 ;; remove article if it was successfully copied somewhere
1335 (imap-message-flags-add (format "%d" article)
1336 "\\Seen \\Deleted")))))
1337 (when (imap-mailbox-select inbox) ;; just in case
1338 ;; todo: UID EXPUNGE (if available) to remove splitted articles
1339 (imap-mailbox-expunge)
1340 (imap-mailbox-close)))
1341 (when nnmail-cache-accepted-message-ids
1342 (nnmail-cache-close))
1345 (deffoo nnimap-request-scan (&optional group server)
1346 (nnimap-split-articles group server))
1348 (deffoo nnimap-request-newgroups (date &optional server)
1349 (when (nnimap-possibly-change-server server)
1350 (with-current-buffer nntp-server-buffer
1351 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
1352 (if (> (length server) 0) " on " "") server)
1354 (nnimap-before-find-minmax-bugworkaround)
1355 (dolist (pattern (nnimap-pattern-to-list-arguments
1356 nnimap-list-pattern))
1357 (dolist (mbx (imap-mailbox-lsub (cdr pattern) (car pattern) nil
1358 nnimap-server-buffer))
1360 (dolist (mailbox (imap-mailbox-get 'list-flags mbx
1361 nnimap-server-buffer))
1362 (if (string= (downcase mailbox) "\\noselect")
1365 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
1367 (insert (format "\"%s\" %d %d y\n"
1368 mbx (or (nth 2 info) 0)
1369 (max 1 (or (nth 1 info) 1)))))))))
1370 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
1371 (if (> (length server) 0) " on " "") server))
1374 (deffoo nnimap-request-create-group (group &optional server args)
1375 (when (nnimap-possibly-change-server server)
1376 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
1377 (imap-mailbox-create group nnimap-server-buffer)
1378 (nnheader-report 'nnimap "%S"
1379 (imap-error-text nnimap-server-buffer)))))
1381 (defun nnimap-time-substract (time1 time2)
1382 "Return TIME for TIME1 - TIME2."
1383 (let* ((ms (- (car time1) (car time2)))
1384 (ls (- (nth 1 time1) (nth 1 time2))))
1386 (list (- ms 1) (+ (expt 2 16) ls))
1389 (eval-when-compile (require 'parse-time))
1390 (defun nnimap-date-days-ago (daysago)
1391 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
1392 (require 'parse-time)
1393 (let* ((time (nnimap-time-substract (current-time) (days-to-time daysago)))
1394 (date (format-time-string
1395 (format "%%d-%s-%%Y"
1396 (capitalize (car (rassoc (nth 4 (decode-time time))
1397 parse-time-months))))
1399 (if (eq ?0 (string-to-char date))
1403 (defun nnimap-request-expire-articles-progress ()
1404 (gnus-message 5 "nnimap: Marking article %d for deletion..."
1405 imap-current-message))
1407 (defun nnimap-expiry-target (arts group server)
1408 (unless (eq nnmail-expiry-target 'delete)
1411 (nnimap-request-article art group server (current-buffer))
1412 ;; hints for optimization in `nnimap-request-accept-article'
1413 (let ((nnimap-current-move-article art)
1414 (nnimap-current-move-group group)
1415 (nnimap-current-move-server server))
1416 (nnmail-expiry-target-group nnmail-expiry-target group))))
1417 ;; It is not clear if `nnmail-expiry-target' somehow cause the
1418 ;; current group to be changed or not, so we make sure here.
1419 (nnimap-possibly-change-group group server)))
1421 ;; Notice that we don't actually delete anything, we just mark them deleted.
1422 (deffoo nnimap-request-expire-articles (articles group &optional server force)
1423 (let ((artseq (gnus-compress-sequence articles)))
1424 (when (and artseq (nnimap-possibly-change-group group server))
1425 (with-current-buffer nnimap-server-buffer
1426 (let ((days (or (and nnmail-expiry-wait-function
1427 (funcall nnmail-expiry-wait-function group))
1428 nnmail-expiry-wait)))
1429 (cond ((or force (eq days 'immediate))
1430 (let ((oldarts (imap-search
1432 (imap-range-to-message-set artseq)))))
1434 (nnimap-expiry-target oldarts group server)
1435 (when (imap-message-flags-add
1436 (imap-range-to-message-set
1437 (gnus-compress-sequence oldarts)) "\\Deleted")
1438 (setq articles (gnus-set-difference
1439 articles oldarts))))))
1441 (let ((oldarts (imap-search
1442 (format nnimap-expunge-search-string
1443 (imap-range-to-message-set artseq)
1444 (nnimap-date-days-ago days))))
1445 (imap-fetch-data-hook
1446 '(nnimap-request-expire-articles-progress)))
1448 (nnimap-expiry-target oldarts group server)
1449 (when (imap-message-flags-add
1450 (imap-range-to-message-set
1451 (gnus-compress-sequence oldarts)) "\\Deleted")
1452 (setq articles (gnus-set-difference
1453 articles oldarts)))))))))))
1454 ;; return articles not deleted
1457 (deffoo nnimap-request-move-article (article group server
1458 accept-form &optional last)
1459 (when (nnimap-possibly-change-server server)
1461 (let ((buf (get-buffer-create " *nnimap move*"))
1462 (nnimap-current-move-article article)
1463 (nnimap-current-move-group group)
1464 (nnimap-current-move-server nnimap-current-server)
1466 (and (nnimap-request-article article group server)
1469 (buffer-disable-undo (current-buffer))
1470 (insert-buffer-substring nntp-server-buffer)
1471 (setq result (eval accept-form))
1474 (imap-message-flags-add
1475 (imap-range-to-message-set (list article))
1476 "\\Deleted" 'silent nnimap-server-buffer))
1479 (deffoo nnimap-request-accept-article (group &optional server last)
1480 (when (nnimap-possibly-change-server server)
1483 (if (string= nnimap-current-server nnimap-current-move-server)
1484 ;; moving article within same server, speed it up...
1485 (and (nnimap-possibly-change-group
1486 nnimap-current-move-group)
1487 (imap-message-copy (number-to-string
1488 nnimap-current-move-article)
1489 group 'dontcreate nil
1490 nnimap-server-buffer))
1491 (with-current-buffer (current-buffer)
1492 (goto-char (point-min))
1493 ;; remove any 'From blabla' lines, some IMAP servers
1494 ;; reject the entire message otherwise.
1495 (when (looking-at "^From[^:]")
1496 (delete-region (point) (progn (forward-line) (point))))
1497 ;; turn into rfc822 format (\r\n eol's)
1498 (while (search-forward "\n" nil t)
1499 (replace-match "\r\n"))
1500 (when nnmail-cache-accepted-message-ids
1501 (nnmail-cache-insert (nnmail-fetch-field "message-id")
1503 (nnmail-fetch-field "subject"))))
1504 (when (and last nnmail-cache-accepted-message-ids)
1505 (nnmail-cache-close))
1506 ;; this 'or' is for Cyrus server bug
1507 (or (null (imap-current-mailbox nnimap-server-buffer))
1508 (imap-mailbox-unselect nnimap-server-buffer))
1509 (imap-message-append group (current-buffer) nil nil
1510 nnimap-server-buffer)))
1511 (cons group (nth 1 uid))
1512 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1514 (deffoo nnimap-request-delete-group (group force &optional server)
1515 (when (nnimap-possibly-change-server server)
1516 (with-current-buffer nnimap-server-buffer
1518 (or (null (imap-mailbox-status group 'uidvalidity))
1519 (imap-mailbox-delete group))
1523 (deffoo nnimap-request-rename-group (group new-name &optional server)
1524 (when (nnimap-possibly-change-server server)
1525 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1527 (defun nnimap-expunge (mailbox server)
1528 (when (nnimap-possibly-change-group mailbox server)
1529 (imap-mailbox-expunge nil nnimap-server-buffer)))
1531 (defun nnimap-acl-get (mailbox server)
1532 (when (nnimap-possibly-change-server server)
1533 (and (imap-capability 'ACL nnimap-server-buffer)
1534 (imap-mailbox-acl-get mailbox nnimap-server-buffer))))
1536 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1537 (when (nnimap-possibly-change-server (cadr method))
1538 (unless (imap-capability 'ACL nnimap-server-buffer)
1539 (error "Your server does not support ACL editing"))
1540 (with-current-buffer nnimap-server-buffer
1541 ;; delete all removed identifiers
1542 (mapcar (lambda (old-acl)
1543 (unless (assoc (car old-acl) new-acls)
1544 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1545 (error "Can't delete ACL for %s" (car old-acl)))))
1547 ;; set all changed acl's
1548 (mapcar (lambda (new-acl)
1549 (let ((new-rights (cdr new-acl))
1550 (old-rights (cdr (assoc (car new-acl) old-acls))))
1551 (unless (and old-rights new-rights
1552 (string= old-rights new-rights))
1553 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1554 (error "Can't set ACL for %s to %s" (car new-acl)
1560 ;;; Internal functions
1563 ;; This is confusing.
1565 ;; mark => read, tick, draft, reply etc
1566 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1567 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1569 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1570 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1573 (defconst nnimap-mark-to-predicate-alist
1575 (lambda (pair) ; cdr is the mark
1576 (or (assoc (cdr pair)
1581 (reply . "ANSWERED")))
1583 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1584 (cons '(read . read) gnus-article-mark-lists)))
1586 (defun nnimap-mark-to-predicate (pred)
1587 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1588 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1589 to be used within a IMAP SEARCH query."
1590 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1592 (defconst nnimap-mark-to-flag-alist
1595 (or (assoc (cdr pair)
1597 (tick . "\\Flagged")
1599 (recent . "\\Recent")
1600 (reply . "\\Answered")))
1602 (format "gnus-%s" (symbol-name (cdr pair))))))
1603 (cons '(read . read) gnus-article-mark-lists)))
1605 (defun nnimap-mark-to-flag-1 (preds)
1606 (if (and (not (null preds)) (listp preds))
1607 (cons (nnimap-mark-to-flag (car preds))
1608 (nnimap-mark-to-flag (cdr preds)))
1609 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1611 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1612 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1613 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1614 be used in a STORE FLAGS command."
1615 (let ((result (nnimap-mark-to-flag-1 preds)))
1616 (setq result (if (and (or make-string always-list)
1617 (not (listp result)))
1621 (mapconcat (lambda (flag)
1623 (mapconcat 'identity flag " ")
1628 (defun nnimap-mark-permanent-p (mark &optional group)
1629 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1630 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1634 (buffer-disable-undo (get-buffer-create nnimap-debug-buffer))
1635 (mapcar (lambda (f) (trace-function-background f nnimap-debug-buffer))
1637 nnimap-possibly-change-server
1638 nnimap-verify-uidvalidity
1639 nnimap-find-minmax-uid
1640 nnimap-before-find-minmax-bugworkaround
1641 nnimap-possibly-change-group
1642 ;;nnimap-replace-whitespace
1643 nnimap-retrieve-headers-progress
1644 nnimap-retrieve-which-headers
1645 nnimap-group-overview-filename
1646 nnimap-retrieve-headers-from-file
1647 nnimap-retrieve-headers-from-server
1648 nnimap-retrieve-headers
1649 nnimap-open-connection
1651 nnimap-server-opened
1653 nnimap-request-close
1654 nnimap-status-message
1656 nnimap-request-article-part
1657 nnimap-request-article
1660 nnimap-request-group
1662 nnimap-pattern-to-list-arguments
1665 nnimap-retrieve-groups
1666 nnimap-request-update-info-internal
1668 nnimap-request-set-mark
1669 nnimap-split-to-groups
1670 nnimap-split-find-rule
1671 nnimap-split-find-inbox
1672 nnimap-split-articles
1674 nnimap-request-newgroups
1675 nnimap-request-create-group
1676 nnimap-time-substract
1677 nnimap-date-days-ago
1678 nnimap-request-expire-articles-progress
1679 nnimap-request-expire-articles
1680 nnimap-request-move-article
1681 nnimap-request-accept-article
1682 nnimap-request-delete-group
1683 nnimap-request-rename-group
1684 gnus-group-nnimap-expunge
1685 gnus-group-nnimap-edit-acl
1686 gnus-group-nnimap-edit-acl-done
1687 nnimap-group-mode-hook
1688 nnimap-mark-to-predicate
1689 nnimap-mark-to-flag-1
1691 nnimap-mark-permanent-p
1696 ;; arch-tag: 2b001f20-3ff9-4094-a0ad-46807c1ba70b
1697 ;;; nnimap.el ends here