1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
39 (if (featurep 'xemacs)
42 (require 'gnus-group))
45 (autoload 'gnus-server-update-server "gnus-srvr")
46 (autoload 'gnus-agent-customize-category "gnus-cus")
49 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
50 "Where the Gnus agent will store its files."
54 (defcustom gnus-agent-plugged-hook nil
55 "Hook run when plugging into the network."
59 (defcustom gnus-agent-unplugged-hook nil
60 "Hook run when unplugging from the network."
64 (defcustom gnus-agent-fetched-hook nil
65 "Hook run when finished fetching articles."
69 (defcustom gnus-agent-handle-level gnus-level-subscribed
70 "Groups on levels higher than this variable will be ignored by the Agent."
74 (defcustom gnus-agent-expire-days 7
75 "Read articles older than this will be expired.
76 This can also be a list of regexp/day pairs. The regexps will be
77 matched against group names."
79 :type '(choice (number :tag "days")
80 (sexp :tag "List" nil)))
82 (defcustom gnus-agent-expire-all nil
83 "If non-nil, also expire unread, ticked and dormant articles.
84 If nil, only read articles will be expired."
88 (defcustom gnus-agent-group-mode-hook nil
89 "Hook run in Agent group minor modes."
93 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
94 (when (featurep 'xemacs)
95 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
97 (defcustom gnus-agent-summary-mode-hook nil
98 "Hook run in Agent summary minor modes."
102 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
103 (when (featurep 'xemacs)
104 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
106 (defcustom gnus-agent-server-mode-hook nil
107 "Hook run in Agent summary minor modes."
111 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
112 (when (featurep 'xemacs)
113 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
115 (defcustom gnus-agent-confirmation-function 'y-or-n-p
116 "Function to confirm when error happens."
121 (defcustom gnus-agent-large-newsgroup nil
122 "*The number of articles which indicates a large newsgroup.
123 If the number of unread articles exceeds it, The number of articles to be
124 fetched will be limited to it. If not a positive integer, never consider it."
126 :type '(choice (const nil)
127 (integer :tag "Number")))
129 (defcustom gnus-agent-synchronize-flags 'ask
130 "Indicate if flags are synchronized when you plug in.
131 If this is `ask' the hook will query the user."
133 :type '(choice (const :tag "Always" t)
134 (const :tag "Never" nil)
135 (const :tag "Ask" ask))
138 (defcustom gnus-agent-go-online 'ask
139 "Indicate if offline servers go online when you plug in.
140 If this is `ask' the hook will query the user."
142 :type '(choice (const :tag "Always" t)
143 (const :tag "Never" nil)
144 (const :tag "Ask" ask))
147 (defcustom gnus-agent-mark-unread-after-downloaded t
148 "Indicate whether to mark articles unread after downloaded."
153 (defcustom gnus-agent-download-marks '(download)
154 "Marks for downloading."
156 :type '(repeat (symbol :tag "Mark"))
159 (defcustom gnus-agent-consider-all-articles nil
160 "If non-nil, consider also the read articles for downloading."
165 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
166 "Chunk size for `gnus-agent-fetch-session'.
167 The function will split its article fetches into chunks smaller than
172 (defcustom gnus-agent-enable-expiration 'ENABLE
173 "The default expiration state for each group.
174 When set to ENABLE, the default, `gnus-agent-expire' will expire old
175 contents from a group's local storage. This value may be overridden
176 to disable expiration in specific categories, topics, and groups. Of
177 course, you could change gnus-agent-enable-expiration to DISABLE then
178 enable expiration per categories, topics, and groups."
180 :type '(radio (const :format "Enable " ENABLE)
181 (const :format "Disable " DISABLE)))
183 ;;; Internal variables
185 (defvar gnus-agent-history-buffers nil)
186 (defvar gnus-agent-buffer-alist nil)
187 (defvar gnus-agent-article-alist nil
188 "An assoc list identifying the articles whose headers have been fetched.
189 If successfully fetched, these headers will be stored in the group's overview
190 file. The key of each assoc pair is the article ID, the value of each assoc
191 pair is a flag indicating whether the identified article has been downloaded
192 \(gnus-agent-fetch-articles sets the value to the day of the download).
194 1) The last element of this list can not be expired as some
195 routines (for example, get-agent-fetch-headers) use the last
196 value to track which articles have had their headers retrieved.
197 2) The function `gnus-agent-regenerate' may destructively modify the value.")
198 (defvar gnus-agent-group-alist nil)
199 (defvar gnus-category-alist nil)
200 (defvar gnus-agent-current-history nil)
201 (defvar gnus-agent-overview-buffer nil)
202 (defvar gnus-category-predicate-cache nil)
203 (defvar gnus-category-group-cache nil)
204 (defvar gnus-agent-spam-hashtb nil)
205 (defvar gnus-agent-file-name nil)
206 (defvar gnus-agent-send-mail-function nil)
207 (defvar gnus-agent-file-coding-system 'raw-text)
208 (defvar gnus-agent-file-loading-cache nil)
209 (defvar gnus-agent-file-header-cache nil)
211 (defvar gnus-agent-auto-agentize-methods '(nntp nnimap)
212 "Initially, all servers from these methods are agentized.
213 The user may remove or add servers using the Server buffer. See Info
214 node `(gnus)Server Buffer'.")
217 (defvar gnus-headers)
224 (defun gnus-open-agent ()
226 (gnus-agent-read-servers)
228 (gnus-agent-create-buffer)
229 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
230 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
231 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
233 (defun gnus-agent-create-buffer ()
234 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
236 (setq gnus-agent-overview-buffer
237 (gnus-get-buffer-create " *Gnus agent overview*"))
238 (with-current-buffer gnus-agent-overview-buffer
239 (set-buffer-multibyte t))
242 (gnus-add-shutdown 'gnus-close-agent 'gnus)
244 (defun gnus-close-agent ()
245 (setq gnus-category-predicate-cache nil
246 gnus-category-group-cache nil
247 gnus-agent-spam-hashtb nil)
248 (gnus-kill-buffer gnus-agent-overview-buffer))
251 ;;; Utility functions
254 (defun gnus-agent-read-file (file)
255 "Load FILE and do a `read' there."
258 (nnheader-insert-file-contents file)
259 (goto-char (point-min))
260 (read (current-buffer)))))
262 (defsubst gnus-agent-method ()
263 (concat (symbol-name (car gnus-command-method)) "/"
264 (if (equal (cadr gnus-command-method) "")
266 (cadr gnus-command-method))))
268 (defsubst gnus-agent-directory ()
269 "The name of the Gnus agent directory."
270 (nnheader-concat gnus-agent-directory
271 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
273 (defun gnus-agent-lib-file (file)
274 "The full name of the Gnus agent library FILE."
275 (expand-file-name file
276 (file-name-as-directory
277 (expand-file-name "agent.lib" (gnus-agent-directory)))))
279 (defun gnus-agent-cat-set-property (category property value)
281 (setcdr (or (assq property category)
282 (let ((cell (cons property nil)))
283 (setcdr category (cons cell (cdr category)))
285 (let ((category category))
286 (while (cond ((eq property (caadr category))
287 (setcdr category (cddr category))
290 (setq category (cdr category)))))))
293 (defmacro gnus-agent-cat-defaccessor (name prop-name)
294 "Define accessor and setter methods for manipulating a list of the form
295 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
296 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
297 manipulated as follows:
298 (func LIST): Returns VALUE1
299 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
300 `(progn (defmacro ,name (category)
301 (list (quote cdr) (list (quote assq)
302 (quote (quote ,prop-name)) category)))
304 (define-setf-method ,name (category)
305 (let* ((--category--temp-- (gensym "--category--"))
306 (--value--temp-- (gensym "--value--")))
307 (list (list --category--temp--) ; temporary-variables
308 (list category) ; value-forms
309 (list --value--temp--) ; store-variables
310 (let* ((category --category--temp--) ; store-form
311 (value --value--temp--))
312 (list (quote gnus-agent-cat-set-property)
314 (quote (quote ,prop-name))
316 (list (quote ,name) --category--temp--) ; access-form
319 (defmacro gnus-agent-cat-name (category)
322 (gnus-agent-cat-defaccessor
323 gnus-agent-cat-days-until-old agent-days-until-old)
324 (gnus-agent-cat-defaccessor
325 gnus-agent-cat-enable-expiration agent-enable-expiration)
326 (gnus-agent-cat-defaccessor
327 gnus-agent-cat-groups agent-groups)
328 (gnus-agent-cat-defaccessor
329 gnus-agent-cat-high-score agent-high-score)
330 (gnus-agent-cat-defaccessor
331 gnus-agent-cat-length-when-long agent-length-when-long)
332 (gnus-agent-cat-defaccessor
333 gnus-agent-cat-length-when-short agent-length-when-short)
334 (gnus-agent-cat-defaccessor
335 gnus-agent-cat-low-score agent-low-score)
336 (gnus-agent-cat-defaccessor
337 gnus-agent-cat-predicate agent-predicate)
338 (gnus-agent-cat-defaccessor
339 gnus-agent-cat-score-file agent-score-file)
341 (defsetf gnus-agent-cat-groups (category) (groups)
342 (list 'gnus-agent-set-cat-groups category groups))
344 (defun gnus-agent-set-cat-groups (category groups)
345 (unless (eq groups 'ignore)
347 (old-g (gnus-agent-cat-groups category)))
348 (cond ((eq new-g old-g)
349 ;; gnus-agent-add-group is fiddling with the group
350 ;; list. Still, Im done.
353 ((eq new-g (cdr old-g))
354 ;; gnus-agent-add-group is fiddling with the group list
355 (setcdr (or (assq 'agent-groups category)
356 (let ((cell (cons 'agent-groups nil)))
357 (setcdr category (cons cell (cdr category)))
360 (let ((groups groups))
362 (let* ((group (pop groups))
363 (old-category (gnus-group-category group)))
364 (if (eq category old-category)
366 (setf (gnus-agent-cat-groups old-category)
367 (delete group (gnus-agent-cat-groups
369 ;; Purge cache as preceeding loop invalidated it.
370 (setq gnus-category-group-cache nil))
372 (setcdr (or (assq 'agent-groups category)
373 (let ((cell (cons 'agent-groups nil)))
374 (setcdr category (cons cell (cdr category)))
377 (defsubst gnus-agent-cat-make (name)
378 (list name '(agent-predicate . false)))
380 ;;; Fetching setup functions.
382 (defun gnus-agent-start-fetch ()
383 "Initialize data structures for efficient fetching."
384 (gnus-agent-create-buffer))
386 (defun gnus-agent-stop-fetch ()
387 "Save all data structures and clean up."
388 (setq gnus-agent-spam-hashtb nil)
390 (set-buffer nntp-server-buffer)
393 (defmacro gnus-agent-with-fetch (&rest forms)
396 (let ((gnus-agent-fetching t))
397 (gnus-agent-start-fetch)
399 (gnus-agent-stop-fetch)))
401 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
402 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
404 (defmacro gnus-agent-append-to-list (tail value)
405 `(setq ,tail (setcdr ,tail (cons ,value nil))))
411 (defvar gnus-agent-mode-hook nil
412 "Hook run when installing agent mode.")
414 (defvar gnus-agent-mode nil)
415 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
417 (defun gnus-agent-mode ()
418 "Minor mode for providing a agent support in Gnus buffers."
419 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
420 (symbol-name major-mode))
421 (match-string 1 (symbol-name major-mode))))
422 (mode (intern (format "gnus-agent-%s-mode" buffer))))
423 (set (make-local-variable 'gnus-agent-mode) t)
425 (set (make-local-variable mode) t)
427 (when (gnus-visual-p 'agent-menu 'menu)
428 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
429 (unless (assq 'gnus-agent-mode minor-mode-alist)
430 (push gnus-agent-mode-status minor-mode-alist))
431 (unless (assq mode minor-mode-map-alist)
432 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
434 minor-mode-map-alist))
435 (when (eq major-mode 'gnus-group-mode)
436 (let ((init-plugged gnus-plugged))
437 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
438 ;; Therefore, make certain that the current value does not
439 ;; match the desired initial value.
440 (setq gnus-plugged :unknown)
441 (gnus-agent-toggle-plugged init-plugged)))
442 (gnus-run-hooks 'gnus-agent-mode-hook
443 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
445 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
446 (gnus-define-keys gnus-agent-group-mode-map
447 "Ju" gnus-agent-fetch-groups
448 "Jc" gnus-enter-category-buffer
449 "Jj" gnus-agent-toggle-plugged
450 "Js" gnus-agent-fetch-session
451 "JY" gnus-agent-synchronize-flags
452 "JS" gnus-group-send-queue
453 "Ja" gnus-agent-add-group
454 "Jr" gnus-agent-remove-group
455 "Jo" gnus-agent-toggle-group-plugged)
457 (defun gnus-agent-group-make-menu-bar ()
458 (unless (boundp 'gnus-agent-group-menu)
460 gnus-agent-group-menu gnus-agent-group-mode-map ""
462 ["Toggle plugged" gnus-agent-toggle-plugged t]
463 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
464 ["List categories" gnus-enter-category-buffer t]
465 ["Add (current) group to category" gnus-agent-add-group t]
466 ["Remove (current) group from category" gnus-agent-remove-group t]
467 ["Send queue" gnus-group-send-queue gnus-plugged]
469 ["All" gnus-agent-fetch-session gnus-plugged]
470 ["Group" gnus-agent-fetch-group gnus-plugged])
471 ["Synchronize flags" gnus-agent-synchronize-flags t]
474 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
475 (gnus-define-keys gnus-agent-summary-mode-map
476 "Jj" gnus-agent-toggle-plugged
477 "Ju" gnus-agent-summary-fetch-group
478 "JS" gnus-agent-fetch-group
479 "Js" gnus-agent-summary-fetch-series
480 "J#" gnus-agent-mark-article
481 "J\M-#" gnus-agent-unmark-article
482 "@" gnus-agent-toggle-mark
483 "Jc" gnus-agent-catchup)
485 (defun gnus-agent-summary-make-menu-bar ()
486 (unless (boundp 'gnus-agent-summary-menu)
488 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
490 ["Toggle plugged" gnus-agent-toggle-plugged t]
491 ["Mark as downloadable" gnus-agent-mark-article t]
492 ["Unmark as downloadable" gnus-agent-unmark-article t]
493 ["Toggle mark" gnus-agent-toggle-mark t]
494 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
495 ["Catchup undownloaded" gnus-agent-catchup t]))))
497 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
498 (gnus-define-keys gnus-agent-server-mode-map
499 "Jj" gnus-agent-toggle-plugged
500 "Ja" gnus-agent-add-server
501 "Jr" gnus-agent-remove-server)
503 (defun gnus-agent-server-make-menu-bar ()
504 (unless (boundp 'gnus-agent-server-menu)
506 gnus-agent-server-menu gnus-agent-server-mode-map ""
508 ["Toggle plugged" gnus-agent-toggle-plugged t]
509 ["Add" gnus-agent-add-server t]
510 ["Remove" gnus-agent-remove-server t]))))
512 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
513 (if (and (fboundp 'propertize)
514 (fboundp 'make-mode-line-mouse-map))
515 (propertize string 'local-map
516 (make-mode-line-mouse-map mouse-button mouse-func))
519 (defun gnus-agent-toggle-plugged (set-to)
520 "Toggle whether Gnus is unplugged or not."
521 (interactive (list (not gnus-plugged)))
522 (cond ((eq set-to gnus-plugged)
525 (setq gnus-plugged set-to)
526 (gnus-run-hooks 'gnus-agent-plugged-hook)
527 (setcar (cdr gnus-agent-mode-status)
528 (gnus-agent-make-mode-line-string " Plugged"
530 'gnus-agent-toggle-plugged))
531 (gnus-agent-go-online gnus-agent-go-online)
532 (gnus-agent-possibly-synchronize-flags))
534 (gnus-agent-close-connections)
535 (setq gnus-plugged set-to)
536 (gnus-run-hooks 'gnus-agent-unplugged-hook)
537 (setcar (cdr gnus-agent-mode-status)
538 (gnus-agent-make-mode-line-string " Unplugged"
540 'gnus-agent-toggle-plugged))))
541 (force-mode-line-update)
542 (set-buffer-modified-p t))
544 (defmacro gnus-agent-while-plugged (&rest body)
545 `(let ((original-gnus-plugged gnus-plugged))
547 (progn (gnus-agent-toggle-plugged t)
549 (gnus-agent-toggle-plugged original-gnus-plugged))))
551 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
552 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
554 (defun gnus-agent-close-connections ()
555 "Close all methods covered by the Gnus agent."
556 (let ((methods gnus-agent-covered-methods))
558 (gnus-close-server (pop methods)))))
561 (defun gnus-unplugged ()
562 "Start Gnus unplugged."
564 (setq gnus-plugged nil)
568 (defun gnus-plugged ()
569 "Start Gnus plugged."
571 (setq gnus-plugged t)
575 (defun gnus-slave-unplugged (&optional arg)
576 "Read news as a slave unplugged."
578 (setq gnus-plugged nil)
579 (gnus arg nil 'slave))
582 (defun gnus-agentize ()
583 "Allow Gnus to be an offline newsreader.
584 The normal usage of this command is to put the following as the
585 last form in your `.gnus.el' file:
589 This will modify the `gnus-setup-news-hook', and
590 `message-send-mail-real-function' variables, and install the Gnus agent
591 minor mode in all Gnus buffers."
594 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
595 (unless gnus-agent-send-mail-function
596 (setq gnus-agent-send-mail-function
597 (or message-send-mail-real-function
598 message-send-mail-function)
599 message-send-mail-real-function 'gnus-agent-send-mail))
601 (unless gnus-agent-covered-methods
604 (if (memq (car (gnus-server-to-method server))
605 gnus-agent-auto-agentize-methods)
606 (setq gnus-agent-covered-methods
607 (cons (gnus-server-to-method server)
608 gnus-agent-covered-methods ))))
609 (append (list gnus-select-method) gnus-secondary-select-methods))))
611 (defun gnus-agent-queue-setup ()
612 "Make sure the queue group exists."
613 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
614 (gnus-request-create-group "queue" '(nndraft ""))
615 (let ((gnus-level-default-subscribed 1))
616 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
617 (gnus-group-set-parameter
618 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
620 (defun gnus-agent-send-mail ()
622 (funcall gnus-agent-send-mail-function)
623 (goto-char (point-min))
625 (concat "^" (regexp-quote mail-header-separator) "\n"))
627 (gnus-agent-insert-meta-information 'mail)
628 (gnus-request-accept-article "nndraft:queue" nil t t)))
630 (defun gnus-agent-insert-meta-information (type &optional method)
631 "Insert meta-information into the message that says how it's to be posted.
632 TYPE can be either `mail' or `news'. If the latter, then METHOD can
635 (message-remove-header gnus-agent-meta-information-header)
636 (goto-char (point-min))
637 (insert gnus-agent-meta-information-header ": "
638 (symbol-name type) " " (format "%S" method)
641 (while (search-backward "\n" nil t)
642 (replace-match "\\n" t t))))
644 (defun gnus-agent-restore-gcc ()
645 "Restore GCC field from saved header."
647 (goto-char (point-min))
648 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
649 (replace-match "Gcc:" 'fixedcase))))
651 (defun gnus-agent-any-covered-gcc ()
653 (message-narrow-to-headers)
654 (let* ((gcc (mail-fetch-field "gcc" nil t))
656 (mapcar 'gnus-inews-group-method
657 (message-unquote-tokens
658 (message-tokenize-header
661 (while (and (not covered) methods)
662 (setq covered (gnus-agent-method-p (car methods))
663 methods (cdr methods)))
667 (defun gnus-agent-possibly-save-gcc ()
668 "Save GCC if Gnus is unplugged."
669 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
671 (goto-char (point-min))
672 (let ((case-fold-search t))
673 (while (re-search-forward "^gcc:" nil t)
674 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
676 (defun gnus-agent-possibly-do-gcc ()
677 "Do GCC if Gnus is plugged."
678 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
679 (gnus-inews-do-gcc)))
682 ;;; Group mode commands
685 (defun gnus-agent-fetch-groups (n)
686 "Put all new articles in the current groups into the Agent."
689 (error "Groups can't be fetched when Gnus is unplugged"))
690 (gnus-group-iterate n 'gnus-agent-fetch-group))
692 (defun gnus-agent-fetch-group (&optional group)
693 "Put all new articles in GROUP into the Agent."
694 (interactive (list (gnus-group-group-name)))
695 (setq group (or group gnus-newsgroup-name))
697 (error "No group on the current line"))
699 (gnus-agent-while-plugged
700 (let ((gnus-command-method (gnus-find-method-for-group group)))
701 (gnus-agent-with-fetch
702 (gnus-agent-fetch-group-1 group gnus-command-method)
703 (gnus-message 5 "Fetching %s...done" group)))))
705 (defun gnus-agent-add-group (category arg)
706 "Add the current group to an agent category."
712 (mapcar (lambda (cat) (list (symbol-name (car cat))))
716 (let ((cat (assq category gnus-category-alist))
718 (gnus-group-iterate arg
720 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
721 (setf (gnus-agent-cat-groups c)
722 (delete group (gnus-agent-cat-groups c))))
723 (push group groups)))
724 (setf (gnus-agent-cat-groups cat)
725 (nconc (gnus-agent-cat-groups cat) groups))
726 (gnus-category-write)))
728 (defun gnus-agent-remove-group (arg)
729 "Remove the current group from its agent category, if any."
732 (gnus-group-iterate arg
734 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
735 (setf (gnus-agent-cat-groups c)
736 (delete group (gnus-agent-cat-groups c))))))
737 (gnus-category-write)))
739 (defun gnus-agent-synchronize-flags ()
740 "Synchronize unplugged flags with servers."
743 (dolist (gnus-command-method gnus-agent-covered-methods)
744 (when (file-exists-p (gnus-agent-lib-file "flags"))
745 (gnus-agent-synchronize-flags-server gnus-command-method)))))
747 (defun gnus-agent-possibly-synchronize-flags ()
748 "Synchronize flags according to `gnus-agent-synchronize-flags'."
751 (dolist (gnus-command-method gnus-agent-covered-methods)
752 (when (file-exists-p (gnus-agent-lib-file "flags"))
753 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
755 (defun gnus-agent-synchronize-flags-server (method)
756 "Synchronize flags set when unplugged for server."
757 (let ((gnus-command-method method))
758 (when (file-exists-p (gnus-agent-lib-file "flags"))
759 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
761 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
762 (if (null (gnus-check-server gnus-command-method))
763 (gnus-message 1 "Couldn't open server %s" (nth 1 gnus-command-method))
765 (if (null (eval (read (current-buffer))))
767 (write-file (gnus-agent-lib-file "flags"))
768 (error "Couldn't set flags from file %s"
769 (gnus-agent-lib-file "flags"))))
770 (delete-file (gnus-agent-lib-file "flags")))
773 (defun gnus-agent-possibly-synchronize-flags-server (method)
774 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
775 (when (or (and gnus-agent-synchronize-flags
776 (not (eq gnus-agent-synchronize-flags 'ask)))
777 (and (eq gnus-agent-synchronize-flags 'ask)
778 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
780 (gnus-agent-synchronize-flags-server method)))
783 ;;; Server mode commands
786 (defun gnus-agent-add-server (server)
787 "Enroll SERVER in the agent program."
788 (interactive (list (gnus-server-server-name)))
790 (error "No server on the current line"))
791 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
792 (when (gnus-agent-method-p method)
793 (error "Server already in the agent program"))
794 (push method gnus-agent-covered-methods)
795 (gnus-server-update-server server)
796 (gnus-agent-write-servers)
797 (gnus-message 1 "Entered %s into the Agent" server)))
799 (defun gnus-agent-remove-server (server)
800 "Remove SERVER from the agent program."
801 (interactive (list (gnus-server-server-name)))
803 (error "No server on the current line"))
804 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
805 (unless (gnus-agent-method-p method)
806 (error "Server not in the agent program"))
807 (setq gnus-agent-covered-methods
808 (delete method gnus-agent-covered-methods))
809 (gnus-server-update-server server)
810 (gnus-agent-write-servers)
811 (gnus-message 1 "Removed %s from the agent" server)))
813 (defun gnus-agent-read-servers ()
814 "Read the alist of covered servers."
816 (let ((method (gnus-server-get-method
820 (unless (member method gnus-agent-covered-methods)
821 (push method gnus-agent-covered-methods))
822 (gnus-message 1 "Ignoring disappeared server `%s'" m)
824 (gnus-agent-read-file
825 (nnheader-concat gnus-agent-directory "lib/servers"))))
827 (defun gnus-agent-write-servers ()
828 "Write the alist of covered servers."
829 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
830 (let ((coding-system-for-write nnheader-file-coding-system)
831 (output-coding-system nnheader-file-coding-system)
832 (file-name-coding-system nnmail-pathname-coding-system)
833 (pathname-coding-system nnmail-pathname-coding-system))
834 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
835 (prin1 (mapcar 'gnus-method-simplify gnus-agent-covered-methods)
842 (defun gnus-agent-mark-article (n &optional unmark)
843 "Mark the next N articles as downloadable.
844 If N is negative, mark backward instead. If UNMARK is non-nil, remove
845 the mark instead. The difference between N and the actual number of
846 articles marked is returned."
848 (let ((backward (< n 0))
853 (gnus-summary-set-agent-mark
854 (gnus-summary-article-number) unmark)
855 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
858 (gnus-message 7 "No more articles"))
859 (gnus-summary-recenter)
860 (gnus-summary-position-point)
863 (defun gnus-agent-unmark-article (n)
864 "Remove the downloadable mark from the next N articles.
865 If N is negative, unmark backward instead. The difference between N and
866 the actual number of articles unmarked is returned."
868 (gnus-agent-mark-article n t))
870 (defun gnus-agent-toggle-mark (n)
871 "Toggle the downloadable mark from the next N articles.
872 If N is negative, toggle backward instead. The difference between N and
873 the actual number of articles toggled is returned."
875 (gnus-agent-mark-article n 'toggle))
877 (defun gnus-summary-set-agent-mark (article &optional unmark)
878 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
879 When UNMARK is t, the article is unmarked. For any other value, the
880 article's mark is toggled."
881 (let ((unmark (cond ((eq nil unmark)
886 (memq article gnus-newsgroup-downloadable)))))
887 (when (gnus-summary-goto-subject article nil t)
888 (gnus-summary-update-mark
891 (setq gnus-newsgroup-downloadable
892 (delq article gnus-newsgroup-downloadable))
893 (gnus-article-mark article))
895 (setq gnus-newsgroup-downloadable
896 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
897 gnus-downloadable-mark)
901 (defun gnus-agent-get-undownloaded-list ()
902 "Construct list of articles that have not been downloaded."
903 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
904 (when (set (make-local-variable 'gnus-newsgroup-agentized)
905 (gnus-agent-method-p gnus-command-method))
906 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
907 (headers (sort (mapcar (lambda (h)
908 (mail-header-number h))
909 gnus-newsgroup-headers) '<))
910 (undownloaded (list nil))
911 (tail-undownloaded undownloaded)
912 (unfetched (list nil))
913 (tail-unfetched unfetched))
914 (while (and alist headers)
915 (let ((a (caar alist))
918 ;; Ignore IDs in the alist that are not being
919 ;; displayed in the summary.
922 ;; Headers that are not in the alist should be
923 ;; fictious (see nnagent-retrieve-headers); they
924 ;; imply that this article isn't in the agent.
925 (gnus-agent-append-to-list tail-undownloaded h)
926 (gnus-agent-append-to-list tail-unfetched h)
931 nil ; ignore already downloaded
936 (gnus-agent-append-to-list tail-undownloaded a)))))
939 (let ((num (pop headers)))
940 (gnus-agent-append-to-list tail-undownloaded num)
941 (gnus-agent-append-to-list tail-unfetched num)))
943 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
944 gnus-newsgroup-unfetched (cdr unfetched))))))
946 (defun gnus-agent-catchup ()
947 "Mark as read all unhandled articles.
948 An article is unhandled if it is neither cached, nor downloaded, nor
952 (let ((articles gnus-newsgroup-undownloaded))
953 (when (or gnus-newsgroup-downloadable
954 gnus-newsgroup-cached)
955 (setq articles (gnus-sorted-ndifference
956 (gnus-sorted-ndifference
957 (gnus-copy-sequence articles)
958 gnus-newsgroup-downloadable)
959 gnus-newsgroup-cached)))
962 (gnus-summary-mark-article
963 (pop articles) gnus-catchup-mark)))
964 (gnus-summary-position-point)))
966 (defun gnus-agent-summary-fetch-series ()
968 (when gnus-newsgroup-processable
969 (setq gnus-newsgroup-downloadable
970 (let* ((dl gnus-newsgroup-downloadable)
971 (gnus-newsgroup-downloadable
972 (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
973 (fetched-articles (gnus-agent-summary-fetch-group)))
974 ;; The preceeding call to (gnus-agent-summary-fetch-group)
975 ;; updated gnus-newsgroup-downloadable to remove each
976 ;; article successfully fetched.
978 ;; For each article that I processed, remove its
979 ;; processable mark IF the article is no longer
980 ;; downloadable (i.e. it's already downloaded)
981 (dolist (article gnus-newsgroup-processable)
982 (unless (memq article gnus-newsgroup-downloadable)
983 (gnus-summary-remove-process-mark article)))
984 (gnus-sorted-ndifference dl fetched-articles)))))
986 (defun gnus-agent-summary-fetch-group (&optional all)
987 "Fetch the downloadable articles in the group.
988 Optional arg ALL, if non-nil, means to fetch all articles."
991 (if all gnus-newsgroup-articles
992 gnus-newsgroup-downloadable))
993 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
995 (gnus-agent-while-plugged
997 (error "No articles to download"))
998 (gnus-agent-with-fetch
999 (setq gnus-newsgroup-undownloaded
1000 (gnus-sorted-ndifference
1001 gnus-newsgroup-undownloaded
1002 (setq fetched-articles
1003 (gnus-agent-fetch-articles
1004 gnus-newsgroup-name articles)))))
1006 (dolist (article articles)
1007 (let ((was-marked-downloadable
1008 (memq article gnus-newsgroup-downloadable)))
1009 (cond (gnus-agent-mark-unread-after-downloaded
1010 (setq gnus-newsgroup-downloadable
1011 (delq article gnus-newsgroup-downloadable))
1013 ;; The downloadable mark is implemented as a
1014 ;; type of read mark. Therefore, marking the
1015 ;; article as unread is sufficient to clear
1016 ;; its downloadable flag.
1017 (gnus-summary-mark-article article gnus-unread-mark))
1018 (was-marked-downloadable
1019 (gnus-summary-set-agent-mark article t)))
1020 (when (gnus-summary-goto-subject article nil t)
1021 (gnus-summary-update-download-mark article))))))
1024 (defun gnus-agent-fetch-selected-article ()
1025 "Fetch the current article as it is selected.
1026 This can be added to `gnus-select-article-hook' or
1027 `gnus-mark-article-hook'."
1028 (let ((gnus-command-method gnus-current-select-method))
1029 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1030 (when (gnus-agent-fetch-articles
1032 (list gnus-current-article))
1033 (setq gnus-newsgroup-undownloaded
1034 (delq gnus-current-article gnus-newsgroup-undownloaded))
1035 (gnus-summary-update-download-mark gnus-current-article)))))
1038 ;;; Internal functions
1041 (defun gnus-agent-save-active (method)
1042 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
1044 (defun gnus-agent-save-active-1 (method function)
1045 (when (gnus-agent-method-p method)
1046 (let* ((gnus-command-method method)
1047 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1048 (file (gnus-agent-lib-file "active")))
1049 (funcall function nil new)
1050 (gnus-agent-write-active file new)
1052 (nnheader-insert-file-contents file))))
1054 (defun gnus-agent-write-active (file new)
1055 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
1056 (file (gnus-agent-lib-file "active"))
1058 (when (file-exists-p file)
1060 (nnheader-insert-file-contents file)
1061 (gnus-active-to-gnus-format nil orig))
1064 (when (and sym (boundp sym))
1065 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
1066 (setq elem (symbol-value osym)))
1068 (if (and (integerp (car (symbol-value sym)))
1069 (> (car elem) (car (symbol-value sym))))
1070 (setcar elem (car (symbol-value sym))))
1071 (if (integerp (cdr (symbol-value sym)))
1072 (setcdr elem (cdr (symbol-value sym)))))
1073 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
1075 (gnus-make-directory (file-name-directory file))
1076 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1077 ;; The hashtable contains real names of groups, no more prefix
1078 ;; removing, so set `full' to `t'.
1079 (gnus-write-active-file file orig t))))
1081 (defun gnus-agent-save-groups (method)
1082 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
1084 (defun gnus-agent-save-group-info (method group active)
1085 (when (gnus-agent-method-p method)
1086 (let* ((gnus-command-method method)
1087 (coding-system-for-write nnheader-file-coding-system)
1088 (output-coding-system nnheader-file-coding-system)
1089 (file-name-coding-system nnmail-pathname-coding-system)
1090 (pathname-coding-system nnmail-pathname-coding-system)
1091 (file (gnus-agent-lib-file "active"))
1093 (gnus-make-directory (file-name-directory file))
1094 (with-temp-file file
1095 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1096 (set-buffer-multibyte nil)
1097 (when (file-exists-p file)
1098 (nnheader-insert-file-contents file))
1099 (goto-char (point-min))
1100 (when (re-search-forward
1101 (concat "^" (regexp-quote group) " ") nil t)
1103 (read (current-buffer)) ;; max
1104 (setq oactive-min (read (current-buffer)))) ;; min
1106 (insert (format "%S %d %d y\n" (intern group)
1108 (or oactive-min (car active))))
1109 (goto-char (point-max))
1110 (while (search-backward "\\." nil t)
1111 (delete-char 1))))))
1113 (defun gnus-agent-group-path (group)
1114 "Translate GROUP into a file name."
1115 (if nnmail-use-long-file-names
1116 (gnus-group-real-name group)
1117 (nnheader-translate-file-chars
1118 (nnheader-replace-chars-in-string
1119 (nnheader-replace-duplicate-chars-in-string
1120 (nnheader-replace-chars-in-string
1121 (gnus-group-real-name group)
1126 (defun gnus-agent-get-function (method)
1127 (if (gnus-online method)
1132 ;;; History functions
1134 (defun gnus-agent-history-buffer ()
1135 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1137 (defun gnus-agent-open-history ()
1139 (push (cons (gnus-agent-method)
1140 (set-buffer (gnus-get-buffer-create
1141 (format " *Gnus agent %s history*"
1142 (gnus-agent-method)))))
1143 gnus-agent-history-buffers)
1144 (set-buffer-multibyte nil) ;; everything is binary
1147 (let ((file (gnus-agent-lib-file "history")))
1148 (when (file-exists-p file)
1149 (nnheader-insert-file-contents file))
1150 (set (make-local-variable 'gnus-agent-file-name) file))))
1152 (defun gnus-agent-close-history ()
1153 (when (gnus-buffer-live-p gnus-agent-current-history)
1154 (kill-buffer gnus-agent-current-history)
1155 (setq gnus-agent-history-buffers
1156 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1157 gnus-agent-history-buffers))))
1163 (defun gnus-agent-fetch-articles (group articles)
1164 "Fetch ARTICLES from GROUP and put them into the Agent."
1166 (gnus-agent-load-alist group)
1167 (let* ((alist gnus-agent-article-alist)
1168 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1169 (selected-sets (list nil))
1170 (current-set-size 0)
1173 ;; Check each article
1174 (while (setq article (pop articles))
1175 ;; Skip alist entries preceeding this article
1176 (while (> article (or (caar alist) (1+ article)))
1177 (setq alist (cdr alist)))
1179 ;; Prune off articles that we have already fetched.
1180 (unless (and (eq article (caar alist))
1182 ;; Skip headers preceeding this article
1185 (let* ((header (car headers)))
1187 (mail-header-number header)
1189 (setq headers (cdr headers)))
1191 ;; Add this article to the current set
1192 (setcar selected-sets (cons article (car selected-sets)))
1194 ;; Update the set size, when the set is too large start a
1195 ;; new one. I do this after adding the article as I want at
1196 ;; least one article in each set.
1197 (when (< gnus-agent-max-fetch-size
1198 (setq current-set-size
1200 (if (= header-number article)
1201 (let ((char-size (mail-header-chars
1203 (if (<= char-size 0)
1204 ;; The char size was missing/invalid,
1205 ;; assume a worst-case situation of
1206 ;; 65 char/line. If the line count
1207 ;; is missing, arbitrarily assume a
1208 ;; size of 1000 characters.
1209 (max (* 65 (mail-header-lines
1214 (setcar selected-sets (nreverse (car selected-sets)))
1215 (setq selected-sets (cons nil selected-sets)
1216 current-set-size 0))))
1218 (when (or (cdr selected-sets) (car selected-sets))
1219 (let* ((fetched-articles (list nil))
1220 (tail-fetched-articles fetched-articles)
1222 (gnus-agent-directory)
1223 (gnus-agent-group-path group) "/"))
1224 (date (time-to-days (current-time)))
1225 (case-fold-search t)
1228 (setcar selected-sets (nreverse (car selected-sets)))
1229 (setq selected-sets (nreverse selected-sets))
1231 (gnus-make-directory dir)
1232 (gnus-message 7 "Fetching articles for %s..." group)
1235 (while (setq articles (pop selected-sets))
1236 ;; Fetch the articles from the backend.
1237 (if (gnus-check-backend-function 'retrieve-articles group)
1238 (setq pos (gnus-retrieve-articles articles group))
1241 (while (setq article (pop articles))
1242 (gnus-message 10 "Fetching article %s for %s..."
1245 (gnus-backlog-request-article group article
1247 (gnus-request-article article group))
1248 (goto-char (point-max))
1249 (push (cons article (point)) pos)
1250 (insert-buffer-substring nntp-server-buffer)))
1252 nntp-server-buffer (point-min) (point-max))
1253 (setq pos (nreverse pos)))))
1254 ;; Then save these articles into the Agent.
1256 (set-buffer nntp-server-buffer)
1258 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1259 (goto-char (point-min))
1260 (unless (eobp) ;; Don't save empty articles.
1261 (when (search-forward "\n\n" nil t)
1262 (when (search-backward "\nXrefs: " nil t)
1263 ;; Handle cross posting.
1264 (goto-char (match-end 0)) ; move to end of header name
1265 (skip-chars-forward "^ ") ; skip server name
1266 (skip-chars-forward " ")
1268 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1269 (push (cons (buffer-substring (match-beginning 1)
1272 (buffer-substring (match-beginning 2)
1275 (goto-char (match-end 0)))
1276 (gnus-agent-crosspost crosses (caar pos) date)))
1277 (goto-char (point-min))
1278 (if (not (re-search-forward
1279 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1280 (setq id "No-Message-ID-in-article")
1281 (setq id (buffer-substring
1282 (match-beginning 1) (match-end 1))))
1283 (write-region-as-coding-system
1284 gnus-agent-file-coding-system (point-min) (point-max)
1285 (concat dir (number-to-string (caar pos))) nil 'silent)
1287 (gnus-agent-append-to-list
1288 tail-fetched-articles (caar pos)))
1292 (gnus-agent-save-alist group (cdr fetched-articles) date)
1293 (gnus-message 7 ""))
1294 (cdr fetched-articles))))))
1296 (defun gnus-agent-crosspost (crosses article &optional date)
1297 (setq date (or date t))
1299 (let (gnus-agent-article-alist group alist beg end)
1301 (set-buffer gnus-agent-overview-buffer)
1302 (when (nnheader-find-nov-line article)
1305 (setq end (progn (forward-line 1) (point)))))
1307 (setq group (caar crosses))
1308 (unless (setq alist (assoc group gnus-agent-group-alist))
1309 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1310 gnus-agent-group-alist))
1311 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1313 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1315 (when (= (point-max) (point-min))
1316 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1318 (nnheader-insert-file-contents
1319 (gnus-agent-article-name ".overview" group))))
1320 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1321 (insert (string-to-number (cdar crosses)))
1322 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1323 (gnus-agent-check-overview-buffer))
1326 (defun gnus-agent-backup-overview-buffer ()
1327 (when gnus-newsgroup-name
1328 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1331 (while (file-exists-p
1332 (setq name (concat root "~"
1333 (int-to-string (setq cnt (1+ cnt))) "~"))))
1334 (write-region (point-min) (point-max) name nil 'no-msg)
1335 (gnus-message 1 "Created backup copy of overview in %s." name)))
1338 (defun gnus-agent-check-overview-buffer (&optional buffer)
1339 "Check the overview file given for sanity.
1340 In particular, checks that the file is sorted by article number
1341 and that there are no duplicates."
1346 (set-buffer buffer))
1349 (goto-char (point-min))
1351 (while (< (point) (point-max))
1353 (cur (condition-case nil
1354 (read (current-buffer))
1357 ((or (not (integerp cur))
1358 (not (eq (char-after) ?\t)))
1360 (setq backed-up (gnus-agent-backup-overview-buffer)))
1362 "Overview buffer contains garbage '%s'."
1364 p (gnus-point-at-eol))))
1367 (setq backed-up (gnus-agent-backup-overview-buffer)))
1369 "Duplicate overview line for %d" cur)
1370 (delete-region (point) (progn (forward-line 1) (point))))
1373 (setq backed-up (gnus-agent-backup-overview-buffer)))
1374 (gnus-message 1 "Overview buffer not sorted!")
1375 (sort-numeric-fields 1 (point-min) (point-max))
1376 (goto-char (point-min))
1379 (setq prev-num cur)))
1380 (forward-line 1)))))))
1382 (defun gnus-agent-flush-cache ()
1384 (while gnus-agent-buffer-alist
1385 (set-buffer (cdar gnus-agent-buffer-alist))
1386 (write-region-as-coding-system
1387 gnus-agent-file-coding-system
1388 (point-min) (point-max)
1389 (gnus-agent-article-name ".overview"
1390 (caar gnus-agent-buffer-alist))
1392 (pop gnus-agent-buffer-alist))
1393 (while gnus-agent-group-alist
1394 (with-temp-file (gnus-agent-article-name
1395 ".agentview" (caar gnus-agent-group-alist))
1396 (princ (cdar gnus-agent-group-alist))
1398 (princ 1 (current-buffer))
1400 (pop gnus-agent-group-alist))))
1402 (defun gnus-agent-find-parameter (group symbol)
1403 "Search for GROUPs SYMBOL in the group's parameters, the group's
1404 topic parameters, the group's category, or the customizable
1405 variables. Returns the first non-nil value found."
1406 (or (gnus-group-find-parameter group symbol t)
1407 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1411 '((agent-short-article . gnus-agent-short-article)
1412 (agent-long-article . gnus-agent-long-article)
1413 (agent-low-score . gnus-agent-low-score)
1414 (agent-high-score . gnus-agent-high-score)
1415 (agent-days-until-old . gnus-agent-expire-days)
1416 (agent-enable-expiration
1417 . gnus-agent-enable-expiration)
1418 (agent-predicate . gnus-agent-predicate)))))))
1420 (defun gnus-agent-fetch-headers (group &optional force)
1421 "Fetch interesting headers into the agent. The group's overview
1422 file will be updated to include the headers while a list of available
1423 article numbers will be returned."
1424 (let* ((fetch-all (and gnus-agent-consider-all-articles
1425 ;; Do not fetch all headers if the predicate
1426 ;; implies that we only consider unread articles.
1427 (not (gnus-predicate-implies-unread
1428 (gnus-agent-find-parameter group
1429 'agent-predicate)))))
1430 (articles (if fetch-all
1431 (gnus-uncompress-range (gnus-active group))
1432 (gnus-list-of-unread-articles group)))
1433 (gnus-decode-encoded-word-function 'identity)
1434 (file (gnus-agent-article-name ".overview" group)))
1435 ;; Check whether the number of articles is not too large.
1436 (when (and (integerp gnus-agent-large-newsgroup)
1437 (> gnus-agent-large-newsgroup 0))
1438 (setq articles (nthcdr (max (- (length articles)
1439 gnus-agent-large-newsgroup)
1443 ;; Add articles with marks to the list of article headers we want to
1444 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1445 ;; mark, but do fetch recent or seen articles if they have other, more
1446 ;; interesting marks. (We have to fetch articles with boring marks
1447 ;; because otherwise the agent will remove their marks.)
1448 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1449 (unless (memq (car arts) '(seen recent killed cache))
1450 (setq articles (gnus-range-add articles (cdr arts)))))
1451 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1453 ;; At this point, I have the list of articles to consider for
1454 ;; fetching. This is the list that I'll return to my caller. Some
1455 ;; of these articles may have already been fetched. That's OK as
1456 ;; the fetch article code will filter those out. Internally, I'll
1457 ;; filter this list to just those articles whose headers need to
1459 (let ((articles articles))
1460 ;; Remove known articles.
1461 (when (and (or gnus-agent-cache
1463 (gnus-agent-load-alist group))
1464 ;; Remove articles marked as downloaded.
1466 ;; I want to fetch all headers in the active range.
1467 ;; Therefore, exclude only those headers that are in the
1469 ;; NOTE: This is probably NOT what I want to do after
1470 ;; agent expiration in this group.
1471 (setq articles (gnus-agent-uncached-articles articles group))
1473 ;; I want to only fetch those headers that have never been
1474 ;; fetched. Therefore, exclude all headers that are, or
1475 ;; WERE, in the article alist.
1476 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1477 (high (cdr (gnus-active group))))
1478 ;; Low can be greater than High when the same group is
1479 ;; fetched twice in the same session {The first fetch will
1480 ;; fill the article alist such that (last
1481 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1482 ;; group))}. The addition of one(the 1+ above) then
1483 ;; forces Low to be greater than High. When this happens,
1484 ;; gnus-list-range-intersection returns nil which
1485 ;; indicates that no headers need to be fetched. -- Kevin
1486 (setq articles (gnus-list-range-intersection
1487 articles (list (cons low high)))))))
1490 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1491 (gnus-compress-sequence articles t))
1494 (set-buffer nntp-server-buffer)
1498 (gnus-message 7 "Fetching headers for %s..." group)
1501 (gnus-make-directory (nnheader-translate-file-chars
1502 (file-name-directory file) t))
1504 (unless (eq 'nov (gnus-retrieve-headers articles group))
1505 (nnvirtual-convert-headers))
1506 (gnus-agent-check-overview-buffer)
1507 ;; Move these headers to the overview buffer so that
1508 ;; gnus-agent-braid-nov can merge them with the contents
1511 gnus-agent-overview-buffer (point-min) (point-max))
1512 (when (file-exists-p file)
1513 (gnus-agent-braid-nov group articles file))
1514 (gnus-agent-check-overview-buffer)
1515 (write-region-as-coding-system
1516 gnus-agent-file-coding-system
1517 (point-min) (point-max) file nil 'silent)
1518 (gnus-agent-save-alist group articles nil)
1522 (nnheader-insert-file-contents file)))))
1525 (defsubst gnus-agent-copy-nov-line (article)
1527 (set-buffer gnus-agent-overview-buffer)
1528 (while (and (not (eobp))
1529 (< (setq art (read (current-buffer))) article))
1533 (not (eq article art)))
1534 (set-buffer nntp-server-buffer)
1536 (setq e (progn (forward-line 1) (point)))
1537 (set-buffer nntp-server-buffer)
1538 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1540 (defun gnus-agent-braid-nov (group articles file)
1541 "Merge agent overview data with given file.
1542 Takes headers for ARTICLES from `gnus-agent-overview-buffer' and the given
1543 FILE and places the combined headers into `nntp-server-buffer'."
1545 (set-buffer gnus-agent-overview-buffer)
1546 (goto-char (point-min))
1547 (set-buffer nntp-server-buffer)
1549 (nnheader-insert-file-contents file)
1550 (goto-char (point-max))
1552 (unless (looking-at "[0-9]+\t")
1553 ;; Remove corrupted lines
1555 1 "Overview %s is corrupted. Removing corrupted lines..." file)
1556 (goto-char (point-min))
1558 (if (looking-at "[0-9]+\t")
1560 (delete-region (point) (progn (forward-line 1) (point)))))
1562 (unless (or (= (point-min) (point-max))
1563 (< (setq last (read (current-buffer))) (car articles)))
1564 ;; We do it the hard way.
1565 (when (nnheader-find-nov-line (car articles))
1566 ;; Replacing existing NOV entry
1567 (delete-region (point) (progn (forward-line 1) (point))))
1568 (gnus-agent-copy-nov-line (pop articles))
1572 (while (let ((art (read (current-buffer))))
1573 (cond ((< art (car articles))
1576 ((= art (car articles))
1579 (point) (progn (forward-line 1) (point)))
1585 (gnus-agent-copy-nov-line (pop articles)))))
1587 ;; Copy the rest lines
1588 (set-buffer nntp-server-buffer)
1589 (goto-char (point-max))
1592 (set-buffer gnus-agent-overview-buffer)
1594 (while (<= (read (current-buffer)) last)
1597 (setq start (point))
1598 (set-buffer nntp-server-buffer))
1599 (insert-buffer-substring gnus-agent-overview-buffer start))))
1601 ;; Keeps the compiler from warning about the free variable in
1602 ;; gnus-agent-read-agentview.
1604 (defvar gnus-agent-read-agentview))
1606 (defun gnus-agent-load-alist (group)
1607 "Load the article-state alist for GROUP."
1608 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1609 (let ((gnus-agent-read-agentview group))
1610 (setq gnus-agent-article-alist
1611 (gnus-cache-file-contents
1612 (gnus-agent-article-name ".agentview" group)
1613 'gnus-agent-file-loading-cache
1614 'gnus-agent-read-agentview))))
1616 ;; Save format may be either 1 or 2. Two is the new, compressed
1617 ;; format that is still being tested. Format 1 is uncompressed but
1618 ;; known to be reliable.
1619 (defconst gnus-agent-article-alist-save-format 2)
1621 (defun gnus-agent-read-agentview (file)
1622 "Load FILE and do a `read' there."
1625 (nnheader-insert-file-contents file)
1626 (goto-char (point-min))
1627 (let ((alist (read (current-buffer)))
1628 (version (condition-case nil (read (current-buffer))
1634 (let ((inhibit-quit t)
1636 (gnus-agent-open-history)
1637 (set-buffer (gnus-agent-history-buffer))
1638 (goto-char (point-min))
1640 (if (and (looking-at
1641 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1642 (string= (match-string 2)
1643 gnus-agent-read-agentview)
1644 (setq entry (assoc (string-to-number (match-string 3)) alist)))
1645 (setcdr entry (string-to-number (match-string 1))))
1647 (gnus-agent-close-history)
1648 (setq changed-version t)))
1650 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
1655 (let ((state (car comp-list))
1656 (sequence (gnus-uncompress-sequence
1658 (mapcar (lambda (article-id)
1659 (setq uncomp (cons (cons article-id state) uncomp)))
1662 (setq alist (sort uncomp
1663 (lambda (first second)
1664 (< (car first) (car second))))))))
1665 (when changed-version
1666 (let ((gnus-agent-article-alist alist))
1667 (gnus-agent-save-alist gnus-agent-read-agentview)))
1670 (defun gnus-agent-save-alist (group &optional articles state dir)
1671 "Save the article-state alist for GROUP."
1672 (let* ((file-name-coding-system nnmail-pathname-coding-system)
1673 (pathname-coding-system nnmail-pathname-coding-system)
1674 (prev (cons nil gnus-agent-article-alist))
1676 print-level print-length item article)
1677 (while (setq article (pop articles))
1678 (while (and (cdr prev)
1679 (< (caadr prev) article))
1680 (setq prev (cdr prev)))
1683 (setcdr prev (list (cons article state))))
1684 ((> (caadr prev) article)
1685 (setcdr prev (cons (cons article state) (cdr prev))))
1686 ((= (caadr prev) article)
1687 (setcdr (cadr prev) state)))
1688 (setq prev (cdr prev)))
1689 (setq gnus-agent-article-alist (cdr all))
1691 (gnus-make-directory dir)
1692 (gnus-make-directory (gnus-agent-article-name "" group)))
1693 (with-temp-file (if dir
1694 (expand-file-name ".agentview" dir)
1695 (gnus-agent-article-name ".agentview" group))
1696 (cond ((eq gnus-agent-article-alist-save-format 1)
1697 (princ gnus-agent-article-alist (current-buffer)))
1698 ((eq gnus-agent-article-alist-save-format 2)
1699 (let ((compressed nil))
1700 (mapcar (lambda (pair)
1701 (let* ((article-id (car pair))
1702 (day-of-download (cdr pair))
1703 (comp-list (assq day-of-download compressed)))
1706 (cons article-id (cdr comp-list)))
1708 (cons (list day-of-download article-id)
1710 nil)) gnus-agent-article-alist)
1711 (mapcar (lambda (comp-list)
1713 (gnus-compress-sequence
1714 (nreverse (cdr comp-list)))))
1716 (princ compressed (current-buffer)))))
1718 (princ gnus-agent-article-alist-save-format (current-buffer))
1721 (defun gnus-agent-article-name (article group)
1722 (expand-file-name article
1723 (file-name-as-directory
1724 (expand-file-name (gnus-agent-group-path group)
1725 (gnus-agent-directory)))))
1727 (defun gnus-agent-batch-confirmation (msg)
1728 "Show error message and return t."
1729 (gnus-message 1 msg)
1733 (defun gnus-agent-batch-fetch ()
1734 "Start Gnus and fetch session."
1737 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1738 (gnus-agent-fetch-session))
1741 (defun gnus-agent-fetch-session ()
1742 "Fetch all articles and headers that are eligible for fetching."
1744 (unless gnus-agent-covered-methods
1745 (error "No servers are covered by the Gnus agent"))
1746 (unless gnus-plugged
1747 (error "Can't fetch articles while Gnus is unplugged"))
1748 (let ((methods gnus-agent-covered-methods)
1749 groups group gnus-command-method)
1752 (setq gnus-command-method (car methods))
1753 (when (and (or (gnus-server-opened gnus-command-method)
1754 (gnus-open-server gnus-command-method))
1755 (gnus-online gnus-command-method))
1756 (setq groups (gnus-groups-from-server (car methods)))
1757 (gnus-agent-with-fetch
1758 (while (setq group (pop groups))
1759 (when (<= (gnus-group-level group)
1760 gnus-agent-handle-level)
1761 (if (or debug-on-error debug-on-quit)
1762 (gnus-agent-fetch-group-1
1763 group gnus-command-method)
1765 (gnus-agent-fetch-group-1
1766 group gnus-command-method)
1768 (unless (funcall gnus-agent-confirmation-function
1769 (format "Error %s. Continue? "
1770 (error-message-string err)))
1771 (error "Cannot fetch articles into the Gnus agent")))
1773 (unless (funcall gnus-agent-confirmation-function
1775 "Quit fetching session %s. Continue? "
1776 (error-message-string err)))
1778 "Cannot fetch articles into the Gnus agent")))))))))
1780 (gnus-run-hooks 'gnus-agent-fetched-hook)
1781 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1783 (defun gnus-agent-fetch-group-1 (group method)
1785 (let ((gnus-command-method method)
1786 (gnus-newsgroup-name group)
1787 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
1788 (gnus-newsgroup-headers gnus-newsgroup-headers)
1789 (gnus-newsgroup-scored gnus-newsgroup-scored)
1790 (gnus-use-cache gnus-use-cache)
1791 (gnus-summary-expunge-below gnus-summary-expunge-below)
1792 (gnus-summary-mark-below gnus-summary-mark-below)
1793 (gnus-orphan-score gnus-orphan-score)
1794 ;; Maybe some other gnus-summary local variables should also
1800 category predicate info marks score-param
1802 (unless (gnus-check-group group)
1803 (error "Can't open server for %s" group))
1806 (when (or gnus-newsgroup-active
1808 (gnus-activate-group group))
1809 (let ((marked-articles gnus-newsgroup-downloadable))
1810 ;; Identify the articles marked for download
1811 (unless gnus-newsgroup-active
1812 ;; The variable gnus-newsgroup-active was selected as I need
1813 ;; a gnus-summary local variable that is NOT bound to any
1814 ;; value (its global value should default to nil).
1815 (dolist (mark gnus-agent-download-marks)
1816 (let ((arts (cdr (assq mark (gnus-info-marks
1817 (setq info (gnus-get-info group)))))))
1819 (setq marked-articles (nconc (gnus-uncompress-range arts)
1822 (setq marked-articles (sort marked-articles '<))
1824 ;; Fetch any new articles from the server
1825 (setq articles (gnus-agent-fetch-headers group))
1827 ;; Merge new articles with marked
1828 (setq articles (sort (append marked-articles articles) '<))
1831 ;; Parse them and see which articles we want to fetch.
1832 (setq gnus-newsgroup-dependencies
1833 (or gnus-newsgroup-dependencies
1834 (make-vector (length articles) 0)))
1835 (setq gnus-newsgroup-headers
1836 (or gnus-newsgroup-headers
1837 (gnus-get-newsgroup-headers-xover articles nil nil
1839 ;; `gnus-agent-overview-buffer' may be killed for
1840 ;; timeout reason. If so, recreate it.
1841 (gnus-agent-create-buffer)
1843 ;; Figure out how to select articles in this group
1844 (setq category (gnus-group-category group))
1848 (gnus-agent-find-parameter group 'agent-predicate)))
1850 ;; If the selection predicate requires scoring, score each header
1851 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
1853 (gnus-agent-find-parameter group 'agent-score-file)))
1854 ;; Translate score-param into real one
1857 ((eq score-param 'file)
1858 (setq score-param (gnus-all-score-files group)))
1859 ((stringp (car score-param)))
1861 (setq score-param (list (list score-param)))))
1863 (gnus-score-headers score-param))))
1865 (unless (and (eq predicate 'gnus-agent-false)
1866 (not marked-articles))
1867 (let ((arts (list nil)))
1868 (let ((arts-tail arts)
1869 (alist (gnus-agent-load-alist group))
1870 (marked-articles marked-articles)
1871 (gnus-newsgroup-headers gnus-newsgroup-headers))
1872 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1873 (let ((num (mail-header-number gnus-headers)))
1874 ;; Determine if this article is already in the cache
1876 (> num (caar alist)))
1877 (setq alist (cdr alist)))
1879 (unless (and (eq num (caar alist))
1882 ;; Determine if this article was marked for download.
1883 (while (and marked-articles
1884 (> num (car marked-articles)))
1885 (setq marked-articles
1886 (cdr marked-articles)))
1888 ;; When this article is marked, or selected by the
1889 ;; predicate, add it to the download list
1890 (when (or (eq num (car marked-articles))
1893 (assq num gnus-newsgroup-scored))
1894 gnus-summary-default-score))
1895 (gnus-agent-long-article
1896 (gnus-agent-find-parameter
1897 group 'agent-long-article))
1898 (gnus-agent-short-article
1899 (gnus-agent-find-parameter
1900 group 'agent-short-article))
1901 (gnus-agent-low-score
1902 (gnus-agent-find-parameter
1903 group 'agent-low-score))
1904 (gnus-agent-high-score
1905 (gnus-agent-find-parameter
1906 group 'agent-high-score))
1907 (gnus-agent-expire-days
1908 (gnus-agent-find-parameter
1909 group 'agent-days-until-old)))
1910 (funcall predicate)))
1911 (gnus-agent-append-to-list arts-tail num))))))
1913 (let (fetched-articles)
1914 ;; Fetch all selected articles
1915 (setq gnus-newsgroup-undownloaded
1916 (gnus-sorted-ndifference
1917 gnus-newsgroup-undownloaded
1918 (setq fetched-articles
1920 (gnus-agent-fetch-articles group (cdr arts))
1923 (let ((unfetched-articles
1924 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
1925 (if gnus-newsgroup-active
1926 ;; Update the summary buffer
1928 (dolist (article marked-articles)
1929 (gnus-summary-set-agent-mark article t))
1930 (dolist (article fetched-articles)
1931 (if gnus-agent-mark-unread-after-downloaded
1932 (gnus-summary-mark-article
1933 article gnus-unread-mark))
1934 (when (gnus-summary-goto-subject article nil t)
1935 (gnus-summary-update-download-mark article)))
1936 (dolist (article unfetched-articles)
1937 (gnus-summary-mark-article
1938 article gnus-canceled-mark)))
1940 ;; Update the group buffer.
1942 ;; When some, or all, of the marked articles came
1943 ;; from the download mark. Remove that mark. I
1944 ;; didn't do this earlier as I only want to remove
1945 ;; the marks after the fetch is completed.
1947 (dolist (mark gnus-agent-download-marks)
1948 (when (eq mark 'download)
1950 (assq mark (gnus-info-marks
1951 (setq info (gnus-get-info group))))))
1952 (when (cdr marked-arts)
1954 (delq marked-arts (gnus-info-marks info)))
1955 (gnus-info-set-marks info marks)))))
1956 (let ((read (gnus-info-read
1957 (or info (setq info (gnus-get-info group))))))
1959 info (gnus-add-to-range read unfetched-articles)))
1961 (gnus-group-update-group group t)
1965 (concat "(gnus-group-set-info '"
1966 (gnus-prin1-to-string info)
1970 ;;; Agent Category Mode
1973 (defvar gnus-category-mode-hook nil
1974 "Hook run in `gnus-category-mode' buffers.")
1976 (defvar gnus-category-line-format " %(%20c%): %g\n"
1977 "Format of category lines.
1979 Valid specifiers include:
1980 %c Topic name (string)
1981 %g The number of groups in the topic (integer)
1983 General format specifiers can also be used. See Info node
1984 `(gnus)Formatting Variables'.")
1986 (defvar gnus-category-mode-line-format "Gnus: %%b"
1987 "The format specification for the category mode line.")
1989 (defvar gnus-agent-predicate 'false
1990 "The selection predicate used when no other source is available.")
1992 (defvar gnus-agent-short-article 100
1993 "Articles that have fewer lines than this are short.")
1995 (defvar gnus-agent-long-article 200
1996 "Articles that have more lines than this are long.")
1998 (defvar gnus-agent-low-score 0
1999 "Articles that have a score lower than this have a low score.")
2001 (defvar gnus-agent-high-score 0
2002 "Articles that have a score higher than this have a high score.")
2005 ;;; Internal variables.
2007 (defvar gnus-category-buffer "*Agent Category*")
2009 (defvar gnus-category-line-format-alist
2010 `((?c gnus-tmp-name ?s)
2011 (?g gnus-tmp-groups ?d)))
2013 (defvar gnus-category-mode-line-format-alist
2014 `((?u user-defined ?s)))
2016 (defvar gnus-category-line-format-spec nil)
2017 (defvar gnus-category-mode-line-format-spec nil)
2019 (defvar gnus-category-mode-map nil)
2020 (put 'gnus-category-mode 'mode-class 'special)
2022 (unless gnus-category-mode-map
2023 (setq gnus-category-mode-map (make-sparse-keymap))
2024 (suppress-keymap gnus-category-mode-map)
2026 (gnus-define-keys gnus-category-mode-map
2027 "q" gnus-category-exit
2028 "k" gnus-category-kill
2029 "c" gnus-category-copy
2030 "a" gnus-category-add
2031 "e" gnus-agent-customize-category
2032 "p" gnus-category-edit-predicate
2033 "g" gnus-category-edit-groups
2034 "s" gnus-category-edit-score
2035 "l" gnus-category-list
2037 "\C-c\C-i" gnus-info-find-node
2038 "\C-c\C-b" gnus-bug))
2040 (defvar gnus-category-menu-hook nil
2041 "*Hook run after the creation of the menu.")
2043 (defun gnus-category-make-menu-bar ()
2044 (gnus-turn-off-edit-menu 'category)
2045 (unless (boundp 'gnus-category-menu)
2047 gnus-category-menu gnus-category-mode-map ""
2049 ["Add" gnus-category-add t]
2050 ["Kill" gnus-category-kill t]
2051 ["Copy" gnus-category-copy t]
2052 ["Edit category" gnus-agent-customize-category t]
2053 ["Edit predicate" gnus-category-edit-predicate t]
2054 ["Edit score" gnus-category-edit-score t]
2055 ["Edit groups" gnus-category-edit-groups t]
2056 ["Exit" gnus-category-exit t]))
2058 (gnus-run-hooks 'gnus-category-menu-hook)))
2060 (defun gnus-category-mode ()
2061 "Major mode for listing and editing agent categories.
2063 All normal editing commands are switched off.
2064 \\<gnus-category-mode-map>
2065 For more in-depth information on this mode, read the manual
2066 \(`\\[gnus-info-find-node]').
2068 The following commands are available:
2070 \\{gnus-category-mode-map}"
2072 (when (gnus-visual-p 'category-menu 'menu)
2073 (gnus-category-make-menu-bar))
2074 (kill-all-local-variables)
2075 (gnus-simplify-mode-line)
2076 (setq major-mode 'gnus-category-mode)
2077 (setq mode-name "Category")
2078 (gnus-set-default-directory)
2079 (setq mode-line-process nil)
2080 (use-local-map gnus-category-mode-map)
2081 (buffer-disable-undo)
2082 (setq truncate-lines t)
2083 (setq buffer-read-only t)
2084 (gnus-run-hooks 'gnus-category-mode-hook))
2086 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2088 (defun gnus-category-insert-line (category)
2089 (let* ((gnus-tmp-name (format "%s" (car category)))
2090 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2092 (gnus-add-text-properties
2096 (eval gnus-category-line-format-spec))
2097 (list 'gnus-category gnus-tmp-name))))
2099 (defun gnus-enter-category-buffer ()
2100 "Go to the Category buffer."
2102 (gnus-category-setup-buffer)
2103 (gnus-configure-windows 'category)
2104 (gnus-category-prepare))
2106 (defun gnus-category-setup-buffer ()
2107 (unless (get-buffer gnus-category-buffer)
2109 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2110 (gnus-category-mode))))
2112 (defun gnus-category-prepare ()
2113 (gnus-set-format 'category-mode)
2114 (gnus-set-format 'category t)
2115 (let ((alist gnus-category-alist)
2116 (buffer-read-only nil))
2119 (gnus-category-insert-line (pop alist)))
2120 (goto-char (point-min))
2121 (gnus-category-position-point)))
2123 (defun gnus-category-name ()
2124 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2125 (error "No category on the current line")))
2127 (defun gnus-category-read ()
2128 "Read the category alist."
2129 (setq gnus-category-alist
2133 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2134 (goto-char (point-min))
2135 ;; This code isn't temp, it will be needed so long as
2136 ;; anyone may be migrating from an older version.
2138 ;; Once we're certain that people will not revert to an
2139 ;; earlier version, we can take out the old-list code in
2140 ;; gnus-category-write.
2141 (let* ((old-list (read (current-buffer)))
2142 (new-list (ignore-errors (read (current-buffer)))))
2145 ;; Convert from a positional list to an alist.
2155 '(agent-predicate agent-score-file agent-groups))))
2158 (list (gnus-agent-cat-make 'default)))))
2160 (defun gnus-category-write ()
2161 "Write the category alist."
2162 (setq gnus-category-predicate-cache nil
2163 gnus-category-group-cache nil)
2164 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2165 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2166 ;; This prin1 is temporary. It exists so that people can revert
2167 ;; to an earlier version of gnus-agent.
2168 (prin1 (mapcar (lambda (c)
2170 (cdr (assoc 'agent-predicate c))
2171 (cdr (assoc 'agent-score-file c))
2172 (cdr (assoc 'agent-groups c))))
2173 gnus-category-alist)
2176 (prin1 gnus-category-alist (current-buffer))))
2178 (defun gnus-category-edit-predicate (category)
2179 "Edit the predicate for CATEGORY."
2180 (interactive (list (gnus-category-name)))
2181 (let ((info (assq category gnus-category-alist)))
2183 (gnus-agent-cat-predicate info)
2184 (format "Editing the select predicate for category %s" category)
2185 `(lambda (predicate)
2186 ;; Avoid run-time execution of setf form
2187 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2189 ;; use its expansion instead:
2190 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2191 'agent-predicate predicate)
2193 (gnus-category-write)
2194 (gnus-category-list)))))
2196 (defun gnus-category-edit-score (category)
2197 "Edit the score expression for CATEGORY."
2198 (interactive (list (gnus-category-name)))
2199 (let ((info (assq category gnus-category-alist)))
2201 (gnus-agent-cat-score-file info)
2202 (format "Editing the score expression for category %s" category)
2203 `(lambda (score-file)
2204 ;; Avoid run-time execution of setf form
2205 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2207 ;; use its expansion instead:
2208 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2209 'agent-score-file score-file)
2211 (gnus-category-write)
2212 (gnus-category-list)))))
2214 (defun gnus-category-edit-groups (category)
2215 "Edit the group list for CATEGORY."
2216 (interactive (list (gnus-category-name)))
2217 (let ((info (assq category gnus-category-alist)))
2219 (gnus-agent-cat-groups info)
2220 (format "Editing the group list for category %s" category)
2222 ;; Avoid run-time execution of setf form
2223 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2225 ;; use its expansion instead:
2226 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2229 (gnus-category-write)
2230 (gnus-category-list)))))
2232 (defun gnus-category-kill (category)
2233 "Kill the current category."
2234 (interactive (list (gnus-category-name)))
2235 (let ((info (assq category gnus-category-alist))
2236 (buffer-read-only nil))
2238 (setq gnus-category-alist (delq info gnus-category-alist))
2239 (gnus-category-write)))
2241 (defun gnus-category-copy (category to)
2242 "Copy the current category."
2243 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2244 (let ((info (assq category gnus-category-alist)))
2245 (push (let ((newcat (gnus-copy-sequence info)))
2246 (setf (gnus-agent-cat-name newcat) to)
2247 (setf (gnus-agent-cat-groups newcat) nil)
2249 gnus-category-alist)
2250 (gnus-category-write)
2251 (gnus-category-list)))
2253 (defun gnus-category-add (category)
2254 "Create a new category."
2255 (interactive "SCategory name: ")
2256 (when (assq category gnus-category-alist)
2257 (error "Category %s already exists" category))
2258 (push (gnus-agent-cat-make category)
2259 gnus-category-alist)
2260 (gnus-category-write)
2261 (gnus-category-list))
2263 (defun gnus-category-list ()
2264 "List all categories."
2266 (gnus-category-prepare))
2268 (defun gnus-category-exit ()
2269 "Return to the group buffer."
2271 (kill-buffer (current-buffer))
2272 (gnus-configure-windows 'group t))
2274 ;; To avoid having 8-bit characters in the source file.
2275 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2277 (defvar gnus-category-predicate-alist
2278 '((spam . gnus-agent-spam-p)
2279 (short . gnus-agent-short-p)
2280 (long . gnus-agent-long-p)
2281 (low . gnus-agent-low-scored-p)
2282 (high . gnus-agent-high-scored-p)
2283 (read . gnus-agent-read-p)
2284 (true . gnus-agent-true)
2285 (false . gnus-agent-false))
2286 "Mapping from short score predicate symbols to predicate functions.")
2288 (defun gnus-agent-spam-p ()
2289 "Say whether an article is spam or not."
2290 (unless gnus-agent-spam-hashtb
2291 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2292 (if (not (equal (mail-header-references gnus-headers) ""))
2294 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2296 (gnus-gethash string gnus-agent-spam-hashtb)
2297 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2299 (defun gnus-agent-short-p ()
2300 "Say whether an article is short or not."
2301 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2303 (defun gnus-agent-long-p ()
2304 "Say whether an article is long or not."
2305 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2307 (defun gnus-agent-low-scored-p ()
2308 "Say whether an article has a low score or not."
2309 (< gnus-score gnus-agent-low-score))
2311 (defun gnus-agent-high-scored-p ()
2312 "Say whether an article has a high score or not."
2313 (> gnus-score gnus-agent-high-score))
2315 (defun gnus-agent-read-p ()
2316 "Say whether an article is read or not."
2317 (gnus-member-of-range (mail-header-number gnus-headers)
2318 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2320 (defun gnus-category-make-function (predicate)
2321 "Make a function from PREDICATE."
2322 (let ((func (gnus-category-make-function-1 predicate)))
2323 (if (and (= (length func) 1)
2324 (symbolp (car func)))
2326 (gnus-byte-compile `(lambda () ,func)))))
2328 (defun gnus-agent-true ()
2332 (defun gnus-agent-false ()
2336 (defun gnus-category-make-function-1 (predicate)
2337 "Make a function from PREDICATE."
2339 ;; Functions are just returned as is.
2340 ((or (symbolp predicate)
2341 (gnus-functionp predicate))
2342 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2344 ;; More complex predicate.
2347 ((memq (car predicate) '(& and))
2349 ((memq (car predicate) '(| or))
2351 ((memq (car predicate) gnus-category-not)
2353 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2355 (error "Unknown predicate type: %s" predicate))))
2357 (defun gnus-get-predicate (predicate)
2358 "Return the function implementing PREDICATE."
2359 (or (cdr (assoc predicate gnus-category-predicate-cache))
2360 (let ((func (gnus-category-make-function predicate)))
2361 (setq gnus-category-predicate-cache
2362 (nconc gnus-category-predicate-cache
2363 (list (cons predicate func))))
2366 (defun gnus-predicate-implies-unread (predicate)
2367 "Say whether PREDICATE implies unread articles only.
2368 It is okay to miss some cases, but there must be no false positives.
2369 That is, if this function returns true, then indeed the predicate must
2370 return only unread articles."
2371 (gnus-function-implies-unread-1 (gnus-category-make-function predicate)))
2373 (defun gnus-function-implies-unread-1 (function)
2374 (cond ((eq function (symbol-function 'gnus-agent-read-p))
2378 ((gnus-functionp function)
2380 ((memq (car function) '(or and not))
2381 (apply (car function)
2382 (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2384 (error "Unknown function: %s" function))))
2386 (defun gnus-group-category (group)
2387 "Return the category GROUP belongs to."
2388 (unless gnus-category-group-cache
2389 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2390 (let ((cs gnus-category-alist)
2392 (while (setq cat (pop cs))
2393 (setq groups (gnus-agent-cat-groups cat))
2395 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2396 (or (gnus-gethash group gnus-category-group-cache)
2397 (assq 'default gnus-category-alist)))
2399 (defun gnus-agent-expire-group (group &optional articles force)
2400 "Expire all old articles in GROUP.
2401 If you want to force expiring of certain articles, this function can
2402 take ARTICLES, and FORCE parameters as well.
2404 The articles on which the expiration process runs are selected as follows:
2405 if ARTICLES is null, all read and unmarked articles.
2406 if ARTICLES is t, all articles.
2407 if ARTICLES is a list, just those articles.
2408 FORCE is equivalent to setting the expiration predicates to true."
2410 (list (let ((def (or (gnus-group-group-name)
2411 gnus-newsgroup-name)))
2412 (let ((select (read-string (if def
2413 (concat "Group Name ("
2416 (if (and (equal "" select)
2422 (gnus-agent-expire articles group force)
2423 (if (or (not (eq articles t))
2425 (concat "Are you sure that you want to "
2426 "expire all articles in " group ".")))
2427 (let ((gnus-command-method (gnus-find-method-for-group group))
2428 (overview (gnus-get-buffer-create " *expire overview*"))
2431 (when (file-exists-p (gnus-agent-lib-file "active"))
2433 (nnheader-insert-file-contents
2434 (gnus-agent-lib-file "active"))
2435 (gnus-active-to-gnus-format
2437 (setq orig (gnus-make-hashtable
2438 (count-lines (point-min) (point-max))))))
2440 (gnus-agent-expire-group-1
2441 group overview (gnus-gethash-safe group orig)
2443 (kill-buffer overview))))
2444 (gnus-message 4 "Expiry...done")))
2446 (defmacro gnus-agent-message (level &rest args)
2447 `(if (<= ,level gnus-verbose)
2450 (defun gnus-agent-expire-group-1 (group overview active articles force)
2451 ;; Internal function - requires caller to have set
2452 ;; gnus-command-method, initialized overview buffer, and to have
2453 ;; provided a non-nil active
2456 (gnus-agent-directory)
2457 (gnus-agent-group-path group)
2459 (when (boundp 'gnus-agent-expire-current-dirs)
2460 (set 'gnus-agent-expire-current-dirs
2462 (symbol-value 'gnus-agent-expire-current-dirs))))
2464 (if (eq 'DISABLE (gnus-agent-find-parameter group
2465 'agent-enable-expiration))
2466 (gnus-message 5 "Expiry skipping over %s" group)
2467 (gnus-message 5 "Expiring articles in %s" group)
2468 (gnus-agent-load-alist group)
2469 (let* ((info (gnus-get-info group))
2470 (alist gnus-agent-article-alist)
2471 (day (- (time-to-days (current-time))
2472 (gnus-agent-find-parameter group 'agent-days-until-old)))
2473 (specials (if (and alist
2475 ;; This could be a bit of a problem. I need to
2476 ;; keep the last article to avoid refetching
2477 ;; headers when using nntp in the backend. At
2478 ;; the same time, if someone uses a backend
2479 ;; that supports article moving then I may have
2480 ;; to remove the last article to complete the
2481 ;; move. Right now, I'm going to assume that
2482 ;; FORCE overrides specials.
2483 (list (caar (last alist)))))
2484 (unreads ;; Articles that are excluded from the
2485 ;; expiration process
2486 (cond (gnus-agent-expire-all
2487 ;; All articles are marked read by global decree
2490 ;; All articles are marked read by function
2494 ;; Unread articles are marked protected from
2495 ;; expiration Don't call
2496 ;; gnus-list-of-unread-articles as it returns
2497 ;; articles that have not been fetched into the
2500 (gnus-agent-unread-articles group)))
2502 ;; All articles EXCEPT those named by the caller
2503 ;; are protected from expiration
2504 (gnus-sorted-difference
2505 (gnus-uncompress-range
2507 (caar (last alist))))
2508 (sort articles '<)))))
2509 (marked ;; More articles that are exluded from the
2510 ;; expiration process
2511 (cond (gnus-agent-expire-all
2512 ;; All articles are unmarked by global decree
2515 ;; All articles are unmarked by function
2519 ;; All articles may as well be unmarked as the
2520 ;; unreads list already names the articles we are
2524 ;; Ticked and/or dormant articles are excluded
2527 (gnus-uncompress-range
2528 (cdr (assq 'tick (gnus-info-marks info))))
2529 (gnus-uncompress-range
2531 (gnus-info-marks info))))))))
2532 (nov-file (concat dir ".overview"))
2538 ;; The normal article alist contains elements that look like
2539 ;; (article# . fetch_date) I need to combine other
2540 ;; information with this list. For example, a flag indicating
2541 ;; that a particular article MUST BE KEPT. To do this, I'm
2542 ;; going to transform the elements to look like (article#
2543 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
2544 ;; the process to generate the expired article alist.
2546 ;; Convert the alist elements to (article# fetch_date nil
2548 (setq dlist (mapcar (lambda (e)
2549 (list (car e) (cdr e) nil nil)) alist))
2551 ;; Convert the keep lists to elements that look like (article#
2552 ;; nil keep_flag nil) then append it to the expanded dlist
2553 ;; These statements are sorted by ascending precidence of the
2555 (setq dlist (nconc dlist
2557 (list e nil 'unread nil))
2559 (setq dlist (nconc dlist
2561 (list e nil 'marked nil))
2563 (setq dlist (nconc dlist
2565 (list e nil 'special nil))
2568 (set-buffer overview)
2570 (buffer-disable-undo)
2571 (when (file-exists-p nov-file)
2572 (gnus-message 7 "gnus-agent-expire: Loading overview...")
2573 (nnheader-insert-file-contents nov-file)
2574 (goto-char (point-min))
2577 (while (< (setq p (point)) (point-max))
2579 ;; If I successfully read an integer (the plus zero
2580 ;; ensures a numeric type), prepend a marker entry
2582 (push (list (+ 0 (read (current-buffer))) nil nil
2583 (set-marker (make-marker) p))
2586 (gnus-message 1 "gnus-agent-expire: read error \
2587 occurred when reading expression at %s in %s. Skipping to next \
2588 line." (point) nov-file)))
2589 ;; Whether I succeeded, or failed, it doesn't matter.
2590 ;; Move to the next line then try again.
2594 7 "gnus-agent-expire: Loading overview... Done"))
2595 (set-buffer-modified-p nil)
2597 ;; At this point, all of the information is in dlist. The
2598 ;; only problem is that much of it is spread across multiple
2599 ;; entries. Sort then MERGE!!
2600 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
2601 ;; If two entries have the same article-number then sort by
2602 ;; ascending keep_flag.
2609 (cond ((< (nth 0 a) (nth 0 b))
2611 ((> (nth 0 a) (nth 0 b))
2614 (let ((a (or (symbol-value (nth 2 a))
2616 (b (or (symbol-value (nth 2 b))
2619 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
2620 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
2621 (let ((dlist dlist))
2622 (while (cdr dlist) ; I'm not at the end-of-list
2623 (if (eq (caar dlist) (caadr dlist))
2624 (let ((first (cdr (car dlist)))
2625 (secnd (cdr (cadr dlist))))
2626 (setcar first (or (car first)
2627 (car secnd))) ; fetch_date
2628 (setq first (cdr first)
2630 (setcar first (or (car first)
2631 (car secnd))) ; Keep_flag
2632 (setq first (cdr first)
2634 (setcar first (or (car first)
2635 (car secnd))) ; NOV_entry_marker
2637 (setcdr dlist (cddr dlist)))
2638 (setq dlist (cdr dlist)))))
2639 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
2641 (let* ((len (float (length dlist)))
2645 (let ((new-completed (truncate (* 100.0
2646 (/ (setq cnt (1+ cnt))
2648 (when (> new-completed completed)
2649 (setq completed new-completed)
2650 (gnus-message 7 "%3d%% completed..." completed)))
2651 (let* ((entry (car dlist))
2652 (article-number (nth 0 entry))
2653 (fetch-date (nth 1 entry))
2654 (keep (nth 2 entry))
2655 (marker (nth 3 entry)))
2658 ;; Kept articles are unread, marked, or special.
2660 (gnus-agent-message 10
2661 "gnus-agent-expire: Article %d: Kept %s article."
2662 article-number keep)
2664 (unless (file-exists-p
2665 (concat dir (number-to-string
2667 (setf (nth 1 entry) nil)
2668 (gnus-agent-message 3 "gnus-agent-expire cleared \
2669 download flag on article %d as the cached article file is missing."
2672 (gnus-message 1 "gnus-agent-expire detected a \
2673 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
2674 (gnus-agent-append-to-list
2676 (cons article-number fetch-date)))
2678 ;; The following articles are READ, UNMARKED, and
2679 ;; ORDINARY. See if they can be EXPIRED!!!
2682 ((not (integerp fetch-date))
2683 'read) ;; never fetched article (may expire
2685 ((not (file-exists-p
2686 (concat dir (number-to-string
2688 (setf (nth 1 entry) nil)
2689 'externally-expired) ;; Can't find the cached
2690 ;; article. Handle case
2691 ;; as though this article
2692 ;; was never fetched.
2694 ;; We now have the arrival day, so we see
2695 ;; whether it's old enough to be expired.
2701 ;; I found some reason to expire this entry.
2703 (let ((actions nil))
2704 (when (memq type '(forced expired))
2705 (ignore-errors ; Just being paranoid.
2706 (delete-file (concat dir (number-to-string
2708 (push "expired cached article" actions))
2709 (setf (nth 1 entry) nil)
2713 (push "NOV entry removed" actions)
2717 ;; If considering all articles is set, I can only
2718 ;; expire article IDs that are no longer in the
2720 (if (and gnus-agent-consider-all-articles
2721 (>= article-number (car active)))
2722 ;; I have to keep this ID in the alist
2723 (gnus-agent-append-to-list
2724 tail-alist (cons article-number fetch-date))
2725 (push (format "Removed %s article number from \
2726 article alist" type) actions))
2728 (gnus-agent-message 8 "gnus-agent-expire: Article %d: %s"
2730 (mapconcat 'identity actions ", "))))
2733 10 "gnus-agent-expire: Article %d: Article kept as \
2734 expiration tests failed." article-number)
2735 (gnus-agent-append-to-list
2736 tail-alist (cons article-number fetch-date)))
2739 ;; Clean up markers as I want to recycle this buffer
2740 ;; over several groups.
2742 (set-marker marker nil))
2744 (setq dlist (cdr dlist))))
2746 (setq alist (cdr alist))
2748 (let ((inhibit-quit t))
2749 (unless (equal alist gnus-agent-article-alist)
2750 (setq gnus-agent-article-alist alist)
2751 (gnus-agent-save-alist group))
2753 (when (buffer-modified-p)
2754 (gnus-make-directory dir)
2755 (write-region-as-coding-system gnus-agent-file-coding-system
2756 (point-min) (point-max) nov-file
2758 ;; clear the modified flag as that I'm not confused by
2759 ;; its status on the next pass through this routine.
2760 (set-buffer-modified-p nil))
2762 (when (eq articles t)
2763 (gnus-summary-update-info))))))))
2765 (defun gnus-agent-expire (&optional articles group force)
2766 "Expire all old articles.
2767 If you want to force expiring of certain articles, this function can
2768 take ARTICLES, GROUP and FORCE parameters as well.
2770 The articles on which the expiration process runs are selected as follows:
2771 if ARTICLES is null, all read and unmarked articles.
2772 if ARTICLES is t, all articles.
2773 if ARTICLES is a list, just those articles.
2774 Setting GROUP will limit expiration to that group.
2775 FORCE is equivalent to setting the expiration predicates to true."
2779 (gnus-agent-expire-group group articles force)
2780 (if (or (not (eq articles t))
2781 (yes-or-no-p "Are you sure that you want to expire all \
2782 articles in every agentized group."))
2783 (let ((methods gnus-agent-covered-methods)
2784 ;; Bind gnus-agent-expire-current-dirs to enable tracking
2785 ;; of agent directories.
2786 (gnus-agent-expire-current-dirs nil)
2787 gnus-command-method overview orig)
2788 (setq overview (gnus-get-buffer-create " *expire overview*"))
2790 (while (setq gnus-command-method (pop methods))
2791 (when (file-exists-p (gnus-agent-lib-file "active"))
2793 (nnheader-insert-file-contents
2794 (gnus-agent-lib-file "active"))
2795 (gnus-active-to-gnus-format
2797 (setq orig (gnus-make-hashtable
2798 (count-lines (point-min) (point-max))))))
2799 (dolist (expiring-group (gnus-groups-from-server
2800 gnus-command-method))
2802 (gnus-gethash-safe expiring-group orig)))
2806 (gnus-agent-expire-group-1
2807 expiring-group overview active articles force)))))))
2808 (kill-buffer overview))
2809 (gnus-agent-expire-unagentized-dirs)
2810 (gnus-message 4 "Expiry...done")))))
2812 (defun gnus-agent-expire-unagentized-dirs ()
2813 (when (boundp 'gnus-agent-expire-current-dirs)
2814 (let* ((keep (gnus-make-hashtable))
2815 ;; Formally bind gnus-agent-expire-current-dirs so that the
2816 ;; compiler will not complain about free references.
2817 (gnus-agent-expire-current-dirs
2818 (symbol-value 'gnus-agent-expire-current-dirs))
2821 (gnus-sethash gnus-agent-directory t keep)
2822 (while gnus-agent-expire-current-dirs
2823 (setq dir (pop gnus-agent-expire-current-dirs))
2824 (when (and (stringp dir)
2825 (file-directory-p dir))
2826 (while (not (gnus-gethash dir keep))
2827 (gnus-sethash dir t keep)
2828 (setq dir (file-name-directory (directory-file-name dir))))))
2835 (let ((files (directory-files d))
2837 (while (setq file (pop files))
2838 (cond ((equal file ".")
2842 ((equal file ".overview")
2843 (let ((d (file-name-as-directory d))
2845 (while (not (gnus-gethash
2846 (setq d (file-name-directory d)) keep))
2848 d (directory-file-name d)))
2850 (push r to-remove))))
2851 ((file-directory-p (setq file (nnheader-concat d file)))
2852 (funcall checker file)))))))))
2853 (funcall checker gnus-agent-directory)
2855 (when (and to-remove
2857 "gnus-agent-expire has identified local directories that are\
2858 not currently required by any agentized group. Do you wish to consider\
2861 (let ((dir (pop to-remove)))
2862 (if (gnus-y-or-n-p (format "Delete %s?" dir))
2863 (let* (delete-recursive
2868 (if (file-directory-p f-or-d)
2870 (delete-directory f-or-d)
2873 (or (member f '("." ".."))
2874 (funcall delete-recursive
2877 (directory-files f-or-d))
2878 (delete-directory f-or-d)))
2879 (delete-file f-or-d)))))))
2880 (funcall delete-recursive dir))))))))))
2883 (defun gnus-agent-batch ()
2884 "Start Gnus, send queue and fetch session."
2886 (let ((init-file-user "")
2887 (gnus-always-read-dribble-file t))
2889 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2890 (gnus-group-send-queue)
2891 (gnus-agent-fetch-session)))
2893 (defun gnus-agent-unread-articles (group)
2894 (let* ((read (gnus-info-read (gnus-get-info group)))
2895 (known (gnus-agent-load-alist group))
2897 (tail-unread unread))
2898 (while (and known read)
2899 (let ((candidate (car (pop known))))
2900 (while (let* ((range (car read))
2901 (min (if (numberp range) range (car range)))
2902 (max (if (numberp range) range (cdr range))))
2903 (cond ((or (not min)
2905 (gnus-agent-append-to-list tail-unread candidate)
2910 (gnus-agent-append-to-list tail-unread (car (pop known))))
2913 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
2914 "Restrict ARTICLES to numbers already fetched.
2915 Returns a sublist of ARTICLES that excludes thos article ids in GROUP
2916 that have already been fetched.
2917 If CACHED-HEADER is nil, articles are only excluded if the article itself
2920 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
2921 ;; 'car gnus-agent-article-alist))
2923 ;; Functionally, I don't need to construct a temp list using mapcar.
2925 (if (and (or gnus-agent-cache (not gnus-plugged))
2926 (gnus-agent-load-alist group))
2927 (let* ((ref gnus-agent-article-alist)
2929 (uncached (list nil))
2930 (tail-uncached uncached))
2931 (while (and ref arts)
2932 (let ((v1 (car arts))
2934 (cond ((< v1 v2) ; v1 does not appear in the reference list
2935 (gnus-agent-append-to-list tail-uncached v1)
2938 (unless (or cached-header (cdar ref)) ; v1 is already cached
2939 (gnus-agent-append-to-list tail-uncached v1))
2942 (t ; reference article (v2) preceeds the list being filtered
2945 (gnus-agent-append-to-list tail-uncached (pop arts)))
2947 ;; if gnus-agent-load-alist fails, no articles are cached.
2950 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
2952 (gnus-agent-create-buffer)
2953 (let ((gnus-decode-encoded-word-function 'identity)
2954 (file (gnus-agent-article-name ".overview" group))
2955 cached-articles uncached-articles)
2956 (gnus-make-directory (nnheader-translate-file-chars
2957 (file-name-directory file) t))
2959 ;; Populate temp buffer with known headers
2960 (when (file-exists-p file)
2961 (with-current-buffer gnus-agent-overview-buffer
2963 (let ((nnheader-file-coding-system
2964 gnus-agent-file-coding-system))
2965 (nnheader-insert-nov-file file (car articles)))))
2967 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
2970 ;; Populate nntp-server-buffer with uncached headers
2971 (set-buffer nntp-server-buffer)
2973 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
2974 (gnus-retrieve-headers
2975 uncached-articles group fetch-old))))
2976 (nnvirtual-convert-headers))
2977 ((eq 'nntp (car gnus-current-select-method))
2978 ;; The author of gnus-get-newsgroup-headers-xover
2979 ;; reports that the XOVER command is commonly
2980 ;; unreliable. The problem is that recently
2981 ;; posted articles may not be entered into the
2982 ;; NOV database in time to respond to my XOVER
2985 ;; I'm going to use his assumption that the NOV
2986 ;; database is updated in order of ascending
2987 ;; article ID. Therefore, a response containing
2988 ;; article ID N implies that all articles from 1
2989 ;; to N-1 are up-to-date. Therefore, missing
2990 ;; articles in that range have expired.
2992 (set-buffer nntp-server-buffer)
2993 (let* ((fetched-articles (list nil))
2994 (tail-fetched-articles fetched-articles)
2995 (min (cond ((numberp fetch-old)
2996 (max 1 (- (car articles) fetch-old)))
3001 (max (car (last articles))))
3003 ;; Get the list of articles that were fetched
3004 (goto-char (point-min))
3005 (let ((pm (point-max)))
3006 (while (< (point) pm)
3007 (when (looking-at "[0-9]+\t")
3008 (gnus-agent-append-to-list
3009 tail-fetched-articles
3010 (read (current-buffer))))
3013 ;; Clip this list to the headers that will
3014 ;; actually be returned
3015 (setq fetched-articles (gnus-list-range-intersection
3016 (cdr fetched-articles)
3019 ;; Clip the uncached articles list to exclude
3020 ;; IDs after the last FETCHED header. The
3021 ;; excluded IDs may be fetchable using HEAD.
3022 (if (car tail-fetched-articles)
3023 (setq uncached-articles
3024 (gnus-list-range-intersection
3026 (cons (car uncached-articles)
3027 (car tail-fetched-articles)))))
3029 ;; Create the list of articles that were
3030 ;; "successfully" fetched. Success, in this
3031 ;; case, means that the ID should not be
3032 ;; fetched again. In the case of an expired
3033 ;; article, the header will not be fetched.
3034 (setq uncached-articles
3035 (gnus-sorted-nunion fetched-articles
3039 ;; Erase the temp buffer
3040 (set-buffer gnus-agent-overview-buffer)
3043 ;; Copy the nntp-server-buffer to the temp buffer
3044 (set-buffer nntp-server-buffer)
3045 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3047 ;; Merge the temp buffer with the known headers (found on
3048 ;; disk in FILE) into the nntp-server-buffer
3049 (when (and uncached-articles (file-exists-p file))
3050 (gnus-agent-braid-nov group uncached-articles file))
3052 ;; Save the new set of known headers to FILE
3053 (set-buffer nntp-server-buffer)
3054 (gnus-agent-check-overview-buffer)
3055 (write-region-as-coding-system
3056 gnus-agent-file-coding-system
3057 (point-min) (point-max) file nil 'silent)
3059 ;; Update the group's article alist to include the newly
3060 ;; fetched articles.
3061 (gnus-agent-load-alist group)
3062 (gnus-agent-save-alist group uncached-articles nil)
3065 ;; Copy the temp buffer to the nntp-server-buffer
3066 (set-buffer nntp-server-buffer)
3068 (insert-buffer-substring gnus-agent-overview-buffer)))
3071 (not (numberp fetch-old)))
3072 t ; Don't remove anything.
3073 (nnheader-nov-delete-outside-range
3074 (if fetch-old (max 1 (- (car articles) fetch-old))
3076 (car (last articles)))
3081 (defun gnus-agent-request-article (article group)
3082 "Retrieve ARTICLE in GROUP from the agent cache."
3083 (when (and gnus-agent
3084 (or gnus-agent-cache
3087 (let* ((gnus-command-method (gnus-find-method-for-group group))
3089 (gnus-agent-directory)
3090 (gnus-agent-group-path group) "/"
3091 (number-to-string article)))
3092 (buffer-read-only nil))
3093 (when (and (file-exists-p file)
3094 (> (nth 7 (file-attributes file)) 0))
3096 (gnus-kill-all-overlays)
3097 (insert-file-contents-as-coding-system gnus-cache-coding-system file)
3100 (defun gnus-agent-regenerate-group (group &optional reread)
3102 If REREAD is t, all articles in the .overview are marked as unread.
3103 If REREAD is not nil, downloaded articles are marked as unread."
3105 (list (let ((def (or (gnus-group-group-name)
3106 gnus-newsgroup-name)))
3107 (let ((select (read-string (if def
3108 (concat "Group Name ("
3111 (if (and (equal "" select)
3117 "Reread (nil)? (t=>all, nil=>none, some=>all downloaded): "))))
3118 (gnus-message 5 "Regenerating in %s" group)
3119 (let* ((gnus-command-method (or gnus-command-method
3120 (gnus-find-method-for-group group)))
3121 (file (gnus-agent-article-name ".overview" group))
3122 (dir (file-name-directory file))
3124 (downloaded (if (file-exists-p dir)
3125 (sort (mapcar (lambda (name) (string-to-int name))
3126 (directory-files dir nil "^[0-9]+$" t))
3128 (progn (gnus-make-directory dir) nil)))
3133 (mm-with-unibyte-buffer
3134 (if (file-exists-p file)
3135 (let ((nnheader-file-coding-system
3136 gnus-agent-file-coding-system))
3137 (nnheader-insert-file-contents file)))
3138 (set-buffer-modified-p nil)
3140 ;; Load the article IDs found in the overview file. As a
3141 ;; side-effect, validate the file contents.
3145 (goto-char (point-min))
3146 (while (< (point) (point-max))
3147 (cond ((and (looking-at "[0-9]+\t")
3148 (<= (- (match-end 0) (match-beginning 0)) 9))
3149 (push (read (current-buffer)) nov-arts)
3151 (let ((l1 (car nov-arts))
3152 (l2 (cadr nov-arts)))
3156 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3157 entries are NOT in ascending order.")
3158 ;; Don't sort now as I haven't verified
3159 ;; that every line begins with a number
3163 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3164 entries contained duplicate of article %s. Duplicate deleted." l1)
3168 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3169 entries contained line that did not begin with an article number. Deleted\
3171 (gnus-delete-line))))
3174 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3175 entries into ascending order.")
3176 (sort-numeric-fields 1 (point-min) (point-max))
3177 (setq nov-arts nil)))))
3178 (gnus-agent-check-overview-buffer)
3180 ;; Construct a new article alist whose nodes match every header
3181 ;; in the .overview file. As a side-effect, missing headers are
3182 ;; reconstructed from the downloaded article file.
3183 (while (or downloaded nov-arts)
3184 (cond ((and downloaded
3186 (> (car downloaded) (car nov-arts))))
3187 ;; This entry is missing from the overview file
3188 (gnus-message 3 "Regenerating NOV %s %d..." group
3190 (let ((file (concat dir (number-to-string (car downloaded)))))
3191 (mm-with-unibyte-buffer
3192 (nnheader-insert-file-contents file)
3193 (nnheader-remove-body)
3194 (setq header (nnheader-parse-naked-head)))
3195 (mail-header-set-number header (car downloaded))
3197 (let ((key (concat "^" (int-to-string (car nov-arts))
3199 (or (re-search-backward key nil t)
3200 (re-search-forward key))
3202 (goto-char (point-min)))
3203 (nnheader-insert-nov header))
3204 (setq nov-arts (cons (car downloaded) nov-arts)))
3205 ((eq (car downloaded) (car nov-arts))
3206 ;; This entry in the overview has been downloaded
3207 (push (cons (car downloaded)
3209 (nth 5 (file-attributes
3210 (concat dir (number-to-string
3211 (car downloaded))))))) alist)
3215 ;; This entry in the overview has not been downloaded
3216 (push (cons (car nov-arts) nil) alist)
3219 ;; When gnus-agent-consider-all-articles is set,
3220 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3221 ;; the alist. Those IDs serve as markers to indicate that an
3222 ;; attempt has been made to fetch that article's header.
3224 ;; When gnus-agent-consider-all-articles is NOT set,
3225 ;; gnus-agent-regenerate-group can remove the article ID of every
3226 ;; article (with the exception of the last ID in the list - it's
3227 ;; special) that no longer appears in the overview. In this
3228 ;; situtation, the last article ID in the list implies that it,
3229 ;; and every article ID preceeding it, have been fetched from the
3231 (if gnus-agent-consider-all-articles
3232 ;; Restore all article IDs that were not found in the overview file.
3233 (let* ((n (cons nil alist))
3235 (o (gnus-agent-load-alist group)))
3237 (let ((nID (caadr n))
3240 (setq n (setcdr n (list (list oID))))
3243 (setcdr n (cons (list oID) (cdr n)))
3250 (setq alist (cdr merged)))
3251 ;; Restore the last article ID if it is not already in the new alist
3252 (let ((n (last alist))
3253 (o (last (gnus-agent-load-alist group))))
3257 (push (cons (caar o) nil) alist))
3258 ((< (caar n) (caar o))
3259 (setcdr n (list (car o)))))))
3261 (let ((inhibit-quit t))
3262 (if (setq regenerated (buffer-modified-p))
3263 (write-region-as-coding-system
3264 gnus-agent-file-coding-system
3265 (point-min) (point-max) file nil 'silent))
3267 (setq regenerated (or regenerated
3268 (and reread gnus-agent-article-alist)
3269 (not (equal alist gnus-agent-article-alist)))
3272 (setq gnus-agent-article-alist alist)
3275 (gnus-agent-save-alist group)))
3278 (when (and reread gnus-agent-article-alist)
3279 (gnus-make-ascending-articles-unread
3281 (delq nil (mapcar (function (lambda (c)
3282 (cond ((eq reread t)
3286 gnus-agent-article-alist)))
3288 (when (gnus-buffer-live-p gnus-group-buffer)
3289 (gnus-group-update-group group t)
3293 (gnus-message 5 nil)
3297 (defun gnus-agent-regenerate (&optional clean reread)
3298 "Regenerate all agent covered files.
3299 If CLEAN, don't read existing active files."
3302 (gnus-message 4 "Regenerating Gnus agent files...")
3303 (dolist (gnus-command-method gnus-agent-covered-methods)
3304 (let ((active-file (gnus-agent-lib-file "active"))
3305 active-hashtb active-changed
3307 (gnus-make-directory (file-name-directory active-file))
3309 (setq active-hashtb (gnus-make-hashtable 1000))
3310 (mm-with-unibyte-buffer
3311 (if (file-exists-p active-file)
3312 (let ((nnheader-file-coding-system
3313 gnus-agent-file-coding-system))
3314 (nnheader-insert-file-contents active-file))
3315 (setq active-changed t))
3316 (gnus-active-to-gnus-format
3317 nil (setq active-hashtb
3318 (gnus-make-hashtable
3319 (count-lines (point-min) (point-max)))))))
3320 (dolist (group (gnus-groups-from-server gnus-command-method))
3321 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3323 (let ((min (or (caar gnus-agent-article-alist) 1))
3324 (max (or (caar (last gnus-agent-article-alist)) 0))
3325 (active (gnus-gethash-safe (gnus-group-real-name group)
3327 (read (gnus-info-read (gnus-get-info group))))
3330 (setq active (cons min max)
3332 (gnus-sethash group active active-hashtb))
3333 (when (> (car active) min)
3335 (setq active-changed t))
3336 (when (< (cdr active) max)
3338 (setq active-changed t)))))
3339 (when active-changed
3340 (setq regenerated t)
3341 (gnus-message 4 "Regenerate %s" active-file)
3342 (let ((nnmail-active-file-coding-system
3343 gnus-agent-file-coding-system))
3344 (gnus-write-active-file active-file active-hashtb)))))
3345 (gnus-message 4 "Regenerating Gnus agent files...done")
3348 (defun gnus-agent-go-online (&optional force)
3349 "Switch servers into online status."
3350 (interactive (list t))
3351 (dolist (server gnus-opened-servers)
3352 (when (eq (nth 1 server) 'offline)
3353 (if (if (eq force 'ask)
3355 (format "Switch %s:%s into online status? "
3356 (caar server) (cadar server)))
3358 (setcar (nthcdr 1 server) 'close)))))
3360 (defun gnus-agent-toggle-group-plugged (group)
3361 "Toggle the status of the server of the current group."
3362 (interactive (list (gnus-group-group-name)))
3363 (let* ((method (gnus-find-method-for-group group))
3364 (status (cadr (assoc method gnus-opened-servers))))
3365 (if (eq status 'offline)
3366 (gnus-server-set-status method 'closed)
3367 (gnus-close-server method)
3368 (gnus-server-set-status method 'offline))
3369 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3370 (if (eq status 'offline) 'offline 'online)
3371 (if (eq status 'offline) 'online 'offline))))
3373 (defun gnus-agent-group-covered-p (group)
3374 (member (gnus-group-method group)
3375 gnus-agent-covered-methods))
3377 (add-hook 'gnus-group-prepare-hook
3381 (when (listp gnus-agent-expire-days)
3384 (gnus-message 1 "WARNING: gnus-agent-expire-days no longer\
3385 supports being set to a list.")(sleep-for 3)
3386 (gnus-message 1 "Change your configuration to set it to an\
3387 integer.")(sleep-for 3)
3388 (gnus-message 1 "I am now setting group parameters on each\
3389 group to match the configuration that the list offered.")
3392 (let ((groups (gnus-group-listed-groups)))
3394 (let* ((group (pop groups))
3395 (days gnus-agent-expire-days)
3398 (when (eq 0 (string-match
3401 (throw 'found (cadar days)))
3405 (gnus-group-set-parameter group 'agent-days-until-old
3408 (let ((h gnus-group-prepare-hook))
3410 (let ((func (pop h)))
3411 (when (and (listp func)
3412 (eq (cadr (caddr func)) 'gnus-agent-do-once))
3413 (remove-hook 'gnus-group-prepare-hook func)
3416 (gnus-message 1 "I have finished setting group parameters on\
3417 each group. You may now customize your groups and/or topics to control the\
3420 (provide 'gnus-agent)
3422 ;;; gnus-agent.el ends here