1 ;;; nnimap.el --- imap backend for Gnus
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Jim Radford <radford@robby.caltech.edu>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
72 (eval-when-compile (require 'cl))
74 (autoload 'auth-source-user-or-password "auth-source")
78 (defconst nnimap-version "nnimap 1.0")
81 "Reading IMAP mail with Gnus."
84 (defvoo nnimap-address nil
85 "Address of physical IMAP server. If nil, use the virtual server's name.")
87 (defvoo nnimap-server-port nil
88 "Port number on physical IMAP server.
89 If nil, defaults to 993 for TLS/SSL connections and 143 otherwise.")
91 ;; Splitting variables
93 (defcustom nnimap-split-crosspost t
94 "If non-nil, do crossposting if several split methods match the mail.
95 If nil, the first match found will be used."
99 (defcustom nnimap-split-inbox nil
100 "Name of mailbox to split mail from.
102 Mail is read from this mailbox and split according to rules in
105 This can be a string or a list of strings."
107 :type '(choice (string)
110 (define-widget 'nnimap-strict-function 'function
111 "This widget only matches values that are functionp.
113 Warning: This means that a value that is the symbol of a not yet
114 loaded function will not match. Use with care."
115 :match 'nnimap-strict-function-match)
117 (defun nnimap-strict-function-match (widget value)
118 "Ignoring WIDGET, match if VALUE is a function."
121 (defcustom nnimap-split-rule nil
122 "Mail will be split according to these rules.
124 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
126 If you'd like, for instance, one mail group for mail from the
127 \"gnus-imap\" mailing list, one group for junk mail and leave
128 everything else in the incoming mailbox, you could do something like
131 \(setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
132 (\"INBOX.junk\" \"Subject:.*buy\")))
134 As you can see, `nnimap-split-rule' is a list of lists, where the
135 first element in each \"rule\" is the name of the IMAP mailbox (or the
136 symbol `junk' if you want to remove the mail), and the second is a
137 regexp that nnimap will try to match on the header to find a fit.
139 The second element can also be a function. In that case, it will be
140 called narrowed to the headers with the first element of the rule as
141 the argument. It should return a non-nil value if it thinks that the
142 mail belongs in that group.
144 This variable can also have a function as its value, the function will
145 be called with the headers narrowed and should return a group where it
146 thinks the article should be splitted to. See `nnimap-split-fancy'.
148 To allow for different split rules on different virtual servers, and
149 even different split rules in different inboxes on the same server,
150 the syntax of this variable have been extended along the lines of:
152 \(setq nnimap-split-rule
153 '((\"my1server\" (\".*\" ((\"ding\" \"ding@gnus.org\")
154 (\"junk\" \"From:.*Simon\")))
155 (\"my2server\" (\"INBOX\" nnimap-split-fancy))
156 (\"my[34]server\" (\".*\" ((\"private\" \"To:.*Simon\")
157 (\"junk\" my-junk-func)))))
159 The virtual server name is in fact a regexp, so that the same rules
160 may apply to several servers. In the example, the servers
161 \"my3server\" and \"my4server\" both use the same rules. Similarly,
162 the inbox string is also a regexp. The actual splitting rules are as
163 before, either a function, or a list with group/regexp or
164 group/function elements."
166 ;; FIXME: Doesn't allow `("my2server" ("INBOX" nnimap-split-fancy))'
167 ;; per example above. -- fx
168 :type '(choice :tag "Rule type"
169 (repeat :menu-tag "Single-server"
170 :tag "Single-server list"
171 (list (string :tag "Mailbox")
172 (choice :tag "Predicate"
173 (regexp :tag "A regexp")
174 (nnimap-strict-function :tag "A function"))))
175 (choice :menu-tag "A function"
177 (function-item nnimap-split-fancy)
178 (function-item nnmail-split-fancy)
179 (nnimap-strict-function :tag "User-defined function"))
180 (repeat :menu-tag "Multi-server (extended)"
181 :tag "Multi-server list"
182 (list (regexp :tag "Server regexp")
183 (list (regexp :tag "Incoming Mailbox regexp")
184 (repeat :tag "Rules for matching server(s) and mailbox(es)"
185 (list (string :tag "Destination mailbox")
186 (choice :tag "Predicate"
187 (regexp :tag "A Regexp")
188 (nnimap-strict-function :tag "A Function")))))))))
190 (defcustom nnimap-split-predicate "UNSEEN UNDELETED"
191 "The predicate used to find articles to split.
192 If you use another IMAP client to peek on articles but always would
193 like nnimap to split them once it's started, you could change this to
194 \"UNDELETED\". Other available predicates are available in
195 RFC2060 section 6.4.4."
199 (defcustom nnimap-split-fancy nil
200 "Like the variable `nnmail-split-fancy'."
204 (defvar nnimap-split-download-body-default nil
205 "Internal variable with default value for `nnimap-split-download-body'.")
207 (defcustom nnimap-split-download-body 'default
208 "Whether to download entire articles during splitting.
209 This is generally not required, and will slow things down considerably.
210 You may need it if you want to use an advanced splitting function that
211 analyzes the body before splitting the article.
212 If this variable is nil, bodies will not be downloaded; if this
213 variable is the symbol `default' the default behavior is
214 used (which currently is nil, unless you use a statistical
215 spam.el test); if this variable is another non-nil value bodies
219 :type '(choice (const :tag "Let system decide" deault)
222 ;; Performance / bug workaround variables
224 (defcustom nnimap-close-asynchronous t
225 "Close mailboxes asynchronously in `nnimap-close-group'.
226 This means that errors caught by nnimap when closing the mailbox will
227 not prevent Gnus from updating the group status, which may be harmful.
228 However, it increases speed."
233 (defcustom nnimap-dont-close t
234 "Never close mailboxes.
235 This increases the speed of closing mailboxes (quiting group) but may
236 decrease the speed of selecting another mailbox later. Re-selecting
237 the same mailbox will be faster though."
242 (defcustom nnimap-retrieve-groups-asynchronous t
243 "Send asynchronous STATUS commands for each mailbox before checking mail.
244 If you have mailboxes that rarely receives mail, this speeds up new
245 mail checking. It works by first sending STATUS commands for each
246 mailbox, and then only checking groups which has a modified UIDNEXT
247 more carefully for new mail.
249 In summary, the default is O((1-p)*k+p*n) and changing it to nil makes
250 it O(n). If p is small, then the default is probably faster."
255 (defvoo nnimap-need-unselect-to-notice-new-mail t
256 "Unselect mailboxes before looking for new mail in them.
257 Some servers seem to need this under some circumstances.")
259 (defvoo nnimap-logout-timeout nil
260 "Close server immediately if it can't logout in this number of seconds.
261 If it is nil, never close server until logout completes. This variable
262 overrides `imap-logout-timeout' on a per-server basis.")
264 ;; Authorization / Privacy variables
266 (defvoo nnimap-auth-method nil
269 (defvoo nnimap-stream nil
270 "How nnimap will connect to the server.
272 The default, nil, will try to use the \"best\" method the server can
277 1) you want to connect with TLS/SSL. The TLS/SSL integration
278 with IMAP is suboptimal so you'll have to tell it
281 2) your server is more capable than your environment -- i.e. your
282 server accept Kerberos login's but you haven't installed the
283 `imtest' program or your machine isn't configured for Kerberos.
285 Possible choices: gssapi, kerberos4, starttls, tls, ssl, network, shell.
286 See also `imap-streams' and `imap-stream-alist'.")
288 (defvoo nnimap-authenticator nil
289 "How nnimap authenticate itself to the server.
291 The default, nil, will try to use the \"best\" method the server can
294 There is only one reason for fiddling with this variable, and that is
295 if your server is more capable than your environment -- i.e. you
296 connect to a server that accept Kerberos login's but you haven't
297 installed the `imtest' program or your machine isn't configured for
300 Possible choices: gssapi, kerberos4, digest-md5, cram-md5, login, anonymous.
301 See also `imap-authenticators' and `imap-authenticator-alist'")
303 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
304 "Directory to keep NOV cache files for nnimap groups.
305 See also `nnimap-nov-file-name'.")
307 (defvoo nnimap-nov-file-name "nnimap."
308 "NOV cache base filename.
309 The group name and `nnimap-nov-file-name-suffix' will be appended. A
310 typical complete file name would be
311 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
312 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
313 `nnmail-use-long-file-names' is nil")
315 (defvoo nnimap-nov-file-name-suffix ".novcache"
316 "Suffix for NOV cache base filename.")
318 (defvoo nnimap-nov-is-evil gnus-agent
319 "If non-nil, never generate or use a local nov database for this backend.
320 Using nov databases should speed up header fetching considerably.
321 However, it will invoke a UID SEARCH UID command on the server, and
322 some servers implement this command inefficiently by opening each and
323 every message in the group, thus making it quite slow.
324 Unlike other backends, you do not need to take special care if you
325 flip this variable.")
327 (defvoo nnimap-search-uids-not-since-is-evil nil
328 "If non-nil, avoid \"UID SEARCH UID ... NOT SINCE\" queries when expiring.
329 Instead, use \"UID SEARCH SINCE\" to prune the list of expirable
330 articles within Gnus. This seems to be faster on Courier in some cases.")
332 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
333 "Whether to expunge a group when it is closed.
334 When a IMAP group with articles marked for deletion is closed, this
335 variable determine if nnimap should actually remove the articles or
338 If always, nnimap always perform a expunge when closing the group.
339 If never, nnimap never expunges articles marked for deletion.
340 If ask, nnimap will ask you if you wish to expunge marked articles.
342 When setting this variable to `never', you can only expunge articles
343 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
345 (defvoo nnimap-list-pattern "*"
346 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
347 See below for available wildcards.
349 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
350 REFERENCE will be passed as the first parameter to LIST/LSUB. The
351 semantics of this are server specific, on the University of Washington
352 server you can specify a directory.
355 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
357 There are two wildcards * and %. * matches everything, % matches
358 everything in the current hierarchy.")
360 (defvoo nnimap-news-groups nil
361 "IMAP support a news-like mode, also known as bulletin board mode,
362 where replies is sent via IMAP instead of SMTP.
364 This variable should contain a regexp matching groups where you wish
365 replies to be stored to the mailbox directly.
368 '(\"^[^I][^N][^B][^O][^X].*$\")
370 This will match all groups not beginning with \"INBOX\".
372 Note that there is nothing technically different between mail-like and
373 news-like mailboxes. If you wish to have a group with todo items or
374 similar which you wouldn't want to set up a mailing list for, you can
375 use this to make replies go directly to the group.")
377 (defvoo nnimap-expunge-search-string "UID %s NOT SINCE %s"
378 "IMAP search command to use for articles that are to be expired.
379 The first %s is replaced by a UID set of articles to search on,
380 and the second %s is replaced by a date criterium.
382 One useful (and perhaps the only useful) value to change this to would
383 be `UID %s NOT SENTSINCE %s' to make nnimap use the Date: header
384 instead of the internal date of messages. See section 6.4.4 of RFC
385 2060 for more information on valid strings.
387 However, if `nnimap-search-uids-not-since-is-evil' is true, this
388 variable has no effect since the search logic is reversed.")
390 (defvoo nnimap-importantize-dormant t
391 "If non-nil, mark \"dormant\" articles as \"ticked\" for other IMAP clients.
392 Note that within Gnus, dormant articles will still (only) be
393 marked as ticked. This is to make \"dormant\" articles stand out,
394 just like \"ticked\" articles, in other IMAP clients.")
396 (defvoo nnimap-server-address nil
397 "Obsolete. Use `nnimap-address'.")
399 (defcustom nnimap-authinfo-file "~/.authinfo"
400 "Authorization information for IMAP servers. In .netrc format."
403 (repeat :tag "Entries"
406 :value ("" ("login" . "") ("password" . ""))
410 (const :format "" "login")
411 (string :format "Login: %v"))
413 (const :format "" "password")
414 (string :format "Password: %v"))))))
417 (defcustom nnimap-prune-cache t
418 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
422 (defvar nnimap-request-list-method 'imap-mailbox-list
423 "Method to use to request a list of all folders from the server.
424 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
425 restrict visible folders.")
427 (defcustom nnimap-id nil
428 "Plist with client identity to send to server upon login.
429 A nil value means no information is sent, symbol `no' to disable ID query
430 altogether, or plist with identifier-value pairs to send to
431 server. RFC 2971 describes the list as follows:
433 Any string may be sent as a field, but the following are defined to
434 describe certain values that might be sent. Implementations are free
435 to send none, any, or all of these. Strings are not case-sensitive.
436 Field strings MUST NOT be longer than 30 octets. Value strings MUST
437 NOT be longer than 1024 octets. Implementations MUST NOT send more
438 than 30 field-value pairs.
440 name Name of the program
441 version Version number of the program
442 os Name of the operating system
443 os-version Version of the operating system
444 vendor Vendor of the client/server
445 support-url URL to contact for support
446 address Postal address of contact/vendor
447 date Date program was released, specified as a date-time
449 command Command used to start the program
450 arguments Arguments supplied on the command line, if any
452 environment Description of environment, i.e., UNIX environment
453 variables or Windows registry settings
455 Implementations MUST NOT send the same field name more than once.
457 An example plist would be '(\"name\" \"Gnus\" \"version\" gnus-version-number
458 \"os\" system-configuration \"vendor\" \"GNU\")."
460 :type '(choice (const :tag "No information" nil)
461 (const :tag "Disable ID query" no)
462 (plist :key-type string :value-type string)))
464 (defcustom nnimap-debug nil
465 "If non-nil, trace nnimap- functions into `nnimap-debug-buffer'.
466 Uses `trace-function-background', so you can turn it off with,
469 Note that username, passwords and other privacy sensitive
470 information (such as e-mail) may be stored in the buffer.
471 It is not written to disk, however. Do not enable this
472 variable unless you are comfortable with that.
474 This variable only takes effect when loading the `nnimap' library.
475 See also `nnimap-log'."
479 ;; Internal variables:
481 (defvar nnimap-debug-buffer "*nnimap-debug*")
482 (defvar nnimap-mailbox-info (gnus-make-hashtable 997))
483 (defvar nnimap-current-move-server nil)
484 (defvar nnimap-current-move-group nil)
485 (defvar nnimap-current-move-article nil)
486 (defvar nnimap-length)
487 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
488 (defvar nnimap-progress-how-often 20)
489 (defvar nnimap-counter)
490 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
491 (defvar nnimap-current-server nil) ;; Current server
492 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
496 (nnoo-define-basics nnimap)
498 ;; Utility functions:
500 (defsubst nnimap-decode-group-name (group)
502 (gnus-group-decoded-name group)))
504 (defsubst nnimap-encode-group-name (group)
506 (mm-encode-coding-string group (gnus-group-name-charset nil group))))
508 (defsubst nnimap-get-server-buffer (server)
509 "Return buffer for SERVER, if nil use current server."
510 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
512 (defun nnimap-remove-server-from-buffer-alist (server list)
513 "Remove SERVER from LIST."
516 (unless (equal server (car-safe e))
520 (defun nnimap-possibly-change-server (server)
521 "Return buffer for SERVER, changing the current server as a side-effect.
522 If SERVER is nil, uses the current server."
523 (setq nnimap-current-server (or server nnimap-current-server)
524 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
526 (defun nnimap-verify-uidvalidity (group server)
527 "Verify stored uidvalidity match current one in GROUP on SERVER."
528 (let* ((gnusgroup (gnus-group-prefixed-name
529 group (gnus-server-to-method
530 (format "nnimap:%s" server))))
531 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
532 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity))
533 (dir (file-name-as-directory (expand-file-name nnimap-directory)))
534 (nameuid (nnheader-translate-file-chars
535 (concat nnimap-nov-file-name
536 (if (equal server "")
538 server) "." group "." old-uidvalidity
539 nnimap-nov-file-name-suffix) t))
540 (file (if (or nnmail-use-long-file-names
541 (file-exists-p (expand-file-name nameuid dir)))
542 (expand-file-name nameuid dir)
544 (mm-encode-coding-string
545 (nnheader-replace-chars-in-string nameuid ?. ?/)
546 nnmail-pathname-coding-system)
549 (if (not (equal old-uidvalidity new-uidvalidity))
551 (gnus-delete-file file)
552 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
554 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
557 (defun nnimap-before-find-minmax-bugworkaround ()
558 "Function called before iterating through mailboxes with
559 `nnimap-find-minmax-uid'."
560 (when nnimap-need-unselect-to-notice-new-mail
561 ;; XXX this is for UoW imapd problem, it doesn't notice new mail in
562 ;; currently selected mailbox without a re-select/examine.
563 (or (null (imap-current-mailbox nnimap-server-buffer))
564 (imap-mailbox-unselect nnimap-server-buffer))))
566 (defun nnimap-find-minmax-uid (group &optional examine)
567 "Find lowest and highest active article number in GROUP.
568 If EXAMINE is non-nil the group is selected read-only."
569 (with-current-buffer nnimap-server-buffer
570 (let ((decoded-group (nnimap-decode-group-name group)))
571 (when (or (string= decoded-group (imap-current-mailbox))
572 (imap-mailbox-select decoded-group examine))
574 (when (> (imap-mailbox-get 'exists) 0)
575 (imap-fetch-safe '("1,*" . "1,*:*") "UID" nil 'nouidfetch)
576 (imap-message-map (lambda (uid Uid)
577 (setq minuid (if minuid (min minuid uid) uid)
578 maxuid (if maxuid (max maxuid uid) uid)))
580 (list (imap-mailbox-get 'exists) minuid maxuid))))))
582 (defun nnimap-possibly-change-group (group &optional server)
583 "Make GROUP the current group, and SERVER the current server."
584 (when (nnimap-possibly-change-server server)
585 (let ((decoded-group (nnimap-decode-group-name group)))
586 (with-current-buffer nnimap-server-buffer
587 (if (or (null group) (imap-current-mailbox-p decoded-group))
588 imap-current-mailbox ; Note: utf-7 encoded.
589 (if (imap-mailbox-select decoded-group)
590 (if (or (nnimap-verify-uidvalidity
591 group (or server nnimap-current-server))
592 (zerop (imap-mailbox-get 'exists decoded-group))
593 t ;; for OGnus to see if ignoring uidvalidity
594 ;; changes has any bad effects.
597 "nnimap: Group %s is not uidvalid. Continue? "
599 imap-current-mailbox ; Note: utf-7 encoded.
600 (imap-mailbox-unselect)
601 (error "nnimap: Group %s is not uid-valid" decoded-group))
602 (nnheader-report 'nnimap (imap-error-text))))))))
604 (defun nnimap-replace-whitespace (string)
605 "Return STRING with all whitespace replaced with space."
607 (while (string-match "[\r\n\t]+" string)
608 (setq string (replace-match " " t t string)))
611 ;; Required backend functions
613 (defun nnimap-retrieve-headers-progress ()
614 "Hook to insert NOV line for current article into `nntp-server-buffer'."
615 (and (numberp nnmail-large-newsgroup)
616 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
617 (> nnimap-length nnmail-large-newsgroup)
618 (nnheader-message 6 "nnimap: Retrieving headers... %c"
619 (nth (/ (% nnimap-counter
620 (* (length nnimap-progress-chars)
621 nnimap-progress-how-often))
622 nnimap-progress-how-often)
623 nnimap-progress-chars)))
624 (with-current-buffer nntp-server-buffer
625 (let (headers lines chars uid mbx)
626 (with-current-buffer nnimap-server-buffer
627 (setq uid imap-current-message
628 mbx (nnimap-encode-group-name (imap-current-mailbox))
629 headers (if (imap-capability 'IMAP4rev1)
630 ;; xxx don't just use car? alist doesn't contain
631 ;; anything else now, but it might...
632 (nth 2 (car (imap-message-get uid 'BODYDETAIL)))
633 (imap-message-get uid 'RFC822.HEADER))
634 lines (imap-body-lines (imap-message-body imap-current-message))
635 chars (imap-message-get imap-current-message 'RFC822.SIZE)))
637 ;; At this stage, we only have bytes, so let's use unibyte buffers
638 ;; to make it more clear.
639 (mm-with-unibyte-buffer
640 (buffer-disable-undo)
641 ;; headers can be nil if article is write-only
642 (when headers (insert headers))
643 (let ((head (nnheader-parse-naked-head uid)))
644 (mail-header-set-number head uid)
645 (mail-header-set-chars head chars)
646 (mail-header-set-lines head lines)
647 (mail-header-set-xref
648 head (format "%s %s:%d" (system-name) mbx uid))
651 (defun nnimap-retrieve-which-headers (articles fetch-old)
652 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
653 (with-current-buffer nnimap-server-buffer
654 (if (numberp (car-safe articles))
657 (imap-range-to-message-set
658 (gnus-compress-sequence
659 (append (gnus-uncompress-sequence
661 (cons (if (numberp fetch-old)
662 (max 1 (- (car articles) fetch-old))
664 (1- (car articles)))))
666 (mapcar (lambda (msgid)
668 (format "HEADER Message-Id \"%s\"" msgid)))
671 (defun nnimap-group-overview-filename (group server)
672 "Make file name for GROUP on SERVER."
673 (let* ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
674 (uidvalidity (gnus-group-get-parameter
675 (gnus-group-prefixed-name
676 group (gnus-server-to-method
677 (format "nnimap:%s" server)))
679 (name (nnheader-translate-file-chars
680 (concat nnimap-nov-file-name
681 (if (equal server "")
683 server) "." group nnimap-nov-file-name-suffix) t))
684 (nameuid (nnheader-translate-file-chars
685 (concat nnimap-nov-file-name
686 (if (equal server "")
688 server) "." group "." uidvalidity
689 nnimap-nov-file-name-suffix) t))
690 (oldfile (if (or nnmail-use-long-file-names
691 (file-exists-p (expand-file-name name dir)))
692 (expand-file-name name dir)
694 (mm-encode-coding-string
695 (nnheader-replace-chars-in-string name ?. ?/)
696 nnmail-pathname-coding-system)
698 (newfile (if (or nnmail-use-long-file-names
699 (file-exists-p (expand-file-name nameuid dir)))
700 (expand-file-name nameuid dir)
702 (mm-encode-coding-string
703 (nnheader-replace-chars-in-string nameuid ?. ?/)
704 nnmail-pathname-coding-system)
706 (when (and (file-exists-p oldfile) (not (file-exists-p newfile)))
707 (message "nnimap: Upgrading novcache filename...")
709 (gnus-make-directory (file-name-directory newfile))
710 (unless (ignore-errors (rename-file oldfile newfile) t)
711 (if (ignore-errors (copy-file oldfile newfile) t)
712 (delete-file oldfile)
713 (error "Can't rename `%s' to `%s'" oldfile newfile))))
716 (defun nnimap-retrieve-headers-from-file (group server)
717 (with-current-buffer nntp-server-buffer
718 (let ((nov (nnimap-group-overview-filename group server)))
719 (when (file-exists-p nov)
720 (mm-insert-file-contents nov)
721 (set-buffer-modified-p nil)
722 (let ((min (ignore-errors (goto-char (point-min))
723 (read (current-buffer))))
724 (max (ignore-errors (goto-char (point-max))
726 (read (current-buffer)))))
727 (if (and (numberp min) (numberp max))
729 ;; junk, remove it, it's saved later
733 (defun nnimap-retrieve-headers-from-server (articles group server)
734 (with-current-buffer nnimap-server-buffer
735 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
736 (nnimap-length (gnus-range-length articles))
738 (imap-fetch (imap-range-to-message-set articles)
739 (concat "(UID RFC822.SIZE BODY "
741 (append '(Subject From Date Message-Id
742 References In-Reply-To Xref)
744 nnmail-extra-headers))))
745 (if (imap-capability 'IMAP4rev1)
746 (format "BODY.PEEK[HEADER.FIELDS %s])" headers)
747 (format "RFC822.HEADER.LINES %s)" headers)))))
748 (with-current-buffer nntp-server-buffer
749 (sort-numeric-fields 1 (point-min) (point-max)))
750 (and (numberp nnmail-large-newsgroup)
751 (> nnimap-length nnmail-large-newsgroup)
752 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
754 (defun nnimap-dont-use-nov-p (group server)
755 (or gnus-nov-is-evil nnimap-nov-is-evil
756 (unless (and (gnus-make-directory
758 (nnimap-group-overview-filename group server)))
760 (nnimap-group-overview-filename group server)))
761 (message "nnimap: Nov cache not writable, %s"
762 (nnimap-group-overview-filename group server)))))
764 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
765 (when (nnimap-possibly-change-group group server)
766 (with-current-buffer nntp-server-buffer
768 (if (nnimap-dont-use-nov-p group server)
769 (nnimap-retrieve-headers-from-server
770 (gnus-compress-sequence articles) group server)
771 (let (uids cached low high)
772 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
774 high (car (last uids)))
775 (if (setq cached (nnimap-retrieve-headers-from-file group server))
777 ;; fetch articles with uids before cache block
778 (when (< low (car cached))
779 (goto-char (point-min))
780 (nnimap-retrieve-headers-from-server
781 (cons low (1- (car cached))) group server))
782 ;; fetch articles with uids after cache block
783 (when (> high (cdr cached))
784 (goto-char (point-max))
785 (nnimap-retrieve-headers-from-server
786 (cons (1+ (cdr cached)) high) group server))
787 (when nnimap-prune-cache
788 ;; remove nov's for articles which has expired on server
789 (goto-char (point-min))
790 (dolist (uid (gnus-set-difference articles uids))
791 (when (re-search-forward (format "^%d\t" uid) nil t)
792 (gnus-delete-line)))))
793 ;; nothing cached, fetch whole range from server
794 (nnimap-retrieve-headers-from-server
795 (cons low high) group server))
796 (when (buffer-modified-p)
798 (point-min) (point-max)
799 (nnimap-group-overview-filename group server) nil 'nomesg))
800 (nnheader-nov-delete-outside-range low high))))
803 (defun nnimap-open-connection (server)
804 ;; Note: `nnimap-open-server' that calls this function binds
805 ;; `imap-logout-timeout' to `nnimap-logout-timeout'.
806 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
807 nnimap-authenticator nnimap-server-buffer))
808 (nnheader-report 'nnimap "Can't open connection to server %s" server)
809 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
810 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
811 (imap-close nnimap-server-buffer)
812 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
813 (let* ((list (progn (gnus-message 7 "Parsing authinfo file `%s'."
814 nnimap-authinfo-file)
815 (netrc-parse nnimap-authinfo-file)))
816 (port (if nnimap-server-port
817 (int-to-string nnimap-server-port)
820 (auth-source-user-or-password '("login" "password") server port))
821 (auth-user (nth 0 auth-info))
822 (auth-passwd (nth 1 auth-info))
824 auth-user ; this is preferred to netrc-*
825 (netrc-machine-user-or-password
829 (or nnimap-server-address
832 (list "imap" "imaps" "143" "993"))))
834 auth-passwd ; this is preferred to netrc-*
835 (netrc-machine-user-or-password
839 (or nnimap-server-address
842 (list "imap" "imaps" "143" "993")))))
843 (if (imap-authenticate user passwd nnimap-server-buffer)
845 (setq nnimap-server-buffer-alist
846 (nnimap-remove-server-from-buffer-alist
848 nnimap-server-buffer-alist))
849 (push (list server nnimap-server-buffer)
850 nnimap-server-buffer-alist)
851 (imap-id nnimap-id nnimap-server-buffer)
852 (nnimap-possibly-change-server server))
853 (imap-close nnimap-server-buffer)
854 (kill-buffer nnimap-server-buffer)
855 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
857 (deffoo nnimap-open-server (server &optional defs)
858 (nnheader-init-server-buffer)
859 (if (nnimap-server-opened server)
861 (unless (assq 'nnimap-server-buffer defs)
862 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
863 ;; translate `nnimap-server-address' to `nnimap-address' in defs
864 ;; for people that configured nnimap with a very old version
865 (unless (assq 'nnimap-address defs)
866 (if (assq 'nnimap-server-address defs)
867 (push (list 'nnimap-address
868 (cadr (assq 'nnimap-server-address defs))) defs)
869 (push (list 'nnimap-address server) defs)))
870 (nnoo-change-server 'nnimap server defs)
871 (or nnimap-server-buffer
872 (setq nnimap-server-buffer (cadr (assq 'nnimap-server-buffer defs))))
873 (with-current-buffer (get-buffer-create nnimap-server-buffer)
874 (nnoo-change-server 'nnimap server defs))
875 (let ((imap-logout-timeout nnimap-logout-timeout))
876 (or (and nnimap-server-buffer
877 (imap-opened nnimap-server-buffer)
878 (if (with-current-buffer nnimap-server-buffer
879 (memq imap-state '(auth selected examine)))
881 (imap-close nnimap-server-buffer)
882 (nnimap-open-connection server)))
883 (nnimap-open-connection server)))))
885 (deffoo nnimap-server-opened (&optional server)
886 "Whether SERVER is opened.
887 If SERVER is the current virtual server, and the connection to the
888 physical server is alive, this function return a non-nil value. If
889 SERVER is nil, it is treated as the current server."
890 ;; clean up autologouts??
891 (and (or server nnimap-current-server)
892 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
893 (imap-opened (nnimap-get-server-buffer server))))
895 (deffoo nnimap-close-server (&optional server)
896 "Close connection to server and free all resources connected to it.
897 Return nil if the server couldn't be closed for some reason."
898 (let ((server (or server nnimap-current-server))
899 (imap-logout-timeout nnimap-logout-timeout))
900 (when (or (nnimap-server-opened server)
901 (imap-opened (nnimap-get-server-buffer server)))
902 (imap-close (nnimap-get-server-buffer server))
903 (kill-buffer (nnimap-get-server-buffer server))
904 (setq nnimap-server-buffer nil
905 nnimap-current-server nil
906 nnimap-server-buffer-alist
907 (nnimap-remove-server-from-buffer-alist
909 nnimap-server-buffer-alist)))
910 (nnoo-close-server 'nnimap server)))
912 (deffoo nnimap-request-close ()
913 "Close connection to all servers and free all resources that the backend have reserved.
914 All buffers that have been created by that
915 backend should be killed. (Not the nntp-server-buffer, though.) This
916 function is generally only called when Gnus is shutting down."
917 (mapc (lambda (server) (nnimap-close-server (car server)))
918 nnimap-server-buffer-alist)
919 (setq nnimap-server-buffer-alist nil))
921 (deffoo nnimap-status-message (&optional server)
922 "This function returns the last error message from server."
923 (when (nnimap-possibly-change-server server)
924 (nnoo-status-message 'nnimap server)))
926 ;; We used to use a string-as-multibyte here, but it is really incorrect.
927 ;; This function is used when we're about to insert a unibyte string
928 ;; into a potentially multibyte buffer. The string is either an article
929 ;; header or body (or both?), undecoded. When Emacs is asked to convert
930 ;; a unibyte string to multibyte, it may either use the equivalent of
931 ;; nothing (e.g. non-Mule XEmacs), string-make-unibyte (i.e. decode using
932 ;; locale), string-as-multibyte (decode using emacs-internal coding system)
933 ;; or string-to-multibyte (keep the data undecoded as a sequence of bytes).
934 ;; Only the last one preserves the data such that we can reliably later on
935 ;; decode the text using the mime info.
936 (defalias 'nnimap-demule 'mm-string-to-multibyte)
938 (defun nnimap-make-callback (article gnus-callback buffer)
939 "Return a callback function."
941 (nnimap-callback ,article ,gnus-callback ,buffer)))
943 (defun nnimap-callback (article gnus-callback buffer)
944 (when (eq article (imap-current-message))
945 (remove-hook 'imap-fetch-data-hook
946 (nnimap-make-callback article gnus-callback buffer))
947 (with-current-buffer buffer
949 (with-current-buffer nnimap-server-buffer
951 (if (imap-capability 'IMAP4rev1)
952 ;; xxx don't just use car? alist doesn't contain
953 ;; anything else now, but it might...
954 (nth 2 (car (imap-message-get article 'BODYDETAIL)))
955 (imap-message-get article 'RFC822)))))
956 (nnheader-ms-strip-cr)
957 (funcall gnus-callback t))))
959 (defun nnimap-request-article-part (article part prop &optional
960 group server to-buffer detail)
961 (when (nnimap-possibly-change-group group server)
962 (let ((article (if (stringp article)
963 (car-safe (imap-search
964 (format "HEADER Message-Id \"%s\"" article)
965 nnimap-server-buffer))
968 (gnus-message 10 "nnimap: Fetching (part of) article %d from %s..."
969 article (or (nnimap-decode-group-name group)
970 (imap-current-mailbox)
971 (nnimap-decode-group-name
972 gnus-newsgroup-name)))
973 (if (not nnheader-callback-function)
974 (with-current-buffer (or to-buffer nntp-server-buffer)
976 (let ((data (imap-fetch article part prop nil
977 nnimap-server-buffer)))
978 ;; data can be nil if article is write-only
980 (insert (nnimap-demule (if detail
983 (nnheader-ms-strip-cr)
985 10 "nnimap: Fetching (part of) article %d from %s...done"
986 article (or (nnimap-decode-group-name group)
987 (imap-current-mailbox)
988 (nnimap-decode-group-name gnus-newsgroup-name)))
990 (nnheader-report 'nnimap "No such article %d in %s: %s"
991 article (or (nnimap-decode-group-name group)
992 (imap-current-mailbox)
993 (nnimap-decode-group-name
994 gnus-newsgroup-name))
995 (imap-error-text nnimap-server-buffer))
996 (cons group article)))
997 (add-hook 'imap-fetch-data-hook
998 (nnimap-make-callback article
999 nnheader-callback-function
1000 nntp-server-buffer))
1001 (imap-fetch-asynch article part nil nnimap-server-buffer)
1002 (cons group article))))))
1004 (deffoo nnimap-asynchronous-p ()
1007 (deffoo nnimap-request-article (article &optional group server to-buffer)
1008 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
1009 (nnimap-request-article-part
1010 article "BODY.PEEK[]" 'BODYDETAIL group server to-buffer 'detail)
1011 (nnimap-request-article-part
1012 article "RFC822.PEEK" 'RFC822 group server to-buffer)))
1014 (deffoo nnimap-request-head (article &optional group server to-buffer)
1015 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
1016 (nnimap-request-article-part
1017 article "BODY.PEEK[HEADER]" 'BODYDETAIL group server to-buffer 'detail)
1018 (nnimap-request-article-part
1019 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer)))
1021 (deffoo nnimap-request-body (article &optional group server to-buffer)
1022 (if (imap-capability 'IMAP4rev1 nnimap-server-buffer)
1023 (nnimap-request-article-part
1024 article "BODY.PEEK[TEXT]" 'BODYDETAIL group server to-buffer 'detail)
1025 (nnimap-request-article-part
1026 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer)))
1028 (deffoo nnimap-request-group (group &optional server fast)
1029 (nnimap-request-update-info-internal
1031 (gnus-get-info (gnus-group-prefixed-name
1032 group (gnus-server-to-method (format "nnimap:%s" server))))
1034 (when (nnimap-possibly-change-group group server)
1035 (nnimap-before-find-minmax-bugworkaround)
1038 ((null (setq info (nnimap-find-minmax-uid group t)))
1039 (nnheader-report 'nnimap "Could not get active info for %s"
1042 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
1043 (max 1 (or (nth 1 info) 1))
1044 (or (nth 2 info) 0) group)
1045 (nnheader-report 'nnimap "Group %s selected" group)
1048 (defun nnimap-update-unseen (group &optional server)
1049 "Update the unseen count in `nnimap-mailbox-info'."
1051 (gnus-group-prefixed-name group server)
1052 (let ((old (gnus-gethash-safe (gnus-group-prefixed-name group server)
1053 nnimap-mailbox-info)))
1054 (list (nth 0 old) (nth 1 old)
1055 (imap-mailbox-status (nnimap-decode-group-name group)
1056 'unseen nnimap-server-buffer)
1058 nnimap-mailbox-info))
1060 (defun nnimap-close-group (group &optional server)
1061 (with-current-buffer nnimap-server-buffer
1062 (when (and (imap-opened)
1063 (nnimap-possibly-change-group group server))
1064 (nnimap-update-unseen group server)
1065 (case nnimap-expunge-on-close
1067 (imap-mailbox-expunge nnimap-close-asynchronous)
1068 (unless nnimap-dont-close
1069 (imap-mailbox-close nnimap-close-asynchronous))))
1070 (ask (if (and (imap-search "DELETED")
1071 (gnus-y-or-n-p (format "Expunge articles in group `%s'? "
1072 (imap-current-mailbox))))
1074 (imap-mailbox-expunge nnimap-close-asynchronous)
1075 (unless nnimap-dont-close
1076 (imap-mailbox-close nnimap-close-asynchronous)))
1077 (imap-mailbox-unselect)))
1078 (t (imap-mailbox-unselect)))
1079 (not imap-current-mailbox))))
1081 (defun nnimap-pattern-to-list-arguments (pattern)
1083 (cons (car-safe p) (or (cdr-safe p) p)))
1084 (if (and (listp pattern)
1085 (listp (cdr pattern)))
1089 (deffoo nnimap-request-list (&optional server)
1090 (when (nnimap-possibly-change-server server)
1091 (with-current-buffer nntp-server-buffer
1093 (gnus-message 5 "nnimap: Generating active list%s..."
1094 (if (> (length server) 0) (concat " for " server) ""))
1095 (nnimap-before-find-minmax-bugworkaround)
1096 (with-current-buffer nnimap-server-buffer
1097 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
1098 (dolist (mbx (funcall nnimap-request-list-method
1099 (cdr pattern) (car pattern)))
1100 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
1101 (let* ((encoded-mbx (nnimap-encode-group-name mbx))
1102 (info (nnimap-find-minmax-uid encoded-mbx 'examine)))
1104 (with-current-buffer nntp-server-buffer
1105 (insert (format "\"%s\" %d %d y\n"
1106 encoded-mbx (or (nth 2 info) 0)
1107 (max 1 (or (nth 1 info) 1)))))))))))
1108 (gnus-message 5 "nnimap: Generating active list%s...done"
1109 (if (> (length server) 0) (concat " for " server) ""))
1112 (deffoo nnimap-request-post (&optional server)
1114 (dolist (mbx (message-unquote-tokens
1115 (message-tokenize-header
1116 (message-fetch-field "Newsgroups") ", ")) success)
1117 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1118 (or (gnus-active to-newsgroup)
1119 (gnus-activate-group to-newsgroup)
1120 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1122 (or (and (gnus-request-create-group
1123 to-newsgroup gnus-command-method)
1124 (gnus-activate-group to-newsgroup nil nil
1125 gnus-command-method))
1126 (error "Couldn't create group %s" to-newsgroup)))
1127 (error "No such group: %s" to-newsgroup))
1128 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
1129 (setq success nil))))))
1131 ;; Optional backend functions
1133 (defun nnimap-string-lessp-numerical (s1 s2)
1134 "Return t if first arg string is less than second in numerical order."
1135 (cond ((string= s1 s2)
1137 ((> (length s1) (length s2))
1139 ((< (length s1) (length s2))
1141 ((< (string-to-number (substring s1 0 1))
1142 (string-to-number (substring s2 0 1)))
1144 ((> (string-to-number (substring s1 0 1))
1145 (string-to-number (substring s2 0 1)))
1148 (nnimap-string-lessp-numerical (substring s1 1) (substring s2 1)))))
1150 (deffoo nnimap-retrieve-groups (groups &optional server)
1151 (when (nnimap-possibly-change-server server)
1152 (gnus-message 5 "nnimap: Checking mailboxes...")
1153 (with-current-buffer nntp-server-buffer
1155 (nnimap-before-find-minmax-bugworkaround)
1156 (let (asyncgroups slowgroups decoded-group)
1157 (if (null nnimap-retrieve-groups-asynchronous)
1158 (setq slowgroups groups)
1159 (dolist (group groups)
1160 (setq decoded-group (nnimap-decode-group-name group))
1161 (gnus-message 9 "nnimap: Quickly checking mailbox %s"
1163 (add-to-list (if (gnus-gethash-safe
1164 (gnus-group-prefixed-name group server)
1165 nnimap-mailbox-info)
1168 (list group (imap-mailbox-status-asynch
1170 '(uidvalidity uidnext unseen)
1171 nnimap-server-buffer))))
1172 (dolist (asyncgroup asyncgroups)
1173 (let ((group (nth 0 asyncgroup))
1174 (tag (nth 1 asyncgroup))
1176 (setq decoded-group (nnimap-decode-group-name group))
1177 (when (imap-ok-p (imap-wait-for-tag tag nnimap-server-buffer))
1178 (if (or (not (string=
1179 (nth 0 (gnus-gethash (gnus-group-prefixed-name
1181 nnimap-mailbox-info))
1182 (imap-mailbox-get 'uidvalidity decoded-group
1183 nnimap-server-buffer)))
1185 (nth 1 (gnus-gethash (gnus-group-prefixed-name
1187 nnimap-mailbox-info))
1188 (imap-mailbox-get 'uidnext decoded-group
1189 nnimap-server-buffer))))
1190 (push (list group) slowgroups)
1191 (insert (nth 3 (gnus-gethash (gnus-group-prefixed-name
1193 nnimap-mailbox-info))))))))
1194 (dolist (group slowgroups)
1195 (if nnimap-retrieve-groups-asynchronous
1196 (setq group (car group)))
1197 (setq decoded-group (nnimap-decode-group-name group))
1198 (gnus-message 7 "nnimap: Mailbox %s modified" decoded-group)
1199 (imap-mailbox-put 'uidnext nil decoded-group nnimap-server-buffer)
1200 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags decoded-group
1201 nnimap-server-buffer))
1202 (let* ((info (nnimap-find-minmax-uid group 'examine))
1203 (str (format "\"%s\" %d %d y\n" group
1205 (max 1 (or (nth 1 info) 1)))))
1206 (when (> (or (imap-mailbox-get 'recent decoded-group
1207 nnimap-server-buffer) 0)
1209 (push (list (cons decoded-group 0)) nnmail-split-history))
1211 (when nnimap-retrieve-groups-asynchronous
1213 (gnus-group-prefixed-name group server)
1214 (list (or (imap-mailbox-get
1215 'uidvalidity decoded-group nnimap-server-buffer)
1216 (imap-mailbox-status
1217 decoded-group 'uidvalidity nnimap-server-buffer))
1218 (or (imap-mailbox-get
1219 'uidnext decoded-group nnimap-server-buffer)
1220 (imap-mailbox-status
1221 decoded-group 'uidnext nnimap-server-buffer))
1222 (or (imap-mailbox-get
1223 'unseen decoded-group nnimap-server-buffer)
1224 (imap-mailbox-status
1225 decoded-group 'unseen nnimap-server-buffer))
1227 nnimap-mailbox-info)))))))
1228 (gnus-message 5 "nnimap: Checking mailboxes...done")
1231 (deffoo nnimap-request-update-info-internal (group info &optional server)
1232 (when (nnimap-possibly-change-group group server)
1233 (when info ;; xxx what does this mean? should we create a info?
1234 (with-current-buffer nnimap-server-buffer
1235 (gnus-message 5 "nnimap: Updating info for %s..."
1236 (nnimap-decode-group-name (gnus-info-group info)))
1238 (when (nnimap-mark-permanent-p 'read)
1240 ;; read info could contain articles marked unread by other
1241 ;; imap clients! we correct this
1242 (setq unseen (gnus-compress-sequence
1243 (imap-search "UNSEEN UNDELETED"))
1244 seen (gnus-range-difference (gnus-info-read info) unseen)
1245 seen (gnus-range-add seen
1246 (gnus-compress-sequence
1247 (imap-search "SEEN")))
1248 seen (if (and (integerp (car seen))
1250 (list (cons (car seen) (car seen)))
1252 (gnus-info-set-read info seen)))
1254 (dolist (pred gnus-article-mark-lists)
1255 (when (or (eq (cdr pred) 'recent)
1256 (and (nnimap-mark-permanent-p (cdr pred))
1257 (member (nnimap-mark-to-flag (cdr pred))
1258 (imap-mailbox-get 'flags))))
1259 (gnus-info-set-marks
1261 (gnus-update-alist-soft
1263 (gnus-compress-sequence
1264 (imap-search (nnimap-mark-to-predicate (cdr pred))))
1265 (gnus-info-marks info))
1268 (when nnimap-importantize-dormant
1269 ;; nnimap mark dormant article as ticked too (for other clients)
1270 ;; so we remove that mark for gnus since we support dormant
1271 (gnus-info-set-marks
1273 (gnus-update-alist-soft
1275 (gnus-remove-from-range
1276 (cdr-safe (assoc 'tick (gnus-info-marks info)))
1277 (cdr-safe (assoc 'dormant (gnus-info-marks info))))
1278 (gnus-info-marks info))
1281 (gnus-message 5 "nnimap: Updating info for %s...done"
1282 (nnimap-decode-group-name (gnus-info-group info)))
1286 (deffoo nnimap-request-type (group &optional article)
1287 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
1291 (deffoo nnimap-request-set-mark (group actions &optional server)
1292 (when (nnimap-possibly-change-group group server)
1293 (with-current-buffer nnimap-server-buffer
1295 (gnus-message 7 "nnimap: Setting marks in %s..."
1296 (nnimap-decode-group-name group))
1297 (while (setq action (pop actions))
1298 (let ((range (nth 0 action))
1299 (what (nth 1 action))
1300 (cmdmarks (nth 2 action))
1302 ;; bookmark can't be stored (not list/range
1303 (setq cmdmarks (delq 'bookmark cmdmarks))
1304 ;; killed can't be stored (not list/range
1305 (setq cmdmarks (delq 'killed cmdmarks))
1306 ;; unsent are for nndraft groups only
1307 (setq cmdmarks (delq 'unsent cmdmarks))
1308 ;; cache flags are pointless on the server
1309 (setq cmdmarks (delq 'cache cmdmarks))
1310 ;; seen flags are local to each gnus
1311 (setq cmdmarks (delq 'seen cmdmarks))
1312 ;; recent marks can't be set
1313 (setq cmdmarks (delq 'recent cmdmarks))
1314 (when nnimap-importantize-dormant
1315 ;; flag dormant articles as ticked
1316 (if (memq 'dormant cmdmarks)
1317 (setq cmdmarks (cons 'tick cmdmarks))))
1318 ;; remove stuff we are forbidden to store
1319 (mapc (lambda (mark)
1320 (if (imap-message-flag-permanent-p
1321 (nnimap-mark-to-flag mark))
1322 (setq marks (cons mark marks))))
1324 (when (and range marks)
1325 (cond ((eq what 'del)
1326 (imap-message-flags-del
1327 (imap-range-to-message-set range)
1328 (nnimap-mark-to-flag marks nil t)))
1330 (imap-message-flags-add
1331 (imap-range-to-message-set range)
1332 (nnimap-mark-to-flag marks nil t)))
1334 (imap-message-flags-set
1335 (imap-range-to-message-set range)
1336 (nnimap-mark-to-flag marks nil t)))))))
1337 (gnus-message 7 "nnimap: Setting marks in %s...done"
1338 (nnimap-decode-group-name group)))))
1341 (defun nnimap-split-fancy ()
1342 "Like the function `nnmail-split-fancy', but uses `nnimap-split-fancy'."
1343 (let ((nnmail-split-fancy nnimap-split-fancy))
1344 (nnmail-split-fancy)))
1346 (defun nnimap-split-to-groups (rules)
1347 ;; tries to match all rules in nnimap-split-rule against content of
1348 ;; nntp-server-buffer, returns a list of groups that matched.
1349 ;; Note: This function takes and returns decoded group names.
1350 (with-current-buffer nntp-server-buffer
1351 ;; Fold continuation lines.
1352 (goto-char (point-min))
1353 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1354 (replace-match " " t t))
1355 (if (functionp rules)
1357 (let (to-groups regrepp)
1359 (dolist (rule rules to-groups)
1360 (let ((group (car rule))
1361 (regexp (cadr rule)))
1362 (goto-char (point-min))
1363 (when (and (if (stringp regexp)
1365 (if (not (stringp group))
1366 (setq group (eval group))
1368 (string-match "\\\\[0-9&]" group)))
1369 (re-search-forward regexp nil t))
1370 (funcall regexp group))
1371 ;; Don't enter the article into the same group twice.
1372 (not (assoc group to-groups)))
1374 (nnmail-expand-newtext group)
1377 (or nnimap-split-crosspost
1378 (throw 'split-done to-groups))))))))))
1380 (defun nnimap-assoc-match (key alist)
1382 (while (and alist (not element))
1383 (if (string-match (car (car alist)) key)
1384 (setq element (car alist)))
1385 (setq alist (cdr alist)))
1388 (defun nnimap-split-find-rule (server inbox)
1389 (if (and (listp nnimap-split-rule) (listp (car nnimap-split-rule))
1390 (list (cdar nnimap-split-rule)) (listp (cadar nnimap-split-rule)))
1392 (cadr (nnimap-assoc-match inbox (cdr (nnimap-assoc-match
1393 server nnimap-split-rule))))
1396 (defun nnimap-split-find-inbox (server)
1397 (if (listp nnimap-split-inbox)
1399 (list nnimap-split-inbox)))
1401 (defun nnimap-split-articles (&optional group server)
1402 ;; Note: Assumes decoded group names in nnimap-split-inbox,
1403 ;; nnimap-split-rule, nnimap-split-fancy, and nnmail-split-history.
1404 (when (nnimap-possibly-change-server server)
1405 (with-current-buffer nnimap-server-buffer
1406 (let (rule inbox removeorig
1407 (inboxes (nnimap-split-find-inbox server)))
1408 ;; iterate over inboxes
1409 (while (and (setq inbox (pop inboxes))
1410 (nnimap-possibly-change-group
1411 (nnimap-encode-group-name inbox))) ;; SELECT
1412 ;; find split rule for this server / inbox
1413 (when (setq rule (nnimap-split-find-rule server inbox))
1414 ;; iterate over articles
1415 (dolist (article (imap-search nnimap-split-predicate))
1416 (when (if (if (eq nnimap-split-download-body 'default)
1417 nnimap-split-download-body-default
1418 nnimap-split-download-body)
1419 (and (nnimap-request-article article)
1420 (with-current-buffer nntp-server-buffer (mail-narrow-to-head)))
1421 (nnimap-request-head article))
1422 ;; copy article to right group(s)
1423 (setq removeorig nil)
1424 (dolist (to-group (nnimap-split-to-groups rule))
1425 (cond ((eq to-group 'junk)
1426 (message "IMAP split removed %s:%s:%d" server inbox
1428 (setq removeorig t))
1429 ((imap-message-copy (number-to-string article)
1430 to-group nil 'nocopyuid)
1431 (message "IMAP split moved %s:%s:%d to %s" server
1432 inbox article to-group)
1434 (when nnmail-cache-accepted-message-ids
1435 (with-current-buffer nntp-server-buffer
1438 (nnmail-fetch-field "message-id"))
1439 (nnmail-cache-insert msgid
1440 (nnimap-encode-group-name to-group)
1441 (nnmail-fetch-field "subject"))))))
1442 ;; Add the group-art list to the history list.
1443 (push (list (cons to-group 0)) nnmail-split-history))
1445 (message "IMAP split failed to move %s:%s:%d to %s"
1446 server inbox article to-group))))
1447 (if (if (eq nnimap-split-download-body 'default)
1448 nnimap-split-download-body-default
1449 nnimap-split-download-body)
1451 ;; remove article if it was successfully copied somewhere
1453 (imap-message-flags-add (format "%d" article)
1454 "\\Seen \\Deleted")))))
1455 (when (imap-mailbox-select inbox) ;; just in case
1456 ;; todo: UID EXPUNGE (if available) to remove splitted articles
1457 (imap-mailbox-expunge)
1458 (imap-mailbox-close)))
1459 (when nnmail-cache-accepted-message-ids
1460 (nnmail-cache-close))
1463 (deffoo nnimap-request-scan (&optional group server)
1464 (nnimap-split-articles group server))
1466 (deffoo nnimap-request-newgroups (date &optional server)
1467 (when (nnimap-possibly-change-server server)
1468 (with-current-buffer nntp-server-buffer
1469 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
1470 (if (> (length server) 0) " on " "") server)
1472 (nnimap-before-find-minmax-bugworkaround)
1473 (dolist (pattern (nnimap-pattern-to-list-arguments
1474 nnimap-list-pattern))
1475 (dolist (mbx (imap-mailbox-lsub (cdr pattern) (car pattern) nil
1476 nnimap-server-buffer))
1478 (dolist (mailbox (imap-mailbox-get 'list-flags mbx
1479 nnimap-server-buffer))
1480 (if (string= (downcase mailbox) "\\noselect")
1483 (let* ((encoded-mbx (nnimap-encode-group-name mbx))
1484 (info (nnimap-find-minmax-uid encoded-mbx 'examine)))
1486 (insert (format "\"%s\" %d %d y\n"
1487 encoded-mbx (or (nth 2 info) 0)
1488 (max 1 (or (nth 1 info) 1)))))))))
1489 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
1490 (if (> (length server) 0) " on " "") server))
1493 (deffoo nnimap-request-create-group (group &optional server args)
1494 (when (nnimap-possibly-change-server server)
1495 (let ((decoded-group (nnimap-decode-group-name group)))
1496 (or (imap-mailbox-status decoded-group 'uidvalidity nnimap-server-buffer)
1497 (imap-mailbox-create decoded-group nnimap-server-buffer)
1498 (nnheader-report 'nnimap "%S"
1499 (imap-error-text nnimap-server-buffer))))))
1501 (defun nnimap-time-substract (time1 time2)
1502 "Return TIME for TIME1 - TIME2."
1503 (let* ((ms (- (car time1) (car time2)))
1504 (ls (- (nth 1 time1) (nth 1 time2))))
1506 (list (- ms 1) (+ (expt 2 16) ls))
1509 (eval-when-compile (require 'parse-time))
1510 (defun nnimap-date-days-ago (daysago)
1511 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
1512 (require 'parse-time)
1513 (let* ((time (nnimap-time-substract (current-time) (days-to-time daysago)))
1514 (date (format-time-string
1515 (format "%%d-%s-%%Y"
1516 (capitalize (car (rassoc (nth 4 (decode-time time))
1517 parse-time-months))))
1519 (if (eq ?0 (string-to-char date))
1523 (defun nnimap-request-expire-articles-progress ()
1524 (gnus-message 5 "nnimap: Marking article %d for deletion..."
1525 imap-current-message))
1527 (defun nnimap-expiry-target (arts group server)
1528 (unless (eq nnmail-expiry-target 'delete)
1531 (nnimap-request-article art group server (current-buffer))
1532 ;; hints for optimization in `nnimap-request-accept-article'
1533 (let ((nnimap-current-move-article art)
1534 (nnimap-current-move-group group)
1535 (nnimap-current-move-server server))
1536 (nnmail-expiry-target-group nnmail-expiry-target group))))
1537 ;; It is not clear if `nnmail-expiry-target' somehow cause the
1538 ;; current group to be changed or not, so we make sure here.
1539 (nnimap-possibly-change-group group server)))
1541 ;; Notice that we don't actually delete anything, we just mark them deleted.
1542 (deffoo nnimap-request-expire-articles (articles group &optional server force)
1543 (let ((artseq (gnus-compress-sequence articles)))
1544 (when (and artseq (nnimap-possibly-change-group group server))
1545 (with-current-buffer nnimap-server-buffer
1546 (let ((days (or (and nnmail-expiry-wait-function
1547 (funcall nnmail-expiry-wait-function group))
1548 nnmail-expiry-wait)))
1549 (cond ((or force (eq days 'immediate))
1550 (let ((oldarts (imap-search
1552 (imap-range-to-message-set artseq)))))
1554 (nnimap-expiry-target oldarts group server)
1555 (when (imap-message-flags-add
1556 (imap-range-to-message-set
1557 (gnus-compress-sequence oldarts)) "\\Deleted")
1558 (setq articles (gnus-set-difference
1559 articles oldarts))))))
1560 ((and nnimap-search-uids-not-since-is-evil (numberp days))
1561 (let* ((all-new-articles
1562 (gnus-compress-sequence
1563 (imap-search (format "SINCE %s"
1564 (nnimap-date-days-ago days)))))
1566 (gnus-range-difference artseq all-new-articles))
1567 (oldarts (gnus-uncompress-range oldartseq)))
1569 (nnimap-expiry-target oldarts group server)
1570 (when (imap-message-flags-add
1571 (imap-range-to-message-set oldartseq)
1573 (setq articles (gnus-set-difference
1574 articles oldarts))))))
1576 (let ((oldarts (imap-search
1577 (format nnimap-expunge-search-string
1578 (imap-range-to-message-set artseq)
1579 (nnimap-date-days-ago days))))
1580 (imap-fetch-data-hook
1581 '(nnimap-request-expire-articles-progress)))
1583 (nnimap-expiry-target oldarts group server)
1584 (when (imap-message-flags-add
1585 (imap-range-to-message-set
1586 (gnus-compress-sequence oldarts)) "\\Deleted")
1587 (setq articles (gnus-set-difference
1588 articles oldarts)))))))))))
1589 ;; return articles not deleted
1592 (deffoo nnimap-request-move-article (article group server accept-form
1593 &optional last move-is-internal)
1594 (when (nnimap-possibly-change-server server)
1596 (let ((buf (get-buffer-create " *nnimap move*"))
1597 (nnimap-current-move-article article)
1598 (nnimap-current-move-group group)
1599 (nnimap-current-move-server nnimap-current-server)
1601 (gnus-message 10 "nnimap-request-move-article: this is an %s move"
1602 (if move-is-internal
1605 ;; request the article only when the move is NOT internal
1606 (and (or move-is-internal
1607 (nnimap-request-article article group server))
1608 (with-current-buffer buf
1609 (buffer-disable-undo (current-buffer))
1610 (insert-buffer-substring nntp-server-buffer)
1611 (setq result (eval accept-form))
1614 (nnimap-possibly-change-group group server)
1615 (imap-message-flags-add
1616 (imap-range-to-message-set (list article))
1617 "\\Deleted" 'silent nnimap-server-buffer))
1620 (deffoo nnimap-request-accept-article (group &optional server last)
1621 (when (nnimap-possibly-change-server server)
1624 (if (string= nnimap-current-server nnimap-current-move-server)
1625 ;; moving article within same server, speed it up...
1626 (and (nnimap-possibly-change-group
1627 nnimap-current-move-group)
1628 (imap-message-copy (number-to-string
1629 nnimap-current-move-article)
1630 (nnimap-decode-group-name group)
1632 nnimap-server-buffer))
1633 (with-current-buffer (current-buffer)
1634 (goto-char (point-min))
1635 ;; remove any 'From blabla' lines, some IMAP servers
1636 ;; reject the entire message otherwise.
1637 (when (looking-at "^From[^:]")
1638 (delete-region (point) (progn (forward-line) (point))))
1639 ;; turn into rfc822 format (\r\n eol's)
1640 (while (search-forward "\n" nil t)
1641 (replace-match "\r\n"))
1642 (when nnmail-cache-accepted-message-ids
1643 (nnmail-cache-insert (nnmail-fetch-field "message-id")
1645 (nnmail-fetch-field "subject"))))
1646 (when (and last nnmail-cache-accepted-message-ids)
1647 (nnmail-cache-close))
1648 ;; this 'or' is for Cyrus server bug
1649 (or (null (imap-current-mailbox nnimap-server-buffer))
1650 (imap-mailbox-unselect nnimap-server-buffer))
1651 (imap-message-append (nnimap-decode-group-name group)
1652 (current-buffer) nil nil
1653 nnimap-server-buffer)))
1654 (cons group (nth 1 uid))
1655 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1657 (deffoo nnimap-request-delete-group (group force &optional server)
1658 (when (nnimap-possibly-change-server server)
1659 (setq group (nnimap-decode-group-name group))
1660 (when (string= group (imap-current-mailbox nnimap-server-buffer))
1661 (imap-mailbox-unselect nnimap-server-buffer))
1662 (with-current-buffer nnimap-server-buffer
1664 (or (null (imap-mailbox-status group 'uidvalidity))
1665 (imap-mailbox-delete group))
1669 (deffoo nnimap-request-rename-group (group new-name &optional server)
1670 (when (nnimap-possibly-change-server server)
1671 (imap-mailbox-rename (nnimap-decode-group-name group)
1672 (nnimap-decode-group-name new-name)
1673 nnimap-server-buffer)))
1675 (defun nnimap-expunge (mailbox server)
1676 (when (nnimap-possibly-change-group mailbox server)
1677 (imap-mailbox-expunge nil nnimap-server-buffer)))
1679 (defun nnimap-acl-get (mailbox server)
1680 (when (nnimap-possibly-change-server server)
1681 (and (imap-capability 'ACL nnimap-server-buffer)
1682 (imap-mailbox-acl-get (nnimap-decode-group-name mailbox)
1683 nnimap-server-buffer))))
1685 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1686 (when (nnimap-possibly-change-server (cadr method))
1687 (unless (imap-capability 'ACL nnimap-server-buffer)
1688 (error "Your server does not support ACL editing"))
1689 (with-current-buffer nnimap-server-buffer
1690 ;; delete all removed identifiers
1691 (mapc (lambda (old-acl)
1692 (unless (assoc (car old-acl) new-acls)
1693 (or (imap-mailbox-acl-delete (car old-acl)
1694 (nnimap-decode-group-name mailbox))
1695 (error "Can't delete ACL for %s" (car old-acl)))))
1697 ;; set all changed acl's
1698 (mapc (lambda (new-acl)
1699 (let ((new-rights (cdr new-acl))
1700 (old-rights (cdr (assoc (car new-acl) old-acls))))
1701 (unless (and old-rights new-rights
1702 (string= old-rights new-rights))
1703 (or (imap-mailbox-acl-set (car new-acl) new-rights
1704 (nnimap-decode-group-name mailbox))
1705 (error "Can't set ACL for %s to %s" (car new-acl)
1711 ;;; Internal functions
1714 ;; This is confusing.
1716 ;; mark => read, tick, draft, reply etc
1717 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1718 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1720 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1721 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1724 (defconst nnimap-mark-to-predicate-alist
1726 (lambda (pair) ; cdr is the mark
1727 (or (assoc (cdr pair)
1732 (reply . "ANSWERED")))
1734 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1735 (cons '(read . read) gnus-article-mark-lists)))
1737 (defun nnimap-mark-to-predicate (pred)
1738 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1739 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1740 to be used within a IMAP SEARCH query."
1741 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1743 (defconst nnimap-mark-to-flag-alist
1746 (or (assoc (cdr pair)
1748 (tick . "\\Flagged")
1750 (recent . "\\Recent")
1751 (reply . "\\Answered")))
1753 (format "gnus-%s" (symbol-name (cdr pair))))))
1754 (cons '(read . read) gnus-article-mark-lists)))
1756 (defun nnimap-mark-to-flag-1 (preds)
1757 (if (and (not (null preds)) (listp preds))
1758 (cons (nnimap-mark-to-flag (car preds))
1759 (nnimap-mark-to-flag (cdr preds)))
1760 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1762 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1763 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1764 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1765 be used in a STORE FLAGS command."
1766 (let ((result (nnimap-mark-to-flag-1 preds)))
1767 (setq result (if (and (or make-string always-list)
1768 (not (listp result)))
1772 (mapconcat (lambda (flag)
1774 (mapconcat 'identity flag " ")
1779 (defun nnimap-mark-permanent-p (mark &optional group)
1780 "Return t if MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1781 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1785 (buffer-disable-undo (get-buffer-create nnimap-debug-buffer))
1786 (mapc (lambda (f) (trace-function-background f nnimap-debug-buffer))
1788 nnimap-possibly-change-server
1789 nnimap-verify-uidvalidity
1790 nnimap-find-minmax-uid
1791 nnimap-before-find-minmax-bugworkaround
1792 nnimap-possibly-change-group
1793 ;;nnimap-replace-whitespace
1794 nnimap-retrieve-headers-progress
1795 nnimap-retrieve-which-headers
1796 nnimap-group-overview-filename
1797 nnimap-retrieve-headers-from-file
1798 nnimap-retrieve-headers-from-server
1799 nnimap-retrieve-headers
1800 nnimap-open-connection
1802 nnimap-server-opened
1804 nnimap-request-close
1805 nnimap-status-message
1807 nnimap-request-article-part
1808 nnimap-request-article
1811 nnimap-request-group
1813 nnimap-pattern-to-list-arguments
1816 nnimap-retrieve-groups
1817 nnimap-request-update-info-internal
1819 nnimap-request-set-mark
1820 nnimap-split-to-groups
1821 nnimap-split-find-rule
1822 nnimap-split-find-inbox
1823 nnimap-split-articles
1825 nnimap-request-newgroups
1826 nnimap-request-create-group
1827 nnimap-time-substract
1828 nnimap-date-days-ago
1829 nnimap-request-expire-articles-progress
1830 nnimap-request-expire-articles
1831 nnimap-request-move-article
1832 nnimap-request-accept-article
1833 nnimap-request-delete-group
1834 nnimap-request-rename-group
1835 gnus-group-nnimap-expunge
1836 gnus-group-nnimap-edit-acl
1837 gnus-group-nnimap-edit-acl-done
1838 nnimap-group-mode-hook
1839 nnimap-mark-to-predicate
1840 nnimap-mark-to-flag-1
1842 nnimap-mark-permanent-p
1847 ;; arch-tag: 2b001f20-3ff9-4094-a0ad-46807c1ba70b
1848 ;;; nnimap.el ends here