1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
37 (if (featurep 'xemacs)
43 (autoload 'gnus-server-update-server "gnus-srvr")
44 (autoload 'gnus-agent-customize-category "gnus-cus")
47 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48 "Where the Gnus agent will store its files."
52 (defcustom gnus-agent-plugged-hook nil
53 "Hook run when plugging into the network."
57 (defcustom gnus-agent-unplugged-hook nil
58 "Hook run when unplugging from the network."
62 (defcustom gnus-agent-fetched-hook nil
63 "Hook run when finished fetching articles."
68 (defcustom gnus-agent-handle-level gnus-level-subscribed
69 "Groups on levels higher than this variable will be ignored by the Agent."
73 (defcustom gnus-agent-expire-days 7
74 "Read articles older than this will be expired.
75 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
77 :type '(number :tag "days"))
79 (defcustom gnus-agent-expire-all nil
80 "If non-nil, also expire unread, ticked and dormant articles.
81 If nil, only read articles will be expired."
85 (defcustom gnus-agent-group-mode-hook nil
86 "Hook run in Agent group minor modes."
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
94 (defcustom gnus-agent-summary-mode-hook nil
95 "Hook run in Agent summary minor modes."
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
103 (defcustom gnus-agent-server-mode-hook nil
104 "Hook run in Agent summary minor modes."
108 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
109 (when (featurep 'xemacs)
110 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
112 (defcustom gnus-agent-confirmation-function 'y-or-n-p
113 "Function to confirm when error happens."
118 (defcustom gnus-agent-synchronize-flags nil
119 "Indicate if flags are synchronized when you plug in.
120 If this is `ask' the hook will query the user."
121 ;; If the default switches to something else than nil, then the function
122 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
124 :type '(choice (const :tag "Always" t)
125 (const :tag "Never" nil)
126 (const :tag "Ask" ask))
129 (defcustom gnus-agent-go-online 'ask
130 "Indicate if offline servers go online when you plug in.
131 If this is `ask' the hook will query the user."
133 :type '(choice (const :tag "Always" t)
134 (const :tag "Never" nil)
135 (const :tag "Ask" ask))
138 (defcustom gnus-agent-mark-unread-after-downloaded t
139 "Indicate whether to mark articles unread after downloaded."
144 (defcustom gnus-agent-download-marks '(download)
145 "Marks for downloading."
147 :type '(repeat (symbol :tag "Mark"))
150 (defcustom gnus-agent-consider-all-articles nil
151 "When non-nil, the agent will let the agent predicate decide
152 whether articles need to be downloaded or not, for all articles. When
153 nil, the default, the agent will only let the predicate decide
154 whether unread articles are downloaded or not. If you enable this,
155 groups with large active ranges may open slower and you may also want
156 to look into the agent expiry settings to block the expiration of
157 read articles as they would just be downloaded again."
162 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
163 "Chunk size for `gnus-agent-fetch-session'.
164 The function will split its article fetches into chunks smaller than
170 (defcustom gnus-agent-enable-expiration 'ENABLE
171 "The default expiration state for each group.
172 When set to ENABLE, the default, `gnus-agent-expire' will expire old
173 contents from a group's local storage. This value may be overridden
174 to disable expiration in specific categories, topics, and groups. Of
175 course, you could change gnus-agent-enable-expiration to DISABLE then
176 enable expiration per categories, topics, and groups."
179 :type '(radio (const :format "Enable " ENABLE)
180 (const :format "Disable " DISABLE)))
182 (defcustom gnus-agent-expire-unagentized-dirs t
183 "*Whether expiration should expire in unagentized directories.
184 Have gnus-agent-expire scan the directories under
185 \(gnus-agent-directory) for groups that are no longer agentized.
186 When found, offer to remove them."
191 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
192 "Initially, all servers from these methods are agentized.
193 The user may remove or add servers using the Server buffer.
194 See Info node `(gnus)Server Buffer'."
196 :type '(repeat symbol)
199 (defcustom gnus-agent-queue-mail t
200 "Whether and when outgoing mail should be queued by the agent.
201 When `always', always queue outgoing mail. When nil, never
202 queue. Otherwise, queue if and only if unplugged."
205 :type '(radio (const :format "Always" always)
206 (const :format "Never" nil)
207 (const :format "When unplugged" t)))
209 (defcustom gnus-agent-prompt-send-queue nil
210 "If non-nil, `gnus-group-send-queue' will prompt if called when
216 (defcustom gnus-agent-article-alist-save-format 1
217 "Indicates whether to use compression(2), versus no
218 compression(1), when writing agentview files. The compressed
219 files do save space but load times are 6-7 times higher. A group
220 must be opened then closed for the agentview to be updated using
222 ;; Wouldn't symbols instead numbers be nicer? --rsteib
225 :type '(radio (const :format "Compressed" 2)
226 (const :format "Uncompressed" 1)))
228 ;;; Internal variables
230 (defvar gnus-agent-history-buffers nil)
231 (defvar gnus-agent-buffer-alist nil)
232 (defvar gnus-agent-article-alist nil
233 "An assoc list identifying the articles whose headers have been fetched.
234 If successfully fetched, these headers will be stored in the group's overview
235 file. The key of each assoc pair is the article ID, the value of each assoc
236 pair is a flag indicating whether the identified article has been downloaded
237 \(gnus-agent-fetch-articles sets the value to the day of the download).
239 1) The last element of this list can not be expired as some
240 routines (for example, get-agent-fetch-headers) use the last
241 value to track which articles have had their headers retrieved.
242 2) The function `gnus-agent-regenerate' may destructively modify the value.")
243 (defvar gnus-agent-group-alist nil)
244 (defvar gnus-category-alist nil)
245 (defvar gnus-agent-current-history nil)
246 (defvar gnus-agent-overview-buffer nil)
247 (defvar gnus-category-predicate-cache nil)
248 (defvar gnus-category-group-cache nil)
249 (defvar gnus-agent-spam-hashtb nil)
250 (defvar gnus-agent-file-name nil)
251 (defvar gnus-agent-send-mail-function nil)
252 (defvar gnus-agent-file-coding-system 'raw-text)
253 (defvar gnus-agent-file-loading-cache nil)
254 (defvar gnus-agent-total-fetched-hashtb nil)
255 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
256 (defvar gnus-agent-need-update-total-fetched-for nil)
259 (defvar gnus-headers)
266 (defun gnus-open-agent ()
268 (gnus-agent-read-servers)
270 (gnus-agent-create-buffer)
271 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
272 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
273 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
275 (defun gnus-agent-create-buffer ()
276 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
278 (setq gnus-agent-overview-buffer
279 (gnus-get-buffer-create " *Gnus agent overview*"))
280 (with-current-buffer gnus-agent-overview-buffer
281 (mm-enable-multibyte))
284 (gnus-add-shutdown 'gnus-close-agent 'gnus)
286 (defun gnus-close-agent ()
287 (setq gnus-category-predicate-cache nil
288 gnus-category-group-cache nil
289 gnus-agent-spam-hashtb nil)
290 (gnus-kill-buffer gnus-agent-overview-buffer))
293 ;;; Utility functions
296 (defmacro gnus-agent-with-refreshed-group (group &rest body)
297 "Performs the body then updates the group's line in the group
298 buffer. Automatically blocks multiple updates due to recursion."
299 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
300 (when (and gnus-agent-need-update-total-fetched-for
301 (not gnus-agent-inhibit-update-total-fetched-for))
303 (set-buffer gnus-group-buffer)
304 (setq gnus-agent-need-update-total-fetched-for nil)
305 (gnus-group-update-group ,group t)))))
307 (defun gnus-agent-read-file (file)
308 "Load FILE and do a `read' there."
311 (nnheader-insert-file-contents file)
312 (goto-char (point-min))
313 (read (current-buffer)))))
315 (defsubst gnus-agent-method ()
316 (concat (symbol-name (car gnus-command-method)) "/"
317 (if (equal (cadr gnus-command-method) "")
319 (cadr gnus-command-method))))
321 (defsubst gnus-agent-directory ()
322 "The name of the Gnus agent directory."
323 (nnheader-concat gnus-agent-directory
324 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
326 (defun gnus-agent-lib-file (file)
327 "The full name of the Gnus agent library FILE."
328 (expand-file-name file
329 (file-name-as-directory
330 (expand-file-name "agent.lib" (gnus-agent-directory)))))
332 (defun gnus-agent-cat-set-property (category property value)
334 (setcdr (or (assq property category)
335 (let ((cell (cons property nil)))
336 (setcdr category (cons cell (cdr category)))
338 (let ((category category))
339 (while (cond ((eq property (caadr category))
340 (setcdr category (cddr category))
343 (setq category (cdr category)))))))
347 (defmacro gnus-agent-cat-defaccessor (name prop-name)
348 "Define accessor and setter methods for manipulating a list of the form
349 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
350 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
351 manipulated as follows:
352 (func LIST): Returns VALUE1
353 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
354 `(progn (defmacro ,name (category)
355 (list (quote cdr) (list (quote assq)
356 (quote (quote ,prop-name)) category)))
358 (define-setf-method ,name (category)
359 (let* ((--category--temp-- (make-symbol "--category--"))
360 (--value--temp-- (make-symbol "--value--")))
361 (list (list --category--temp--) ; temporary-variables
362 (list category) ; value-forms
363 (list --value--temp--) ; store-variables
364 (let* ((category --category--temp--) ; store-form
365 (value --value--temp--))
366 (list (quote gnus-agent-cat-set-property)
368 (quote (quote ,prop-name))
370 (list (quote ,name) --category--temp--) ; access-form
374 (defmacro gnus-agent-cat-name (category)
377 (gnus-agent-cat-defaccessor
378 gnus-agent-cat-days-until-old agent-days-until-old)
379 (gnus-agent-cat-defaccessor
380 gnus-agent-cat-enable-expiration agent-enable-expiration)
381 (gnus-agent-cat-defaccessor
382 gnus-agent-cat-groups agent-groups)
383 (gnus-agent-cat-defaccessor
384 gnus-agent-cat-high-score agent-high-score)
385 (gnus-agent-cat-defaccessor
386 gnus-agent-cat-length-when-long agent-long-article)
387 (gnus-agent-cat-defaccessor
388 gnus-agent-cat-length-when-short agent-short-article)
389 (gnus-agent-cat-defaccessor
390 gnus-agent-cat-low-score agent-low-score)
391 (gnus-agent-cat-defaccessor
392 gnus-agent-cat-predicate agent-predicate)
393 (gnus-agent-cat-defaccessor
394 gnus-agent-cat-score-file agent-score)
395 (gnus-agent-cat-defaccessor
396 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
399 ;; This form is equivalent to defsetf except that it calls make-symbol
400 ;; whereas defsetf calls gensym (Using gensym creates a run-time
401 ;; dependency on the CL library).
404 (define-setf-method gnus-agent-cat-groups (category)
405 (let* ((--category--temp-- (make-symbol "--category--"))
406 (--groups--temp-- (make-symbol "--groups--")))
407 (list (list --category--temp--)
409 (list --groups--temp--)
410 (let* ((category --category--temp--)
411 (groups --groups--temp--))
412 (list (quote gnus-agent-set-cat-groups) category groups))
413 (list (quote gnus-agent-cat-groups) --category--temp--))))
416 (defun gnus-agent-set-cat-groups (category groups)
417 (unless (eq groups 'ignore)
419 (old-g (gnus-agent-cat-groups category)))
420 (cond ((eq new-g old-g)
421 ;; gnus-agent-add-group is fiddling with the group
422 ;; list. Still, Im done.
425 ((eq new-g (cdr old-g))
426 ;; gnus-agent-add-group is fiddling with the group list
427 (setcdr (or (assq 'agent-groups category)
428 (let ((cell (cons 'agent-groups nil)))
429 (setcdr category (cons cell (cdr category)))
432 (let ((groups groups))
434 (let* ((group (pop groups))
435 (old-category (gnus-group-category group)))
436 (if (eq category old-category)
438 (setf (gnus-agent-cat-groups old-category)
439 (delete group (gnus-agent-cat-groups
441 ;; Purge cache as preceeding loop invalidated it.
442 (setq gnus-category-group-cache nil))
444 (setcdr (or (assq 'agent-groups category)
445 (let ((cell (cons 'agent-groups nil)))
446 (setcdr category (cons cell (cdr category)))
449 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
450 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
452 ;;; Fetching setup functions.
454 (defun gnus-agent-start-fetch ()
455 "Initialize data structures for efficient fetching."
456 (gnus-agent-create-buffer))
458 (defun gnus-agent-stop-fetch ()
459 "Save all data structures and clean up."
460 (setq gnus-agent-spam-hashtb nil)
462 (set-buffer nntp-server-buffer)
465 (defmacro gnus-agent-with-fetch (&rest forms)
468 (let ((gnus-agent-fetching t))
469 (gnus-agent-start-fetch)
471 (gnus-agent-stop-fetch)))
473 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
474 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
476 (defmacro gnus-agent-append-to-list (tail value)
477 `(setq ,tail (setcdr ,tail (cons ,value nil))))
479 (defmacro gnus-agent-message (level &rest args)
480 `(if (<= ,level gnus-verbose)
487 (defvar gnus-agent-mode-hook nil
488 "Hook run when installing agent mode.")
490 (defvar gnus-agent-mode nil)
491 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
493 (defun gnus-agent-mode ()
494 "Minor mode for providing a agent support in Gnus buffers."
495 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
496 (symbol-name major-mode))
497 (match-string 1 (symbol-name major-mode))))
498 (mode (intern (format "gnus-agent-%s-mode" buffer))))
499 (set (make-local-variable 'gnus-agent-mode) t)
501 (set (make-local-variable mode) t)
503 (when (gnus-visual-p 'agent-menu 'menu)
504 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
505 (unless (assq 'gnus-agent-mode minor-mode-alist)
506 (push gnus-agent-mode-status minor-mode-alist))
507 (unless (assq mode minor-mode-map-alist)
508 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
510 minor-mode-map-alist))
511 (when (eq major-mode 'gnus-group-mode)
512 (let ((init-plugged gnus-plugged)
513 (gnus-agent-go-online nil))
514 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
515 ;; Therefore, make certain that the current value does not
516 ;; match the desired initial value.
517 (setq gnus-plugged :unknown)
518 (gnus-agent-toggle-plugged init-plugged)))
519 (gnus-run-hooks 'gnus-agent-mode-hook
520 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
522 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
523 (gnus-define-keys gnus-agent-group-mode-map
524 "Ju" gnus-agent-fetch-groups
525 "Jc" gnus-enter-category-buffer
526 "Jj" gnus-agent-toggle-plugged
527 "Js" gnus-agent-fetch-session
528 "JY" gnus-agent-synchronize-flags
529 "JS" gnus-group-send-queue
530 "Ja" gnus-agent-add-group
531 "Jr" gnus-agent-remove-group
532 "Jo" gnus-agent-toggle-group-plugged)
534 (defun gnus-agent-group-make-menu-bar ()
535 (unless (boundp 'gnus-agent-group-menu)
537 gnus-agent-group-menu gnus-agent-group-mode-map ""
539 ["Toggle plugged" gnus-agent-toggle-plugged t]
540 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
541 ["List categories" gnus-enter-category-buffer t]
542 ["Add (current) group to category" gnus-agent-add-group t]
543 ["Remove (current) group from category" gnus-agent-remove-group t]
544 ["Send queue" gnus-group-send-queue gnus-plugged]
546 ["All" gnus-agent-fetch-session gnus-plugged]
547 ["Group" gnus-agent-fetch-group gnus-plugged])
548 ["Synchronize flags" gnus-agent-synchronize-flags t]
551 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
552 (gnus-define-keys gnus-agent-summary-mode-map
553 "Jj" gnus-agent-toggle-plugged
554 "Ju" gnus-agent-summary-fetch-group
555 "JS" gnus-agent-fetch-group
556 "Js" gnus-agent-summary-fetch-series
557 "J#" gnus-agent-mark-article
558 "J\M-#" gnus-agent-unmark-article
559 "@" gnus-agent-toggle-mark
560 "Jc" gnus-agent-catchup)
562 (defun gnus-agent-summary-make-menu-bar ()
563 (unless (boundp 'gnus-agent-summary-menu)
565 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
567 ["Toggle plugged" gnus-agent-toggle-plugged t]
568 ["Mark as downloadable" gnus-agent-mark-article t]
569 ["Unmark as downloadable" gnus-agent-unmark-article t]
570 ["Toggle mark" gnus-agent-toggle-mark t]
571 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
572 ["Catchup undownloaded" gnus-agent-catchup t]))))
574 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
575 (gnus-define-keys gnus-agent-server-mode-map
576 "Jj" gnus-agent-toggle-plugged
577 "Ja" gnus-agent-add-server
578 "Jr" gnus-agent-remove-server)
580 (defun gnus-agent-server-make-menu-bar ()
581 (unless (boundp 'gnus-agent-server-menu)
583 gnus-agent-server-menu gnus-agent-server-mode-map ""
585 ["Toggle plugged" gnus-agent-toggle-plugged t]
586 ["Add" gnus-agent-add-server t]
587 ["Remove" gnus-agent-remove-server t]))))
589 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
590 (if (and (fboundp 'propertize)
591 (fboundp 'make-mode-line-mouse-map))
592 (propertize string 'local-map
593 (make-mode-line-mouse-map mouse-button mouse-func)
594 'mouse-face 'mode-line-highlight)
597 (defun gnus-agent-toggle-plugged (set-to)
598 "Toggle whether Gnus is unplugged or not."
599 (interactive (list (not gnus-plugged)))
600 (cond ((eq set-to gnus-plugged)
603 (setq gnus-plugged set-to)
604 (gnus-run-hooks 'gnus-agent-plugged-hook)
605 (setcar (cdr gnus-agent-mode-status)
606 (gnus-agent-make-mode-line-string " Plugged"
608 'gnus-agent-toggle-plugged))
609 (gnus-agent-go-online gnus-agent-go-online)
610 (gnus-agent-possibly-synchronize-flags))
612 (gnus-agent-close-connections)
613 (setq gnus-plugged set-to)
614 (gnus-run-hooks 'gnus-agent-unplugged-hook)
615 (setcar (cdr gnus-agent-mode-status)
616 (gnus-agent-make-mode-line-string " Unplugged"
618 'gnus-agent-toggle-plugged))))
619 (set-buffer-modified-p t))
621 (defmacro gnus-agent-while-plugged (&rest body)
622 `(let ((original-gnus-plugged gnus-plugged))
624 (progn (gnus-agent-toggle-plugged t)
626 (gnus-agent-toggle-plugged original-gnus-plugged))))
628 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
629 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
631 (defun gnus-agent-close-connections ()
632 "Close all methods covered by the Gnus agent."
633 (let ((methods (gnus-agent-covered-methods)))
635 (gnus-close-server (pop methods)))))
638 (defun gnus-unplugged ()
639 "Start Gnus unplugged."
641 (setq gnus-plugged nil)
645 (defun gnus-plugged ()
646 "Start Gnus plugged."
648 (setq gnus-plugged t)
652 (defun gnus-slave-unplugged (&optional arg)
653 "Read news as a slave unplugged."
655 (setq gnus-plugged nil)
656 (gnus arg nil 'slave))
659 (defun gnus-agentize ()
660 "Allow Gnus to be an offline newsreader.
662 The gnus-agentize function is now called internally by gnus when
663 gnus-agent is set. If you wish to avoid calling gnus-agentize,
664 customize gnus-agent to nil.
666 This will modify the `gnus-setup-news-hook', and
667 `message-send-mail-real-function' variables, and install the Gnus agent
668 minor mode in all Gnus buffers."
671 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
672 (unless gnus-agent-send-mail-function
673 (setq gnus-agent-send-mail-function
674 (or message-send-mail-real-function
675 (function (lambda () (funcall message-send-mail-function))))
676 message-send-mail-real-function 'gnus-agent-send-mail))
678 ;; If the servers file doesn't exist, auto-agentize some servers and
679 ;; save the servers file so this auto-agentizing isn't invoked
681 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
682 (gnus-message 3 "First time agent user, agentizing remote groups...")
684 (lambda (server-or-method)
685 (let ((method (gnus-server-to-method server-or-method)))
686 (when (memq (car method)
687 gnus-agent-auto-agentize-methods)
688 (push (gnus-method-to-server method)
689 gnus-agent-covered-methods)
690 (setq gnus-agent-method-p-cache nil))))
691 (cons gnus-select-method gnus-secondary-select-methods))
692 (gnus-agent-write-servers)))
694 (defun gnus-agent-queue-setup (&optional group-name)
695 "Make sure the queue group exists.
696 Optional arg GROUP-NAME allows to specify another group."
697 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
699 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
700 (let ((gnus-level-default-subscribed 1))
701 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
703 (gnus-group-set-parameter
704 (format "nndraft:%s" (or group-name "queue"))
705 'gnus-dummy '((gnus-draft-mode)))))
707 (defun gnus-agent-send-mail ()
708 (if (or (not gnus-agent-queue-mail)
709 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
710 (funcall gnus-agent-send-mail-function)
711 (goto-char (point-min))
713 (concat "^" (regexp-quote mail-header-separator) "\n"))
715 (gnus-agent-insert-meta-information 'mail)
716 (gnus-request-accept-article "nndraft:queue" nil t t)))
718 (defun gnus-agent-insert-meta-information (type &optional method)
719 "Insert meta-information into the message that says how it's to be posted.
720 TYPE can be either `mail' or `news'. If the latter, then METHOD can
723 (message-remove-header gnus-agent-meta-information-header)
724 (goto-char (point-min))
725 (insert gnus-agent-meta-information-header ": "
726 (symbol-name type) " " (format "%S" method)
729 (while (search-backward "\n" nil t)
730 (replace-match "\\n" t t))))
732 (defun gnus-agent-restore-gcc ()
733 "Restore GCC field from saved header."
735 (goto-char (point-min))
736 (while (re-search-forward
737 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
738 (replace-match "Gcc:" 'fixedcase))))
740 (defun gnus-agent-any-covered-gcc ()
742 (message-narrow-to-headers)
743 (let* ((gcc (mail-fetch-field "gcc" nil t))
745 (mapcar 'gnus-inews-group-method
746 (message-unquote-tokens
747 (message-tokenize-header
750 (while (and (not covered) methods)
751 (setq covered (gnus-agent-method-p (car methods))
752 methods (cdr methods)))
756 (defun gnus-agent-possibly-save-gcc ()
757 "Save GCC if Gnus is unplugged."
758 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
760 (goto-char (point-min))
761 (let ((case-fold-search t))
762 (while (re-search-forward "^gcc:" nil t)
763 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
765 (defun gnus-agent-possibly-do-gcc ()
766 "Do GCC if Gnus is plugged."
767 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
768 (gnus-inews-do-gcc)))
771 ;;; Group mode commands
774 (defun gnus-agent-fetch-groups (n)
775 "Put all new articles in the current groups into the Agent."
778 (error "Groups can't be fetched when Gnus is unplugged"))
779 (gnus-group-iterate n 'gnus-agent-fetch-group))
781 (defun gnus-agent-fetch-group (&optional group)
782 "Put all new articles in GROUP into the Agent."
783 (interactive (list (gnus-group-group-name)))
784 (setq group (or group gnus-newsgroup-name))
786 (error "No group on the current line"))
788 (gnus-agent-while-plugged
789 (let ((gnus-command-method (gnus-find-method-for-group group)))
790 (gnus-agent-with-fetch
791 (gnus-agent-fetch-group-1 group gnus-command-method)
792 (gnus-message 5 "Fetching %s...done" group)))))
794 (defun gnus-agent-add-group (category arg)
795 "Add the current group to an agent category."
801 (mapcar (lambda (cat) (list (symbol-name (car cat))))
805 (let ((cat (assq category gnus-category-alist))
807 (gnus-group-iterate arg
809 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
810 (setf (gnus-agent-cat-groups c)
811 (delete group (gnus-agent-cat-groups c))))
812 (push group groups)))
813 (setf (gnus-agent-cat-groups cat)
814 (nconc (gnus-agent-cat-groups cat) groups))
815 (gnus-category-write)))
817 (defun gnus-agent-remove-group (arg)
818 "Remove the current group from its agent category, if any."
821 (gnus-group-iterate arg
823 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
824 (setf (gnus-agent-cat-groups c)
825 (delete group (gnus-agent-cat-groups c))))))
826 (gnus-category-write)))
828 (defun gnus-agent-synchronize-flags ()
829 "Synchronize unplugged flags with servers."
832 (dolist (gnus-command-method (gnus-agent-covered-methods))
833 (when (file-exists-p (gnus-agent-lib-file "flags"))
834 (gnus-agent-synchronize-flags-server gnus-command-method)))))
836 (defun gnus-agent-possibly-synchronize-flags ()
837 "Synchronize flags according to `gnus-agent-synchronize-flags'."
840 (dolist (gnus-command-method (gnus-agent-covered-methods))
841 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
842 (eq (gnus-server-status gnus-command-method) 'ok))
843 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
845 (defun gnus-agent-synchronize-flags-server (method)
846 "Synchronize flags set when unplugged for server."
847 (let ((gnus-command-method method)
849 (when (file-exists-p (gnus-agent-lib-file "flags"))
850 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
852 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
853 (cond ((null gnus-plugged)
855 1 "You must be plugged to synchronize flags with server %s"
856 (nth 1 gnus-command-method)))
857 ((null (gnus-check-server gnus-command-method))
859 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
864 (eval (read (current-buffer)))
865 (delete-region bgn (point))))
867 (delete-file (gnus-agent-lib-file "flags")))
869 (let ((file (gnus-agent-lib-file "flags")))
870 (write-region (point-min) (point-max)
871 (gnus-agent-lib-file "flags") nil 'silent)
872 (error "Couldn't set flags from file %s due to %s"
873 file (error-message-string err)))))))
876 (defun gnus-agent-possibly-synchronize-flags-server (method)
877 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
878 (when (or (and gnus-agent-synchronize-flags
879 (not (eq gnus-agent-synchronize-flags 'ask)))
880 (and (eq gnus-agent-synchronize-flags 'ask)
881 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
883 (gnus-agent-synchronize-flags-server method)))
886 (defun gnus-agent-rename-group (old-group new-group)
887 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
888 Always updates the agent, even when disabled, as the old agent
889 files would corrupt gnus when the agent was next enabled.
890 Depends upon the caller to determine whether group renaming is
892 (let* ((old-command-method (gnus-find-method-for-group old-group))
893 (old-path (directory-file-name
894 (let (gnus-command-method old-command-method)
895 (gnus-agent-group-pathname old-group))))
896 (new-command-method (gnus-find-method-for-group new-group))
897 (new-path (directory-file-name
898 (let (gnus-command-method new-command-method)
899 (gnus-agent-group-pathname new-group)))))
900 (gnus-rename-file old-path new-path t)
902 (let* ((old-real-group (gnus-group-real-name old-group))
903 (new-real-group (gnus-group-real-name new-group))
904 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
905 (gnus-agent-save-group-info old-command-method old-real-group nil)
906 (gnus-agent-save-group-info new-command-method new-real-group old-active)
908 (let ((old-local (gnus-agent-get-local old-group
909 old-real-group old-command-method)))
910 (gnus-agent-set-local old-group
912 old-real-group old-command-method)
913 (gnus-agent-set-local new-group
914 (car old-local) (cdr old-local)
915 new-real-group new-command-method)))))
918 (defun gnus-agent-delete-group (group)
919 "Delete fully-qualified GROUP.
920 Always updates the agent, even when disabled, as the old agent
921 files would corrupt gnus when the agent was next enabled.
922 Depends upon the caller to determine whether group deletion is
924 (let* ((command-method (gnus-find-method-for-group group))
925 (path (directory-file-name
926 (let (gnus-command-method command-method)
927 (gnus-agent-group-pathname group)))))
928 (gnus-delete-directory path)
930 (let* ((real-group (gnus-group-real-name group)))
931 (gnus-agent-save-group-info command-method real-group nil)
933 (let ((local (gnus-agent-get-local group
934 real-group command-method)))
935 (gnus-agent-set-local group
937 real-group command-method)))))
940 ;;; Server mode commands
943 (defun gnus-agent-add-server ()
944 "Enroll SERVER in the agent program."
946 (let* ((server (gnus-server-server-name))
947 (named-server (gnus-server-named-server))
949 (gnus-server-get-method nil server))))
951 (error "No server on the current line"))
953 (when (gnus-agent-method-p method)
954 (error "Server already in the agent program"))
956 (push named-server gnus-agent-covered-methods)
958 (setq gnus-agent-method-p-cache nil)
959 (gnus-server-update-server server)
960 (gnus-agent-write-servers)
961 (gnus-message 1 "Entered %s into the Agent" server)))
963 (defun gnus-agent-remove-server ()
964 "Remove SERVER from the agent program."
966 (let* ((server (gnus-server-server-name))
967 (named-server (gnus-server-named-server)))
969 (error "No server on the current line"))
971 (unless (member named-server gnus-agent-covered-methods)
972 (error "Server not in the agent program"))
974 (setq gnus-agent-covered-methods
975 (delete named-server gnus-agent-covered-methods)
976 gnus-agent-method-p-cache nil)
978 (gnus-server-update-server server)
979 (gnus-agent-write-servers)
980 (gnus-message 1 "Removed %s from the agent" server)))
982 (defun gnus-agent-read-servers ()
983 "Read the alist of covered servers."
984 (setq gnus-agent-covered-methods
985 (gnus-agent-read-file
986 (nnheader-concat gnus-agent-directory "lib/servers"))
987 gnus-agent-method-p-cache nil)
989 ;; I am called so early in start-up that I can not validate server
990 ;; names. When that is the case, I skip the validation. That is
991 ;; alright as the gnus startup code calls the validate methods
993 (if gnus-server-alist
994 (gnus-agent-read-servers-validate)))
996 (defun gnus-agent-read-servers-validate ()
997 (mapcar (lambda (server-or-method)
998 (let* ((server (if (stringp server-or-method)
1000 (gnus-method-to-server server-or-method)))
1001 (method (gnus-server-to-method server)))
1003 (unless (member server gnus-agent-covered-methods)
1004 (push server gnus-agent-covered-methods)
1005 (setq gnus-agent-method-p-cache nil))
1006 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1007 (prog1 gnus-agent-covered-methods
1008 (setq gnus-agent-covered-methods nil))))
1010 (defun gnus-agent-read-servers-validate-native (native-method)
1011 (setq gnus-agent-covered-methods
1012 (mapcar (lambda (method)
1013 (if (or (not method)
1014 (equal method native-method))
1016 method)) gnus-agent-covered-methods)))
1018 (defun gnus-agent-write-servers ()
1019 "Write the alist of covered servers."
1020 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1021 (let ((coding-system-for-write nnheader-file-coding-system)
1022 (file-name-coding-system nnmail-pathname-coding-system))
1023 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1024 (prin1 gnus-agent-covered-methods
1025 (current-buffer)))))
1028 ;;; Summary commands
1031 (defun gnus-agent-mark-article (n &optional unmark)
1032 "Mark the next N articles as downloadable.
1033 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1034 the mark instead. The difference between N and the actual number of
1035 articles marked is returned."
1037 (let ((backward (< n 0))
1042 (gnus-summary-set-agent-mark
1043 (gnus-summary-article-number) unmark)
1044 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1047 (gnus-message 7 "No more articles"))
1048 (gnus-summary-recenter)
1049 (gnus-summary-position-point)
1052 (defun gnus-agent-unmark-article (n)
1053 "Remove the downloadable mark from the next N articles.
1054 If N is negative, unmark backward instead. The difference between N and
1055 the actual number of articles unmarked is returned."
1057 (gnus-agent-mark-article n t))
1059 (defun gnus-agent-toggle-mark (n)
1060 "Toggle the downloadable mark from the next N articles.
1061 If N is negative, toggle backward instead. The difference between N and
1062 the actual number of articles toggled is returned."
1064 (gnus-agent-mark-article n 'toggle))
1066 (defun gnus-summary-set-agent-mark (article &optional unmark)
1067 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1068 When UNMARK is t, the article is unmarked. For any other value, the
1069 article's mark is toggled."
1070 (let ((unmark (cond ((eq nil unmark)
1075 (memq article gnus-newsgroup-downloadable)))))
1076 (when (gnus-summary-goto-subject article nil t)
1077 (gnus-summary-update-mark
1080 (setq gnus-newsgroup-downloadable
1081 (delq article gnus-newsgroup-downloadable))
1082 (gnus-article-mark article))
1083 (setq gnus-newsgroup-downloadable
1084 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1085 gnus-downloadable-mark)
1089 (defun gnus-agent-get-undownloaded-list ()
1090 "Construct list of articles that have not been downloaded."
1091 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1092 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1093 (gnus-agent-method-p gnus-command-method))
1094 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1095 (headers (sort (mapcar (lambda (h)
1096 (mail-header-number h))
1097 gnus-newsgroup-headers) '<))
1098 (cached (and gnus-use-cache gnus-newsgroup-cached))
1099 (undownloaded (list nil))
1100 (tail-undownloaded undownloaded)
1101 (unfetched (list nil))
1102 (tail-unfetched unfetched))
1103 (while (and alist headers)
1104 (let ((a (caar alist))
1107 ;; Ignore IDs in the alist that are not being
1108 ;; displayed in the summary.
1109 (setq alist (cdr alist)))
1111 ;; Headers that are not in the alist should be
1112 ;; fictious (see nnagent-retrieve-headers); they
1113 ;; imply that this article isn't in the agent.
1114 (gnus-agent-append-to-list tail-undownloaded h)
1115 (gnus-agent-append-to-list tail-unfetched h)
1116 (setq headers (cdr headers)))
1118 (setq alist (cdr alist))
1119 (setq headers (cdr headers))
1120 nil ; ignore already downloaded
1123 (setq alist (cdr alist))
1124 (setq headers (cdr headers))
1126 ;; This article isn't in the agent. Check to see
1127 ;; if it is in the cache. If it is, it's been
1129 (while (and cached (< (car cached) a))
1130 (setq cached (cdr cached)))
1131 (unless (equal a (car cached))
1132 (gnus-agent-append-to-list tail-undownloaded a))))))
1135 (let ((num (pop headers)))
1136 (gnus-agent-append-to-list tail-undownloaded num)
1137 (gnus-agent-append-to-list tail-unfetched num)))
1139 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1140 gnus-newsgroup-unfetched (cdr unfetched))))))
1142 (defun gnus-agent-catchup ()
1143 "Mark as read all unhandled articles.
1144 An article is unhandled if it is neither cached, nor downloaded, nor
1148 (let ((articles gnus-newsgroup-undownloaded))
1149 (when (or gnus-newsgroup-downloadable
1150 gnus-newsgroup-cached)
1151 (setq articles (gnus-sorted-ndifference
1152 (gnus-sorted-ndifference
1153 (gnus-copy-sequence articles)
1154 gnus-newsgroup-downloadable)
1155 gnus-newsgroup-cached)))
1158 (gnus-summary-mark-article
1159 (pop articles) gnus-catchup-mark)))
1160 (gnus-summary-position-point)))
1162 (defun gnus-agent-summary-fetch-series ()
1164 (when gnus-newsgroup-processable
1165 (setq gnus-newsgroup-downloadable
1166 (let* ((dl gnus-newsgroup-downloadable)
1167 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1168 (gnus-newsgroup-downloadable processable))
1169 (gnus-agent-summary-fetch-group)
1171 ;; For each article that I processed that is no longer
1172 ;; undownloaded, remove its processable mark.
1174 (mapc #'gnus-summary-remove-process-mark
1175 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1177 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1178 ;; updated the temporary gnus-newsgroup-downloadable to
1179 ;; remove each article successfully fetched. Now, I
1180 ;; update the real gnus-newsgroup-downloadable to only
1181 ;; include undownloaded articles.
1182 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1184 (defun gnus-agent-summary-fetch-group (&optional all)
1185 "Fetch the downloadable articles in the group.
1186 Optional arg ALL, if non-nil, means to fetch all articles."
1189 (if all gnus-newsgroup-articles
1190 gnus-newsgroup-downloadable))
1191 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1193 (gnus-agent-while-plugged
1195 (error "No articles to download"))
1196 (gnus-agent-with-fetch
1197 (setq gnus-newsgroup-undownloaded
1198 (gnus-sorted-ndifference
1199 gnus-newsgroup-undownloaded
1200 (setq fetched-articles
1201 (gnus-agent-fetch-articles
1202 gnus-newsgroup-name articles)))))
1204 (dolist (article articles)
1205 (let ((was-marked-downloadable
1206 (memq article gnus-newsgroup-downloadable)))
1207 (cond (gnus-agent-mark-unread-after-downloaded
1208 (setq gnus-newsgroup-downloadable
1209 (delq article gnus-newsgroup-downloadable))
1211 (gnus-summary-mark-article article gnus-unread-mark))
1212 (was-marked-downloadable
1213 (gnus-summary-set-agent-mark article t)))
1214 (when (gnus-summary-goto-subject article nil t)
1215 (gnus-summary-update-download-mark article))))))
1218 (defun gnus-agent-fetch-selected-article ()
1219 "Fetch the current article as it is selected.
1220 This can be added to `gnus-select-article-hook' or
1221 `gnus-mark-article-hook'."
1222 (let ((gnus-command-method gnus-current-select-method))
1223 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1224 (when (gnus-agent-fetch-articles
1226 (list gnus-current-article))
1227 (setq gnus-newsgroup-undownloaded
1228 (delq gnus-current-article gnus-newsgroup-undownloaded))
1229 (gnus-summary-update-download-mark gnus-current-article)))))
1232 ;;; Internal functions
1235 (defun gnus-agent-synchronize-group-flags (group actions server)
1236 "Update a plugged group by performing the indicated actions."
1237 (let* ((gnus-command-method (gnus-server-to-method server))
1239 ;; This initializer is required as gnus-request-set-mark
1240 ;; calls gnus-group-real-name to strip off the host name
1241 ;; before calling the backend. Now that the backend is
1242 ;; trying to call gnus-request-set-mark, I have to
1243 ;; reconstruct the original group name.
1244 (or (gnus-get-info group)
1246 (setq group (gnus-group-full-name
1247 group gnus-command-method))))))
1248 (gnus-request-set-mark group actions)
1251 (dolist (action actions)
1252 (let ((range (nth 0 action))
1253 (what (nth 1 action))
1254 (marks (nth 2 action)))
1255 (dolist (mark marks)
1256 (cond ((eq mark 'read)
1259 (funcall (if (eq what 'add)
1261 'gnus-remove-from-range)
1262 (gnus-info-read info)
1264 (gnus-get-unread-articles-in-group
1266 (gnus-active (gnus-info-group info))))
1267 ((memq mark '(tick))
1268 (let ((info-marks (assoc mark (gnus-info-marks info))))
1270 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1271 (setcdr info-marks (funcall (if (eq what 'add)
1273 'gnus-remove-from-range)
1277 ;;Marks can be synchronized at any time by simply toggling from
1278 ;;unplugged to plugged. If that is what is happening right now, make
1279 ;;sure that the group buffer is up to date.
1280 (when (gnus-buffer-live-p gnus-group-buffer)
1281 (gnus-group-update-group group t)))
1284 (defun gnus-agent-save-active (method)
1285 (when (gnus-agent-method-p method)
1286 (let* ((gnus-command-method method)
1287 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1288 (file (gnus-agent-lib-file "active")))
1289 (gnus-active-to-gnus-format nil new)
1290 (gnus-agent-write-active file new)
1292 (nnheader-insert-file-contents file))))
1294 (defun gnus-agent-write-active (file new)
1295 (gnus-make-directory (file-name-directory file))
1296 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1297 ;; The hashtable contains real names of groups. However, do NOT
1298 ;; add the foreign server prefix as gnus-active-to-gnus-format
1299 ;; will add it while reading the file.
1300 (gnus-write-active-file file new nil)))
1303 (defun gnus-agent-possibly-alter-active (group active &optional info)
1304 "Possibly expand a group's active range to include articles
1305 downloaded into the agent."
1306 (let* ((gnus-command-method (or gnus-command-method
1307 (gnus-find-method-for-group group))))
1308 (when (gnus-agent-method-p gnus-command-method)
1309 (let* ((local (gnus-agent-get-local group))
1310 (active-min (or (car active) 0))
1311 (active-max (or (cdr active) 0))
1312 (agent-min (or (car local) active-min))
1313 (agent-max (or (cdr local) active-max)))
1315 (when (< agent-min active-min)
1316 (setcar active agent-min))
1318 (when (> agent-max active-max)
1319 (setcdr active agent-max))
1321 (when (and info (< agent-max (- active-min 100)))
1322 ;; I'm expanding the active range by such a large amount
1323 ;; that there is a gap of more than 100 articles between the
1324 ;; last article known to the agent and the first article
1325 ;; currently available on the server. This gap contains
1326 ;; articles that have been lost, mark them as read so that
1327 ;; gnus doesn't waste resources trying to fetch them.
1329 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1330 ;; want to modify the local file everytime someone restarts
1331 ;; gnus. The small gap will cause a tiny performance hit
1332 ;; when gnus tries, and fails, to retrieve the articles.
1333 ;; Still that should be smaller than opening a buffer,
1334 ;; printing this list to the buffer, and then writing it to a
1337 (let ((read (gnus-info-read info)))
1342 (list (cons (1+ agent-max)
1343 (1- active-min))))))
1345 ;; Lie about the agent's local range for this group to
1346 ;; disable the set read each time this server is opened.
1347 ;; NOTE: Opening this group will restore the valid local
1348 ;; range but it will also expand the local range to
1349 ;; incompass the new active range.
1350 (gnus-agent-set-local group agent-min (1- active-min)))))))
1352 (defun gnus-agent-save-group-info (method group active)
1353 "Update a single group's active range in the agent's copy of the server's active file."
1354 (when (gnus-agent-method-p method)
1355 (let* ((gnus-command-method (or method gnus-command-method))
1356 (coding-system-for-write nnheader-file-coding-system)
1357 (file-name-coding-system nnmail-pathname-coding-system)
1358 (file (gnus-agent-lib-file "active"))
1359 oactive-min oactive-max)
1360 (gnus-make-directory (file-name-directory file))
1361 (with-temp-file file
1362 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1363 (mm-disable-multibyte)
1364 (when (file-exists-p file)
1365 (nnheader-insert-file-contents file)
1367 (goto-char (point-min))
1368 (when (re-search-forward
1369 (concat "^" (regexp-quote group) " ") nil t)
1371 (setq oactive-max (read (current-buffer)) ;; max
1372 oactive-min (read (current-buffer)))) ;; min
1373 (gnus-delete-line)))
1375 (insert (format "%S %d %d y\n" (intern group)
1376 (max (or oactive-max (cdr active)) (cdr active))
1377 (min (or oactive-min (car active)) (car active))))
1378 (goto-char (point-max))
1379 (while (search-backward "\\." nil t)
1380 (delete-char 1)))))))
1382 (defun gnus-agent-get-group-info (method group)
1383 "Get a single group's active range in the agent's copy of the server's active file."
1384 (when (gnus-agent-method-p method)
1385 (let* ((gnus-command-method (or method gnus-command-method))
1386 (coding-system-for-write nnheader-file-coding-system)
1387 (file-name-coding-system nnmail-pathname-coding-system)
1388 (file (gnus-agent-lib-file "active"))
1389 oactive-min oactive-max)
1390 (gnus-make-directory (file-name-directory file))
1392 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1393 (mm-disable-multibyte)
1394 (when (file-exists-p file)
1395 (nnheader-insert-file-contents file)
1397 (goto-char (point-min))
1398 (when (re-search-forward
1399 (concat "^" (regexp-quote group) " ") nil t)
1401 (setq oactive-max (read (current-buffer)) ;; max
1402 oactive-min (read (current-buffer))) ;; min
1403 (cons oactive-min oactive-max))))))))
1405 (defun gnus-agent-group-path (group)
1406 "Translate GROUP into a file name."
1408 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1409 ;; The two methods must be kept synchronized, which is why
1410 ;; gnus-agent-group-pathname was added.
1413 (nnheader-translate-file-chars
1414 (nnheader-replace-duplicate-chars-in-string
1415 (nnheader-replace-chars-in-string
1416 (gnus-group-real-name (gnus-group-decoded-name group))
1419 (if (or nnmail-use-long-file-names
1420 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1422 (mm-encode-coding-string
1423 (nnheader-replace-chars-in-string group ?. ?/)
1424 nnmail-pathname-coding-system)))
1426 (defun gnus-agent-group-pathname (group)
1427 "Translate GROUP into a file name."
1428 ;; nnagent uses nnmail-group-pathname to read articles while
1429 ;; unplugged. The agent must, therefore, use the same directory
1431 (let ((gnus-command-method (or gnus-command-method
1432 (gnus-find-method-for-group group))))
1433 (nnmail-group-pathname (gnus-group-real-name
1434 (gnus-group-decoded-name group))
1435 (gnus-agent-directory))))
1437 (defun gnus-agent-get-function (method)
1438 (if (gnus-online method)
1443 (defun gnus-agent-covered-methods ()
1444 "Return the subset of methods that are covered by the agent."
1445 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1447 ;;; History functions
1449 (defun gnus-agent-history-buffer ()
1450 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1452 (defun gnus-agent-open-history ()
1454 (push (cons (gnus-agent-method)
1455 (set-buffer (gnus-get-buffer-create
1456 (format " *Gnus agent %s history*"
1457 (gnus-agent-method)))))
1458 gnus-agent-history-buffers)
1459 (mm-disable-multibyte) ;; everything is binary
1462 (let ((file (gnus-agent-lib-file "history")))
1463 (when (file-exists-p file)
1464 (nnheader-insert-file-contents file))
1465 (set (make-local-variable 'gnus-agent-file-name) file))))
1467 (defun gnus-agent-close-history ()
1468 (when (gnus-buffer-live-p gnus-agent-current-history)
1469 (kill-buffer gnus-agent-current-history)
1470 (setq gnus-agent-history-buffers
1471 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1472 gnus-agent-history-buffers))))
1478 (defun gnus-agent-fetch-articles (group articles)
1479 "Fetch ARTICLES from GROUP and put them into the Agent."
1481 (gnus-agent-load-alist group)
1482 (let* ((alist gnus-agent-article-alist)
1483 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1484 (selected-sets (list nil))
1485 (current-set-size 0)
1488 ;; Check each article
1489 (while (setq article (pop articles))
1490 ;; Skip alist entries preceeding this article
1491 (while (> article (or (caar alist) (1+ article)))
1492 (setq alist (cdr alist)))
1494 ;; Prune off articles that we have already fetched.
1495 (unless (and (eq article (caar alist))
1497 ;; Skip headers preceeding this article
1500 (let* ((header (car headers)))
1502 (mail-header-number header)
1504 (setq headers (cdr headers)))
1506 ;; Add this article to the current set
1507 (setcar selected-sets (cons article (car selected-sets)))
1509 ;; Update the set size, when the set is too large start a
1510 ;; new one. I do this after adding the article as I want at
1511 ;; least one article in each set.
1512 (when (< gnus-agent-max-fetch-size
1513 (setq current-set-size
1515 (if (= header-number article)
1516 (let ((char-size (mail-header-chars
1518 (if (<= char-size 0)
1519 ;; The char size was missing/invalid,
1520 ;; assume a worst-case situation of
1521 ;; 65 char/line. If the line count
1522 ;; is missing, arbitrarily assume a
1523 ;; size of 1000 characters.
1524 (max (* 65 (mail-header-lines
1529 (setcar selected-sets (nreverse (car selected-sets)))
1530 (setq selected-sets (cons nil selected-sets)
1531 current-set-size 0))))
1533 (when (or (cdr selected-sets) (car selected-sets))
1534 (let* ((fetched-articles (list nil))
1535 (tail-fetched-articles fetched-articles)
1536 (dir (gnus-agent-group-pathname group))
1537 (date (time-to-days (current-time)))
1538 (case-fold-search t)
1541 (setcar selected-sets (nreverse (car selected-sets)))
1542 (setq selected-sets (nreverse selected-sets))
1544 (gnus-make-directory dir)
1545 (gnus-message 7 "Fetching articles for %s..." group)
1548 (while (setq articles (pop selected-sets))
1549 ;; Fetch the articles from the backend.
1550 (if (gnus-check-backend-function 'retrieve-articles group)
1551 (setq pos (gnus-retrieve-articles articles group))
1554 (while (setq article (pop articles))
1555 (gnus-message 10 "Fetching article %s for %s..."
1558 (gnus-backlog-request-article group article
1560 (gnus-request-article article group))
1561 (goto-char (point-max))
1562 (push (cons article (point)) pos)
1563 (insert-buffer-substring nntp-server-buffer)))
1565 nntp-server-buffer (point-min) (point-max))
1566 (setq pos (nreverse pos)))))
1567 ;; Then save these articles into the Agent.
1569 (set-buffer nntp-server-buffer)
1571 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1572 (goto-char (point-min))
1573 (unless (eobp) ;; Don't save empty articles.
1574 (when (search-forward "\n\n" nil t)
1575 (when (search-backward "\nXrefs: " nil t)
1576 ;; Handle cross posting.
1577 (goto-char (match-end 0)) ; move to end of header name
1578 (skip-chars-forward "^ ") ; skip server name
1579 (skip-chars-forward " ")
1581 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1582 (push (cons (buffer-substring (match-beginning 1)
1585 (buffer-substring (match-beginning 2)
1588 (goto-char (match-end 0)))
1589 (gnus-agent-crosspost crosses (caar pos) date)))
1590 (goto-char (point-min))
1591 (if (not (re-search-forward
1592 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1593 (setq id "No-Message-ID-in-article")
1594 (setq id (buffer-substring
1595 (match-beginning 1) (match-end 1))))
1596 (let ((coding-system-for-write
1597 gnus-agent-file-coding-system))
1598 (write-region (point-min) (point-max)
1599 (concat dir (number-to-string (caar pos)))
1602 (gnus-agent-append-to-list
1603 tail-fetched-articles (caar pos)))
1605 (setq pos (cdr pos)))))
1607 (gnus-agent-save-alist group (cdr fetched-articles) date)
1608 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1610 (gnus-message 7 ""))
1611 (cdr fetched-articles))))))
1613 (defun gnus-agent-unfetch-articles (group articles)
1614 "Delete ARTICLES that were fetched from GROUP into the agent."
1616 (gnus-agent-with-refreshed-group
1618 (gnus-agent-load-alist group)
1619 (let* ((alist (cons nil gnus-agent-article-alist))
1620 (articles (sort articles #'<))
1621 (next-possibility alist)
1622 (delete-this (pop articles)))
1623 (while (and (cdr next-possibility) delete-this)
1624 (let ((have-this (caar (cdr next-possibility))))
1625 (cond ((< delete-this have-this)
1626 (setq delete-this (pop articles)))
1627 ((= delete-this have-this)
1628 (let ((timestamp (cdar (cdr next-possibility))))
1630 (let* ((file-name (concat (gnus-agent-group-pathname group)
1631 (number-to-string have-this)))
1632 (size-file (float (or (and gnus-agent-total-fetched-hashtb
1633 (nth 7 (file-attributes file-name)))
1635 (delete-file file-name)
1636 (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1638 (setcdr next-possibility (cddr next-possibility)))
1640 (setq next-possibility (cdr next-possibility))))))
1641 (setq gnus-agent-article-alist (cdr alist))
1642 (gnus-agent-save-alist group)))))
1644 (defun gnus-agent-crosspost (crosses article &optional date)
1645 (setq date (or date t))
1647 (let (gnus-agent-article-alist group alist beg end)
1649 (set-buffer gnus-agent-overview-buffer)
1650 (when (nnheader-find-nov-line article)
1653 (setq end (progn (forward-line 1) (point)))))
1655 (setq group (caar crosses))
1656 (unless (setq alist (assoc group gnus-agent-group-alist))
1657 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1658 gnus-agent-group-alist))
1659 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1661 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1663 (when (= (point-max) (point-min))
1664 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1666 (nnheader-insert-file-contents
1667 (gnus-agent-article-name ".overview" group))))
1668 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1669 (insert (string-to-number (cdar crosses)))
1670 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1671 (gnus-agent-check-overview-buffer))
1672 (setq crosses (cdr crosses)))))
1674 (defun gnus-agent-backup-overview-buffer ()
1675 (when gnus-newsgroup-name
1676 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1679 (while (file-exists-p
1680 (setq name (concat root "~"
1681 (int-to-string (setq cnt (1+ cnt))) "~"))))
1682 (write-region (point-min) (point-max) name nil 'no-msg)
1683 (gnus-message 1 "Created backup copy of overview in %s." name)))
1686 (defun gnus-agent-check-overview-buffer (&optional buffer)
1687 "Check the overview file given for sanity.
1688 In particular, checks that the file is sorted by article number
1689 and that there are no duplicates."
1694 (set-buffer buffer))
1697 (goto-char (point-min))
1699 (while (< (point) (point-max))
1701 (cur (condition-case nil
1702 (read (current-buffer))
1705 ((or (not (integerp cur))
1706 (not (eq (char-after) ?\t)))
1708 (setq backed-up (gnus-agent-backup-overview-buffer)))
1710 "Overview buffer contains garbage '%s'."
1715 (setq backed-up (gnus-agent-backup-overview-buffer)))
1717 "Duplicate overview line for %d" cur)
1718 (delete-region p (progn (forward-line 1) (point))))
1721 (setq backed-up (gnus-agent-backup-overview-buffer)))
1722 (gnus-message 1 "Overview buffer not sorted!")
1723 (sort-numeric-fields 1 (point-min) (point-max))
1724 (goto-char (point-min))
1727 (setq prev-num cur)))
1728 (forward-line 1)))))))
1730 (defun gnus-agent-flush-server (&optional server-or-method)
1731 "Flush all agent index files for every subscribed group within
1732 the given SERVER-OR-METHOD. When called with nil, the current
1733 value of gnus-command-method identifies the server."
1734 (let* ((gnus-command-method (if server-or-method
1735 (gnus-server-to-method server-or-method)
1736 gnus-command-method))
1737 (alist gnus-newsrc-alist))
1739 (let ((entry (pop alist)))
1740 (when (gnus-methods-equal-p gnus-command-method (gnus-info-method entry))
1741 (gnus-agent-flush-group (gnus-info-group entry)))))))
1743 (defun gnus-agent-flush-group (group)
1744 "Flush the agent's index files such that the GROUP no longer
1745 appears to have any local content. The actual content, the
1746 article files, may then be deleted using gnus-agent-expire-group.
1747 If flushing was a mistake, the gnus-agent-regenerate-group method
1748 provides an undo mechanism by reconstructing the index files from
1751 (list (let ((def (or (gnus-group-group-name)
1752 gnus-newsgroup-name)))
1753 (let ((select (read-string (if def
1754 (concat "Group Name ("
1757 (if (and (equal "" select)
1762 (let* ((gnus-command-method (or gnus-command-method
1763 (gnus-find-method-for-group group)))
1764 (overview (gnus-agent-article-name ".overview" group))
1765 (agentview (gnus-agent-article-name ".agentview" group)))
1767 (if (file-exists-p overview)
1768 (delete-file overview))
1769 (if (file-exists-p agentview)
1770 (delete-file agentview))
1772 (gnus-agent-update-view-total-fetched-for group nil gnus-command-method)
1773 (gnus-agent-update-view-total-fetched-for group t gnus-command-method)
1775 ;(gnus-agent-set-local group nil nil)
1776 ;(gnus-agent-save-local t)
1777 (gnus-agent-save-group-info nil group nil)))
1779 (defun gnus-agent-flush-cache ()
1780 "Flush the agent's index files such that the group no longer
1781 appears to have any local content. The actual content, the
1782 article files, is then deleted using gnus-agent-expire-group. The
1783 gnus-agent-regenerate-group method provides an undo mechanism by
1784 reconstructing the index files from the article files."
1786 (while gnus-agent-buffer-alist
1787 (set-buffer (cdar gnus-agent-buffer-alist))
1788 (let ((coding-system-for-write
1789 gnus-agent-file-coding-system))
1790 (write-region (point-min) (point-max)
1791 (gnus-agent-article-name ".overview"
1792 (caar gnus-agent-buffer-alist))
1794 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1795 (while gnus-agent-group-alist
1796 (with-temp-file (gnus-agent-article-name
1797 ".agentview" (caar gnus-agent-group-alist))
1798 (princ (cdar gnus-agent-group-alist))
1800 (princ 1 (current-buffer))
1802 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1805 (defun gnus-agent-find-parameter (group symbol)
1806 "Search for GROUPs SYMBOL in the group's parameters, the group's
1807 topic parameters, the group's category, or the customizable
1808 variables. Returns the first non-nil value found."
1809 (or (gnus-group-find-parameter group symbol t)
1810 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1814 '((agent-short-article . gnus-agent-short-article)
1815 (agent-long-article . gnus-agent-long-article)
1816 (agent-low-score . gnus-agent-low-score)
1817 (agent-high-score . gnus-agent-high-score)
1818 (agent-days-until-old . gnus-agent-expire-days)
1819 (agent-enable-expiration
1820 . gnus-agent-enable-expiration)
1821 (agent-predicate . gnus-agent-predicate)))))))
1823 (defun gnus-agent-fetch-headers (group &optional force)
1824 "Fetch interesting headers into the agent. The group's overview
1825 file will be updated to include the headers while a list of available
1826 article numbers will be returned."
1827 (let* ((fetch-all (and gnus-agent-consider-all-articles
1828 ;; Do not fetch all headers if the predicate
1829 ;; implies that we only consider unread articles.
1830 (not (gnus-predicate-implies-unread
1831 (gnus-agent-find-parameter group
1832 'agent-predicate)))))
1833 (articles (if fetch-all
1834 (gnus-uncompress-range (gnus-active group))
1835 (gnus-list-of-unread-articles group)))
1836 (gnus-decode-encoded-word-function 'identity)
1837 (file (gnus-agent-article-name ".overview" group)))
1840 ;; Add articles with marks to the list of article headers we want to
1841 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1842 ;; mark, but do fetch recent or seen articles if they have other, more
1843 ;; interesting marks. (We have to fetch articles with boring marks
1844 ;; because otherwise the agent will remove their marks.)
1845 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1846 (unless (memq (car arts) '(seen recent killed cache))
1847 (setq articles (gnus-range-add articles (cdr arts)))))
1848 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1850 ;; At this point, I have the list of articles to consider for
1851 ;; fetching. This is the list that I'll return to my caller. Some
1852 ;; of these articles may have already been fetched. That's OK as
1853 ;; the fetch article code will filter those out. Internally, I'll
1854 ;; filter this list to just those articles whose headers need to
1856 (let ((articles articles))
1857 ;; Remove known articles.
1858 (when (and (or gnus-agent-cache
1860 (gnus-agent-load-alist group))
1861 ;; Remove articles marked as downloaded.
1863 ;; I want to fetch all headers in the active range.
1864 ;; Therefore, exclude only those headers that are in the
1866 ;; NOTE: This is probably NOT what I want to do after
1867 ;; agent expiration in this group.
1868 (setq articles (gnus-agent-uncached-articles articles group))
1870 ;; I want to only fetch those headers that have never been
1871 ;; fetched. Therefore, exclude all headers that are, or
1872 ;; WERE, in the article alist.
1873 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1874 (high (cdr (gnus-active group))))
1875 ;; Low can be greater than High when the same group is
1876 ;; fetched twice in the same session {The first fetch will
1877 ;; fill the article alist such that (last
1878 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1879 ;; group))}. The addition of one(the 1+ above) then
1880 ;; forces Low to be greater than High. When this happens,
1881 ;; gnus-list-range-intersection returns nil which
1882 ;; indicates that no headers need to be fetched. -- Kevin
1883 (setq articles (gnus-list-range-intersection
1884 articles (list (cons low high)))))))
1887 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1888 (gnus-compress-sequence articles t))
1891 (set-buffer nntp-server-buffer)
1895 (gnus-message 7 "Fetching headers for %s..." group)
1898 (gnus-make-directory (nnheader-translate-file-chars
1899 (file-name-directory file) t))
1901 (unless (eq 'nov (gnus-retrieve-headers articles group))
1902 (nnvirtual-convert-headers))
1903 (gnus-agent-check-overview-buffer)
1904 ;; Move these headers to the overview buffer so that
1905 ;; gnus-agent-braid-nov can merge them with the contents
1908 gnus-agent-overview-buffer (point-min) (point-max))
1909 ;; NOTE: Call g-a-brand-nov even when the file does not
1910 ;; exist. As a minimum, it will validate the article
1911 ;; numbers already in the buffer.
1912 (gnus-agent-braid-nov group articles file)
1913 (let ((coding-system-for-write
1914 gnus-agent-file-coding-system))
1915 (gnus-agent-check-overview-buffer)
1916 (write-region (point-min) (point-max) file nil 'silent))
1917 (gnus-agent-update-view-total-fetched-for group t)
1918 (gnus-agent-save-alist group articles nil)
1922 (nnheader-insert-file-contents file)))))
1925 (defsubst gnus-agent-read-article-number ()
1926 "Reads the article number at point. Returns nil when a valid article number can not be read."
1928 ;; It is unfortunate but the read function quietly overflows
1929 ;; integer. As a result, I have to use string operations to test
1930 ;; for overflow BEFORE calling read.
1931 (when (looking-at "[0-9]+\t")
1932 (let ((len (- (match-end 0) (match-beginning 0))))
1934 (read (current-buffer)))
1936 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1937 ;; Back convert from int to string to ensure that this is one of them.
1938 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1939 (num (read (current-buffer)))
1940 (str2 (int-to-string num)))
1941 (when (equal str1 str2)
1944 (defsubst gnus-agent-copy-nov-line (article)
1945 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1947 (set-buffer gnus-agent-overview-buffer)
1948 (while (and (not (eobp))
1949 (or (not (setq art (gnus-agent-read-article-number)))
1954 (not (eq article art)))
1955 (set-buffer nntp-server-buffer)
1957 (setq e (progn (forward-line 1) (point)))
1958 (set-buffer nntp-server-buffer)
1959 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1961 (defun gnus-agent-braid-nov (group articles file)
1962 "Merge agent overview data with given file.
1963 Takes unvalidated headers for ARTICLES from
1964 `gnus-agent-overview-buffer' and validated headers from the given
1965 FILE and places the combined valid headers into
1966 `nntp-server-buffer'. This function can be used, when file
1967 doesn't exist, to valid the overview buffer."
1969 (set-buffer gnus-agent-overview-buffer)
1970 (goto-char (point-min))
1971 (set-buffer nntp-server-buffer)
1973 (when (file-exists-p file)
1974 (nnheader-insert-file-contents file))
1975 (goto-char (point-max))
1978 (unless (or (= (point-min) (point-max))
1979 (< (setq last (read (current-buffer))) (car articles)))
1980 ;; Old and new overlap -- We do it the hard way.
1981 (when (nnheader-find-nov-line (car articles))
1982 ;; Replacing existing NOV entry
1983 (delete-region (point) (progn (forward-line 1) (point))))
1984 (gnus-agent-copy-nov-line (pop articles))
1988 (while (let ((art (read (current-buffer))))
1989 (cond ((< art (car articles))
1992 ((= art (car articles))
1995 (point) (progn (forward-line 1) (point)))
2001 (gnus-agent-copy-nov-line (pop articles)))))
2003 (goto-char (point-max))
2005 ;; Append the remaining lines
2008 (set-buffer gnus-agent-overview-buffer)
2009 (setq start (point))
2010 (set-buffer nntp-server-buffer))
2013 (insert-buffer-substring gnus-agent-overview-buffer start)
2016 (setq last (or last -134217728))
2017 (while (catch 'problems
2020 (setq art (gnus-agent-read-article-number))
2022 ;; Bad art num - delete this line
2024 (delete-region (point) (progn (forward-line 1) (point))))
2026 ;; Art num out of order - enable sort
2030 ;; Bad repeat of art number - delete this line
2032 (delete-region (point) (progn (forward-line 1) (point))))
2038 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
2039 ;; First, we'll fix the sort.
2040 (sort-numeric-fields 1 (point-min) (point-max))
2042 ;; but now we have to consider that we may have duplicate rows...
2043 ;; so reset to beginning of file
2044 (goto-char (point-min))
2045 (setq last -134217728)
2047 ;; and throw a code that restarts this scan
2048 (throw 'problems t))
2051 ;; Keeps the compiler from warning about the free variable in
2052 ;; gnus-agent-read-agentview.
2054 (defvar gnus-agent-read-agentview))
2056 (defun gnus-agent-load-alist (group)
2057 "Load the article-state alist for GROUP."
2058 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
2059 (let ((gnus-agent-read-agentview group))
2060 (setq gnus-agent-article-alist
2061 (gnus-cache-file-contents
2062 (gnus-agent-article-name ".agentview" group)
2063 'gnus-agent-file-loading-cache
2064 'gnus-agent-read-agentview))))
2066 (defun gnus-agent-read-agentview (file)
2067 "Load FILE and do a `read' there."
2071 (nnheader-insert-file-contents file)
2072 (goto-char (point-min))
2073 (let ((alist (read (current-buffer)))
2074 (version (condition-case nil (read (current-buffer))
2080 (let ((inhibit-quit t)
2082 (gnus-agent-open-history)
2083 (set-buffer (gnus-agent-history-buffer))
2084 (goto-char (point-min))
2086 (if (and (looking-at
2087 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2088 (string= (match-string 2)
2089 gnus-agent-read-agentview)
2090 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2091 (setcdr entry (string-to-number (match-string 1))))
2093 (gnus-agent-close-history)
2094 (setq changed-version t)))
2096 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2101 (let ((state (car comp-list))
2103 (gnus-uncompress-range
2105 (mapcar (lambda (article-id)
2106 (setq uncomp (cons (cons article-id state) uncomp)))
2109 (setq alist (sort uncomp 'car-less-than-car)))
2110 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2111 (when changed-version
2112 (let ((gnus-agent-article-alist alist))
2113 (gnus-agent-save-alist gnus-agent-read-agentview)))
2115 ((end-of-file file-error)
2116 ;; The agentview file is missing. Perform a brute-force
2117 ;; reconstruction of its contents.
2119 (file-attributes (directory-files-and-attributes
2120 (gnus-agent-article-name ""
2121 gnus-agent-read-agentview) nil "^[0-9]+$" t)))
2122 (while file-attributes
2123 (let* ((fa (pop file-attributes))
2124 (artnum (string-to-number (nth 0 fa)))
2125 (days (time-to-days (nth 5 fa))))
2126 (push (cons artnum days) alist)))
2129 (defun gnus-agent-save-alist (group &optional articles state)
2130 "Save the article-state alist for GROUP."
2131 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2132 (prev (cons nil gnus-agent-article-alist))
2134 print-level print-length item article)
2135 (while (setq article (pop articles))
2136 (while (and (cdr prev)
2137 (< (caadr prev) article))
2138 (setq prev (cdr prev)))
2141 (setcdr prev (list (cons article state))))
2142 ((> (caadr prev) article)
2143 (setcdr prev (cons (cons article state) (cdr prev))))
2144 ((= (caadr prev) article)
2145 (setcdr (cadr prev) state)))
2146 (setq prev (cdr prev)))
2147 (setq gnus-agent-article-alist (cdr all))
2149 (gnus-agent-set-local group
2150 (caar gnus-agent-article-alist)
2151 (caar (last gnus-agent-article-alist)))
2153 (gnus-make-directory (gnus-agent-article-name "" group))
2154 (with-temp-file (gnus-agent-article-name ".agentview" group)
2155 (cond ((eq gnus-agent-article-alist-save-format 1)
2156 (princ gnus-agent-article-alist (current-buffer)))
2157 ((eq gnus-agent-article-alist-save-format 2)
2158 (let ((compressed nil))
2159 (mapcar (lambda (pair)
2160 (let* ((article-id (car pair))
2161 (day-of-download (cdr pair))
2162 (comp-list (assq day-of-download compressed)))
2165 (cons article-id (cdr comp-list)))
2167 (cons (list day-of-download article-id)
2169 nil)) gnus-agent-article-alist)
2170 (mapcar (lambda (comp-list)
2172 (gnus-compress-sequence
2173 (nreverse (cdr comp-list)))))
2175 (princ compressed (current-buffer)))))
2177 (princ gnus-agent-article-alist-save-format (current-buffer))
2180 (gnus-agent-update-view-total-fetched-for group nil)))
2182 (defvar gnus-agent-article-local nil)
2183 (defvar gnus-agent-file-loading-local nil)
2185 (defun gnus-agent-load-local (&optional method)
2186 "Load the METHOD'S local file. The local file contains min/max
2187 article counts for each of the method's subscribed groups."
2188 (let ((gnus-command-method (or method gnus-command-method)))
2189 (setq gnus-agent-article-local
2190 (gnus-cache-file-contents
2191 (gnus-agent-lib-file "local")
2192 'gnus-agent-file-loading-local
2193 'gnus-agent-read-and-cache-local))))
2195 (defun gnus-agent-read-and-cache-local (file)
2196 "Load and read FILE then bind its contents to
2197 gnus-agent-article-local. If that variable had `dirty' (also known as
2198 modified) original contents, they are first saved to their own file."
2200 (if (and gnus-agent-article-local
2201 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2202 (gnus-agent-save-local))
2203 (gnus-agent-read-local file))
2205 (defun gnus-agent-read-local (file)
2206 "Load FILE and do a `read' there."
2207 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2212 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2213 (nnheader-insert-file-contents file))
2216 (goto-char (point-min))
2217 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2218 (while (= (following-char) ?\;)
2220 (setq line (1+ line)))
2227 (cur (current-buffer))
2228 (obarray my-obarray))
2229 (setq group (read cur)
2233 (when (stringp group)
2234 (setq group (intern group my-obarray)))
2236 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2237 (set group (cons (+ 0 min) (+ 0 max))))
2239 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2240 file line (error-message-string err))))
2242 (setq line (1+ line))))
2244 (set (intern "+dirty" my-obarray) nil)
2245 (set (intern "+method" my-obarray) gnus-command-method)
2248 (defun gnus-agent-save-local (&optional force)
2249 "Save gnus-agent-article-local under it method's agent.lib directory."
2250 (let ((my-obarray gnus-agent-article-local))
2251 (when (and my-obarray
2252 (or force (symbol-value (intern "+dirty" my-obarray))))
2253 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2254 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2255 (dest (gnus-agent-lib-file "local")))
2256 (gnus-make-directory (gnus-agent-lib-file ""))
2258 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2259 (with-temp-file dest
2260 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2261 (file-name-coding-system nnmail-pathname-coding-system)
2262 print-level print-length item article
2263 (standard-output (current-buffer)))
2264 (mapatoms (lambda (symbol)
2265 (cond ((not (boundp symbol))
2267 ((member (symbol-name symbol) '("+dirty" "+method"))
2270 (let ((range (symbol-value symbol)))
2280 (defun gnus-agent-get-local (group &optional gmane method)
2281 (let* ((gmane (or gmane (gnus-group-real-name group)))
2282 (gnus-command-method (or method (gnus-find-method-for-group group)))
2283 (local (gnus-agent-load-local))
2284 (symb (intern gmane local))
2285 (minmax (and (boundp symb) (symbol-value symb))))
2287 ;; Bind these so that gnus-agent-load-alist doesn't change the
2288 ;; current alist (i.e. gnus-agent-article-alist)
2289 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2290 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2291 (alist (gnus-agent-load-alist group)))
2295 (caar (last alist))))
2296 (gnus-agent-set-local group (car minmax) (cdr minmax)
2297 gmane gnus-command-method local))))
2300 (defun gnus-agent-set-local (group min max &optional gmane method local)
2301 (let* ((gmane (or gmane (gnus-group-real-name group)))
2302 (gnus-command-method (or method (gnus-find-method-for-group group)))
2303 (local (or local (gnus-agent-load-local)))
2304 (symb (intern gmane local))
2305 (minmax (and (boundp symb) (symbol-value symb))))
2307 (if (cond ((and minmax
2308 (or (not (eq min (car minmax)))
2309 (not (eq max (cdr minmax))))
2318 (set symb (cons min max))
2321 (unintern symb local)))
2322 (set (intern "+dirty" local) t))))
2324 (defun gnus-agent-article-name (article group)
2325 (expand-file-name article
2326 (file-name-as-directory
2327 (gnus-agent-group-pathname group))))
2329 (defun gnus-agent-batch-confirmation (msg)
2330 "Show error message and return t."
2331 (gnus-message 1 msg)
2335 (defun gnus-agent-batch-fetch ()
2336 "Start Gnus and fetch session."
2339 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2340 (gnus-agent-fetch-session))
2343 (defun gnus-agent-fetch-session ()
2344 "Fetch all articles and headers that are eligible for fetching."
2346 (unless gnus-agent-covered-methods
2347 (error "No servers are covered by the Gnus agent"))
2348 (unless gnus-plugged
2349 (error "Can't fetch articles while Gnus is unplugged"))
2350 (let ((methods (gnus-agent-covered-methods))
2351 groups group gnus-command-method)
2354 (setq gnus-command-method (car methods))
2355 (when (and (or (gnus-server-opened gnus-command-method)
2356 (gnus-open-server gnus-command-method))
2357 (gnus-online gnus-command-method))
2358 (setq groups (gnus-groups-from-server (car methods)))
2359 (gnus-agent-with-fetch
2360 (while (setq group (pop groups))
2361 (when (<= (gnus-group-level group)
2362 gnus-agent-handle-level)
2363 (if (or debug-on-error debug-on-quit)
2364 (gnus-agent-fetch-group-1
2365 group gnus-command-method)
2367 (gnus-agent-fetch-group-1
2368 group gnus-command-method)
2370 (unless (funcall gnus-agent-confirmation-function
2371 (format "Error %s while fetching session. Should gnus continue? "
2372 (error-message-string err)))
2373 (error "Cannot fetch articles into the Gnus agent")))
2375 (gnus-agent-regenerate-group group)
2376 (unless (funcall gnus-agent-confirmation-function
2378 "%s while fetching session. Should gnus continue? "
2379 (error-message-string err)))
2381 "Cannot fetch articles into the Gnus agent")))))))))
2382 (setq methods (cdr methods)))
2383 (gnus-run-hooks 'gnus-agent-fetched-hook)
2384 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2386 (defun gnus-agent-fetch-group-1 (group method)
2388 (let ((gnus-command-method method)
2389 (gnus-newsgroup-name group)
2390 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2391 (gnus-newsgroup-headers gnus-newsgroup-headers)
2392 (gnus-newsgroup-scored gnus-newsgroup-scored)
2393 (gnus-use-cache gnus-use-cache)
2394 (gnus-summary-expunge-below gnus-summary-expunge-below)
2395 (gnus-summary-mark-below gnus-summary-mark-below)
2396 (gnus-orphan-score gnus-orphan-score)
2397 ;; Maybe some other gnus-summary local variables should also
2403 category predicate info marks score-param
2405 (unless (gnus-check-group group)
2406 (error "Can't open server for %s" group))
2409 (when (or gnus-newsgroup-active
2411 (gnus-activate-group group))
2412 (let ((marked-articles gnus-newsgroup-downloadable))
2413 ;; Identify the articles marked for download
2414 (unless gnus-newsgroup-active
2415 ;; The variable gnus-newsgroup-active was selected as I need
2416 ;; a gnus-summary local variable that is NOT bound to any
2417 ;; value (its global value should default to nil).
2418 (dolist (mark gnus-agent-download-marks)
2419 (let ((arts (cdr (assq mark (gnus-info-marks
2420 (setq info (gnus-get-info group)))))))
2422 (setq marked-articles (nconc (gnus-uncompress-range arts)
2425 (setq marked-articles (sort marked-articles '<))
2427 ;; Fetch any new articles from the server
2428 (setq articles (gnus-agent-fetch-headers group))
2430 ;; Merge new articles with marked
2431 (setq articles (sort (append marked-articles articles) '<))
2434 ;; Parse them and see which articles we want to fetch.
2435 (setq gnus-newsgroup-dependencies
2436 (or gnus-newsgroup-dependencies
2437 (make-vector (length articles) 0)))
2438 (setq gnus-newsgroup-headers
2439 (or gnus-newsgroup-headers
2440 (gnus-get-newsgroup-headers-xover articles nil nil
2442 ;; `gnus-agent-overview-buffer' may be killed for
2443 ;; timeout reason. If so, recreate it.
2444 (gnus-agent-create-buffer)
2446 ;; Figure out how to select articles in this group
2447 (setq category (gnus-group-category group))
2451 (gnus-agent-find-parameter group 'agent-predicate)))
2453 ;; If the selection predicate requires scoring, score each header
2454 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2456 (gnus-agent-find-parameter group 'agent-score-file)))
2457 ;; Translate score-param into real one
2460 ((eq score-param 'file)
2461 (setq score-param (gnus-all-score-files group)))
2462 ((stringp (car score-param)))
2464 (setq score-param (list (list score-param)))))
2466 (gnus-score-headers score-param))))
2468 (unless (and (eq predicate 'gnus-agent-false)
2469 (not marked-articles))
2470 (let ((arts (list nil)))
2471 (let ((arts-tail arts)
2472 (alist (gnus-agent-load-alist group))
2473 (marked-articles marked-articles)
2474 (gnus-newsgroup-headers gnus-newsgroup-headers))
2475 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2476 (let ((num (mail-header-number gnus-headers)))
2477 ;; Determine if this article is already in the cache
2479 (> num (caar alist)))
2480 (setq alist (cdr alist)))
2482 (unless (and (eq num (caar alist))
2485 ;; Determine if this article was marked for download.
2486 (while (and marked-articles
2487 (> num (car marked-articles)))
2488 (setq marked-articles
2489 (cdr marked-articles)))
2491 ;; When this article is marked, or selected by the
2492 ;; predicate, add it to the download list
2493 (when (or (eq num (car marked-articles))
2496 (assq num gnus-newsgroup-scored))
2497 gnus-summary-default-score))
2498 (gnus-agent-long-article
2499 (gnus-agent-find-parameter
2500 group 'agent-long-article))
2501 (gnus-agent-short-article
2502 (gnus-agent-find-parameter
2503 group 'agent-short-article))
2504 (gnus-agent-low-score
2505 (gnus-agent-find-parameter
2506 group 'agent-low-score))
2507 (gnus-agent-high-score
2508 (gnus-agent-find-parameter
2509 group 'agent-high-score))
2510 (gnus-agent-expire-days
2511 (gnus-agent-find-parameter
2512 group 'agent-days-until-old)))
2513 (funcall predicate)))
2514 (gnus-agent-append-to-list arts-tail num))))))
2516 (let (fetched-articles)
2517 ;; Fetch all selected articles
2518 (setq gnus-newsgroup-undownloaded
2519 (gnus-sorted-ndifference
2520 gnus-newsgroup-undownloaded
2521 (setq fetched-articles
2523 (gnus-agent-fetch-articles group (cdr arts))
2526 (let ((unfetched-articles
2527 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2528 (if gnus-newsgroup-active
2529 ;; Update the summary buffer
2531 (dolist (article marked-articles)
2532 (gnus-summary-set-agent-mark article t))
2533 (dolist (article fetched-articles)
2534 (when gnus-agent-mark-unread-after-downloaded
2535 (setq gnus-newsgroup-downloadable
2536 (delq article gnus-newsgroup-downloadable))
2537 (gnus-summary-mark-article
2538 article gnus-unread-mark))
2539 (when (gnus-summary-goto-subject article nil t)
2540 (gnus-summary-update-download-mark article)))
2541 (dolist (article unfetched-articles)
2542 (gnus-summary-mark-article
2543 article gnus-canceled-mark)))
2545 ;; Update the group buffer.
2547 ;; When some, or all, of the marked articles came
2548 ;; from the download mark. Remove that mark. I
2549 ;; didn't do this earlier as I only want to remove
2550 ;; the marks after the fetch is completed.
2552 (dolist (mark gnus-agent-download-marks)
2553 (when (eq mark 'download)
2555 (assq mark (gnus-info-marks
2556 (setq info (gnus-get-info group))))))
2557 (when (cdr marked-arts)
2559 (delq marked-arts (gnus-info-marks info)))
2560 (gnus-info-set-marks info marks)))))
2561 (let ((read (gnus-info-read
2562 (or info (setq info (gnus-get-info group))))))
2564 info (gnus-add-to-range read unfetched-articles)))
2566 (gnus-group-update-group group t)
2570 (concat "(gnus-group-set-info '"
2571 (gnus-prin1-to-string info)
2575 ;;; Agent Category Mode
2578 (defvar gnus-category-mode-hook nil
2579 "Hook run in `gnus-category-mode' buffers.")
2581 (defvar gnus-category-line-format " %(%20c%): %g\n"
2582 "Format of category lines.
2584 Valid specifiers include:
2585 %c Topic name (string)
2586 %g The number of groups in the topic (integer)
2588 General format specifiers can also be used. See Info node
2589 `(gnus)Formatting Variables'.")
2591 (defvar gnus-category-mode-line-format "Gnus: %%b"
2592 "The format specification for the category mode line.")
2594 (defvar gnus-agent-predicate 'false
2595 "The selection predicate used when no other source is available.")
2597 (defvar gnus-agent-short-article 100
2598 "Articles that have fewer lines than this are short.")
2600 (defvar gnus-agent-long-article 200
2601 "Articles that have more lines than this are long.")
2603 (defvar gnus-agent-low-score 0
2604 "Articles that have a score lower than this have a low score.")
2606 (defvar gnus-agent-high-score 0
2607 "Articles that have a score higher than this have a high score.")
2610 ;;; Internal variables.
2612 (defvar gnus-category-buffer "*Agent Category*")
2614 (defvar gnus-category-line-format-alist
2615 `((?c gnus-tmp-name ?s)
2616 (?g gnus-tmp-groups ?d)))
2618 (defvar gnus-category-mode-line-format-alist
2619 `((?u user-defined ?s)))
2621 (defvar gnus-category-line-format-spec nil)
2622 (defvar gnus-category-mode-line-format-spec nil)
2624 (defvar gnus-category-mode-map nil)
2625 (put 'gnus-category-mode 'mode-class 'special)
2627 (unless gnus-category-mode-map
2628 (setq gnus-category-mode-map (make-sparse-keymap))
2629 (suppress-keymap gnus-category-mode-map)
2631 (gnus-define-keys gnus-category-mode-map
2632 "q" gnus-category-exit
2633 "k" gnus-category-kill
2634 "c" gnus-category-copy
2635 "a" gnus-category-add
2636 "e" gnus-agent-customize-category
2637 "p" gnus-category-edit-predicate
2638 "g" gnus-category-edit-groups
2639 "s" gnus-category-edit-score
2640 "l" gnus-category-list
2642 "\C-c\C-i" gnus-info-find-node
2643 "\C-c\C-b" gnus-bug))
2645 (defvar gnus-category-menu-hook nil
2646 "*Hook run after the creation of the menu.")
2648 (defun gnus-category-make-menu-bar ()
2649 (gnus-turn-off-edit-menu 'category)
2650 (unless (boundp 'gnus-category-menu)
2652 gnus-category-menu gnus-category-mode-map ""
2654 ["Add" gnus-category-add t]
2655 ["Kill" gnus-category-kill t]
2656 ["Copy" gnus-category-copy t]
2657 ["Edit category" gnus-agent-customize-category t]
2658 ["Edit predicate" gnus-category-edit-predicate t]
2659 ["Edit score" gnus-category-edit-score t]
2660 ["Edit groups" gnus-category-edit-groups t]
2661 ["Exit" gnus-category-exit t]))
2663 (gnus-run-hooks 'gnus-category-menu-hook)))
2665 (defun gnus-category-mode ()
2666 "Major mode for listing and editing agent categories.
2668 All normal editing commands are switched off.
2669 \\<gnus-category-mode-map>
2670 For more in-depth information on this mode, read the manual
2671 \(`\\[gnus-info-find-node]').
2673 The following commands are available:
2675 \\{gnus-category-mode-map}"
2677 (when (gnus-visual-p 'category-menu 'menu)
2678 (gnus-category-make-menu-bar))
2679 (kill-all-local-variables)
2680 (gnus-simplify-mode-line)
2681 (setq major-mode 'gnus-category-mode)
2682 (setq mode-name "Category")
2683 (gnus-set-default-directory)
2684 (setq mode-line-process nil)
2685 (use-local-map gnus-category-mode-map)
2686 (buffer-disable-undo)
2687 (setq truncate-lines t)
2688 (setq buffer-read-only t)
2689 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2691 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2693 (defun gnus-category-insert-line (category)
2694 (let* ((gnus-tmp-name (format "%s" (car category)))
2695 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2697 (gnus-add-text-properties
2701 (eval gnus-category-line-format-spec))
2702 (list 'gnus-category gnus-tmp-name))))
2704 (defun gnus-enter-category-buffer ()
2705 "Go to the Category buffer."
2707 (gnus-category-setup-buffer)
2708 (gnus-configure-windows 'category)
2709 (gnus-category-prepare))
2711 (defun gnus-category-setup-buffer ()
2712 (unless (get-buffer gnus-category-buffer)
2714 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2715 (gnus-category-mode))))
2717 (defun gnus-category-prepare ()
2718 (gnus-set-format 'category-mode)
2719 (gnus-set-format 'category t)
2720 (let ((alist gnus-category-alist)
2721 (buffer-read-only nil))
2724 (gnus-category-insert-line (pop alist)))
2725 (goto-char (point-min))
2726 (gnus-category-position-point)))
2728 (defun gnus-category-name ()
2729 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2730 (error "No category on the current line")))
2732 (defun gnus-category-read ()
2733 "Read the category alist."
2734 (setq gnus-category-alist
2738 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2739 (goto-char (point-min))
2740 ;; This code isn't temp, it will be needed so long as
2741 ;; anyone may be migrating from an older version.
2743 ;; Once we're certain that people will not revert to an
2744 ;; earlier version, we can take out the old-list code in
2745 ;; gnus-category-write.
2746 (let* ((old-list (read (current-buffer)))
2747 (new-list (ignore-errors (read (current-buffer)))))
2750 ;; Convert from a positional list to an alist.
2760 '(agent-predicate agent-score-file agent-groups))))
2763 (list (gnus-agent-cat-make 'default 'short)))))
2765 (defun gnus-category-write ()
2766 "Write the category alist."
2767 (setq gnus-category-predicate-cache nil
2768 gnus-category-group-cache nil)
2769 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2770 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2771 ;; This prin1 is temporary. It exists so that people can revert
2772 ;; to an earlier version of gnus-agent.
2773 (prin1 (mapcar (lambda (c)
2775 (cdr (assoc 'agent-predicate c))
2776 (cdr (assoc 'agent-score-file c))
2777 (cdr (assoc 'agent-groups c))))
2778 gnus-category-alist)
2781 (prin1 gnus-category-alist (current-buffer))))
2783 (defun gnus-category-edit-predicate (category)
2784 "Edit the predicate for CATEGORY."
2785 (interactive (list (gnus-category-name)))
2786 (let ((info (assq category gnus-category-alist)))
2788 (gnus-agent-cat-predicate info)
2789 (format "Editing the select predicate for category %s" category)
2790 `(lambda (predicate)
2791 ;; Avoid run-time execution of setf form
2792 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2794 ;; use its expansion instead:
2795 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2796 'agent-predicate predicate)
2798 (gnus-category-write)
2799 (gnus-category-list)))))
2801 (defun gnus-category-edit-score (category)
2802 "Edit the score expression for CATEGORY."
2803 (interactive (list (gnus-category-name)))
2804 (let ((info (assq category gnus-category-alist)))
2806 (gnus-agent-cat-score-file info)
2807 (format "Editing the score expression for category %s" category)
2808 `(lambda (score-file)
2809 ;; Avoid run-time execution of setf form
2810 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2812 ;; use its expansion instead:
2813 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2814 'agent-score-file score-file)
2816 (gnus-category-write)
2817 (gnus-category-list)))))
2819 (defun gnus-category-edit-groups (category)
2820 "Edit the group list for CATEGORY."
2821 (interactive (list (gnus-category-name)))
2822 (let ((info (assq category gnus-category-alist)))
2824 (gnus-agent-cat-groups info)
2825 (format "Editing the group list for category %s" category)
2827 ;; Avoid run-time execution of setf form
2828 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2830 ;; use its expansion instead:
2831 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2834 (gnus-category-write)
2835 (gnus-category-list)))))
2837 (defun gnus-category-kill (category)
2838 "Kill the current category."
2839 (interactive (list (gnus-category-name)))
2840 (let ((info (assq category gnus-category-alist))
2841 (buffer-read-only nil))
2843 (setq gnus-category-alist (delq info gnus-category-alist))
2844 (gnus-category-write)))
2846 (defun gnus-category-copy (category to)
2847 "Copy the current category."
2848 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2849 (let ((info (assq category gnus-category-alist)))
2850 (push (let ((newcat (gnus-copy-sequence info)))
2851 (setf (gnus-agent-cat-name newcat) to)
2852 (setf (gnus-agent-cat-groups newcat) nil)
2854 gnus-category-alist)
2855 (gnus-category-write)
2856 (gnus-category-list)))
2858 (defun gnus-category-add (category)
2859 "Create a new category."
2860 (interactive "SCategory name: ")
2861 (when (assq category gnus-category-alist)
2862 (error "Category %s already exists" category))
2863 (push (gnus-agent-cat-make category)
2864 gnus-category-alist)
2865 (gnus-category-write)
2866 (gnus-category-list))
2868 (defun gnus-category-list ()
2869 "List all categories."
2871 (gnus-category-prepare))
2873 (defun gnus-category-exit ()
2874 "Return to the group buffer."
2876 (kill-buffer (current-buffer))
2877 (gnus-configure-windows 'group t))
2879 ;; To avoid having 8-bit characters in the source file.
2880 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2882 (defvar gnus-category-predicate-alist
2883 '((spam . gnus-agent-spam-p)
2884 (short . gnus-agent-short-p)
2885 (long . gnus-agent-long-p)
2886 (low . gnus-agent-low-scored-p)
2887 (high . gnus-agent-high-scored-p)
2888 (read . gnus-agent-read-p)
2889 (true . gnus-agent-true)
2890 (false . gnus-agent-false))
2891 "Mapping from short score predicate symbols to predicate functions.")
2893 (defun gnus-agent-spam-p ()
2894 "Say whether an article is spam or not."
2895 (unless gnus-agent-spam-hashtb
2896 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2897 (if (not (equal (mail-header-references gnus-headers) ""))
2899 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2901 (gnus-gethash string gnus-agent-spam-hashtb)
2902 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2904 (defun gnus-agent-short-p ()
2905 "Say whether an article is short or not."
2906 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2908 (defun gnus-agent-long-p ()
2909 "Say whether an article is long or not."
2910 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2912 (defun gnus-agent-low-scored-p ()
2913 "Say whether an article has a low score or not."
2914 (< gnus-score gnus-agent-low-score))
2916 (defun gnus-agent-high-scored-p ()
2917 "Say whether an article has a high score or not."
2918 (> gnus-score gnus-agent-high-score))
2920 (defun gnus-agent-read-p ()
2921 "Say whether an article is read or not."
2922 (gnus-member-of-range (mail-header-number gnus-headers)
2923 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2925 (defun gnus-category-make-function (predicate)
2926 "Make a function from PREDICATE."
2927 (let ((func (gnus-category-make-function-1 predicate)))
2928 (if (and (= (length func) 1)
2929 (symbolp (car func)))
2931 (gnus-byte-compile `(lambda () ,func)))))
2933 (defun gnus-agent-true ()
2937 (defun gnus-agent-false ()
2941 (defun gnus-category-make-function-1 (predicate)
2942 "Make a function from PREDICATE."
2944 ;; Functions are just returned as is.
2945 ((or (symbolp predicate)
2946 (functionp predicate))
2947 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2949 ;; More complex predicate.
2952 ((memq (car predicate) '(& and))
2954 ((memq (car predicate) '(| or))
2956 ((memq (car predicate) gnus-category-not)
2958 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2960 (error "Unknown predicate type: %s" predicate))))
2962 (defun gnus-get-predicate (predicate)
2963 "Return the function implementing PREDICATE."
2964 (or (cdr (assoc predicate gnus-category-predicate-cache))
2965 (let ((func (gnus-category-make-function predicate)))
2966 (setq gnus-category-predicate-cache
2967 (nconc gnus-category-predicate-cache
2968 (list (cons predicate func))))
2971 (defun gnus-predicate-implies-unread (predicate)
2972 "Say whether PREDICATE implies unread articles only.
2973 It is okay to miss some cases, but there must be no false positives.
2974 That is, if this predicate returns true, then indeed the predicate must
2975 return only unread articles."
2976 (eq t (gnus-function-implies-unread-1
2977 (gnus-category-make-function-1 predicate))))
2979 (defun gnus-function-implies-unread-1 (function)
2980 "Recursively evaluate a predicate function to determine whether it can select
2981 any read articles. Returns t if the function is known to never
2982 return read articles, nil when it is known to always return read
2983 articles, and t_nil when the function may return both read and unread
2985 (let ((func (car function))
2986 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2987 (cond ((eq func 'and)
2988 (cond ((memq t args) ; if any argument returns only unread articles
2989 ;; then that argument constrains the result to only unread articles.
2991 ((memq 't_nil args) ; if any argument is indeterminate
2992 ;; then the result is indeterminate
2995 (cond ((memq nil args) ; if any argument returns read articles
2996 ;; then that argument ensures that the results includes read articles.
2998 ((memq 't_nil args) ; if any argument is indeterminate
2999 ;; then that argument ensures that the results are indeterminate
3001 (t ; if all arguments return only unread articles
3002 ;; then the result returns only unread articles
3005 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
3006 ; then the result is indeterminate
3009 ; toggle the result to be the opposite of the argument
3011 ((eq func 'gnus-agent-read-p)
3012 nil) ; The read predicate NEVER returns unread articles
3013 ((eq func 'gnus-agent-false)
3014 t) ; The false predicate returns t as the empty set excludes all read articles
3015 ((eq func 'gnus-agent-true)
3016 nil) ; The true predicate ALWAYS returns read articles
3017 ((catch 'found-match
3018 (let ((alist gnus-category-predicate-alist))
3020 (if (eq func (cdar alist))
3021 (throw 'found-match t)
3022 (setq alist (cdr alist))))))
3023 't_nil) ; All other predicates return read and unread articles
3025 (error "Unknown predicate function: %s" function)))))
3027 (defun gnus-group-category (group)
3028 "Return the category GROUP belongs to."
3029 (unless gnus-category-group-cache
3030 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
3031 (let ((cs gnus-category-alist)
3033 (while (setq cat (pop cs))
3034 (setq groups (gnus-agent-cat-groups cat))
3036 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
3037 (or (gnus-gethash group gnus-category-group-cache)
3038 (assq 'default gnus-category-alist)))
3040 (defun gnus-agent-expire-group (group &optional articles force)
3041 "Expire all old articles in GROUP.
3042 If you want to force expiring of certain articles, this function can
3043 take ARTICLES, and FORCE parameters as well.
3045 The articles on which the expiration process runs are selected as follows:
3046 if ARTICLES is null, all read and unmarked articles.
3047 if ARTICLES is t, all articles.
3048 if ARTICLES is a list, just those articles.
3049 FORCE is equivalent to setting the expiration predicates to true."
3051 (list (let ((def (or (gnus-group-group-name)
3052 gnus-newsgroup-name)))
3053 (let ((select (read-string (if def
3054 (concat "Group Name ("
3057 (if (and (equal "" select)
3063 (gnus-agent-expire articles group force)
3064 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
3065 ;; expiration statistics of this single group
3066 (gnus-agent-expire-stats (list 0 0 0.0)))
3067 (if (or (not (eq articles t))
3069 (concat "Are you sure that you want to "
3070 "expire all articles in " group "? ")))
3071 (let ((gnus-command-method (gnus-find-method-for-group group))
3072 (overview (gnus-get-buffer-create " *expire overview*"))
3075 (let ((active-file (gnus-agent-lib-file "active")))
3076 (when (file-exists-p active-file)
3078 (nnheader-insert-file-contents active-file)
3079 (gnus-active-to-gnus-format
3081 (setq orig (gnus-make-hashtable
3082 (count-lines (point-min) (point-max))))))
3084 (gnus-agent-expire-group-1
3085 group overview (gnus-gethash-safe group orig)
3087 (kill-buffer overview))))
3088 (gnus-message 4 (gnus-agent-expire-done-message)))))
3090 (defun gnus-agent-expire-group-1 (group overview active articles force)
3091 ;; Internal function - requires caller to have set
3092 ;; gnus-command-method, initialized overview buffer, and to have
3093 ;; provided a non-nil active
3095 (let ((dir (gnus-agent-group-pathname group)))
3096 (gnus-agent-with-refreshed-group
3098 (when (boundp 'gnus-agent-expire-current-dirs)
3099 (set 'gnus-agent-expire-current-dirs
3101 (symbol-value 'gnus-agent-expire-current-dirs))))
3103 (if (and (not force)
3104 (eq 'DISABLE (gnus-agent-find-parameter group
3105 'agent-enable-expiration)))
3106 (gnus-message 5 "Expiry skipping over %s" group)
3107 (gnus-message 5 "Expiring articles in %s" group)
3108 (gnus-agent-load-alist group)
3109 (let* ((bytes-freed 0)
3110 (size-files-deleted 0.0)
3112 (nov-entries-deleted 0)
3113 (info (gnus-get-info group))
3114 (alist gnus-agent-article-alist)
3115 (day (- (time-to-days (current-time))
3116 (gnus-agent-find-parameter group 'agent-days-until-old)))
3117 (specials (if (and alist
3119 ;; This could be a bit of a problem. I need to
3120 ;; keep the last article to avoid refetching
3121 ;; headers when using nntp in the backend. At
3122 ;; the same time, if someone uses a backend
3123 ;; that supports article moving then I may have
3124 ;; to remove the last article to complete the
3125 ;; move. Right now, I'm going to assume that
3126 ;; FORCE overrides specials.
3127 (list (caar (last alist)))))
3128 (unreads ;; Articles that are excluded from the
3129 ;; expiration process
3130 (cond (gnus-agent-expire-all
3131 ;; All articles are marked read by global decree
3134 ;; All articles are marked read by function
3138 ;; Unread articles are marked protected from
3139 ;; expiration Don't call
3140 ;; gnus-list-of-unread-articles as it returns
3141 ;; articles that have not been fetched into the
3144 (gnus-agent-unread-articles group)))
3146 ;; All articles EXCEPT those named by the caller
3147 ;; are protected from expiration
3148 (gnus-sorted-difference
3149 (gnus-uncompress-range
3151 (caar (last alist))))
3152 (sort articles '<)))))
3153 (marked ;; More articles that are excluded from the
3154 ;; expiration process
3155 (cond (gnus-agent-expire-all
3156 ;; All articles are unmarked by global decree
3159 ;; All articles are unmarked by function
3163 ;; All articles may as well be unmarked as the
3164 ;; unreads list already names the articles we are
3168 ;; Ticked and/or dormant articles are excluded
3171 (gnus-uncompress-range
3172 (cdr (assq 'tick (gnus-info-marks info))))
3173 (gnus-uncompress-range
3175 (gnus-info-marks info))))))))
3176 (nov-file (concat dir ".overview"))
3182 ;; The normal article alist contains elements that look like
3183 ;; (article# . fetch_date) I need to combine other
3184 ;; information with this list. For example, a flag indicating
3185 ;; that a particular article MUST BE KEPT. To do this, I'm
3186 ;; going to transform the elements to look like (article#
3187 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3188 ;; the process to generate the expired article alist.
3190 ;; Convert the alist elements to (article# fetch_date nil
3192 (setq dlist (mapcar (lambda (e)
3193 (list (car e) (cdr e) nil nil)) alist))
3195 ;; Convert the keep lists to elements that look like (article#
3196 ;; nil keep_flag nil) then append it to the expanded dlist
3197 ;; These statements are sorted by ascending precidence of the
3199 (setq dlist (nconc dlist
3201 (list e nil 'unread nil))
3203 (setq dlist (nconc dlist
3205 (list e nil 'marked nil))
3207 (setq dlist (nconc dlist
3209 (list e nil 'special nil))
3212 (set-buffer overview)
3214 (buffer-disable-undo)
3215 (when (file-exists-p nov-file)
3216 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3217 (nnheader-insert-file-contents nov-file)
3218 (goto-char (point-min))
3221 (while (< (setq p (point)) (point-max))
3223 ;; If I successfully read an integer (the plus zero
3224 ;; ensures a numeric type), append the position
3226 (push (list (+ 0 (read (current-buffer))) nil nil
3230 (gnus-message 1 "gnus-agent-expire: read error \
3231 occurred when reading expression at %s in %s. Skipping to next \
3232 line." (point) nov-file)))
3233 ;; Whether I succeeded, or failed, it doesn't matter.
3234 ;; Move to the next line then try again.
3238 7 "gnus-agent-expire: Loading overview... Done"))
3239 (set-buffer-modified-p nil)
3241 ;; At this point, all of the information is in dlist. The
3242 ;; only problem is that much of it is spread across multiple
3243 ;; entries. Sort then MERGE!!
3244 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3245 ;; If two entries have the same article-number then sort by
3246 ;; ascending keep_flag.
3253 (cond ((< (nth 0 a) (nth 0 b))
3255 ((> (nth 0 a) (nth 0 b))
3258 (let ((a (or (symbol-value (nth 2 a))
3260 (b (or (symbol-value (nth 2 b))
3263 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3264 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3265 (let ((dlist dlist))
3266 (while (cdr dlist) ; I'm not at the end-of-list
3267 (if (eq (caar dlist) (caadr dlist))
3268 (let ((first (cdr (car dlist)))
3269 (secnd (cdr (cadr dlist))))
3270 (setcar first (or (car first)
3271 (car secnd))) ; fetch_date
3272 (setq first (cdr first)
3274 (setcar first (or (car first)
3275 (car secnd))) ; Keep_flag
3276 (setq first (cdr first)
3278 (setcar first (or (car first)
3279 (car secnd))) ; NOV_entry_position
3281 (setcdr dlist (cddr dlist)))
3282 (setq dlist (cdr dlist)))))
3284 ;; Check the order of the entry positions. They should be in
3285 ;; ascending order. If they aren't, the positions must be
3286 ;; converted to markers.
3287 (when (let ((dlist dlist)
3291 (if (setq pos (nth 3 (pop dlist)))
3292 (if (< pos prev-pos)
3293 (throw 'sort-results 'unsorted)
3294 (setq prev-pos pos)))))
3295 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3296 (mapcar (lambda (entry)
3297 (let ((pos (nth 3 entry)))
3300 (set-marker (make-marker)
3304 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3306 (let* ((len (float (length dlist)))
3313 (let ((new-completed (truncate (* 100.0
3314 (/ (setq cnt (1+ cnt))
3317 (when (> new-completed completed)
3318 (setq completed new-completed)
3319 (gnus-message 7 "%3d%% completed..." completed)))
3320 (let* ((entry (car dlist))
3321 (article-number (nth 0 entry))
3322 (fetch-date (nth 1 entry))
3323 (keep (nth 2 entry))
3324 (marker (nth 3 entry)))
3327 ;; Kept articles are unread, marked, or special.
3329 (gnus-agent-message 10
3330 "gnus-agent-expire: %s:%d: Kept %s article%s."
3331 group article-number keep (if fetch-date " and file" ""))
3333 (unless (file-exists-p
3334 (concat dir (number-to-string
3336 (setf (nth 1 entry) nil)
3337 (gnus-agent-message 3 "gnus-agent-expire cleared \
3338 download flag on %s:%d as the cached article file is missing."
3339 group (caar dlist)))
3341 (gnus-message 1 "gnus-agent-expire detected a \
3342 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3343 (gnus-agent-append-to-list
3345 (cons article-number fetch-date)))
3347 ;; The following articles are READ, UNMARKED, and
3348 ;; ORDINARY. See if they can be EXPIRED!!!
3351 ((not (integerp fetch-date))
3352 'read) ;; never fetched article (may expire
3354 ((not (file-exists-p
3355 (concat dir (number-to-string
3357 (setf (nth 1 entry) nil)
3358 'externally-expired) ;; Can't find the cached
3359 ;; article. Handle case
3360 ;; as though this article
3361 ;; was never fetched.
3363 ;; We now have the arrival day, so we see
3364 ;; whether it's old enough to be expired.
3370 ;; I found some reason to expire this entry.
3372 (let ((actions nil))
3373 (when (memq type '(forced expired))
3374 (ignore-errors ; Just being paranoid.
3375 (let* ((file-name (nnheader-concat dir (number-to-string
3377 (size (float (nth 7 (file-attributes file-name)))))
3378 (incf bytes-freed size)
3379 (incf size-files-deleted size)
3380 (incf files-deleted)
3381 (delete-file file-name))
3382 (push "expired cached article" actions))
3383 (setf (nth 1 entry) nil)
3387 (push "NOV entry removed" actions)
3389 (goto-char (if (markerp marker)
3391 (- marker position-offset)))
3393 (incf nov-entries-deleted)
3395 (let* ((from (point-at-bol))
3396 (to (progn (forward-line 1) (point)))
3397 (freed (- to from)))
3398 (incf bytes-freed freed)
3399 (incf position-offset freed)
3400 (delete-region from to)))
3402 ;; If considering all articles is set, I can only
3403 ;; expire article IDs that are no longer in the
3404 ;; active range (That is, articles that preceed the
3405 ;; first article in the new alist).
3406 (if (and gnus-agent-consider-all-articles
3407 (>= article-number (car active)))
3408 ;; I have to keep this ID in the alist
3409 (gnus-agent-append-to-list
3410 tail-alist (cons article-number fetch-date))
3411 (push (format "Removed %s article number from \
3412 article alist" type) actions))
3415 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3416 group article-number
3417 (mapconcat 'identity actions ", ")))))
3420 10 "gnus-agent-expire: %s:%d: Article kept as \
3421 expiration tests failed." group article-number)
3422 (gnus-agent-append-to-list
3423 tail-alist (cons article-number fetch-date)))
3426 ;; Remove markers as I intend to reuse this buffer again.
3429 (set-marker marker nil))
3431 (setq dlist (cdr dlist))))
3433 (setq alist (cdr alist))
3435 (let ((inhibit-quit t))
3436 (unless (equal alist gnus-agent-article-alist)
3437 (setq gnus-agent-article-alist alist)
3438 (gnus-agent-save-alist group))
3440 (when (buffer-modified-p)
3441 (let ((coding-system-for-write
3442 gnus-agent-file-coding-system))
3443 (gnus-make-directory dir)
3444 (write-region (point-min) (point-max) nov-file nil
3446 ;; clear the modified flag as that I'm not confused by
3447 ;; its status on the next pass through this routine.
3448 (set-buffer-modified-p nil)
3449 (gnus-agent-update-view-total-fetched-for group t)))
3451 (when (eq articles t)
3452 (gnus-summary-update-info))))
3454 (when (boundp 'gnus-agent-expire-stats)
3455 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3456 (incf (nth 2 stats) bytes-freed)
3457 (incf (nth 1 stats) files-deleted)
3458 (incf (nth 0 stats) nov-entries-deleted)))
3460 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3462 (defun gnus-agent-expire (&optional articles group force)
3463 "Expire all old articles.
3464 If you want to force expiring of certain articles, this function can
3465 take ARTICLES, GROUP and FORCE parameters as well.
3467 The articles on which the expiration process runs are selected as follows:
3468 if ARTICLES is null, all read and unmarked articles.
3469 if ARTICLES is t, all articles.
3470 if ARTICLES is a list, just those articles.
3471 Setting GROUP will limit expiration to that group.
3472 FORCE is equivalent to setting the expiration predicates to true."
3476 (gnus-agent-expire-group group articles force)
3477 (if (or (not (eq articles t))
3478 (yes-or-no-p "Are you sure that you want to expire all \
3479 articles in every agentized group? "))
3480 (let ((methods (gnus-agent-covered-methods))
3481 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3482 ;; of agent directories.
3483 (gnus-agent-expire-current-dirs nil)
3484 ;; Bind gnus-agent-expire-stats to enable tracking of
3485 ;; expiration statistics across all groups
3486 (gnus-agent-expire-stats (list 0 0 0.0))
3487 gnus-command-method overview orig)
3488 (setq overview (gnus-get-buffer-create " *expire overview*"))
3490 (while (setq gnus-command-method (pop methods))
3491 (let ((active-file (gnus-agent-lib-file "active")))
3492 (when (file-exists-p active-file)
3494 (nnheader-insert-file-contents active-file)
3495 (gnus-active-to-gnus-format
3497 (setq orig (gnus-make-hashtable
3498 (count-lines (point-min) (point-max))))))
3499 (dolist (expiring-group (gnus-groups-from-server
3500 gnus-command-method))
3502 (gnus-gethash-safe expiring-group orig)))
3506 (gnus-agent-expire-group-1
3507 expiring-group overview active articles force))))))))
3508 (kill-buffer overview))
3509 (gnus-agent-expire-unagentized-dirs)
3510 (gnus-message 4 (gnus-agent-expire-done-message))))))
3512 (defun gnus-agent-expire-done-message ()
3513 (if (and (> gnus-verbose 4)
3514 (boundp 'gnus-agent-expire-stats))
3515 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3516 (size (nth 2 stats))
3517 (units '(B KB MB GB)))
3518 (while (and (> size 1024.0)
3520 (setq size (/ size 1024.0)
3523 (format "Expiry recovered %d NOV entries, deleted %d files,\
3530 (defun gnus-agent-expire-unagentized-dirs ()
3531 (when (and gnus-agent-expire-unagentized-dirs
3532 (boundp 'gnus-agent-expire-current-dirs))
3533 (let* ((keep (gnus-make-hashtable))
3534 ;; Formally bind gnus-agent-expire-current-dirs so that the
3535 ;; compiler will not complain about free references.
3536 (gnus-agent-expire-current-dirs
3537 (symbol-value 'gnus-agent-expire-current-dirs))
3540 (gnus-sethash gnus-agent-directory t keep)
3541 (while gnus-agent-expire-current-dirs
3542 (setq dir (pop gnus-agent-expire-current-dirs))
3543 (when (and (stringp dir)
3544 (file-directory-p dir))
3545 (while (not (gnus-gethash dir keep))
3546 (gnus-sethash dir t keep)
3547 (setq dir (file-name-directory (directory-file-name dir))))))
3554 "Given a directory, check it and its subdirectories for
3555 membership in the keep hash. If it isn't found, add
3557 (let ((files (directory-files d))
3559 (while (setq file (pop files))
3560 (cond ((equal file ".") ; Ignore self
3562 ((equal file "..") ; Ignore parent
3564 ((equal file ".overview")
3565 ;; Directory must contain .overview to be
3566 ;; agent's cache of a group.
3567 (let ((d (file-name-as-directory d))
3569 ;; Search ancestor's for last directory NOT
3570 ;; found in keep hash.
3571 (while (not (gnus-gethash
3572 (setq d (file-name-directory d)) keep))
3574 d (directory-file-name d)))
3575 ;; if ANY ancestor was NOT in keep hash and
3576 ;; it it's already in to-remove, add it to
3579 (not (member r to-remove)))
3580 (push r to-remove))))
3581 ((file-directory-p (setq file (nnheader-concat d file)))
3582 (funcall checker file)))))))))
3583 (funcall checker (expand-file-name gnus-agent-directory))
3585 (when (and to-remove
3586 (or gnus-expert-user
3588 "gnus-agent-expire has identified local directories that are\
3589 not currently required by any agentized group. Do you wish to consider\
3592 (let ((dir (pop to-remove)))
3593 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3594 (let* (delete-recursive
3599 (if (file-directory-p f-or-d)
3601 (delete-directory f-or-d)
3604 (or (member f '("." ".."))
3605 (funcall delete-recursive
3608 (directory-files f-or-d))
3609 (delete-directory f-or-d)))
3610 (delete-file f-or-d)))))))
3611 (funcall delete-recursive dir))))))))))
3614 (defun gnus-agent-batch ()
3615 "Start Gnus, send queue and fetch session."
3617 (let ((init-file-user "")
3618 (gnus-always-read-dribble-file t))
3620 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3621 (gnus-group-send-queue)
3622 (gnus-agent-fetch-session)))
3624 (defun gnus-agent-unread-articles (group)
3625 (let* ((read (gnus-info-read (gnus-get-info group)))
3626 (known (gnus-agent-load-alist group))
3628 (tail-unread unread))
3629 (while (and known read)
3630 (let ((candidate (car (pop known))))
3631 (while (let* ((range (car read))
3632 (min (if (numberp range) range (car range)))
3633 (max (if (numberp range) range (cdr range))))
3634 (cond ((or (not min)
3636 (gnus-agent-append-to-list tail-unread candidate)
3639 (setq read (cdr read))
3640 ;; return t so that I always loop one more
3641 ;; time. If I just iterated off the end of
3642 ;; read, min will become nil and the current
3643 ;; candidate will be added to the unread list.
3646 (gnus-agent-append-to-list tail-unread (car (pop known))))
3649 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3650 "Restrict ARTICLES to numbers already fetched.
3651 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3652 that have already been fetched.
3653 If CACHED-HEADER is nil, articles are only excluded if the article itself
3656 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3657 ;; 'car gnus-agent-article-alist))
3659 ;; Functionally, I don't need to construct a temp list using mapcar.
3661 (if (and (or gnus-agent-cache (not gnus-plugged))
3662 (gnus-agent-load-alist group))
3663 (let* ((ref gnus-agent-article-alist)
3665 (uncached (list nil))
3666 (tail-uncached uncached))
3667 (while (and ref arts)
3668 (let ((v1 (car arts))
3670 (cond ((< v1 v2) ; v1 does not appear in the reference list
3671 (gnus-agent-append-to-list tail-uncached v1)
3672 (setq arts (cdr arts)))
3674 (unless (or cached-header (cdar ref)) ; v1 is already cached
3675 (gnus-agent-append-to-list tail-uncached v1))
3676 (setq arts (cdr arts))
3677 (setq ref (cdr ref)))
3678 (t ; reference article (v2) preceeds the list being filtered
3679 (setq ref (cdr ref))))))
3681 (gnus-agent-append-to-list tail-uncached (pop arts)))
3683 ;; if gnus-agent-load-alist fails, no articles are cached.
3686 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3688 (gnus-agent-create-buffer)
3689 (let ((gnus-decode-encoded-word-function 'identity)
3690 (file (gnus-agent-article-name ".overview" group))
3691 cached-articles uncached-articles)
3692 (gnus-make-directory (nnheader-translate-file-chars
3693 (file-name-directory file) t))
3695 ;; Populate temp buffer with known headers
3696 (when (file-exists-p file)
3697 (with-current-buffer gnus-agent-overview-buffer
3699 (let ((nnheader-file-coding-system
3700 gnus-agent-file-coding-system))
3701 (nnheader-insert-nov-file file (car articles)))))
3703 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3706 ;; Populate nntp-server-buffer with uncached headers
3707 (set-buffer nntp-server-buffer)
3709 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3710 (gnus-retrieve-headers
3711 uncached-articles group fetch-old))))
3712 (nnvirtual-convert-headers))
3713 ((eq 'nntp (car gnus-current-select-method))
3714 ;; The author of gnus-get-newsgroup-headers-xover
3715 ;; reports that the XOVER command is commonly
3716 ;; unreliable. The problem is that recently
3717 ;; posted articles may not be entered into the
3718 ;; NOV database in time to respond to my XOVER
3721 ;; I'm going to use his assumption that the NOV
3722 ;; database is updated in order of ascending
3723 ;; article ID. Therefore, a response containing
3724 ;; article ID N implies that all articles from 1
3725 ;; to N-1 are up-to-date. Therefore, missing
3726 ;; articles in that range have expired.
3728 (set-buffer nntp-server-buffer)
3729 (let* ((fetched-articles (list nil))
3730 (tail-fetched-articles fetched-articles)
3731 (min (cond ((numberp fetch-old)
3732 (max 1 (- (car articles) fetch-old)))
3737 (max (car (last articles))))
3739 ;; Get the list of articles that were fetched
3740 (goto-char (point-min))
3741 (let ((pm (point-max))
3743 (while (< (point) pm)
3744 (when (setq art (gnus-agent-read-article-number))
3745 (gnus-agent-append-to-list tail-fetched-articles art))
3748 ;; Clip this list to the headers that will
3749 ;; actually be returned
3750 (setq fetched-articles (gnus-list-range-intersection
3751 (cdr fetched-articles)
3754 ;; Clip the uncached articles list to exclude
3755 ;; IDs after the last FETCHED header. The
3756 ;; excluded IDs may be fetchable using HEAD.
3757 (if (car tail-fetched-articles)
3758 (setq uncached-articles
3759 (gnus-list-range-intersection
3761 (cons (car uncached-articles)
3762 (car tail-fetched-articles)))))
3764 ;; Create the list of articles that were
3765 ;; "successfully" fetched. Success, in this
3766 ;; case, means that the ID should not be
3767 ;; fetched again. In the case of an expired
3768 ;; article, the header will not be fetched.
3769 (setq uncached-articles
3770 (gnus-sorted-nunion fetched-articles
3774 ;; Erase the temp buffer
3775 (set-buffer gnus-agent-overview-buffer)
3778 ;; Copy the nntp-server-buffer to the temp buffer
3779 (set-buffer nntp-server-buffer)
3780 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3782 ;; Merge the temp buffer with the known headers (found on
3783 ;; disk in FILE) into the nntp-server-buffer
3784 (when uncached-articles
3785 (gnus-agent-braid-nov group uncached-articles file))
3787 ;; Save the new set of known headers to FILE
3788 (set-buffer nntp-server-buffer)
3789 (let ((coding-system-for-write
3790 gnus-agent-file-coding-system))
3791 (gnus-agent-check-overview-buffer)
3792 (write-region (point-min) (point-max) file nil 'silent))
3794 (gnus-agent-update-view-total-fetched-for group t)
3796 ;; Update the group's article alist to include the newly
3797 ;; fetched articles.
3798 (gnus-agent-load-alist group)
3799 (gnus-agent-save-alist group uncached-articles nil)
3802 ;; Copy the temp buffer to the nntp-server-buffer
3803 (set-buffer nntp-server-buffer)
3805 (insert-buffer-substring gnus-agent-overview-buffer)))
3808 (not (numberp fetch-old)))
3809 t ; Don't remove anything.
3810 (nnheader-nov-delete-outside-range
3811 (if fetch-old (max 1 (- (car articles) fetch-old))
3813 (car (last articles)))
3818 (defun gnus-agent-request-article (article group)
3819 "Retrieve ARTICLE in GROUP from the agent cache."
3820 (when (and gnus-agent
3821 (or gnus-agent-cache
3824 (let* ((gnus-command-method (gnus-find-method-for-group group))
3825 (file (gnus-agent-article-name (number-to-string article) group))
3826 (buffer-read-only nil))
3827 (when (and (file-exists-p file)
3828 (> (nth 7 (file-attributes file)) 0))
3830 (gnus-kill-all-overlays)
3831 (let ((coding-system-for-read gnus-cache-coding-system))
3832 (insert-file-contents file))
3835 (defun gnus-agent-regenerate-group (group &optional reread)
3837 If REREAD is t, all articles in the .overview are marked as unread.
3838 If REREAD is a list, the specified articles will be marked as unread.
3839 In addition, their NOV entries in .overview will be refreshed using
3840 the articles' current headers.
3841 If REREAD is not nil, downloaded articles are marked as unread."
3843 (list (let ((def (or (gnus-group-group-name)
3844 gnus-newsgroup-name)))
3845 (let ((select (read-string (if def
3846 (concat "Group Name ("
3849 (if (and (equal "" select)
3855 (cursor-in-echo-area t)
3856 (echo-keystrokes 0))
3857 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3858 (setq c (read-char-exclusive))
3860 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3862 ((or (eq c ?a) (eq c ?A))
3864 ((or (eq c ?d) (eq c ?D))
3865 (throw 'mark 'some)))
3866 (gnus-message 3 "Ignoring unexpected input")
3870 (gnus-message 5 "Regenerating in %s" group)
3871 (let* ((gnus-command-method (or gnus-command-method
3872 (gnus-find-method-for-group group)))
3873 (file (gnus-agent-article-name ".overview" group))
3874 (dir (file-name-directory file))
3876 (downloaded (if (file-exists-p dir)
3877 (sort (delq nil (mapcar (lambda (name)
3878 (and (not (file-directory-p (nnheader-concat dir name)))
3879 (string-to-number name)))
3880 (directory-files dir nil "^[0-9]+$" t)))
3882 (progn (gnus-make-directory dir) nil)))
3887 (mm-with-unibyte-buffer
3888 (if (file-exists-p file)
3889 (let ((nnheader-file-coding-system
3890 gnus-agent-file-coding-system))
3891 (nnheader-insert-file-contents file)))
3892 (set-buffer-modified-p nil)
3894 ;; Load the article IDs found in the overview file. As a
3895 ;; side-effect, validate the file contents.
3899 (goto-char (point-min))
3900 (while (< (point) (point-max))
3901 (cond ((and (looking-at "[0-9]+\t")
3902 (<= (- (match-end 0) (match-beginning 0)) 9))
3903 (push (read (current-buffer)) nov-arts)
3905 (let ((l1 (car nov-arts))
3906 (l2 (cadr nov-arts)))
3907 (cond ((and (listp reread) (memq l1 reread))
3909 (setq nov-arts (cdr nov-arts))
3910 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3911 entry of article %s deleted." l1))
3915 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3916 entries are NOT in ascending order.")
3917 ;; Don't sort now as I haven't verified
3918 ;; that every line begins with a number
3922 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3923 entries contained duplicate of article %s. Duplicate deleted." l1)
3925 (setq nov-arts (cdr nov-arts))))))
3927 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3928 entries contained line that did not begin with an article number. Deleted\
3930 (gnus-delete-line))))
3932 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3933 entries into ascending order.")
3934 (sort-numeric-fields 1 (point-min) (point-max))
3935 (setq nov-arts nil))))
3936 (gnus-agent-check-overview-buffer)
3938 ;; Construct a new article alist whose nodes match every header
3939 ;; in the .overview file. As a side-effect, missing headers are
3940 ;; reconstructed from the downloaded article file.
3941 (while (or downloaded nov-arts)
3942 (cond ((and downloaded
3944 (> (car downloaded) (car nov-arts))))
3945 ;; This entry is missing from the overview file
3946 (gnus-message 3 "Regenerating NOV %s %d..." group
3948 (let ((file (concat dir (number-to-string (car downloaded)))))
3949 (mm-with-unibyte-buffer
3950 (nnheader-insert-file-contents file)
3951 (nnheader-remove-body)
3952 (setq header (nnheader-parse-naked-head)))
3953 (mail-header-set-number header (car downloaded))
3955 (let ((key (concat "^" (int-to-string (car nov-arts))
3957 (or (re-search-backward key nil t)
3958 (re-search-forward key))
3960 (goto-char (point-min)))
3961 (nnheader-insert-nov header))
3962 (setq nov-arts (cons (car downloaded) nov-arts)))
3963 ((eq (car downloaded) (car nov-arts))
3964 ;; This entry in the overview has been downloaded
3965 (push (cons (car downloaded)
3967 (nth 5 (file-attributes
3968 (concat dir (number-to-string
3969 (car downloaded))))))) alist)
3970 (setq downloaded (cdr downloaded))
3971 (setq nov-arts (cdr nov-arts)))
3973 ;; This entry in the overview has not been downloaded
3974 (push (cons (car nov-arts) nil) alist)
3975 (setq nov-arts (cdr nov-arts)))))
3977 ;; When gnus-agent-consider-all-articles is set,
3978 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3979 ;; the alist. Those IDs serve as markers to indicate that an
3980 ;; attempt has been made to fetch that article's header.
3982 ;; When gnus-agent-consider-all-articles is NOT set,
3983 ;; gnus-agent-regenerate-group can remove the article ID of every
3984 ;; article (with the exception of the last ID in the list - it's
3985 ;; special) that no longer appears in the overview. In this
3986 ;; situtation, the last article ID in the list implies that it,
3987 ;; and every article ID preceeding it, have been fetched from the
3990 (if gnus-agent-consider-all-articles
3991 ;; Restore all article IDs that were not found in the overview file.
3992 (let* ((n (cons nil alist))
3994 (o (gnus-agent-load-alist group)))
3996 (let ((nID (caadr n))
3999 (setq n (setcdr n (list (list oID))))
4002 (setcdr n (cons (list oID) (cdr n)))
4008 (setq n (cdr n))))))
4009 (setq alist (cdr merged)))
4010 ;; Restore the last article ID if it is not already in the new alist
4011 (let ((n (last alist))
4012 (o (last (gnus-agent-load-alist group))))
4016 (push (cons (caar o) nil) alist))
4017 ((< (caar n) (caar o))
4018 (setcdr n (list (car o)))))))
4020 (let ((inhibit-quit t))
4021 (if (setq regenerated (buffer-modified-p))
4022 (let ((coding-system-for-write gnus-agent-file-coding-system))
4023 (write-region (point-min) (point-max) file nil 'silent)))
4025 (setq regenerated (or regenerated
4026 (and reread gnus-agent-article-alist)
4027 (not (equal alist gnus-agent-article-alist))))
4029 (setq gnus-agent-article-alist alist)
4032 (gnus-agent-save-alist group)
4034 ;; I have to alter the group's active range NOW as
4035 ;; gnus-make-ascending-articles-unread will use it to
4036 ;; recalculate the number of unread articles in the group
4038 (let ((group (gnus-group-real-name group))
4039 (group-active (or (gnus-active group)
4040 (gnus-activate-group group))))
4041 (gnus-agent-possibly-alter-active group group-active)))))
4043 (when (and reread gnus-agent-article-alist)
4044 (gnus-agent-synchronize-group-flags
4049 (delq nil (mapcar (function (lambda (c)
4050 (cond ((eq reread t)
4054 gnus-agent-article-alist)))
4056 gnus-command-method)
4059 (gnus-agent-update-files-total-fetched-for group nil)))
4065 (defun gnus-agent-regenerate (&optional clean reread)
4066 "Regenerate all agent covered files.
4067 If CLEAN, obsolete (ignore)."
4070 (gnus-message 4 "Regenerating Gnus agent files...")
4071 (dolist (gnus-command-method (gnus-agent-covered-methods))
4072 (dolist (group (gnus-groups-from-server gnus-command-method))
4073 (setq regenerated (or (gnus-agent-regenerate-group group reread)
4075 (gnus-message 4 "Regenerating Gnus agent files...done")
4079 (defun gnus-agent-go-online (&optional force)
4080 "Switch servers into online status."
4081 (interactive (list t))
4082 (dolist (server gnus-opened-servers)
4083 (when (eq (nth 1 server) 'offline)
4084 (if (if (eq force 'ask)
4086 (format "Switch %s:%s into online status? "
4087 (caar server) (cadar server)))
4089 (setcar (nthcdr 1 server) 'close)))))
4091 (defun gnus-agent-toggle-group-plugged (group)
4092 "Toggle the status of the server of the current group."
4093 (interactive (list (gnus-group-group-name)))
4094 (let* ((method (gnus-find-method-for-group group))
4095 (status (cadr (assoc method gnus-opened-servers))))
4096 (if (eq status 'offline)
4097 (gnus-server-set-status method 'closed)
4098 (gnus-close-server method)
4099 (gnus-server-set-status method 'offline))
4100 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4101 (if (eq status 'offline) 'offline 'online)
4102 (if (eq status 'offline) 'online 'offline))))
4104 (defun gnus-agent-group-covered-p (group)
4105 (gnus-agent-method-p (gnus-group-method group)))
4107 ;; Added to support XEmacs
4109 (unless (fboundp 'directory-files-and-attributes)
4110 (defun directory-files-and-attributes (directory
4111 &optional full match nosort)
4113 (dolist (file (directory-files directory full match nosort))
4114 (push (cons file (file-attributes file)) result))
4115 (nreverse result)))))
4117 (defun gnus-agent-update-files-total-fetched-for
4118 (group delta &optional method path)
4119 "Update, or set, the total disk space used by the articles that the
4121 (when gnus-agent-total-fetched-hashtb
4122 (gnus-agent-with-refreshed-group
4124 ;; if null, gnus-agent-group-pathname will calc method.
4125 (let* ((gnus-command-method method)
4126 (path (or path (gnus-agent-group-pathname group)))
4127 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4128 (gnus-sethash path (make-list 3 0)
4129 gnus-agent-total-fetched-hashtb))))
4134 (while (setq file (pop delta))
4135 (incf sum (float (or (nth 7 (file-attributes
4139 (number-to-string file)
4142 (let ((sum (- (nth 2 entry)))
4143 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4145 (while (setq file (pop info))
4146 (incf sum (float (or (nth 8 file) 0))))
4149 (setq gnus-agent-need-update-total-fetched-for t)
4150 (incf (nth 2 entry) delta)))))
4152 (defun gnus-agent-update-view-total-fetched-for
4153 (group agent-over &optional method path)
4154 "Update, or set, the total disk space used by the .agentview and
4155 .overview files. These files are calculated separately as they can be
4157 (when gnus-agent-total-fetched-hashtb
4158 (gnus-agent-with-refreshed-group
4160 ;; if null, gnus-agent-group-pathname will calc method.
4161 (let* ((gnus-command-method method)
4162 (path (or path (gnus-agent-group-pathname group)))
4163 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4164 (gnus-sethash path (make-list 3 0)
4165 gnus-agent-total-fetched-hashtb)))
4166 (size (or (nth 7 (file-attributes
4172 (setq gnus-agent-need-update-total-fetched-for t)
4173 (setf (nth (if agent-over 1 0) entry) size)))))
4175 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4176 "Get the total disk space used by the specified GROUP."
4177 (unless (equal group "dummy.group")
4178 (unless gnus-agent-total-fetched-hashtb
4179 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4181 ;; if null, gnus-agent-group-pathname will calc method.
4182 (let* ((gnus-command-method method)
4183 (path (gnus-agent-group-pathname group))
4184 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4187 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4189 (gnus-agent-update-view-total-fetched-for group nil method path)
4190 (gnus-agent-update-view-total-fetched-for group t method path)
4191 (gnus-agent-update-files-total-fetched-for group nil method path)))))))
4193 (provide 'gnus-agent)
4195 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
4196 ;;; gnus-agent.el ends here