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))
40 (if (featurep 'xemacs)
43 (require 'gnus-group))
46 (autoload 'gnus-server-update-server "gnus-srvr")
47 (autoload 'gnus-agent-customize-category "gnus-cus")
50 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
51 "Where the Gnus agent will store its files."
55 (defcustom gnus-agent-plugged-hook nil
56 "Hook run when plugging into the network."
60 (defcustom gnus-agent-unplugged-hook nil
61 "Hook run when unplugging from the network."
65 (defcustom gnus-agent-fetched-hook nil
66 "Hook run when finished fetching articles."
70 (defcustom gnus-agent-handle-level gnus-level-subscribed
71 "Groups on levels higher than this variable will be ignored by the Agent."
75 (defcustom gnus-agent-expire-days 7
76 "Read articles older than this will be expired.
77 This can also be a list of regexp/day pairs. The regexps will be
78 matched against group names."
80 :type '(choice (number :tag "days")
81 (sexp :tag "List" nil)))
83 (defcustom gnus-agent-expire-all nil
84 "If non-nil, also expire unread, ticked and dormant articles.
85 If nil, only read articles will be expired."
89 (defcustom gnus-agent-group-mode-hook nil
90 "Hook run in Agent group minor modes."
94 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
95 (when (featurep 'xemacs)
96 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
98 (defcustom gnus-agent-summary-mode-hook nil
99 "Hook run in Agent summary minor modes."
103 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
104 (when (featurep 'xemacs)
105 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
107 (defcustom gnus-agent-server-mode-hook nil
108 "Hook run in Agent summary minor modes."
112 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
113 (when (featurep 'xemacs)
114 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
116 (defcustom gnus-agent-confirmation-function 'y-or-n-p
117 "Function to confirm when error happens."
122 (defcustom gnus-agent-large-newsgroup nil
123 "*The number of articles which indicates a large newsgroup.
124 If the number of unread articles exceeds it, The number of articles to be
125 fetched will be limited to it. If not a positive integer, never consider it."
127 :type '(choice (const nil)
128 (integer :tag "Number")))
130 (defcustom gnus-agent-synchronize-flags 'ask
131 "Indicate if flags are synchronized when you plug in.
132 If this is `ask' the hook will query the user."
134 :type '(choice (const :tag "Always" t)
135 (const :tag "Never" nil)
136 (const :tag "Ask" ask))
139 (defcustom gnus-agent-go-online 'ask
140 "Indicate if offline servers go online when you plug in.
141 If this is `ask' the hook will query the user."
143 :type '(choice (const :tag "Always" t)
144 (const :tag "Never" nil)
145 (const :tag "Ask" ask))
148 (defcustom gnus-agent-mark-unread-after-downloaded t
149 "Indicate whether to mark articles unread after downloaded."
154 (defcustom gnus-agent-download-marks '(download)
155 "Marks for downloading."
157 :type '(repeat (symbol :tag "Mark"))
160 (defcustom gnus-agent-consider-all-articles nil
161 "If non-nil, consider also the read articles for downloading."
166 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
167 "Chunk size for `gnus-agent-fetch-session'.
168 The function will split its article fetches into chunks smaller than
173 (defcustom gnus-agent-enable-expiration 'ENABLE
174 "The default expiration state for each group.
175 When set to ENABLE, the default, `gnus-agent-expire' will expire old
176 contents from a group's local storage. This value may be overridden
177 to disable expiration in specific categories, topics, and groups. Of
178 course, you could change gnus-agent-enable-expiration to DISABLE then
179 enable expiration per categories, topics, and groups."
181 :type '(radio (const :format "Enable " ENABLE)
182 (const :format "Disable " DISABLE)))
184 (defcustom gnus-agent-expire-unagentized-dirs t
185 "Have gnus-agent-expire scan the directories under
186 \(gnus-agent-directory) for groups that are no longer agentized. When
187 found, offer to remove them.")
189 ;;; Internal variables
191 (defvar gnus-agent-history-buffers nil)
192 (defvar gnus-agent-buffer-alist nil)
193 (defvar gnus-agent-article-alist nil
194 "An assoc list identifying the articles whose headers have been fetched.
195 If successfully fetched, these headers will be stored in the group's overview
196 file. The key of each assoc pair is the article ID, the value of each assoc
197 pair is a flag indicating whether the identified article has been downloaded
198 \(gnus-agent-fetch-articles sets the value to the day of the download).
200 1) The last element of this list can not be expired as some
201 routines (for example, get-agent-fetch-headers) use the last
202 value to track which articles have had their headers retrieved.
203 2) The function `gnus-agent-regenerate' may destructively modify the value.")
204 (defvar gnus-agent-group-alist nil)
205 (defvar gnus-category-alist nil)
206 (defvar gnus-agent-current-history nil)
207 (defvar gnus-agent-overview-buffer nil)
208 (defvar gnus-category-predicate-cache nil)
209 (defvar gnus-category-group-cache nil)
210 (defvar gnus-agent-spam-hashtb nil)
211 (defvar gnus-agent-file-name nil)
212 (defvar gnus-agent-send-mail-function nil)
213 (defvar gnus-agent-file-coding-system 'raw-text)
214 (defvar gnus-agent-file-loading-cache nil)
215 (defvar gnus-agent-file-header-cache nil)
217 (defvar gnus-agent-auto-agentize-methods '(nntp nnimap)
218 "Initially, all servers from these methods are agentized.
219 The user may remove or add servers using the Server buffer. See Info
220 node `(gnus)Server Buffer'.")
223 (defvar gnus-headers)
230 (defun gnus-open-agent ()
232 (gnus-agent-read-servers)
234 (gnus-agent-create-buffer)
235 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
236 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
237 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
239 (defun gnus-agent-create-buffer ()
240 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
242 (setq gnus-agent-overview-buffer
243 (gnus-get-buffer-create " *Gnus agent overview*"))
244 (with-current-buffer gnus-agent-overview-buffer
245 (set-buffer-multibyte t))
248 (gnus-add-shutdown 'gnus-close-agent 'gnus)
250 (defun gnus-close-agent ()
251 (setq gnus-category-predicate-cache nil
252 gnus-category-group-cache nil
253 gnus-agent-spam-hashtb nil)
254 (gnus-kill-buffer gnus-agent-overview-buffer))
257 ;;; Utility functions
260 (defun gnus-agent-read-file (file)
261 "Load FILE and do a `read' there."
264 (nnheader-insert-file-contents file)
265 (goto-char (point-min))
266 (read (current-buffer)))))
268 (defsubst gnus-agent-method ()
269 (concat (symbol-name (car gnus-command-method)) "/"
270 (if (equal (cadr gnus-command-method) "")
272 (cadr gnus-command-method))))
274 (defsubst gnus-agent-directory ()
275 "The name of the Gnus agent directory."
276 (nnheader-concat gnus-agent-directory
277 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
279 (defun gnus-agent-lib-file (file)
280 "The full name of the Gnus agent library FILE."
281 (expand-file-name file
282 (file-name-as-directory
283 (expand-file-name "agent.lib" (gnus-agent-directory)))))
285 (defun gnus-agent-cat-set-property (category property value)
287 (setcdr (or (assq property category)
288 (let ((cell (cons property nil)))
289 (setcdr category (cons cell (cdr category)))
291 (let ((category category))
292 (while (cond ((eq property (caadr category))
293 (setcdr category (cddr category))
296 (setq category (cdr category)))))))
300 (defmacro gnus-agent-cat-defaccessor (name prop-name)
301 "Define accessor and setter methods for manipulating a list of the form
302 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
303 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
304 manipulated as follows:
305 (func LIST): Returns VALUE1
306 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
307 `(progn (defmacro ,name (category)
308 (list (quote cdr) (list (quote assq)
309 (quote (quote ,prop-name)) category)))
311 (define-setf-method ,name (category)
312 (let* ((--category--temp-- (make-symbol "--category--"))
313 (--value--temp-- (make-symbol "--value--")))
314 (list (list --category--temp--) ; temporary-variables
315 (list category) ; value-forms
316 (list --value--temp--) ; store-variables
317 (let* ((category --category--temp--) ; store-form
318 (value --value--temp--))
319 (list (quote gnus-agent-cat-set-property)
321 (quote (quote ,prop-name))
323 (list (quote ,name) --category--temp--) ; access-form
327 (defmacro gnus-agent-cat-name (category)
330 (gnus-agent-cat-defaccessor
331 gnus-agent-cat-days-until-old agent-days-until-old)
332 (gnus-agent-cat-defaccessor
333 gnus-agent-cat-enable-expiration agent-enable-expiration)
334 (gnus-agent-cat-defaccessor
335 gnus-agent-cat-groups agent-groups)
336 (gnus-agent-cat-defaccessor
337 gnus-agent-cat-high-score agent-high-score)
338 (gnus-agent-cat-defaccessor
339 gnus-agent-cat-length-when-long agent-length-when-long)
340 (gnus-agent-cat-defaccessor
341 gnus-agent-cat-length-when-short agent-length-when-short)
342 (gnus-agent-cat-defaccessor
343 gnus-agent-cat-low-score agent-low-score)
344 (gnus-agent-cat-defaccessor
345 gnus-agent-cat-predicate agent-predicate)
346 (gnus-agent-cat-defaccessor
347 gnus-agent-cat-score-file agent-score-file)
348 (gnus-agent-cat-defaccessor
349 gnus-agent-cat-disable-undownloaded-faces agent-disable-undownloaded-faces)
352 (defsetf gnus-agent-cat-groups (category) (groups)
353 (list 'gnus-agent-set-cat-groups category groups)))
355 (defun gnus-agent-set-cat-groups (category groups)
356 (unless (eq groups 'ignore)
358 (old-g (gnus-agent-cat-groups category)))
359 (cond ((eq new-g old-g)
360 ;; gnus-agent-add-group is fiddling with the group
361 ;; list. Still, Im done.
364 ((eq new-g (cdr old-g))
365 ;; gnus-agent-add-group is fiddling with the group list
366 (setcdr (or (assq 'agent-groups category)
367 (let ((cell (cons 'agent-groups nil)))
368 (setcdr category (cons cell (cdr category)))
371 (let ((groups groups))
373 (let* ((group (pop groups))
374 (old-category (gnus-group-category group)))
375 (if (eq category old-category)
377 (setf (gnus-agent-cat-groups old-category)
378 (delete group (gnus-agent-cat-groups
380 ;; Purge cache as preceeding loop invalidated it.
381 (setq gnus-category-group-cache nil))
383 (setcdr (or (assq 'agent-groups category)
384 (let ((cell (cons 'agent-groups nil)))
385 (setcdr category (cons cell (cdr category)))
388 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
389 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
391 ;;; Fetching setup functions.
393 (defun gnus-agent-start-fetch ()
394 "Initialize data structures for efficient fetching."
395 (gnus-agent-create-buffer))
397 (defun gnus-agent-stop-fetch ()
398 "Save all data structures and clean up."
399 (setq gnus-agent-spam-hashtb nil)
401 (set-buffer nntp-server-buffer)
404 (defmacro gnus-agent-with-fetch (&rest forms)
407 (let ((gnus-agent-fetching t))
408 (gnus-agent-start-fetch)
410 (gnus-agent-stop-fetch)))
412 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
413 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
415 (defmacro gnus-agent-append-to-list (tail value)
416 `(setq ,tail (setcdr ,tail (cons ,value nil))))
418 (defmacro gnus-agent-message (level &rest args)
419 `(if (<= ,level gnus-verbose)
426 (defvar gnus-agent-mode-hook nil
427 "Hook run when installing agent mode.")
429 (defvar gnus-agent-mode nil)
430 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
432 (defun gnus-agent-mode ()
433 "Minor mode for providing a agent support in Gnus buffers."
434 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
435 (symbol-name major-mode))
436 (match-string 1 (symbol-name major-mode))))
437 (mode (intern (format "gnus-agent-%s-mode" buffer))))
438 (set (make-local-variable 'gnus-agent-mode) t)
440 (set (make-local-variable mode) t)
442 (when (gnus-visual-p 'agent-menu 'menu)
443 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
444 (unless (assq 'gnus-agent-mode minor-mode-alist)
445 (push gnus-agent-mode-status minor-mode-alist))
446 (unless (assq mode minor-mode-map-alist)
447 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
449 minor-mode-map-alist))
450 (when (eq major-mode 'gnus-group-mode)
451 (let ((init-plugged gnus-plugged)
452 (gnus-agent-go-online nil))
453 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
454 ;; Therefore, make certain that the current value does not
455 ;; match the desired initial value.
456 (setq gnus-plugged :unknown)
457 (gnus-agent-toggle-plugged init-plugged)))
458 (gnus-run-hooks 'gnus-agent-mode-hook
459 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
461 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
462 (gnus-define-keys gnus-agent-group-mode-map
463 "Ju" gnus-agent-fetch-groups
464 "Jc" gnus-enter-category-buffer
465 "Jj" gnus-agent-toggle-plugged
466 "Js" gnus-agent-fetch-session
467 "JY" gnus-agent-synchronize-flags
468 "JS" gnus-group-send-queue
469 "Ja" gnus-agent-add-group
470 "Jr" gnus-agent-remove-group
471 "Jo" gnus-agent-toggle-group-plugged)
473 (defun gnus-agent-group-make-menu-bar ()
474 (unless (boundp 'gnus-agent-group-menu)
476 gnus-agent-group-menu gnus-agent-group-mode-map ""
478 ["Toggle plugged" gnus-agent-toggle-plugged t]
479 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
480 ["List categories" gnus-enter-category-buffer t]
481 ["Add (current) group to category" gnus-agent-add-group t]
482 ["Remove (current) group from category" gnus-agent-remove-group t]
483 ["Send queue" gnus-group-send-queue gnus-plugged]
485 ["All" gnus-agent-fetch-session gnus-plugged]
486 ["Group" gnus-agent-fetch-group gnus-plugged])
487 ["Synchronize flags" gnus-agent-synchronize-flags t]
490 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
491 (gnus-define-keys gnus-agent-summary-mode-map
492 "Jj" gnus-agent-toggle-plugged
493 "Ju" gnus-agent-summary-fetch-group
494 "JS" gnus-agent-fetch-group
495 "Js" gnus-agent-summary-fetch-series
496 "J#" gnus-agent-mark-article
497 "J\M-#" gnus-agent-unmark-article
498 "@" gnus-agent-toggle-mark
499 "Jc" gnus-agent-catchup)
501 (defun gnus-agent-summary-make-menu-bar ()
502 (unless (boundp 'gnus-agent-summary-menu)
504 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
506 ["Toggle plugged" gnus-agent-toggle-plugged t]
507 ["Mark as downloadable" gnus-agent-mark-article t]
508 ["Unmark as downloadable" gnus-agent-unmark-article t]
509 ["Toggle mark" gnus-agent-toggle-mark t]
510 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
511 ["Catchup undownloaded" gnus-agent-catchup t]))))
513 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
514 (gnus-define-keys gnus-agent-server-mode-map
515 "Jj" gnus-agent-toggle-plugged
516 "Ja" gnus-agent-add-server
517 "Jr" gnus-agent-remove-server)
519 (defun gnus-agent-server-make-menu-bar ()
520 (unless (boundp 'gnus-agent-server-menu)
522 gnus-agent-server-menu gnus-agent-server-mode-map ""
524 ["Toggle plugged" gnus-agent-toggle-plugged t]
525 ["Add" gnus-agent-add-server t]
526 ["Remove" gnus-agent-remove-server t]))))
528 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
529 (if (and (fboundp 'propertize)
530 (fboundp 'make-mode-line-mouse-map))
531 (propertize string 'local-map
532 (make-mode-line-mouse-map mouse-button mouse-func))
535 (defun gnus-agent-toggle-plugged (set-to)
536 "Toggle whether Gnus is unplugged or not."
537 (interactive (list (not gnus-plugged)))
538 (cond ((eq set-to gnus-plugged)
541 (setq gnus-plugged set-to)
542 (gnus-run-hooks 'gnus-agent-plugged-hook)
543 (setcar (cdr gnus-agent-mode-status)
544 (gnus-agent-make-mode-line-string " Plugged"
546 'gnus-agent-toggle-plugged))
547 (gnus-agent-go-online gnus-agent-go-online)
548 (gnus-agent-possibly-synchronize-flags))
550 (gnus-agent-close-connections)
551 (setq gnus-plugged set-to)
552 (gnus-run-hooks 'gnus-agent-unplugged-hook)
553 (setcar (cdr gnus-agent-mode-status)
554 (gnus-agent-make-mode-line-string " Unplugged"
556 'gnus-agent-toggle-plugged))))
557 (force-mode-line-update)
558 (set-buffer-modified-p t))
560 (defmacro gnus-agent-while-plugged (&rest body)
561 `(let ((original-gnus-plugged gnus-plugged))
563 (progn (gnus-agent-toggle-plugged t)
565 (gnus-agent-toggle-plugged original-gnus-plugged))))
567 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
568 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
570 (defun gnus-agent-close-connections ()
571 "Close all methods covered by the Gnus agent."
572 (let ((methods gnus-agent-covered-methods))
574 (gnus-close-server (pop methods)))))
577 (defun gnus-unplugged ()
578 "Start Gnus unplugged."
580 (setq gnus-plugged nil)
584 (defun gnus-plugged ()
585 "Start Gnus plugged."
587 (setq gnus-plugged t)
591 (defun gnus-slave-unplugged (&optional arg)
592 "Read news as a slave unplugged."
594 (setq gnus-plugged nil)
595 (gnus arg nil 'slave))
598 (defun gnus-agentize ()
599 "Allow Gnus to be an offline newsreader.
601 The gnus-agentize function is now called internally by gnus when
602 gnus-agent is set. If you wish to avoid calling gnus-agentize,
603 customize gnus-agent to nil.
605 This will modify the `gnus-setup-news-hook', and
606 `message-send-mail-real-function' variables, and install the Gnus agent
607 minor mode in all Gnus buffers."
610 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
611 (unless gnus-agent-send-mail-function
612 (setq gnus-agent-send-mail-function
613 (or message-send-mail-real-function
614 message-send-mail-function)
615 message-send-mail-real-function 'gnus-agent-send-mail))
617 (unless gnus-agent-covered-methods
620 (if (memq (car (gnus-server-to-method server))
621 gnus-agent-auto-agentize-methods)
622 (setq gnus-agent-covered-methods
623 (cons (gnus-server-to-method server)
624 gnus-agent-covered-methods ))))
625 (append (list gnus-select-method) gnus-secondary-select-methods))))
627 (defun gnus-agent-queue-setup (&optional group-name)
628 "Make sure the queue group exists.
629 Optional arg GROUP-NAME allows to specify another group."
630 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
632 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
633 (let ((gnus-level-default-subscribed 1))
634 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
636 (gnus-group-set-parameter
637 (format "nndraft:%s" (or group-name "queue"))
638 'gnus-dummy '((gnus-draft-mode)))))
640 (defun gnus-agent-send-mail ()
642 (funcall gnus-agent-send-mail-function)
643 (goto-char (point-min))
645 (concat "^" (regexp-quote mail-header-separator) "\n"))
647 (gnus-agent-insert-meta-information 'mail)
648 (gnus-request-accept-article "nndraft:queue" nil t t)))
650 (defun gnus-agent-insert-meta-information (type &optional method)
651 "Insert meta-information into the message that says how it's to be posted.
652 TYPE can be either `mail' or `news'. If the latter, then METHOD can
655 (message-remove-header gnus-agent-meta-information-header)
656 (goto-char (point-min))
657 (insert gnus-agent-meta-information-header ": "
658 (symbol-name type) " " (format "%S" method)
661 (while (search-backward "\n" nil t)
662 (replace-match "\\n" t t))))
664 (defun gnus-agent-restore-gcc ()
665 "Restore GCC field from saved header."
667 (goto-char (point-min))
668 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
669 (replace-match "Gcc:" 'fixedcase))))
671 (defun gnus-agent-any-covered-gcc ()
673 (message-narrow-to-headers)
674 (let* ((gcc (mail-fetch-field "gcc" nil t))
676 (mapcar 'gnus-inews-group-method
677 (message-unquote-tokens
678 (message-tokenize-header
681 (while (and (not covered) methods)
682 (setq covered (gnus-agent-method-p (car methods))
683 methods (cdr methods)))
687 (defun gnus-agent-possibly-save-gcc ()
688 "Save GCC if Gnus is unplugged."
689 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
691 (goto-char (point-min))
692 (let ((case-fold-search t))
693 (while (re-search-forward "^gcc:" nil t)
694 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
696 (defun gnus-agent-possibly-do-gcc ()
697 "Do GCC if Gnus is plugged."
698 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
699 (gnus-inews-do-gcc)))
702 ;;; Group mode commands
705 (defun gnus-agent-fetch-groups (n)
706 "Put all new articles in the current groups into the Agent."
709 (error "Groups can't be fetched when Gnus is unplugged"))
710 (gnus-group-iterate n 'gnus-agent-fetch-group))
712 (defun gnus-agent-fetch-group (&optional group)
713 "Put all new articles in GROUP into the Agent."
714 (interactive (list (gnus-group-group-name)))
715 (setq group (or group gnus-newsgroup-name))
717 (error "No group on the current line"))
719 (gnus-agent-while-plugged
720 (let ((gnus-command-method (gnus-find-method-for-group group)))
721 (gnus-agent-with-fetch
722 (gnus-agent-fetch-group-1 group gnus-command-method)
723 (gnus-message 5 "Fetching %s...done" group)))))
725 (defun gnus-agent-add-group (category arg)
726 "Add the current group to an agent category."
732 (mapcar (lambda (cat) (list (symbol-name (car cat))))
736 (let ((cat (assq category gnus-category-alist))
738 (gnus-group-iterate arg
740 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
741 (setf (gnus-agent-cat-groups c)
742 (delete group (gnus-agent-cat-groups c))))
743 (push group groups)))
744 (setf (gnus-agent-cat-groups cat)
745 (nconc (gnus-agent-cat-groups cat) groups))
746 (gnus-category-write)))
748 (defun gnus-agent-remove-group (arg)
749 "Remove the current group from its agent category, if any."
752 (gnus-group-iterate arg
754 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
755 (setf (gnus-agent-cat-groups c)
756 (delete group (gnus-agent-cat-groups c))))))
757 (gnus-category-write)))
759 (defun gnus-agent-synchronize-flags ()
760 "Synchronize unplugged flags with servers."
763 (dolist (gnus-command-method gnus-agent-covered-methods)
764 (when (file-exists-p (gnus-agent-lib-file "flags"))
765 (gnus-agent-synchronize-flags-server gnus-command-method)))))
767 (defun gnus-agent-possibly-synchronize-flags ()
768 "Synchronize flags according to `gnus-agent-synchronize-flags'."
771 (dolist (gnus-command-method gnus-agent-covered-methods)
772 (when (file-exists-p (gnus-agent-lib-file "flags"))
773 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
775 (defun gnus-agent-synchronize-flags-server (method)
776 "Synchronize flags set when unplugged for server."
777 (let ((gnus-command-method method))
778 (when (file-exists-p (gnus-agent-lib-file "flags"))
779 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
781 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
782 (if (null (gnus-check-server gnus-command-method))
783 (gnus-message 1 "Couldn't open server %s" (nth 1 gnus-command-method))
785 (if (null (eval (read (current-buffer))))
787 (write-file (gnus-agent-lib-file "flags"))
788 (error "Couldn't set flags from file %s"
789 (gnus-agent-lib-file "flags"))))
790 (delete-file (gnus-agent-lib-file "flags")))
793 (defun gnus-agent-possibly-synchronize-flags-server (method)
794 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
795 (when (or (and gnus-agent-synchronize-flags
796 (not (eq gnus-agent-synchronize-flags 'ask)))
797 (and (eq gnus-agent-synchronize-flags 'ask)
798 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
800 (gnus-agent-synchronize-flags-server method)))
803 ;;; Server mode commands
806 (defun gnus-agent-add-server (server)
807 "Enroll SERVER in the agent program."
808 (interactive (list (gnus-server-server-name)))
810 (error "No server on the current line"))
811 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
812 (when (gnus-agent-method-p method)
813 (error "Server already in the agent program"))
814 (push method gnus-agent-covered-methods)
815 (gnus-server-update-server server)
816 (gnus-agent-write-servers)
817 (gnus-message 1 "Entered %s into the Agent" server)))
819 (defun gnus-agent-remove-server (server)
820 "Remove SERVER from the agent program."
821 (interactive (list (gnus-server-server-name)))
823 (error "No server on the current line"))
824 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
825 (unless (gnus-agent-method-p method)
826 (error "Server not in the agent program"))
827 (setq gnus-agent-covered-methods
828 (delete method gnus-agent-covered-methods))
829 (gnus-server-update-server server)
830 (gnus-agent-write-servers)
831 (gnus-message 1 "Removed %s from the agent" server)))
833 (defun gnus-agent-read-servers ()
834 "Read the alist of covered servers."
836 (let ((method (gnus-server-get-method
840 (unless (member method gnus-agent-covered-methods)
841 (push method gnus-agent-covered-methods))
842 (gnus-message 1 "Ignoring disappeared server `%s'" m)
844 (gnus-agent-read-file
845 (nnheader-concat gnus-agent-directory "lib/servers"))))
847 (defun gnus-agent-write-servers ()
848 "Write the alist of covered servers."
849 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
850 (let ((coding-system-for-write nnheader-file-coding-system)
851 (output-coding-system nnheader-file-coding-system)
852 (file-name-coding-system nnmail-pathname-coding-system)
853 (pathname-coding-system nnmail-pathname-coding-system))
854 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
855 (prin1 (mapcar 'gnus-method-simplify gnus-agent-covered-methods)
862 (defun gnus-agent-mark-article (n &optional unmark)
863 "Mark the next N articles as downloadable.
864 If N is negative, mark backward instead. If UNMARK is non-nil, remove
865 the mark instead. The difference between N and the actual number of
866 articles marked is returned."
868 (let ((backward (< n 0))
873 (gnus-summary-set-agent-mark
874 (gnus-summary-article-number) unmark)
875 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
878 (gnus-message 7 "No more articles"))
879 (gnus-summary-recenter)
880 (gnus-summary-position-point)
883 (defun gnus-agent-unmark-article (n)
884 "Remove the downloadable mark from the next N articles.
885 If N is negative, unmark backward instead. The difference between N and
886 the actual number of articles unmarked is returned."
888 (gnus-agent-mark-article n t))
890 (defun gnus-agent-toggle-mark (n)
891 "Toggle the downloadable mark from the next N articles.
892 If N is negative, toggle backward instead. The difference between N and
893 the actual number of articles toggled is returned."
895 (gnus-agent-mark-article n 'toggle))
897 (defun gnus-summary-set-agent-mark (article &optional unmark)
898 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
899 When UNMARK is t, the article is unmarked. For any other value, the
900 article's mark is toggled."
901 (let ((unmark (cond ((eq nil unmark)
906 (memq article gnus-newsgroup-downloadable)))))
907 (when (gnus-summary-goto-subject article nil t)
908 (gnus-summary-update-mark
911 (setq gnus-newsgroup-downloadable
912 (delq article gnus-newsgroup-downloadable))
913 (gnus-article-mark article))
915 (setq gnus-newsgroup-downloadable
916 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
917 gnus-downloadable-mark)
921 (defun gnus-agent-get-undownloaded-list ()
922 "Construct list of articles that have not been downloaded."
923 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
924 (when (set (make-local-variable 'gnus-newsgroup-agentized)
925 (gnus-agent-method-p gnus-command-method))
926 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
927 (headers (sort (mapcar (lambda (h)
928 (mail-header-number h))
929 gnus-newsgroup-headers) '<))
930 (cached (and gnus-use-cache gnus-newsgroup-cached))
931 (undownloaded (list nil))
932 (tail-undownloaded undownloaded)
933 (unfetched (list nil))
934 (tail-unfetched unfetched))
935 (while (and alist headers)
936 (let ((a (caar alist))
939 ;; Ignore IDs in the alist that are not being
940 ;; displayed in the summary.
941 (setq alist (cdr alist)))
943 ;; Headers that are not in the alist should be
944 ;; fictious (see nnagent-retrieve-headers); they
945 ;; imply that this article isn't in the agent.
946 (gnus-agent-append-to-list tail-undownloaded h)
947 (gnus-agent-append-to-list tail-unfetched h)
948 (setq headers (cdr headers)))
950 (setq alist (cdr alist))
951 (setq headers (cdr headers))
952 nil ; ignore already downloaded
955 (setq alist (cdr alist))
956 (setq headers (cdr headers))
958 ;; This article isn't in the agent. Check to see
959 ;; if it is in the cache. If it is, it's been
961 (while (and cached (< (car cached) a))
962 (setq cached (cdr cached)))
963 (unless (equal a (car cached))
964 (gnus-agent-append-to-list tail-undownloaded a))))))
967 (let ((num (pop headers)))
968 (gnus-agent-append-to-list tail-undownloaded num)
969 (gnus-agent-append-to-list tail-unfetched num)))
971 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
972 gnus-newsgroup-unfetched (cdr unfetched))))))
974 (defun gnus-agent-catchup ()
975 "Mark as read all unhandled articles.
976 An article is unhandled if it is neither cached, nor downloaded, nor
980 (let ((articles gnus-newsgroup-undownloaded))
981 (when (or gnus-newsgroup-downloadable
982 gnus-newsgroup-cached)
983 (setq articles (gnus-sorted-ndifference
984 (gnus-sorted-ndifference
985 (gnus-copy-sequence articles)
986 gnus-newsgroup-downloadable)
987 gnus-newsgroup-cached)))
990 (gnus-summary-mark-article
991 (pop articles) gnus-catchup-mark)))
992 (gnus-summary-position-point)))
994 (defun gnus-agent-summary-fetch-series ()
996 (when gnus-newsgroup-processable
997 (setq gnus-newsgroup-downloadable
998 (let* ((dl gnus-newsgroup-downloadable)
999 (gnus-newsgroup-downloadable
1000 (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1001 (fetched-articles (gnus-agent-summary-fetch-group)))
1002 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1003 ;; updated gnus-newsgroup-downloadable to remove each
1004 ;; article successfully fetched.
1006 ;; For each article that I processed, remove its
1007 ;; processable mark IF the article is no longer
1008 ;; downloadable (i.e. it's already downloaded)
1009 (dolist (article gnus-newsgroup-processable)
1010 (unless (memq article gnus-newsgroup-downloadable)
1011 (gnus-summary-remove-process-mark article)))
1012 (gnus-sorted-ndifference dl fetched-articles)))))
1014 (defun gnus-agent-summary-fetch-group (&optional all)
1015 "Fetch the downloadable articles in the group.
1016 Optional arg ALL, if non-nil, means to fetch all articles."
1019 (if all gnus-newsgroup-articles
1020 gnus-newsgroup-downloadable))
1021 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1023 (gnus-agent-while-plugged
1025 (error "No articles to download"))
1026 (gnus-agent-with-fetch
1027 (setq gnus-newsgroup-undownloaded
1028 (gnus-sorted-ndifference
1029 gnus-newsgroup-undownloaded
1030 (setq fetched-articles
1031 (gnus-agent-fetch-articles
1032 gnus-newsgroup-name articles)))))
1034 (dolist (article articles)
1035 (let ((was-marked-downloadable
1036 (memq article gnus-newsgroup-downloadable)))
1037 (cond (gnus-agent-mark-unread-after-downloaded
1038 (setq gnus-newsgroup-downloadable
1039 (delq article gnus-newsgroup-downloadable))
1041 ;; The downloadable mark is implemented as a
1042 ;; type of read mark. Therefore, marking the
1043 ;; article as unread is sufficient to clear
1044 ;; its downloadable flag.
1045 (gnus-summary-mark-article article gnus-unread-mark))
1046 (was-marked-downloadable
1047 (gnus-summary-set-agent-mark article t)))
1048 (when (gnus-summary-goto-subject article nil t)
1049 (gnus-summary-update-download-mark article))))))
1052 (defun gnus-agent-fetch-selected-article ()
1053 "Fetch the current article as it is selected.
1054 This can be added to `gnus-select-article-hook' or
1055 `gnus-mark-article-hook'."
1056 (let ((gnus-command-method gnus-current-select-method))
1057 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1058 (when (gnus-agent-fetch-articles
1060 (list gnus-current-article))
1061 (setq gnus-newsgroup-undownloaded
1062 (delq gnus-current-article gnus-newsgroup-undownloaded))
1063 (gnus-summary-update-download-mark gnus-current-article)))))
1066 ;;; Internal functions
1070 ;;; The agent's active range is defined as follows:
1071 ;;; If the agent has no record of the group, use the actual active
1073 ;;; If the agent has a record, set the agent's active range to
1074 ;;; include the max limit of the actual active range.
1075 ;;; When expiring, update the min limit to match the smallest of the
1076 ;;; min article not expired or the min actual active range.
1078 (defun gnus-agent-save-active (method)
1079 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
1081 (defun gnus-agent-save-active-1 (method function)
1082 (when (gnus-agent-method-p method)
1083 (let* ((gnus-command-method method)
1084 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1085 (file (gnus-agent-lib-file "active")))
1086 (funcall function nil new)
1087 (gnus-agent-write-active file new)
1089 (nnheader-insert-file-contents file))))
1091 (defun gnus-agent-write-active (file new &optional literal-replacement)
1093 (when (and (not literal-replacement)
1094 (file-exists-p file))
1095 (setq old (gnus-make-hashtable (count-lines (point-min) (point-max))))
1097 (nnheader-insert-file-contents file)
1098 (gnus-active-to-gnus-format nil old))
1099 ;; Iterate over the current active groups, the current active
1100 ;; range may expand, but NOT CONTRACT, the agent's active range.
1103 (let ((new-active (and nsym (boundp nsym) (symbol-value nsym))))
1105 (let* ((osym (intern (symbol-name nsym) old))
1106 (old-active (and (boundp osym) (symbol-value osym))))
1108 (let ((new-min (car new-active))
1109 (old-min (car old-active))
1110 (new-max (cdr new-active))
1111 (old-max (cdr old-active)))
1112 (if (and (integerp new-min)
1113 (< new-min old-min))
1114 (setcar old-active new-min))
1115 (if (and (integerp new-max)
1116 (> new-max old-max))
1117 (setcdr old-active new-max)))
1118 (set osym new-active))))))
1120 (gnus-make-directory (file-name-directory file))
1121 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1122 ;; The hashtable contains real names of groups. However, do NOT
1123 ;; add the foreign server prefix as gnus-active-to-gnus-format
1124 ;; will add it while reading the file.
1125 (gnus-write-active-file file old nil))))
1127 (defun gnus-agent-save-groups (method)
1128 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
1130 (defun gnus-agent-save-group-info (method group active)
1131 (when (gnus-agent-method-p method)
1132 (let* ((gnus-command-method method)
1133 (coding-system-for-write nnheader-file-coding-system)
1134 (output-coding-system nnheader-file-coding-system)
1135 (file-name-coding-system nnmail-pathname-coding-system)
1136 (pathname-coding-system nnmail-pathname-coding-system)
1137 (file (gnus-agent-lib-file "active"))
1138 oactive-min oactive-max)
1139 (gnus-make-directory (file-name-directory file))
1140 (with-temp-file file
1141 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1142 (set-buffer-multibyte nil)
1143 (when (file-exists-p file)
1144 (nnheader-insert-file-contents file)
1146 (goto-char (point-min))
1147 (when (re-search-forward
1148 (concat "^" (regexp-quote group) " ") nil t)
1150 (setq oactive-max (read (current-buffer)) ;; max
1151 oactive-min (read (current-buffer)))) ;; min
1152 (gnus-delete-line)))
1153 (insert (format "%S %d %d y\n" (intern group)
1154 (max (or oactive-max (cdr active)) (cdr active))
1155 (min (or oactive-min (car active)) (car active))))
1156 (goto-char (point-max))
1157 (while (search-backward "\\." nil t)
1158 (delete-char 1))))))
1160 (defun gnus-agent-group-path (group)
1161 "Translate GROUP into a file name."
1163 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1164 ;; The two methods must be kept synchronized, which is why
1165 ;; gnus-agent-group-pathname was added.
1168 (nnheader-translate-file-chars
1169 (nnheader-replace-duplicate-chars-in-string
1170 (nnheader-replace-chars-in-string
1171 (gnus-group-real-name group)
1174 (if (or nnmail-use-long-file-names
1175 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1177 (mm-encode-coding-string
1178 (nnheader-replace-chars-in-string group ?. ?/)
1179 nnmail-pathname-coding-system)))
1181 (defun gnus-agent-group-pathname (group)
1182 "Translate GROUP into a file name."
1183 ;; nnagent uses nnmail-group-pathname to read articles while
1184 ;; unplugged. The agent must, therefore, use the same directory
1186 (let ((gnus-command-method (or gnus-command-method
1187 (gnus-find-method-for-group group))))
1188 (nnmail-group-pathname (gnus-group-real-name group) (gnus-agent-directory))))
1190 (defun gnus-agent-get-function (method)
1191 (if (gnus-online method)
1196 ;;; History functions
1198 (defun gnus-agent-history-buffer ()
1199 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1201 (defun gnus-agent-open-history ()
1203 (push (cons (gnus-agent-method)
1204 (set-buffer (gnus-get-buffer-create
1205 (format " *Gnus agent %s history*"
1206 (gnus-agent-method)))))
1207 gnus-agent-history-buffers)
1208 (set-buffer-multibyte nil) ;; everything is binary
1211 (let ((file (gnus-agent-lib-file "history")))
1212 (when (file-exists-p file)
1213 (nnheader-insert-file-contents file))
1214 (set (make-local-variable 'gnus-agent-file-name) file))))
1216 (defun gnus-agent-close-history ()
1217 (when (gnus-buffer-live-p gnus-agent-current-history)
1218 (kill-buffer gnus-agent-current-history)
1219 (setq gnus-agent-history-buffers
1220 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1221 gnus-agent-history-buffers))))
1227 (defun gnus-agent-fetch-articles (group articles)
1228 "Fetch ARTICLES from GROUP and put them into the Agent."
1230 (gnus-agent-load-alist group)
1231 (let* ((alist gnus-agent-article-alist)
1232 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1233 (selected-sets (list nil))
1234 (current-set-size 0)
1237 ;; Check each article
1238 (while (setq article (pop articles))
1239 ;; Skip alist entries preceeding this article
1240 (while (> article (or (caar alist) (1+ article)))
1241 (setq alist (cdr alist)))
1243 ;; Prune off articles that we have already fetched.
1244 (unless (and (eq article (caar alist))
1246 ;; Skip headers preceeding this article
1249 (let* ((header (car headers)))
1251 (mail-header-number header)
1253 (setq headers (cdr headers)))
1255 ;; Add this article to the current set
1256 (setcar selected-sets (cons article (car selected-sets)))
1258 ;; Update the set size, when the set is too large start a
1259 ;; new one. I do this after adding the article as I want at
1260 ;; least one article in each set.
1261 (when (< gnus-agent-max-fetch-size
1262 (setq current-set-size
1264 (if (= header-number article)
1265 (let ((char-size (mail-header-chars
1267 (if (<= char-size 0)
1268 ;; The char size was missing/invalid,
1269 ;; assume a worst-case situation of
1270 ;; 65 char/line. If the line count
1271 ;; is missing, arbitrarily assume a
1272 ;; size of 1000 characters.
1273 (max (* 65 (mail-header-lines
1278 (setcar selected-sets (nreverse (car selected-sets)))
1279 (setq selected-sets (cons nil selected-sets)
1280 current-set-size 0))))
1282 (when (or (cdr selected-sets) (car selected-sets))
1283 (let* ((fetched-articles (list nil))
1284 (tail-fetched-articles fetched-articles)
1285 (dir (gnus-agent-group-pathname group))
1286 (date (time-to-days (current-time)))
1287 (case-fold-search t)
1290 (setcar selected-sets (nreverse (car selected-sets)))
1291 (setq selected-sets (nreverse selected-sets))
1293 (gnus-make-directory dir)
1294 (gnus-message 7 "Fetching articles for %s..." group)
1297 (while (setq articles (pop selected-sets))
1298 ;; Fetch the articles from the backend.
1299 (if (gnus-check-backend-function 'retrieve-articles group)
1300 (setq pos (gnus-retrieve-articles articles group))
1303 (while (setq article (pop articles))
1304 (gnus-message 10 "Fetching article %s for %s..."
1307 (gnus-backlog-request-article group article
1309 (gnus-request-article article group))
1310 (goto-char (point-max))
1311 (push (cons article (point)) pos)
1312 (insert-buffer-substring nntp-server-buffer)))
1314 nntp-server-buffer (point-min) (point-max))
1315 (setq pos (nreverse pos)))))
1316 ;; Then save these articles into the Agent.
1318 (set-buffer nntp-server-buffer)
1320 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1321 (goto-char (point-min))
1322 (unless (eobp) ;; Don't save empty articles.
1323 (when (search-forward "\n\n" nil t)
1324 (when (search-backward "\nXrefs: " nil t)
1325 ;; Handle cross posting.
1326 (goto-char (match-end 0)) ; move to end of header name
1327 (skip-chars-forward "^ ") ; skip server name
1328 (skip-chars-forward " ")
1330 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1331 (push (cons (buffer-substring (match-beginning 1)
1334 (buffer-substring (match-beginning 2)
1337 (goto-char (match-end 0)))
1338 (gnus-agent-crosspost crosses (caar pos) date)))
1339 (goto-char (point-min))
1340 (if (not (re-search-forward
1341 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1342 (setq id "No-Message-ID-in-article")
1343 (setq id (buffer-substring
1344 (match-beginning 1) (match-end 1))))
1345 (write-region-as-coding-system
1346 gnus-agent-file-coding-system (point-min) (point-max)
1347 (concat dir (number-to-string (caar pos))) nil 'silent)
1349 (gnus-agent-append-to-list
1350 tail-fetched-articles (caar pos)))
1352 (setq pos (cdr pos)))))
1354 (gnus-agent-save-alist group (cdr fetched-articles) date)
1355 (gnus-message 7 ""))
1356 (cdr fetched-articles))))))
1358 (defun gnus-agent-crosspost (crosses article &optional date)
1359 (setq date (or date t))
1361 (let (gnus-agent-article-alist group alist beg end)
1363 (set-buffer gnus-agent-overview-buffer)
1364 (when (nnheader-find-nov-line article)
1367 (setq end (progn (forward-line 1) (point)))))
1369 (setq group (caar crosses))
1370 (unless (setq alist (assoc group gnus-agent-group-alist))
1371 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1372 gnus-agent-group-alist))
1373 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1375 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1377 (when (= (point-max) (point-min))
1378 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1380 (nnheader-insert-file-contents
1381 (gnus-agent-article-name ".overview" group))))
1382 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1383 (insert (string-to-number (cdar crosses)))
1384 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1385 (gnus-agent-check-overview-buffer))
1386 (setq crosses (cdr crosses)))))
1388 (defun gnus-agent-backup-overview-buffer ()
1389 (when gnus-newsgroup-name
1390 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1393 (while (file-exists-p
1394 (setq name (concat root "~"
1395 (int-to-string (setq cnt (1+ cnt))) "~"))))
1396 (write-region (point-min) (point-max) name nil 'no-msg)
1397 (gnus-message 1 "Created backup copy of overview in %s." name)))
1400 (defun gnus-agent-check-overview-buffer (&optional buffer)
1401 "Check the overview file given for sanity.
1402 In particular, checks that the file is sorted by article number
1403 and that there are no duplicates."
1408 (set-buffer buffer))
1411 (goto-char (point-min))
1413 (while (< (point) (point-max))
1415 (cur (condition-case nil
1416 (read (current-buffer))
1419 ((or (not (integerp cur))
1420 (not (eq (char-after) ?\t)))
1422 (setq backed-up (gnus-agent-backup-overview-buffer)))
1424 "Overview buffer contains garbage '%s'."
1426 p (gnus-point-at-eol))))
1429 (setq backed-up (gnus-agent-backup-overview-buffer)))
1431 "Duplicate overview line for %d" cur)
1432 (delete-region (point) (progn (forward-line 1) (point))))
1435 (setq backed-up (gnus-agent-backup-overview-buffer)))
1436 (gnus-message 1 "Overview buffer not sorted!")
1437 (sort-numeric-fields 1 (point-min) (point-max))
1438 (goto-char (point-min))
1441 (setq prev-num cur)))
1442 (forward-line 1)))))))
1444 (defun gnus-agent-flush-cache ()
1446 (while gnus-agent-buffer-alist
1447 (set-buffer (cdar gnus-agent-buffer-alist))
1448 (write-region-as-coding-system
1449 gnus-agent-file-coding-system
1450 (point-min) (point-max)
1451 (gnus-agent-article-name ".overview"
1452 (caar gnus-agent-buffer-alist))
1454 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1455 (while gnus-agent-group-alist
1456 (with-temp-file (gnus-agent-article-name
1457 ".agentview" (caar gnus-agent-group-alist))
1458 (princ (cdar gnus-agent-group-alist))
1460 (princ 1 (current-buffer))
1462 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1464 (defun gnus-agent-find-parameter (group symbol)
1465 "Search for GROUPs SYMBOL in the group's parameters, the group's
1466 topic parameters, the group's category, or the customizable
1467 variables. Returns the first non-nil value found."
1468 (or (gnus-group-find-parameter group symbol t)
1469 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1473 '((agent-short-article . gnus-agent-short-article)
1474 (agent-long-article . gnus-agent-long-article)
1475 (agent-low-score . gnus-agent-low-score)
1476 (agent-high-score . gnus-agent-high-score)
1477 (agent-days-until-old . gnus-agent-expire-days)
1478 (agent-enable-expiration
1479 . gnus-agent-enable-expiration)
1480 (agent-predicate . gnus-agent-predicate)))))))
1482 (defun gnus-agent-fetch-headers (group &optional force)
1483 "Fetch interesting headers into the agent. The group's overview
1484 file will be updated to include the headers while a list of available
1485 article numbers will be returned."
1486 (let* ((fetch-all (and gnus-agent-consider-all-articles
1487 ;; Do not fetch all headers if the predicate
1488 ;; implies that we only consider unread articles.
1489 (not (gnus-predicate-implies-unread
1490 (gnus-agent-find-parameter group
1491 'agent-predicate)))))
1492 (articles (if fetch-all
1493 (gnus-uncompress-range (gnus-active group))
1494 (gnus-list-of-unread-articles group)))
1495 (gnus-decode-encoded-word-function 'identity)
1496 (file (gnus-agent-article-name ".overview" group)))
1497 ;; Check whether the number of articles is not too large.
1498 (when (and (integerp gnus-agent-large-newsgroup)
1499 (> gnus-agent-large-newsgroup 0))
1500 (setq articles (nthcdr (max (- (length articles)
1501 gnus-agent-large-newsgroup)
1505 ;; Add articles with marks to the list of article headers we want to
1506 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1507 ;; mark, but do fetch recent or seen articles if they have other, more
1508 ;; interesting marks. (We have to fetch articles with boring marks
1509 ;; because otherwise the agent will remove their marks.)
1510 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1511 (unless (memq (car arts) '(seen recent killed cache))
1512 (setq articles (gnus-range-add articles (cdr arts)))))
1513 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1515 ;; At this point, I have the list of articles to consider for
1516 ;; fetching. This is the list that I'll return to my caller. Some
1517 ;; of these articles may have already been fetched. That's OK as
1518 ;; the fetch article code will filter those out. Internally, I'll
1519 ;; filter this list to just those articles whose headers need to
1521 (let ((articles articles))
1522 ;; Remove known articles.
1523 (when (and (or gnus-agent-cache
1525 (gnus-agent-load-alist group))
1526 ;; Remove articles marked as downloaded.
1528 ;; I want to fetch all headers in the active range.
1529 ;; Therefore, exclude only those headers that are in the
1531 ;; NOTE: This is probably NOT what I want to do after
1532 ;; agent expiration in this group.
1533 (setq articles (gnus-agent-uncached-articles articles group))
1535 ;; I want to only fetch those headers that have never been
1536 ;; fetched. Therefore, exclude all headers that are, or
1537 ;; WERE, in the article alist.
1538 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1539 (high (cdr (gnus-active group))))
1540 ;; Low can be greater than High when the same group is
1541 ;; fetched twice in the same session {The first fetch will
1542 ;; fill the article alist such that (last
1543 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1544 ;; group))}. The addition of one(the 1+ above) then
1545 ;; forces Low to be greater than High. When this happens,
1546 ;; gnus-list-range-intersection returns nil which
1547 ;; indicates that no headers need to be fetched. -- Kevin
1548 (setq articles (gnus-list-range-intersection
1549 articles (list (cons low high)))))))
1552 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1553 (gnus-compress-sequence articles t))
1556 (set-buffer nntp-server-buffer)
1560 (gnus-message 7 "Fetching headers for %s..." group)
1563 (gnus-make-directory (nnheader-translate-file-chars
1564 (file-name-directory file) t))
1566 (unless (eq 'nov (gnus-retrieve-headers articles group))
1567 (nnvirtual-convert-headers))
1568 (gnus-agent-check-overview-buffer)
1569 ;; Move these headers to the overview buffer so that
1570 ;; gnus-agent-braid-nov can merge them with the contents
1573 gnus-agent-overview-buffer (point-min) (point-max))
1574 (when (file-exists-p file)
1575 (gnus-agent-braid-nov group articles file))
1576 (gnus-agent-check-overview-buffer)
1577 (write-region-as-coding-system
1578 gnus-agent-file-coding-system
1579 (point-min) (point-max) file nil 'silent)
1580 (gnus-agent-save-alist group articles nil)
1584 (nnheader-insert-file-contents file)))))
1587 (defsubst gnus-agent-copy-nov-line (article)
1589 (set-buffer gnus-agent-overview-buffer)
1590 (while (and (not (eobp))
1591 (< (setq art (read (current-buffer))) article))
1595 (not (eq article art)))
1596 (set-buffer nntp-server-buffer)
1598 (setq e (progn (forward-line 1) (point)))
1599 (set-buffer nntp-server-buffer)
1600 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1602 (defun gnus-agent-braid-nov (group articles file)
1603 "Merge agent overview data with given file.
1604 Takes headers for ARTICLES from `gnus-agent-overview-buffer' and the given
1605 FILE and places the combined headers into `nntp-server-buffer'."
1607 (set-buffer gnus-agent-overview-buffer)
1608 (goto-char (point-min))
1609 (set-buffer nntp-server-buffer)
1611 (nnheader-insert-file-contents file)
1612 (goto-char (point-max))
1614 (unless (looking-at "[0-9]+\t")
1615 ;; Remove corrupted lines
1617 1 "Overview %s is corrupted. Removing corrupted lines..." file)
1618 (goto-char (point-min))
1620 (if (looking-at "[0-9]+\t")
1622 (delete-region (point) (progn (forward-line 1) (point)))))
1624 (unless (or (= (point-min) (point-max))
1625 (< (setq last (read (current-buffer))) (car articles)))
1626 ;; We do it the hard way.
1627 (when (nnheader-find-nov-line (car articles))
1628 ;; Replacing existing NOV entry
1629 (delete-region (point) (progn (forward-line 1) (point))))
1630 (gnus-agent-copy-nov-line (pop articles))
1634 (while (let ((art (read (current-buffer))))
1635 (cond ((< art (car articles))
1638 ((= art (car articles))
1641 (point) (progn (forward-line 1) (point)))
1647 (gnus-agent-copy-nov-line (pop articles)))))
1649 ;; Copy the rest lines
1650 (set-buffer nntp-server-buffer)
1651 (goto-char (point-max))
1654 (set-buffer gnus-agent-overview-buffer)
1656 (while (<= (read (current-buffer)) last)
1659 (setq start (point))
1660 (set-buffer nntp-server-buffer))
1661 (insert-buffer-substring gnus-agent-overview-buffer start))))
1663 ;; Keeps the compiler from warning about the free variable in
1664 ;; gnus-agent-read-agentview.
1666 (defvar gnus-agent-read-agentview))
1668 (defun gnus-agent-load-alist (group)
1669 "Load the article-state alist for GROUP."
1670 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1671 (let ((gnus-agent-read-agentview group))
1672 (setq gnus-agent-article-alist
1673 (gnus-cache-file-contents
1674 (gnus-agent-article-name ".agentview" group)
1675 'gnus-agent-file-loading-cache
1676 'gnus-agent-read-agentview))))
1678 ;; Save format may be either 1 or 2. Two is the new, compressed
1679 ;; format that is still being tested. Format 1 is uncompressed but
1680 ;; known to be reliable.
1681 (defconst gnus-agent-article-alist-save-format 2)
1683 (defun gnus-agent-read-agentview (file)
1684 "Load FILE and do a `read' there."
1687 (nnheader-insert-file-contents file)
1688 (goto-char (point-min))
1689 (let ((alist (read (current-buffer)))
1690 (version (condition-case nil (read (current-buffer))
1696 (let ((inhibit-quit t)
1698 (gnus-agent-open-history)
1699 (set-buffer (gnus-agent-history-buffer))
1700 (goto-char (point-min))
1702 (if (and (looking-at
1703 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1704 (string= (match-string 2)
1705 gnus-agent-read-agentview)
1706 (setq entry (assoc (string-to-number (match-string 3)) alist)))
1707 (setcdr entry (string-to-number (match-string 1))))
1709 (gnus-agent-close-history)
1710 (setq changed-version t)))
1712 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
1717 (let ((state (car comp-list))
1718 (sequence (gnus-uncompress-sequence
1720 (mapcar (lambda (article-id)
1721 (setq uncomp (cons (cons article-id state) uncomp)))
1724 (setq alist (sort uncomp
1725 (lambda (first second)
1726 (< (car first) (car second))))))))
1727 (when changed-version
1728 (let ((gnus-agent-article-alist alist))
1729 (gnus-agent-save-alist gnus-agent-read-agentview)))
1732 (defun gnus-agent-save-alist (group &optional articles state dir)
1733 "Save the article-state alist for GROUP."
1734 (let* ((file-name-coding-system nnmail-pathname-coding-system)
1735 (pathname-coding-system nnmail-pathname-coding-system)
1736 (prev (cons nil gnus-agent-article-alist))
1738 print-level print-length item article)
1739 (while (setq article (pop articles))
1740 (while (and (cdr prev)
1741 (< (caadr prev) article))
1742 (setq prev (cdr prev)))
1745 (setcdr prev (list (cons article state))))
1746 ((> (caadr prev) article)
1747 (setcdr prev (cons (cons article state) (cdr prev))))
1748 ((= (caadr prev) article)
1749 (setcdr (cadr prev) state)))
1750 (setq prev (cdr prev)))
1751 (setq gnus-agent-article-alist (cdr all))
1753 (gnus-make-directory dir)
1754 (gnus-make-directory (gnus-agent-article-name "" group)))
1755 (with-temp-file (if dir
1756 (expand-file-name ".agentview" dir)
1757 (gnus-agent-article-name ".agentview" group))
1758 (cond ((eq gnus-agent-article-alist-save-format 1)
1759 (princ gnus-agent-article-alist (current-buffer)))
1760 ((eq gnus-agent-article-alist-save-format 2)
1761 (let ((compressed nil))
1762 (mapcar (lambda (pair)
1763 (let* ((article-id (car pair))
1764 (day-of-download (cdr pair))
1765 (comp-list (assq day-of-download compressed)))
1768 (cons article-id (cdr comp-list)))
1770 (cons (list day-of-download article-id)
1772 nil)) gnus-agent-article-alist)
1773 (mapcar (lambda (comp-list)
1775 (gnus-compress-sequence
1776 (nreverse (cdr comp-list)))))
1778 (princ compressed (current-buffer)))))
1780 (princ gnus-agent-article-alist-save-format (current-buffer))
1783 (defun gnus-agent-article-name (article group)
1784 (expand-file-name article
1785 (file-name-as-directory
1786 (gnus-agent-group-pathname group))))
1788 (defun gnus-agent-batch-confirmation (msg)
1789 "Show error message and return t."
1790 (gnus-message 1 msg)
1794 (defun gnus-agent-batch-fetch ()
1795 "Start Gnus and fetch session."
1798 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1799 (gnus-agent-fetch-session))
1802 (defun gnus-agent-fetch-session ()
1803 "Fetch all articles and headers that are eligible for fetching."
1805 (unless gnus-agent-covered-methods
1806 (error "No servers are covered by the Gnus agent"))
1807 (unless gnus-plugged
1808 (error "Can't fetch articles while Gnus is unplugged"))
1809 (let ((methods gnus-agent-covered-methods)
1810 groups group gnus-command-method)
1813 (setq gnus-command-method (car methods))
1814 (when (and (or (gnus-server-opened gnus-command-method)
1815 (gnus-open-server gnus-command-method))
1816 (gnus-online gnus-command-method))
1817 (setq groups (gnus-groups-from-server (car methods)))
1818 (gnus-agent-with-fetch
1819 (while (setq group (pop groups))
1820 (when (<= (gnus-group-level group)
1821 gnus-agent-handle-level)
1822 (if (or debug-on-error debug-on-quit)
1823 (gnus-agent-fetch-group-1
1824 group gnus-command-method)
1826 (gnus-agent-fetch-group-1
1827 group gnus-command-method)
1829 (unless (funcall gnus-agent-confirmation-function
1830 (format "Error %s. Continue? "
1831 (error-message-string err)))
1832 (error "Cannot fetch articles into the Gnus agent")))
1834 (unless (funcall gnus-agent-confirmation-function
1836 "Quit fetching session %s. Continue? "
1837 (error-message-string err)))
1839 "Cannot fetch articles into the Gnus agent")))))))))
1840 (setq methods (cdr methods)))
1841 (gnus-run-hooks 'gnus-agent-fetched-hook)
1842 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1844 (defun gnus-agent-fetch-group-1 (group method)
1846 (let ((gnus-command-method method)
1847 (gnus-newsgroup-name group)
1848 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
1849 (gnus-newsgroup-headers gnus-newsgroup-headers)
1850 (gnus-newsgroup-scored gnus-newsgroup-scored)
1851 (gnus-use-cache gnus-use-cache)
1852 (gnus-summary-expunge-below gnus-summary-expunge-below)
1853 (gnus-summary-mark-below gnus-summary-mark-below)
1854 (gnus-orphan-score gnus-orphan-score)
1855 ;; Maybe some other gnus-summary local variables should also
1861 category predicate info marks score-param
1863 (unless (gnus-check-group group)
1864 (error "Can't open server for %s" group))
1867 (when (or gnus-newsgroup-active
1869 (gnus-activate-group group))
1870 (let ((marked-articles gnus-newsgroup-downloadable))
1871 ;; Identify the articles marked for download
1872 (unless gnus-newsgroup-active
1873 ;; The variable gnus-newsgroup-active was selected as I need
1874 ;; a gnus-summary local variable that is NOT bound to any
1875 ;; value (its global value should default to nil).
1876 (dolist (mark gnus-agent-download-marks)
1877 (let ((arts (cdr (assq mark (gnus-info-marks
1878 (setq info (gnus-get-info group)))))))
1880 (setq marked-articles (nconc (gnus-uncompress-range arts)
1883 (setq marked-articles (sort marked-articles '<))
1885 ;; Fetch any new articles from the server
1886 (setq articles (gnus-agent-fetch-headers group))
1888 ;; Merge new articles with marked
1889 (setq articles (sort (append marked-articles articles) '<))
1892 ;; Parse them and see which articles we want to fetch.
1893 (setq gnus-newsgroup-dependencies
1894 (or gnus-newsgroup-dependencies
1895 (make-vector (length articles) 0)))
1896 (setq gnus-newsgroup-headers
1897 (or gnus-newsgroup-headers
1898 (gnus-get-newsgroup-headers-xover articles nil nil
1900 ;; `gnus-agent-overview-buffer' may be killed for
1901 ;; timeout reason. If so, recreate it.
1902 (gnus-agent-create-buffer)
1904 ;; Figure out how to select articles in this group
1905 (setq category (gnus-group-category group))
1909 (gnus-agent-find-parameter group 'agent-predicate)))
1911 ;; If the selection predicate requires scoring, score each header
1912 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
1914 (gnus-agent-find-parameter group 'agent-score-file)))
1915 ;; Translate score-param into real one
1918 ((eq score-param 'file)
1919 (setq score-param (gnus-all-score-files group)))
1920 ((stringp (car score-param)))
1922 (setq score-param (list (list score-param)))))
1924 (gnus-score-headers score-param))))
1926 (unless (and (eq predicate 'gnus-agent-false)
1927 (not marked-articles))
1928 (let ((arts (list nil)))
1929 (let ((arts-tail arts)
1930 (alist (gnus-agent-load-alist group))
1931 (marked-articles marked-articles)
1932 (gnus-newsgroup-headers gnus-newsgroup-headers))
1933 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1934 (let ((num (mail-header-number gnus-headers)))
1935 ;; Determine if this article is already in the cache
1937 (> num (caar alist)))
1938 (setq alist (cdr alist)))
1940 (unless (and (eq num (caar alist))
1943 ;; Determine if this article was marked for download.
1944 (while (and marked-articles
1945 (> num (car marked-articles)))
1946 (setq marked-articles
1947 (cdr marked-articles)))
1949 ;; When this article is marked, or selected by the
1950 ;; predicate, add it to the download list
1951 (when (or (eq num (car marked-articles))
1954 (assq num gnus-newsgroup-scored))
1955 gnus-summary-default-score))
1956 (gnus-agent-long-article
1957 (gnus-agent-find-parameter
1958 group 'agent-long-article))
1959 (gnus-agent-short-article
1960 (gnus-agent-find-parameter
1961 group 'agent-short-article))
1962 (gnus-agent-low-score
1963 (gnus-agent-find-parameter
1964 group 'agent-low-score))
1965 (gnus-agent-high-score
1966 (gnus-agent-find-parameter
1967 group 'agent-high-score))
1968 (gnus-agent-expire-days
1969 (gnus-agent-find-parameter
1970 group 'agent-days-until-old)))
1971 (funcall predicate)))
1972 (gnus-agent-append-to-list arts-tail num))))))
1974 (let (fetched-articles)
1975 ;; Fetch all selected articles
1976 (setq gnus-newsgroup-undownloaded
1977 (gnus-sorted-ndifference
1978 gnus-newsgroup-undownloaded
1979 (setq fetched-articles
1981 (gnus-agent-fetch-articles group (cdr arts))
1984 (let ((unfetched-articles
1985 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
1986 (if gnus-newsgroup-active
1987 ;; Update the summary buffer
1989 (dolist (article marked-articles)
1990 (gnus-summary-set-agent-mark article t))
1991 (dolist (article fetched-articles)
1992 (if gnus-agent-mark-unread-after-downloaded
1993 (gnus-summary-mark-article
1994 article gnus-unread-mark))
1995 (when (gnus-summary-goto-subject article nil t)
1996 (gnus-summary-update-download-mark article)))
1997 (dolist (article unfetched-articles)
1998 (gnus-summary-mark-article
1999 article gnus-canceled-mark)))
2001 ;; Update the group buffer.
2003 ;; When some, or all, of the marked articles came
2004 ;; from the download mark. Remove that mark. I
2005 ;; didn't do this earlier as I only want to remove
2006 ;; the marks after the fetch is completed.
2008 (dolist (mark gnus-agent-download-marks)
2009 (when (eq mark 'download)
2011 (assq mark (gnus-info-marks
2012 (setq info (gnus-get-info group))))))
2013 (when (cdr marked-arts)
2015 (delq marked-arts (gnus-info-marks info)))
2016 (gnus-info-set-marks info marks)))))
2017 (let ((read (gnus-info-read
2018 (or info (setq info (gnus-get-info group))))))
2020 info (gnus-add-to-range read unfetched-articles)))
2022 (gnus-group-update-group group t)
2026 (concat "(gnus-group-set-info '"
2027 (gnus-prin1-to-string info)
2031 ;;; Agent Category Mode
2034 (defvar gnus-category-mode-hook nil
2035 "Hook run in `gnus-category-mode' buffers.")
2037 (defvar gnus-category-line-format " %(%20c%): %g\n"
2038 "Format of category lines.
2040 Valid specifiers include:
2041 %c Topic name (string)
2042 %g The number of groups in the topic (integer)
2044 General format specifiers can also be used. See Info node
2045 `(gnus)Formatting Variables'.")
2047 (defvar gnus-category-mode-line-format "Gnus: %%b"
2048 "The format specification for the category mode line.")
2050 (defvar gnus-agent-predicate 'false
2051 "The selection predicate used when no other source is available.")
2053 (defvar gnus-agent-short-article 100
2054 "Articles that have fewer lines than this are short.")
2056 (defvar gnus-agent-long-article 200
2057 "Articles that have more lines than this are long.")
2059 (defvar gnus-agent-low-score 0
2060 "Articles that have a score lower than this have a low score.")
2062 (defvar gnus-agent-high-score 0
2063 "Articles that have a score higher than this have a high score.")
2066 ;;; Internal variables.
2068 (defvar gnus-category-buffer "*Agent Category*")
2070 (defvar gnus-category-line-format-alist
2071 `((?c gnus-tmp-name ?s)
2072 (?g gnus-tmp-groups ?d)))
2074 (defvar gnus-category-mode-line-format-alist
2075 `((?u user-defined ?s)))
2077 (defvar gnus-category-line-format-spec nil)
2078 (defvar gnus-category-mode-line-format-spec nil)
2080 (defvar gnus-category-mode-map nil)
2081 (put 'gnus-category-mode 'mode-class 'special)
2083 (unless gnus-category-mode-map
2084 (setq gnus-category-mode-map (make-sparse-keymap))
2085 (suppress-keymap gnus-category-mode-map)
2087 (gnus-define-keys gnus-category-mode-map
2088 "q" gnus-category-exit
2089 "k" gnus-category-kill
2090 "c" gnus-category-copy
2091 "a" gnus-category-add
2092 "e" gnus-agent-customize-category
2093 "p" gnus-category-edit-predicate
2094 "g" gnus-category-edit-groups
2095 "s" gnus-category-edit-score
2096 "l" gnus-category-list
2098 "\C-c\C-i" gnus-info-find-node
2099 "\C-c\C-b" gnus-bug))
2101 (defvar gnus-category-menu-hook nil
2102 "*Hook run after the creation of the menu.")
2104 (defun gnus-category-make-menu-bar ()
2105 (gnus-turn-off-edit-menu 'category)
2106 (unless (boundp 'gnus-category-menu)
2108 gnus-category-menu gnus-category-mode-map ""
2110 ["Add" gnus-category-add t]
2111 ["Kill" gnus-category-kill t]
2112 ["Copy" gnus-category-copy t]
2113 ["Edit category" gnus-agent-customize-category t]
2114 ["Edit predicate" gnus-category-edit-predicate t]
2115 ["Edit score" gnus-category-edit-score t]
2116 ["Edit groups" gnus-category-edit-groups t]
2117 ["Exit" gnus-category-exit t]))
2119 (gnus-run-hooks 'gnus-category-menu-hook)))
2121 (defun gnus-category-mode ()
2122 "Major mode for listing and editing agent categories.
2124 All normal editing commands are switched off.
2125 \\<gnus-category-mode-map>
2126 For more in-depth information on this mode, read the manual
2127 \(`\\[gnus-info-find-node]').
2129 The following commands are available:
2131 \\{gnus-category-mode-map}"
2133 (when (gnus-visual-p 'category-menu 'menu)
2134 (gnus-category-make-menu-bar))
2135 (kill-all-local-variables)
2136 (gnus-simplify-mode-line)
2137 (setq major-mode 'gnus-category-mode)
2138 (setq mode-name "Category")
2139 (gnus-set-default-directory)
2140 (setq mode-line-process nil)
2141 (use-local-map gnus-category-mode-map)
2142 (buffer-disable-undo)
2143 (setq truncate-lines t)
2144 (setq buffer-read-only t)
2145 (gnus-run-hooks 'gnus-category-mode-hook))
2147 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2149 (defun gnus-category-insert-line (category)
2150 (let* ((gnus-tmp-name (format "%s" (car category)))
2151 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2153 (gnus-add-text-properties
2157 (eval gnus-category-line-format-spec))
2158 (list 'gnus-category gnus-tmp-name))))
2160 (defun gnus-enter-category-buffer ()
2161 "Go to the Category buffer."
2163 (gnus-category-setup-buffer)
2164 (gnus-configure-windows 'category)
2165 (gnus-category-prepare))
2167 (defun gnus-category-setup-buffer ()
2168 (unless (get-buffer gnus-category-buffer)
2170 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2171 (gnus-category-mode))))
2173 (defun gnus-category-prepare ()
2174 (gnus-set-format 'category-mode)
2175 (gnus-set-format 'category t)
2176 (let ((alist gnus-category-alist)
2177 (buffer-read-only nil))
2180 (gnus-category-insert-line (pop alist)))
2181 (goto-char (point-min))
2182 (gnus-category-position-point)))
2184 (defun gnus-category-name ()
2185 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2186 (error "No category on the current line")))
2188 (defun gnus-category-read ()
2189 "Read the category alist."
2190 (setq gnus-category-alist
2194 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2195 (goto-char (point-min))
2196 ;; This code isn't temp, it will be needed so long as
2197 ;; anyone may be migrating from an older version.
2199 ;; Once we're certain that people will not revert to an
2200 ;; earlier version, we can take out the old-list code in
2201 ;; gnus-category-write.
2202 (let* ((old-list (read (current-buffer)))
2203 (new-list (ignore-errors (read (current-buffer)))))
2206 ;; Convert from a positional list to an alist.
2216 '(agent-predicate agent-score-file agent-groups))))
2219 (list (gnus-agent-cat-make 'default 'short)))))
2221 (defun gnus-category-write ()
2222 "Write the category alist."
2223 (setq gnus-category-predicate-cache nil
2224 gnus-category-group-cache nil)
2225 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2226 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2227 ;; This prin1 is temporary. It exists so that people can revert
2228 ;; to an earlier version of gnus-agent.
2229 (prin1 (mapcar (lambda (c)
2231 (cdr (assoc 'agent-predicate c))
2232 (cdr (assoc 'agent-score-file c))
2233 (cdr (assoc 'agent-groups c))))
2234 gnus-category-alist)
2237 (prin1 gnus-category-alist (current-buffer))))
2239 (defun gnus-category-edit-predicate (category)
2240 "Edit the predicate for CATEGORY."
2241 (interactive (list (gnus-category-name)))
2242 (let ((info (assq category gnus-category-alist)))
2244 (gnus-agent-cat-predicate info)
2245 (format "Editing the select predicate for category %s" category)
2246 `(lambda (predicate)
2247 ;; Avoid run-time execution of setf form
2248 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2250 ;; use its expansion instead:
2251 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2252 'agent-predicate predicate)
2254 (gnus-category-write)
2255 (gnus-category-list)))))
2257 (defun gnus-category-edit-score (category)
2258 "Edit the score expression for CATEGORY."
2259 (interactive (list (gnus-category-name)))
2260 (let ((info (assq category gnus-category-alist)))
2262 (gnus-agent-cat-score-file info)
2263 (format "Editing the score expression for category %s" category)
2264 `(lambda (score-file)
2265 ;; Avoid run-time execution of setf form
2266 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2268 ;; use its expansion instead:
2269 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2270 'agent-score-file score-file)
2272 (gnus-category-write)
2273 (gnus-category-list)))))
2275 (defun gnus-category-edit-groups (category)
2276 "Edit the group list for CATEGORY."
2277 (interactive (list (gnus-category-name)))
2278 (let ((info (assq category gnus-category-alist)))
2280 (gnus-agent-cat-groups info)
2281 (format "Editing the group list for category %s" category)
2283 ;; Avoid run-time execution of setf form
2284 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2286 ;; use its expansion instead:
2287 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2290 (gnus-category-write)
2291 (gnus-category-list)))))
2293 (defun gnus-category-kill (category)
2294 "Kill the current category."
2295 (interactive (list (gnus-category-name)))
2296 (let ((info (assq category gnus-category-alist))
2297 (buffer-read-only nil))
2299 (setq gnus-category-alist (delq info gnus-category-alist))
2300 (gnus-category-write)))
2302 (defun gnus-category-copy (category to)
2303 "Copy the current category."
2304 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2305 (let ((info (assq category gnus-category-alist)))
2306 (push (let ((newcat (gnus-copy-sequence info)))
2307 (setf (gnus-agent-cat-name newcat) to)
2308 (setf (gnus-agent-cat-groups newcat) nil)
2310 gnus-category-alist)
2311 (gnus-category-write)
2312 (gnus-category-list)))
2314 (defun gnus-category-add (category)
2315 "Create a new category."
2316 (interactive "SCategory name: ")
2317 (when (assq category gnus-category-alist)
2318 (error "Category %s already exists" category))
2319 (push (gnus-agent-cat-make category)
2320 gnus-category-alist)
2321 (gnus-category-write)
2322 (gnus-category-list))
2324 (defun gnus-category-list ()
2325 "List all categories."
2327 (gnus-category-prepare))
2329 (defun gnus-category-exit ()
2330 "Return to the group buffer."
2332 (kill-buffer (current-buffer))
2333 (gnus-configure-windows 'group t))
2335 ;; To avoid having 8-bit characters in the source file.
2336 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2338 (defvar gnus-category-predicate-alist
2339 '((spam . gnus-agent-spam-p)
2340 (short . gnus-agent-short-p)
2341 (long . gnus-agent-long-p)
2342 (low . gnus-agent-low-scored-p)
2343 (high . gnus-agent-high-scored-p)
2344 (read . gnus-agent-read-p)
2345 (true . gnus-agent-true)
2346 (false . gnus-agent-false))
2347 "Mapping from short score predicate symbols to predicate functions.")
2349 (defun gnus-agent-spam-p ()
2350 "Say whether an article is spam or not."
2351 (unless gnus-agent-spam-hashtb
2352 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2353 (if (not (equal (mail-header-references gnus-headers) ""))
2355 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2357 (gnus-gethash string gnus-agent-spam-hashtb)
2358 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2360 (defun gnus-agent-short-p ()
2361 "Say whether an article is short or not."
2362 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2364 (defun gnus-agent-long-p ()
2365 "Say whether an article is long or not."
2366 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2368 (defun gnus-agent-low-scored-p ()
2369 "Say whether an article has a low score or not."
2370 (< gnus-score gnus-agent-low-score))
2372 (defun gnus-agent-high-scored-p ()
2373 "Say whether an article has a high score or not."
2374 (> gnus-score gnus-agent-high-score))
2376 (defun gnus-agent-read-p ()
2377 "Say whether an article is read or not."
2378 (gnus-member-of-range (mail-header-number gnus-headers)
2379 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2381 (defun gnus-category-make-function (predicate)
2382 "Make a function from PREDICATE."
2383 (let ((func (gnus-category-make-function-1 predicate)))
2384 (if (and (= (length func) 1)
2385 (symbolp (car func)))
2387 (gnus-byte-compile `(lambda () ,func)))))
2389 (defun gnus-agent-true ()
2393 (defun gnus-agent-false ()
2397 (defun gnus-category-make-function-1 (predicate)
2398 "Make a function from PREDICATE."
2400 ;; Functions are just returned as is.
2401 ((or (symbolp predicate)
2402 (functionp predicate))
2403 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2405 ;; More complex predicate.
2408 ((memq (car predicate) '(& and))
2410 ((memq (car predicate) '(| or))
2412 ((memq (car predicate) gnus-category-not)
2414 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2416 (error "Unknown predicate type: %s" predicate))))
2418 (defun gnus-get-predicate (predicate)
2419 "Return the function implementing PREDICATE."
2420 (or (cdr (assoc predicate gnus-category-predicate-cache))
2421 (let ((func (gnus-category-make-function predicate)))
2422 (setq gnus-category-predicate-cache
2423 (nconc gnus-category-predicate-cache
2424 (list (cons predicate func))))
2427 (defun gnus-predicate-implies-unread (predicate)
2428 "Say whether PREDICATE implies unread articles only.
2429 It is okay to miss some cases, but there must be no false positives.
2430 That is, if this function returns true, then indeed the predicate must
2431 return only unread articles."
2432 (gnus-function-implies-unread-1 (gnus-category-make-function predicate)))
2434 (defun gnus-function-implies-unread-1 (function)
2435 (cond ((eq function (symbol-function 'gnus-agent-read-p))
2439 ((functionp function)
2441 ((memq (car function) '(or and not))
2442 (apply (car function)
2443 (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2445 (error "Unknown function: %s" function))))
2447 (defun gnus-group-category (group)
2448 "Return the category GROUP belongs to."
2449 (unless gnus-category-group-cache
2450 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2451 (let ((cs gnus-category-alist)
2453 (while (setq cat (pop cs))
2454 (setq groups (gnus-agent-cat-groups cat))
2456 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2457 (or (gnus-gethash group gnus-category-group-cache)
2458 (assq 'default gnus-category-alist)))
2460 (defun gnus-agent-expire-group (group &optional articles force)
2461 "Expire all old articles in GROUP.
2462 If you want to force expiring of certain articles, this function can
2463 take ARTICLES, and FORCE parameters as well.
2465 The articles on which the expiration process runs are selected as follows:
2466 if ARTICLES is null, all read and unmarked articles.
2467 if ARTICLES is t, all articles.
2468 if ARTICLES is a list, just those articles.
2469 FORCE is equivalent to setting the expiration predicates to true."
2471 (list (let ((def (or (gnus-group-group-name)
2472 gnus-newsgroup-name)))
2473 (let ((select (read-string (if def
2474 (concat "Group Name ("
2477 (if (and (equal "" select)
2483 (gnus-agent-expire articles group force)
2484 (if (or (not (eq articles t))
2486 (concat "Are you sure that you want to "
2487 "expire all articles in " group ".")))
2488 (let ((gnus-command-method (gnus-find-method-for-group group))
2489 (overview (gnus-get-buffer-create " *expire overview*"))
2492 (let ((active-file (gnus-agent-lib-file "active")))
2493 (when (file-exists-p active-file)
2495 (nnheader-insert-file-contents active-file)
2496 (gnus-active-to-gnus-format
2498 (setq orig (gnus-make-hashtable
2499 (count-lines (point-min) (point-max))))))
2501 (gnus-agent-expire-group-1
2502 group overview (gnus-gethash-safe group orig)
2504 (gnus-agent-write-active active-file orig t)))
2505 (kill-buffer overview))))
2506 (gnus-message 4 "Expiry...done")))
2508 (defun gnus-agent-expire-group-1 (group overview active articles force)
2509 ;; Internal function - requires caller to have set
2510 ;; gnus-command-method, initialized overview buffer, and to have
2511 ;; provided a non-nil active
2513 (let ((dir (gnus-agent-group-pathname group)))
2514 (when (boundp 'gnus-agent-expire-current-dirs)
2515 (set 'gnus-agent-expire-current-dirs
2517 (symbol-value 'gnus-agent-expire-current-dirs))))
2519 (if (and (not force)
2520 (eq 'DISABLE (gnus-agent-find-parameter group
2521 'agent-enable-expiration)))
2522 (gnus-message 5 "Expiry skipping over %s" group)
2523 (gnus-message 5 "Expiring articles in %s" group)
2524 (gnus-agent-load-alist group)
2525 (let* ((info (gnus-get-info group))
2526 (alist gnus-agent-article-alist)
2527 (day (- (time-to-days (current-time))
2528 (gnus-agent-find-parameter group 'agent-days-until-old)))
2529 (specials (if (and alist
2531 ;; This could be a bit of a problem. I need to
2532 ;; keep the last article to avoid refetching
2533 ;; headers when using nntp in the backend. At
2534 ;; the same time, if someone uses a backend
2535 ;; that supports article moving then I may have
2536 ;; to remove the last article to complete the
2537 ;; move. Right now, I'm going to assume that
2538 ;; FORCE overrides specials.
2539 (list (caar (last alist)))))
2540 (unreads ;; Articles that are excluded from the
2541 ;; expiration process
2542 (cond (gnus-agent-expire-all
2543 ;; All articles are marked read by global decree
2546 ;; All articles are marked read by function
2550 ;; Unread articles are marked protected from
2551 ;; expiration Don't call
2552 ;; gnus-list-of-unread-articles as it returns
2553 ;; articles that have not been fetched into the
2556 (gnus-agent-unread-articles group)))
2558 ;; All articles EXCEPT those named by the caller
2559 ;; are protected from expiration
2560 (gnus-sorted-difference
2561 (gnus-uncompress-range
2563 (caar (last alist))))
2564 (sort articles '<)))))
2565 (marked ;; More articles that are exluded from the
2566 ;; expiration process
2567 (cond (gnus-agent-expire-all
2568 ;; All articles are unmarked by global decree
2571 ;; All articles are unmarked by function
2575 ;; All articles may as well be unmarked as the
2576 ;; unreads list already names the articles we are
2580 ;; Ticked and/or dormant articles are excluded
2583 (gnus-uncompress-range
2584 (cdr (assq 'tick (gnus-info-marks info))))
2585 (gnus-uncompress-range
2587 (gnus-info-marks info))))))))
2588 (nov-file (concat dir ".overview"))
2594 ;; The normal article alist contains elements that look like
2595 ;; (article# . fetch_date) I need to combine other
2596 ;; information with this list. For example, a flag indicating
2597 ;; that a particular article MUST BE KEPT. To do this, I'm
2598 ;; going to transform the elements to look like (article#
2599 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
2600 ;; the process to generate the expired article alist.
2602 ;; Convert the alist elements to (article# fetch_date nil
2604 (setq dlist (mapcar (lambda (e)
2605 (list (car e) (cdr e) nil nil)) alist))
2607 ;; Convert the keep lists to elements that look like (article#
2608 ;; nil keep_flag nil) then append it to the expanded dlist
2609 ;; These statements are sorted by ascending precidence of the
2611 (setq dlist (nconc dlist
2613 (list e nil 'unread nil))
2615 (setq dlist (nconc dlist
2617 (list e nil 'marked nil))
2619 (setq dlist (nconc dlist
2621 (list e nil 'special nil))
2624 (set-buffer overview)
2626 (buffer-disable-undo)
2627 (when (file-exists-p nov-file)
2628 (gnus-message 7 "gnus-agent-expire: Loading overview...")
2629 (nnheader-insert-file-contents nov-file)
2630 (goto-char (point-min))
2633 (while (< (setq p (point)) (point-max))
2635 ;; If I successfully read an integer (the plus zero
2636 ;; ensures a numeric type), prepend a marker entry
2638 (push (list (+ 0 (read (current-buffer))) nil nil
2639 (set-marker (make-marker) p))
2642 (gnus-message 1 "gnus-agent-expire: read error \
2643 occurred when reading expression at %s in %s. Skipping to next \
2644 line." (point) nov-file)))
2645 ;; Whether I succeeded, or failed, it doesn't matter.
2646 ;; Move to the next line then try again.
2650 7 "gnus-agent-expire: Loading overview... Done"))
2651 (set-buffer-modified-p nil)
2653 ;; At this point, all of the information is in dlist. The
2654 ;; only problem is that much of it is spread across multiple
2655 ;; entries. Sort then MERGE!!
2656 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
2657 ;; If two entries have the same article-number then sort by
2658 ;; ascending keep_flag.
2665 (cond ((< (nth 0 a) (nth 0 b))
2667 ((> (nth 0 a) (nth 0 b))
2670 (let ((a (or (symbol-value (nth 2 a))
2672 (b (or (symbol-value (nth 2 b))
2675 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
2676 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
2677 (let ((dlist dlist))
2678 (while (cdr dlist) ; I'm not at the end-of-list
2679 (if (eq (caar dlist) (caadr dlist))
2680 (let ((first (cdr (car dlist)))
2681 (secnd (cdr (cadr dlist))))
2682 (setcar first (or (car first)
2683 (car secnd))) ; fetch_date
2684 (setq first (cdr first)
2686 (setcar first (or (car first)
2687 (car secnd))) ; Keep_flag
2688 (setq first (cdr first)
2690 (setcar first (or (car first)
2691 (car secnd))) ; NOV_entry_marker
2693 (setcdr dlist (cddr dlist)))
2694 (setq dlist (cdr dlist)))))
2695 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
2697 (let* ((len (float (length dlist)))
2701 (let ((new-completed (truncate (* 100.0
2702 (/ (setq cnt (1+ cnt))
2704 (when (> new-completed completed)
2705 (setq completed new-completed)
2706 (gnus-message 7 "%3d%% completed..." completed)))
2707 (let* ((entry (car dlist))
2708 (article-number (nth 0 entry))
2709 (fetch-date (nth 1 entry))
2710 (keep (nth 2 entry))
2711 (marker (nth 3 entry)))
2714 ;; Kept articles are unread, marked, or special.
2716 (gnus-agent-message 10
2717 "gnus-agent-expire: Article %d: Kept %s article."
2718 article-number keep)
2720 (unless (file-exists-p
2721 (concat dir (number-to-string
2723 (setf (nth 1 entry) nil)
2724 (gnus-agent-message 3 "gnus-agent-expire cleared \
2725 download flag on article %d as the cached article file is missing."
2728 (gnus-message 1 "gnus-agent-expire detected a \
2729 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
2730 (gnus-agent-append-to-list
2732 (cons article-number fetch-date)))
2734 ;; The following articles are READ, UNMARKED, and
2735 ;; ORDINARY. See if they can be EXPIRED!!!
2738 ((not (integerp fetch-date))
2739 'read) ;; never fetched article (may expire
2741 ((not (file-exists-p
2742 (concat dir (number-to-string
2744 (setf (nth 1 entry) nil)
2745 'externally-expired) ;; Can't find the cached
2746 ;; article. Handle case
2747 ;; as though this article
2748 ;; was never fetched.
2750 ;; We now have the arrival day, so we see
2751 ;; whether it's old enough to be expired.
2757 ;; I found some reason to expire this entry.
2759 (let ((actions nil))
2760 (when (memq type '(forced expired))
2761 (ignore-errors ; Just being paranoid.
2762 (delete-file (concat dir (number-to-string
2764 (push "expired cached article" actions))
2765 (setf (nth 1 entry) nil)
2769 (push "NOV entry removed" actions)
2773 ;; If considering all articles is set, I can only
2774 ;; expire article IDs that are no longer in the
2775 ;; active range (That is, articles that preceed the
2776 ;; first article in the new alist).
2777 (if (and gnus-agent-consider-all-articles
2778 (>= article-number (car active)))
2779 ;; I have to keep this ID in the alist
2780 (gnus-agent-append-to-list
2781 tail-alist (cons article-number fetch-date))
2782 (push (format "Removed %s article number from \
2783 article alist" type) actions))
2785 (gnus-agent-message 8 "gnus-agent-expire: Article %d: %s"
2787 (mapconcat 'identity actions ", "))))
2790 10 "gnus-agent-expire: Article %d: Article kept as \
2791 expiration tests failed." article-number)
2792 (gnus-agent-append-to-list
2793 tail-alist (cons article-number fetch-date)))
2796 ;; Clean up markers as I want to recycle this buffer
2797 ;; over several groups.
2799 (set-marker marker nil))
2801 (setq dlist (cdr dlist))))
2803 (setq alist (cdr alist))
2805 (let ((inhibit-quit t))
2806 (unless (equal alist gnus-agent-article-alist)
2807 (setq gnus-agent-article-alist alist)
2808 (gnus-agent-save-alist group)
2810 ;; The active list changed, set the agent's active range
2811 ;; to match the beginning of the list.
2813 (setcar active (caar alist))))
2815 (when (buffer-modified-p)
2816 (gnus-make-directory dir)
2817 (write-region-as-coding-system gnus-agent-file-coding-system
2818 (point-min) (point-max) nov-file
2820 ;; clear the modified flag as that I'm not confused by
2821 ;; its status on the next pass through this routine.
2822 (set-buffer-modified-p nil))
2824 (when (eq articles t)
2825 (gnus-summary-update-info))))))))
2827 (defun gnus-agent-expire (&optional articles group force)
2828 "Expire all old articles.
2829 If you want to force expiring of certain articles, this function can
2830 take ARTICLES, GROUP and FORCE parameters as well.
2832 The articles on which the expiration process runs are selected as follows:
2833 if ARTICLES is null, all read and unmarked articles.
2834 if ARTICLES is t, all articles.
2835 if ARTICLES is a list, just those articles.
2836 Setting GROUP will limit expiration to that group.
2837 FORCE is equivalent to setting the expiration predicates to true."
2841 (gnus-agent-expire-group group articles force)
2842 (if (or (not (eq articles t))
2843 (yes-or-no-p "Are you sure that you want to expire all \
2844 articles in every agentized group."))
2845 (let ((methods gnus-agent-covered-methods)
2846 ;; Bind gnus-agent-expire-current-dirs to enable tracking
2847 ;; of agent directories.
2848 (gnus-agent-expire-current-dirs nil)
2849 gnus-command-method overview orig)
2850 (setq overview (gnus-get-buffer-create " *expire overview*"))
2852 (while (setq gnus-command-method (pop methods))
2853 (let ((active-file (gnus-agent-lib-file "active")))
2854 (when (file-exists-p active-file)
2856 (nnheader-insert-file-contents active-file)
2857 (gnus-active-to-gnus-format
2859 (setq orig (gnus-make-hashtable
2860 (count-lines (point-min) (point-max))))))
2861 (dolist (expiring-group (gnus-groups-from-server
2862 gnus-command-method))
2864 (gnus-gethash-safe expiring-group orig)))
2868 (gnus-agent-expire-group-1
2869 expiring-group overview active articles force)))))
2870 (gnus-agent-write-active active-file orig t))))
2871 (kill-buffer overview))
2872 (gnus-agent-expire-unagentized-dirs)
2873 (gnus-message 4 "Expiry...done")))))
2875 (defun gnus-agent-expire-unagentized-dirs ()
2876 (when (and gnus-agent-expire-unagentized-dirs
2877 (boundp 'gnus-agent-expire-current-dirs))
2878 (let* ((keep (gnus-make-hashtable))
2879 ;; Formally bind gnus-agent-expire-current-dirs so that the
2880 ;; compiler will not complain about free references.
2881 (gnus-agent-expire-current-dirs
2882 (symbol-value 'gnus-agent-expire-current-dirs))
2885 (gnus-sethash gnus-agent-directory t keep)
2886 (while gnus-agent-expire-current-dirs
2887 (setq dir (pop gnus-agent-expire-current-dirs))
2888 (when (and (stringp dir)
2889 (file-directory-p dir))
2890 (while (not (gnus-gethash dir keep))
2891 (gnus-sethash dir t keep)
2892 (setq dir (file-name-directory (directory-file-name dir))))))
2899 "Given a directory, check it and its subdirectories for
2900 membership in the keep hash. If it isn't found, add
2902 (let ((files (directory-files d))
2904 (while (setq file (pop files))
2905 (cond ((equal file ".") ; Ignore self
2907 ((equal file "..") ; Ignore parent
2909 ((equal file ".overview")
2910 ;; Directory must contain .overview to be
2911 ;; agent's cache of a group.
2912 (let ((d (file-name-as-directory d))
2914 ;; Search ancestor's for last directory NOT
2915 ;; found in keep hash.
2916 (while (not (gnus-gethash
2917 (setq d (file-name-directory d)) keep))
2919 d (directory-file-name d)))
2920 ;; if ANY ancestor was NOT in keep hash and
2921 ;; it it's already in to-remove, add it to
2924 (not (member r to-remove)))
2925 (push r to-remove))))
2926 ((file-directory-p (setq file (nnheader-concat d file)))
2927 (funcall checker file)))))))))
2928 (funcall checker (expand-file-name gnus-agent-directory))
2930 (when (and to-remove
2931 (or gnus-expert-user
2933 "gnus-agent-expire has identified local directories that are\
2934 not currently required by any agentized group. Do you wish to consider\
2937 (let ((dir (pop to-remove)))
2938 (if (gnus-y-or-n-p (format "Delete %s? " dir))
2939 (let* (delete-recursive
2944 (if (file-directory-p f-or-d)
2946 (delete-directory f-or-d)
2949 (or (member f '("." ".."))
2950 (funcall delete-recursive
2953 (directory-files f-or-d))
2954 (delete-directory f-or-d)))
2955 (delete-file f-or-d)))))))
2956 (funcall delete-recursive dir))))))))))
2959 (defun gnus-agent-batch ()
2960 "Start Gnus, send queue and fetch session."
2962 (let ((init-file-user "")
2963 (gnus-always-read-dribble-file t))
2965 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2966 (gnus-group-send-queue)
2967 (gnus-agent-fetch-session)))
2969 (defun gnus-agent-unread-articles (group)
2970 (let* ((read (gnus-info-read (gnus-get-info group)))
2971 (known (gnus-agent-load-alist group))
2973 (tail-unread unread))
2974 (while (and known read)
2975 (let ((candidate (car (pop known))))
2976 (while (let* ((range (car read))
2977 (min (if (numberp range) range (car range)))
2978 (max (if (numberp range) range (cdr range))))
2979 (cond ((or (not min)
2981 (gnus-agent-append-to-list tail-unread candidate)
2984 (setq read (cdr read))))))))
2986 (gnus-agent-append-to-list tail-unread (car (pop known))))
2989 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
2990 "Restrict ARTICLES to numbers already fetched.
2991 Returns a sublist of ARTICLES that excludes thos article ids in GROUP
2992 that have already been fetched.
2993 If CACHED-HEADER is nil, articles are only excluded if the article itself
2996 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
2997 ;; 'car gnus-agent-article-alist))
2999 ;; Functionally, I don't need to construct a temp list using mapcar.
3001 (if (and (or gnus-agent-cache (not gnus-plugged))
3002 (gnus-agent-load-alist group))
3003 (let* ((ref gnus-agent-article-alist)
3005 (uncached (list nil))
3006 (tail-uncached uncached))
3007 (while (and ref arts)
3008 (let ((v1 (car arts))
3010 (cond ((< v1 v2) ; v1 does not appear in the reference list
3011 (gnus-agent-append-to-list tail-uncached v1)
3012 (setq arts (cdr arts)))
3014 (unless (or cached-header (cdar ref)) ; v1 is already cached
3015 (gnus-agent-append-to-list tail-uncached v1))
3016 (setq arts (cdr arts))
3017 (setq ref (cdr ref)))
3018 (t ; reference article (v2) preceeds the list being filtered
3019 (setq ref (cdr ref))))))
3021 (gnus-agent-append-to-list tail-uncached (pop arts)))
3023 ;; if gnus-agent-load-alist fails, no articles are cached.
3026 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3028 (gnus-agent-create-buffer)
3029 (let ((gnus-decode-encoded-word-function 'identity)
3030 (file (gnus-agent-article-name ".overview" group))
3031 cached-articles uncached-articles)
3032 (gnus-make-directory (nnheader-translate-file-chars
3033 (file-name-directory file) t))
3035 ;; Populate temp buffer with known headers
3036 (when (file-exists-p file)
3037 (with-current-buffer gnus-agent-overview-buffer
3039 (let ((nnheader-file-coding-system
3040 gnus-agent-file-coding-system))
3041 (nnheader-insert-nov-file file (car articles)))))
3043 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3046 ;; Populate nntp-server-buffer with uncached headers
3047 (set-buffer nntp-server-buffer)
3049 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3050 (gnus-retrieve-headers
3051 uncached-articles group fetch-old))))
3052 (nnvirtual-convert-headers))
3053 ((eq 'nntp (car gnus-current-select-method))
3054 ;; The author of gnus-get-newsgroup-headers-xover
3055 ;; reports that the XOVER command is commonly
3056 ;; unreliable. The problem is that recently
3057 ;; posted articles may not be entered into the
3058 ;; NOV database in time to respond to my XOVER
3061 ;; I'm going to use his assumption that the NOV
3062 ;; database is updated in order of ascending
3063 ;; article ID. Therefore, a response containing
3064 ;; article ID N implies that all articles from 1
3065 ;; to N-1 are up-to-date. Therefore, missing
3066 ;; articles in that range have expired.
3068 (set-buffer nntp-server-buffer)
3069 (let* ((fetched-articles (list nil))
3070 (tail-fetched-articles fetched-articles)
3071 (min (cond ((numberp fetch-old)
3072 (max 1 (- (car articles) fetch-old)))
3077 (max (car (last articles))))
3079 ;; Get the list of articles that were fetched
3080 (goto-char (point-min))
3081 (let ((pm (point-max)))
3082 (while (< (point) pm)
3083 (when (looking-at "[0-9]+\t")
3084 (gnus-agent-append-to-list
3085 tail-fetched-articles
3086 (read (current-buffer))))
3089 ;; Clip this list to the headers that will
3090 ;; actually be returned
3091 (setq fetched-articles (gnus-list-range-intersection
3092 (cdr fetched-articles)
3095 ;; Clip the uncached articles list to exclude
3096 ;; IDs after the last FETCHED header. The
3097 ;; excluded IDs may be fetchable using HEAD.
3098 (if (car tail-fetched-articles)
3099 (setq uncached-articles
3100 (gnus-list-range-intersection
3102 (cons (car uncached-articles)
3103 (car tail-fetched-articles)))))
3105 ;; Create the list of articles that were
3106 ;; "successfully" fetched. Success, in this
3107 ;; case, means that the ID should not be
3108 ;; fetched again. In the case of an expired
3109 ;; article, the header will not be fetched.
3110 (setq uncached-articles
3111 (gnus-sorted-nunion fetched-articles
3115 ;; Erase the temp buffer
3116 (set-buffer gnus-agent-overview-buffer)
3119 ;; Copy the nntp-server-buffer to the temp buffer
3120 (set-buffer nntp-server-buffer)
3121 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3123 ;; Merge the temp buffer with the known headers (found on
3124 ;; disk in FILE) into the nntp-server-buffer
3125 (when (and uncached-articles (file-exists-p file))
3126 (gnus-agent-braid-nov group uncached-articles file))
3128 ;; Save the new set of known headers to FILE
3129 (set-buffer nntp-server-buffer)
3130 (gnus-agent-check-overview-buffer)
3131 (write-region-as-coding-system
3132 gnus-agent-file-coding-system
3133 (point-min) (point-max) file nil 'silent)
3135 ;; Update the group's article alist to include the newly
3136 ;; fetched articles.
3137 (gnus-agent-load-alist group)
3138 (gnus-agent-save-alist group uncached-articles nil)
3141 ;; Copy the temp buffer to the nntp-server-buffer
3142 (set-buffer nntp-server-buffer)
3144 (insert-buffer-substring gnus-agent-overview-buffer)))
3147 (not (numberp fetch-old)))
3148 t ; Don't remove anything.
3149 (nnheader-nov-delete-outside-range
3150 (if fetch-old (max 1 (- (car articles) fetch-old))
3152 (car (last articles)))
3157 (defun gnus-agent-request-article (article group)
3158 "Retrieve ARTICLE in GROUP from the agent cache."
3159 (when (and gnus-agent
3160 (or gnus-agent-cache
3163 (let* ((gnus-command-method (gnus-find-method-for-group group))
3164 (file (gnus-agent-article-name (number-to-string article) group))
3165 (buffer-read-only nil))
3166 (when (and (file-exists-p file)
3167 (> (nth 7 (file-attributes file)) 0))
3169 (gnus-kill-all-overlays)
3170 (insert-file-contents-as-coding-system gnus-cache-coding-system file)
3173 (defun gnus-agent-regenerate-group (group &optional reread)
3175 If REREAD is t, all articles in the .overview are marked as unread.
3176 If REREAD is not nil, downloaded articles are marked as unread."
3178 (list (let ((def (or (gnus-group-group-name)
3179 gnus-newsgroup-name)))
3180 (let ((select (read-string (if def
3181 (concat "Group Name ("
3184 (if (and (equal "" select)
3190 "Reread (nil)? (t=>all, nil=>none, some=>all downloaded): "))))
3191 (gnus-message 5 "Regenerating in %s" group)
3192 (let* ((gnus-command-method (or gnus-command-method
3193 (gnus-find-method-for-group group)))
3194 (file (gnus-agent-article-name ".overview" group))
3195 (dir (file-name-directory file))
3197 (downloaded (if (file-exists-p dir)
3198 (sort (mapcar (lambda (name) (string-to-int name))
3199 (directory-files dir nil "^[0-9]+$" t))
3201 (progn (gnus-make-directory dir) nil)))
3206 (mm-with-unibyte-buffer
3207 (if (file-exists-p file)
3208 (let ((nnheader-file-coding-system
3209 gnus-agent-file-coding-system))
3210 (nnheader-insert-file-contents file)))
3211 (set-buffer-modified-p nil)
3213 ;; Load the article IDs found in the overview file. As a
3214 ;; side-effect, validate the file contents.
3218 (goto-char (point-min))
3219 (while (< (point) (point-max))
3220 (cond ((and (looking-at "[0-9]+\t")
3221 (<= (- (match-end 0) (match-beginning 0)) 9))
3222 (push (read (current-buffer)) nov-arts)
3224 (let ((l1 (car nov-arts))
3225 (l2 (cadr nov-arts)))
3229 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3230 entries are NOT in ascending order.")
3231 ;; Don't sort now as I haven't verified
3232 ;; that every line begins with a number
3236 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3237 entries contained duplicate of article %s. Duplicate deleted." l1)
3239 (setq nov-arts (cdr nov-arts))))))
3241 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3242 entries contained line that did not begin with an article number. Deleted\
3244 (gnus-delete-line))))
3247 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3248 entries into ascending order.")
3249 (sort-numeric-fields 1 (point-min) (point-max))
3250 (setq nov-arts nil)))))
3251 (gnus-agent-check-overview-buffer)
3253 ;; Construct a new article alist whose nodes match every header
3254 ;; in the .overview file. As a side-effect, missing headers are
3255 ;; reconstructed from the downloaded article file.
3256 (while (or downloaded nov-arts)
3257 (cond ((and downloaded
3259 (> (car downloaded) (car nov-arts))))
3260 ;; This entry is missing from the overview file
3261 (gnus-message 3 "Regenerating NOV %s %d..." group
3263 (let ((file (concat dir (number-to-string (car downloaded)))))
3264 (mm-with-unibyte-buffer
3265 (nnheader-insert-file-contents file)
3266 (nnheader-remove-body)
3267 (setq header (nnheader-parse-naked-head)))
3268 (mail-header-set-number header (car downloaded))
3270 (let ((key (concat "^" (int-to-string (car nov-arts))
3272 (or (re-search-backward key nil t)
3273 (re-search-forward key))
3275 (goto-char (point-min)))
3276 (nnheader-insert-nov header))
3277 (setq nov-arts (cons (car downloaded) nov-arts)))
3278 ((eq (car downloaded) (car nov-arts))
3279 ;; This entry in the overview has been downloaded
3280 (push (cons (car downloaded)
3282 (nth 5 (file-attributes
3283 (concat dir (number-to-string
3284 (car downloaded))))))) alist)
3285 (setq downloaded (cdr downloaded))
3286 (setq nov-arts (cdr nov-arts)))
3288 ;; This entry in the overview has not been downloaded
3289 (push (cons (car nov-arts) nil) alist)
3290 (setq nov-arts (cdr nov-arts)))))
3292 ;; When gnus-agent-consider-all-articles is set,
3293 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3294 ;; the alist. Those IDs serve as markers to indicate that an
3295 ;; attempt has been made to fetch that article's header.
3297 ;; When gnus-agent-consider-all-articles is NOT set,
3298 ;; gnus-agent-regenerate-group can remove the article ID of every
3299 ;; article (with the exception of the last ID in the list - it's
3300 ;; special) that no longer appears in the overview. In this
3301 ;; situtation, the last article ID in the list implies that it,
3302 ;; and every article ID preceeding it, have been fetched from the
3304 (if gnus-agent-consider-all-articles
3305 ;; Restore all article IDs that were not found in the overview file.
3306 (let* ((n (cons nil alist))
3308 (o (gnus-agent-load-alist group)))
3310 (let ((nID (caadr n))
3313 (setq n (setcdr n (list (list oID))))
3316 (setcdr n (cons (list oID) (cdr n)))
3322 (setq n (cdr n))))))
3323 (setq alist (cdr merged)))
3324 ;; Restore the last article ID if it is not already in the new alist
3325 (let ((n (last alist))
3326 (o (last (gnus-agent-load-alist group))))
3330 (push (cons (caar o) nil) alist))
3331 ((< (caar n) (caar o))
3332 (setcdr n (list (car o)))))))
3334 (let ((inhibit-quit t))
3335 (if (setq regenerated (buffer-modified-p))
3336 (write-region-as-coding-system
3337 gnus-agent-file-coding-system
3338 (point-min) (point-max) file nil 'silent))
3340 (setq regenerated (or regenerated
3341 (and reread gnus-agent-article-alist)
3342 (not (equal alist gnus-agent-article-alist)))
3345 (setq gnus-agent-article-alist alist)
3348 (gnus-agent-save-alist group)))
3351 (when (and reread gnus-agent-article-alist)
3352 (gnus-make-ascending-articles-unread
3354 (delq nil (mapcar (function (lambda (c)
3355 (cond ((eq reread t)
3359 gnus-agent-article-alist)))
3361 (when (gnus-buffer-live-p gnus-group-buffer)
3362 (gnus-group-update-group group t)
3366 (gnus-message 5 nil)
3370 (defun gnus-agent-regenerate (&optional clean reread)
3371 "Regenerate all agent covered files.
3372 If CLEAN, don't read existing active files."
3375 (gnus-message 4 "Regenerating Gnus agent files...")
3376 (dolist (gnus-command-method gnus-agent-covered-methods)
3377 (let ((active-file (gnus-agent-lib-file "active"))
3378 active-hashtb active-changed
3380 (gnus-make-directory (file-name-directory active-file))
3382 (setq active-hashtb (gnus-make-hashtable 1000))
3383 (mm-with-unibyte-buffer
3384 (if (file-exists-p active-file)
3385 (let ((nnheader-file-coding-system
3386 gnus-agent-file-coding-system))
3387 (nnheader-insert-file-contents active-file))
3388 (setq active-changed t))
3389 (gnus-active-to-gnus-format
3390 nil (setq active-hashtb
3391 (gnus-make-hashtable
3392 (count-lines (point-min) (point-max)))))))
3393 (dolist (group (gnus-groups-from-server gnus-command-method))
3394 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3396 (let ((min (or (caar gnus-agent-article-alist) 1))
3397 (max (or (caar (last gnus-agent-article-alist)) 0))
3398 (active (gnus-gethash-safe (gnus-group-real-name group)
3400 (read (gnus-info-read (gnus-get-info group))))
3403 (setq active (cons min max)
3405 (gnus-sethash group active active-hashtb))
3406 (when (> (car active) min)
3408 (setq active-changed t))
3409 (when (< (cdr active) max)
3411 (setq active-changed t)))))
3412 (when active-changed
3413 (setq regenerated t)
3414 (gnus-message 4 "Regenerate %s" active-file)
3415 (let ((nnmail-active-file-coding-system
3416 gnus-agent-file-coding-system))
3417 (gnus-write-active-file active-file active-hashtb)))))
3418 (gnus-message 4 "Regenerating Gnus agent files...done")
3421 (defun gnus-agent-go-online (&optional force)
3422 "Switch servers into online status."
3423 (interactive (list t))
3424 (dolist (server gnus-opened-servers)
3425 (when (eq (nth 1 server) 'offline)
3426 (if (if (eq force 'ask)
3428 (format "Switch %s:%s into online status? "
3429 (caar server) (cadar server)))
3431 (setcar (nthcdr 1 server) 'close)))))
3433 (defun gnus-agent-toggle-group-plugged (group)
3434 "Toggle the status of the server of the current group."
3435 (interactive (list (gnus-group-group-name)))
3436 (let* ((method (gnus-find-method-for-group group))
3437 (status (cadr (assoc method gnus-opened-servers))))
3438 (if (eq status 'offline)
3439 (gnus-server-set-status method 'closed)
3440 (gnus-close-server method)
3441 (gnus-server-set-status method 'offline))
3442 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3443 (if (eq status 'offline) 'offline 'online)
3444 (if (eq status 'offline) 'online 'offline))))
3446 (defun gnus-agent-group-covered-p (group)
3447 (member (gnus-group-method group)
3448 gnus-agent-covered-methods))
3450 (add-hook 'gnus-group-prepare-hook
3454 (when (listp gnus-agent-expire-days)
3457 (gnus-message 1 "WARNING: gnus-agent-expire-days no longer\
3458 supports being set to a list.")(sleep-for 3)
3459 (gnus-message 1 "Change your configuration to set it to an\
3460 integer.")(sleep-for 3)
3461 (gnus-message 1 "I am now setting group parameters on each\
3462 group to match the configuration that the list offered.")
3465 (let ((groups (gnus-group-listed-groups)))
3467 (let* ((group (pop groups))
3468 (days gnus-agent-expire-days)
3471 (when (eq 0 (string-match
3474 (throw 'found (cadar days)))
3475 (setq days (cdr days)))
3478 (gnus-group-set-parameter group 'agent-days-until-old
3481 (let ((h gnus-group-prepare-hook))
3483 (let ((func (pop h)))
3484 (when (and (listp func)
3485 (eq (cadr (caddr func)) 'gnus-agent-do-once))
3486 (remove-hook 'gnus-group-prepare-hook func)
3489 (gnus-message 1 "I have finished setting group parameters on\
3490 each group. You may now customize your groups and/or topics to control the\
3493 (provide 'gnus-agent)
3495 ;;; gnus-agent.el ends here