1 ;;; nnimap.el --- imap backend for Gnus
2 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <jas@pdc.kth.se>
5 ;; Jim Radford <radford@robby.caltech.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Todo, major things:
29 ;; o Fix Gnus to view correct number of unread/total articles in group buffer
30 ;; o Fix Gnus to handle leading '.' in group names (fixed?)
31 ;; o Finish disconnected mode (moving articles between mailboxes unplugged)
33 ;; o MIME (partial article fetches)
34 ;; o Split to other backends, different split rules for different
37 ;; Todo, minor things:
39 ;; o Don't require half of Gnus -- backends should be standalone
40 ;; o Support escape characters in `message-tokenize-header'
41 ;; o Support NOV nnmail-extra-headers.
42 ;; o Verify that we don't use IMAP4rev1 specific things (RFC2060 App B)
43 ;; o Dont uid fetch 1,* in nnimap-retrive-groups (slow)
44 ;; o Split up big fetches (1,* header especially) in smaller chunks
45 ;; o What do I do with gnus-newsgroup-*?
46 ;; o Tell Gnus about new groups (how can we tell?)
47 ;; o Respooling (fix Gnus?) (unnecessery?)
48 ;; o Add support for the following: (if applicable)
49 ;; request-list-newsgroups, request-regenerate
51 ;; request-associate-buffer, request-restore-buffer,
52 ;; o Do The Right Thing when UIDVALIDITY changes (what's the right thing?)
53 ;; o Support RFC2221 (Login referrals)
54 ;; o IMAP2BIS compatibility? (RFC2061)
55 ;; o ACAP stuff (perhaps a different project, would be nice to ACAPify
57 ;; o What about Gnus's article editing, can we support it? NO!
58 ;; o Use \Draft to support the draft group??
76 (defconst nnimap-version "nnimap 0.131")
78 (defvoo nnimap-address nil
79 "Address of physical IMAP server. If nil, use the virtual server's name.")
81 (defvoo nnimap-server-port nil
82 "Port number on physical IMAP server.
83 If nil, defaults to 993 for SSL connections and 143 otherwise.")
85 ;; Splitting variables
87 (defvar nnimap-split-crosspost t
88 "If non-nil, do crossposting if several split methods match the mail.
89 If nil, the first match found will be used.")
91 (defvar nnimap-split-inbox nil
92 "*Name of mailbox to split mail from.
94 Mail is read from this mailbox and split according to rules in
97 This can be a string or a list of strings.")
99 (defvar nnimap-split-rule nil
100 "*Mail will be split according to theese rules.
102 Mail is read from mailbox(es) specified in `nnimap-split-inbox'.
104 If you'd like, for instance, one mail group for mail from the
105 \"gnus-imap\" mailing list, one group for junk mail and leave
106 everything else in the incoming mailbox, you could do something like
109 (setq nnimap-split-rule '((\"INBOX.gnus-imap\" \"From:.*gnus-imap\")
110 (\"INBOX.junk\" \"Subject:.*buy\")))
112 As you can see, `nnimap-split-rule' is a list of lists, where the first
113 element in each \"rule\" is the name of the IMAP mailbox, and the
114 second is a regexp that nnimap will try to match on the header to find
117 The first element can also be a list. In that case, the first element
118 is the server the second element is the group on that server in which
119 the matching article will be stored.
121 The second element can also be a function. In that case, it will be
122 called narrowed to the headers with the first element of the rule as
123 the argument. It should return a non-nil value if it thinks that the
124 mail belongs in that group.")
126 ;; Authorization / Privacy variables
128 (defvoo nnimap-auth-method nil
131 (defvoo nnimap-stream nil
132 "How nnimap will connect to the server.
134 The default, nil, will try to use the \"best\" method the server can
139 1) you want to connect with SSL. The SSL integration with IMAP is
140 brain-dead so you'll have to tell it specifically.
142 2) your server is more capable than your environment -- i.e. your
143 server accept Kerberos login's but you haven't installed the
144 `imtest' program or your machine isn't configured for Kerberos.
146 Possible choices: kerberos4, ssl, network")
148 (defvoo nnimap-authenticator nil
149 "How nnimap authenticate itself to the server.
151 The default, nil, will try to use the \"best\" method the server can
154 There is only one reason for fiddling with this variable, and that is
155 if your server is more capable than your environment -- i.e. you
156 connect to a server that accept Kerberos login's but you haven't
157 installed the `imtest' program or your machine isn't configured for
160 Possible choices: kerberos4, cram-md5, login, anonymous.")
162 (defvoo nnimap-directory (nnheader-concat gnus-directory "overview/")
163 "Directory to keep NOV cache files for nnimap groups.
164 See also `nnimap-nov-file-name'.")
166 (defvoo nnimap-nov-file-name "nnimap."
167 "NOV cache base filename.
168 The group name and `nnimap-nov-file-name-suffix' will be appended. A
169 typical complete file name would be
170 ~/News/overview/nnimap.pdc.INBOX.ding.nov, or
171 ~/News/overview/nnimap/pdc/INBOX/ding/nov if
172 `nnmail-use-long-file-names' is nil")
174 (defvoo nnimap-nov-file-name-suffix ".novcache"
175 "Suffix for NOV cache base filename.")
177 (defvoo nnimap-nov-is-evil nil
178 "If non-nil, nnimap will never generate or use a local nov database for this backend.
179 Using nov databases will speed up header fetching considerably.
180 Unlike other backends, you do not need to take special care if you
181 flip this variable.")
183 (defvoo nnimap-expunge-on-close 'always ; 'ask, 'never
184 "Whether to expunge a group when it is closed.
185 When a IMAP group with articles marked for deletion is closed, this
186 variable determine if nnimap should actually remove the articles or
189 If always, nnimap always perform a expunge when closing the group.
190 If never, nnimap never expunges articles marked for deletion.
191 If ask, nnimap will ask you if you wish to expunge marked articles.
193 When setting this variable to `never', you can only expunge articles
194 by using `G x' (gnus-group-nnimap-expunge) from the Group buffer.")
196 (defvoo nnimap-list-pattern "*"
197 "A string LIMIT or list of strings with mailbox wildcards used to limit available groups.
198 See below for available wildcards.
200 The LIMIT string can be a cons cell (REFERENCE . LIMIT), where
201 REFERENCE will be passed as the first parameter to LIST/LSUB. The
202 semantics of this are server specific, on the University of Washington
203 server you can specify a directory.
206 '(\"INBOX\" \"mail/*\" (\"~friend/mail/\" . \"list/*\"))
208 There are two wildcards * and %. * matches everything, % matches
209 everything in the current hierarchy.")
211 (defvoo nnimap-news-groups nil
212 "IMAP support a news-like mode, also known as bulletin board mode, where replies is sent via IMAP instead of SMTP.
214 This variable should contain a regexp matching groups where you wish
215 replies to be stored to the mailbox directly.
218 '(\"^[^I][^N][^B][^O][^X].*$\")
220 This will match all groups not beginning with \"INBOX\".
222 Note that there is nothing technically different between mail-like and
223 news-like mailboxes. If you wish to have a group with todo items or
224 similar which you wouldn't want to set up a mailing list for, you can
225 use this to make replies go directly to the group.")
227 (defvoo nnimap-server-address nil
228 "Obsolete. Use `nnimap-address'.")
230 (defcustom nnimap-authinfo-file "~/.authinfo"
231 "Authorization information for IMAP servers. In .netrc format."
234 (repeat :tag "Entries"
237 :value ("" ("login" . "") ("password" . ""))
241 (const :format "" "login")
242 (string :format "Login: %v"))
244 (const :format "" "password")
245 (string :format "Password: %v")))))))
247 (defcustom nnimap-prune-cache t
248 "If non-nil, nnimap check whether articles still exist on server before using data stored in NOV cache."
251 (defvar nnimap-request-list-method 'imap-mailbox-list
252 "Method to use to request a list of all folders from the server.
253 If this is 'imap-mailbox-lsub, then use a server-side subscription list to
254 restrict visible folders.")
256 ;; Internal variables:
258 (defvar nnimap-debug nil);; "*nnimap-debug*")
259 (defvar nnimap-current-move-server nil)
260 (defvar nnimap-current-move-group nil)
261 (defvar nnimap-current-move-article nil)
262 (defvar nnimap-length)
263 (defvar nnimap-progress-chars '(?| ?/ ?- ?\\))
264 (defvar nnimap-progress-how-often 20)
265 (defvar nnimap-counter)
266 (defvar nnimap-callback-callback-function nil
267 "Gnus callback the nnimap asynchronous callback should call.")
268 (defvar nnimap-callback-buffer nil
269 "Which buffer the asynchronous article prefetch callback should work in.")
270 (defvar nnimap-server-buffer-alist nil) ;; Map server name to buffers.
271 (defvar nnimap-current-server nil) ;; Current server
272 (defvar nnimap-server-buffer nil) ;; Current servers' buffer
276 (nnoo-define-basics nnimap)
278 ;; Utility functions:
280 (defsubst nnimap-get-server-buffer (server)
281 "Return buffer for SERVER, if nil use current server."
282 (cadr (assoc (or server nnimap-current-server) nnimap-server-buffer-alist)))
284 (defun nnimap-possibly-change-server (server)
285 "Return buffer for SERVER, changing the current server as a side-effect.
286 If SERVER is nil, uses the current server."
287 (setq nnimap-current-server (or server nnimap-current-server)
288 nnimap-server-buffer (nnimap-get-server-buffer nnimap-current-server)))
290 (defun nnimap-verify-uidvalidity (group server)
291 "Verify stored uidvalidity match current one in GROUP on SERVER."
292 (let* ((gnusgroup (gnus-group-prefixed-name
293 group (gnus-server-to-method
294 (format "nnimap:%s" server))))
295 (new-uidvalidity (imap-mailbox-get 'uidvalidity))
296 (old-uidvalidity (gnus-group-get-parameter gnusgroup 'uidvalidity)))
298 (if (not (equal old-uidvalidity new-uidvalidity))
299 nil ;; uidvalidity clash
300 (gnus-group-set-parameter gnusgroup 'uidvalidity new-uidvalidity)
302 (gnus-group-add-parameter gnusgroup (cons 'uidvalidity new-uidvalidity))
305 (defun nnimap-find-minmax-uid (group &optional examine)
306 "Find lowest and highest active article nummber in GROUP.
307 If EXAMINE is non-nil the group is selected read-only."
308 (with-current-buffer nnimap-server-buffer
309 (when (imap-mailbox-select group examine)
311 (when (> (imap-mailbox-get 'exists) 0)
312 (imap-fetch "1,*" "UID" nil 'nouidfetch)
313 (imap-message-map (lambda (uid Uid)
314 (setq minuid (if minuid (min minuid uid) uid)
315 maxuid (if maxuid (max maxuid uid) uid)))
317 (list (imap-mailbox-get 'exists) minuid maxuid)))))
319 (defun nnimap-possibly-change-group (group &optional server)
320 "Make GROUP the current group, and SERVER the current server."
321 (when (nnimap-possibly-change-server server)
322 (with-current-buffer nnimap-server-buffer
323 (if (or (null group) (imap-current-mailbox-p group))
325 (if (imap-mailbox-select group)
326 (if (or (nnimap-verify-uidvalidity
327 group (or server nnimap-current-server))
328 (zerop (imap-mailbox-get 'exists group))
331 "nnimap: Group %s is not uidvalid. Continue? " group)))
333 (imap-mailbox-unselect)
334 (error "nnimap: Group %s is not uid-valid." group))
335 (nnheader-report 'nnimap (imap-error-text)))))))
337 (defun nnimap-replace-whitespace (string)
338 "Return STRING with all whitespace replaced with space."
340 (while (string-match "[\r\n\t]+" string)
341 (setq string (replace-match " " t t string)))
344 ;; Required backend functions
346 (defun nnimap-retrieve-headers-progress ()
347 "Hook to insert NOV line for current article into `nntp-server-buffer'."
348 (and (numberp nnmail-large-newsgroup)
349 (zerop (% (incf nnimap-counter) nnimap-progress-how-often))
350 (> nnimap-length nnmail-large-newsgroup)
351 (nnheader-message 6 "nnimap: Retrieving headers... %c"
352 (nth (/ (% nnimap-counter
353 (* (length nnimap-progress-chars)
354 nnimap-progress-how-often))
355 nnimap-progress-how-often)
356 nnimap-progress-chars)))
357 (with-current-buffer nntp-server-buffer
359 (with-current-buffer nnimap-server-buffer
360 (vector imap-current-message
361 (nnimap-replace-whitespace
362 (imap-message-envelope-subject imap-current-message))
363 (nnimap-replace-whitespace
365 (car-safe (imap-message-envelope-from
366 imap-current-message))))
367 (nnimap-replace-whitespace
368 (imap-message-envelope-date imap-current-message))
369 (nnimap-replace-whitespace
370 (imap-message-envelope-message-id imap-current-message))
371 (nnimap-replace-whitespace
372 (let ((str (if (imap-capability 'IMAP4rev1)
374 "HEADER.FIELDS REFERENCES"
376 imap-current-message 'BODYDETAIL)))
377 (imap-message-get imap-current-message
379 (if (> (length str) (length "References: "))
380 (substring str (length "References: "))
381 (if (and (setq str (imap-message-envelope-in-reply-to
382 imap-current-message))
383 (string-match "<[^>]+>" str))
384 (substring str (match-beginning 0) (match-end 0))))))
385 (imap-message-get imap-current-message 'RFC822.SIZE)
386 (imap-body-lines (imap-message-body imap-current-message))
388 nil)))));; extra-headers
390 (defun nnimap-retrieve-which-headers (articles fetch-old)
391 "Get a range of articles to fetch based on ARTICLES and FETCH-OLD."
392 (with-current-buffer nnimap-server-buffer
393 (if (numberp (car-safe articles))
396 (nnimap-range-to-string
397 (gnus-compress-sequence
398 (append (gnus-uncompress-sequence
400 (cons (if (numberp fetch-old)
401 (max 1 (- (car articles) fetch-old))
403 (1- (car articles)))))
405 (mapcar (lambda (msgid)
407 (format "HEADER Message-Id %s" msgid)))
410 (defun nnimap-group-overview-filename (group server)
411 "Make pathname for GROUP on SERVER."
412 (let ((dir (file-name-as-directory (expand-file-name nnimap-directory)))
413 (file (nnheader-translate-file-chars
414 (concat nnimap-nov-file-name
415 (if (equal server "")
417 server) "." group nnimap-nov-file-name-suffix) t)))
418 (if (or nnmail-use-long-file-names
419 (file-exists-p (concat dir file)))
421 (concat dir (mm-encode-coding-string
422 (nnheader-replace-chars-in-string file ?. ?/)
423 nnmail-pathname-coding-system)))))
425 (defun nnimap-retrieve-headers-from-file (group server)
426 (with-current-buffer nntp-server-buffer
427 (let ((nov (nnimap-group-overview-filename group server)))
428 (when (file-exists-p nov)
429 (mm-insert-file-contents nov)
430 (set-buffer-modified-p nil)
431 (let ((min (progn (goto-char (point-min))
433 (read (current-buffer)))))
434 (max (progn (goto-char (point-max))
437 (read (current-buffer))))))
438 (if (and (numberp min) (numberp max))
440 ;; junk, remove it, it's saved later
444 (defun nnimap-retrieve-headers-from-server (articles group server)
445 (with-current-buffer nnimap-server-buffer
446 (let ((imap-fetch-data-hook '(nnimap-retrieve-headers-progress))
447 (nnimap-length (gnus-range-length articles))
449 (imap-fetch (nnimap-range-to-string articles)
450 (concat "(UID RFC822.SIZE ENVELOPE BODY "
451 (if (imap-capability 'IMAP4rev1)
452 "BODY.PEEK[HEADER.FIELDS (References)])"
453 "RFC822.HEADER.LINES (References))")))
454 (and (numberp nnmail-large-newsgroup)
455 (> nnimap-length nnmail-large-newsgroup)
456 (nnheader-message 6 "nnimap: Retrieving headers...done")))))
458 (defun nnimap-use-nov-p (group server)
459 (or gnus-nov-is-evil nnimap-nov-is-evil
460 (unless (and (gnus-make-directory
462 (nnimap-group-overview-filename group server)))
464 (nnimap-group-overview-filename group server)))
465 (message "nnimap: Nov cache not writable, %s"
466 (nnimap-group-overview-filename group server)))))
468 (deffoo nnimap-retrieve-headers (articles &optional group server fetch-old)
469 (when (nnimap-possibly-change-group group server)
470 (with-current-buffer nntp-server-buffer
472 (if (nnimap-use-nov-p group server)
473 (nnimap-retrieve-headers-from-server
474 (gnus-compress-sequence articles) group server)
475 (let (uids cached low high)
476 (when (setq uids (nnimap-retrieve-which-headers articles fetch-old)
478 high (car (last uids)))
479 (if (setq cached (nnimap-retrieve-headers-from-file group server))
481 ;; fetch articles with uids before cache block
482 (when (< low (car cached))
483 (goto-char (point-min))
484 (nnimap-retrieve-headers-from-server
485 (cons low (1- (car cached))) group server))
486 ;; fetch articles with uids after cache block
487 (when (> high (cdr cached))
488 (goto-char (point-max))
489 (nnimap-retrieve-headers-from-server
490 (cons (1+ (cdr cached)) high) group server))
491 (when nnimap-prune-cache
492 ;; remove nov's for articles which has expired on server
493 (goto-char (point-min))
494 (dolist (uid (gnus-set-difference articles uids))
495 (when (re-search-forward (format "^%d\t" uid) nil t)
496 (gnus-delete-line)))))
497 ;; nothing cached, fetch whole range from server
498 (nnimap-retrieve-headers-from-server
499 (cons low high) group server))
500 (when (buffer-modified-p)
502 1 (point-max) (nnimap-group-overview-filename group server)
504 (nnheader-nov-delete-outside-range low high))))
507 (defun nnimap-open-connection (server)
508 (if (not (imap-open nnimap-address nnimap-server-port nnimap-stream
509 nnimap-authenticator nnimap-server-buffer))
510 (nnheader-report 'nnimap "Can't open connection to server %s" server)
511 (unless (or (imap-capability 'IMAP4 nnimap-server-buffer)
512 (imap-capability 'IMAP4rev1 nnimap-server-buffer))
513 (imap-close nnimap-server-buffer)
514 (nnheader-report 'nnimap "Server %s is not IMAP4 compliant" server))
515 (let (list alist user passwd)
516 (and (fboundp 'gnus-parse-netrc)
517 (setq list (gnus-parse-netrc nnimap-authinfo-file)
518 alist (or (and (gnus-netrc-get
519 (gnus-netrc-machine list server) "machine")
520 (gnus-netrc-machine list server))
521 (gnus-netrc-machine list nnimap-address))
522 user (gnus-netrc-get alist "login")
523 passwd (gnus-netrc-get alist "password")))
524 (if (imap-authenticate user passwd nnimap-server-buffer)
526 (push (list server nnimap-server-buffer)
527 nnimap-server-buffer-alist)
528 (nnimap-possibly-change-server server))
529 (imap-close nnimap-server-buffer)
530 (kill-buffer nnimap-server-buffer)
531 (nnheader-report 'nnimap "Could not authenticate to %s" server)))))
533 (deffoo nnimap-open-server (server &optional defs)
534 (nnheader-init-server-buffer)
535 (if (nnimap-server-opened server)
537 (unless (assq 'nnimap-server-buffer defs)
538 (push (list 'nnimap-server-buffer (concat " *nnimap* " server)) defs))
539 ;; translate `nnimap-server-address' to `nnimap-address' in defs
540 ;; for people that configured nnimap with a very old version
541 (unless (assq 'nnimap-address defs)
542 (if (assq 'nnimap-server-address defs)
543 (push (list 'nnimap-address
544 (cadr (assq 'nnimap-server-address defs))) defs)
545 (push (list 'nnimap-address server) defs)))
546 (nnoo-change-server 'nnimap server defs)
547 (or (and nnimap-server-buffer
548 (imap-opened nnimap-server-buffer))
549 (nnimap-open-connection server))))
551 (deffoo nnimap-server-opened (&optional server)
552 "Whether SERVER is opened.
553 If SERVER is the current virtual server, and the connection to the
554 physical server is alive, this function return a non-nil value. If
555 SERVER is nil, it is treated as the current server."
556 ;; clean up autologouts??
557 (and (or server nnimap-current-server)
558 (nnoo-server-opened 'nnimap (or server nnimap-current-server))
559 (imap-opened (nnimap-get-server-buffer server))))
561 (deffoo nnimap-close-server (&optional server)
562 "Close connection to server and free all resources connected to it.
563 Return nil if the server couldn't be closed for some reason."
564 (let ((server (or server nnimap-current-server)))
565 (when (or (nnimap-server-opened server)
566 (imap-opened (nnimap-get-server-buffer server)))
567 (imap-close (nnimap-get-server-buffer server))
568 (kill-buffer (nnimap-get-server-buffer server))
569 (setq nnimap-server-buffer nil
570 nnimap-current-server nil
571 nnimap-server-buffer-alist
572 (delq server nnimap-server-buffer-alist)))
573 (nnoo-close-server 'nnimap server)))
575 (deffoo nnimap-request-close ()
576 "Close connection to all servers and free all resources that the backend have reserved.
577 All buffers that have been created by that
578 backend should be killed. (Not the nntp-server-buffer, though.) This
579 function is generally only called when Gnus is shutting down."
580 (mapcar (lambda (server) (nnimap-close-server (car server)))
581 nnimap-server-buffer-alist)
582 (setq nnimap-server-buffer-alist nil))
584 (deffoo nnimap-status-message (&optional server)
585 "This function returns the last error message from server."
586 (when (nnimap-possibly-change-server server)
587 (nnoo-status-message 'nnimap server)))
589 (defun nnimap-demule (string)
590 (funcall (if (and (fboundp 'string-as-multibyte)
591 (subrp (symbol-function 'string-as-multibyte)))
596 (defun nnimap-callback ()
597 (remove-hook 'imap-fetch-data-hook 'nnimap-callback)
598 (with-current-buffer nnimap-callback-buffer
600 (with-current-buffer nnimap-server-buffer
601 (nnimap-demule (imap-message-get (imap-current-message) 'RFC822)))) ;xxx
602 (nnheader-ms-strip-cr)
603 (funcall nnimap-callback-callback-function t)))
605 (defun nnimap-request-article-part (article part prop
606 &optional group server to-buffer)
607 (when (nnimap-possibly-change-group group server)
608 (let ((article (if (stringp article)
609 (car-safe (imap-search
610 (format "HEADER Message-Id %s" article)
611 nnimap-server-buffer))
614 (gnus-message 9 "nnimap: Fetching (part of) article %d..." article)
615 (if (not nnheader-callback-function)
616 (with-current-buffer (or to-buffer nntp-server-buffer)
618 (insert (nnimap-demule (imap-fetch article part prop nil
619 nnimap-server-buffer)))
620 (nnheader-ms-strip-cr)
621 (gnus-message 9 "nnimap: Fetching (part of) article %d...done"
624 (nnheader-report 'nnimap "No such article: %s"
625 (imap-error-text nnimap-server-buffer))
626 (cons group article)))
627 (add-hook 'imap-fetch-data-hook 'nnimap-callback)
628 (setq nnimap-callback-callback-function nnheader-callback-function
629 nnimap-callback-buffer nntp-server-buffer)
630 (imap-fetch-asynch article part nil nnimap-server-buffer)
631 (cons group article))))))
633 (deffoo nnimap-asynchronous-p ()
636 (deffoo nnimap-request-article (article &optional group server to-buffer)
637 (nnimap-request-article-part
638 article "RFC822.PEEK" 'RFC822 group server to-buffer))
640 (deffoo nnimap-request-head (article &optional group server to-buffer)
641 (nnimap-request-article-part
642 article "RFC822.HEADER" 'RFC822.HEADER group server to-buffer))
644 (deffoo nnimap-request-body (article &optional group server to-buffer)
645 (nnimap-request-article-part
646 article "RFC822.TEXT.PEEK" 'RFC822.TEXT group server to-buffer))
648 (deffoo nnimap-request-group (group &optional server fast)
649 (nnimap-request-update-info-internal
651 (gnus-get-info (gnus-group-prefixed-name
652 group (gnus-server-to-method (format "nnimap:%s" server))))
654 (when (nnimap-possibly-change-group group server)
657 ((null (setq info (nnimap-find-minmax-uid group t)))
658 (nnheader-report 'nnimap "Could not get active info for %s"
661 (nnheader-insert "211 %d %d %d %s\n" (or (nth 0 info) 0)
662 (max 1 (or (nth 1 info) 1))
663 (or (nth 2 info) 0) group)
664 (nnheader-report 'nnimap "Group %s selected" group)
667 (defun nnimap-close-group (group &optional server)
668 (with-current-buffer nnimap-server-buffer
669 (when (and (imap-opened)
670 (nnimap-possibly-change-group group server))
671 (case nnimap-expunge-on-close
672 ('always (imap-mailbox-expunge)
673 (imap-mailbox-close))
674 ('ask (if (and (imap-search "DELETED")
675 (gnus-y-or-n-p (format
676 "Expunge articles in group `%s'? "
677 imap-current-mailbox)))
678 (progn (imap-mailbox-expunge)
679 (imap-mailbox-close))
680 (imap-mailbox-unselect)))
681 (t (imap-mailbox-unselect)))
682 (not imap-current-mailbox))))
684 (defun nnimap-pattern-to-list-arguments (pattern)
686 (cons (car-safe p) (or (cdr-safe p) p)))
687 (if (and (listp pattern)
688 (listp (cdr pattern)))
692 (deffoo nnimap-request-list (&optional server)
693 (when (nnimap-possibly-change-server server)
694 (with-current-buffer nntp-server-buffer
696 (gnus-message 5 "nnimap: Generating active list%s..."
697 (if (> (length server) 0) (concat " for " server) ""))
698 (with-current-buffer nnimap-server-buffer
699 (dolist (pattern (nnimap-pattern-to-list-arguments nnimap-list-pattern))
700 (dolist (mbx (funcall nnimap-request-list-method
701 (cdr pattern) (car pattern)))
702 (or (member "\\NoSelect" (imap-mailbox-get 'list-flags mbx))
703 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
705 (with-current-buffer nntp-server-buffer
706 (insert (format "\"%s\" %d %d y\n"
707 mbx (or (nth 2 info) 0)
708 (max 1 (or (nth 1 info) 1)))))))))))
709 (gnus-message 5 "nnimap: Generating active list%s...done"
710 (if (> (length server) 0) (concat " for " server) ""))
713 (deffoo nnimap-request-post (&optional server)
715 (dolist (mbx (message-tokenize-header
716 (message-fetch-field "Newsgroups")) success)
717 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
718 (or (gnus-active to-newsgroup)
719 (gnus-activate-group to-newsgroup)
720 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
722 (or (and (gnus-request-create-group
723 to-newsgroup gnus-command-method)
724 (gnus-activate-group to-newsgroup nil nil
725 gnus-command-method))
726 (error "Couldn't create group %s" to-newsgroup)))
727 (error "No such group: %s" to-newsgroup))
728 (unless (nnimap-request-accept-article mbx (nth 1 gnus-command-method))
729 (setq success nil))))))
731 ;; Optional backend functions
733 (deffoo nnimap-retrieve-groups (groups &optional server)
734 (when (nnimap-possibly-change-server server)
735 (gnus-message 5 "nnimap: Checking mailboxes...")
736 (with-current-buffer nntp-server-buffer
738 (dolist (group groups)
739 (gnus-message 7 "nnimap: Checking mailbox %s" group)
740 (or (member "\\NoSelect"
741 (imap-mailbox-get 'list-flags group nnimap-server-buffer))
742 (let ((info (nnimap-find-minmax-uid group 'examine)))
743 (insert (format "\"%s\" %d %d y\n" group
745 (max 1 (or (nth 1 info) 1))))))))
746 (gnus-message 5 "nnimap: Checking mailboxes...done")
749 (deffoo nnimap-request-update-info-internal (group info &optional server)
750 (when (nnimap-possibly-change-group group server)
751 (when info;; xxx what does this mean? should we create a info?
752 (with-current-buffer nnimap-server-buffer
753 (gnus-message 5 "nnimap: Updating info for %s..."
754 (gnus-info-group info))
756 (when (nnimap-mark-permanent-p 'read)
758 ;; read info could contain articles marked unread by other
759 ;; imap clients! we correct this
760 (setq seen (gnus-uncompress-range (gnus-info-read info))
761 unseen (imap-search "UNSEEN UNDELETED")
762 seen (gnus-set-difference seen unseen)
763 ;; seen might lack articles marked as read by other
764 ;; imap clients! we correct this
765 seen (append seen (imap-search "SEEN"))
768 seen (gnus-compress-sequence seen t)
769 ;; we can't return '(1) since this isn't a "list of ranges",
770 ;; and we can't return '((1)) since g-list-of-unread-articles
771 ;; is buggy so we return '((1 . 1)).
772 seen (if (and (integerp (car seen))
774 (list (cons (car seen) (car seen)))
776 (gnus-info-set-read info seen)))
779 (when (and (nnimap-mark-permanent-p (cdr pred))
780 (member (nnimap-mark-to-flag (cdr pred))
781 (imap-mailbox-get 'flags)))
784 (nnimap-update-alist-soft
786 (gnus-compress-sequence
787 (imap-search (nnimap-mark-to-predicate (cdr pred))))
788 (gnus-info-marks info))
790 gnus-article-mark-lists)
792 (gnus-message 5 "nnimap: Updating info for %s...done"
793 (gnus-info-group info))
797 (deffoo nnimap-request-type (group &optional article)
798 (if (and nnimap-news-groups (string-match nnimap-news-groups group))
802 (deffoo nnimap-request-set-mark (group actions &optional server)
803 (when (nnimap-possibly-change-group group server)
804 (with-current-buffer nnimap-server-buffer
806 (gnus-message 7 "nnimap: Setting marks in %s..." group)
807 (while (setq action (pop actions))
808 (let ((range (nth 0 action))
809 (what (nth 1 action))
810 (cmdmarks (nth 2 action))
812 ;; cache flags are pointless on the server
813 (setq cmdmarks (delq 'cache cmdmarks))
814 ;; flag dormant articles as ticked
815 (if (memq 'dormant cmdmarks)
816 (setq cmdmarks (cons 'tick cmdmarks)))
817 ;; remove stuff we are forbidden to store
818 (mapcar (lambda (mark)
819 (if (imap-message-flag-permanent-p
820 (nnimap-mark-to-flag mark))
821 (setq marks (cons mark marks))))
823 (when (and range marks)
824 (cond ((eq what 'del)
825 (imap-message-flags-del
826 (nnimap-range-to-string range)
827 (nnimap-mark-to-flag marks nil t)))
829 (imap-message-flags-add
830 (nnimap-range-to-string range)
831 (nnimap-mark-to-flag marks nil t)))
833 (imap-message-flags-set
834 (nnimap-range-to-string range)
835 (nnimap-mark-to-flag marks nil t)))))))
836 (gnus-message 7 "nnimap: Setting marks in %s...done" group))))
839 (defun nnimap-split-to-groups (rules)
840 ;; tries to match all rules in nnimap-split-rule against content of
841 ;; nntp-server-buffer, returns a list of groups that matched.
842 (with-current-buffer nntp-server-buffer
843 ;; Fold continuation lines.
844 (goto-char (point-min))
845 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
846 (replace-match " " t t))
847 (if (functionp rules)
849 (let (to-groups regrepp)
851 (dolist (rule rules to-groups)
852 (let ((group (car rule))
853 (regexp (cadr rule)))
854 (goto-char (point-min))
855 (when (and (if (stringp regexp)
857 (setq regrepp (string-match "\\\\[0-9&]" group))
858 (re-search-forward regexp nil t))
859 (funcall regexp group))
860 ;; Don't enter the article into the same group twice.
861 (not (assoc group to-groups)))
863 (nnmail-expand-newtext group)
866 (or nnimap-split-crosspost
867 (throw 'split-done to-groups))))))))))
869 (defun nnimap-split-find-rule (server inbox)
872 (defun nnimap-split-find-inbox (server)
873 (if (listp nnimap-split-inbox)
875 (list nnimap-split-inbox)))
877 (defun nnimap-split-articles (&optional group server)
878 (when (nnimap-possibly-change-server server)
879 (with-current-buffer nnimap-server-buffer
880 (let (rule inbox removeorig (inboxes (nnimap-split-find-inbox server)))
881 ;; iterate over inboxes
882 (while (and (setq inbox (pop inboxes))
883 (nnimap-possibly-change-group inbox));; SELECT
884 ;; find split rule for this server / inbox
885 (when (setq rule (nnimap-split-find-rule server inbox))
886 ;; iterate over articles
887 (dolist (article (imap-search "UNSEEN UNDELETED"))
888 (when (nnimap-request-head article)
889 ;; copy article to right group(s)
890 (setq removeorig nil)
891 (dolist (to-group (nnimap-split-to-groups rule))
892 (if (imap-message-copy (number-to-string article)
893 to-group nil 'nocopyuid)
895 (message "IMAP split moved %s:%s:%d to %s" server inbox
898 ;; Add the group-art list to the history list.
899 (push (list (cons to-group 0)) nnmail-split-history))
900 (message "IMAP split failed to move %s:%s:%d to %s" server
901 inbox article to-group)))
902 ;; remove article if it was successfully copied somewhere
904 (imap-message-flags-add (format "%d" article)
905 "\\Seen \\Deleted")))))
906 (when (imap-mailbox-select inbox);; just in case
907 ;; todo: UID EXPUNGE (if available) to remove splitted articles
908 (imap-mailbox-expunge)
909 (imap-mailbox-close)))
912 (deffoo nnimap-request-scan (&optional group server)
913 (nnimap-split-articles group server))
915 (deffoo nnimap-request-newgroups (date &optional server)
916 (when (nnimap-possibly-change-server server)
917 (with-current-buffer nntp-server-buffer
918 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s..."
919 (if (> (length server) 0) " on " "") server)
921 (dolist (pattern (nnimap-pattern-to-list-arguments
922 nnimap-list-pattern))
923 (dolist (mbx (imap-mailbox-lsub "*" (car pattern) nil
924 nnimap-server-buffer))
925 (or (member-if (lambda (mailbox)
926 (string= (downcase mailbox) "\\noselect"))
927 (imap-mailbox-get 'list-flags mbx
928 nnimap-server-buffer))
929 (let ((info (nnimap-find-minmax-uid mbx 'examine)))
931 (insert (format "\"%s\" %d %d y\n"
932 mbx (or (nth 2 info) 0)
933 (max 1 (or (nth 1 info) 1)))))))))
934 (gnus-message 5 "nnimap: Listing subscribed mailboxes%s%s...done"
935 (if (> (length server) 0) " on " "") server))
938 (deffoo nnimap-request-create-group (group &optional server args)
939 (when (nnimap-possibly-change-server server)
940 (or (imap-mailbox-status group 'uidvalidity nnimap-server-buffer)
941 (imap-mailbox-create group nnimap-server-buffer))))
943 (defun nnimap-time-substract (time1 time2)
944 "Return TIME for TIME1 - TIME2."
945 (let* ((ms (- (car time1) (car time2)))
946 (ls (- (nth 1 time1) (nth 1 time2))))
948 (list (- ms 1) (+ (expt 2 16) ls))
951 (defun nnimap-date-days-ago (daysago)
952 "Return date, in format \"3-Aug-1998\", for DAYSAGO days ago."
953 (let ((date (format-time-string "%d-%b-%Y"
954 (nnimap-time-substract
956 (days-to-time daysago)))))
957 (if (eq ?0 (string-to-char date))
961 (defun nnimap-request-expire-articles-progress ()
962 (gnus-message 5 "nnimap: Marking article %d for deletion..."
963 imap-current-message))
965 ;; Notice that we don't actually delete anything, we just mark them deleted.
966 (deffoo nnimap-request-expire-articles (articles group &optional server force)
967 (let ((artseq (gnus-compress-sequence articles)))
968 (when (and artseq (nnimap-possibly-change-group group server))
969 (with-current-buffer nnimap-server-buffer
971 (and (imap-message-flags-add
972 (nnimap-range-to-string artseq) "\\Deleted")
974 (let ((days (or (and nnmail-expiry-wait-function
975 (funcall nnmail-expiry-wait-function group))
976 nnmail-expiry-wait)))
977 (cond ((eq days 'immediate)
978 (and (imap-message-flags-add
979 (nnimap-range-to-string artseq) "\\Deleted")
980 (setq articles nil)))
982 (let ((oldarts (imap-search
983 (format "UID %s NOT SINCE %s"
984 (nnimap-range-to-string artseq)
985 (nnimap-date-days-ago days))))
986 (imap-fetch-data-hook
987 '(nnimap-request-expire-articles-progress)))
989 (imap-message-flags-add
990 (nnimap-range-to-string
991 (gnus-compress-sequence oldarts))
993 (setq articles (gnus-set-difference
994 articles oldarts)))))))))))
995 ;; return articles not deleted
998 (deffoo nnimap-request-move-article (article group server
999 accept-form &optional last)
1000 (when (nnimap-possibly-change-server server)
1002 (let ((buf (get-buffer-create " *nnimap move*"))
1003 (nnimap-current-move-article article)
1004 (nnimap-current-move-group group)
1005 (nnimap-current-move-server nnimap-current-server)
1007 (and (nnimap-request-article article group server)
1010 (buffer-disable-undo (current-buffer))
1011 (insert-buffer-substring nntp-server-buffer)
1012 (setq result (eval accept-form))
1015 (nnimap-request-expire-articles (list article) group server t))
1018 (deffoo nnimap-request-accept-article (group &optional server last)
1019 (when (nnimap-possibly-change-server server)
1022 (if (string= nnimap-current-server nnimap-current-move-server)
1023 ;; moving article within same server, speed it up...
1024 (and (nnimap-possibly-change-group
1025 nnimap-current-move-group)
1026 (imap-message-copy (number-to-string
1027 nnimap-current-move-article)
1028 group 'dontcreate nil
1029 nnimap-server-buffer))
1030 ;; turn into rfc822 format (\r\n eol's)
1031 (with-current-buffer (current-buffer)
1032 (goto-char (point-min))
1033 (while (search-forward "\n" nil t)
1034 (replace-match "\r\n")))
1035 ;; next line for Cyrus server bug
1036 (imap-mailbox-unselect nnimap-server-buffer)
1037 (imap-message-append group (current-buffer) nil nil
1038 nnimap-server-buffer)))
1039 (cons group (nth 1 uid))
1040 (nnheader-report 'nnimap (imap-error-text nnimap-server-buffer))))))
1042 (deffoo nnimap-request-delete-group (group force &optional server)
1043 (when (nnimap-possibly-change-server server)
1044 (with-current-buffer nnimap-server-buffer
1046 (or (null (imap-mailbox-status group 'uidvalidity))
1047 (imap-mailbox-delete group))
1051 (deffoo nnimap-request-rename-group (group new-name &optional server)
1052 (when (nnimap-possibly-change-server server)
1053 (imap-mailbox-rename group new-name nnimap-server-buffer)))
1055 (defun nnimap-expunge (mailbox server)
1056 (when (nnimap-possibly-change-group mailbox server)
1057 (imap-mailbox-expunge nnimap-server-buffer)))
1059 (defun nnimap-acl-get (mailbox server)
1060 (when (nnimap-possibly-change-server server)
1061 (imap-mailbox-acl-get mailbox nnimap-server-buffer)))
1063 (defun nnimap-acl-edit (mailbox method old-acls new-acls)
1064 (when (nnimap-possibly-change-server (cadr method))
1065 (unless (imap-capability 'ACL nnimap-server-buffer)
1066 (error "Your server does not support ACL editing"))
1067 (with-current-buffer nnimap-server-buffer
1068 ;; delete all removed identifiers
1069 (mapcar (lambda (old-acl)
1070 (unless (assoc (car old-acl) new-acls)
1071 (or (imap-mailbox-acl-delete (car old-acl) mailbox)
1072 (error "Can't delete ACL for %s" (car old-acl)))))
1074 ;; set all changed acl's
1075 (mapcar (lambda (new-acl)
1076 (let ((new-rights (cdr new-acl))
1077 (old-rights (cdr (assoc (car new-acl) old-acls))))
1078 (unless (and old-rights new-rights
1079 (string= old-rights new-rights))
1080 (or (imap-mailbox-acl-set (car new-acl) new-rights mailbox)
1081 (error "Can't set ACL for %s to %s" (car new-acl)
1087 ;;; Internal functions
1090 ;; This is confusing.
1092 ;; mark => read, tick, draft, reply etc
1093 ;; flag => "\\Seen", "\\Flagged", "\\Draft", "gnus-expire" etc
1094 ;; predicate => "SEEN", "FLAGGED", "DRAFT", "KEYWORD gnus-expire" etc
1096 ;; Mark should not really contain 'read since it's not a "mark" in the Gnus
1097 ;; world, but we cheat. Mark == gnus-article-mark-lists + '(read . read).
1100 (defconst nnimap-mark-to-predicate-alist
1102 (lambda (pair) ; cdr is the mark
1103 (or (assoc (cdr pair)
1107 (reply . "ANSWERED")))
1109 (format "KEYWORD gnus-%s" (symbol-name (cdr pair))))))
1110 (cons '(read . read) gnus-article-mark-lists)))
1112 (defun nnimap-mark-to-predicate (pred)
1113 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP predicate.
1114 This is a string such as \"SEEN\", \"FLAGGED\", \"KEYWORD gnus-expire\",
1115 to be used within a IMAP SEARCH query."
1116 (cdr (assq pred nnimap-mark-to-predicate-alist)))
1118 (defconst nnimap-mark-to-flag-alist
1121 (or (assoc (cdr pair)
1123 (tick . "\\Flagged")
1125 (reply . "\\Answered")))
1127 (format "gnus-%s" (symbol-name (cdr pair))))))
1128 (cons '(read . read) gnus-article-mark-lists)))
1130 (defun nnimap-mark-to-flag-1 (preds)
1131 (if (and (not (null preds)) (listp preds))
1132 (cons (nnimap-mark-to-flag (car preds))
1133 (nnimap-mark-to-flag (cdr preds)))
1134 (cdr (assoc preds nnimap-mark-to-flag-alist))))
1136 (defun nnimap-mark-to-flag (preds &optional always-list make-string)
1137 "Convert a Gnus mark (a symbol such as read, tick, expire) to a IMAP flag.
1138 This is a string such as \"\\Seen\", \"\\Flagged\", \"gnus-expire\", to
1139 be used in a STORE FLAGS command."
1140 (let ((result (nnimap-mark-to-flag-1 preds)))
1141 (setq result (if (and (or make-string always-list)
1142 (not (listp result)))
1146 (mapconcat (lambda (flag)
1148 (mapconcat 'identity flag " ")
1153 (defun nnimap-mark-permanent-p (mark &optional group)
1154 "Return t iff MARK can be permanently (between IMAP sessions) saved on articles, in GROUP."
1155 (imap-message-flag-permanent-p (nnimap-mark-to-flag mark)))
1157 (defun nnimap-remassoc (key alist)
1158 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1159 The modified LIST is returned. If the first member
1160 of LIST has a car that is `equal' to KEY, there is no way to remove it
1161 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1162 sure of changing the value of `foo'."
1164 (if (equal key (caar alist))
1166 (setcdr alist (nnimap-remassoc key (cdr alist)))
1169 (defun nnimap-update-alist-soft (key value alist)
1171 (cons (cons key value) (nnimap-remassoc key alist))
1172 (nnimap-remassoc key alist)))
1174 (defun nnimap-range-to-string (range)
1179 (car item) (cdr item))
1180 (format "%d" item)))
1181 (if (and (listp range) (not (listp (cdr range))))
1182 (list range);; make (1 . 2) into ((1 . 2))
1188 (buffer-disable-undo (get-buffer-create nnimap-debug))
1189 (mapc (lambda (f) (trace-function-background f nnimap-debug))
1191 nnimap-possibly-change-server
1192 nnimap-verify-uidvalidity
1193 nnimap-find-minmax-uid
1194 nnimap-possibly-change-group
1195 ;;nnimap-replace-whitespace
1196 nnimap-retrieve-headers-progress
1197 nnimap-retrieve-which-headers
1198 nnimap-group-overview-filename
1199 nnimap-retrieve-headers-from-file
1200 nnimap-retrieve-headers-from-server
1201 nnimap-retrieve-headers
1202 nnimap-open-connection
1204 nnimap-server-opened
1206 nnimap-request-close
1207 nnimap-status-message
1209 nnimap-request-article-part
1210 nnimap-request-article
1213 nnimap-request-group
1215 nnimap-pattern-to-list-arguments
1218 nnimap-retrieve-groups
1219 nnimap-request-update-info-internal
1221 nnimap-request-set-mark
1222 nnimap-split-to-groups
1223 nnimap-split-find-rule
1224 nnimap-split-find-inbox
1225 nnimap-split-articles
1227 nnimap-request-newgroups
1228 nnimap-request-create-group
1229 nnimap-time-substract
1230 nnimap-date-days-ago
1231 nnimap-request-expire-articles-progress
1232 nnimap-request-expire-articles
1233 nnimap-request-move-article
1234 nnimap-request-accept-article
1235 nnimap-request-delete-group
1236 nnimap-request-rename-group
1237 gnus-group-nnimap-expunge
1238 gnus-group-nnimap-edit-acl
1239 gnus-group-nnimap-edit-acl-done
1240 nnimap-group-mode-hook
1241 nnimap-mark-to-predicate
1242 nnimap-mark-to-flag-1
1244 nnimap-mark-permanent-p
1246 nnimap-update-alist-soft
1247 nnimap-range-to-string
1252 ;;; nnimap.el ends here