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)
262 ;; Added to support XEmacs
264 (unless (fboundp 'directory-files-and-attributes)
265 (defun directory-files-and-attributes (directory
266 &optional full match nosort)
268 (dolist (file (directory-files directory full match nosort))
269 (push (cons file (file-attributes file)) result))
270 (nreverse result)))))
276 (defun gnus-open-agent ()
278 (gnus-agent-read-servers)
280 (gnus-agent-create-buffer)
281 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
282 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
283 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
285 (defun gnus-agent-create-buffer ()
286 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
288 (setq gnus-agent-overview-buffer
289 (gnus-get-buffer-create " *Gnus agent overview*"))
290 (with-current-buffer gnus-agent-overview-buffer
291 (mm-enable-multibyte))
294 (gnus-add-shutdown 'gnus-close-agent 'gnus)
296 (defun gnus-close-agent ()
297 (setq gnus-category-predicate-cache nil
298 gnus-category-group-cache nil
299 gnus-agent-spam-hashtb nil)
300 (gnus-kill-buffer gnus-agent-overview-buffer))
303 ;;; Utility functions
306 (defmacro gnus-agent-with-refreshed-group (group &rest body)
307 "Performs the body then updates the group's line in the group
308 buffer. Automatically blocks multiple updates due to recursion."
309 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
310 (when (and gnus-agent-need-update-total-fetched-for
311 (not gnus-agent-inhibit-update-total-fetched-for))
313 (set-buffer gnus-group-buffer)
314 (setq gnus-agent-need-update-total-fetched-for nil)
315 (gnus-group-update-group ,group t)))))
317 (defun gnus-agent-read-file (file)
318 "Load FILE and do a `read' there."
321 (nnheader-insert-file-contents file)
322 (goto-char (point-min))
323 (read (current-buffer)))))
325 (defsubst gnus-agent-method ()
326 (concat (symbol-name (car gnus-command-method)) "/"
327 (if (equal (cadr gnus-command-method) "")
329 (cadr gnus-command-method))))
331 (defsubst gnus-agent-directory ()
332 "The name of the Gnus agent directory."
333 (nnheader-concat gnus-agent-directory
334 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
336 (defun gnus-agent-lib-file (file)
337 "The full name of the Gnus agent library FILE."
338 (expand-file-name file
339 (file-name-as-directory
340 (expand-file-name "agent.lib" (gnus-agent-directory)))))
342 (defun gnus-agent-cat-set-property (category property value)
344 (setcdr (or (assq property category)
345 (let ((cell (cons property nil)))
346 (setcdr category (cons cell (cdr category)))
348 (let ((category category))
349 (while (cond ((eq property (caadr category))
350 (setcdr category (cddr category))
353 (setq category (cdr category)))))))
357 (defmacro gnus-agent-cat-defaccessor (name prop-name)
358 "Define accessor and setter methods for manipulating a list of the form
359 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
360 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
361 manipulated as follows:
362 (func LIST): Returns VALUE1
363 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
364 `(progn (defmacro ,name (category)
365 (list (quote cdr) (list (quote assq)
366 (quote (quote ,prop-name)) category)))
368 (define-setf-method ,name (category)
369 (let* ((--category--temp-- (make-symbol "--category--"))
370 (--value--temp-- (make-symbol "--value--")))
371 (list (list --category--temp--) ; temporary-variables
372 (list category) ; value-forms
373 (list --value--temp--) ; store-variables
374 (let* ((category --category--temp--) ; store-form
375 (value --value--temp--))
376 (list (quote gnus-agent-cat-set-property)
378 (quote (quote ,prop-name))
380 (list (quote ,name) --category--temp--) ; access-form
384 (defmacro gnus-agent-cat-name (category)
387 (gnus-agent-cat-defaccessor
388 gnus-agent-cat-days-until-old agent-days-until-old)
389 (gnus-agent-cat-defaccessor
390 gnus-agent-cat-enable-expiration agent-enable-expiration)
391 (gnus-agent-cat-defaccessor
392 gnus-agent-cat-groups agent-groups)
393 (gnus-agent-cat-defaccessor
394 gnus-agent-cat-high-score agent-high-score)
395 (gnus-agent-cat-defaccessor
396 gnus-agent-cat-length-when-long agent-long-article)
397 (gnus-agent-cat-defaccessor
398 gnus-agent-cat-length-when-short agent-short-article)
399 (gnus-agent-cat-defaccessor
400 gnus-agent-cat-low-score agent-low-score)
401 (gnus-agent-cat-defaccessor
402 gnus-agent-cat-predicate agent-predicate)
403 (gnus-agent-cat-defaccessor
404 gnus-agent-cat-score-file agent-score)
405 (gnus-agent-cat-defaccessor
406 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
409 ;; This form is equivalent to defsetf except that it calls make-symbol
410 ;; whereas defsetf calls gensym (Using gensym creates a run-time
411 ;; dependency on the CL library).
414 (define-setf-method gnus-agent-cat-groups (category)
415 (let* ((--category--temp-- (make-symbol "--category--"))
416 (--groups--temp-- (make-symbol "--groups--")))
417 (list (list --category--temp--)
419 (list --groups--temp--)
420 (let* ((category --category--temp--)
421 (groups --groups--temp--))
422 (list (quote gnus-agent-set-cat-groups) category groups))
423 (list (quote gnus-agent-cat-groups) --category--temp--))))
426 (defun gnus-agent-set-cat-groups (category groups)
427 (unless (eq groups 'ignore)
429 (old-g (gnus-agent-cat-groups category)))
430 (cond ((eq new-g old-g)
431 ;; gnus-agent-add-group is fiddling with the group
432 ;; list. Still, Im done.
435 ((eq new-g (cdr old-g))
436 ;; gnus-agent-add-group is fiddling with the group list
437 (setcdr (or (assq 'agent-groups category)
438 (let ((cell (cons 'agent-groups nil)))
439 (setcdr category (cons cell (cdr category)))
442 (let ((groups groups))
444 (let* ((group (pop groups))
445 (old-category (gnus-group-category group)))
446 (if (eq category old-category)
448 (setf (gnus-agent-cat-groups old-category)
449 (delete group (gnus-agent-cat-groups
451 ;; Purge cache as preceeding loop invalidated it.
452 (setq gnus-category-group-cache nil))
454 (setcdr (or (assq 'agent-groups category)
455 (let ((cell (cons 'agent-groups nil)))
456 (setcdr category (cons cell (cdr category)))
459 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
460 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
462 ;;; Fetching setup functions.
464 (defun gnus-agent-start-fetch ()
465 "Initialize data structures for efficient fetching."
466 (gnus-agent-create-buffer))
468 (defun gnus-agent-stop-fetch ()
469 "Save all data structures and clean up."
470 (setq gnus-agent-spam-hashtb nil)
472 (set-buffer nntp-server-buffer)
475 (defmacro gnus-agent-with-fetch (&rest forms)
478 (let ((gnus-agent-fetching t))
479 (gnus-agent-start-fetch)
481 (gnus-agent-stop-fetch)))
483 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
484 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
486 (defmacro gnus-agent-append-to-list (tail value)
487 `(setq ,tail (setcdr ,tail (cons ,value nil))))
489 (defmacro gnus-agent-message (level &rest args)
490 `(if (<= ,level gnus-verbose)
497 (defvar gnus-agent-mode-hook nil
498 "Hook run when installing agent mode.")
500 (defvar gnus-agent-mode nil)
501 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
503 (defun gnus-agent-mode ()
504 "Minor mode for providing a agent support in Gnus buffers."
505 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
506 (symbol-name major-mode))
507 (match-string 1 (symbol-name major-mode))))
508 (mode (intern (format "gnus-agent-%s-mode" buffer))))
509 (set (make-local-variable 'gnus-agent-mode) t)
511 (set (make-local-variable mode) t)
513 (when (gnus-visual-p 'agent-menu 'menu)
514 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
515 (unless (assq 'gnus-agent-mode minor-mode-alist)
516 (push gnus-agent-mode-status minor-mode-alist))
517 (unless (assq mode minor-mode-map-alist)
518 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
520 minor-mode-map-alist))
521 (when (eq major-mode 'gnus-group-mode)
522 (let ((init-plugged gnus-plugged)
523 (gnus-agent-go-online nil))
524 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
525 ;; Therefore, make certain that the current value does not
526 ;; match the desired initial value.
527 (setq gnus-plugged :unknown)
528 (gnus-agent-toggle-plugged init-plugged)))
529 (gnus-run-hooks 'gnus-agent-mode-hook
530 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
532 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
533 (gnus-define-keys gnus-agent-group-mode-map
534 "Ju" gnus-agent-fetch-groups
535 "Jc" gnus-enter-category-buffer
536 "Jj" gnus-agent-toggle-plugged
537 "Js" gnus-agent-fetch-session
538 "JY" gnus-agent-synchronize-flags
539 "JS" gnus-group-send-queue
540 "Ja" gnus-agent-add-group
541 "Jr" gnus-agent-remove-group
542 "Jo" gnus-agent-toggle-group-plugged)
544 (defun gnus-agent-group-make-menu-bar ()
545 (unless (boundp 'gnus-agent-group-menu)
547 gnus-agent-group-menu gnus-agent-group-mode-map ""
549 ["Toggle plugged" gnus-agent-toggle-plugged t]
550 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
551 ["List categories" gnus-enter-category-buffer t]
552 ["Add (current) group to category" gnus-agent-add-group t]
553 ["Remove (current) group from category" gnus-agent-remove-group t]
554 ["Send queue" gnus-group-send-queue gnus-plugged]
556 ["All" gnus-agent-fetch-session gnus-plugged]
557 ["Group" gnus-agent-fetch-group gnus-plugged])
558 ["Synchronize flags" gnus-agent-synchronize-flags t]
561 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
562 (gnus-define-keys gnus-agent-summary-mode-map
563 "Jj" gnus-agent-toggle-plugged
564 "Ju" gnus-agent-summary-fetch-group
565 "JS" gnus-agent-fetch-group
566 "Js" gnus-agent-summary-fetch-series
567 "J#" gnus-agent-mark-article
568 "J\M-#" gnus-agent-unmark-article
569 "@" gnus-agent-toggle-mark
570 "Jc" gnus-agent-catchup)
572 (defun gnus-agent-summary-make-menu-bar ()
573 (unless (boundp 'gnus-agent-summary-menu)
575 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
577 ["Toggle plugged" gnus-agent-toggle-plugged t]
578 ["Mark as downloadable" gnus-agent-mark-article t]
579 ["Unmark as downloadable" gnus-agent-unmark-article t]
580 ["Toggle mark" gnus-agent-toggle-mark t]
581 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
582 ["Catchup undownloaded" gnus-agent-catchup t]))))
584 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
585 (gnus-define-keys gnus-agent-server-mode-map
586 "Jj" gnus-agent-toggle-plugged
587 "Ja" gnus-agent-add-server
588 "Jr" gnus-agent-remove-server)
590 (defun gnus-agent-server-make-menu-bar ()
591 (unless (boundp 'gnus-agent-server-menu)
593 gnus-agent-server-menu gnus-agent-server-mode-map ""
595 ["Toggle plugged" gnus-agent-toggle-plugged t]
596 ["Add" gnus-agent-add-server t]
597 ["Remove" gnus-agent-remove-server t]))))
599 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
600 (if (and (fboundp 'propertize)
601 (fboundp 'make-mode-line-mouse-map))
602 (propertize string 'local-map
603 (make-mode-line-mouse-map mouse-button mouse-func)
605 (cond ((and (featurep 'xemacs)
606 ;; XEmacs' `facep' only checks for a face
607 ;; object, not for a face name, so it's useless
608 ;; to check with `facep'.
609 (find-face 'modeline))
611 ((facep 'mode-line-highlight) ;; Emacs 22
612 'mode-line-highlight)
613 ((facep 'mode-line) ;; Emacs 21
617 (defun gnus-agent-toggle-plugged (set-to)
618 "Toggle whether Gnus is unplugged or not."
619 (interactive (list (not gnus-plugged)))
620 (cond ((eq set-to gnus-plugged)
623 (setq gnus-plugged set-to)
624 (gnus-run-hooks 'gnus-agent-plugged-hook)
625 (setcar (cdr gnus-agent-mode-status)
626 (gnus-agent-make-mode-line-string " Plugged"
628 'gnus-agent-toggle-plugged))
629 (gnus-agent-go-online gnus-agent-go-online)
630 (gnus-agent-possibly-synchronize-flags))
632 (gnus-agent-close-connections)
633 (setq gnus-plugged set-to)
634 (gnus-run-hooks 'gnus-agent-unplugged-hook)
635 (setcar (cdr gnus-agent-mode-status)
636 (gnus-agent-make-mode-line-string " Unplugged"
638 'gnus-agent-toggle-plugged))))
639 (set-buffer-modified-p t))
641 (defmacro gnus-agent-while-plugged (&rest body)
642 `(let ((original-gnus-plugged gnus-plugged))
644 (progn (gnus-agent-toggle-plugged t)
646 (gnus-agent-toggle-plugged original-gnus-plugged))))
648 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
649 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
651 (defun gnus-agent-close-connections ()
652 "Close all methods covered by the Gnus agent."
653 (let ((methods (gnus-agent-covered-methods)))
655 (gnus-close-server (pop methods)))))
658 (defun gnus-unplugged ()
659 "Start Gnus unplugged."
661 (setq gnus-plugged nil)
665 (defun gnus-plugged ()
666 "Start Gnus plugged."
668 (setq gnus-plugged t)
672 (defun gnus-slave-unplugged (&optional arg)
673 "Read news as a slave unplugged."
675 (setq gnus-plugged nil)
676 (gnus arg nil 'slave))
679 (defun gnus-agentize ()
680 "Allow Gnus to be an offline newsreader.
682 The gnus-agentize function is now called internally by gnus when
683 gnus-agent is set. If you wish to avoid calling gnus-agentize,
684 customize gnus-agent to nil.
686 This will modify the `gnus-setup-news-hook', and
687 `message-send-mail-real-function' variables, and install the Gnus agent
688 minor mode in all Gnus buffers."
691 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
692 (unless gnus-agent-send-mail-function
693 (setq gnus-agent-send-mail-function
694 (or message-send-mail-real-function
695 (function (lambda () (funcall message-send-mail-function))))
696 message-send-mail-real-function 'gnus-agent-send-mail))
698 ;; If the servers file doesn't exist, auto-agentize some servers and
699 ;; save the servers file so this auto-agentizing isn't invoked
701 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
702 (gnus-message 3 "First time agent user, agentizing remote groups...")
704 (lambda (server-or-method)
705 (let ((method (gnus-server-to-method server-or-method)))
706 (when (memq (car method)
707 gnus-agent-auto-agentize-methods)
708 (push (gnus-method-to-server method)
709 gnus-agent-covered-methods)
710 (setq gnus-agent-method-p-cache nil))))
711 (cons gnus-select-method gnus-secondary-select-methods))
712 (gnus-agent-write-servers)))
714 (defun gnus-agent-queue-setup (&optional group-name)
715 "Make sure the queue group exists.
716 Optional arg GROUP-NAME allows to specify another group."
717 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
719 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
720 (let ((gnus-level-default-subscribed 1))
721 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
723 (gnus-group-set-parameter
724 (format "nndraft:%s" (or group-name "queue"))
725 'gnus-dummy '((gnus-draft-mode)))))
727 (defun gnus-agent-send-mail ()
728 (if (or (not gnus-agent-queue-mail)
729 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
730 (funcall gnus-agent-send-mail-function)
731 (goto-char (point-min))
733 (concat "^" (regexp-quote mail-header-separator) "\n"))
735 (gnus-agent-insert-meta-information 'mail)
736 (gnus-request-accept-article "nndraft:queue" nil t t)))
738 (defun gnus-agent-insert-meta-information (type &optional method)
739 "Insert meta-information into the message that says how it's to be posted.
740 TYPE can be either `mail' or `news'. If the latter, then METHOD can
743 (message-remove-header gnus-agent-meta-information-header)
744 (goto-char (point-min))
745 (insert gnus-agent-meta-information-header ": "
746 (symbol-name type) " " (format "%S" method)
749 (while (search-backward "\n" nil t)
750 (replace-match "\\n" t t))))
752 (defun gnus-agent-restore-gcc ()
753 "Restore GCC field from saved header."
755 (goto-char (point-min))
756 (while (re-search-forward
757 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
758 (replace-match "Gcc:" 'fixedcase))))
760 (defun gnus-agent-any-covered-gcc ()
762 (message-narrow-to-headers)
763 (let* ((gcc (mail-fetch-field "gcc" nil t))
765 (mapcar 'gnus-inews-group-method
766 (message-unquote-tokens
767 (message-tokenize-header
770 (while (and (not covered) methods)
771 (setq covered (gnus-agent-method-p (car methods))
772 methods (cdr methods)))
776 (defun gnus-agent-possibly-save-gcc ()
777 "Save GCC if Gnus is unplugged."
778 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
780 (goto-char (point-min))
781 (let ((case-fold-search t))
782 (while (re-search-forward "^gcc:" nil t)
783 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
785 (defun gnus-agent-possibly-do-gcc ()
786 "Do GCC if Gnus is plugged."
787 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
788 (gnus-inews-do-gcc)))
791 ;;; Group mode commands
794 (defun gnus-agent-fetch-groups (n)
795 "Put all new articles in the current groups into the Agent."
798 (error "Groups can't be fetched when Gnus is unplugged"))
799 (gnus-group-iterate n 'gnus-agent-fetch-group))
801 (defun gnus-agent-fetch-group (&optional group)
802 "Put all new articles in GROUP into the Agent."
803 (interactive (list (gnus-group-group-name)))
804 (setq group (or group gnus-newsgroup-name))
806 (error "No group on the current line"))
808 (gnus-agent-while-plugged
809 (let ((gnus-command-method (gnus-find-method-for-group group)))
810 (gnus-agent-with-fetch
811 (gnus-agent-fetch-group-1 group gnus-command-method)
812 (gnus-message 5 "Fetching %s...done" group)))))
814 (defun gnus-agent-add-group (category arg)
815 "Add the current group to an agent category."
821 (mapcar (lambda (cat) (list (symbol-name (car cat))))
825 (let ((cat (assq category gnus-category-alist))
827 (gnus-group-iterate arg
829 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
830 (setf (gnus-agent-cat-groups c)
831 (delete group (gnus-agent-cat-groups c))))
832 (push group groups)))
833 (setf (gnus-agent-cat-groups cat)
834 (nconc (gnus-agent-cat-groups cat) groups))
835 (gnus-category-write)))
837 (defun gnus-agent-remove-group (arg)
838 "Remove the current group from its agent category, if any."
841 (gnus-group-iterate arg
843 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
844 (setf (gnus-agent-cat-groups c)
845 (delete group (gnus-agent-cat-groups c))))))
846 (gnus-category-write)))
848 (defun gnus-agent-synchronize-flags ()
849 "Synchronize unplugged flags with servers."
852 (dolist (gnus-command-method (gnus-agent-covered-methods))
853 (when (file-exists-p (gnus-agent-lib-file "flags"))
854 (gnus-agent-synchronize-flags-server gnus-command-method)))))
856 (defun gnus-agent-possibly-synchronize-flags ()
857 "Synchronize flags according to `gnus-agent-synchronize-flags'."
860 (dolist (gnus-command-method (gnus-agent-covered-methods))
861 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
862 (eq (gnus-server-status gnus-command-method) 'ok))
863 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
865 (defun gnus-agent-synchronize-flags-server (method)
866 "Synchronize flags set when unplugged for server."
867 (let ((gnus-command-method method)
869 (when (file-exists-p (gnus-agent-lib-file "flags"))
870 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
872 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
873 (cond ((null gnus-plugged)
875 1 "You must be plugged to synchronize flags with server %s"
876 (nth 1 gnus-command-method)))
877 ((null (gnus-check-server gnus-command-method))
879 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
884 (eval (read (current-buffer)))
885 (delete-region bgn (point))))
887 (delete-file (gnus-agent-lib-file "flags")))
889 (let ((file (gnus-agent-lib-file "flags")))
890 (write-region (point-min) (point-max)
891 (gnus-agent-lib-file "flags") nil 'silent)
892 (error "Couldn't set flags from file %s due to %s"
893 file (error-message-string err)))))))
896 (defun gnus-agent-possibly-synchronize-flags-server (method)
897 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
898 (when (or (and gnus-agent-synchronize-flags
899 (not (eq gnus-agent-synchronize-flags 'ask)))
900 (and (eq gnus-agent-synchronize-flags 'ask)
901 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
903 (gnus-agent-synchronize-flags-server method)))
906 (defun gnus-agent-rename-group (old-group new-group)
907 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
908 Always updates the agent, even when disabled, as the old agent
909 files would corrupt gnus when the agent was next enabled.
910 Depends upon the caller to determine whether group renaming is
912 (let* ((old-command-method (gnus-find-method-for-group old-group))
913 (old-path (directory-file-name
914 (let (gnus-command-method old-command-method)
915 (gnus-agent-group-pathname old-group))))
916 (new-command-method (gnus-find-method-for-group new-group))
917 (new-path (directory-file-name
918 (let (gnus-command-method new-command-method)
919 (gnus-agent-group-pathname new-group)))))
920 (gnus-rename-file old-path new-path t)
922 (let* ((old-real-group (gnus-group-real-name old-group))
923 (new-real-group (gnus-group-real-name new-group))
924 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
925 (gnus-agent-save-group-info old-command-method old-real-group nil)
926 (gnus-agent-save-group-info new-command-method new-real-group old-active)
928 (let ((old-local (gnus-agent-get-local old-group
929 old-real-group old-command-method)))
930 (gnus-agent-set-local old-group
932 old-real-group old-command-method)
933 (gnus-agent-set-local new-group
934 (car old-local) (cdr old-local)
935 new-real-group new-command-method)))))
938 (defun gnus-agent-delete-group (group)
939 "Delete fully-qualified GROUP.
940 Always updates the agent, even when disabled, as the old agent
941 files would corrupt gnus when the agent was next enabled.
942 Depends upon the caller to determine whether group deletion is
944 (let* ((command-method (gnus-find-method-for-group group))
945 (path (directory-file-name
946 (let (gnus-command-method command-method)
947 (gnus-agent-group-pathname group)))))
948 (gnus-delete-directory path)
950 (let* ((real-group (gnus-group-real-name group)))
951 (gnus-agent-save-group-info command-method real-group nil)
953 (let ((local (gnus-agent-get-local group
954 real-group command-method)))
955 (gnus-agent-set-local group
957 real-group command-method)))))
960 ;;; Server mode commands
963 (defun gnus-agent-add-server ()
964 "Enroll SERVER in the agent program."
966 (let* ((server (gnus-server-server-name))
967 (named-server (gnus-server-named-server))
969 (gnus-server-get-method nil server))))
971 (error "No server on the current line"))
973 (when (gnus-agent-method-p method)
974 (error "Server already in the agent program"))
976 (push named-server gnus-agent-covered-methods)
978 (setq gnus-agent-method-p-cache nil)
979 (gnus-server-update-server server)
980 (gnus-agent-write-servers)
981 (gnus-message 1 "Entered %s into the Agent" server)))
983 (defun gnus-agent-remove-server ()
984 "Remove SERVER from the agent program."
986 (let* ((server (gnus-server-server-name))
987 (named-server (gnus-server-named-server)))
989 (error "No server on the current line"))
991 (unless (member named-server gnus-agent-covered-methods)
992 (error "Server not in the agent program"))
994 (setq gnus-agent-covered-methods
995 (delete named-server gnus-agent-covered-methods)
996 gnus-agent-method-p-cache nil)
998 (gnus-server-update-server server)
999 (gnus-agent-write-servers)
1000 (gnus-message 1 "Removed %s from the agent" server)))
1002 (defun gnus-agent-read-servers ()
1003 "Read the alist of covered servers."
1004 (setq gnus-agent-covered-methods
1005 (gnus-agent-read-file
1006 (nnheader-concat gnus-agent-directory "lib/servers"))
1007 gnus-agent-method-p-cache nil)
1009 ;; I am called so early in start-up that I can not validate server
1010 ;; names. When that is the case, I skip the validation. That is
1011 ;; alright as the gnus startup code calls the validate methods
1013 (if gnus-server-alist
1014 (gnus-agent-read-servers-validate)))
1016 (defun gnus-agent-read-servers-validate ()
1017 (mapcar (lambda (server-or-method)
1018 (let* ((server (if (stringp server-or-method)
1020 (gnus-method-to-server server-or-method)))
1021 (method (gnus-server-to-method server)))
1023 (unless (member server gnus-agent-covered-methods)
1024 (push server gnus-agent-covered-methods)
1025 (setq gnus-agent-method-p-cache nil))
1026 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1027 (prog1 gnus-agent-covered-methods
1028 (setq gnus-agent-covered-methods nil))))
1030 (defun gnus-agent-read-servers-validate-native (native-method)
1031 (setq gnus-agent-covered-methods
1032 (mapcar (lambda (method)
1033 (if (or (not method)
1034 (equal method native-method))
1036 method)) gnus-agent-covered-methods)))
1038 (defun gnus-agent-write-servers ()
1039 "Write the alist of covered servers."
1040 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1041 (let ((coding-system-for-write nnheader-file-coding-system)
1042 (file-name-coding-system nnmail-pathname-coding-system))
1043 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1044 (prin1 gnus-agent-covered-methods
1045 (current-buffer)))))
1048 ;;; Summary commands
1051 (defun gnus-agent-mark-article (n &optional unmark)
1052 "Mark the next N articles as downloadable.
1053 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1054 the mark instead. The difference between N and the actual number of
1055 articles marked is returned."
1057 (let ((backward (< n 0))
1062 (gnus-summary-set-agent-mark
1063 (gnus-summary-article-number) unmark)
1064 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1067 (gnus-message 7 "No more articles"))
1068 (gnus-summary-recenter)
1069 (gnus-summary-position-point)
1072 (defun gnus-agent-unmark-article (n)
1073 "Remove the downloadable mark from the next N articles.
1074 If N is negative, unmark backward instead. The difference between N and
1075 the actual number of articles unmarked is returned."
1077 (gnus-agent-mark-article n t))
1079 (defun gnus-agent-toggle-mark (n)
1080 "Toggle the downloadable mark from the next N articles.
1081 If N is negative, toggle backward instead. The difference between N and
1082 the actual number of articles toggled is returned."
1084 (gnus-agent-mark-article n 'toggle))
1086 (defun gnus-summary-set-agent-mark (article &optional unmark)
1087 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1088 When UNMARK is t, the article is unmarked. For any other value, the
1089 article's mark is toggled."
1090 (let ((unmark (cond ((eq nil unmark)
1095 (memq article gnus-newsgroup-downloadable)))))
1096 (when (gnus-summary-goto-subject article nil t)
1097 (gnus-summary-update-mark
1100 (setq gnus-newsgroup-downloadable
1101 (delq article gnus-newsgroup-downloadable))
1102 (gnus-article-mark article))
1103 (setq gnus-newsgroup-downloadable
1104 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1105 gnus-downloadable-mark)
1109 (defun gnus-agent-get-undownloaded-list ()
1110 "Construct list of articles that have not been downloaded."
1111 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1112 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1113 (gnus-agent-method-p gnus-command-method))
1114 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1115 (headers (sort (mapcar (lambda (h)
1116 (mail-header-number h))
1117 gnus-newsgroup-headers) '<))
1118 (cached (and gnus-use-cache gnus-newsgroup-cached))
1119 (undownloaded (list nil))
1120 (tail-undownloaded undownloaded)
1121 (unfetched (list nil))
1122 (tail-unfetched unfetched))
1123 (while (and alist headers)
1124 (let ((a (caar alist))
1127 ;; Ignore IDs in the alist that are not being
1128 ;; displayed in the summary.
1129 (setq alist (cdr alist)))
1131 ;; Headers that are not in the alist should be
1132 ;; fictious (see nnagent-retrieve-headers); they
1133 ;; imply that this article isn't in the agent.
1134 (gnus-agent-append-to-list tail-undownloaded h)
1135 (gnus-agent-append-to-list tail-unfetched h)
1136 (setq headers (cdr headers)))
1138 (setq alist (cdr alist))
1139 (setq headers (cdr headers))
1140 nil ; ignore already downloaded
1143 (setq alist (cdr alist))
1144 (setq headers (cdr headers))
1146 ;; This article isn't in the agent. Check to see
1147 ;; if it is in the cache. If it is, it's been
1149 (while (and cached (< (car cached) a))
1150 (setq cached (cdr cached)))
1151 (unless (equal a (car cached))
1152 (gnus-agent-append-to-list tail-undownloaded a))))))
1155 (let ((num (pop headers)))
1156 (gnus-agent-append-to-list tail-undownloaded num)
1157 (gnus-agent-append-to-list tail-unfetched num)))
1159 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1160 gnus-newsgroup-unfetched (cdr unfetched))))))
1162 (defun gnus-agent-catchup ()
1163 "Mark as read all unhandled articles.
1164 An article is unhandled if it is neither cached, nor downloaded, nor
1168 (let ((articles gnus-newsgroup-undownloaded))
1169 (when (or gnus-newsgroup-downloadable
1170 gnus-newsgroup-cached)
1171 (setq articles (gnus-sorted-ndifference
1172 (gnus-sorted-ndifference
1173 (gnus-copy-sequence articles)
1174 gnus-newsgroup-downloadable)
1175 gnus-newsgroup-cached)))
1178 (gnus-summary-mark-article
1179 (pop articles) gnus-catchup-mark)))
1180 (gnus-summary-position-point)))
1182 (defun gnus-agent-summary-fetch-series ()
1184 (when gnus-newsgroup-processable
1185 (setq gnus-newsgroup-downloadable
1186 (let* ((dl gnus-newsgroup-downloadable)
1187 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1188 (gnus-newsgroup-downloadable processable))
1189 (gnus-agent-summary-fetch-group)
1191 ;; For each article that I processed that is no longer
1192 ;; undownloaded, remove its processable mark.
1194 (mapc #'gnus-summary-remove-process-mark
1195 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1197 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1198 ;; updated the temporary gnus-newsgroup-downloadable to
1199 ;; remove each article successfully fetched. Now, I
1200 ;; update the real gnus-newsgroup-downloadable to only
1201 ;; include undownloaded articles.
1202 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1204 (defun gnus-agent-summary-fetch-group (&optional all)
1205 "Fetch the downloadable articles in the group.
1206 Optional arg ALL, if non-nil, means to fetch all articles."
1209 (if all gnus-newsgroup-articles
1210 gnus-newsgroup-downloadable))
1211 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1213 (gnus-agent-while-plugged
1215 (error "No articles to download"))
1216 (gnus-agent-with-fetch
1217 (setq gnus-newsgroup-undownloaded
1218 (gnus-sorted-ndifference
1219 gnus-newsgroup-undownloaded
1220 (setq fetched-articles
1221 (gnus-agent-fetch-articles
1222 gnus-newsgroup-name articles)))))
1224 (dolist (article articles)
1225 (let ((was-marked-downloadable
1226 (memq article gnus-newsgroup-downloadable)))
1227 (cond (gnus-agent-mark-unread-after-downloaded
1228 (setq gnus-newsgroup-downloadable
1229 (delq article gnus-newsgroup-downloadable))
1231 (gnus-summary-mark-article article gnus-unread-mark))
1232 (was-marked-downloadable
1233 (gnus-summary-set-agent-mark article t)))
1234 (when (gnus-summary-goto-subject article nil t)
1235 (gnus-summary-update-download-mark article))))))
1238 (defun gnus-agent-fetch-selected-article ()
1239 "Fetch the current article as it is selected.
1240 This can be added to `gnus-select-article-hook' or
1241 `gnus-mark-article-hook'."
1242 (let ((gnus-command-method gnus-current-select-method))
1243 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1244 (when (gnus-agent-fetch-articles
1246 (list gnus-current-article))
1247 (setq gnus-newsgroup-undownloaded
1248 (delq gnus-current-article gnus-newsgroup-undownloaded))
1249 (gnus-summary-update-download-mark gnus-current-article)))))
1252 ;;; Internal functions
1255 (defun gnus-agent-synchronize-group-flags (group actions server)
1256 "Update a plugged group by performing the indicated actions."
1257 (let* ((gnus-command-method (gnus-server-to-method server))
1259 ;; This initializer is required as gnus-request-set-mark
1260 ;; calls gnus-group-real-name to strip off the host name
1261 ;; before calling the backend. Now that the backend is
1262 ;; trying to call gnus-request-set-mark, I have to
1263 ;; reconstruct the original group name.
1264 (or (gnus-get-info group)
1266 (setq group (gnus-group-full-name
1267 group gnus-command-method))))))
1268 (gnus-request-set-mark group actions)
1271 (dolist (action actions)
1272 (let ((range (nth 0 action))
1273 (what (nth 1 action))
1274 (marks (nth 2 action)))
1275 (dolist (mark marks)
1276 (cond ((eq mark 'read)
1279 (funcall (if (eq what 'add)
1281 'gnus-remove-from-range)
1282 (gnus-info-read info)
1284 (gnus-get-unread-articles-in-group
1286 (gnus-active (gnus-info-group info))))
1287 ((memq mark '(tick))
1288 (let ((info-marks (assoc mark (gnus-info-marks info))))
1290 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1291 (setcdr info-marks (funcall (if (eq what 'add)
1293 'gnus-remove-from-range)
1297 ;;Marks can be synchronized at any time by simply toggling from
1298 ;;unplugged to plugged. If that is what is happening right now, make
1299 ;;sure that the group buffer is up to date.
1300 (when (gnus-buffer-live-p gnus-group-buffer)
1301 (gnus-group-update-group group t)))
1304 (defun gnus-agent-save-active (method)
1305 (when (gnus-agent-method-p method)
1306 (let* ((gnus-command-method method)
1307 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1308 (file (gnus-agent-lib-file "active")))
1309 (gnus-active-to-gnus-format nil new)
1310 (gnus-agent-write-active file new)
1312 (nnheader-insert-file-contents file))))
1314 (defun gnus-agent-write-active (file new)
1315 (gnus-make-directory (file-name-directory file))
1316 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1317 ;; The hashtable contains real names of groups. However, do NOT
1318 ;; add the foreign server prefix as gnus-active-to-gnus-format
1319 ;; will add it while reading the file.
1320 (gnus-write-active-file file new nil)))
1323 (defun gnus-agent-possibly-alter-active (group active &optional info)
1324 "Possibly expand a group's active range to include articles
1325 downloaded into the agent."
1326 (let* ((gnus-command-method (or gnus-command-method
1327 (gnus-find-method-for-group group))))
1328 (when (gnus-agent-method-p gnus-command-method)
1329 (let* ((local (gnus-agent-get-local group))
1330 (active-min (or (car active) 0))
1331 (active-max (or (cdr active) 0))
1332 (agent-min (or (car local) active-min))
1333 (agent-max (or (cdr local) active-max)))
1335 (when (< agent-min active-min)
1336 (setcar active agent-min))
1338 (when (> agent-max active-max)
1339 (setcdr active agent-max))
1341 (when (and info (< agent-max (- active-min 100)))
1342 ;; I'm expanding the active range by such a large amount
1343 ;; that there is a gap of more than 100 articles between the
1344 ;; last article known to the agent and the first article
1345 ;; currently available on the server. This gap contains
1346 ;; articles that have been lost, mark them as read so that
1347 ;; gnus doesn't waste resources trying to fetch them.
1349 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1350 ;; want to modify the local file everytime someone restarts
1351 ;; gnus. The small gap will cause a tiny performance hit
1352 ;; when gnus tries, and fails, to retrieve the articles.
1353 ;; Still that should be smaller than opening a buffer,
1354 ;; printing this list to the buffer, and then writing it to a
1357 (let ((read (gnus-info-read info)))
1362 (list (cons (1+ agent-max)
1363 (1- active-min))))))
1365 ;; Lie about the agent's local range for this group to
1366 ;; disable the set read each time this server is opened.
1367 ;; NOTE: Opening this group will restore the valid local
1368 ;; range but it will also expand the local range to
1369 ;; incompass the new active range.
1370 (gnus-agent-set-local group agent-min (1- active-min)))))))
1372 (defun gnus-agent-save-group-info (method group active)
1373 "Update a single group's active range in the agent's copy of the server's active file."
1374 (when (gnus-agent-method-p method)
1375 (let* ((gnus-command-method (or method gnus-command-method))
1376 (coding-system-for-write nnheader-file-coding-system)
1377 (file-name-coding-system nnmail-pathname-coding-system)
1378 (file (gnus-agent-lib-file "active"))
1379 oactive-min oactive-max)
1380 (gnus-make-directory (file-name-directory file))
1381 (with-temp-file file
1382 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1383 (mm-disable-multibyte)
1384 (when (file-exists-p file)
1385 (nnheader-insert-file-contents file)
1387 (goto-char (point-min))
1388 (when (re-search-forward
1389 (concat "^" (regexp-quote group) " ") nil t)
1391 (setq oactive-max (read (current-buffer)) ;; max
1392 oactive-min (read (current-buffer)))) ;; min
1393 (gnus-delete-line)))
1395 (insert (format "%S %d %d y\n" (intern group)
1396 (max (or oactive-max (cdr active)) (cdr active))
1397 (min (or oactive-min (car active)) (car active))))
1398 (goto-char (point-max))
1399 (while (search-backward "\\." nil t)
1400 (delete-char 1)))))))
1402 (defun gnus-agent-get-group-info (method group)
1403 "Get a single group's active range in the agent's copy of the server's active file."
1404 (when (gnus-agent-method-p method)
1405 (let* ((gnus-command-method (or method gnus-command-method))
1406 (coding-system-for-write nnheader-file-coding-system)
1407 (file-name-coding-system nnmail-pathname-coding-system)
1408 (file (gnus-agent-lib-file "active"))
1409 oactive-min oactive-max)
1410 (gnus-make-directory (file-name-directory file))
1412 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1413 (mm-disable-multibyte)
1414 (when (file-exists-p file)
1415 (nnheader-insert-file-contents file)
1417 (goto-char (point-min))
1418 (when (re-search-forward
1419 (concat "^" (regexp-quote group) " ") nil t)
1421 (setq oactive-max (read (current-buffer)) ;; max
1422 oactive-min (read (current-buffer))) ;; min
1423 (cons oactive-min oactive-max))))))))
1425 (defun gnus-agent-group-path (group)
1426 "Translate GROUP into a file name."
1428 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1429 ;; The two methods must be kept synchronized, which is why
1430 ;; gnus-agent-group-pathname was added.
1433 (nnheader-translate-file-chars
1434 (nnheader-replace-duplicate-chars-in-string
1435 (nnheader-replace-chars-in-string
1436 (gnus-group-real-name (gnus-group-decoded-name group))
1439 (if (or nnmail-use-long-file-names
1440 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1442 (mm-encode-coding-string
1443 (nnheader-replace-chars-in-string group ?. ?/)
1444 nnmail-pathname-coding-system)))
1446 (defun gnus-agent-group-pathname (group)
1447 "Translate GROUP into a file name."
1448 ;; nnagent uses nnmail-group-pathname to read articles while
1449 ;; unplugged. The agent must, therefore, use the same directory
1451 (let ((gnus-command-method (or gnus-command-method
1452 (gnus-find-method-for-group group))))
1453 (nnmail-group-pathname (gnus-group-real-name
1454 (gnus-group-decoded-name group))
1455 (gnus-agent-directory))))
1457 (defun gnus-agent-get-function (method)
1458 (if (gnus-online method)
1463 (defun gnus-agent-covered-methods ()
1464 "Return the subset of methods that are covered by the agent."
1465 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1467 ;;; History functions
1469 (defun gnus-agent-history-buffer ()
1470 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1472 (defun gnus-agent-open-history ()
1474 (push (cons (gnus-agent-method)
1475 (set-buffer (gnus-get-buffer-create
1476 (format " *Gnus agent %s history*"
1477 (gnus-agent-method)))))
1478 gnus-agent-history-buffers)
1479 (mm-disable-multibyte) ;; everything is binary
1482 (let ((file (gnus-agent-lib-file "history")))
1483 (when (file-exists-p file)
1484 (nnheader-insert-file-contents file))
1485 (set (make-local-variable 'gnus-agent-file-name) file))))
1487 (defun gnus-agent-close-history ()
1488 (when (gnus-buffer-live-p gnus-agent-current-history)
1489 (kill-buffer gnus-agent-current-history)
1490 (setq gnus-agent-history-buffers
1491 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1492 gnus-agent-history-buffers))))
1498 (defun gnus-agent-fetch-articles (group articles)
1499 "Fetch ARTICLES from GROUP and put them into the Agent."
1501 (gnus-agent-load-alist group)
1502 (let* ((alist gnus-agent-article-alist)
1503 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1504 (selected-sets (list nil))
1505 (current-set-size 0)
1508 ;; Check each article
1509 (while (setq article (pop articles))
1510 ;; Skip alist entries preceeding this article
1511 (while (> article (or (caar alist) (1+ article)))
1512 (setq alist (cdr alist)))
1514 ;; Prune off articles that we have already fetched.
1515 (unless (and (eq article (caar alist))
1517 ;; Skip headers preceeding this article
1520 (let* ((header (car headers)))
1522 (mail-header-number header)
1524 (setq headers (cdr headers)))
1526 ;; Add this article to the current set
1527 (setcar selected-sets (cons article (car selected-sets)))
1529 ;; Update the set size, when the set is too large start a
1530 ;; new one. I do this after adding the article as I want at
1531 ;; least one article in each set.
1532 (when (< gnus-agent-max-fetch-size
1533 (setq current-set-size
1535 (if (= header-number article)
1536 (let ((char-size (mail-header-chars
1538 (if (<= char-size 0)
1539 ;; The char size was missing/invalid,
1540 ;; assume a worst-case situation of
1541 ;; 65 char/line. If the line count
1542 ;; is missing, arbitrarily assume a
1543 ;; size of 1000 characters.
1544 (max (* 65 (mail-header-lines
1549 (setcar selected-sets (nreverse (car selected-sets)))
1550 (setq selected-sets (cons nil selected-sets)
1551 current-set-size 0))))
1553 (when (or (cdr selected-sets) (car selected-sets))
1554 (let* ((fetched-articles (list nil))
1555 (tail-fetched-articles fetched-articles)
1556 (dir (gnus-agent-group-pathname group))
1557 (date (time-to-days (current-time)))
1558 (case-fold-search t)
1561 (setcar selected-sets (nreverse (car selected-sets)))
1562 (setq selected-sets (nreverse selected-sets))
1564 (gnus-make-directory dir)
1565 (gnus-message 7 "Fetching articles for %s..." group)
1568 (while (setq articles (pop selected-sets))
1569 ;; Fetch the articles from the backend.
1570 (if (gnus-check-backend-function 'retrieve-articles group)
1571 (setq pos (gnus-retrieve-articles articles group))
1574 (while (setq article (pop articles))
1575 (gnus-message 10 "Fetching article %s for %s..."
1578 (gnus-backlog-request-article group article
1580 (gnus-request-article article group))
1581 (goto-char (point-max))
1582 (push (cons article (point)) pos)
1583 (insert-buffer-substring nntp-server-buffer)))
1585 nntp-server-buffer (point-min) (point-max))
1586 (setq pos (nreverse pos)))))
1587 ;; Then save these articles into the Agent.
1589 (set-buffer nntp-server-buffer)
1591 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1592 (goto-char (point-min))
1593 (unless (eobp) ;; Don't save empty articles.
1594 (when (search-forward "\n\n" nil t)
1595 (when (search-backward "\nXrefs: " nil t)
1596 ;; Handle cross posting.
1597 (goto-char (match-end 0)) ; move to end of header name
1598 (skip-chars-forward "^ ") ; skip server name
1599 (skip-chars-forward " ")
1601 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1602 (push (cons (buffer-substring (match-beginning 1)
1605 (buffer-substring (match-beginning 2)
1608 (goto-char (match-end 0)))
1609 (gnus-agent-crosspost crosses (caar pos) date)))
1610 (goto-char (point-min))
1611 (if (not (re-search-forward
1612 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1613 (setq id "No-Message-ID-in-article")
1614 (setq id (buffer-substring
1615 (match-beginning 1) (match-end 1))))
1616 (let ((coding-system-for-write
1617 gnus-agent-file-coding-system))
1618 (write-region (point-min) (point-max)
1619 (concat dir (number-to-string (caar pos)))
1622 (gnus-agent-append-to-list
1623 tail-fetched-articles (caar pos)))
1625 (setq pos (cdr pos)))))
1627 (gnus-agent-save-alist group (cdr fetched-articles) date)
1628 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1630 (gnus-message 7 ""))
1631 (cdr fetched-articles))))))
1633 (defun gnus-agent-unfetch-articles (group articles)
1634 "Delete ARTICLES that were fetched from GROUP into the agent."
1636 (gnus-agent-with-refreshed-group
1638 (gnus-agent-load-alist group)
1639 (let* ((alist (cons nil gnus-agent-article-alist))
1640 (articles (sort articles #'<))
1641 (next-possibility alist)
1642 (delete-this (pop articles)))
1643 (while (and (cdr next-possibility) delete-this)
1644 (let ((have-this (caar (cdr next-possibility))))
1645 (cond ((< delete-this have-this)
1646 (setq delete-this (pop articles)))
1647 ((= delete-this have-this)
1648 (let ((timestamp (cdar (cdr next-possibility))))
1650 (let* ((file-name (concat (gnus-agent-group-pathname group)
1651 (number-to-string have-this)))
1652 (size-file (float (or (and gnus-agent-total-fetched-hashtb
1653 (nth 7 (file-attributes file-name)))
1655 (delete-file file-name)
1656 (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1658 (setcdr next-possibility (cddr next-possibility)))
1660 (setq next-possibility (cdr next-possibility))))))
1661 (setq gnus-agent-article-alist (cdr alist))
1662 (gnus-agent-save-alist group)))))
1664 (defun gnus-agent-crosspost (crosses article &optional date)
1665 (setq date (or date t))
1667 (let (gnus-agent-article-alist group alist beg end)
1669 (set-buffer gnus-agent-overview-buffer)
1670 (when (nnheader-find-nov-line article)
1673 (setq end (progn (forward-line 1) (point)))))
1675 (setq group (caar crosses))
1676 (unless (setq alist (assoc group gnus-agent-group-alist))
1677 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1678 gnus-agent-group-alist))
1679 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1681 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1683 (when (= (point-max) (point-min))
1684 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1686 (nnheader-insert-file-contents
1687 (gnus-agent-article-name ".overview" group))))
1688 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1689 (insert (string-to-number (cdar crosses)))
1690 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1691 (gnus-agent-check-overview-buffer))
1692 (setq crosses (cdr crosses)))))
1694 (defun gnus-agent-backup-overview-buffer ()
1695 (when gnus-newsgroup-name
1696 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1699 (while (file-exists-p
1700 (setq name (concat root "~"
1701 (int-to-string (setq cnt (1+ cnt))) "~"))))
1702 (write-region (point-min) (point-max) name nil 'no-msg)
1703 (gnus-message 1 "Created backup copy of overview in %s." name)))
1706 (defun gnus-agent-check-overview-buffer (&optional buffer)
1707 "Check the overview file given for sanity.
1708 In particular, checks that the file is sorted by article number
1709 and that there are no duplicates."
1714 (set-buffer buffer))
1717 (goto-char (point-min))
1719 (while (< (point) (point-max))
1721 (cur (condition-case nil
1722 (read (current-buffer))
1725 ((or (not (integerp cur))
1726 (not (eq (char-after) ?\t)))
1728 (setq backed-up (gnus-agent-backup-overview-buffer)))
1730 "Overview buffer contains garbage '%s'."
1735 (setq backed-up (gnus-agent-backup-overview-buffer)))
1737 "Duplicate overview line for %d" cur)
1738 (delete-region p (progn (forward-line 1) (point))))
1741 (setq backed-up (gnus-agent-backup-overview-buffer)))
1742 (gnus-message 1 "Overview buffer not sorted!")
1743 (sort-numeric-fields 1 (point-min) (point-max))
1744 (goto-char (point-min))
1747 (setq prev-num cur)))
1748 (forward-line 1)))))))
1750 (defun gnus-agent-flush-server (&optional server-or-method)
1751 "Flush all agent index files for every subscribed group within
1752 the given SERVER-OR-METHOD. When called with nil, the current
1753 value of gnus-command-method identifies the server."
1754 (let* ((gnus-command-method (if server-or-method
1755 (gnus-server-to-method server-or-method)
1756 gnus-command-method))
1757 (alist gnus-newsrc-alist))
1759 (let ((entry (pop alist)))
1760 (when (gnus-methods-equal-p gnus-command-method (gnus-info-method entry))
1761 (gnus-agent-flush-group (gnus-info-group entry)))))))
1763 (defun gnus-agent-flush-group (group)
1764 "Flush the agent's index files such that the GROUP no longer
1765 appears to have any local content. The actual content, the
1766 article files, may then be deleted using gnus-agent-expire-group.
1767 If flushing was a mistake, the gnus-agent-regenerate-group method
1768 provides an undo mechanism by reconstructing the index files from
1771 (list (let ((def (or (gnus-group-group-name)
1772 gnus-newsgroup-name)))
1773 (let ((select (read-string (if def
1774 (concat "Group Name ("
1777 (if (and (equal "" select)
1782 (let* ((gnus-command-method (or gnus-command-method
1783 (gnus-find-method-for-group group)))
1784 (overview (gnus-agent-article-name ".overview" group))
1785 (agentview (gnus-agent-article-name ".agentview" group)))
1787 (if (file-exists-p overview)
1788 (delete-file overview))
1789 (if (file-exists-p agentview)
1790 (delete-file agentview))
1792 (gnus-agent-update-view-total-fetched-for group nil gnus-command-method)
1793 (gnus-agent-update-view-total-fetched-for group t gnus-command-method)
1795 ;(gnus-agent-set-local group nil nil)
1796 ;(gnus-agent-save-local t)
1797 (gnus-agent-save-group-info nil group nil)))
1799 (defun gnus-agent-flush-cache ()
1800 "Flush the agent's index files such that the group no longer
1801 appears to have any local content. The actual content, the
1802 article files, is then deleted using gnus-agent-expire-group. The
1803 gnus-agent-regenerate-group method provides an undo mechanism by
1804 reconstructing the index files from the article files."
1806 (while gnus-agent-buffer-alist
1807 (set-buffer (cdar gnus-agent-buffer-alist))
1808 (let ((coding-system-for-write
1809 gnus-agent-file-coding-system))
1810 (write-region (point-min) (point-max)
1811 (gnus-agent-article-name ".overview"
1812 (caar gnus-agent-buffer-alist))
1814 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1815 (while gnus-agent-group-alist
1816 (with-temp-file (gnus-agent-article-name
1817 ".agentview" (caar gnus-agent-group-alist))
1818 (princ (cdar gnus-agent-group-alist))
1820 (princ 1 (current-buffer))
1822 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1825 (defun gnus-agent-find-parameter (group symbol)
1826 "Search for GROUPs SYMBOL in the group's parameters, the group's
1827 topic parameters, the group's category, or the customizable
1828 variables. Returns the first non-nil value found."
1829 (or (gnus-group-find-parameter group symbol t)
1830 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1834 '((agent-short-article . gnus-agent-short-article)
1835 (agent-long-article . gnus-agent-long-article)
1836 (agent-low-score . gnus-agent-low-score)
1837 (agent-high-score . gnus-agent-high-score)
1838 (agent-days-until-old . gnus-agent-expire-days)
1839 (agent-enable-expiration
1840 . gnus-agent-enable-expiration)
1841 (agent-predicate . gnus-agent-predicate)))))))
1843 (defun gnus-agent-fetch-headers (group &optional force)
1844 "Fetch interesting headers into the agent. The group's overview
1845 file will be updated to include the headers while a list of available
1846 article numbers will be returned."
1847 (let* ((fetch-all (and gnus-agent-consider-all-articles
1848 ;; Do not fetch all headers if the predicate
1849 ;; implies that we only consider unread articles.
1850 (not (gnus-predicate-implies-unread
1851 (gnus-agent-find-parameter group
1852 'agent-predicate)))))
1853 (articles (if fetch-all
1854 (gnus-uncompress-range (gnus-active group))
1855 (gnus-list-of-unread-articles group)))
1856 (gnus-decode-encoded-word-function 'identity)
1857 (file (gnus-agent-article-name ".overview" group)))
1860 ;; Add articles with marks to the list of article headers we want to
1861 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1862 ;; mark, but do fetch recent or seen articles if they have other, more
1863 ;; interesting marks. (We have to fetch articles with boring marks
1864 ;; because otherwise the agent will remove their marks.)
1865 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1866 (unless (memq (car arts) '(seen recent killed cache))
1867 (setq articles (gnus-range-add articles (cdr arts)))))
1868 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1870 ;; At this point, I have the list of articles to consider for
1871 ;; fetching. This is the list that I'll return to my caller. Some
1872 ;; of these articles may have already been fetched. That's OK as
1873 ;; the fetch article code will filter those out. Internally, I'll
1874 ;; filter this list to just those articles whose headers need to
1876 (let ((articles articles))
1877 ;; Remove known articles.
1878 (when (and (or gnus-agent-cache
1880 (gnus-agent-load-alist group))
1881 ;; Remove articles marked as downloaded.
1883 ;; I want to fetch all headers in the active range.
1884 ;; Therefore, exclude only those headers that are in the
1886 ;; NOTE: This is probably NOT what I want to do after
1887 ;; agent expiration in this group.
1888 (setq articles (gnus-agent-uncached-articles articles group))
1890 ;; I want to only fetch those headers that have never been
1891 ;; fetched. Therefore, exclude all headers that are, or
1892 ;; WERE, in the article alist.
1893 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1894 (high (cdr (gnus-active group))))
1895 ;; Low can be greater than High when the same group is
1896 ;; fetched twice in the same session {The first fetch will
1897 ;; fill the article alist such that (last
1898 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1899 ;; group))}. The addition of one(the 1+ above) then
1900 ;; forces Low to be greater than High. When this happens,
1901 ;; gnus-list-range-intersection returns nil which
1902 ;; indicates that no headers need to be fetched. -- Kevin
1903 (setq articles (gnus-list-range-intersection
1904 articles (list (cons low high)))))))
1907 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1908 (gnus-compress-sequence articles t))
1911 (set-buffer nntp-server-buffer)
1915 (gnus-message 7 "Fetching headers for %s..." group)
1918 (gnus-make-directory (nnheader-translate-file-chars
1919 (file-name-directory file) t))
1921 (unless (eq 'nov (gnus-retrieve-headers articles group))
1922 (nnvirtual-convert-headers))
1923 (gnus-agent-check-overview-buffer)
1924 ;; Move these headers to the overview buffer so that
1925 ;; gnus-agent-braid-nov can merge them with the contents
1928 gnus-agent-overview-buffer (point-min) (point-max))
1929 ;; NOTE: Call g-a-brand-nov even when the file does not
1930 ;; exist. As a minimum, it will validate the article
1931 ;; numbers already in the buffer.
1932 (gnus-agent-braid-nov group articles file)
1933 (let ((coding-system-for-write
1934 gnus-agent-file-coding-system))
1935 (gnus-agent-check-overview-buffer)
1936 (write-region (point-min) (point-max) file nil 'silent))
1937 (gnus-agent-update-view-total-fetched-for group t)
1938 (gnus-agent-save-alist group articles nil)
1942 (nnheader-insert-file-contents file)))))
1945 (defsubst gnus-agent-read-article-number ()
1946 "Reads the article number at point. Returns nil when a valid article number can not be read."
1948 ;; It is unfortunate but the read function quietly overflows
1949 ;; integer. As a result, I have to use string operations to test
1950 ;; for overflow BEFORE calling read.
1951 (when (looking-at "[0-9]+\t")
1952 (let ((len (- (match-end 0) (match-beginning 0))))
1954 (read (current-buffer)))
1956 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1957 ;; Back convert from int to string to ensure that this is one of them.
1958 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1959 (num (read (current-buffer)))
1960 (str2 (int-to-string num)))
1961 (when (equal str1 str2)
1964 (defsubst gnus-agent-copy-nov-line (article)
1965 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1967 (set-buffer gnus-agent-overview-buffer)
1968 (while (and (not (eobp))
1969 (or (not (setq art (gnus-agent-read-article-number)))
1974 (not (eq article art)))
1975 (set-buffer nntp-server-buffer)
1977 (setq e (progn (forward-line 1) (point)))
1978 (set-buffer nntp-server-buffer)
1979 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1981 (defun gnus-agent-braid-nov (group articles file)
1982 "Merge agent overview data with given file.
1983 Takes unvalidated headers for ARTICLES from
1984 `gnus-agent-overview-buffer' and validated headers from the given
1985 FILE and places the combined valid headers into
1986 `nntp-server-buffer'. This function can be used, when file
1987 doesn't exist, to valid the overview buffer."
1989 (set-buffer gnus-agent-overview-buffer)
1990 (goto-char (point-min))
1991 (set-buffer nntp-server-buffer)
1993 (when (file-exists-p file)
1994 (nnheader-insert-file-contents file))
1995 (goto-char (point-max))
1998 (unless (or (= (point-min) (point-max))
1999 (< (setq last (read (current-buffer))) (car articles)))
2000 ;; Old and new overlap -- We do it the hard way.
2001 (when (nnheader-find-nov-line (car articles))
2002 ;; Replacing existing NOV entry
2003 (delete-region (point) (progn (forward-line 1) (point))))
2004 (gnus-agent-copy-nov-line (pop articles))
2008 (while (let ((art (read (current-buffer))))
2009 (cond ((< art (car articles))
2012 ((= art (car articles))
2015 (point) (progn (forward-line 1) (point)))
2021 (gnus-agent-copy-nov-line (pop articles)))))
2023 (goto-char (point-max))
2025 ;; Append the remaining lines
2028 (set-buffer gnus-agent-overview-buffer)
2029 (setq start (point))
2030 (set-buffer nntp-server-buffer))
2033 (insert-buffer-substring gnus-agent-overview-buffer start)
2036 (setq last (or last -134217728))
2037 (while (catch 'problems
2040 (setq art (gnus-agent-read-article-number))
2042 ;; Bad art num - delete this line
2044 (delete-region (point) (progn (forward-line 1) (point))))
2046 ;; Art num out of order - enable sort
2050 ;; Bad repeat of art number - delete this line
2052 (delete-region (point) (progn (forward-line 1) (point))))
2058 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
2059 ;; First, we'll fix the sort.
2060 (sort-numeric-fields 1 (point-min) (point-max))
2062 ;; but now we have to consider that we may have duplicate rows...
2063 ;; so reset to beginning of file
2064 (goto-char (point-min))
2065 (setq last -134217728)
2067 ;; and throw a code that restarts this scan
2068 (throw 'problems t))
2071 ;; Keeps the compiler from warning about the free variable in
2072 ;; gnus-agent-read-agentview.
2074 (defvar gnus-agent-read-agentview))
2076 (defun gnus-agent-load-alist (group)
2077 "Load the article-state alist for GROUP."
2078 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
2079 (let ((gnus-agent-read-agentview group))
2080 (setq gnus-agent-article-alist
2081 (gnus-cache-file-contents
2082 (gnus-agent-article-name ".agentview" group)
2083 'gnus-agent-file-loading-cache
2084 'gnus-agent-read-agentview))))
2086 (defun gnus-agent-read-agentview (file)
2087 "Load FILE and do a `read' there."
2091 (nnheader-insert-file-contents file)
2092 (goto-char (point-min))
2093 (let ((alist (read (current-buffer)))
2094 (version (condition-case nil (read (current-buffer))
2100 (let ((inhibit-quit t)
2102 (gnus-agent-open-history)
2103 (set-buffer (gnus-agent-history-buffer))
2104 (goto-char (point-min))
2106 (if (and (looking-at
2107 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2108 (string= (match-string 2)
2109 gnus-agent-read-agentview)
2110 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2111 (setcdr entry (string-to-number (match-string 1))))
2113 (gnus-agent-close-history)
2114 (setq changed-version t)))
2116 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2121 (let ((state (car comp-list))
2123 (gnus-uncompress-range
2125 (mapcar (lambda (article-id)
2126 (setq uncomp (cons (cons article-id state) uncomp)))
2129 (setq alist (sort uncomp 'car-less-than-car)))
2130 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2131 (when changed-version
2132 (let ((gnus-agent-article-alist alist))
2133 (gnus-agent-save-alist gnus-agent-read-agentview)))
2135 ((end-of-file file-error)
2136 ;; The agentview file is missing.
2138 ;; If the agent directory exists, attempt to perform a brute-force
2139 ;; reconstruction of its contents.
2141 (file-attributes (directory-files-and-attributes
2142 (gnus-agent-article-name ""
2143 gnus-agent-read-agentview) nil "^[0-9]+$" t)))
2144 (while file-attributes
2145 (let ((fa (pop file-attributes)))
2147 (push (cons (string-to-number (nth 0 fa)) (time-to-days (nth 5 fa))) alist))))
2149 (file-error nil))))))
2151 (defun gnus-agent-save-alist (group &optional articles state)
2152 "Save the article-state alist for GROUP."
2153 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2154 (prev (cons nil gnus-agent-article-alist))
2156 print-level print-length item article)
2157 (while (setq article (pop articles))
2158 (while (and (cdr prev)
2159 (< (caadr prev) article))
2160 (setq prev (cdr prev)))
2163 (setcdr prev (list (cons article state))))
2164 ((> (caadr prev) article)
2165 (setcdr prev (cons (cons article state) (cdr prev))))
2166 ((= (caadr prev) article)
2167 (setcdr (cadr prev) state)))
2168 (setq prev (cdr prev)))
2169 (setq gnus-agent-article-alist (cdr all))
2171 (gnus-agent-set-local group
2172 (caar gnus-agent-article-alist)
2173 (caar (last gnus-agent-article-alist)))
2175 (gnus-make-directory (gnus-agent-article-name "" group))
2176 (with-temp-file (gnus-agent-article-name ".agentview" group)
2177 (cond ((eq gnus-agent-article-alist-save-format 1)
2178 (princ gnus-agent-article-alist (current-buffer)))
2179 ((eq gnus-agent-article-alist-save-format 2)
2180 (let ((compressed nil))
2181 (mapcar (lambda (pair)
2182 (let* ((article-id (car pair))
2183 (day-of-download (cdr pair))
2184 (comp-list (assq day-of-download compressed)))
2187 (cons article-id (cdr comp-list)))
2189 (cons (list day-of-download article-id)
2191 nil)) gnus-agent-article-alist)
2192 (mapcar (lambda (comp-list)
2194 (gnus-compress-sequence
2195 (nreverse (cdr comp-list)))))
2197 (princ compressed (current-buffer)))))
2199 (princ gnus-agent-article-alist-save-format (current-buffer))
2202 (gnus-agent-update-view-total-fetched-for group nil)))
2204 (defvar gnus-agent-article-local nil)
2205 (defvar gnus-agent-file-loading-local nil)
2207 (defun gnus-agent-load-local (&optional method)
2208 "Load the METHOD'S local file. The local file contains min/max
2209 article counts for each of the method's subscribed groups."
2210 (let ((gnus-command-method (or method gnus-command-method)))
2211 (setq gnus-agent-article-local
2212 (gnus-cache-file-contents
2213 (gnus-agent-lib-file "local")
2214 'gnus-agent-file-loading-local
2215 'gnus-agent-read-and-cache-local))))
2217 (defun gnus-agent-read-and-cache-local (file)
2218 "Load and read FILE then bind its contents to
2219 gnus-agent-article-local. If that variable had `dirty' (also known as
2220 modified) original contents, they are first saved to their own file."
2222 (if (and gnus-agent-article-local
2223 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2224 (gnus-agent-save-local))
2225 (gnus-agent-read-local file))
2227 (defun gnus-agent-read-local (file)
2228 "Load FILE and do a `read' there."
2229 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2234 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2235 (nnheader-insert-file-contents file))
2238 (goto-char (point-min))
2239 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2240 (while (= (following-char) ?\;)
2242 (setq line (1+ line)))
2249 (cur (current-buffer))
2250 (obarray my-obarray))
2251 (setq group (read cur)
2255 (when (stringp group)
2256 (setq group (intern group my-obarray)))
2258 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2259 (set group (cons (+ 0 min) (+ 0 max))))
2261 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2262 file line (error-message-string err))))
2264 (setq line (1+ line))))
2266 (set (intern "+dirty" my-obarray) nil)
2267 (set (intern "+method" my-obarray) gnus-command-method)
2270 (defun gnus-agent-save-local (&optional force)
2271 "Save gnus-agent-article-local under it method's agent.lib directory."
2272 (let ((my-obarray gnus-agent-article-local))
2273 (when (and my-obarray
2274 (or force (symbol-value (intern "+dirty" my-obarray))))
2275 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2276 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2277 (dest (gnus-agent-lib-file "local")))
2278 (gnus-make-directory (gnus-agent-lib-file ""))
2280 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2281 (with-temp-file dest
2282 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2283 (file-name-coding-system nnmail-pathname-coding-system)
2284 print-level print-length item article
2285 (standard-output (current-buffer)))
2286 (mapatoms (lambda (symbol)
2287 (cond ((not (boundp symbol))
2289 ((member (symbol-name symbol) '("+dirty" "+method"))
2292 (let ((range (symbol-value symbol)))
2302 (defun gnus-agent-get-local (group &optional gmane method)
2303 (let* ((gmane (or gmane (gnus-group-real-name group)))
2304 (gnus-command-method (or method (gnus-find-method-for-group group)))
2305 (local (gnus-agent-load-local))
2306 (symb (intern gmane local))
2307 (minmax (and (boundp symb) (symbol-value symb))))
2309 ;; Bind these so that gnus-agent-load-alist doesn't change the
2310 ;; current alist (i.e. gnus-agent-article-alist)
2311 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2312 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2313 (alist (gnus-agent-load-alist group)))
2317 (caar (last alist))))
2318 (gnus-agent-set-local group (car minmax) (cdr minmax)
2319 gmane gnus-command-method local))))
2322 (defun gnus-agent-set-local (group min max &optional gmane method local)
2323 (let* ((gmane (or gmane (gnus-group-real-name group)))
2324 (gnus-command-method (or method (gnus-find-method-for-group group)))
2325 (local (or local (gnus-agent-load-local)))
2326 (symb (intern gmane local))
2327 (minmax (and (boundp symb) (symbol-value symb))))
2329 (if (cond ((and minmax
2330 (or (not (eq min (car minmax)))
2331 (not (eq max (cdr minmax))))
2340 (set symb (cons min max))
2343 (unintern symb local)))
2344 (set (intern "+dirty" local) t))))
2346 (defun gnus-agent-article-name (article group)
2347 (expand-file-name article
2348 (file-name-as-directory
2349 (gnus-agent-group-pathname group))))
2351 (defun gnus-agent-batch-confirmation (msg)
2352 "Show error message and return t."
2353 (gnus-message 1 msg)
2357 (defun gnus-agent-batch-fetch ()
2358 "Start Gnus and fetch session."
2361 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2362 (gnus-agent-fetch-session))
2365 (defun gnus-agent-fetch-session ()
2366 "Fetch all articles and headers that are eligible for fetching."
2368 (unless gnus-agent-covered-methods
2369 (error "No servers are covered by the Gnus agent"))
2370 (unless gnus-plugged
2371 (error "Can't fetch articles while Gnus is unplugged"))
2372 (let ((methods (gnus-agent-covered-methods))
2373 groups group gnus-command-method)
2376 (setq gnus-command-method (car methods))
2377 (when (and (or (gnus-server-opened gnus-command-method)
2378 (gnus-open-server gnus-command-method))
2379 (gnus-online gnus-command-method))
2380 (setq groups (gnus-groups-from-server (car methods)))
2381 (gnus-agent-with-fetch
2382 (while (setq group (pop groups))
2383 (when (<= (gnus-group-level group)
2384 gnus-agent-handle-level)
2385 (if (or debug-on-error debug-on-quit)
2386 (gnus-agent-fetch-group-1
2387 group gnus-command-method)
2389 (gnus-agent-fetch-group-1
2390 group gnus-command-method)
2392 (unless (funcall gnus-agent-confirmation-function
2393 (format "Error %s while fetching session. Should gnus continue? "
2394 (error-message-string err)))
2395 (error "Cannot fetch articles into the Gnus agent")))
2397 (gnus-agent-regenerate-group group)
2398 (unless (funcall gnus-agent-confirmation-function
2400 "%s while fetching session. Should gnus continue? "
2401 (error-message-string err)))
2403 "Cannot fetch articles into the Gnus agent")))))))))
2404 (setq methods (cdr methods)))
2405 (gnus-run-hooks 'gnus-agent-fetched-hook)
2406 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2408 (defun gnus-agent-fetch-group-1 (group method)
2410 (let ((gnus-command-method method)
2411 (gnus-newsgroup-name group)
2412 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2413 (gnus-newsgroup-headers gnus-newsgroup-headers)
2414 (gnus-newsgroup-scored gnus-newsgroup-scored)
2415 (gnus-use-cache gnus-use-cache)
2416 (gnus-summary-expunge-below gnus-summary-expunge-below)
2417 (gnus-summary-mark-below gnus-summary-mark-below)
2418 (gnus-orphan-score gnus-orphan-score)
2419 ;; Maybe some other gnus-summary local variables should also
2425 category predicate info marks score-param
2427 (unless (gnus-check-group group)
2428 (error "Can't open server for %s" group))
2431 (when (or gnus-newsgroup-active
2433 (gnus-activate-group group))
2434 (let ((marked-articles gnus-newsgroup-downloadable))
2435 ;; Identify the articles marked for download
2436 (unless gnus-newsgroup-active
2437 ;; The variable gnus-newsgroup-active was selected as I need
2438 ;; a gnus-summary local variable that is NOT bound to any
2439 ;; value (its global value should default to nil).
2440 (dolist (mark gnus-agent-download-marks)
2441 (let ((arts (cdr (assq mark (gnus-info-marks
2442 (setq info (gnus-get-info group)))))))
2444 (setq marked-articles (nconc (gnus-uncompress-range arts)
2447 (setq marked-articles (sort marked-articles '<))
2449 ;; Fetch any new articles from the server
2450 (setq articles (gnus-agent-fetch-headers group))
2452 ;; Merge new articles with marked
2453 (setq articles (sort (append marked-articles articles) '<))
2456 ;; Parse them and see which articles we want to fetch.
2457 (setq gnus-newsgroup-dependencies
2458 (or gnus-newsgroup-dependencies
2459 (make-vector (length articles) 0)))
2460 (setq gnus-newsgroup-headers
2461 (or gnus-newsgroup-headers
2462 (gnus-get-newsgroup-headers-xover articles nil nil
2464 ;; `gnus-agent-overview-buffer' may be killed for
2465 ;; timeout reason. If so, recreate it.
2466 (gnus-agent-create-buffer)
2468 ;; Figure out how to select articles in this group
2469 (setq category (gnus-group-category group))
2473 (gnus-agent-find-parameter group 'agent-predicate)))
2475 ;; If the selection predicate requires scoring, score each header
2476 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2478 (gnus-agent-find-parameter group 'agent-score-file)))
2479 ;; Translate score-param into real one
2482 ((eq score-param 'file)
2483 (setq score-param (gnus-all-score-files group)))
2484 ((stringp (car score-param)))
2486 (setq score-param (list (list score-param)))))
2488 (gnus-score-headers score-param))))
2490 (unless (and (eq predicate 'gnus-agent-false)
2491 (not marked-articles))
2492 (let ((arts (list nil)))
2493 (let ((arts-tail arts)
2494 (alist (gnus-agent-load-alist group))
2495 (marked-articles marked-articles)
2496 (gnus-newsgroup-headers gnus-newsgroup-headers))
2497 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2498 (let ((num (mail-header-number gnus-headers)))
2499 ;; Determine if this article is already in the cache
2501 (> num (caar alist)))
2502 (setq alist (cdr alist)))
2504 (unless (and (eq num (caar alist))
2507 ;; Determine if this article was marked for download.
2508 (while (and marked-articles
2509 (> num (car marked-articles)))
2510 (setq marked-articles
2511 (cdr marked-articles)))
2513 ;; When this article is marked, or selected by the
2514 ;; predicate, add it to the download list
2515 (when (or (eq num (car marked-articles))
2518 (assq num gnus-newsgroup-scored))
2519 gnus-summary-default-score))
2520 (gnus-agent-long-article
2521 (gnus-agent-find-parameter
2522 group 'agent-long-article))
2523 (gnus-agent-short-article
2524 (gnus-agent-find-parameter
2525 group 'agent-short-article))
2526 (gnus-agent-low-score
2527 (gnus-agent-find-parameter
2528 group 'agent-low-score))
2529 (gnus-agent-high-score
2530 (gnus-agent-find-parameter
2531 group 'agent-high-score))
2532 (gnus-agent-expire-days
2533 (gnus-agent-find-parameter
2534 group 'agent-days-until-old)))
2535 (funcall predicate)))
2536 (gnus-agent-append-to-list arts-tail num))))))
2538 (let (fetched-articles)
2539 ;; Fetch all selected articles
2540 (setq gnus-newsgroup-undownloaded
2541 (gnus-sorted-ndifference
2542 gnus-newsgroup-undownloaded
2543 (setq fetched-articles
2545 (gnus-agent-fetch-articles group (cdr arts))
2548 (let ((unfetched-articles
2549 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2550 (if gnus-newsgroup-active
2551 ;; Update the summary buffer
2553 (dolist (article marked-articles)
2554 (gnus-summary-set-agent-mark article t))
2555 (dolist (article fetched-articles)
2556 (when gnus-agent-mark-unread-after-downloaded
2557 (setq gnus-newsgroup-downloadable
2558 (delq article gnus-newsgroup-downloadable))
2559 (gnus-summary-mark-article
2560 article gnus-unread-mark))
2561 (when (gnus-summary-goto-subject article nil t)
2562 (gnus-summary-update-download-mark article)))
2563 (dolist (article unfetched-articles)
2564 (gnus-summary-mark-article
2565 article gnus-canceled-mark)))
2567 ;; Update the group buffer.
2569 ;; When some, or all, of the marked articles came
2570 ;; from the download mark. Remove that mark. I
2571 ;; didn't do this earlier as I only want to remove
2572 ;; the marks after the fetch is completed.
2574 (dolist (mark gnus-agent-download-marks)
2575 (when (eq mark 'download)
2577 (assq mark (gnus-info-marks
2578 (setq info (gnus-get-info group))))))
2579 (when (cdr marked-arts)
2581 (delq marked-arts (gnus-info-marks info)))
2582 (gnus-info-set-marks info marks)))))
2583 (let ((read (gnus-info-read
2584 (or info (setq info (gnus-get-info group))))))
2586 info (gnus-add-to-range read unfetched-articles)))
2588 (gnus-group-update-group group t)
2592 (concat "(gnus-group-set-info '"
2593 (gnus-prin1-to-string info)
2597 ;;; Agent Category Mode
2600 (defvar gnus-category-mode-hook nil
2601 "Hook run in `gnus-category-mode' buffers.")
2603 (defvar gnus-category-line-format " %(%20c%): %g\n"
2604 "Format of category lines.
2606 Valid specifiers include:
2607 %c Topic name (string)
2608 %g The number of groups in the topic (integer)
2610 General format specifiers can also be used. See Info node
2611 `(gnus)Formatting Variables'.")
2613 (defvar gnus-category-mode-line-format "Gnus: %%b"
2614 "The format specification for the category mode line.")
2616 (defvar gnus-agent-predicate 'false
2617 "The selection predicate used when no other source is available.")
2619 (defvar gnus-agent-short-article 100
2620 "Articles that have fewer lines than this are short.")
2622 (defvar gnus-agent-long-article 200
2623 "Articles that have more lines than this are long.")
2625 (defvar gnus-agent-low-score 0
2626 "Articles that have a score lower than this have a low score.")
2628 (defvar gnus-agent-high-score 0
2629 "Articles that have a score higher than this have a high score.")
2632 ;;; Internal variables.
2634 (defvar gnus-category-buffer "*Agent Category*")
2636 (defvar gnus-category-line-format-alist
2637 `((?c gnus-tmp-name ?s)
2638 (?g gnus-tmp-groups ?d)))
2640 (defvar gnus-category-mode-line-format-alist
2641 `((?u user-defined ?s)))
2643 (defvar gnus-category-line-format-spec nil)
2644 (defvar gnus-category-mode-line-format-spec nil)
2646 (defvar gnus-category-mode-map nil)
2647 (put 'gnus-category-mode 'mode-class 'special)
2649 (unless gnus-category-mode-map
2650 (setq gnus-category-mode-map (make-sparse-keymap))
2651 (suppress-keymap gnus-category-mode-map)
2653 (gnus-define-keys gnus-category-mode-map
2654 "q" gnus-category-exit
2655 "k" gnus-category-kill
2656 "c" gnus-category-copy
2657 "a" gnus-category-add
2658 "e" gnus-agent-customize-category
2659 "p" gnus-category-edit-predicate
2660 "g" gnus-category-edit-groups
2661 "s" gnus-category-edit-score
2662 "l" gnus-category-list
2664 "\C-c\C-i" gnus-info-find-node
2665 "\C-c\C-b" gnus-bug))
2667 (defvar gnus-category-menu-hook nil
2668 "*Hook run after the creation of the menu.")
2670 (defun gnus-category-make-menu-bar ()
2671 (gnus-turn-off-edit-menu 'category)
2672 (unless (boundp 'gnus-category-menu)
2674 gnus-category-menu gnus-category-mode-map ""
2676 ["Add" gnus-category-add t]
2677 ["Kill" gnus-category-kill t]
2678 ["Copy" gnus-category-copy t]
2679 ["Edit category" gnus-agent-customize-category t]
2680 ["Edit predicate" gnus-category-edit-predicate t]
2681 ["Edit score" gnus-category-edit-score t]
2682 ["Edit groups" gnus-category-edit-groups t]
2683 ["Exit" gnus-category-exit t]))
2685 (gnus-run-hooks 'gnus-category-menu-hook)))
2687 (defun gnus-category-mode ()
2688 "Major mode for listing and editing agent categories.
2690 All normal editing commands are switched off.
2691 \\<gnus-category-mode-map>
2692 For more in-depth information on this mode, read the manual
2693 \(`\\[gnus-info-find-node]').
2695 The following commands are available:
2697 \\{gnus-category-mode-map}"
2699 (when (gnus-visual-p 'category-menu 'menu)
2700 (gnus-category-make-menu-bar))
2701 (kill-all-local-variables)
2702 (gnus-simplify-mode-line)
2703 (setq major-mode 'gnus-category-mode)
2704 (setq mode-name "Category")
2705 (gnus-set-default-directory)
2706 (setq mode-line-process nil)
2707 (use-local-map gnus-category-mode-map)
2708 (buffer-disable-undo)
2709 (setq truncate-lines t)
2710 (setq buffer-read-only t)
2711 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2713 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2715 (defun gnus-category-insert-line (category)
2716 (let* ((gnus-tmp-name (format "%s" (car category)))
2717 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2719 (gnus-add-text-properties
2723 (eval gnus-category-line-format-spec))
2724 (list 'gnus-category gnus-tmp-name))))
2726 (defun gnus-enter-category-buffer ()
2727 "Go to the Category buffer."
2729 (gnus-category-setup-buffer)
2730 (gnus-configure-windows 'category)
2731 (gnus-category-prepare))
2733 (defun gnus-category-setup-buffer ()
2734 (unless (get-buffer gnus-category-buffer)
2736 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2737 (gnus-category-mode))))
2739 (defun gnus-category-prepare ()
2740 (gnus-set-format 'category-mode)
2741 (gnus-set-format 'category t)
2742 (let ((alist gnus-category-alist)
2743 (buffer-read-only nil))
2746 (gnus-category-insert-line (pop alist)))
2747 (goto-char (point-min))
2748 (gnus-category-position-point)))
2750 (defun gnus-category-name ()
2751 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2752 (error "No category on the current line")))
2754 (defun gnus-category-read ()
2755 "Read the category alist."
2756 (setq gnus-category-alist
2760 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2761 (goto-char (point-min))
2762 ;; This code isn't temp, it will be needed so long as
2763 ;; anyone may be migrating from an older version.
2765 ;; Once we're certain that people will not revert to an
2766 ;; earlier version, we can take out the old-list code in
2767 ;; gnus-category-write.
2768 (let* ((old-list (read (current-buffer)))
2769 (new-list (ignore-errors (read (current-buffer)))))
2772 ;; Convert from a positional list to an alist.
2782 '(agent-predicate agent-score-file agent-groups))))
2785 (list (gnus-agent-cat-make 'default 'short)))))
2787 (defun gnus-category-write ()
2788 "Write the category alist."
2789 (setq gnus-category-predicate-cache nil
2790 gnus-category-group-cache nil)
2791 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2792 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2793 ;; This prin1 is temporary. It exists so that people can revert
2794 ;; to an earlier version of gnus-agent.
2795 (prin1 (mapcar (lambda (c)
2797 (cdr (assoc 'agent-predicate c))
2798 (cdr (assoc 'agent-score-file c))
2799 (cdr (assoc 'agent-groups c))))
2800 gnus-category-alist)
2803 (prin1 gnus-category-alist (current-buffer))))
2805 (defun gnus-category-edit-predicate (category)
2806 "Edit the predicate for CATEGORY."
2807 (interactive (list (gnus-category-name)))
2808 (let ((info (assq category gnus-category-alist)))
2810 (gnus-agent-cat-predicate info)
2811 (format "Editing the select predicate for category %s" category)
2812 `(lambda (predicate)
2813 ;; Avoid run-time execution of setf form
2814 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2816 ;; use its expansion instead:
2817 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2818 'agent-predicate predicate)
2820 (gnus-category-write)
2821 (gnus-category-list)))))
2823 (defun gnus-category-edit-score (category)
2824 "Edit the score expression for CATEGORY."
2825 (interactive (list (gnus-category-name)))
2826 (let ((info (assq category gnus-category-alist)))
2828 (gnus-agent-cat-score-file info)
2829 (format "Editing the score expression for category %s" category)
2830 `(lambda (score-file)
2831 ;; Avoid run-time execution of setf form
2832 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2834 ;; use its expansion instead:
2835 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2836 'agent-score-file score-file)
2838 (gnus-category-write)
2839 (gnus-category-list)))))
2841 (defun gnus-category-edit-groups (category)
2842 "Edit the group list for CATEGORY."
2843 (interactive (list (gnus-category-name)))
2844 (let ((info (assq category gnus-category-alist)))
2846 (gnus-agent-cat-groups info)
2847 (format "Editing the group list for category %s" category)
2849 ;; Avoid run-time execution of setf form
2850 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2852 ;; use its expansion instead:
2853 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2856 (gnus-category-write)
2857 (gnus-category-list)))))
2859 (defun gnus-category-kill (category)
2860 "Kill the current category."
2861 (interactive (list (gnus-category-name)))
2862 (let ((info (assq category gnus-category-alist))
2863 (buffer-read-only nil))
2865 (setq gnus-category-alist (delq info gnus-category-alist))
2866 (gnus-category-write)))
2868 (defun gnus-category-copy (category to)
2869 "Copy the current category."
2870 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2871 (let ((info (assq category gnus-category-alist)))
2872 (push (let ((newcat (gnus-copy-sequence info)))
2873 (setf (gnus-agent-cat-name newcat) to)
2874 (setf (gnus-agent-cat-groups newcat) nil)
2876 gnus-category-alist)
2877 (gnus-category-write)
2878 (gnus-category-list)))
2880 (defun gnus-category-add (category)
2881 "Create a new category."
2882 (interactive "SCategory name: ")
2883 (when (assq category gnus-category-alist)
2884 (error "Category %s already exists" category))
2885 (push (gnus-agent-cat-make category)
2886 gnus-category-alist)
2887 (gnus-category-write)
2888 (gnus-category-list))
2890 (defun gnus-category-list ()
2891 "List all categories."
2893 (gnus-category-prepare))
2895 (defun gnus-category-exit ()
2896 "Return to the group buffer."
2898 (kill-buffer (current-buffer))
2899 (gnus-configure-windows 'group t))
2901 ;; To avoid having 8-bit characters in the source file.
2902 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2904 (defvar gnus-category-predicate-alist
2905 '((spam . gnus-agent-spam-p)
2906 (short . gnus-agent-short-p)
2907 (long . gnus-agent-long-p)
2908 (low . gnus-agent-low-scored-p)
2909 (high . gnus-agent-high-scored-p)
2910 (read . gnus-agent-read-p)
2911 (true . gnus-agent-true)
2912 (false . gnus-agent-false))
2913 "Mapping from short score predicate symbols to predicate functions.")
2915 (defun gnus-agent-spam-p ()
2916 "Say whether an article is spam or not."
2917 (unless gnus-agent-spam-hashtb
2918 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2919 (if (not (equal (mail-header-references gnus-headers) ""))
2921 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2923 (gnus-gethash string gnus-agent-spam-hashtb)
2924 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2926 (defun gnus-agent-short-p ()
2927 "Say whether an article is short or not."
2928 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2930 (defun gnus-agent-long-p ()
2931 "Say whether an article is long or not."
2932 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2934 (defun gnus-agent-low-scored-p ()
2935 "Say whether an article has a low score or not."
2936 (< gnus-score gnus-agent-low-score))
2938 (defun gnus-agent-high-scored-p ()
2939 "Say whether an article has a high score or not."
2940 (> gnus-score gnus-agent-high-score))
2942 (defun gnus-agent-read-p ()
2943 "Say whether an article is read or not."
2944 (gnus-member-of-range (mail-header-number gnus-headers)
2945 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2947 (defun gnus-category-make-function (predicate)
2948 "Make a function from PREDICATE."
2949 (let ((func (gnus-category-make-function-1 predicate)))
2950 (if (and (= (length func) 1)
2951 (symbolp (car func)))
2953 (gnus-byte-compile `(lambda () ,func)))))
2955 (defun gnus-agent-true ()
2959 (defun gnus-agent-false ()
2963 (defun gnus-category-make-function-1 (predicate)
2964 "Make a function from PREDICATE."
2966 ;; Functions are just returned as is.
2967 ((or (symbolp predicate)
2968 (functionp predicate))
2969 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2971 ;; More complex predicate.
2974 ((memq (car predicate) '(& and))
2976 ((memq (car predicate) '(| or))
2978 ((memq (car predicate) gnus-category-not)
2980 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2982 (error "Unknown predicate type: %s" predicate))))
2984 (defun gnus-get-predicate (predicate)
2985 "Return the function implementing PREDICATE."
2986 (or (cdr (assoc predicate gnus-category-predicate-cache))
2987 (let ((func (gnus-category-make-function predicate)))
2988 (setq gnus-category-predicate-cache
2989 (nconc gnus-category-predicate-cache
2990 (list (cons predicate func))))
2993 (defun gnus-predicate-implies-unread (predicate)
2994 "Say whether PREDICATE implies unread articles only.
2995 It is okay to miss some cases, but there must be no false positives.
2996 That is, if this predicate returns true, then indeed the predicate must
2997 return only unread articles."
2998 (eq t (gnus-function-implies-unread-1
2999 (gnus-category-make-function-1 predicate))))
3001 (defun gnus-function-implies-unread-1 (function)
3002 "Recursively evaluate a predicate function to determine whether it can select
3003 any read articles. Returns t if the function is known to never
3004 return read articles, nil when it is known to always return read
3005 articles, and t_nil when the function may return both read and unread
3007 (let ((func (car function))
3008 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
3009 (cond ((eq func 'and)
3010 (cond ((memq t args) ; if any argument returns only unread articles
3011 ;; then that argument constrains the result to only unread articles.
3013 ((memq 't_nil args) ; if any argument is indeterminate
3014 ;; then the result is indeterminate
3017 (cond ((memq nil args) ; if any argument returns read articles
3018 ;; then that argument ensures that the results includes read articles.
3020 ((memq 't_nil args) ; if any argument is indeterminate
3021 ;; then that argument ensures that the results are indeterminate
3023 (t ; if all arguments return only unread articles
3024 ;; then the result returns only unread articles
3027 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
3028 ; then the result is indeterminate
3031 ; toggle the result to be the opposite of the argument
3033 ((eq func 'gnus-agent-read-p)
3034 nil) ; The read predicate NEVER returns unread articles
3035 ((eq func 'gnus-agent-false)
3036 t) ; The false predicate returns t as the empty set excludes all read articles
3037 ((eq func 'gnus-agent-true)
3038 nil) ; The true predicate ALWAYS returns read articles
3039 ((catch 'found-match
3040 (let ((alist gnus-category-predicate-alist))
3042 (if (eq func (cdar alist))
3043 (throw 'found-match t)
3044 (setq alist (cdr alist))))))
3045 't_nil) ; All other predicates return read and unread articles
3047 (error "Unknown predicate function: %s" function)))))
3049 (defun gnus-group-category (group)
3050 "Return the category GROUP belongs to."
3051 (unless gnus-category-group-cache
3052 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
3053 (let ((cs gnus-category-alist)
3055 (while (setq cat (pop cs))
3056 (setq groups (gnus-agent-cat-groups cat))
3058 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
3059 (or (gnus-gethash group gnus-category-group-cache)
3060 (assq 'default gnus-category-alist)))
3062 (defun gnus-agent-expire-group (group &optional articles force)
3063 "Expire all old articles in GROUP.
3064 If you want to force expiring of certain articles, this function can
3065 take ARTICLES, and FORCE parameters as well.
3067 The articles on which the expiration process runs are selected as follows:
3068 if ARTICLES is null, all read and unmarked articles.
3069 if ARTICLES is t, all articles.
3070 if ARTICLES is a list, just those articles.
3071 FORCE is equivalent to setting the expiration predicates to true."
3073 (list (let ((def (or (gnus-group-group-name)
3074 gnus-newsgroup-name)))
3075 (let ((select (read-string (if def
3076 (concat "Group Name ("
3079 (if (and (equal "" select)
3085 (gnus-agent-expire articles group force)
3086 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
3087 ;; expiration statistics of this single group
3088 (gnus-agent-expire-stats (list 0 0 0.0)))
3089 (if (or (not (eq articles t))
3091 (concat "Are you sure that you want to "
3092 "expire all articles in " group "? ")))
3093 (let ((gnus-command-method (gnus-find-method-for-group group))
3094 (overview (gnus-get-buffer-create " *expire overview*"))
3097 (let ((active-file (gnus-agent-lib-file "active")))
3098 (when (file-exists-p active-file)
3100 (nnheader-insert-file-contents active-file)
3101 (gnus-active-to-gnus-format
3103 (setq orig (gnus-make-hashtable
3104 (count-lines (point-min) (point-max))))))
3106 (gnus-agent-expire-group-1
3107 group overview (gnus-gethash-safe group orig)
3109 (kill-buffer overview))))
3110 (gnus-message 4 (gnus-agent-expire-done-message)))))
3112 (defun gnus-agent-expire-group-1 (group overview active articles force)
3113 ;; Internal function - requires caller to have set
3114 ;; gnus-command-method, initialized overview buffer, and to have
3115 ;; provided a non-nil active
3117 (let ((dir (gnus-agent-group-pathname group)))
3118 (gnus-agent-with-refreshed-group
3120 (when (boundp 'gnus-agent-expire-current-dirs)
3121 (set 'gnus-agent-expire-current-dirs
3123 (symbol-value 'gnus-agent-expire-current-dirs))))
3125 (if (and (not force)
3126 (eq 'DISABLE (gnus-agent-find-parameter group
3127 'agent-enable-expiration)))
3128 (gnus-message 5 "Expiry skipping over %s" group)
3129 (gnus-message 5 "Expiring articles in %s" group)
3130 (gnus-agent-load-alist group)
3131 (let* ((bytes-freed 0)
3132 (size-files-deleted 0.0)
3134 (nov-entries-deleted 0)
3135 (info (gnus-get-info group))
3136 (alist gnus-agent-article-alist)
3137 (day (- (time-to-days (current-time))
3138 (gnus-agent-find-parameter group 'agent-days-until-old)))
3139 (specials (if (and alist
3141 ;; This could be a bit of a problem. I need to
3142 ;; keep the last article to avoid refetching
3143 ;; headers when using nntp in the backend. At
3144 ;; the same time, if someone uses a backend
3145 ;; that supports article moving then I may have
3146 ;; to remove the last article to complete the
3147 ;; move. Right now, I'm going to assume that
3148 ;; FORCE overrides specials.
3149 (list (caar (last alist)))))
3150 (unreads ;; Articles that are excluded from the
3151 ;; expiration process
3152 (cond (gnus-agent-expire-all
3153 ;; All articles are marked read by global decree
3156 ;; All articles are marked read by function
3160 ;; Unread articles are marked protected from
3161 ;; expiration Don't call
3162 ;; gnus-list-of-unread-articles as it returns
3163 ;; articles that have not been fetched into the
3166 (gnus-agent-unread-articles group)))
3168 ;; All articles EXCEPT those named by the caller
3169 ;; are protected from expiration
3170 (gnus-sorted-difference
3171 (gnus-uncompress-range
3173 (caar (last alist))))
3174 (sort articles '<)))))
3175 (marked ;; More articles that are excluded from the
3176 ;; expiration process
3177 (cond (gnus-agent-expire-all
3178 ;; All articles are unmarked by global decree
3181 ;; All articles are unmarked by function
3185 ;; All articles may as well be unmarked as the
3186 ;; unreads list already names the articles we are
3190 ;; Ticked and/or dormant articles are excluded
3193 (gnus-uncompress-range
3194 (cdr (assq 'tick (gnus-info-marks info))))
3195 (gnus-uncompress-range
3197 (gnus-info-marks info))))))))
3198 (nov-file (concat dir ".overview"))
3204 ;; The normal article alist contains elements that look like
3205 ;; (article# . fetch_date) I need to combine other
3206 ;; information with this list. For example, a flag indicating
3207 ;; that a particular article MUST BE KEPT. To do this, I'm
3208 ;; going to transform the elements to look like (article#
3209 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3210 ;; the process to generate the expired article alist.
3212 ;; Convert the alist elements to (article# fetch_date nil
3214 (setq dlist (mapcar (lambda (e)
3215 (list (car e) (cdr e) nil nil)) alist))
3217 ;; Convert the keep lists to elements that look like (article#
3218 ;; nil keep_flag nil) then append it to the expanded dlist
3219 ;; These statements are sorted by ascending precidence of the
3221 (setq dlist (nconc dlist
3223 (list e nil 'unread nil))
3225 (setq dlist (nconc dlist
3227 (list e nil 'marked nil))
3229 (setq dlist (nconc dlist
3231 (list e nil 'special nil))
3234 (set-buffer overview)
3236 (buffer-disable-undo)
3237 (when (file-exists-p nov-file)
3238 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3239 (nnheader-insert-file-contents nov-file)
3240 (goto-char (point-min))
3243 (while (< (setq p (point)) (point-max))
3245 ;; If I successfully read an integer (the plus zero
3246 ;; ensures a numeric type), append the position
3248 (push (list (+ 0 (read (current-buffer))) nil nil
3252 (gnus-message 1 "gnus-agent-expire: read error \
3253 occurred when reading expression at %s in %s. Skipping to next \
3254 line." (point) nov-file)))
3255 ;; Whether I succeeded, or failed, it doesn't matter.
3256 ;; Move to the next line then try again.
3260 7 "gnus-agent-expire: Loading overview... Done"))
3261 (set-buffer-modified-p nil)
3263 ;; At this point, all of the information is in dlist. The
3264 ;; only problem is that much of it is spread across multiple
3265 ;; entries. Sort then MERGE!!
3266 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3267 ;; If two entries have the same article-number then sort by
3268 ;; ascending keep_flag.
3275 (cond ((< (nth 0 a) (nth 0 b))
3277 ((> (nth 0 a) (nth 0 b))
3280 (let ((a (or (symbol-value (nth 2 a))
3282 (b (or (symbol-value (nth 2 b))
3285 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3286 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3287 (let ((dlist dlist))
3288 (while (cdr dlist) ; I'm not at the end-of-list
3289 (if (eq (caar dlist) (caadr dlist))
3290 (let ((first (cdr (car dlist)))
3291 (secnd (cdr (cadr dlist))))
3292 (setcar first (or (car first)
3293 (car secnd))) ; fetch_date
3294 (setq first (cdr first)
3296 (setcar first (or (car first)
3297 (car secnd))) ; Keep_flag
3298 (setq first (cdr first)
3300 (setcar first (or (car first)
3301 (car secnd))) ; NOV_entry_position
3303 (setcdr dlist (cddr dlist)))
3304 (setq dlist (cdr dlist)))))
3306 ;; Check the order of the entry positions. They should be in
3307 ;; ascending order. If they aren't, the positions must be
3308 ;; converted to markers.
3309 (when (let ((dlist dlist)
3313 (if (setq pos (nth 3 (pop dlist)))
3314 (if (< pos prev-pos)
3315 (throw 'sort-results 'unsorted)
3316 (setq prev-pos pos)))))
3317 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3318 (mapcar (lambda (entry)
3319 (let ((pos (nth 3 entry)))
3322 (set-marker (make-marker)
3326 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3328 (let* ((len (float (length dlist)))
3335 (let ((new-completed (truncate (* 100.0
3336 (/ (setq cnt (1+ cnt))
3339 (when (> new-completed completed)
3340 (setq completed new-completed)
3341 (gnus-message 7 "%3d%% completed..." completed)))
3342 (let* ((entry (car dlist))
3343 (article-number (nth 0 entry))
3344 (fetch-date (nth 1 entry))
3345 (keep (nth 2 entry))
3346 (marker (nth 3 entry)))
3349 ;; Kept articles are unread, marked, or special.
3351 (gnus-agent-message 10
3352 "gnus-agent-expire: %s:%d: Kept %s article%s."
3353 group article-number keep (if fetch-date " and file" ""))
3355 (unless (file-exists-p
3356 (concat dir (number-to-string
3358 (setf (nth 1 entry) nil)
3359 (gnus-agent-message 3 "gnus-agent-expire cleared \
3360 download flag on %s:%d as the cached article file is missing."
3361 group (caar dlist)))
3363 (gnus-message 1 "gnus-agent-expire detected a \
3364 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3365 (gnus-agent-append-to-list
3367 (cons article-number fetch-date)))
3369 ;; The following articles are READ, UNMARKED, and
3370 ;; ORDINARY. See if they can be EXPIRED!!!
3373 ((not (integerp fetch-date))
3374 'read) ;; never fetched article (may expire
3376 ((not (file-exists-p
3377 (concat dir (number-to-string
3379 (setf (nth 1 entry) nil)
3380 'externally-expired) ;; Can't find the cached
3381 ;; article. Handle case
3382 ;; as though this article
3383 ;; was never fetched.
3385 ;; We now have the arrival day, so we see
3386 ;; whether it's old enough to be expired.
3392 ;; I found some reason to expire this entry.
3394 (let ((actions nil))
3395 (when (memq type '(forced expired))
3396 (ignore-errors ; Just being paranoid.
3397 (let* ((file-name (nnheader-concat dir (number-to-string
3399 (size (float (nth 7 (file-attributes file-name)))))
3400 (incf bytes-freed size)
3401 (incf size-files-deleted size)
3402 (incf files-deleted)
3403 (delete-file file-name))
3404 (push "expired cached article" actions))
3405 (setf (nth 1 entry) nil)
3409 (push "NOV entry removed" actions)
3411 (goto-char (if (markerp marker)
3413 (- marker position-offset)))
3415 (incf nov-entries-deleted)
3417 (let* ((from (point-at-bol))
3418 (to (progn (forward-line 1) (point)))
3419 (freed (- to from)))
3420 (incf bytes-freed freed)
3421 (incf position-offset freed)
3422 (delete-region from to)))
3424 ;; If considering all articles is set, I can only
3425 ;; expire article IDs that are no longer in the
3426 ;; active range (That is, articles that preceed the
3427 ;; first article in the new alist).
3428 (if (and gnus-agent-consider-all-articles
3429 (>= article-number (car active)))
3430 ;; I have to keep this ID in the alist
3431 (gnus-agent-append-to-list
3432 tail-alist (cons article-number fetch-date))
3433 (push (format "Removed %s article number from \
3434 article alist" type) actions))
3437 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3438 group article-number
3439 (mapconcat 'identity actions ", ")))))
3442 10 "gnus-agent-expire: %s:%d: Article kept as \
3443 expiration tests failed." group article-number)
3444 (gnus-agent-append-to-list
3445 tail-alist (cons article-number fetch-date)))
3448 ;; Remove markers as I intend to reuse this buffer again.
3451 (set-marker marker nil))
3453 (setq dlist (cdr dlist))))
3455 (setq alist (cdr alist))
3457 (let ((inhibit-quit t))
3458 (unless (equal alist gnus-agent-article-alist)
3459 (setq gnus-agent-article-alist alist)
3460 (gnus-agent-save-alist group))
3462 (when (buffer-modified-p)
3463 (let ((coding-system-for-write
3464 gnus-agent-file-coding-system))
3465 (gnus-make-directory dir)
3466 (write-region (point-min) (point-max) nov-file nil
3468 ;; clear the modified flag as that I'm not confused by
3469 ;; its status on the next pass through this routine.
3470 (set-buffer-modified-p nil)
3471 (gnus-agent-update-view-total-fetched-for group t)))
3473 (when (eq articles t)
3474 (gnus-summary-update-info))))
3476 (when (boundp 'gnus-agent-expire-stats)
3477 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3478 (incf (nth 2 stats) bytes-freed)
3479 (incf (nth 1 stats) files-deleted)
3480 (incf (nth 0 stats) nov-entries-deleted)))
3482 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3484 (defun gnus-agent-expire (&optional articles group force)
3485 "Expire all old articles.
3486 If you want to force expiring of certain articles, this function can
3487 take ARTICLES, GROUP and FORCE parameters as well.
3489 The articles on which the expiration process runs are selected as follows:
3490 if ARTICLES is null, all read and unmarked articles.
3491 if ARTICLES is t, all articles.
3492 if ARTICLES is a list, just those articles.
3493 Setting GROUP will limit expiration to that group.
3494 FORCE is equivalent to setting the expiration predicates to true."
3498 (gnus-agent-expire-group group articles force)
3499 (if (or (not (eq articles t))
3500 (yes-or-no-p "Are you sure that you want to expire all \
3501 articles in every agentized group? "))
3502 (let ((methods (gnus-agent-covered-methods))
3503 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3504 ;; of agent directories.
3505 (gnus-agent-expire-current-dirs nil)
3506 ;; Bind gnus-agent-expire-stats to enable tracking of
3507 ;; expiration statistics across all groups
3508 (gnus-agent-expire-stats (list 0 0 0.0))
3509 gnus-command-method overview orig)
3510 (setq overview (gnus-get-buffer-create " *expire overview*"))
3512 (while (setq gnus-command-method (pop methods))
3513 (let ((active-file (gnus-agent-lib-file "active")))
3514 (when (file-exists-p active-file)
3516 (nnheader-insert-file-contents active-file)
3517 (gnus-active-to-gnus-format
3519 (setq orig (gnus-make-hashtable
3520 (count-lines (point-min) (point-max))))))
3521 (dolist (expiring-group (gnus-groups-from-server
3522 gnus-command-method))
3524 (gnus-gethash-safe expiring-group orig)))
3528 (gnus-agent-expire-group-1
3529 expiring-group overview active articles force))))))))
3530 (kill-buffer overview))
3531 (gnus-agent-expire-unagentized-dirs)
3532 (gnus-message 4 (gnus-agent-expire-done-message))))))
3534 (defun gnus-agent-expire-done-message ()
3535 (if (and (> gnus-verbose 4)
3536 (boundp 'gnus-agent-expire-stats))
3537 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3538 (size (nth 2 stats))
3539 (units '(B KB MB GB)))
3540 (while (and (> size 1024.0)
3542 (setq size (/ size 1024.0)
3545 (format "Expiry recovered %d NOV entries, deleted %d files,\
3552 (defun gnus-agent-expire-unagentized-dirs ()
3553 (when (and gnus-agent-expire-unagentized-dirs
3554 (boundp 'gnus-agent-expire-current-dirs))
3555 (let* ((keep (gnus-make-hashtable))
3556 ;; Formally bind gnus-agent-expire-current-dirs so that the
3557 ;; compiler will not complain about free references.
3558 (gnus-agent-expire-current-dirs
3559 (symbol-value 'gnus-agent-expire-current-dirs))
3562 (gnus-sethash gnus-agent-directory t keep)
3563 (while gnus-agent-expire-current-dirs
3564 (setq dir (pop gnus-agent-expire-current-dirs))
3565 (when (and (stringp dir)
3566 (file-directory-p dir))
3567 (while (not (gnus-gethash dir keep))
3568 (gnus-sethash dir t keep)
3569 (setq dir (file-name-directory (directory-file-name dir))))))
3576 "Given a directory, check it and its subdirectories for
3577 membership in the keep hash. If it isn't found, add
3579 (let ((files (directory-files d))
3581 (while (setq file (pop files))
3582 (cond ((equal file ".") ; Ignore self
3584 ((equal file "..") ; Ignore parent
3586 ((equal file ".overview")
3587 ;; Directory must contain .overview to be
3588 ;; agent's cache of a group.
3589 (let ((d (file-name-as-directory d))
3591 ;; Search ancestor's for last directory NOT
3592 ;; found in keep hash.
3593 (while (not (gnus-gethash
3594 (setq d (file-name-directory d)) keep))
3596 d (directory-file-name d)))
3597 ;; if ANY ancestor was NOT in keep hash and
3598 ;; it it's already in to-remove, add it to
3601 (not (member r to-remove)))
3602 (push r to-remove))))
3603 ((file-directory-p (setq file (nnheader-concat d file)))
3604 (funcall checker file)))))))))
3605 (funcall checker (expand-file-name gnus-agent-directory))
3607 (when (and to-remove
3608 (or gnus-expert-user
3610 "gnus-agent-expire has identified local directories that are\
3611 not currently required by any agentized group. Do you wish to consider\
3614 (let ((dir (pop to-remove)))
3615 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3616 (let* (delete-recursive
3621 (if (file-directory-p f-or-d)
3623 (delete-directory f-or-d)
3626 (or (member f '("." ".."))
3627 (funcall delete-recursive
3630 (directory-files f-or-d))
3631 (delete-directory f-or-d)))
3632 (delete-file f-or-d)))))))
3633 (funcall delete-recursive dir))))))))))
3636 (defun gnus-agent-batch ()
3637 "Start Gnus, send queue and fetch session."
3639 (let ((init-file-user "")
3640 (gnus-always-read-dribble-file t))
3642 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3643 (gnus-group-send-queue)
3644 (gnus-agent-fetch-session)))
3646 (defun gnus-agent-unread-articles (group)
3647 (let* ((read (gnus-info-read (gnus-get-info group)))
3648 (known (gnus-agent-load-alist group))
3650 (tail-unread unread))
3651 (while (and known read)
3652 (let ((candidate (car (pop known))))
3653 (while (let* ((range (car read))
3654 (min (if (numberp range) range (car range)))
3655 (max (if (numberp range) range (cdr range))))
3656 (cond ((or (not min)
3658 (gnus-agent-append-to-list tail-unread candidate)
3661 (setq read (cdr read))
3662 ;; return t so that I always loop one more
3663 ;; time. If I just iterated off the end of
3664 ;; read, min will become nil and the current
3665 ;; candidate will be added to the unread list.
3668 (gnus-agent-append-to-list tail-unread (car (pop known))))
3671 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3672 "Restrict ARTICLES to numbers already fetched.
3673 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3674 that have already been fetched.
3675 If CACHED-HEADER is nil, articles are only excluded if the article itself
3678 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3679 ;; 'car gnus-agent-article-alist))
3681 ;; Functionally, I don't need to construct a temp list using mapcar.
3683 (if (and (or gnus-agent-cache (not gnus-plugged))
3684 (gnus-agent-load-alist group))
3685 (let* ((ref gnus-agent-article-alist)
3687 (uncached (list nil))
3688 (tail-uncached uncached))
3689 (while (and ref arts)
3690 (let ((v1 (car arts))
3692 (cond ((< v1 v2) ; v1 does not appear in the reference list
3693 (gnus-agent-append-to-list tail-uncached v1)
3694 (setq arts (cdr arts)))
3696 (unless (or cached-header (cdar ref)) ; v1 is already cached
3697 (gnus-agent-append-to-list tail-uncached v1))
3698 (setq arts (cdr arts))
3699 (setq ref (cdr ref)))
3700 (t ; reference article (v2) preceeds the list being filtered
3701 (setq ref (cdr ref))))))
3703 (gnus-agent-append-to-list tail-uncached (pop arts)))
3705 ;; if gnus-agent-load-alist fails, no articles are cached.
3708 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3710 (gnus-agent-create-buffer)
3711 (let ((gnus-decode-encoded-word-function 'identity)
3712 (file (gnus-agent-article-name ".overview" group))
3713 cached-articles uncached-articles)
3714 (gnus-make-directory (nnheader-translate-file-chars
3715 (file-name-directory file) t))
3717 ;; Populate temp buffer with known headers
3718 (when (file-exists-p file)
3719 (with-current-buffer gnus-agent-overview-buffer
3721 (let ((nnheader-file-coding-system
3722 gnus-agent-file-coding-system))
3723 (nnheader-insert-nov-file file (car articles)))))
3725 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3728 ;; Populate nntp-server-buffer with uncached headers
3729 (set-buffer nntp-server-buffer)
3731 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3732 (gnus-retrieve-headers
3733 uncached-articles group fetch-old))))
3734 (nnvirtual-convert-headers))
3735 ((eq 'nntp (car gnus-current-select-method))
3736 ;; The author of gnus-get-newsgroup-headers-xover
3737 ;; reports that the XOVER command is commonly
3738 ;; unreliable. The problem is that recently
3739 ;; posted articles may not be entered into the
3740 ;; NOV database in time to respond to my XOVER
3743 ;; I'm going to use his assumption that the NOV
3744 ;; database is updated in order of ascending
3745 ;; article ID. Therefore, a response containing
3746 ;; article ID N implies that all articles from 1
3747 ;; to N-1 are up-to-date. Therefore, missing
3748 ;; articles in that range have expired.
3750 (set-buffer nntp-server-buffer)
3751 (let* ((fetched-articles (list nil))
3752 (tail-fetched-articles fetched-articles)
3753 (min (cond ((numberp fetch-old)
3754 (max 1 (- (car articles) fetch-old)))
3759 (max (car (last articles))))
3761 ;; Get the list of articles that were fetched
3762 (goto-char (point-min))
3763 (let ((pm (point-max))
3765 (while (< (point) pm)
3766 (when (setq art (gnus-agent-read-article-number))
3767 (gnus-agent-append-to-list tail-fetched-articles art))
3770 ;; Clip this list to the headers that will
3771 ;; actually be returned
3772 (setq fetched-articles (gnus-list-range-intersection
3773 (cdr fetched-articles)
3776 ;; Clip the uncached articles list to exclude
3777 ;; IDs after the last FETCHED header. The
3778 ;; excluded IDs may be fetchable using HEAD.
3779 (if (car tail-fetched-articles)
3780 (setq uncached-articles
3781 (gnus-list-range-intersection
3783 (cons (car uncached-articles)
3784 (car tail-fetched-articles)))))
3786 ;; Create the list of articles that were
3787 ;; "successfully" fetched. Success, in this
3788 ;; case, means that the ID should not be
3789 ;; fetched again. In the case of an expired
3790 ;; article, the header will not be fetched.
3791 (setq uncached-articles
3792 (gnus-sorted-nunion fetched-articles
3796 ;; Erase the temp buffer
3797 (set-buffer gnus-agent-overview-buffer)
3800 ;; Copy the nntp-server-buffer to the temp buffer
3801 (set-buffer nntp-server-buffer)
3802 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3804 ;; Merge the temp buffer with the known headers (found on
3805 ;; disk in FILE) into the nntp-server-buffer
3806 (when uncached-articles
3807 (gnus-agent-braid-nov group uncached-articles file))
3809 ;; Save the new set of known headers to FILE
3810 (set-buffer nntp-server-buffer)
3811 (let ((coding-system-for-write
3812 gnus-agent-file-coding-system))
3813 (gnus-agent-check-overview-buffer)
3814 (write-region (point-min) (point-max) file nil 'silent))
3816 (gnus-agent-update-view-total-fetched-for group t)
3818 ;; Update the group's article alist to include the newly
3819 ;; fetched articles.
3820 (gnus-agent-load-alist group)
3821 (gnus-agent-save-alist group uncached-articles nil)
3824 ;; Copy the temp buffer to the nntp-server-buffer
3825 (set-buffer nntp-server-buffer)
3827 (insert-buffer-substring gnus-agent-overview-buffer)))
3830 (not (numberp fetch-old)))
3831 t ; Don't remove anything.
3832 (nnheader-nov-delete-outside-range
3833 (if fetch-old (max 1 (- (car articles) fetch-old))
3835 (car (last articles)))
3840 (defun gnus-agent-request-article (article group)
3841 "Retrieve ARTICLE in GROUP from the agent cache."
3842 (when (and gnus-agent
3843 (or gnus-agent-cache
3846 (let* ((gnus-command-method (gnus-find-method-for-group group))
3847 (file (gnus-agent-article-name (number-to-string article) group))
3848 (buffer-read-only nil))
3849 (when (and (file-exists-p file)
3850 (> (nth 7 (file-attributes file)) 0))
3852 (gnus-kill-all-overlays)
3853 (let ((coding-system-for-read gnus-cache-coding-system))
3854 (insert-file-contents file))
3857 (defun gnus-agent-regenerate-group (group &optional reread)
3859 If REREAD is t, all articles in the .overview are marked as unread.
3860 If REREAD is a list, the specified articles will be marked as unread.
3861 In addition, their NOV entries in .overview will be refreshed using
3862 the articles' current headers.
3863 If REREAD is not nil, downloaded articles are marked as unread."
3865 (list (let ((def (or (gnus-group-group-name)
3866 gnus-newsgroup-name)))
3867 (let ((select (read-string (if def
3868 (concat "Group Name ("
3871 (if (and (equal "" select)
3877 (cursor-in-echo-area t)
3878 (echo-keystrokes 0))
3879 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3880 (setq c (read-char-exclusive))
3882 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3884 ((or (eq c ?a) (eq c ?A))
3886 ((or (eq c ?d) (eq c ?D))
3887 (throw 'mark 'some)))
3888 (gnus-message 3 "Ignoring unexpected input")
3892 (gnus-message 5 "Regenerating in %s" group)
3893 (let* ((gnus-command-method (or gnus-command-method
3894 (gnus-find-method-for-group group)))
3895 (file (gnus-agent-article-name ".overview" group))
3896 (dir (file-name-directory file))
3898 (downloaded (if (file-exists-p dir)
3899 (sort (delq nil (mapcar (lambda (name)
3900 (and (not (file-directory-p (nnheader-concat dir name)))
3901 (string-to-number name)))
3902 (directory-files dir nil "^[0-9]+$" t)))
3904 (progn (gnus-make-directory dir) nil)))
3909 (mm-with-unibyte-buffer
3910 (if (file-exists-p file)
3911 (let ((nnheader-file-coding-system
3912 gnus-agent-file-coding-system))
3913 (nnheader-insert-file-contents file)))
3914 (set-buffer-modified-p nil)
3916 ;; Load the article IDs found in the overview file. As a
3917 ;; side-effect, validate the file contents.
3921 (goto-char (point-min))
3922 (while (< (point) (point-max))
3923 (cond ((and (looking-at "[0-9]+\t")
3924 (<= (- (match-end 0) (match-beginning 0)) 9))
3925 (push (read (current-buffer)) nov-arts)
3927 (let ((l1 (car nov-arts))
3928 (l2 (cadr nov-arts)))
3929 (cond ((and (listp reread) (memq l1 reread))
3931 (setq nov-arts (cdr nov-arts))
3932 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3933 entry of article %s deleted." l1))
3937 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3938 entries are NOT in ascending order.")
3939 ;; Don't sort now as I haven't verified
3940 ;; that every line begins with a number
3944 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3945 entries contained duplicate of article %s. Duplicate deleted." l1)
3947 (setq nov-arts (cdr nov-arts))))))
3949 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3950 entries contained line that did not begin with an article number. Deleted\
3952 (gnus-delete-line))))
3954 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3955 entries into ascending order.")
3956 (sort-numeric-fields 1 (point-min) (point-max))
3957 (setq nov-arts nil))))
3958 (gnus-agent-check-overview-buffer)
3960 ;; Construct a new article alist whose nodes match every header
3961 ;; in the .overview file. As a side-effect, missing headers are
3962 ;; reconstructed from the downloaded article file.
3963 (while (or downloaded nov-arts)
3964 (cond ((and downloaded
3966 (> (car downloaded) (car nov-arts))))
3967 ;; This entry is missing from the overview file
3968 (gnus-message 3 "Regenerating NOV %s %d..." group
3970 (let ((file (concat dir (number-to-string (car downloaded)))))
3971 (mm-with-unibyte-buffer
3972 (nnheader-insert-file-contents file)
3973 (nnheader-remove-body)
3974 (setq header (nnheader-parse-naked-head)))
3975 (mail-header-set-number header (car downloaded))
3977 (let ((key (concat "^" (int-to-string (car nov-arts))
3979 (or (re-search-backward key nil t)
3980 (re-search-forward key))
3982 (goto-char (point-min)))
3983 (nnheader-insert-nov header))
3984 (setq nov-arts (cons (car downloaded) nov-arts)))
3985 ((eq (car downloaded) (car nov-arts))
3986 ;; This entry in the overview has been downloaded
3987 (push (cons (car downloaded)
3989 (nth 5 (file-attributes
3990 (concat dir (number-to-string
3991 (car downloaded))))))) alist)
3992 (setq downloaded (cdr downloaded))
3993 (setq nov-arts (cdr nov-arts)))
3995 ;; This entry in the overview has not been downloaded
3996 (push (cons (car nov-arts) nil) alist)
3997 (setq nov-arts (cdr nov-arts)))))
3999 ;; When gnus-agent-consider-all-articles is set,
4000 ;; gnus-agent-regenerate-group should NOT remove article IDs from
4001 ;; the alist. Those IDs serve as markers to indicate that an
4002 ;; attempt has been made to fetch that article's header.
4004 ;; When gnus-agent-consider-all-articles is NOT set,
4005 ;; gnus-agent-regenerate-group can remove the article ID of every
4006 ;; article (with the exception of the last ID in the list - it's
4007 ;; special) that no longer appears in the overview. In this
4008 ;; situtation, the last article ID in the list implies that it,
4009 ;; and every article ID preceeding it, have been fetched from the
4012 (if gnus-agent-consider-all-articles
4013 ;; Restore all article IDs that were not found in the overview file.
4014 (let* ((n (cons nil alist))
4016 (o (gnus-agent-load-alist group)))
4018 (let ((nID (caadr n))
4021 (setq n (setcdr n (list (list oID))))
4024 (setcdr n (cons (list oID) (cdr n)))
4030 (setq n (cdr n))))))
4031 (setq alist (cdr merged)))
4032 ;; Restore the last article ID if it is not already in the new alist
4033 (let ((n (last alist))
4034 (o (last (gnus-agent-load-alist group))))
4038 (push (cons (caar o) nil) alist))
4039 ((< (caar n) (caar o))
4040 (setcdr n (list (car o)))))))
4042 (let ((inhibit-quit t))
4043 (if (setq regenerated (buffer-modified-p))
4044 (let ((coding-system-for-write gnus-agent-file-coding-system))
4045 (write-region (point-min) (point-max) file nil 'silent)))
4047 (setq regenerated (or regenerated
4048 (and reread gnus-agent-article-alist)
4049 (not (equal alist gnus-agent-article-alist))))
4051 (setq gnus-agent-article-alist alist)
4054 (gnus-agent-save-alist group)
4056 ;; I have to alter the group's active range NOW as
4057 ;; gnus-make-ascending-articles-unread will use it to
4058 ;; recalculate the number of unread articles in the group
4060 (let ((group (gnus-group-real-name group))
4061 (group-active (or (gnus-active group)
4062 (gnus-activate-group group))))
4063 (gnus-agent-possibly-alter-active group group-active)))))
4065 (when (and reread gnus-agent-article-alist)
4066 (gnus-agent-synchronize-group-flags
4071 (delq nil (mapcar (function (lambda (c)
4072 (cond ((eq reread t)
4076 gnus-agent-article-alist)))
4078 gnus-command-method)
4081 (gnus-agent-update-files-total-fetched-for group nil)))
4087 (defun gnus-agent-regenerate (&optional clean reread)
4088 "Regenerate all agent covered files.
4089 If CLEAN, obsolete (ignore)."
4092 (gnus-message 4 "Regenerating Gnus agent files...")
4093 (dolist (gnus-command-method (gnus-agent-covered-methods))
4094 (dolist (group (gnus-groups-from-server gnus-command-method))
4095 (setq regenerated (or (gnus-agent-regenerate-group group reread)
4097 (gnus-message 4 "Regenerating Gnus agent files...done")
4101 (defun gnus-agent-go-online (&optional force)
4102 "Switch servers into online status."
4103 (interactive (list t))
4104 (dolist (server gnus-opened-servers)
4105 (when (eq (nth 1 server) 'offline)
4106 (if (if (eq force 'ask)
4108 (format "Switch %s:%s into online status? "
4109 (caar server) (cadar server)))
4111 (setcar (nthcdr 1 server) 'close)))))
4113 (defun gnus-agent-toggle-group-plugged (group)
4114 "Toggle the status of the server of the current group."
4115 (interactive (list (gnus-group-group-name)))
4116 (let* ((method (gnus-find-method-for-group group))
4117 (status (cadr (assoc method gnus-opened-servers))))
4118 (if (eq status 'offline)
4119 (gnus-server-set-status method 'closed)
4120 (gnus-close-server method)
4121 (gnus-server-set-status method 'offline))
4122 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4123 (if (eq status 'offline) 'offline 'online)
4124 (if (eq status 'offline) 'online 'offline))))
4126 (defun gnus-agent-group-covered-p (group)
4127 (gnus-agent-method-p (gnus-group-method group)))
4129 (defun gnus-agent-update-files-total-fetched-for
4130 (group delta &optional method path)
4131 "Update, or set, the total disk space used by the articles that the
4133 (when gnus-agent-total-fetched-hashtb
4134 (gnus-agent-with-refreshed-group
4136 ;; if null, gnus-agent-group-pathname will calc method.
4137 (let* ((gnus-command-method method)
4138 (path (or path (gnus-agent-group-pathname group)))
4139 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4140 (gnus-sethash path (make-list 3 0)
4141 gnus-agent-total-fetched-hashtb))))
4146 (while (setq file (pop delta))
4147 (incf sum (float (or (nth 7 (file-attributes
4151 (number-to-string file)
4154 (let ((sum (- (nth 2 entry)))
4155 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4157 (while (setq file (pop info))
4158 (incf sum (float (or (nth 8 file) 0))))
4161 (setq gnus-agent-need-update-total-fetched-for t)
4162 (incf (nth 2 entry) delta)))))
4164 (defun gnus-agent-update-view-total-fetched-for
4165 (group agent-over &optional method path)
4166 "Update, or set, the total disk space used by the .agentview and
4167 .overview files. These files are calculated separately as they can be
4169 (when gnus-agent-total-fetched-hashtb
4170 (gnus-agent-with-refreshed-group
4172 ;; if null, gnus-agent-group-pathname will calc method.
4173 (let* ((gnus-command-method method)
4174 (path (or path (gnus-agent-group-pathname group)))
4175 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4176 (gnus-sethash path (make-list 3 0)
4177 gnus-agent-total-fetched-hashtb)))
4178 (size (or (nth 7 (file-attributes
4184 (setq gnus-agent-need-update-total-fetched-for t)
4185 (setf (nth (if agent-over 1 0) entry) size)))))
4187 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4188 "Get the total disk space used by the specified GROUP."
4189 (unless (equal group "dummy.group")
4190 (unless gnus-agent-total-fetched-hashtb
4191 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4193 ;; if null, gnus-agent-group-pathname will calc method.
4194 (let* ((gnus-command-method method)
4195 (path (gnus-agent-group-pathname group))
4196 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4199 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4201 (gnus-agent-update-view-total-fetched-for group nil method path)
4202 (gnus-agent-update-view-total-fetched-for group t method path)
4203 (gnus-agent-update-files-total-fetched-for group nil method path)))))))
4205 (provide 'gnus-agent)
4207 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
4208 ;;; gnus-agent.el ends here