1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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."
122 :type '(choice (const :tag "Always" t)
123 (const :tag "Never" nil)
124 (const :tag "Ask" ask))
127 (defcustom gnus-agent-go-online 'ask
128 "Indicate if offline servers go online when you plug in.
129 If this is `ask' the hook will query the user."
131 :type '(choice (const :tag "Always" t)
132 (const :tag "Never" nil)
133 (const :tag "Ask" ask))
136 (defcustom gnus-agent-mark-unread-after-downloaded t
137 "Indicate whether to mark articles unread after downloaded."
142 (defcustom gnus-agent-download-marks '(download)
143 "Marks for downloading."
145 :type '(repeat (symbol :tag "Mark"))
148 (defcustom gnus-agent-consider-all-articles nil
149 "When non-nil, the agent will let the agent predicate decide
150 whether articles need to be downloaded or not, for all articles. When
151 nil, the default, the agent will only let the predicate decide
152 whether unread articles are downloaded or not. If you enable this,
153 groups with large active ranges may open slower and you may also want
154 to look into the agent expiry settings to block the expiration of
155 read articles as they would just be downloaded again."
160 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
161 "Chunk size for `gnus-agent-fetch-session'.
162 The function will split its article fetches into chunks smaller than
168 (defcustom gnus-agent-enable-expiration 'ENABLE
169 "The default expiration state for each group.
170 When set to ENABLE, the default, `gnus-agent-expire' will expire old
171 contents from a group's local storage. This value may be overridden
172 to disable expiration in specific categories, topics, and groups. Of
173 course, you could change gnus-agent-enable-expiration to DISABLE then
174 enable expiration per categories, topics, and groups."
177 :type '(radio (const :format "Enable " ENABLE)
178 (const :format "Disable " DISABLE)))
180 (defcustom gnus-agent-expire-unagentized-dirs t
181 "*Whether expiration should expire in unagentized directories.
182 Have gnus-agent-expire scan the directories under
183 \(gnus-agent-directory) for groups that are no longer agentized.
184 When found, offer to remove them."
189 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
190 "Initially, all servers from these methods are agentized.
191 The user may remove or add servers using the Server buffer.
192 See Info node `(gnus)Server Buffer'."
194 :type '(repeat symbol)
197 (defcustom gnus-agent-queue-mail t
198 "Whether and when outgoing mail should be queued by the agent.
199 When `always', always queue outgoing mail. When nil, never
200 queue. Otherwise, queue if and only if unplugged."
203 :type '(radio (const :format "Always" always)
204 (const :format "Never" nil)
205 (const :format "When plugged" t)))
207 (defcustom gnus-agent-prompt-send-queue nil
208 "If non-nil, `gnus-group-send-queue' will prompt if called when
214 ;;; Internal variables
216 (defvar gnus-agent-history-buffers nil)
217 (defvar gnus-agent-buffer-alist nil)
218 (defvar gnus-agent-article-alist nil
219 "An assoc list identifying the articles whose headers have been fetched.
220 If successfully fetched, these headers will be stored in the group's overview
221 file. The key of each assoc pair is the article ID, the value of each assoc
222 pair is a flag indicating whether the identified article has been downloaded
223 \(gnus-agent-fetch-articles sets the value to the day of the download).
225 1) The last element of this list can not be expired as some
226 routines (for example, get-agent-fetch-headers) use the last
227 value to track which articles have had their headers retrieved.
228 2) The function `gnus-agent-regenerate' may destructively modify the value.")
229 (defvar gnus-agent-group-alist nil)
230 (defvar gnus-category-alist nil)
231 (defvar gnus-agent-current-history nil)
232 (defvar gnus-agent-overview-buffer nil)
233 (defvar gnus-category-predicate-cache nil)
234 (defvar gnus-category-group-cache nil)
235 (defvar gnus-agent-spam-hashtb nil)
236 (defvar gnus-agent-file-name nil)
237 (defvar gnus-agent-send-mail-function nil)
238 (defvar gnus-agent-file-coding-system 'raw-text)
239 (defvar gnus-agent-file-loading-cache nil)
242 (defvar gnus-headers)
249 (defun gnus-open-agent ()
251 (gnus-agent-read-servers)
253 (gnus-agent-create-buffer)
254 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
255 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
256 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
258 (defun gnus-agent-create-buffer ()
259 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
261 (setq gnus-agent-overview-buffer
262 (gnus-get-buffer-create " *Gnus agent overview*"))
263 (with-current-buffer gnus-agent-overview-buffer
264 (mm-enable-multibyte))
267 (gnus-add-shutdown 'gnus-close-agent 'gnus)
269 (defun gnus-close-agent ()
270 (setq gnus-category-predicate-cache nil
271 gnus-category-group-cache nil
272 gnus-agent-spam-hashtb nil)
273 (gnus-kill-buffer gnus-agent-overview-buffer))
276 ;;; Utility functions
279 (defun gnus-agent-read-file (file)
280 "Load FILE and do a `read' there."
283 (nnheader-insert-file-contents file)
284 (goto-char (point-min))
285 (read (current-buffer)))))
287 (defsubst gnus-agent-method ()
288 (concat (symbol-name (car gnus-command-method)) "/"
289 (if (equal (cadr gnus-command-method) "")
291 (cadr gnus-command-method))))
293 (defsubst gnus-agent-directory ()
294 "The name of the Gnus agent directory."
295 (nnheader-concat gnus-agent-directory
296 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
298 (defun gnus-agent-lib-file (file)
299 "The full name of the Gnus agent library FILE."
300 (expand-file-name file
301 (file-name-as-directory
302 (expand-file-name "agent.lib" (gnus-agent-directory)))))
304 (defun gnus-agent-cat-set-property (category property value)
306 (setcdr (or (assq property category)
307 (let ((cell (cons property nil)))
308 (setcdr category (cons cell (cdr category)))
310 (let ((category category))
311 (while (cond ((eq property (caadr category))
312 (setcdr category (cddr category))
315 (setq category (cdr category)))))))
319 (defmacro gnus-agent-cat-defaccessor (name prop-name)
320 "Define accessor and setter methods for manipulating a list of the form
321 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
322 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
323 manipulated as follows:
324 (func LIST): Returns VALUE1
325 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
326 `(progn (defmacro ,name (category)
327 (list (quote cdr) (list (quote assq)
328 (quote (quote ,prop-name)) category)))
330 (define-setf-method ,name (category)
331 (let* ((--category--temp-- (make-symbol "--category--"))
332 (--value--temp-- (make-symbol "--value--")))
333 (list (list --category--temp--) ; temporary-variables
334 (list category) ; value-forms
335 (list --value--temp--) ; store-variables
336 (let* ((category --category--temp--) ; store-form
337 (value --value--temp--))
338 (list (quote gnus-agent-cat-set-property)
340 (quote (quote ,prop-name))
342 (list (quote ,name) --category--temp--) ; access-form
346 (defmacro gnus-agent-cat-name (category)
349 (gnus-agent-cat-defaccessor
350 gnus-agent-cat-days-until-old agent-days-until-old)
351 (gnus-agent-cat-defaccessor
352 gnus-agent-cat-enable-expiration agent-enable-expiration)
353 (gnus-agent-cat-defaccessor
354 gnus-agent-cat-groups agent-groups)
355 (gnus-agent-cat-defaccessor
356 gnus-agent-cat-high-score agent-high-score)
357 (gnus-agent-cat-defaccessor
358 gnus-agent-cat-length-when-long agent-length-when-long)
359 (gnus-agent-cat-defaccessor
360 gnus-agent-cat-length-when-short agent-length-when-short)
361 (gnus-agent-cat-defaccessor
362 gnus-agent-cat-low-score agent-low-score)
363 (gnus-agent-cat-defaccessor
364 gnus-agent-cat-predicate agent-predicate)
365 (gnus-agent-cat-defaccessor
366 gnus-agent-cat-score-file agent-score-file)
367 (gnus-agent-cat-defaccessor
368 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
371 ;; This form is equivalent to defsetf except that it calls make-symbol
372 ;; whereas defsetf calls gensym (Using gensym creates a run-time
373 ;; dependency on the CL library).
376 (define-setf-method gnus-agent-cat-groups (category)
377 (let* ((--category--temp-- (make-symbol "--category--"))
378 (--groups--temp-- (make-symbol "--groups--")))
379 (list (list --category--temp--)
381 (list --groups--temp--)
382 (let* ((category --category--temp--)
383 (groups --groups--temp--))
384 (list (quote gnus-agent-set-cat-groups) category groups))
385 (list (quote gnus-agent-cat-groups) --category--temp--))))
388 (defun gnus-agent-set-cat-groups (category groups)
389 (unless (eq groups 'ignore)
391 (old-g (gnus-agent-cat-groups category)))
392 (cond ((eq new-g old-g)
393 ;; gnus-agent-add-group is fiddling with the group
394 ;; list. Still, Im done.
397 ((eq new-g (cdr old-g))
398 ;; gnus-agent-add-group is fiddling with the group list
399 (setcdr (or (assq 'agent-groups category)
400 (let ((cell (cons 'agent-groups nil)))
401 (setcdr category (cons cell (cdr category)))
404 (let ((groups groups))
406 (let* ((group (pop groups))
407 (old-category (gnus-group-category group)))
408 (if (eq category old-category)
410 (setf (gnus-agent-cat-groups old-category)
411 (delete group (gnus-agent-cat-groups
413 ;; Purge cache as preceeding loop invalidated it.
414 (setq gnus-category-group-cache nil))
416 (setcdr (or (assq 'agent-groups category)
417 (let ((cell (cons 'agent-groups nil)))
418 (setcdr category (cons cell (cdr category)))
421 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
422 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
424 ;;; Fetching setup functions.
426 (defun gnus-agent-start-fetch ()
427 "Initialize data structures for efficient fetching."
428 (gnus-agent-create-buffer))
430 (defun gnus-agent-stop-fetch ()
431 "Save all data structures and clean up."
432 (setq gnus-agent-spam-hashtb nil)
434 (set-buffer nntp-server-buffer)
437 (defmacro gnus-agent-with-fetch (&rest forms)
440 (let ((gnus-agent-fetching t))
441 (gnus-agent-start-fetch)
443 (gnus-agent-stop-fetch)))
445 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
446 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
448 (defmacro gnus-agent-append-to-list (tail value)
449 `(setq ,tail (setcdr ,tail (cons ,value nil))))
451 (defmacro gnus-agent-message (level &rest args)
452 `(if (<= ,level gnus-verbose)
459 (defvar gnus-agent-mode-hook nil
460 "Hook run when installing agent mode.")
462 (defvar gnus-agent-mode nil)
463 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
465 (defun gnus-agent-mode ()
466 "Minor mode for providing a agent support in Gnus buffers."
467 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
468 (symbol-name major-mode))
469 (match-string 1 (symbol-name major-mode))))
470 (mode (intern (format "gnus-agent-%s-mode" buffer))))
471 (set (make-local-variable 'gnus-agent-mode) t)
473 (set (make-local-variable mode) t)
475 (when (gnus-visual-p 'agent-menu 'menu)
476 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
477 (unless (assq 'gnus-agent-mode minor-mode-alist)
478 (push gnus-agent-mode-status minor-mode-alist))
479 (unless (assq mode minor-mode-map-alist)
480 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
482 minor-mode-map-alist))
483 (when (eq major-mode 'gnus-group-mode)
484 (let ((init-plugged gnus-plugged)
485 (gnus-agent-go-online nil))
486 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
487 ;; Therefore, make certain that the current value does not
488 ;; match the desired initial value.
489 (setq gnus-plugged :unknown)
490 (gnus-agent-toggle-plugged init-plugged)))
491 (gnus-run-hooks 'gnus-agent-mode-hook
492 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
494 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
495 (gnus-define-keys gnus-agent-group-mode-map
496 "Ju" gnus-agent-fetch-groups
497 "Jc" gnus-enter-category-buffer
498 "Jj" gnus-agent-toggle-plugged
499 "Js" gnus-agent-fetch-session
500 "JY" gnus-agent-synchronize-flags
501 "JS" gnus-group-send-queue
502 "Ja" gnus-agent-add-group
503 "Jr" gnus-agent-remove-group
504 "Jo" gnus-agent-toggle-group-plugged)
506 (defun gnus-agent-group-make-menu-bar ()
507 (unless (boundp 'gnus-agent-group-menu)
509 gnus-agent-group-menu gnus-agent-group-mode-map ""
511 ["Toggle plugged" gnus-agent-toggle-plugged t]
512 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
513 ["List categories" gnus-enter-category-buffer t]
514 ["Add (current) group to category" gnus-agent-add-group t]
515 ["Remove (current) group from category" gnus-agent-remove-group t]
516 ["Send queue" gnus-group-send-queue gnus-plugged]
518 ["All" gnus-agent-fetch-session gnus-plugged]
519 ["Group" gnus-agent-fetch-group gnus-plugged])
520 ["Synchronize flags" gnus-agent-synchronize-flags t]
523 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
524 (gnus-define-keys gnus-agent-summary-mode-map
525 "Jj" gnus-agent-toggle-plugged
526 "Ju" gnus-agent-summary-fetch-group
527 "JS" gnus-agent-fetch-group
528 "Js" gnus-agent-summary-fetch-series
529 "J#" gnus-agent-mark-article
530 "J\M-#" gnus-agent-unmark-article
531 "@" gnus-agent-toggle-mark
532 "Jc" gnus-agent-catchup)
534 (defun gnus-agent-summary-make-menu-bar ()
535 (unless (boundp 'gnus-agent-summary-menu)
537 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
539 ["Toggle plugged" gnus-agent-toggle-plugged t]
540 ["Mark as downloadable" gnus-agent-mark-article t]
541 ["Unmark as downloadable" gnus-agent-unmark-article t]
542 ["Toggle mark" gnus-agent-toggle-mark t]
543 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
544 ["Catchup undownloaded" gnus-agent-catchup t]))))
546 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
547 (gnus-define-keys gnus-agent-server-mode-map
548 "Jj" gnus-agent-toggle-plugged
549 "Ja" gnus-agent-add-server
550 "Jr" gnus-agent-remove-server)
552 (defun gnus-agent-server-make-menu-bar ()
553 (unless (boundp 'gnus-agent-server-menu)
555 gnus-agent-server-menu gnus-agent-server-mode-map ""
557 ["Toggle plugged" gnus-agent-toggle-plugged t]
558 ["Add" gnus-agent-add-server t]
559 ["Remove" gnus-agent-remove-server t]))))
561 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
562 (if (and (fboundp 'propertize)
563 (fboundp 'make-mode-line-mouse-map))
564 (propertize string 'local-map
565 (make-mode-line-mouse-map mouse-button mouse-func)
566 'mouse-face 'mode-line-highlight)
569 (defun gnus-agent-toggle-plugged (set-to)
570 "Toggle whether Gnus is unplugged or not."
571 (interactive (list (not gnus-plugged)))
572 (cond ((eq set-to gnus-plugged)
575 (setq gnus-plugged set-to)
576 (gnus-run-hooks 'gnus-agent-plugged-hook)
577 (setcar (cdr gnus-agent-mode-status)
578 (gnus-agent-make-mode-line-string " Plugged"
580 'gnus-agent-toggle-plugged))
581 (gnus-agent-go-online gnus-agent-go-online)
582 (gnus-agent-possibly-synchronize-flags))
584 (gnus-agent-close-connections)
585 (setq gnus-plugged set-to)
586 (gnus-run-hooks 'gnus-agent-unplugged-hook)
587 (setcar (cdr gnus-agent-mode-status)
588 (gnus-agent-make-mode-line-string " Unplugged"
590 'gnus-agent-toggle-plugged))))
591 (set-buffer-modified-p t))
593 (defmacro gnus-agent-while-plugged (&rest body)
594 `(let ((original-gnus-plugged gnus-plugged))
596 (progn (gnus-agent-toggle-plugged t)
598 (gnus-agent-toggle-plugged original-gnus-plugged))))
600 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
601 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
603 (defun gnus-agent-close-connections ()
604 "Close all methods covered by the Gnus agent."
605 (let ((methods (gnus-agent-covered-methods)))
607 (gnus-close-server (pop methods)))))
610 (defun gnus-unplugged ()
611 "Start Gnus unplugged."
613 (setq gnus-plugged nil)
617 (defun gnus-plugged ()
618 "Start Gnus plugged."
620 (setq gnus-plugged t)
624 (defun gnus-slave-unplugged (&optional arg)
625 "Read news as a slave unplugged."
627 (setq gnus-plugged nil)
628 (gnus arg nil 'slave))
631 (defun gnus-agentize ()
632 "Allow Gnus to be an offline newsreader.
634 The gnus-agentize function is now called internally by gnus when
635 gnus-agent is set. If you wish to avoid calling gnus-agentize,
636 customize gnus-agent to nil.
638 This will modify the `gnus-setup-news-hook', and
639 `message-send-mail-real-function' variables, and install the Gnus agent
640 minor mode in all Gnus buffers."
643 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
644 (unless gnus-agent-send-mail-function
645 (setq gnus-agent-send-mail-function
646 (or message-send-mail-real-function
647 (function (lambda () (funcall message-send-mail-function))))
648 message-send-mail-real-function 'gnus-agent-send-mail))
650 ;; If the servers file doesn't exist, auto-agentize some servers and
651 ;; save the servers file so this auto-agentizing isn't invoked
653 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
654 (gnus-message 3 "First time agent user, agentizing remote groups...")
656 (lambda (server-or-method)
657 (let ((method (gnus-server-to-method server-or-method)))
658 (when (memq (car method)
659 gnus-agent-auto-agentize-methods)
660 (push (gnus-method-to-server method)
661 gnus-agent-covered-methods)
662 (setq gnus-agent-method-p-cache nil))))
663 (cons gnus-select-method gnus-secondary-select-methods))
664 (gnus-agent-write-servers)))
666 (defun gnus-agent-queue-setup (&optional group-name)
667 "Make sure the queue group exists.
668 Optional arg GROUP-NAME allows to specify another group."
669 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
671 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
672 (let ((gnus-level-default-subscribed 1))
673 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
675 (gnus-group-set-parameter
676 (format "nndraft:%s" (or group-name "queue"))
677 'gnus-dummy '((gnus-draft-mode)))))
679 (defun gnus-agent-send-mail ()
680 (if (or (not gnus-agent-queue-mail)
681 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
682 (funcall gnus-agent-send-mail-function)
683 (goto-char (point-min))
685 (concat "^" (regexp-quote mail-header-separator) "\n"))
687 (gnus-agent-insert-meta-information 'mail)
688 (gnus-request-accept-article "nndraft:queue" nil t t)))
690 (defun gnus-agent-insert-meta-information (type &optional method)
691 "Insert meta-information into the message that says how it's to be posted.
692 TYPE can be either `mail' or `news'. If the latter, then METHOD can
695 (message-remove-header gnus-agent-meta-information-header)
696 (goto-char (point-min))
697 (insert gnus-agent-meta-information-header ": "
698 (symbol-name type) " " (format "%S" method)
701 (while (search-backward "\n" nil t)
702 (replace-match "\\n" t t))))
704 (defun gnus-agent-restore-gcc ()
705 "Restore GCC field from saved header."
707 (goto-char (point-min))
708 (while (re-search-forward
709 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
710 (replace-match "Gcc:" 'fixedcase))))
712 (defun gnus-agent-any-covered-gcc ()
714 (message-narrow-to-headers)
715 (let* ((gcc (mail-fetch-field "gcc" nil t))
717 (mapcar 'gnus-inews-group-method
718 (message-unquote-tokens
719 (message-tokenize-header
722 (while (and (not covered) methods)
723 (setq covered (gnus-agent-method-p (car methods))
724 methods (cdr methods)))
728 (defun gnus-agent-possibly-save-gcc ()
729 "Save GCC if Gnus is unplugged."
730 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
732 (goto-char (point-min))
733 (let ((case-fold-search t))
734 (while (re-search-forward "^gcc:" nil t)
735 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
737 (defun gnus-agent-possibly-do-gcc ()
738 "Do GCC if Gnus is plugged."
739 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
740 (gnus-inews-do-gcc)))
743 ;;; Group mode commands
746 (defun gnus-agent-fetch-groups (n)
747 "Put all new articles in the current groups into the Agent."
750 (error "Groups can't be fetched when Gnus is unplugged"))
751 (gnus-group-iterate n 'gnus-agent-fetch-group))
753 (defun gnus-agent-fetch-group (&optional group)
754 "Put all new articles in GROUP into the Agent."
755 (interactive (list (gnus-group-group-name)))
756 (setq group (or group gnus-newsgroup-name))
758 (error "No group on the current line"))
760 (gnus-agent-while-plugged
761 (let ((gnus-command-method (gnus-find-method-for-group group)))
762 (gnus-agent-with-fetch
763 (gnus-agent-fetch-group-1 group gnus-command-method)
764 (gnus-message 5 "Fetching %s...done" group)))))
766 (defun gnus-agent-add-group (category arg)
767 "Add the current group to an agent category."
773 (mapcar (lambda (cat) (list (symbol-name (car cat))))
777 (let ((cat (assq category gnus-category-alist))
779 (gnus-group-iterate arg
781 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
782 (setf (gnus-agent-cat-groups c)
783 (delete group (gnus-agent-cat-groups c))))
784 (push group groups)))
785 (setf (gnus-agent-cat-groups cat)
786 (nconc (gnus-agent-cat-groups cat) groups))
787 (gnus-category-write)))
789 (defun gnus-agent-remove-group (arg)
790 "Remove the current group from its agent category, if any."
793 (gnus-group-iterate arg
795 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
796 (setf (gnus-agent-cat-groups c)
797 (delete group (gnus-agent-cat-groups c))))))
798 (gnus-category-write)))
800 (defun gnus-agent-synchronize-flags ()
801 "Synchronize unplugged flags with servers."
804 (dolist (gnus-command-method (gnus-agent-covered-methods))
805 (when (file-exists-p (gnus-agent-lib-file "flags"))
806 (gnus-agent-synchronize-flags-server gnus-command-method)))))
808 (defun gnus-agent-possibly-synchronize-flags ()
809 "Synchronize flags according to `gnus-agent-synchronize-flags'."
812 (dolist (gnus-command-method (gnus-agent-covered-methods))
813 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
814 (not (eq (gnus-server-status gnus-command-method) 'offline)))
815 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
817 (defun gnus-agent-synchronize-flags-server (method)
818 "Synchronize flags set when unplugged for server."
819 (let ((gnus-command-method method)
821 (when (file-exists-p (gnus-agent-lib-file "flags"))
822 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
824 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
825 (cond ((null gnus-plugged)
827 1 "You must be plugged to synchronize flags with server %s"
828 (nth 1 gnus-command-method)))
829 ((null (gnus-check-server gnus-command-method))
831 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
836 (eval (read (current-buffer)))
837 (delete-region bgn (point))))
839 (delete-file (gnus-agent-lib-file "flags")))
841 (let ((file (gnus-agent-lib-file "flags")))
842 (write-region (point-min) (point-max)
843 (gnus-agent-lib-file "flags") nil 'silent)
844 (error "Couldn't set flags from file %s due to %s"
845 file (error-message-string err)))))))
848 (defun gnus-agent-possibly-synchronize-flags-server (method)
849 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
850 (when (or (and gnus-agent-synchronize-flags
851 (not (eq gnus-agent-synchronize-flags 'ask)))
852 (and (eq gnus-agent-synchronize-flags 'ask)
853 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
855 (gnus-agent-synchronize-flags-server method)))
858 (defun gnus-agent-rename-group (old-group new-group)
859 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
860 disabled, as the old agent files would corrupt gnus when the agent was
861 next enabled. Depends upon the caller to determine whether group renaming is supported."
862 (let* ((old-command-method (gnus-find-method-for-group old-group))
863 (old-path (directory-file-name
864 (let (gnus-command-method old-command-method)
865 (gnus-agent-group-pathname old-group))))
866 (new-command-method (gnus-find-method-for-group new-group))
867 (new-path (directory-file-name
868 (let (gnus-command-method new-command-method)
869 (gnus-agent-group-pathname new-group)))))
870 (gnus-rename-file old-path new-path t)
872 (let* ((old-real-group (gnus-group-real-name old-group))
873 (new-real-group (gnus-group-real-name new-group))
874 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
875 (gnus-agent-save-group-info old-command-method old-real-group nil)
876 (gnus-agent-save-group-info new-command-method new-real-group old-active)
878 (let ((old-local (gnus-agent-get-local old-group
879 old-real-group old-command-method)))
880 (gnus-agent-set-local old-group
882 old-real-group old-command-method)
883 (gnus-agent-set-local new-group
884 (car old-local) (cdr old-local)
885 new-real-group new-command-method)))))
888 (defun gnus-agent-delete-group (group)
889 "Delete fully-qualified GROUP. Always updates the agent, even when
890 disabled, as the old agent files would corrupt gnus when the agent was
891 next enabled. Depends upon the caller to determine whether group deletion is supported."
892 (let* ((command-method (gnus-find-method-for-group group))
893 (path (directory-file-name
894 (let (gnus-command-method command-method)
895 (gnus-agent-group-pathname group)))))
896 (gnus-delete-directory path)
898 (let* ((real-group (gnus-group-real-name group)))
899 (gnus-agent-save-group-info command-method real-group nil)
901 (let ((local (gnus-agent-get-local group
902 real-group command-method)))
903 (gnus-agent-set-local group
905 real-group command-method)))))
908 ;;; Server mode commands
911 (defun gnus-agent-add-server ()
912 "Enroll SERVER in the agent program."
914 (let* ((server (gnus-server-server-name))
915 (named-server (gnus-server-named-server))
917 (gnus-server-get-method nil server))))
919 (error "No server on the current line"))
921 (when (gnus-agent-method-p method)
922 (error "Server already in the agent program"))
924 (push named-server gnus-agent-covered-methods)
926 (setq gnus-agent-method-p-cache nil)
927 (gnus-server-update-server server)
928 (gnus-agent-write-servers)
929 (gnus-message 1 "Entered %s into the Agent" server)))
931 (defun gnus-agent-remove-server ()
932 "Remove SERVER from the agent program."
934 (let* ((server (gnus-server-server-name))
935 (named-server (gnus-server-named-server)))
937 (error "No server on the current line"))
939 (unless (member named-server gnus-agent-covered-methods)
940 (error "Server not in the agent program"))
942 (setq gnus-agent-covered-methods
943 (delete named-server gnus-agent-covered-methods)
944 gnus-agent-method-p-cache nil)
946 (gnus-server-update-server server)
947 (gnus-agent-write-servers)
948 (gnus-message 1 "Removed %s from the agent" server)))
950 (defun gnus-agent-read-servers ()
951 "Read the alist of covered servers."
952 (setq gnus-agent-covered-methods
953 (gnus-agent-read-file
954 (nnheader-concat gnus-agent-directory "lib/servers"))
955 gnus-agent-method-p-cache nil)
957 ;; I am called so early in start-up that I can not validate server
958 ;; names. When that is the case, I skip the validation. That is
959 ;; alright as the gnus startup code calls the validate methods
961 (if gnus-server-alist
962 (gnus-agent-read-servers-validate)))
964 (defun gnus-agent-read-servers-validate ()
965 (mapcar (lambda (server-or-method)
966 (let* ((server (if (stringp server-or-method)
968 (gnus-method-to-server server-or-method)))
969 (method (gnus-server-to-method server)))
971 (unless (member server gnus-agent-covered-methods)
972 (push server gnus-agent-covered-methods)
973 (setq gnus-agent-method-p-cache nil))
974 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
975 (prog1 gnus-agent-covered-methods
976 (setq gnus-agent-covered-methods nil))))
978 (defun gnus-agent-read-servers-validate-native (native-method)
979 (setq gnus-agent-covered-methods
980 (mapcar (lambda (method)
982 (equal method native-method))
984 method)) gnus-agent-covered-methods)))
986 (defun gnus-agent-write-servers ()
987 "Write the alist of covered servers."
988 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
989 (let ((coding-system-for-write nnheader-file-coding-system)
990 (file-name-coding-system nnmail-pathname-coding-system))
991 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
992 (prin1 gnus-agent-covered-methods
999 (defun gnus-agent-mark-article (n &optional unmark)
1000 "Mark the next N articles as downloadable.
1001 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1002 the mark instead. The difference between N and the actual number of
1003 articles marked is returned."
1005 (let ((backward (< n 0))
1010 (gnus-summary-set-agent-mark
1011 (gnus-summary-article-number) unmark)
1012 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1015 (gnus-message 7 "No more articles"))
1016 (gnus-summary-recenter)
1017 (gnus-summary-position-point)
1020 (defun gnus-agent-unmark-article (n)
1021 "Remove the downloadable mark from the next N articles.
1022 If N is negative, unmark backward instead. The difference between N and
1023 the actual number of articles unmarked is returned."
1025 (gnus-agent-mark-article n t))
1027 (defun gnus-agent-toggle-mark (n)
1028 "Toggle the downloadable mark from the next N articles.
1029 If N is negative, toggle backward instead. The difference between N and
1030 the actual number of articles toggled is returned."
1032 (gnus-agent-mark-article n 'toggle))
1034 (defun gnus-summary-set-agent-mark (article &optional unmark)
1035 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1036 When UNMARK is t, the article is unmarked. For any other value, the
1037 article's mark is toggled."
1038 (let ((unmark (cond ((eq nil unmark)
1043 (memq article gnus-newsgroup-downloadable)))))
1044 (when (gnus-summary-goto-subject article nil t)
1045 (gnus-summary-update-mark
1048 (setq gnus-newsgroup-downloadable
1049 (delq article gnus-newsgroup-downloadable))
1050 (gnus-article-mark article))
1051 (setq gnus-newsgroup-downloadable
1052 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1053 gnus-downloadable-mark)
1057 (defun gnus-agent-get-undownloaded-list ()
1058 "Construct list of articles that have not been downloaded."
1059 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1060 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1061 (gnus-agent-method-p gnus-command-method))
1062 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1063 (headers (sort (mapcar (lambda (h)
1064 (mail-header-number h))
1065 gnus-newsgroup-headers) '<))
1066 (cached (and gnus-use-cache gnus-newsgroup-cached))
1067 (undownloaded (list nil))
1068 (tail-undownloaded undownloaded)
1069 (unfetched (list nil))
1070 (tail-unfetched unfetched))
1071 (while (and alist headers)
1072 (let ((a (caar alist))
1075 ;; Ignore IDs in the alist that are not being
1076 ;; displayed in the summary.
1077 (setq alist (cdr alist)))
1079 ;; Headers that are not in the alist should be
1080 ;; fictious (see nnagent-retrieve-headers); they
1081 ;; imply that this article isn't in the agent.
1082 (gnus-agent-append-to-list tail-undownloaded h)
1083 (gnus-agent-append-to-list tail-unfetched h)
1084 (setq headers (cdr headers)))
1086 (setq alist (cdr alist))
1087 (setq headers (cdr headers))
1088 nil ; ignore already downloaded
1091 (setq alist (cdr alist))
1092 (setq headers (cdr headers))
1094 ;; This article isn't in the agent. Check to see
1095 ;; if it is in the cache. If it is, it's been
1097 (while (and cached (< (car cached) a))
1098 (setq cached (cdr cached)))
1099 (unless (equal a (car cached))
1100 (gnus-agent-append-to-list tail-undownloaded a))))))
1103 (let ((num (pop headers)))
1104 (gnus-agent-append-to-list tail-undownloaded num)
1105 (gnus-agent-append-to-list tail-unfetched num)))
1107 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1108 gnus-newsgroup-unfetched (cdr unfetched))))))
1110 (defun gnus-agent-catchup ()
1111 "Mark as read all unhandled articles.
1112 An article is unhandled if it is neither cached, nor downloaded, nor
1116 (let ((articles gnus-newsgroup-undownloaded))
1117 (when (or gnus-newsgroup-downloadable
1118 gnus-newsgroup-cached)
1119 (setq articles (gnus-sorted-ndifference
1120 (gnus-sorted-ndifference
1121 (gnus-copy-sequence articles)
1122 gnus-newsgroup-downloadable)
1123 gnus-newsgroup-cached)))
1126 (gnus-summary-mark-article
1127 (pop articles) gnus-catchup-mark)))
1128 (gnus-summary-position-point)))
1130 (defun gnus-agent-summary-fetch-series ()
1132 (when gnus-newsgroup-processable
1133 (setq gnus-newsgroup-downloadable
1134 (let* ((dl gnus-newsgroup-downloadable)
1135 (gnus-newsgroup-downloadable
1136 (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1137 (fetched-articles (gnus-agent-summary-fetch-group)))
1138 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1139 ;; updated gnus-newsgroup-downloadable to remove each
1140 ;; article successfully fetched.
1142 ;; For each article that I processed, remove its
1143 ;; processable mark IF the article is no longer
1144 ;; downloadable (i.e. it's already downloaded)
1145 (dolist (article gnus-newsgroup-processable)
1146 (unless (memq article gnus-newsgroup-downloadable)
1147 (gnus-summary-remove-process-mark article)))
1148 (gnus-sorted-ndifference dl fetched-articles)))))
1150 (defun gnus-agent-summary-fetch-group (&optional all)
1151 "Fetch the downloadable articles in the group.
1152 Optional arg ALL, if non-nil, means to fetch all articles."
1155 (if all gnus-newsgroup-articles
1156 gnus-newsgroup-downloadable))
1157 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1159 (gnus-agent-while-plugged
1161 (error "No articles to download"))
1162 (gnus-agent-with-fetch
1163 (setq gnus-newsgroup-undownloaded
1164 (gnus-sorted-ndifference
1165 gnus-newsgroup-undownloaded
1166 (setq fetched-articles
1167 (gnus-agent-fetch-articles
1168 gnus-newsgroup-name articles)))))
1170 (dolist (article articles)
1171 (let ((was-marked-downloadable
1172 (memq article gnus-newsgroup-downloadable)))
1173 (cond (gnus-agent-mark-unread-after-downloaded
1174 (setq gnus-newsgroup-downloadable
1175 (delq article gnus-newsgroup-downloadable))
1177 (gnus-summary-mark-article article gnus-unread-mark))
1178 (was-marked-downloadable
1179 (gnus-summary-set-agent-mark article t)))
1180 (when (gnus-summary-goto-subject article nil t)
1181 (gnus-summary-update-download-mark article))))))
1184 (defun gnus-agent-fetch-selected-article ()
1185 "Fetch the current article as it is selected.
1186 This can be added to `gnus-select-article-hook' or
1187 `gnus-mark-article-hook'."
1188 (let ((gnus-command-method gnus-current-select-method))
1189 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1190 (when (gnus-agent-fetch-articles
1192 (list gnus-current-article))
1193 (setq gnus-newsgroup-undownloaded
1194 (delq gnus-current-article gnus-newsgroup-undownloaded))
1195 (gnus-summary-update-download-mark gnus-current-article)))))
1198 ;;; Internal functions
1201 (defun gnus-agent-synchronize-group-flags (group actions server)
1202 "Update a plugged group by performing the indicated actions."
1203 (let* ((gnus-command-method (gnus-server-to-method server))
1205 ;; This initializer is required as gnus-request-set-mark
1206 ;; calls gnus-group-real-name to strip off the host name
1207 ;; before calling the backend. Now that the backend is
1208 ;; trying to call gnus-request-set-mark, I have to
1209 ;; reconstruct the original group name.
1210 (or (gnus-get-info group)
1212 (setq group (gnus-group-full-name
1213 group gnus-command-method))))))
1214 (gnus-request-set-mark group actions)
1217 (dolist (action actions)
1218 (let ((range (nth 0 action))
1219 (what (nth 1 action))
1220 (marks (nth 2 action)))
1221 (dolist (mark marks)
1222 (cond ((eq mark 'read)
1225 (funcall (if (eq what 'add)
1227 'gnus-remove-from-range)
1228 (gnus-info-read info)
1230 (gnus-get-unread-articles-in-group
1232 (gnus-active (gnus-info-group info))))
1233 ((memq mark '(tick))
1234 (let ((info-marks (assoc mark (gnus-info-marks info))))
1236 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1237 (setcdr info-marks (funcall (if (eq what 'add)
1239 'gnus-remove-from-range)
1244 (defun gnus-agent-save-active (method)
1245 (when (gnus-agent-method-p method)
1246 (let* ((gnus-command-method method)
1247 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1248 (file (gnus-agent-lib-file "active")))
1249 (gnus-active-to-gnus-format nil new)
1250 (gnus-agent-write-active file new)
1252 (nnheader-insert-file-contents file))))
1254 (defun gnus-agent-write-active (file new)
1255 (gnus-make-directory (file-name-directory file))
1256 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1257 ;; The hashtable contains real names of groups. However, do NOT
1258 ;; add the foreign server prefix as gnus-active-to-gnus-format
1259 ;; will add it while reading the file.
1260 (gnus-write-active-file file new nil)))
1263 (defun gnus-agent-possibly-alter-active (group active &optional info)
1264 "Possibly expand a group's active range to include articles
1265 downloaded into the agent."
1266 (let* ((gnus-command-method (or gnus-command-method
1267 (gnus-find-method-for-group group))))
1268 (when (gnus-agent-method-p gnus-command-method)
1269 (let* ((local (gnus-agent-get-local group))
1270 (active-min (or (car active) 0))
1271 (active-max (or (cdr active) 0))
1272 (agent-min (or (car local) active-min))
1273 (agent-max (or (cdr local) active-max)))
1275 (when (< agent-min active-min)
1276 (setcar active agent-min))
1278 (when (> agent-max active-max)
1279 (setcdr active agent-max))
1281 (when (and info (< agent-max (- active-min 100)))
1282 ;; I'm expanding the active range by such a large amount
1283 ;; that there is a gap of more than 100 articles between the
1284 ;; last article known to the agent and the first article
1285 ;; currently available on the server. This gap contains
1286 ;; articles that have been lost, mark them as read so that
1287 ;; gnus doesn't waste resources trying to fetch them.
1289 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1290 ;; want to modify the local file everytime someone restarts
1291 ;; gnus. The small gap will cause a tiny performance hit
1292 ;; when gnus tries, and fails, to retrieve the articles.
1293 ;; Still that should be smaller than opening a buffer,
1294 ;; printing this list to the buffer, and then writing it to a
1297 (let ((read (gnus-info-read info)))
1302 (list (cons (1+ agent-max)
1303 (1- active-min))))))
1305 ;; Lie about the agent's local range for this group to
1306 ;; disable the set read each time this server is opened.
1307 ;; NOTE: Opening this group will restore the valid local
1308 ;; range but it will also expand the local range to
1309 ;; incompass the new active range.
1310 (gnus-agent-set-local group agent-min (1- active-min)))))))
1312 (defun gnus-agent-save-group-info (method group active)
1313 "Update a single group's active range in the agent's copy of the server's active file."
1314 (when (gnus-agent-method-p method)
1315 (let* ((gnus-command-method (or method gnus-command-method))
1316 (coding-system-for-write nnheader-file-coding-system)
1317 (file-name-coding-system nnmail-pathname-coding-system)
1318 (file (gnus-agent-lib-file "active"))
1319 oactive-min oactive-max)
1320 (gnus-make-directory (file-name-directory file))
1321 (with-temp-file file
1322 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1323 (mm-disable-multibyte)
1324 (when (file-exists-p file)
1325 (nnheader-insert-file-contents file)
1327 (goto-char (point-min))
1328 (when (re-search-forward
1329 (concat "^" (regexp-quote group) " ") nil t)
1331 (setq oactive-max (read (current-buffer)) ;; max
1332 oactive-min (read (current-buffer)))) ;; min
1333 (gnus-delete-line)))
1335 (insert (format "%S %d %d y\n" (intern group)
1336 (max (or oactive-max (cdr active)) (cdr active))
1337 (min (or oactive-min (car active)) (car active))))
1338 (goto-char (point-max))
1339 (while (search-backward "\\." nil t)
1340 (delete-char 1)))))))
1342 (defun gnus-agent-get-group-info (method group)
1343 "Get a single group's active range in the agent's copy of the server's active file."
1344 (when (gnus-agent-method-p method)
1345 (let* ((gnus-command-method (or method gnus-command-method))
1346 (coding-system-for-write nnheader-file-coding-system)
1347 (file-name-coding-system nnmail-pathname-coding-system)
1348 (file (gnus-agent-lib-file "active"))
1349 oactive-min oactive-max)
1350 (gnus-make-directory (file-name-directory file))
1352 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1353 (mm-disable-multibyte)
1354 (when (file-exists-p file)
1355 (nnheader-insert-file-contents file)
1357 (goto-char (point-min))
1358 (when (re-search-forward
1359 (concat "^" (regexp-quote group) " ") nil t)
1361 (setq oactive-max (read (current-buffer)) ;; max
1362 oactive-min (read (current-buffer))) ;; min
1363 (cons oactive-min oactive-max))))))))
1365 (defun gnus-agent-group-path (group)
1366 "Translate GROUP into a file name."
1368 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1369 ;; The two methods must be kept synchronized, which is why
1370 ;; gnus-agent-group-pathname was added.
1373 (nnheader-translate-file-chars
1374 (nnheader-replace-duplicate-chars-in-string
1375 (nnheader-replace-chars-in-string
1376 (gnus-group-real-name (gnus-group-decoded-name group))
1379 (if (or nnmail-use-long-file-names
1380 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1382 (mm-encode-coding-string
1383 (nnheader-replace-chars-in-string group ?. ?/)
1384 nnmail-pathname-coding-system)))
1386 (defun gnus-agent-group-pathname (group)
1387 "Translate GROUP into a file name."
1388 ;; nnagent uses nnmail-group-pathname to read articles while
1389 ;; unplugged. The agent must, therefore, use the same directory
1391 (let ((gnus-command-method (or gnus-command-method
1392 (gnus-find-method-for-group group))))
1393 (nnmail-group-pathname (gnus-group-real-name
1394 (gnus-group-decoded-name group))
1395 (gnus-agent-directory))))
1397 (defun gnus-agent-get-function (method)
1398 (if (gnus-online method)
1403 (defun gnus-agent-covered-methods ()
1404 "Return the subset of methods that are covered by the agent."
1405 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1407 ;;; History functions
1409 (defun gnus-agent-history-buffer ()
1410 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1412 (defun gnus-agent-open-history ()
1414 (push (cons (gnus-agent-method)
1415 (set-buffer (gnus-get-buffer-create
1416 (format " *Gnus agent %s history*"
1417 (gnus-agent-method)))))
1418 gnus-agent-history-buffers)
1419 (mm-disable-multibyte) ;; everything is binary
1422 (let ((file (gnus-agent-lib-file "history")))
1423 (when (file-exists-p file)
1424 (nnheader-insert-file-contents file))
1425 (set (make-local-variable 'gnus-agent-file-name) file))))
1427 (defun gnus-agent-close-history ()
1428 (when (gnus-buffer-live-p gnus-agent-current-history)
1429 (kill-buffer gnus-agent-current-history)
1430 (setq gnus-agent-history-buffers
1431 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1432 gnus-agent-history-buffers))))
1438 (defun gnus-agent-fetch-articles (group articles)
1439 "Fetch ARTICLES from GROUP and put them into the Agent."
1441 (gnus-agent-load-alist group)
1442 (let* ((alist gnus-agent-article-alist)
1443 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1444 (selected-sets (list nil))
1445 (current-set-size 0)
1448 ;; Check each article
1449 (while (setq article (pop articles))
1450 ;; Skip alist entries preceeding this article
1451 (while (> article (or (caar alist) (1+ article)))
1452 (setq alist (cdr alist)))
1454 ;; Prune off articles that we have already fetched.
1455 (unless (and (eq article (caar alist))
1457 ;; Skip headers preceeding this article
1460 (let* ((header (car headers)))
1462 (mail-header-number header)
1464 (setq headers (cdr headers)))
1466 ;; Add this article to the current set
1467 (setcar selected-sets (cons article (car selected-sets)))
1469 ;; Update the set size, when the set is too large start a
1470 ;; new one. I do this after adding the article as I want at
1471 ;; least one article in each set.
1472 (when (< gnus-agent-max-fetch-size
1473 (setq current-set-size
1475 (if (= header-number article)
1476 (let ((char-size (mail-header-chars
1478 (if (<= char-size 0)
1479 ;; The char size was missing/invalid,
1480 ;; assume a worst-case situation of
1481 ;; 65 char/line. If the line count
1482 ;; is missing, arbitrarily assume a
1483 ;; size of 1000 characters.
1484 (max (* 65 (mail-header-lines
1489 (setcar selected-sets (nreverse (car selected-sets)))
1490 (setq selected-sets (cons nil selected-sets)
1491 current-set-size 0))))
1493 (when (or (cdr selected-sets) (car selected-sets))
1494 (let* ((fetched-articles (list nil))
1495 (tail-fetched-articles fetched-articles)
1496 (dir (gnus-agent-group-pathname group))
1497 (date (time-to-days (current-time)))
1498 (case-fold-search t)
1501 (setcar selected-sets (nreverse (car selected-sets)))
1502 (setq selected-sets (nreverse selected-sets))
1504 (gnus-make-directory dir)
1505 (gnus-message 7 "Fetching articles for %s..." group)
1508 (while (setq articles (pop selected-sets))
1509 ;; Fetch the articles from the backend.
1510 (if (gnus-check-backend-function 'retrieve-articles group)
1511 (setq pos (gnus-retrieve-articles articles group))
1514 (while (setq article (pop articles))
1515 (gnus-message 10 "Fetching article %s for %s..."
1518 (gnus-backlog-request-article group article
1520 (gnus-request-article article group))
1521 (goto-char (point-max))
1522 (push (cons article (point)) pos)
1523 (insert-buffer-substring nntp-server-buffer)))
1525 nntp-server-buffer (point-min) (point-max))
1526 (setq pos (nreverse pos)))))
1527 ;; Then save these articles into the Agent.
1529 (set-buffer nntp-server-buffer)
1531 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1532 (goto-char (point-min))
1533 (unless (eobp) ;; Don't save empty articles.
1534 (when (search-forward "\n\n" nil t)
1535 (when (search-backward "\nXrefs: " nil t)
1536 ;; Handle cross posting.
1537 (goto-char (match-end 0)) ; move to end of header name
1538 (skip-chars-forward "^ ") ; skip server name
1539 (skip-chars-forward " ")
1541 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1542 (push (cons (buffer-substring (match-beginning 1)
1545 (buffer-substring (match-beginning 2)
1548 (goto-char (match-end 0)))
1549 (gnus-agent-crosspost crosses (caar pos) date)))
1550 (goto-char (point-min))
1551 (if (not (re-search-forward
1552 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1553 (setq id "No-Message-ID-in-article")
1554 (setq id (buffer-substring
1555 (match-beginning 1) (match-end 1))))
1556 (let ((coding-system-for-write
1557 gnus-agent-file-coding-system))
1558 (write-region (point-min) (point-max)
1559 (concat dir (number-to-string (caar pos)))
1562 (gnus-agent-append-to-list
1563 tail-fetched-articles (caar pos)))
1565 (setq pos (cdr pos)))))
1567 (gnus-agent-save-alist group (cdr fetched-articles) date)
1568 (gnus-message 7 ""))
1569 (cdr fetched-articles))))))
1571 (defun gnus-agent-unfetch-articles (group articles)
1572 "Delete ARTICLES that were fetched from GROUP into the agent."
1574 (gnus-agent-load-alist group)
1575 (let* ((alist (cons nil gnus-agent-article-alist))
1576 (articles (sort articles #'<))
1577 (next-possibility alist)
1578 (delete-this (pop articles)))
1579 (while (and (cdr next-possibility) delete-this)
1580 (let ((have-this (caar (cdr next-possibility))))
1581 (cond ((< delete-this have-this)
1582 (setq delete-this (pop articles)))
1583 ((= delete-this have-this)
1584 (let ((timestamp (cdar (cdr next-possibility))))
1586 (let* ((file-name (concat (gnus-agent-group-pathname group)
1587 (number-to-string have-this))))
1588 (delete-file file-name))))
1590 (setcdr next-possibility (cddr next-possibility)))
1592 (setq next-possibility (cdr next-possibility))))))
1593 (setq gnus-agent-article-alist (cdr alist))
1594 (gnus-agent-save-alist group))))
1596 (defun gnus-agent-crosspost (crosses article &optional date)
1597 (setq date (or date t))
1599 (let (gnus-agent-article-alist group alist beg end)
1601 (set-buffer gnus-agent-overview-buffer)
1602 (when (nnheader-find-nov-line article)
1605 (setq end (progn (forward-line 1) (point)))))
1607 (setq group (caar crosses))
1608 (unless (setq alist (assoc group gnus-agent-group-alist))
1609 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1610 gnus-agent-group-alist))
1611 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1613 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1615 (when (= (point-max) (point-min))
1616 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1618 (nnheader-insert-file-contents
1619 (gnus-agent-article-name ".overview" group))))
1620 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1621 (insert (string-to-number (cdar crosses)))
1622 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1623 (gnus-agent-check-overview-buffer))
1624 (setq crosses (cdr crosses)))))
1626 (defun gnus-agent-backup-overview-buffer ()
1627 (when gnus-newsgroup-name
1628 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1631 (while (file-exists-p
1632 (setq name (concat root "~"
1633 (int-to-string (setq cnt (1+ cnt))) "~"))))
1634 (write-region (point-min) (point-max) name nil 'no-msg)
1635 (gnus-message 1 "Created backup copy of overview in %s." name)))
1638 (defun gnus-agent-check-overview-buffer (&optional buffer)
1639 "Check the overview file given for sanity.
1640 In particular, checks that the file is sorted by article number
1641 and that there are no duplicates."
1646 (set-buffer buffer))
1649 (goto-char (point-min))
1651 (while (< (point) (point-max))
1653 (cur (condition-case nil
1654 (read (current-buffer))
1657 ((or (not (integerp cur))
1658 (not (eq (char-after) ?\t)))
1660 (setq backed-up (gnus-agent-backup-overview-buffer)))
1662 "Overview buffer contains garbage '%s'."
1664 p (gnus-point-at-eol))))
1667 (setq backed-up (gnus-agent-backup-overview-buffer)))
1669 "Duplicate overview line for %d" cur)
1670 (delete-region p (progn (forward-line 1) (point))))
1673 (setq backed-up (gnus-agent-backup-overview-buffer)))
1674 (gnus-message 1 "Overview buffer not sorted!")
1675 (sort-numeric-fields 1 (point-min) (point-max))
1676 (goto-char (point-min))
1679 (setq prev-num cur)))
1680 (forward-line 1)))))))
1682 (defun gnus-agent-flush-cache ()
1684 (while gnus-agent-buffer-alist
1685 (set-buffer (cdar gnus-agent-buffer-alist))
1686 (let ((coding-system-for-write
1687 gnus-agent-file-coding-system))
1688 (write-region (point-min) (point-max)
1689 (gnus-agent-article-name ".overview"
1690 (caar gnus-agent-buffer-alist))
1692 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1693 (while gnus-agent-group-alist
1694 (with-temp-file (gnus-agent-article-name
1695 ".agentview" (caar gnus-agent-group-alist))
1696 (princ (cdar gnus-agent-group-alist))
1698 (princ 1 (current-buffer))
1700 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1703 (defun gnus-agent-find-parameter (group symbol)
1704 "Search for GROUPs SYMBOL in the group's parameters, the group's
1705 topic parameters, the group's category, or the customizable
1706 variables. Returns the first non-nil value found."
1707 (or (gnus-group-find-parameter group symbol t)
1708 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1712 '((agent-short-article . gnus-agent-short-article)
1713 (agent-long-article . gnus-agent-long-article)
1714 (agent-low-score . gnus-agent-low-score)
1715 (agent-high-score . gnus-agent-high-score)
1716 (agent-days-until-old . gnus-agent-expire-days)
1717 (agent-enable-expiration
1718 . gnus-agent-enable-expiration)
1719 (agent-predicate . gnus-agent-predicate)))))))
1721 (defun gnus-agent-fetch-headers (group &optional force)
1722 "Fetch interesting headers into the agent. The group's overview
1723 file will be updated to include the headers while a list of available
1724 article numbers will be returned."
1725 (let* ((fetch-all (and gnus-agent-consider-all-articles
1726 ;; Do not fetch all headers if the predicate
1727 ;; implies that we only consider unread articles.
1728 (not (gnus-predicate-implies-unread
1729 (gnus-agent-find-parameter group
1730 'agent-predicate)))))
1731 (articles (if fetch-all
1732 (gnus-uncompress-range (gnus-active group))
1733 (gnus-list-of-unread-articles group)))
1734 (gnus-decode-encoded-word-function 'identity)
1735 (file (gnus-agent-article-name ".overview" group)))
1738 ;; Add articles with marks to the list of article headers we want to
1739 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1740 ;; mark, but do fetch recent or seen articles if they have other, more
1741 ;; interesting marks. (We have to fetch articles with boring marks
1742 ;; because otherwise the agent will remove their marks.)
1743 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1744 (unless (memq (car arts) '(seen recent killed cache))
1745 (setq articles (gnus-range-add articles (cdr arts)))))
1746 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1748 ;; At this point, I have the list of articles to consider for
1749 ;; fetching. This is the list that I'll return to my caller. Some
1750 ;; of these articles may have already been fetched. That's OK as
1751 ;; the fetch article code will filter those out. Internally, I'll
1752 ;; filter this list to just those articles whose headers need to
1754 (let ((articles articles))
1755 ;; Remove known articles.
1756 (when (and (or gnus-agent-cache
1758 (gnus-agent-load-alist group))
1759 ;; Remove articles marked as downloaded.
1761 ;; I want to fetch all headers in the active range.
1762 ;; Therefore, exclude only those headers that are in the
1764 ;; NOTE: This is probably NOT what I want to do after
1765 ;; agent expiration in this group.
1766 (setq articles (gnus-agent-uncached-articles articles group))
1768 ;; I want to only fetch those headers that have never been
1769 ;; fetched. Therefore, exclude all headers that are, or
1770 ;; WERE, in the article alist.
1771 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1772 (high (cdr (gnus-active group))))
1773 ;; Low can be greater than High when the same group is
1774 ;; fetched twice in the same session {The first fetch will
1775 ;; fill the article alist such that (last
1776 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1777 ;; group))}. The addition of one(the 1+ above) then
1778 ;; forces Low to be greater than High. When this happens,
1779 ;; gnus-list-range-intersection returns nil which
1780 ;; indicates that no headers need to be fetched. -- Kevin
1781 (setq articles (gnus-list-range-intersection
1782 articles (list (cons low high)))))))
1785 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1786 (gnus-compress-sequence articles t))
1789 (set-buffer nntp-server-buffer)
1793 (gnus-message 7 "Fetching headers for %s..." group)
1796 (gnus-make-directory (nnheader-translate-file-chars
1797 (file-name-directory file) t))
1799 (unless (eq 'nov (gnus-retrieve-headers articles group))
1800 (nnvirtual-convert-headers))
1801 (gnus-agent-check-overview-buffer)
1802 ;; Move these headers to the overview buffer so that
1803 ;; gnus-agent-braid-nov can merge them with the contents
1806 gnus-agent-overview-buffer (point-min) (point-max))
1807 ;; NOTE: Call g-a-brand-nov even when the file does not
1808 ;; exist. As a minimum, it will validate the article
1809 ;; numbers already in the buffer.
1810 (gnus-agent-braid-nov group articles file)
1811 (let ((coding-system-for-write
1812 gnus-agent-file-coding-system))
1813 (gnus-agent-check-overview-buffer)
1814 (write-region (point-min) (point-max) file nil 'silent))
1815 (gnus-agent-save-alist group articles nil)
1819 (nnheader-insert-file-contents file)))))
1822 (defsubst gnus-agent-read-article-number ()
1823 "Reads the article number at point. Returns nil when a valid article number can not be read."
1825 ;; It is unfortunite but the read function quietly overflows
1826 ;; integer. As a result, I have to use string operations to test
1827 ;; for overflow BEFORE calling read.
1828 (when (looking-at "[0-9]+\t")
1829 (let ((len (- (match-end 0) (match-beginning 0))))
1831 (read (current-buffer)))
1833 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1834 ;; Back convert from int to string to ensure that this is one of them.
1835 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1836 (num (read (current-buffer)))
1837 (str2 (int-to-string num)))
1838 (when (equal str1 str2)
1841 (defsubst gnus-agent-copy-nov-line (article)
1842 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1844 (set-buffer gnus-agent-overview-buffer)
1845 (while (and (not (eobp))
1846 (or (not (setq art (gnus-agent-read-article-number)))
1851 (not (eq article art)))
1852 (set-buffer nntp-server-buffer)
1854 (setq e (progn (forward-line 1) (point)))
1855 (set-buffer nntp-server-buffer)
1856 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1858 (defun gnus-agent-braid-nov (group articles file)
1859 "Merge agent overview data with given file.
1860 Takes unvalidated headers for ARTICLES from
1861 `gnus-agent-overview-buffer' and validated headers from the given
1862 FILE and places the combined valid headers into
1863 `nntp-server-buffer'. This function can be used, when file
1864 doesn't exist, to valid the overview buffer."
1866 (set-buffer gnus-agent-overview-buffer)
1867 (goto-char (point-min))
1868 (set-buffer nntp-server-buffer)
1870 (when (file-exists-p file)
1871 (nnheader-insert-file-contents file))
1872 (goto-char (point-max))
1875 (unless (or (= (point-min) (point-max))
1876 (< (setq last (read (current-buffer))) (car articles)))
1877 ;; Old and new overlap -- We do it the hard way.
1878 (when (nnheader-find-nov-line (car articles))
1879 ;; Replacing existing NOV entry
1880 (delete-region (point) (progn (forward-line 1) (point))))
1881 (gnus-agent-copy-nov-line (pop articles))
1885 (while (let ((art (read (current-buffer))))
1886 (cond ((< art (car articles))
1889 ((= art (car articles))
1892 (point) (progn (forward-line 1) (point)))
1898 (gnus-agent-copy-nov-line (pop articles)))))
1900 (goto-char (point-max))
1902 ;; Append the remaining lines
1905 (set-buffer gnus-agent-overview-buffer)
1906 (setq start (point))
1907 (set-buffer nntp-server-buffer))
1910 (insert-buffer-substring gnus-agent-overview-buffer start)
1913 (setq last (or last -134217728))
1916 (setq art (gnus-agent-read-article-number))
1918 ;; Bad art num - delete this line
1920 (delete-region (point) (progn (forward-line 1) (point))))
1922 ;; Art num out of order - enable sort
1930 (sort-numeric-fields 1 (point-min) (point-max)))))))
1932 ;; Keeps the compiler from warning about the free variable in
1933 ;; gnus-agent-read-agentview.
1935 (defvar gnus-agent-read-agentview))
1937 (defun gnus-agent-load-alist (group)
1938 "Load the article-state alist for GROUP."
1939 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1940 (let ((gnus-agent-read-agentview group))
1941 (setq gnus-agent-article-alist
1942 (gnus-cache-file-contents
1943 (gnus-agent-article-name ".agentview" group)
1944 'gnus-agent-file-loading-cache
1945 'gnus-agent-read-agentview))))
1947 ;; Save format may be either 1 or 2. Two is the new, compressed
1948 ;; format that is still being tested. Format 1 is uncompressed but
1949 ;; known to be reliable.
1950 (defconst gnus-agent-article-alist-save-format 2)
1952 (defun gnus-agent-read-agentview (file)
1953 "Load FILE and do a `read' there."
1957 (nnheader-insert-file-contents file)
1958 (goto-char (point-min))
1959 (let ((alist (read (current-buffer)))
1960 (version (condition-case nil (read (current-buffer))
1966 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version))
1968 (let ((inhibit-quit t)
1970 (gnus-agent-open-history)
1971 (set-buffer (gnus-agent-history-buffer))
1972 (goto-char (point-min))
1974 (if (and (looking-at
1975 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1976 (string= (match-string 2)
1977 gnus-agent-read-agentview)
1978 (setq entry (assoc (string-to-number (match-string 3)) alist)))
1979 (setcdr entry (string-to-number (match-string 1))))
1981 (gnus-agent-close-history)
1982 (setq changed-version t)))
1984 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
1989 (let ((state (car comp-list))
1991 (gnus-uncompress-range
1993 (mapcar (lambda (article-id)
1994 (setq uncomp (cons (cons article-id state) uncomp)))
1997 (setq alist (sort uncomp 'car-less-than-car)))))
1998 (when changed-version
1999 (let ((gnus-agent-article-alist alist))
2000 (gnus-agent-save-alist gnus-agent-read-agentview)))
2004 (defun gnus-agent-save-alist (group &optional articles state)
2005 "Save the article-state alist for GROUP."
2006 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2007 (prev (cons nil gnus-agent-article-alist))
2009 print-level print-length item article)
2010 (while (setq article (pop articles))
2011 (while (and (cdr prev)
2012 (< (caadr prev) article))
2013 (setq prev (cdr prev)))
2016 (setcdr prev (list (cons article state))))
2017 ((> (caadr prev) article)
2018 (setcdr prev (cons (cons article state) (cdr prev))))
2019 ((= (caadr prev) article)
2020 (setcdr (cadr prev) state)))
2021 (setq prev (cdr prev)))
2022 (setq gnus-agent-article-alist (cdr all))
2024 (gnus-agent-set-local group
2025 (caar gnus-agent-article-alist)
2026 (caar (last gnus-agent-article-alist)))
2028 (gnus-make-directory (gnus-agent-article-name "" group))
2029 (with-temp-file (gnus-agent-article-name ".agentview" group)
2030 (cond ((eq gnus-agent-article-alist-save-format 1)
2031 (princ gnus-agent-article-alist (current-buffer)))
2032 ((eq gnus-agent-article-alist-save-format 2)
2033 (let ((compressed nil))
2034 (mapcar (lambda (pair)
2035 (let* ((article-id (car pair))
2036 (day-of-download (cdr pair))
2037 (comp-list (assq day-of-download compressed)))
2040 (cons article-id (cdr comp-list)))
2042 (cons (list day-of-download article-id)
2044 nil)) gnus-agent-article-alist)
2045 (mapcar (lambda (comp-list)
2047 (gnus-compress-sequence
2048 (nreverse (cdr comp-list)))))
2050 (princ compressed (current-buffer)))))
2052 (princ gnus-agent-article-alist-save-format (current-buffer))
2055 (defvar gnus-agent-article-local nil)
2056 (defvar gnus-agent-file-loading-local nil)
2058 (defun gnus-agent-load-local (&optional method)
2059 "Load the METHOD'S local file. The local file contains min/max
2060 article counts for each of the method's subscribed groups."
2061 (let ((gnus-command-method (or method gnus-command-method)))
2062 (setq gnus-agent-article-local
2063 (gnus-cache-file-contents
2064 (gnus-agent-lib-file "local")
2065 'gnus-agent-file-loading-local
2066 'gnus-agent-read-and-cache-local))))
2068 (defun gnus-agent-read-and-cache-local (file)
2069 "Load and read FILE then bind its contents to
2070 gnus-agent-article-local. If that variable had `dirty' (also known as
2071 modified) original contents, they are first saved to their own file."
2073 (if (and gnus-agent-article-local
2074 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2075 (gnus-agent-save-local))
2076 (gnus-agent-read-local file))
2078 (defun gnus-agent-read-local (file)
2079 "Load FILE and do a `read' there."
2080 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2085 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2086 (nnheader-insert-file-contents file))
2089 (goto-char (point-min))
2090 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2091 (while (= (following-char) ?\;)
2093 (setq line (1+ line)))
2100 (cur (current-buffer)))
2101 (setq group (read cur)
2105 (when (stringp group)
2106 (setq group (intern group my-obarray)))
2108 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2109 (set group (cons (+ 0 min) (+ 0 max))))
2111 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2112 file line (error-message-string err))))
2114 (setq line (1+ line))))
2116 (set (intern "+dirty" my-obarray) nil)
2117 (set (intern "+method" my-obarray) gnus-command-method)
2120 (defun gnus-agent-save-local (&optional force)
2121 "Save gnus-agent-article-local under it method's agent.lib directory."
2122 (let ((my-obarray gnus-agent-article-local))
2123 (when (and my-obarray
2124 (or force (symbol-value (intern "+dirty" my-obarray))))
2125 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2126 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2127 (dest (gnus-agent-lib-file "local")))
2128 (gnus-make-directory (gnus-agent-lib-file ""))
2130 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2131 (with-temp-file dest
2132 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2133 (file-name-coding-system nnmail-pathname-coding-system)
2134 print-level print-length item article
2135 (standard-output (current-buffer)))
2136 (mapatoms (lambda (symbol)
2137 (cond ((not (boundp symbol))
2139 ((member (symbol-name symbol) '("+dirty" "+method"))
2143 (let ((range (symbol-value symbol)))
2151 (defun gnus-agent-get-local (group &optional gmane method)
2152 (let* ((gmane (or gmane (gnus-group-real-name group)))
2153 (gnus-command-method (or method (gnus-find-method-for-group group)))
2154 (local (gnus-agent-load-local))
2155 (symb (intern gmane local))
2156 (minmax (and (boundp symb) (symbol-value symb))))
2158 ;; Bind these so that gnus-agent-load-alist doesn't change the
2159 ;; current alist (i.e. gnus-agent-article-alist)
2160 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2161 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2162 (alist (gnus-agent-load-alist group)))
2166 (caar (last alist))))
2167 (gnus-agent-set-local group (car minmax) (cdr minmax)
2168 gmane gnus-command-method local))))
2171 (defun gnus-agent-set-local (group min max &optional gmane method local)
2172 (let* ((gmane (or gmane (gnus-group-real-name group)))
2173 (gnus-command-method (or method (gnus-find-method-for-group group)))
2174 (local (or local (gnus-agent-load-local)))
2175 (symb (intern gmane local))
2176 (minmax (and (boundp symb) (symbol-value symb))))
2178 (if (cond ((and minmax
2179 (or (not (eq min (car minmax)))
2180 (not (eq max (cdr minmax)))))
2187 (set symb (cons min max))
2190 (unintern symb local)))
2191 (set (intern "+dirty" local) t))))
2193 (defun gnus-agent-article-name (article group)
2194 (expand-file-name article
2195 (file-name-as-directory
2196 (gnus-agent-group-pathname group))))
2198 (defun gnus-agent-batch-confirmation (msg)
2199 "Show error message and return t."
2200 (gnus-message 1 msg)
2204 (defun gnus-agent-batch-fetch ()
2205 "Start Gnus and fetch session."
2208 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2209 (gnus-agent-fetch-session))
2212 (defun gnus-agent-fetch-session ()
2213 "Fetch all articles and headers that are eligible for fetching."
2215 (unless gnus-agent-covered-methods
2216 (error "No servers are covered by the Gnus agent"))
2217 (unless gnus-plugged
2218 (error "Can't fetch articles while Gnus is unplugged"))
2219 (let ((methods (gnus-agent-covered-methods))
2220 groups group gnus-command-method)
2223 (setq gnus-command-method (car methods))
2224 (when (and (or (gnus-server-opened gnus-command-method)
2225 (gnus-open-server gnus-command-method))
2226 (gnus-online gnus-command-method))
2227 (setq groups (gnus-groups-from-server (car methods)))
2228 (gnus-agent-with-fetch
2229 (while (setq group (pop groups))
2230 (when (<= (gnus-group-level group)
2231 gnus-agent-handle-level)
2232 (if (or debug-on-error debug-on-quit)
2233 (gnus-agent-fetch-group-1
2234 group gnus-command-method)
2236 (gnus-agent-fetch-group-1
2237 group gnus-command-method)
2239 (unless (funcall gnus-agent-confirmation-function
2240 (format "Error %s while fetching session. Should gnus continue? "
2241 (error-message-string err)))
2242 (error "Cannot fetch articles into the Gnus agent")))
2244 (gnus-agent-regenerate-group group)
2245 (unless (funcall gnus-agent-confirmation-function
2247 "%s while fetching session. Should gnus continue? "
2248 (error-message-string err)))
2250 "Cannot fetch articles into the Gnus agent")))))))))
2251 (setq methods (cdr methods)))
2252 (gnus-run-hooks 'gnus-agent-fetched-hook)
2253 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2255 (defun gnus-agent-fetch-group-1 (group method)
2257 (let ((gnus-command-method method)
2258 (gnus-newsgroup-name group)
2259 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2260 (gnus-newsgroup-headers gnus-newsgroup-headers)
2261 (gnus-newsgroup-scored gnus-newsgroup-scored)
2262 (gnus-use-cache gnus-use-cache)
2263 (gnus-summary-expunge-below gnus-summary-expunge-below)
2264 (gnus-summary-mark-below gnus-summary-mark-below)
2265 (gnus-orphan-score gnus-orphan-score)
2266 ;; Maybe some other gnus-summary local variables should also
2272 category predicate info marks score-param
2274 (unless (gnus-check-group group)
2275 (error "Can't open server for %s" group))
2278 (when (or gnus-newsgroup-active
2280 (gnus-activate-group group))
2281 (let ((marked-articles gnus-newsgroup-downloadable))
2282 ;; Identify the articles marked for download
2283 (unless gnus-newsgroup-active
2284 ;; The variable gnus-newsgroup-active was selected as I need
2285 ;; a gnus-summary local variable that is NOT bound to any
2286 ;; value (its global value should default to nil).
2287 (dolist (mark gnus-agent-download-marks)
2288 (let ((arts (cdr (assq mark (gnus-info-marks
2289 (setq info (gnus-get-info group)))))))
2291 (setq marked-articles (nconc (gnus-uncompress-range arts)
2294 (setq marked-articles (sort marked-articles '<))
2296 ;; Fetch any new articles from the server
2297 (setq articles (gnus-agent-fetch-headers group))
2299 ;; Merge new articles with marked
2300 (setq articles (sort (append marked-articles articles) '<))
2303 ;; Parse them and see which articles we want to fetch.
2304 (setq gnus-newsgroup-dependencies
2305 (or gnus-newsgroup-dependencies
2306 (make-vector (length articles) 0)))
2307 (setq gnus-newsgroup-headers
2308 (or gnus-newsgroup-headers
2309 (gnus-get-newsgroup-headers-xover articles nil nil
2311 ;; `gnus-agent-overview-buffer' may be killed for
2312 ;; timeout reason. If so, recreate it.
2313 (gnus-agent-create-buffer)
2315 ;; Figure out how to select articles in this group
2316 (setq category (gnus-group-category group))
2320 (gnus-agent-find-parameter group 'agent-predicate)))
2322 ;; If the selection predicate requires scoring, score each header
2323 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2325 (gnus-agent-find-parameter group 'agent-score-file)))
2326 ;; Translate score-param into real one
2329 ((eq score-param 'file)
2330 (setq score-param (gnus-all-score-files group)))
2331 ((stringp (car score-param)))
2333 (setq score-param (list (list score-param)))))
2335 (gnus-score-headers score-param))))
2337 (unless (and (eq predicate 'gnus-agent-false)
2338 (not marked-articles))
2339 (let ((arts (list nil)))
2340 (let ((arts-tail arts)
2341 (alist (gnus-agent-load-alist group))
2342 (marked-articles marked-articles)
2343 (gnus-newsgroup-headers gnus-newsgroup-headers))
2344 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2345 (let ((num (mail-header-number gnus-headers)))
2346 ;; Determine if this article is already in the cache
2348 (> num (caar alist)))
2349 (setq alist (cdr alist)))
2351 (unless (and (eq num (caar alist))
2354 ;; Determine if this article was marked for download.
2355 (while (and marked-articles
2356 (> num (car marked-articles)))
2357 (setq marked-articles
2358 (cdr marked-articles)))
2360 ;; When this article is marked, or selected by the
2361 ;; predicate, add it to the download list
2362 (when (or (eq num (car marked-articles))
2365 (assq num gnus-newsgroup-scored))
2366 gnus-summary-default-score))
2367 (gnus-agent-long-article
2368 (gnus-agent-find-parameter
2369 group 'agent-long-article))
2370 (gnus-agent-short-article
2371 (gnus-agent-find-parameter
2372 group 'agent-short-article))
2373 (gnus-agent-low-score
2374 (gnus-agent-find-parameter
2375 group 'agent-low-score))
2376 (gnus-agent-high-score
2377 (gnus-agent-find-parameter
2378 group 'agent-high-score))
2379 (gnus-agent-expire-days
2380 (gnus-agent-find-parameter
2381 group 'agent-days-until-old)))
2382 (funcall predicate)))
2383 (gnus-agent-append-to-list arts-tail num))))))
2385 (let (fetched-articles)
2386 ;; Fetch all selected articles
2387 (setq gnus-newsgroup-undownloaded
2388 (gnus-sorted-ndifference
2389 gnus-newsgroup-undownloaded
2390 (setq fetched-articles
2392 (gnus-agent-fetch-articles group (cdr arts))
2395 (let ((unfetched-articles
2396 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2397 (if gnus-newsgroup-active
2398 ;; Update the summary buffer
2400 (dolist (article marked-articles)
2401 (gnus-summary-set-agent-mark article t))
2402 (dolist (article fetched-articles)
2403 (if gnus-agent-mark-unread-after-downloaded
2404 (gnus-summary-mark-article
2405 article gnus-unread-mark))
2406 (when (gnus-summary-goto-subject article nil t)
2407 (gnus-summary-update-download-mark article)))
2408 (dolist (article unfetched-articles)
2409 (gnus-summary-mark-article
2410 article gnus-canceled-mark)))
2412 ;; Update the group buffer.
2414 ;; When some, or all, of the marked articles came
2415 ;; from the download mark. Remove that mark. I
2416 ;; didn't do this earlier as I only want to remove
2417 ;; the marks after the fetch is completed.
2419 (dolist (mark gnus-agent-download-marks)
2420 (when (eq mark 'download)
2422 (assq mark (gnus-info-marks
2423 (setq info (gnus-get-info group))))))
2424 (when (cdr marked-arts)
2426 (delq marked-arts (gnus-info-marks info)))
2427 (gnus-info-set-marks info marks)))))
2428 (let ((read (gnus-info-read
2429 (or info (setq info (gnus-get-info group))))))
2431 info (gnus-add-to-range read unfetched-articles)))
2433 (gnus-group-update-group group t)
2437 (concat "(gnus-group-set-info '"
2438 (gnus-prin1-to-string info)
2442 ;;; Agent Category Mode
2445 (defvar gnus-category-mode-hook nil
2446 "Hook run in `gnus-category-mode' buffers.")
2448 (defvar gnus-category-line-format " %(%20c%): %g\n"
2449 "Format of category lines.
2451 Valid specifiers include:
2452 %c Topic name (string)
2453 %g The number of groups in the topic (integer)
2455 General format specifiers can also be used. See Info node
2456 `(gnus)Formatting Variables'.")
2458 (defvar gnus-category-mode-line-format "Gnus: %%b"
2459 "The format specification for the category mode line.")
2461 (defvar gnus-agent-predicate 'false
2462 "The selection predicate used when no other source is available.")
2464 (defvar gnus-agent-short-article 100
2465 "Articles that have fewer lines than this are short.")
2467 (defvar gnus-agent-long-article 200
2468 "Articles that have more lines than this are long.")
2470 (defvar gnus-agent-low-score 0
2471 "Articles that have a score lower than this have a low score.")
2473 (defvar gnus-agent-high-score 0
2474 "Articles that have a score higher than this have a high score.")
2477 ;;; Internal variables.
2479 (defvar gnus-category-buffer "*Agent Category*")
2481 (defvar gnus-category-line-format-alist
2482 `((?c gnus-tmp-name ?s)
2483 (?g gnus-tmp-groups ?d)))
2485 (defvar gnus-category-mode-line-format-alist
2486 `((?u user-defined ?s)))
2488 (defvar gnus-category-line-format-spec nil)
2489 (defvar gnus-category-mode-line-format-spec nil)
2491 (defvar gnus-category-mode-map nil)
2492 (put 'gnus-category-mode 'mode-class 'special)
2494 (unless gnus-category-mode-map
2495 (setq gnus-category-mode-map (make-sparse-keymap))
2496 (suppress-keymap gnus-category-mode-map)
2498 (gnus-define-keys gnus-category-mode-map
2499 "q" gnus-category-exit
2500 "k" gnus-category-kill
2501 "c" gnus-category-copy
2502 "a" gnus-category-add
2503 "e" gnus-agent-customize-category
2504 "p" gnus-category-edit-predicate
2505 "g" gnus-category-edit-groups
2506 "s" gnus-category-edit-score
2507 "l" gnus-category-list
2509 "\C-c\C-i" gnus-info-find-node
2510 "\C-c\C-b" gnus-bug))
2512 (defvar gnus-category-menu-hook nil
2513 "*Hook run after the creation of the menu.")
2515 (defun gnus-category-make-menu-bar ()
2516 (gnus-turn-off-edit-menu 'category)
2517 (unless (boundp 'gnus-category-menu)
2519 gnus-category-menu gnus-category-mode-map ""
2521 ["Add" gnus-category-add t]
2522 ["Kill" gnus-category-kill t]
2523 ["Copy" gnus-category-copy t]
2524 ["Edit category" gnus-agent-customize-category t]
2525 ["Edit predicate" gnus-category-edit-predicate t]
2526 ["Edit score" gnus-category-edit-score t]
2527 ["Edit groups" gnus-category-edit-groups t]
2528 ["Exit" gnus-category-exit t]))
2530 (gnus-run-hooks 'gnus-category-menu-hook)))
2532 (defun gnus-category-mode ()
2533 "Major mode for listing and editing agent categories.
2535 All normal editing commands are switched off.
2536 \\<gnus-category-mode-map>
2537 For more in-depth information on this mode, read the manual
2538 \(`\\[gnus-info-find-node]').
2540 The following commands are available:
2542 \\{gnus-category-mode-map}"
2544 (when (gnus-visual-p 'category-menu 'menu)
2545 (gnus-category-make-menu-bar))
2546 (kill-all-local-variables)
2547 (gnus-simplify-mode-line)
2548 (setq major-mode 'gnus-category-mode)
2549 (setq mode-name "Category")
2550 (gnus-set-default-directory)
2551 (setq mode-line-process nil)
2552 (use-local-map gnus-category-mode-map)
2553 (buffer-disable-undo)
2554 (setq truncate-lines t)
2555 (setq buffer-read-only t)
2556 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2558 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2560 (defun gnus-category-insert-line (category)
2561 (let* ((gnus-tmp-name (format "%s" (car category)))
2562 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2564 (gnus-add-text-properties
2568 (eval gnus-category-line-format-spec))
2569 (list 'gnus-category gnus-tmp-name))))
2571 (defun gnus-enter-category-buffer ()
2572 "Go to the Category buffer."
2574 (gnus-category-setup-buffer)
2575 (gnus-configure-windows 'category)
2576 (gnus-category-prepare))
2578 (defun gnus-category-setup-buffer ()
2579 (unless (get-buffer gnus-category-buffer)
2581 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2582 (gnus-category-mode))))
2584 (defun gnus-category-prepare ()
2585 (gnus-set-format 'category-mode)
2586 (gnus-set-format 'category t)
2587 (let ((alist gnus-category-alist)
2588 (buffer-read-only nil))
2591 (gnus-category-insert-line (pop alist)))
2592 (goto-char (point-min))
2593 (gnus-category-position-point)))
2595 (defun gnus-category-name ()
2596 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2597 (error "No category on the current line")))
2599 (defun gnus-category-read ()
2600 "Read the category alist."
2601 (setq gnus-category-alist
2605 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2606 (goto-char (point-min))
2607 ;; This code isn't temp, it will be needed so long as
2608 ;; anyone may be migrating from an older version.
2610 ;; Once we're certain that people will not revert to an
2611 ;; earlier version, we can take out the old-list code in
2612 ;; gnus-category-write.
2613 (let* ((old-list (read (current-buffer)))
2614 (new-list (ignore-errors (read (current-buffer)))))
2617 ;; Convert from a positional list to an alist.
2627 '(agent-predicate agent-score-file agent-groups))))
2630 (list (gnus-agent-cat-make 'default 'short)))))
2632 (defun gnus-category-write ()
2633 "Write the category alist."
2634 (setq gnus-category-predicate-cache nil
2635 gnus-category-group-cache nil)
2636 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2637 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2638 ;; This prin1 is temporary. It exists so that people can revert
2639 ;; to an earlier version of gnus-agent.
2640 (prin1 (mapcar (lambda (c)
2642 (cdr (assoc 'agent-predicate c))
2643 (cdr (assoc 'agent-score-file c))
2644 (cdr (assoc 'agent-groups c))))
2645 gnus-category-alist)
2648 (prin1 gnus-category-alist (current-buffer))))
2650 (defun gnus-category-edit-predicate (category)
2651 "Edit the predicate for CATEGORY."
2652 (interactive (list (gnus-category-name)))
2653 (let ((info (assq category gnus-category-alist)))
2655 (gnus-agent-cat-predicate info)
2656 (format "Editing the select predicate for category %s" category)
2657 `(lambda (predicate)
2658 ;; Avoid run-time execution of setf form
2659 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2661 ;; use its expansion instead:
2662 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2663 'agent-predicate predicate)
2665 (gnus-category-write)
2666 (gnus-category-list)))))
2668 (defun gnus-category-edit-score (category)
2669 "Edit the score expression for CATEGORY."
2670 (interactive (list (gnus-category-name)))
2671 (let ((info (assq category gnus-category-alist)))
2673 (gnus-agent-cat-score-file info)
2674 (format "Editing the score expression for category %s" category)
2675 `(lambda (score-file)
2676 ;; Avoid run-time execution of setf form
2677 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2679 ;; use its expansion instead:
2680 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2681 'agent-score-file score-file)
2683 (gnus-category-write)
2684 (gnus-category-list)))))
2686 (defun gnus-category-edit-groups (category)
2687 "Edit the group list for CATEGORY."
2688 (interactive (list (gnus-category-name)))
2689 (let ((info (assq category gnus-category-alist)))
2691 (gnus-agent-cat-groups info)
2692 (format "Editing the group list for category %s" category)
2694 ;; Avoid run-time execution of setf form
2695 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2697 ;; use its expansion instead:
2698 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2701 (gnus-category-write)
2702 (gnus-category-list)))))
2704 (defun gnus-category-kill (category)
2705 "Kill the current category."
2706 (interactive (list (gnus-category-name)))
2707 (let ((info (assq category gnus-category-alist))
2708 (buffer-read-only nil))
2710 (setq gnus-category-alist (delq info gnus-category-alist))
2711 (gnus-category-write)))
2713 (defun gnus-category-copy (category to)
2714 "Copy the current category."
2715 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2716 (let ((info (assq category gnus-category-alist)))
2717 (push (let ((newcat (gnus-copy-sequence info)))
2718 (setf (gnus-agent-cat-name newcat) to)
2719 (setf (gnus-agent-cat-groups newcat) nil)
2721 gnus-category-alist)
2722 (gnus-category-write)
2723 (gnus-category-list)))
2725 (defun gnus-category-add (category)
2726 "Create a new category."
2727 (interactive "SCategory name: ")
2728 (when (assq category gnus-category-alist)
2729 (error "Category %s already exists" category))
2730 (push (gnus-agent-cat-make category)
2731 gnus-category-alist)
2732 (gnus-category-write)
2733 (gnus-category-list))
2735 (defun gnus-category-list ()
2736 "List all categories."
2738 (gnus-category-prepare))
2740 (defun gnus-category-exit ()
2741 "Return to the group buffer."
2743 (kill-buffer (current-buffer))
2744 (gnus-configure-windows 'group t))
2746 ;; To avoid having 8-bit characters in the source file.
2747 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2749 (defvar gnus-category-predicate-alist
2750 '((spam . gnus-agent-spam-p)
2751 (short . gnus-agent-short-p)
2752 (long . gnus-agent-long-p)
2753 (low . gnus-agent-low-scored-p)
2754 (high . gnus-agent-high-scored-p)
2755 (read . gnus-agent-read-p)
2756 (true . gnus-agent-true)
2757 (false . gnus-agent-false))
2758 "Mapping from short score predicate symbols to predicate functions.")
2760 (defun gnus-agent-spam-p ()
2761 "Say whether an article is spam or not."
2762 (unless gnus-agent-spam-hashtb
2763 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2764 (if (not (equal (mail-header-references gnus-headers) ""))
2766 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2768 (gnus-gethash string gnus-agent-spam-hashtb)
2769 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2771 (defun gnus-agent-short-p ()
2772 "Say whether an article is short or not."
2773 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2775 (defun gnus-agent-long-p ()
2776 "Say whether an article is long or not."
2777 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2779 (defun gnus-agent-low-scored-p ()
2780 "Say whether an article has a low score or not."
2781 (< gnus-score gnus-agent-low-score))
2783 (defun gnus-agent-high-scored-p ()
2784 "Say whether an article has a high score or not."
2785 (> gnus-score gnus-agent-high-score))
2787 (defun gnus-agent-read-p ()
2788 "Say whether an article is read or not."
2789 (gnus-member-of-range (mail-header-number gnus-headers)
2790 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2792 (defun gnus-category-make-function (predicate)
2793 "Make a function from PREDICATE."
2794 (let ((func (gnus-category-make-function-1 predicate)))
2795 (if (and (= (length func) 1)
2796 (symbolp (car func)))
2798 (gnus-byte-compile `(lambda () ,func)))))
2800 (defun gnus-agent-true ()
2804 (defun gnus-agent-false ()
2808 (defun gnus-category-make-function-1 (predicate)
2809 "Make a function from PREDICATE."
2811 ;; Functions are just returned as is.
2812 ((or (symbolp predicate)
2813 (functionp predicate))
2814 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2816 ;; More complex predicate.
2819 ((memq (car predicate) '(& and))
2821 ((memq (car predicate) '(| or))
2823 ((memq (car predicate) gnus-category-not)
2825 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2827 (error "Unknown predicate type: %s" predicate))))
2829 (defun gnus-get-predicate (predicate)
2830 "Return the function implementing PREDICATE."
2831 (or (cdr (assoc predicate gnus-category-predicate-cache))
2832 (let ((func (gnus-category-make-function predicate)))
2833 (setq gnus-category-predicate-cache
2834 (nconc gnus-category-predicate-cache
2835 (list (cons predicate func))))
2838 (defun gnus-predicate-implies-unread (predicate)
2839 "Say whether PREDICATE implies unread articles only.
2840 It is okay to miss some cases, but there must be no false positives.
2841 That is, if this predicate returns true, then indeed the predicate must
2842 return only unread articles."
2843 (eq t (gnus-function-implies-unread-1
2844 (gnus-category-make-function-1 predicate))))
2846 (defun gnus-function-implies-unread-1 (function)
2847 "Recursively evaluate a predicate function to determine whether it can select
2848 any read articles. Returns t if the function is known to never
2849 return read articles, nil when it is known to always return read
2850 articles, and t_nil when the function may return both read and unread
2852 (let ((func (car function))
2853 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2854 (cond ((eq func 'and)
2855 (cond ((memq t args) ; if any argument returns only unread articles
2856 ;; then that argument constrains the result to only unread articles.
2858 ((memq 't_nil args) ; if any argument is indeterminate
2859 ;; then the result is indeterminate
2862 (cond ((memq nil args) ; if any argument returns read articles
2863 ;; then that argument ensures that the results includes read articles.
2865 ((memq 't_nil args) ; if any argument is indeterminate
2866 ;; then that argument ensures that the results are indeterminate
2868 (t ; if all arguments return only unread articles
2869 ;; then the result returns only unread articles
2872 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2873 ; then the result is indeterminate
2876 ; toggle the result to be the opposite of the argument
2878 ((eq func 'gnus-agent-read-p)
2879 nil) ; The read predicate NEVER returns unread articles
2880 ((eq func 'gnus-agent-false)
2881 t) ; The false predicate returns t as the empty set excludes all read articles
2882 ((eq func 'gnus-agent-true)
2883 nil) ; The true predicate ALWAYS returns read articles
2884 ((catch 'found-match
2885 (let ((alist gnus-category-predicate-alist))
2887 (if (eq func (cdar alist))
2888 (throw 'found-match t)
2889 (setq alist (cdr alist))))))
2890 't_nil) ; All other predicates return read and unread articles
2892 (error "Unknown predicate function: %s" function)))))
2894 (defun gnus-group-category (group)
2895 "Return the category GROUP belongs to."
2896 (unless gnus-category-group-cache
2897 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2898 (let ((cs gnus-category-alist)
2900 (while (setq cat (pop cs))
2901 (setq groups (gnus-agent-cat-groups cat))
2903 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2904 (or (gnus-gethash group gnus-category-group-cache)
2905 (assq 'default gnus-category-alist)))
2907 (defun gnus-agent-expire-group (group &optional articles force)
2908 "Expire all old articles in GROUP.
2909 If you want to force expiring of certain articles, this function can
2910 take ARTICLES, and FORCE parameters as well.
2912 The articles on which the expiration process runs are selected as follows:
2913 if ARTICLES is null, all read and unmarked articles.
2914 if ARTICLES is t, all articles.
2915 if ARTICLES is a list, just those articles.
2916 FORCE is equivalent to setting the expiration predicates to true."
2918 (list (let ((def (or (gnus-group-group-name)
2919 gnus-newsgroup-name)))
2920 (let ((select (read-string (if def
2921 (concat "Group Name ("
2924 (if (and (equal "" select)
2930 (gnus-agent-expire articles group force)
2931 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2932 ;; expiration statistics of this single group
2933 (gnus-agent-expire-stats (list 0 0 0.0)))
2934 (if (or (not (eq articles t))
2936 (concat "Are you sure that you want to "
2937 "expire all articles in " group ".")))
2938 (let ((gnus-command-method (gnus-find-method-for-group group))
2939 (overview (gnus-get-buffer-create " *expire overview*"))
2942 (let ((active-file (gnus-agent-lib-file "active")))
2943 (when (file-exists-p active-file)
2945 (nnheader-insert-file-contents active-file)
2946 (gnus-active-to-gnus-format
2948 (setq orig (gnus-make-hashtable
2949 (count-lines (point-min) (point-max))))))
2951 (gnus-agent-expire-group-1
2952 group overview (gnus-gethash-safe group orig)
2954 (kill-buffer overview))))
2955 (gnus-message 4 (gnus-agent-expire-done-message)))))
2957 (defun gnus-agent-expire-group-1 (group overview active articles force)
2958 ;; Internal function - requires caller to have set
2959 ;; gnus-command-method, initialized overview buffer, and to have
2960 ;; provided a non-nil active
2962 (let ((dir (gnus-agent-group-pathname group)))
2963 (when (boundp 'gnus-agent-expire-current-dirs)
2964 (set 'gnus-agent-expire-current-dirs
2966 (symbol-value 'gnus-agent-expire-current-dirs))))
2968 (if (and (not force)
2969 (eq 'DISABLE (gnus-agent-find-parameter group
2970 'agent-enable-expiration)))
2971 (gnus-message 5 "Expiry skipping over %s" group)
2972 (gnus-message 5 "Expiring articles in %s" group)
2973 (gnus-agent-load-alist group)
2974 (let* ((bytes-freed 0)
2976 (nov-entries-deleted 0)
2977 (info (gnus-get-info group))
2978 (alist gnus-agent-article-alist)
2979 (day (- (time-to-days (current-time))
2980 (gnus-agent-find-parameter group 'agent-days-until-old)))
2981 (specials (if (and alist
2983 ;; This could be a bit of a problem. I need to
2984 ;; keep the last article to avoid refetching
2985 ;; headers when using nntp in the backend. At
2986 ;; the same time, if someone uses a backend
2987 ;; that supports article moving then I may have
2988 ;; to remove the last article to complete the
2989 ;; move. Right now, I'm going to assume that
2990 ;; FORCE overrides specials.
2991 (list (caar (last alist)))))
2992 (unreads ;; Articles that are excluded from the
2993 ;; expiration process
2994 (cond (gnus-agent-expire-all
2995 ;; All articles are marked read by global decree
2998 ;; All articles are marked read by function
3002 ;; Unread articles are marked protected from
3003 ;; expiration Don't call
3004 ;; gnus-list-of-unread-articles as it returns
3005 ;; articles that have not been fetched into the
3008 (gnus-agent-unread-articles group)))
3010 ;; All articles EXCEPT those named by the caller
3011 ;; are protected from expiration
3012 (gnus-sorted-difference
3013 (gnus-uncompress-range
3015 (caar (last alist))))
3016 (sort articles '<)))))
3017 (marked ;; More articles that are excluded from the
3018 ;; expiration process
3019 (cond (gnus-agent-expire-all
3020 ;; All articles are unmarked by global decree
3023 ;; All articles are unmarked by function
3027 ;; All articles may as well be unmarked as the
3028 ;; unreads list already names the articles we are
3032 ;; Ticked and/or dormant articles are excluded
3035 (gnus-uncompress-range
3036 (cdr (assq 'tick (gnus-info-marks info))))
3037 (gnus-uncompress-range
3039 (gnus-info-marks info))))))))
3040 (nov-file (concat dir ".overview"))
3046 ;; The normal article alist contains elements that look like
3047 ;; (article# . fetch_date) I need to combine other
3048 ;; information with this list. For example, a flag indicating
3049 ;; that a particular article MUST BE KEPT. To do this, I'm
3050 ;; going to transform the elements to look like (article#
3051 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3052 ;; the process to generate the expired article alist.
3054 ;; Convert the alist elements to (article# fetch_date nil
3056 (setq dlist (mapcar (lambda (e)
3057 (list (car e) (cdr e) nil nil)) alist))
3059 ;; Convert the keep lists to elements that look like (article#
3060 ;; nil keep_flag nil) then append it to the expanded dlist
3061 ;; These statements are sorted by ascending precidence of the
3063 (setq dlist (nconc dlist
3065 (list e nil 'unread nil))
3067 (setq dlist (nconc dlist
3069 (list e nil 'marked nil))
3071 (setq dlist (nconc dlist
3073 (list e nil 'special nil))
3076 (set-buffer overview)
3078 (buffer-disable-undo)
3079 (when (file-exists-p nov-file)
3080 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3081 (nnheader-insert-file-contents nov-file)
3082 (goto-char (point-min))
3085 (while (< (setq p (point)) (point-max))
3087 ;; If I successfully read an integer (the plus zero
3088 ;; ensures a numeric type), prepend a marker entry
3090 (push (list (+ 0 (read (current-buffer))) nil nil
3091 (set-marker (make-marker) p))
3094 (gnus-message 1 "gnus-agent-expire: read error \
3095 occurred when reading expression at %s in %s. Skipping to next \
3096 line." (point) nov-file)))
3097 ;; Whether I succeeded, or failed, it doesn't matter.
3098 ;; Move to the next line then try again.
3102 7 "gnus-agent-expire: Loading overview... Done"))
3103 (set-buffer-modified-p nil)
3105 ;; At this point, all of the information is in dlist. The
3106 ;; only problem is that much of it is spread across multiple
3107 ;; entries. Sort then MERGE!!
3108 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3109 ;; If two entries have the same article-number then sort by
3110 ;; ascending keep_flag.
3117 (cond ((< (nth 0 a) (nth 0 b))
3119 ((> (nth 0 a) (nth 0 b))
3122 (let ((a (or (symbol-value (nth 2 a))
3124 (b (or (symbol-value (nth 2 b))
3127 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3128 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3129 (let ((dlist dlist))
3130 (while (cdr dlist) ; I'm not at the end-of-list
3131 (if (eq (caar dlist) (caadr dlist))
3132 (let ((first (cdr (car dlist)))
3133 (secnd (cdr (cadr dlist))))
3134 (setcar first (or (car first)
3135 (car secnd))) ; fetch_date
3136 (setq first (cdr first)
3138 (setcar first (or (car first)
3139 (car secnd))) ; Keep_flag
3140 (setq first (cdr first)
3142 (setcar first (or (car first)
3143 (car secnd))) ; NOV_entry_marker
3145 (setcdr dlist (cddr dlist)))
3146 (setq dlist (cdr dlist)))))
3147 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3149 (let* ((len (float (length dlist)))
3153 (let ((new-completed (truncate (* 100.0
3154 (/ (setq cnt (1+ cnt))
3157 (when (> new-completed completed)
3158 (setq completed new-completed)
3159 (gnus-message 7 "%3d%% completed..." completed)))
3160 (let* ((entry (car dlist))
3161 (article-number (nth 0 entry))
3162 (fetch-date (nth 1 entry))
3163 (keep (nth 2 entry))
3164 (marker (nth 3 entry)))
3167 ;; Kept articles are unread, marked, or special.
3169 (gnus-agent-message 10
3170 "gnus-agent-expire: %s:%d: Kept %s article%s."
3171 group article-number keep (if fetch-date " and file" ""))
3173 (unless (file-exists-p
3174 (concat dir (number-to-string
3176 (setf (nth 1 entry) nil)
3177 (gnus-agent-message 3 "gnus-agent-expire cleared \
3178 download flag on %s:%d as the cached article file is missing."
3179 group (caar dlist)))
3181 (gnus-message 1 "gnus-agent-expire detected a \
3182 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3183 (gnus-agent-append-to-list
3185 (cons article-number fetch-date)))
3187 ;; The following articles are READ, UNMARKED, and
3188 ;; ORDINARY. See if they can be EXPIRED!!!
3191 ((not (integerp fetch-date))
3192 'read) ;; never fetched article (may expire
3194 ((not (file-exists-p
3195 (concat dir (number-to-string
3197 (setf (nth 1 entry) nil)
3198 'externally-expired) ;; Can't find the cached
3199 ;; article. Handle case
3200 ;; as though this article
3201 ;; was never fetched.
3203 ;; We now have the arrival day, so we see
3204 ;; whether it's old enough to be expired.
3210 ;; I found some reason to expire this entry.
3212 (let ((actions nil))
3213 (when (memq type '(forced expired))
3214 (ignore-errors ; Just being paranoid.
3215 (let* ((file-name (nnheader-concat dir (number-to-string
3217 (size (float (nth 7 (file-attributes file-name)))))
3218 (incf bytes-freed size)
3219 (incf files-deleted)
3220 (delete-file file-name))
3221 (push "expired cached article" actions))
3222 (setf (nth 1 entry) nil)
3226 (push "NOV entry removed" actions)
3229 (incf nov-entries-deleted)
3231 (let ((from (gnus-point-at-bol))
3232 (to (progn (forward-line 1) (point))))
3233 (incf bytes-freed (- to from))
3234 (delete-region from to)))
3236 ;; If considering all articles is set, I can only
3237 ;; expire article IDs that are no longer in the
3238 ;; active range (That is, articles that preceed the
3239 ;; first article in the new alist).
3240 (if (and gnus-agent-consider-all-articles
3241 (>= article-number (car active)))
3242 ;; I have to keep this ID in the alist
3243 (gnus-agent-append-to-list
3244 tail-alist (cons article-number fetch-date))
3245 (push (format "Removed %s article number from \
3246 article alist" type) actions))
3249 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3250 group article-number
3251 (mapconcat 'identity actions ", ")))))
3254 10 "gnus-agent-expire: %s:%d: Article kept as \
3255 expiration tests failed." group article-number)
3256 (gnus-agent-append-to-list
3257 tail-alist (cons article-number fetch-date)))
3260 ;; Clean up markers as I want to recycle this buffer
3261 ;; over several groups.
3263 (set-marker marker nil))
3265 (setq dlist (cdr dlist))))
3267 (setq alist (cdr alist))
3269 (let ((inhibit-quit t))
3270 (unless (equal alist gnus-agent-article-alist)
3271 (setq gnus-agent-article-alist alist)
3272 (gnus-agent-save-alist group))
3274 (when (buffer-modified-p)
3275 (let ((coding-system-for-write
3276 gnus-agent-file-coding-system))
3277 (gnus-make-directory dir)
3278 (write-region (point-min) (point-max) nov-file nil
3280 ;; clear the modified flag as that I'm not confused by
3281 ;; its status on the next pass through this routine.
3282 (set-buffer-modified-p nil)))
3284 (when (eq articles t)
3285 (gnus-summary-update-info))))
3287 (when (boundp 'gnus-agent-expire-stats)
3288 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3289 (incf (nth 2 stats) bytes-freed)
3290 (incf (nth 1 stats) files-deleted)
3291 (incf (nth 0 stats) nov-entries-deleted)))
3294 (defun gnus-agent-expire (&optional articles group force)
3295 "Expire all old articles.
3296 If you want to force expiring of certain articles, this function can
3297 take ARTICLES, GROUP and FORCE parameters as well.
3299 The articles on which the expiration process runs are selected as follows:
3300 if ARTICLES is null, all read and unmarked articles.
3301 if ARTICLES is t, all articles.
3302 if ARTICLES is a list, just those articles.
3303 Setting GROUP will limit expiration to that group.
3304 FORCE is equivalent to setting the expiration predicates to true."
3308 (gnus-agent-expire-group group articles force)
3309 (if (or (not (eq articles t))
3310 (yes-or-no-p "Are you sure that you want to expire all \
3311 articles in every agentized group."))
3312 (let ((methods (gnus-agent-covered-methods))
3313 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3314 ;; of agent directories.
3315 (gnus-agent-expire-current-dirs nil)
3316 ;; Bind gnus-agent-expire-stats to enable tracking of
3317 ;; expiration statistics across all groups
3318 (gnus-agent-expire-stats (list 0 0 0.0))
3319 gnus-command-method overview orig)
3320 (setq overview (gnus-get-buffer-create " *expire overview*"))
3322 (while (setq gnus-command-method (pop methods))
3323 (let ((active-file (gnus-agent-lib-file "active")))
3324 (when (file-exists-p active-file)
3326 (nnheader-insert-file-contents active-file)
3327 (gnus-active-to-gnus-format
3329 (setq orig (gnus-make-hashtable
3330 (count-lines (point-min) (point-max))))))
3331 (dolist (expiring-group (gnus-groups-from-server
3332 gnus-command-method))
3334 (gnus-gethash-safe expiring-group orig)))
3338 (gnus-agent-expire-group-1
3339 expiring-group overview active articles force))))))))
3340 (kill-buffer overview))
3341 (gnus-agent-expire-unagentized-dirs)
3342 (gnus-message 4 (gnus-agent-expire-done-message))))))
3344 (defun gnus-agent-expire-done-message ()
3345 (if (and (> gnus-verbose 4)
3346 (boundp 'gnus-agent-expire-stats))
3347 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3348 (size (nth 2 stats))
3349 (units '(B KB MB GB)))
3350 (while (and (> size 1024.0)
3352 (setq size (/ size 1024.0)
3355 (format "Expiry recovered %d NOV entries, deleted %d files,\
3362 (defun gnus-agent-expire-unagentized-dirs ()
3363 (when (and gnus-agent-expire-unagentized-dirs
3364 (boundp 'gnus-agent-expire-current-dirs))
3365 (let* ((keep (gnus-make-hashtable))
3366 ;; Formally bind gnus-agent-expire-current-dirs so that the
3367 ;; compiler will not complain about free references.
3368 (gnus-agent-expire-current-dirs
3369 (symbol-value 'gnus-agent-expire-current-dirs))
3372 (gnus-sethash gnus-agent-directory t keep)
3373 (while gnus-agent-expire-current-dirs
3374 (setq dir (pop gnus-agent-expire-current-dirs))
3375 (when (and (stringp dir)
3376 (file-directory-p dir))
3377 (while (not (gnus-gethash dir keep))
3378 (gnus-sethash dir t keep)
3379 (setq dir (file-name-directory (directory-file-name dir))))))
3386 "Given a directory, check it and its subdirectories for
3387 membership in the keep hash. If it isn't found, add
3389 (let ((files (directory-files d))
3391 (while (setq file (pop files))
3392 (cond ((equal file ".") ; Ignore self
3394 ((equal file "..") ; Ignore parent
3396 ((equal file ".overview")
3397 ;; Directory must contain .overview to be
3398 ;; agent's cache of a group.
3399 (let ((d (file-name-as-directory d))
3401 ;; Search ancestor's for last directory NOT
3402 ;; found in keep hash.
3403 (while (not (gnus-gethash
3404 (setq d (file-name-directory d)) keep))
3406 d (directory-file-name d)))
3407 ;; if ANY ancestor was NOT in keep hash and
3408 ;; it it's already in to-remove, add it to
3411 (not (member r to-remove)))
3412 (push r to-remove))))
3413 ((file-directory-p (setq file (nnheader-concat d file)))
3414 (funcall checker file)))))))))
3415 (funcall checker (expand-file-name gnus-agent-directory))
3417 (when (and to-remove
3418 (or gnus-expert-user
3420 "gnus-agent-expire has identified local directories that are\
3421 not currently required by any agentized group. Do you wish to consider\
3424 (let ((dir (pop to-remove)))
3425 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3426 (let* (delete-recursive
3431 (if (file-directory-p f-or-d)
3433 (delete-directory f-or-d)
3436 (or (member f '("." ".."))
3437 (funcall delete-recursive
3440 (directory-files f-or-d))
3441 (delete-directory f-or-d)))
3442 (delete-file f-or-d)))))))
3443 (funcall delete-recursive dir))))))))))
3446 (defun gnus-agent-batch ()
3447 "Start Gnus, send queue and fetch session."
3449 (let ((init-file-user "")
3450 (gnus-always-read-dribble-file t))
3452 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3453 (gnus-group-send-queue)
3454 (gnus-agent-fetch-session)))
3456 (defun gnus-agent-unread-articles (group)
3457 (let* ((read (gnus-info-read (gnus-get-info group)))
3458 (known (gnus-agent-load-alist group))
3460 (tail-unread unread))
3461 (while (and known read)
3462 (let ((candidate (car (pop known))))
3463 (while (let* ((range (car read))
3464 (min (if (numberp range) range (car range)))
3465 (max (if (numberp range) range (cdr range))))
3466 (cond ((or (not min)
3468 (gnus-agent-append-to-list tail-unread candidate)
3471 (setq read (cdr read))
3472 ;; return t so that I always loop one more
3473 ;; time. If I just iterated off the end of
3474 ;; read, min will become nil and the current
3475 ;; candidate will be added to the unread list.
3478 (gnus-agent-append-to-list tail-unread (car (pop known))))
3481 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3482 "Restrict ARTICLES to numbers already fetched.
3483 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3484 that have already been fetched.
3485 If CACHED-HEADER is nil, articles are only excluded if the article itself
3488 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3489 ;; 'car gnus-agent-article-alist))
3491 ;; Functionally, I don't need to construct a temp list using mapcar.
3493 (if (and (or gnus-agent-cache (not gnus-plugged))
3494 (gnus-agent-load-alist group))
3495 (let* ((ref gnus-agent-article-alist)
3497 (uncached (list nil))
3498 (tail-uncached uncached))
3499 (while (and ref arts)
3500 (let ((v1 (car arts))
3502 (cond ((< v1 v2) ; v1 does not appear in the reference list
3503 (gnus-agent-append-to-list tail-uncached v1)
3504 (setq arts (cdr arts)))
3506 (unless (or cached-header (cdar ref)) ; v1 is already cached
3507 (gnus-agent-append-to-list tail-uncached v1))
3508 (setq arts (cdr arts))
3509 (setq ref (cdr ref)))
3510 (t ; reference article (v2) preceeds the list being filtered
3511 (setq ref (cdr ref))))))
3513 (gnus-agent-append-to-list tail-uncached (pop arts)))
3515 ;; if gnus-agent-load-alist fails, no articles are cached.
3518 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3520 (gnus-agent-create-buffer)
3521 (let ((gnus-decode-encoded-word-function 'identity)
3522 (file (gnus-agent-article-name ".overview" group))
3523 cached-articles uncached-articles)
3524 (gnus-make-directory (nnheader-translate-file-chars
3525 (file-name-directory file) t))
3527 ;; Populate temp buffer with known headers
3528 (when (file-exists-p file)
3529 (with-current-buffer gnus-agent-overview-buffer
3531 (let ((nnheader-file-coding-system
3532 gnus-agent-file-coding-system))
3533 (nnheader-insert-nov-file file (car articles)))))
3535 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3538 ;; Populate nntp-server-buffer with uncached headers
3539 (set-buffer nntp-server-buffer)
3541 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3542 (gnus-retrieve-headers
3543 uncached-articles group fetch-old))))
3544 (nnvirtual-convert-headers))
3545 ((eq 'nntp (car gnus-current-select-method))
3546 ;; The author of gnus-get-newsgroup-headers-xover
3547 ;; reports that the XOVER command is commonly
3548 ;; unreliable. The problem is that recently
3549 ;; posted articles may not be entered into the
3550 ;; NOV database in time to respond to my XOVER
3553 ;; I'm going to use his assumption that the NOV
3554 ;; database is updated in order of ascending
3555 ;; article ID. Therefore, a response containing
3556 ;; article ID N implies that all articles from 1
3557 ;; to N-1 are up-to-date. Therefore, missing
3558 ;; articles in that range have expired.
3560 (set-buffer nntp-server-buffer)
3561 (let* ((fetched-articles (list nil))
3562 (tail-fetched-articles fetched-articles)
3563 (min (cond ((numberp fetch-old)
3564 (max 1 (- (car articles) fetch-old)))
3569 (max (car (last articles))))
3571 ;; Get the list of articles that were fetched
3572 (goto-char (point-min))
3573 (let ((pm (point-max))
3575 (while (< (point) pm)
3576 (when (setq art (gnus-agent-read-article-number))
3577 (gnus-agent-append-to-list tail-fetched-articles art))
3580 ;; Clip this list to the headers that will
3581 ;; actually be returned
3582 (setq fetched-articles (gnus-list-range-intersection
3583 (cdr fetched-articles)
3586 ;; Clip the uncached articles list to exclude
3587 ;; IDs after the last FETCHED header. The
3588 ;; excluded IDs may be fetchable using HEAD.
3589 (if (car tail-fetched-articles)
3590 (setq uncached-articles
3591 (gnus-list-range-intersection
3593 (cons (car uncached-articles)
3594 (car tail-fetched-articles)))))
3596 ;; Create the list of articles that were
3597 ;; "successfully" fetched. Success, in this
3598 ;; case, means that the ID should not be
3599 ;; fetched again. In the case of an expired
3600 ;; article, the header will not be fetched.
3601 (setq uncached-articles
3602 (gnus-sorted-nunion fetched-articles
3606 ;; Erase the temp buffer
3607 (set-buffer gnus-agent-overview-buffer)
3610 ;; Copy the nntp-server-buffer to the temp buffer
3611 (set-buffer nntp-server-buffer)
3612 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3614 ;; Merge the temp buffer with the known headers (found on
3615 ;; disk in FILE) into the nntp-server-buffer
3616 (when uncached-articles
3617 (gnus-agent-braid-nov group uncached-articles file))
3619 ;; Save the new set of known headers to FILE
3620 (set-buffer nntp-server-buffer)
3621 (let ((coding-system-for-write
3622 gnus-agent-file-coding-system))
3623 (gnus-agent-check-overview-buffer)
3624 (write-region (point-min) (point-max) file nil 'silent))
3626 ;; Update the group's article alist to include the newly
3627 ;; fetched articles.
3628 (gnus-agent-load-alist group)
3629 (gnus-agent-save-alist group uncached-articles nil)
3632 ;; Copy the temp buffer to the nntp-server-buffer
3633 (set-buffer nntp-server-buffer)
3635 (insert-buffer-substring gnus-agent-overview-buffer)))
3638 (not (numberp fetch-old)))
3639 t ; Don't remove anything.
3640 (nnheader-nov-delete-outside-range
3641 (if fetch-old (max 1 (- (car articles) fetch-old))
3643 (car (last articles)))
3648 (defun gnus-agent-request-article (article group)
3649 "Retrieve ARTICLE in GROUP from the agent cache."
3650 (when (and gnus-agent
3651 (or gnus-agent-cache
3654 (let* ((gnus-command-method (gnus-find-method-for-group group))
3655 (file (gnus-agent-article-name (number-to-string article) group))
3656 (buffer-read-only nil))
3657 (when (and (file-exists-p file)
3658 (> (nth 7 (file-attributes file)) 0))
3660 (gnus-kill-all-overlays)
3661 (let ((coding-system-for-read gnus-cache-coding-system))
3662 (insert-file-contents file))
3665 (defun gnus-agent-regenerate-group (group &optional reread)
3667 If REREAD is t, all articles in the .overview are marked as unread.
3668 If REREAD is a list, the specified articles will be marked as unread.
3669 In addition, their NOV entries in .overview will be refreshed using
3670 the articles' current headers.
3671 If REREAD is not nil, downloaded articles are marked as unread."
3673 (list (let ((def (or (gnus-group-group-name)
3674 gnus-newsgroup-name)))
3675 (let ((select (read-string (if def
3676 (concat "Group Name ("
3679 (if (and (equal "" select)
3685 (cursor-in-echo-area t)
3686 (echo-keystrokes 0))
3687 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3688 (setq c (read-char-exclusive))
3690 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3692 ((or (eq c ?a) (eq c ?A))
3694 ((or (eq c ?d) (eq c ?D))
3695 (throw 'mark 'some)))
3696 (gnus-message 3 "Ignoring unexpected input")
3700 (gnus-message 5 "Regenerating in %s" group)
3701 (let* ((gnus-command-method (or gnus-command-method
3702 (gnus-find-method-for-group group)))
3703 (file (gnus-agent-article-name ".overview" group))
3704 (dir (file-name-directory file))
3706 (downloaded (if (file-exists-p dir)
3707 (sort (mapcar (lambda (name) (string-to-number name))
3708 (directory-files dir nil "^[0-9]+$" t))
3710 (progn (gnus-make-directory dir) nil)))
3715 (mm-with-unibyte-buffer
3716 (if (file-exists-p file)
3717 (let ((nnheader-file-coding-system
3718 gnus-agent-file-coding-system))
3719 (nnheader-insert-file-contents file)))
3720 (set-buffer-modified-p nil)
3722 ;; Load the article IDs found in the overview file. As a
3723 ;; side-effect, validate the file contents.
3727 (goto-char (point-min))
3728 (while (< (point) (point-max))
3729 (cond ((and (looking-at "[0-9]+\t")
3730 (<= (- (match-end 0) (match-beginning 0)) 9))
3731 (push (read (current-buffer)) nov-arts)
3733 (let ((l1 (car nov-arts))
3734 (l2 (cadr nov-arts)))
3735 (cond ((and (listp reread) (memq l1 reread))
3737 (setq nov-arts (cdr nov-arts))
3738 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3739 entry of article %s deleted." l1))
3743 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3744 entries are NOT in ascending order.")
3745 ;; Don't sort now as I haven't verified
3746 ;; that every line begins with a number
3750 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3751 entries contained duplicate of article %s. Duplicate deleted." l1)
3753 (setq nov-arts (cdr nov-arts))))))
3755 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3756 entries contained line that did not begin with an article number. Deleted\
3758 (gnus-delete-line))))
3760 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3761 entries into ascending order.")
3762 (sort-numeric-fields 1 (point-min) (point-max))
3763 (setq nov-arts nil))))
3764 (gnus-agent-check-overview-buffer)
3766 ;; Construct a new article alist whose nodes match every header
3767 ;; in the .overview file. As a side-effect, missing headers are
3768 ;; reconstructed from the downloaded article file.
3769 (while (or downloaded nov-arts)
3770 (cond ((and downloaded
3772 (> (car downloaded) (car nov-arts))))
3773 ;; This entry is missing from the overview file
3774 (gnus-message 3 "Regenerating NOV %s %d..." group
3776 (let ((file (concat dir (number-to-string (car downloaded)))))
3777 (mm-with-unibyte-buffer
3778 (nnheader-insert-file-contents file)
3779 (nnheader-remove-body)
3780 (setq header (nnheader-parse-naked-head)))
3781 (mail-header-set-number header (car downloaded))
3783 (let ((key (concat "^" (int-to-string (car nov-arts))
3785 (or (re-search-backward key nil t)
3786 (re-search-forward key))
3788 (goto-char (point-min)))
3789 (nnheader-insert-nov header))
3790 (setq nov-arts (cons (car downloaded) nov-arts)))
3791 ((eq (car downloaded) (car nov-arts))
3792 ;; This entry in the overview has been downloaded
3793 (push (cons (car downloaded)
3795 (nth 5 (file-attributes
3796 (concat dir (number-to-string
3797 (car downloaded))))))) alist)
3798 (setq downloaded (cdr downloaded))
3799 (setq nov-arts (cdr nov-arts)))
3801 ;; This entry in the overview has not been downloaded
3802 (push (cons (car nov-arts) nil) alist)
3803 (setq nov-arts (cdr nov-arts)))))
3805 ;; When gnus-agent-consider-all-articles is set,
3806 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3807 ;; the alist. Those IDs serve as markers to indicate that an
3808 ;; attempt has been made to fetch that article's header.
3810 ;; When gnus-agent-consider-all-articles is NOT set,
3811 ;; gnus-agent-regenerate-group can remove the article ID of every
3812 ;; article (with the exception of the last ID in the list - it's
3813 ;; special) that no longer appears in the overview. In this
3814 ;; situtation, the last article ID in the list implies that it,
3815 ;; and every article ID preceeding it, have been fetched from the
3818 (if gnus-agent-consider-all-articles
3819 ;; Restore all article IDs that were not found in the overview file.
3820 (let* ((n (cons nil alist))
3822 (o (gnus-agent-load-alist group)))
3824 (let ((nID (caadr n))
3827 (setq n (setcdr n (list (list oID))))
3830 (setcdr n (cons (list oID) (cdr n)))
3836 (setq n (cdr n))))))
3837 (setq alist (cdr merged)))
3838 ;; Restore the last article ID if it is not already in the new alist
3839 (let ((n (last alist))
3840 (o (last (gnus-agent-load-alist group))))
3844 (push (cons (caar o) nil) alist))
3845 ((< (caar n) (caar o))
3846 (setcdr n (list (car o)))))))
3848 (let ((inhibit-quit t))
3849 (if (setq regenerated (buffer-modified-p))
3850 (let ((coding-system-for-write gnus-agent-file-coding-system))
3851 (write-region (point-min) (point-max) file nil 'silent)))
3853 (setq regenerated (or regenerated
3854 (and reread gnus-agent-article-alist)
3855 (not (equal alist gnus-agent-article-alist))))
3857 (setq gnus-agent-article-alist alist)
3860 (gnus-agent-save-alist group)
3862 ;; I have to alter the group's active range NOW as
3863 ;; gnus-make-ascending-articles-unread will use it to
3864 ;; recalculate the number of unread articles in the group
3866 (let ((group (gnus-group-real-name group))
3867 (group-active (or (gnus-active group)
3868 (gnus-activate-group group))))
3869 (gnus-agent-possibly-alter-active group group-active)))))
3871 (when (and reread gnus-agent-article-alist)
3872 (gnus-make-ascending-articles-unread
3876 (delq nil (mapcar (function (lambda (c)
3877 (cond ((eq reread t)
3881 gnus-agent-article-alist))))
3883 (when (gnus-buffer-live-p gnus-group-buffer)
3884 (gnus-group-update-group group t)))
3890 (defun gnus-agent-regenerate (&optional clean reread)
3891 "Regenerate all agent covered files.
3892 If CLEAN, obsolete (ignore)."
3895 (gnus-message 4 "Regenerating Gnus agent files...")
3896 (dolist (gnus-command-method (gnus-agent-covered-methods))
3897 (dolist (group (gnus-groups-from-server gnus-command-method))
3898 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3900 (gnus-message 4 "Regenerating Gnus agent files...done")
3904 (defun gnus-agent-go-online (&optional force)
3905 "Switch servers into online status."
3906 (interactive (list t))
3907 (dolist (server gnus-opened-servers)
3908 (when (eq (nth 1 server) 'offline)
3909 (if (if (eq force 'ask)
3911 (format "Switch %s:%s into online status? "
3912 (caar server) (cadar server)))
3914 (setcar (nthcdr 1 server) 'close)))))
3916 (defun gnus-agent-toggle-group-plugged (group)
3917 "Toggle the status of the server of the current group."
3918 (interactive (list (gnus-group-group-name)))
3919 (let* ((method (gnus-find-method-for-group group))
3920 (status (cadr (assoc method gnus-opened-servers))))
3921 (if (eq status 'offline)
3922 (gnus-server-set-status method 'closed)
3923 (gnus-close-server method)
3924 (gnus-server-set-status method 'offline))
3925 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3926 (if (eq status 'offline) 'offline 'online)
3927 (if (eq status 'offline) 'online 'offline))))
3929 (defun gnus-agent-group-covered-p (group)
3930 (gnus-agent-method-p (gnus-group-method group)))
3932 (provide 'gnus-agent)
3934 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
3935 ;;; gnus-agent.el ends here