1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
39 (if (featurep 'xemacs)
42 (require 'gnus-group))
45 (autoload 'gnus-server-update-server "gnus-srvr")
46 (autoload 'gnus-agent-customize-category "gnus-cus")
49 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
50 "Where the Gnus agent will store its files."
54 (defcustom gnus-agent-plugged-hook nil
55 "Hook run when plugging into the network."
59 (defcustom gnus-agent-unplugged-hook nil
60 "Hook run when unplugging from the network."
64 (defcustom gnus-agent-fetched-hook nil
65 "Hook run when finished fetching articles."
70 (defcustom gnus-agent-handle-level gnus-level-subscribed
71 "Groups on levels higher than this variable will be ignored by the Agent."
75 (defcustom gnus-agent-expire-days 7
76 "Read articles older than this will be expired.
77 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
79 :type '(number :tag "days"))
81 (defcustom gnus-agent-expire-all nil
82 "If non-nil, also expire unread, ticked and dormant articles.
83 If nil, only read articles will be expired."
87 (defcustom gnus-agent-group-mode-hook nil
88 "Hook run in Agent group minor modes."
92 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
93 (when (featurep 'xemacs)
94 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
96 (defcustom gnus-agent-summary-mode-hook nil
97 "Hook run in Agent summary minor modes."
101 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
102 (when (featurep 'xemacs)
103 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
105 (defcustom gnus-agent-server-mode-hook nil
106 "Hook run in Agent summary minor modes."
110 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
111 (when (featurep 'xemacs)
112 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
114 (defcustom gnus-agent-confirmation-function 'y-or-n-p
115 "Function to confirm when error happens."
120 (defcustom gnus-agent-large-newsgroup nil
121 "*The number of articles which indicates a large newsgroup.
122 If the number of unread articles exceeds it, The number of articles to be
123 fetched will be limited to it. If not a positive integer, never consider it."
125 :type '(choice (const nil)
126 (integer :tag "Number")))
128 (defcustom gnus-agent-synchronize-flags 'ask
129 "Indicate if flags are synchronized when you plug in.
130 If this is `ask' the hook will query the user."
132 :type '(choice (const :tag "Always" t)
133 (const :tag "Never" nil)
134 (const :tag "Ask" ask))
137 (defcustom gnus-agent-go-online 'ask
138 "Indicate if offline servers go online when you plug in.
139 If this is `ask' the hook will query the user."
141 :type '(choice (const :tag "Always" t)
142 (const :tag "Never" nil)
143 (const :tag "Ask" ask))
146 (defcustom gnus-agent-mark-unread-after-downloaded t
147 "Indicate whether to mark articles unread after downloaded."
152 (defcustom gnus-agent-download-marks '(download)
153 "Marks for downloading."
155 :type '(repeat (symbol :tag "Mark"))
158 (defcustom gnus-agent-consider-all-articles nil
159 "When non-nil, the agent will let the agent predicate decide
160 whether articles need to be downloaded or not, for all articles. When
161 nil, the default, the agent will only let the predicate decide
162 whether unread articles are downloaded or not. If you enable this,
163 groups with large active ranges may open slower and you may also want
164 to look into the agent expiry settings to block the expiration of
165 read articles as they would just be downloaded again."
170 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
171 "Chunk size for `gnus-agent-fetch-session'.
172 The function will split its article fetches into chunks smaller than
177 (defcustom gnus-agent-enable-expiration 'ENABLE
178 "The default expiration state for each group.
179 When set to ENABLE, the default, `gnus-agent-expire' will expire old
180 contents from a group's local storage. This value may be overridden
181 to disable expiration in specific categories, topics, and groups. Of
182 course, you could change gnus-agent-enable-expiration to DISABLE then
183 enable expiration per categories, topics, and groups."
185 :type '(radio (const :format "Enable " ENABLE)
186 (const :format "Disable " DISABLE)))
188 (defcustom gnus-agent-expire-unagentized-dirs t
189 "*Whether expiration should expire in unagentized directories.
190 Have gnus-agent-expire scan the directories under
191 \(gnus-agent-directory) for groups that are no longer agentized.
192 When found, offer to remove them."
197 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
198 "Initially, all servers from these methods are agentized.
199 The user may remove or add servers using the Server buffer.
200 See Info node `(gnus)Server Buffer'."
202 :type '(repeat symbol)
205 (defcustom gnus-agent-queue-mail t
206 "Whether and when outgoing mail should be queued by the agent.
207 When `always', always queue outgoing mail. When nil, never
208 queue. Otherwise, queue if and only if unplugged."
210 :type '(radio (const :format "Always" always)
211 (const :format "Never" nil)
212 (const :format "When plugged" t)))
214 (defcustom gnus-agent-prompt-send-queue nil
215 "If non-nil, `gnus-group-send-queue' will prompt if called when
220 ;;; Internal variables
222 (defvar gnus-agent-history-buffers nil)
223 (defvar gnus-agent-buffer-alist nil)
224 (defvar gnus-agent-article-alist nil
225 "An assoc list identifying the articles whose headers have been fetched.
226 If successfully fetched, these headers will be stored in the group's overview
227 file. The key of each assoc pair is the article ID, the value of each assoc
228 pair is a flag indicating whether the identified article has been downloaded
229 \(gnus-agent-fetch-articles sets the value to the day of the download).
231 1) The last element of this list can not be expired as some
232 routines (for example, get-agent-fetch-headers) use the last
233 value to track which articles have had their headers retrieved.
234 2) The function `gnus-agent-regenerate' may destructively modify the value.")
235 (defvar gnus-agent-group-alist nil)
236 (defvar gnus-category-alist nil)
237 (defvar gnus-agent-current-history nil)
238 (defvar gnus-agent-overview-buffer nil)
239 (defvar gnus-category-predicate-cache nil)
240 (defvar gnus-category-group-cache nil)
241 (defvar gnus-agent-spam-hashtb nil)
242 (defvar gnus-agent-file-name nil)
243 (defvar gnus-agent-send-mail-function nil)
244 (defvar gnus-agent-file-coding-system 'raw-text)
245 (defvar gnus-agent-file-loading-cache nil)
246 (defvar gnus-agent-total-fetched-hashtb nil)
247 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
248 (defvar gnus-agent-need-update-total-fetched-for nil)
251 (defvar gnus-headers)
258 (defun gnus-open-agent ()
260 (gnus-agent-read-servers)
262 (gnus-agent-create-buffer)
263 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
264 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
265 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
267 (defun gnus-agent-create-buffer ()
268 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
270 (setq gnus-agent-overview-buffer
271 (gnus-get-buffer-create " *Gnus agent overview*"))
272 (with-current-buffer gnus-agent-overview-buffer
273 (set-buffer-multibyte t))
276 (gnus-add-shutdown 'gnus-close-agent 'gnus)
278 (defun gnus-close-agent ()
279 (setq gnus-category-predicate-cache nil
280 gnus-category-group-cache nil
281 gnus-agent-spam-hashtb nil)
282 (gnus-kill-buffer gnus-agent-overview-buffer))
285 ;;; Utility functions
288 (defmacro gnus-agent-with-refreshed-group (group &rest body)
289 "Performs the body then updates the group's line in the group
290 buffer. Automatically blocks multiple updates due to recursion."
291 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
292 (when (and gnus-agent-need-update-total-fetched-for
293 (not gnus-agent-inhibit-update-total-fetched-for))
295 (set-buffer gnus-group-buffer)
296 (setq gnus-agent-need-update-total-fetched-for nil)
297 (gnus-group-update-group ,group t)))))
299 (defun gnus-agent-read-file (file)
300 "Load FILE and do a `read' there."
303 (nnheader-insert-file-contents file)
304 (goto-char (point-min))
305 (read (current-buffer)))))
307 (defsubst gnus-agent-method ()
308 (concat (symbol-name (car gnus-command-method)) "/"
309 (if (equal (cadr gnus-command-method) "")
311 (cadr gnus-command-method))))
313 (defsubst gnus-agent-directory ()
314 "The name of the Gnus agent directory."
315 (nnheader-concat gnus-agent-directory
316 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
318 (defun gnus-agent-lib-file (file)
319 "The full name of the Gnus agent library FILE."
320 (expand-file-name file
321 (file-name-as-directory
322 (expand-file-name "agent.lib" (gnus-agent-directory)))))
324 (defun gnus-agent-cat-set-property (category property value)
326 (setcdr (or (assq property category)
327 (let ((cell (cons property nil)))
328 (setcdr category (cons cell (cdr category)))
330 (let ((category category))
331 (while (cond ((eq property (caadr category))
332 (setcdr category (cddr category))
335 (setq category (cdr category)))))))
339 (defmacro gnus-agent-cat-defaccessor (name prop-name)
340 "Define accessor and setter methods for manipulating a list of the form
341 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
342 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
343 manipulated as follows:
344 (func LIST): Returns VALUE1
345 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
346 `(progn (defmacro ,name (category)
347 (list (quote cdr) (list (quote assq)
348 (quote (quote ,prop-name)) category)))
350 (define-setf-method ,name (category)
351 (let* ((--category--temp-- (make-symbol "--category--"))
352 (--value--temp-- (make-symbol "--value--")))
353 (list (list --category--temp--) ; temporary-variables
354 (list category) ; value-forms
355 (list --value--temp--) ; store-variables
356 (let* ((category --category--temp--) ; store-form
357 (value --value--temp--))
358 (list (quote gnus-agent-cat-set-property)
360 (quote (quote ,prop-name))
362 (list (quote ,name) --category--temp--) ; access-form
366 (defmacro gnus-agent-cat-name (category)
369 (gnus-agent-cat-defaccessor
370 gnus-agent-cat-days-until-old agent-days-until-old)
371 (gnus-agent-cat-defaccessor
372 gnus-agent-cat-enable-expiration agent-enable-expiration)
373 (gnus-agent-cat-defaccessor
374 gnus-agent-cat-groups agent-groups)
375 (gnus-agent-cat-defaccessor
376 gnus-agent-cat-high-score agent-high-score)
377 (gnus-agent-cat-defaccessor
378 gnus-agent-cat-length-when-long agent-length-when-long)
379 (gnus-agent-cat-defaccessor
380 gnus-agent-cat-length-when-short agent-length-when-short)
381 (gnus-agent-cat-defaccessor
382 gnus-agent-cat-low-score agent-low-score)
383 (gnus-agent-cat-defaccessor
384 gnus-agent-cat-predicate agent-predicate)
385 (gnus-agent-cat-defaccessor
386 gnus-agent-cat-score-file agent-score-file)
387 (gnus-agent-cat-defaccessor
388 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
391 ;; This form is equivalent to defsetf except that it calls make-symbol
392 ;; whereas defsetf calls gensym (Using gensym creates a run-time
393 ;; dependency on the CL library).
396 (define-setf-method gnus-agent-cat-groups (category)
397 (let* ((--category--temp-- (make-symbol "--category--"))
398 (--groups--temp-- (make-symbol "--groups--")))
399 (list (list --category--temp--)
401 (list --groups--temp--)
402 (let* ((category --category--temp--)
403 (groups --groups--temp--))
404 (list (quote gnus-agent-set-cat-groups) category groups))
405 (list (quote gnus-agent-cat-groups) --category--temp--))))
408 (defun gnus-agent-set-cat-groups (category groups)
409 (unless (eq groups 'ignore)
411 (old-g (gnus-agent-cat-groups category)))
412 (cond ((eq new-g old-g)
413 ;; gnus-agent-add-group is fiddling with the group
414 ;; list. Still, Im done.
417 ((eq new-g (cdr old-g))
418 ;; gnus-agent-add-group is fiddling with the group list
419 (setcdr (or (assq 'agent-groups category)
420 (let ((cell (cons 'agent-groups nil)))
421 (setcdr category (cons cell (cdr category)))
424 (let ((groups groups))
426 (let* ((group (pop groups))
427 (old-category (gnus-group-category group)))
428 (if (eq category old-category)
430 (setf (gnus-agent-cat-groups old-category)
431 (delete group (gnus-agent-cat-groups
433 ;; Purge cache as preceeding loop invalidated it.
434 (setq gnus-category-group-cache nil))
436 (setcdr (or (assq 'agent-groups category)
437 (let ((cell (cons 'agent-groups nil)))
438 (setcdr category (cons cell (cdr category)))
441 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
442 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
444 ;;; Fetching setup functions.
446 (defun gnus-agent-start-fetch ()
447 "Initialize data structures for efficient fetching."
448 (gnus-agent-create-buffer))
450 (defun gnus-agent-stop-fetch ()
451 "Save all data structures and clean up."
452 (setq gnus-agent-spam-hashtb nil)
454 (set-buffer nntp-server-buffer)
457 (defmacro gnus-agent-with-fetch (&rest forms)
460 (let ((gnus-agent-fetching t))
461 (gnus-agent-start-fetch)
463 (gnus-agent-stop-fetch)))
465 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
466 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
468 (defmacro gnus-agent-append-to-list (tail value)
469 `(setq ,tail (setcdr ,tail (cons ,value nil))))
471 (defmacro gnus-agent-message (level &rest args)
472 `(if (<= ,level gnus-verbose)
479 (defvar gnus-agent-mode-hook nil
480 "Hook run when installing agent mode.")
482 (defvar gnus-agent-mode nil)
483 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
485 (defun gnus-agent-mode ()
486 "Minor mode for providing a agent support in Gnus buffers."
487 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
488 (symbol-name major-mode))
489 (match-string 1 (symbol-name major-mode))))
490 (mode (intern (format "gnus-agent-%s-mode" buffer))))
491 (set (make-local-variable 'gnus-agent-mode) t)
493 (set (make-local-variable mode) t)
495 (when (gnus-visual-p 'agent-menu 'menu)
496 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
497 (unless (assq 'gnus-agent-mode minor-mode-alist)
498 (push gnus-agent-mode-status minor-mode-alist))
499 (unless (assq mode minor-mode-map-alist)
500 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
502 minor-mode-map-alist))
503 (when (eq major-mode 'gnus-group-mode)
504 (let ((init-plugged gnus-plugged)
505 (gnus-agent-go-online nil))
506 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
507 ;; Therefore, make certain that the current value does not
508 ;; match the desired initial value.
509 (setq gnus-plugged :unknown)
510 (gnus-agent-toggle-plugged init-plugged)))
511 (gnus-run-hooks 'gnus-agent-mode-hook
512 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
514 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
515 (gnus-define-keys gnus-agent-group-mode-map
516 "Ju" gnus-agent-fetch-groups
517 "Jc" gnus-enter-category-buffer
518 "Jj" gnus-agent-toggle-plugged
519 "Js" gnus-agent-fetch-session
520 "JY" gnus-agent-synchronize-flags
521 "JS" gnus-group-send-queue
522 "Ja" gnus-agent-add-group
523 "Jr" gnus-agent-remove-group
524 "Jo" gnus-agent-toggle-group-plugged)
526 (defun gnus-agent-group-make-menu-bar ()
527 (unless (boundp 'gnus-agent-group-menu)
529 gnus-agent-group-menu gnus-agent-group-mode-map ""
531 ["Toggle plugged" gnus-agent-toggle-plugged t]
532 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
533 ["List categories" gnus-enter-category-buffer t]
534 ["Add (current) group to category" gnus-agent-add-group t]
535 ["Remove (current) group from category" gnus-agent-remove-group t]
536 ["Send queue" gnus-group-send-queue gnus-plugged]
538 ["All" gnus-agent-fetch-session gnus-plugged]
539 ["Group" gnus-agent-fetch-group gnus-plugged])
540 ["Synchronize flags" gnus-agent-synchronize-flags t]
543 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
544 (gnus-define-keys gnus-agent-summary-mode-map
545 "Jj" gnus-agent-toggle-plugged
546 "Ju" gnus-agent-summary-fetch-group
547 "JS" gnus-agent-fetch-group
548 "Js" gnus-agent-summary-fetch-series
549 "J#" gnus-agent-mark-article
550 "J\M-#" gnus-agent-unmark-article
551 "@" gnus-agent-toggle-mark
552 "Jc" gnus-agent-catchup)
554 (defun gnus-agent-summary-make-menu-bar ()
555 (unless (boundp 'gnus-agent-summary-menu)
557 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
559 ["Toggle plugged" gnus-agent-toggle-plugged t]
560 ["Mark as downloadable" gnus-agent-mark-article t]
561 ["Unmark as downloadable" gnus-agent-unmark-article t]
562 ["Toggle mark" gnus-agent-toggle-mark t]
563 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
564 ["Catchup undownloaded" gnus-agent-catchup t]))))
566 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
567 (gnus-define-keys gnus-agent-server-mode-map
568 "Jj" gnus-agent-toggle-plugged
569 "Ja" gnus-agent-add-server
570 "Jr" gnus-agent-remove-server)
572 (defun gnus-agent-server-make-menu-bar ()
573 (unless (boundp 'gnus-agent-server-menu)
575 gnus-agent-server-menu gnus-agent-server-mode-map ""
577 ["Toggle plugged" gnus-agent-toggle-plugged t]
578 ["Add" gnus-agent-add-server t]
579 ["Remove" gnus-agent-remove-server t]))))
581 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
582 (if (and (fboundp 'propertize)
583 (fboundp 'make-mode-line-mouse-map))
584 (propertize string 'local-map
585 (make-mode-line-mouse-map mouse-button mouse-func))
588 (defun gnus-agent-toggle-plugged (set-to)
589 "Toggle whether Gnus is unplugged or not."
590 (interactive (list (not gnus-plugged)))
591 (cond ((eq set-to gnus-plugged)
594 (setq gnus-plugged set-to)
595 (gnus-run-hooks 'gnus-agent-plugged-hook)
596 (setcar (cdr gnus-agent-mode-status)
597 (gnus-agent-make-mode-line-string " Plugged"
599 'gnus-agent-toggle-plugged))
600 (gnus-agent-go-online gnus-agent-go-online)
601 (gnus-agent-possibly-synchronize-flags))
603 (gnus-agent-close-connections)
604 (setq gnus-plugged set-to)
605 (gnus-run-hooks 'gnus-agent-unplugged-hook)
606 (setcar (cdr gnus-agent-mode-status)
607 (gnus-agent-make-mode-line-string " Unplugged"
609 'gnus-agent-toggle-plugged))))
610 (force-mode-line-update)
611 (set-buffer-modified-p t))
613 (defmacro gnus-agent-while-plugged (&rest body)
614 `(let ((original-gnus-plugged gnus-plugged))
616 (progn (gnus-agent-toggle-plugged t)
618 (gnus-agent-toggle-plugged original-gnus-plugged))))
620 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
621 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
623 (defun gnus-agent-close-connections ()
624 "Close all methods covered by the Gnus agent."
625 (let ((methods (gnus-agent-covered-methods)))
627 (gnus-close-server (pop methods)))))
630 (defun gnus-unplugged ()
631 "Start Gnus unplugged."
633 (setq gnus-plugged nil)
637 (defun gnus-plugged ()
638 "Start Gnus plugged."
640 (setq gnus-plugged t)
644 (defun gnus-slave-unplugged (&optional arg)
645 "Read news as a slave unplugged."
647 (setq gnus-plugged nil)
648 (gnus arg nil 'slave))
651 (defun gnus-agentize ()
652 "Allow Gnus to be an offline newsreader.
654 The gnus-agentize function is now called internally by gnus when
655 gnus-agent is set. If you wish to avoid calling gnus-agentize,
656 customize gnus-agent to nil.
658 This will modify the `gnus-setup-news-hook', and
659 `message-send-mail-real-function' variables, and install the Gnus agent
660 minor mode in all Gnus buffers."
663 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
664 (unless gnus-agent-send-mail-function
665 (setq gnus-agent-send-mail-function
666 (or message-send-mail-real-function
667 (function (lambda () (funcall message-send-mail-function))))
668 message-send-mail-real-function 'gnus-agent-send-mail))
670 ;; If the servers file doesn't exist, auto-agentize some servers and
671 ;; save the servers file so this auto-agentizing isn't invoked
673 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
674 (gnus-message 3 "First time agent user, agentizing remote groups...")
676 (lambda (server-or-method)
677 (let ((method (gnus-server-to-method server-or-method)))
678 (when (memq (car method)
679 gnus-agent-auto-agentize-methods)
680 (push (gnus-method-to-server method)
681 gnus-agent-covered-methods)
682 (setq gnus-agent-method-p-cache nil))))
683 (cons gnus-select-method gnus-secondary-select-methods))
684 (gnus-agent-write-servers)))
686 (defun gnus-agent-queue-setup (&optional group-name)
687 "Make sure the queue group exists.
688 Optional arg GROUP-NAME allows to specify another group."
689 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
691 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
692 (let ((gnus-level-default-subscribed 1))
693 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
695 (gnus-group-set-parameter
696 (format "nndraft:%s" (or group-name "queue"))
697 'gnus-dummy '((gnus-draft-mode)))))
699 (defun gnus-agent-send-mail ()
700 (if (or (not gnus-agent-queue-mail)
701 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
702 (funcall gnus-agent-send-mail-function)
703 (goto-char (point-min))
705 (concat "^" (regexp-quote mail-header-separator) "\n"))
707 (gnus-agent-insert-meta-information 'mail)
708 (gnus-request-accept-article "nndraft:queue" nil t t)))
710 (defun gnus-agent-insert-meta-information (type &optional method)
711 "Insert meta-information into the message that says how it's to be posted.
712 TYPE can be either `mail' or `news'. If the latter, then METHOD can
715 (message-remove-header gnus-agent-meta-information-header)
716 (goto-char (point-min))
717 (insert gnus-agent-meta-information-header ": "
718 (symbol-name type) " " (format "%S" method)
721 (while (search-backward "\n" nil t)
722 (replace-match "\\n" t t))))
724 (defun gnus-agent-restore-gcc ()
725 "Restore GCC field from saved header."
727 (goto-char (point-min))
728 (while (re-search-forward
729 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
730 (replace-match "Gcc:" 'fixedcase))))
732 (defun gnus-agent-any-covered-gcc ()
734 (message-narrow-to-headers)
735 (let* ((gcc (mail-fetch-field "gcc" nil t))
737 (mapcar 'gnus-inews-group-method
738 (message-unquote-tokens
739 (message-tokenize-header
742 (while (and (not covered) methods)
743 (setq covered (gnus-agent-method-p (car methods))
744 methods (cdr methods)))
748 (defun gnus-agent-possibly-save-gcc ()
749 "Save GCC if Gnus is unplugged."
750 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
752 (goto-char (point-min))
753 (let ((case-fold-search t))
754 (while (re-search-forward "^gcc:" nil t)
755 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
757 (defun gnus-agent-possibly-do-gcc ()
758 "Do GCC if Gnus is plugged."
759 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
760 (gnus-inews-do-gcc)))
763 ;;; Group mode commands
766 (defun gnus-agent-fetch-groups (n)
767 "Put all new articles in the current groups into the Agent."
770 (error "Groups can't be fetched when Gnus is unplugged"))
771 (gnus-group-iterate n 'gnus-agent-fetch-group))
773 (defun gnus-agent-fetch-group (&optional group)
774 "Put all new articles in GROUP into the Agent."
775 (interactive (list (gnus-group-group-name)))
776 (setq group (or group gnus-newsgroup-name))
778 (error "No group on the current line"))
780 (gnus-agent-while-plugged
781 (let ((gnus-command-method (gnus-find-method-for-group group)))
782 (gnus-agent-with-fetch
783 (gnus-agent-fetch-group-1 group gnus-command-method)
784 (gnus-message 5 "Fetching %s...done" group)))))
786 (defun gnus-agent-add-group (category arg)
787 "Add the current group to an agent category."
793 (mapcar (lambda (cat) (list (symbol-name (car cat))))
797 (let ((cat (assq category gnus-category-alist))
799 (gnus-group-iterate arg
801 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
802 (setf (gnus-agent-cat-groups c)
803 (delete group (gnus-agent-cat-groups c))))
804 (push group groups)))
805 (setf (gnus-agent-cat-groups cat)
806 (nconc (gnus-agent-cat-groups cat) groups))
807 (gnus-category-write)))
809 (defun gnus-agent-remove-group (arg)
810 "Remove the current group from its agent category, if any."
813 (gnus-group-iterate arg
815 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
816 (setf (gnus-agent-cat-groups c)
817 (delete group (gnus-agent-cat-groups c))))))
818 (gnus-category-write)))
820 (defun gnus-agent-synchronize-flags ()
821 "Synchronize unplugged flags with servers."
824 (dolist (gnus-command-method (gnus-agent-covered-methods))
825 (when (file-exists-p (gnus-agent-lib-file "flags"))
826 (gnus-agent-synchronize-flags-server gnus-command-method)))))
828 (defun gnus-agent-possibly-synchronize-flags ()
829 "Synchronize flags according to `gnus-agent-synchronize-flags'."
832 (dolist (gnus-command-method (gnus-agent-covered-methods))
833 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
834 (not (eq (gnus-server-status gnus-command-method) 'offline)))
835 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
837 (defun gnus-agent-synchronize-flags-server (method)
838 "Synchronize flags set when unplugged for server."
839 (let ((gnus-command-method method)
841 (when (file-exists-p (gnus-agent-lib-file "flags"))
842 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
844 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
845 (cond ((null gnus-plugged)
847 1 "You must be plugged to synchronize flags with server %s"
848 (nth 1 gnus-command-method)))
849 ((null (gnus-check-server gnus-command-method))
851 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
856 (eval (read (current-buffer)))
857 (delete-region bgn (point))))
859 (delete-file (gnus-agent-lib-file "flags")))
861 (let ((file (gnus-agent-lib-file "flags")))
863 (error "Couldn't set flags from file %s due to %s"
864 file (error-message-string err)))))))
867 (defun gnus-agent-possibly-synchronize-flags-server (method)
868 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
869 (when (or (and gnus-agent-synchronize-flags
870 (not (eq gnus-agent-synchronize-flags 'ask)))
871 (and (eq gnus-agent-synchronize-flags 'ask)
872 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
874 (gnus-agent-synchronize-flags-server method)))
877 (defun gnus-agent-rename-group (old-group new-group)
878 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
879 disabled, as the old agent files would corrupt gnus when the agent was
880 next enabled. Depends upon the caller to determine whether group renaming is supported."
881 (let* ((old-command-method (gnus-find-method-for-group old-group))
882 (old-path (directory-file-name
883 (let (gnus-command-method old-command-method)
884 (gnus-agent-group-pathname old-group))))
885 (new-command-method (gnus-find-method-for-group new-group))
886 (new-path (directory-file-name
887 (let (gnus-command-method new-command-method)
888 (gnus-agent-group-pathname new-group)))))
889 (gnus-rename-file old-path new-path t)
891 (let* ((old-real-group (gnus-group-real-name old-group))
892 (new-real-group (gnus-group-real-name new-group))
893 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
894 (gnus-agent-save-group-info old-command-method old-real-group nil)
895 (gnus-agent-save-group-info new-command-method new-real-group old-active)
897 (let ((old-local (gnus-agent-get-local old-group
898 old-real-group old-command-method)))
899 (gnus-agent-set-local old-group
901 old-real-group old-command-method)
902 (gnus-agent-set-local new-group
903 (car old-local) (cdr old-local)
904 new-real-group new-command-method)))))
907 (defun gnus-agent-delete-group (group)
908 "Delete fully-qualified GROUP. Always updates the agent, even when
909 disabled, as the old agent files would corrupt gnus when the agent was
910 next enabled. Depends upon the caller to determine whether group deletion is supported."
911 (let* ((command-method (gnus-find-method-for-group group))
912 (path (directory-file-name
913 (let (gnus-command-method command-method)
914 (gnus-agent-group-pathname group)))))
915 (gnus-delete-file path)
917 (let* ((real-group (gnus-group-real-name group)))
918 (gnus-agent-save-group-info command-method real-group nil)
920 (let ((local (gnus-agent-get-local group
921 real-group command-method)))
922 (gnus-agent-set-local group
924 real-group command-method)))))
927 ;;; Server mode commands
930 (defun gnus-agent-add-server ()
931 "Enroll SERVER in the agent program."
933 (let* ((server (gnus-server-server-name))
934 (named-server (gnus-server-named-server))
936 (gnus-server-get-method nil server))))
938 (error "No server on the current line"))
940 (when (gnus-agent-method-p method)
941 (error "Server already in the agent program"))
943 (push named-server gnus-agent-covered-methods)
945 (setq gnus-agent-method-p-cache nil)
946 (gnus-server-update-server server)
947 (gnus-agent-write-servers)
948 (gnus-message 1 "Entered %s into the Agent" server)))
950 (defun gnus-agent-remove-server ()
951 "Remove SERVER from the agent program."
953 (let* ((server (gnus-server-server-name))
954 (named-server (gnus-server-named-server)))
956 (error "No server on the current line"))
958 (unless (member named-server gnus-agent-covered-methods)
959 (error "Server not in the agent program"))
961 (setq gnus-agent-covered-methods
962 (delete named-server gnus-agent-covered-methods)
963 gnus-agent-method-p-cache nil)
965 (gnus-server-update-server server)
966 (gnus-agent-write-servers)
967 (gnus-message 1 "Removed %s from the agent" server)))
969 (defun gnus-agent-read-servers ()
970 "Read the alist of covered servers."
971 (setq gnus-agent-covered-methods
972 (gnus-agent-read-file
973 (nnheader-concat gnus-agent-directory "lib/servers"))
974 gnus-agent-method-p-cache nil)
976 ;; I am called so early in start-up that I can not validate server
977 ;; names. When that is the case, I skip the validation. That is
978 ;; alright as the gnus startup code calls the validate methods
980 (if gnus-server-alist
981 (gnus-agent-read-servers-validate)))
983 (defun gnus-agent-read-servers-validate ()
984 (mapcar (lambda (server-or-method)
985 (let* ((server (if (stringp server-or-method)
987 (gnus-method-to-server server-or-method)))
988 (method (gnus-server-to-method server)))
990 (unless (member server gnus-agent-covered-methods)
991 (push server gnus-agent-covered-methods)
992 (setq gnus-agent-method-p-cache nil))
993 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
994 (prog1 gnus-agent-covered-methods
995 (setq gnus-agent-covered-methods nil))))
997 (defun gnus-agent-read-servers-validate-native (native-method)
998 (setq gnus-agent-covered-methods
999 (mapcar (lambda (method)
1000 (if (or (not method)
1001 (equal method native-method))
1003 method)) gnus-agent-covered-methods)))
1005 (defun gnus-agent-write-servers ()
1006 "Write the alist of covered servers."
1007 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1008 (let ((coding-system-for-write nnheader-file-coding-system)
1009 (file-name-coding-system nnmail-pathname-coding-system))
1010 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1011 (prin1 gnus-agent-covered-methods
1012 (current-buffer)))))
1015 ;;; Summary commands
1018 (defun gnus-agent-mark-article (n &optional unmark)
1019 "Mark the next N articles as downloadable.
1020 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1021 the mark instead. The difference between N and the actual number of
1022 articles marked is returned."
1024 (let ((backward (< n 0))
1029 (gnus-summary-set-agent-mark
1030 (gnus-summary-article-number) unmark)
1031 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1034 (gnus-message 7 "No more articles"))
1035 (gnus-summary-recenter)
1036 (gnus-summary-position-point)
1039 (defun gnus-agent-unmark-article (n)
1040 "Remove the downloadable mark from the next N articles.
1041 If N is negative, unmark backward instead. The difference between N and
1042 the actual number of articles unmarked is returned."
1044 (gnus-agent-mark-article n t))
1046 (defun gnus-agent-toggle-mark (n)
1047 "Toggle the downloadable mark from the next N articles.
1048 If N is negative, toggle backward instead. The difference between N and
1049 the actual number of articles toggled is returned."
1051 (gnus-agent-mark-article n 'toggle))
1053 (defun gnus-summary-set-agent-mark (article &optional unmark)
1054 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1055 When UNMARK is t, the article is unmarked. For any other value, the
1056 article's mark is toggled."
1057 (let ((unmark (cond ((eq nil unmark)
1062 (memq article gnus-newsgroup-downloadable)))))
1063 (when (gnus-summary-goto-subject article nil t)
1064 (gnus-summary-update-mark
1067 (setq gnus-newsgroup-downloadable
1068 (delq article gnus-newsgroup-downloadable))
1069 (gnus-article-mark article))
1070 (setq gnus-newsgroup-downloadable
1071 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1072 gnus-downloadable-mark)
1076 (defun gnus-agent-get-undownloaded-list ()
1077 "Construct list of articles that have not been downloaded."
1078 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1079 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1080 (gnus-agent-method-p gnus-command-method))
1081 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1082 (headers (sort (mapcar (lambda (h)
1083 (mail-header-number h))
1084 gnus-newsgroup-headers) '<))
1085 (cached (and gnus-use-cache gnus-newsgroup-cached))
1086 (undownloaded (list nil))
1087 (tail-undownloaded undownloaded)
1088 (unfetched (list nil))
1089 (tail-unfetched unfetched))
1090 (while (and alist headers)
1091 (let ((a (caar alist))
1094 ;; Ignore IDs in the alist that are not being
1095 ;; displayed in the summary.
1096 (setq alist (cdr alist)))
1098 ;; Headers that are not in the alist should be
1099 ;; fictious (see nnagent-retrieve-headers); they
1100 ;; imply that this article isn't in the agent.
1101 (gnus-agent-append-to-list tail-undownloaded h)
1102 (gnus-agent-append-to-list tail-unfetched h)
1103 (setq headers (cdr headers)))
1105 (setq alist (cdr alist))
1106 (setq headers (cdr headers))
1107 nil ; ignore already downloaded
1110 (setq alist (cdr alist))
1111 (setq headers (cdr headers))
1113 ;; This article isn't in the agent. Check to see
1114 ;; if it is in the cache. If it is, it's been
1116 (while (and cached (< (car cached) a))
1117 (setq cached (cdr cached)))
1118 (unless (equal a (car cached))
1119 (gnus-agent-append-to-list tail-undownloaded a))))))
1122 (let ((num (pop headers)))
1123 (gnus-agent-append-to-list tail-undownloaded num)
1124 (gnus-agent-append-to-list tail-unfetched num)))
1126 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1127 gnus-newsgroup-unfetched (cdr unfetched))))))
1129 (defun gnus-agent-catchup ()
1130 "Mark as read all unhandled articles.
1131 An article is unhandled if it is neither cached, nor downloaded, nor
1135 (let ((articles gnus-newsgroup-undownloaded))
1136 (when (or gnus-newsgroup-downloadable
1137 gnus-newsgroup-cached)
1138 (setq articles (gnus-sorted-ndifference
1139 (gnus-sorted-ndifference
1140 (gnus-copy-sequence articles)
1141 gnus-newsgroup-downloadable)
1142 gnus-newsgroup-cached)))
1145 (gnus-summary-mark-article
1146 (pop articles) gnus-catchup-mark)))
1147 (gnus-summary-position-point)))
1149 (defun gnus-agent-summary-fetch-series ()
1151 (when gnus-newsgroup-processable
1152 (setq gnus-newsgroup-downloadable
1153 (let* ((dl gnus-newsgroup-downloadable)
1154 (gnus-newsgroup-downloadable
1155 (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1156 (fetched-articles (gnus-agent-summary-fetch-group)))
1157 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1158 ;; updated gnus-newsgroup-downloadable to remove each
1159 ;; article successfully fetched.
1161 ;; For each article that I processed, remove its
1162 ;; processable mark IF the article is no longer
1163 ;; downloadable (i.e. it's already downloaded)
1164 (dolist (article gnus-newsgroup-processable)
1165 (unless (memq article gnus-newsgroup-downloadable)
1166 (gnus-summary-remove-process-mark article)))
1167 (gnus-sorted-ndifference dl fetched-articles)))))
1169 (defun gnus-agent-summary-fetch-group (&optional all)
1170 "Fetch the downloadable articles in the group.
1171 Optional arg ALL, if non-nil, means to fetch all articles."
1174 (if all gnus-newsgroup-articles
1175 gnus-newsgroup-downloadable))
1176 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1178 (gnus-agent-while-plugged
1180 (error "No articles to download"))
1181 (gnus-agent-with-fetch
1182 (setq gnus-newsgroup-undownloaded
1183 (gnus-sorted-ndifference
1184 gnus-newsgroup-undownloaded
1185 (setq fetched-articles
1186 (gnus-agent-fetch-articles
1187 gnus-newsgroup-name articles)))))
1189 (dolist (article articles)
1190 (let ((was-marked-downloadable
1191 (memq article gnus-newsgroup-downloadable)))
1192 (cond (gnus-agent-mark-unread-after-downloaded
1193 (setq gnus-newsgroup-downloadable
1194 (delq article gnus-newsgroup-downloadable))
1196 (gnus-summary-mark-article article gnus-unread-mark))
1197 (was-marked-downloadable
1198 (gnus-summary-set-agent-mark article t)))
1199 (when (gnus-summary-goto-subject article nil t)
1200 (gnus-summary-update-download-mark article))))))
1203 (defun gnus-agent-fetch-selected-article ()
1204 "Fetch the current article as it is selected.
1205 This can be added to `gnus-select-article-hook' or
1206 `gnus-mark-article-hook'."
1207 (let ((gnus-command-method gnus-current-select-method))
1208 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1209 (when (gnus-agent-fetch-articles
1211 (list gnus-current-article))
1212 (setq gnus-newsgroup-undownloaded
1213 (delq gnus-current-article gnus-newsgroup-undownloaded))
1214 (gnus-summary-update-download-mark gnus-current-article)))))
1217 ;;; Internal functions
1220 (defun gnus-agent-synchronize-group-flags (group action server)
1221 "Update a plugged group by performing the indicated action."
1222 (let* ((gnus-command-method (gnus-server-to-method server))
1223 (info (gnus-get-info group)))
1224 (gnus-request-set-mark group action)
1227 (gnus-request-update-info info gnus-command-method))
1230 (defun gnus-agent-save-active (method)
1231 (when (gnus-agent-method-p method)
1232 (let* ((gnus-command-method method)
1233 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1234 (file (gnus-agent-lib-file "active")))
1235 (gnus-active-to-gnus-format nil new)
1236 (gnus-agent-write-active file new)
1238 (nnheader-insert-file-contents file))))
1240 (defun gnus-agent-write-active (file new)
1241 (gnus-make-directory (file-name-directory file))
1242 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1243 ;; The hashtable contains real names of groups. However, do NOT
1244 ;; add the foreign server prefix as gnus-active-to-gnus-format
1245 ;; will add it while reading the file.
1246 (gnus-write-active-file file new nil)))
1249 (defun gnus-agent-possibly-alter-active (group active &optional info)
1250 "Possibly expand a group's active range to include articles
1251 downloaded into the agent."
1252 (let* ((gnus-command-method (or gnus-command-method
1253 (gnus-find-method-for-group group))))
1254 (when (gnus-agent-method-p gnus-command-method)
1255 (let* ((local (gnus-agent-get-local group))
1256 (active-min (or (car active) 0))
1257 (active-max (or (cdr active) 0))
1258 (agent-min (or (car local) active-min))
1259 (agent-max (or (cdr local) active-max)))
1261 (when (< agent-min active-min)
1262 (setcar active agent-min))
1264 (when (> agent-max active-max)
1265 (setcdr active agent-max))
1267 (when (and info (< agent-max (- active-min 100)))
1268 ;; I'm expanding the active range by such a large amount
1269 ;; that there is a gap of more than 100 articles between the
1270 ;; last article known to the agent and the first article
1271 ;; currently available on the server. This gap contains
1272 ;; articles that have been lost, mark them as read so that
1273 ;; gnus doesn't waste resources trying to fetch them.
1275 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1276 ;; want to modify the local file everytime someone restarts
1277 ;; gnus. The small gap will cause a tiny performance hit
1278 ;; when gnus tries, and fails, to retrieve the articles.
1279 ;; Still that should be smaller than opening a buffer,
1280 ;; printing this list to the buffer, and then writing it to a
1283 (let ((read (gnus-info-read info)))
1288 (list (cons (1+ agent-max)
1289 (1- active-min))))))
1291 ;; Lie about the agent's local range for this group to
1292 ;; disable the set read each time this server is opened.
1293 ;; NOTE: Opening this group will restore the valid local
1294 ;; range but it will also expand the local range to
1295 ;; incompass the new active range.
1296 (gnus-agent-set-local group agent-min (1- active-min)))))))
1298 (defun gnus-agent-save-group-info (method group active)
1299 "Update a single group's active range in the agent's copy of the server's active file."
1300 (when (gnus-agent-method-p method)
1301 (let* ((gnus-command-method (or method gnus-command-method))
1302 (coding-system-for-write nnheader-file-coding-system)
1303 (file-name-coding-system nnmail-pathname-coding-system)
1304 (file (gnus-agent-lib-file "active"))
1305 oactive-min oactive-max)
1306 (gnus-make-directory (file-name-directory file))
1307 (with-temp-file file
1308 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1309 (set-buffer-multibyte nil)
1310 (when (file-exists-p file)
1311 (nnheader-insert-file-contents file)
1313 (goto-char (point-min))
1314 (when (re-search-forward
1315 (concat "^" (regexp-quote group) " ") nil t)
1317 (setq oactive-max (read (current-buffer)) ;; max
1318 oactive-min (read (current-buffer)))) ;; min
1319 (gnus-delete-line)))
1321 (insert (format "%S %d %d y\n" (intern group)
1322 (max (or oactive-max (cdr active)) (cdr active))
1323 (min (or oactive-min (car active)) (car active))))
1324 (goto-char (point-max))
1325 (while (search-backward "\\." nil t)
1326 (delete-char 1)))))))
1328 (defun gnus-agent-get-group-info (method group)
1329 "Get a single group's active range in the agent's copy of the server's active file."
1330 (when (gnus-agent-method-p method)
1331 (let* ((gnus-command-method (or method gnus-command-method))
1332 (coding-system-for-write nnheader-file-coding-system)
1333 (file-name-coding-system nnmail-pathname-coding-system)
1334 (file (gnus-agent-lib-file "active"))
1335 oactive-min oactive-max)
1336 (gnus-make-directory (file-name-directory file))
1338 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1339 (mm-disable-multibyte)
1340 (when (file-exists-p file)
1341 (nnheader-insert-file-contents file)
1343 (goto-char (point-min))
1344 (when (re-search-forward
1345 (concat "^" (regexp-quote group) " ") nil t)
1347 (setq oactive-max (read (current-buffer)) ;; max
1348 oactive-min (read (current-buffer))) ;; min
1349 (cons oactive-min oactive-max))))))))
1351 (defun gnus-agent-group-path (group)
1352 "Translate GROUP into a file name."
1354 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1355 ;; The two methods must be kept synchronized, which is why
1356 ;; gnus-agent-group-pathname was added.
1359 (nnheader-translate-file-chars
1360 (nnheader-replace-duplicate-chars-in-string
1361 (nnheader-replace-chars-in-string
1362 (gnus-group-real-name group)
1365 (if (or nnmail-use-long-file-names
1366 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1368 (mm-encode-coding-string
1369 (nnheader-replace-chars-in-string group ?. ?/)
1370 nnmail-pathname-coding-system)))
1372 (defun gnus-agent-group-pathname (group)
1373 "Translate GROUP into a file name."
1374 ;; nnagent uses nnmail-group-pathname to read articles while
1375 ;; unplugged. The agent must, therefore, use the same directory
1377 (let ((gnus-command-method (or gnus-command-method
1378 (gnus-find-method-for-group group))))
1379 (nnmail-group-pathname (gnus-group-real-name group) (gnus-agent-directory))))
1381 (defun gnus-agent-get-function (method)
1382 (if (gnus-online method)
1387 (defun gnus-agent-covered-methods ()
1388 "Return the subset of methods that are covered by the agent."
1389 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1391 ;;; History functions
1393 (defun gnus-agent-history-buffer ()
1394 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1396 (defun gnus-agent-open-history ()
1398 (push (cons (gnus-agent-method)
1399 (set-buffer (gnus-get-buffer-create
1400 (format " *Gnus agent %s history*"
1401 (gnus-agent-method)))))
1402 gnus-agent-history-buffers)
1403 (set-buffer-multibyte nil) ;; everything is binary
1406 (let ((file (gnus-agent-lib-file "history")))
1407 (when (file-exists-p file)
1408 (nnheader-insert-file-contents file))
1409 (set (make-local-variable 'gnus-agent-file-name) file))))
1411 (defun gnus-agent-close-history ()
1412 (when (gnus-buffer-live-p gnus-agent-current-history)
1413 (kill-buffer gnus-agent-current-history)
1414 (setq gnus-agent-history-buffers
1415 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1416 gnus-agent-history-buffers))))
1422 (defun gnus-agent-fetch-articles (group articles)
1423 "Fetch ARTICLES from GROUP and put them into the Agent."
1425 (gnus-agent-load-alist group)
1426 (let* ((alist gnus-agent-article-alist)
1427 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1428 (selected-sets (list nil))
1429 (current-set-size 0)
1432 ;; Check each article
1433 (while (setq article (pop articles))
1434 ;; Skip alist entries preceeding this article
1435 (while (> article (or (caar alist) (1+ article)))
1436 (setq alist (cdr alist)))
1438 ;; Prune off articles that we have already fetched.
1439 (unless (and (eq article (caar alist))
1441 ;; Skip headers preceeding this article
1444 (let* ((header (car headers)))
1446 (mail-header-number header)
1448 (setq headers (cdr headers)))
1450 ;; Add this article to the current set
1451 (setcar selected-sets (cons article (car selected-sets)))
1453 ;; Update the set size, when the set is too large start a
1454 ;; new one. I do this after adding the article as I want at
1455 ;; least one article in each set.
1456 (when (< gnus-agent-max-fetch-size
1457 (setq current-set-size
1459 (if (= header-number article)
1460 (let ((char-size (mail-header-chars
1462 (if (<= char-size 0)
1463 ;; The char size was missing/invalid,
1464 ;; assume a worst-case situation of
1465 ;; 65 char/line. If the line count
1466 ;; is missing, arbitrarily assume a
1467 ;; size of 1000 characters.
1468 (max (* 65 (mail-header-lines
1473 (setcar selected-sets (nreverse (car selected-sets)))
1474 (setq selected-sets (cons nil selected-sets)
1475 current-set-size 0))))
1477 (when (or (cdr selected-sets) (car selected-sets))
1478 (let* ((fetched-articles (list nil))
1479 (tail-fetched-articles fetched-articles)
1480 (dir (gnus-agent-group-pathname group))
1481 (date (time-to-days (current-time)))
1482 (case-fold-search t)
1485 (setcar selected-sets (nreverse (car selected-sets)))
1486 (setq selected-sets (nreverse selected-sets))
1488 (gnus-make-directory dir)
1489 (gnus-message 7 "Fetching articles for %s..." group)
1492 (while (setq articles (pop selected-sets))
1493 ;; Fetch the articles from the backend.
1494 (if (gnus-check-backend-function 'retrieve-articles group)
1495 (setq pos (gnus-retrieve-articles articles group))
1498 (while (setq article (pop articles))
1499 (gnus-message 10 "Fetching article %s for %s..."
1502 (gnus-backlog-request-article group article
1504 (gnus-request-article article group))
1505 (goto-char (point-max))
1506 (push (cons article (point)) pos)
1507 (insert-buffer-substring nntp-server-buffer)))
1509 nntp-server-buffer (point-min) (point-max))
1510 (setq pos (nreverse pos)))))
1511 ;; Then save these articles into the Agent.
1513 (set-buffer nntp-server-buffer)
1515 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1516 (goto-char (point-min))
1517 (unless (eobp) ;; Don't save empty articles.
1518 (when (search-forward "\n\n" nil t)
1519 (when (search-backward "\nXrefs: " nil t)
1520 ;; Handle cross posting.
1521 (goto-char (match-end 0)) ; move to end of header name
1522 (skip-chars-forward "^ ") ; skip server name
1523 (skip-chars-forward " ")
1525 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1526 (push (cons (buffer-substring (match-beginning 1)
1529 (buffer-substring (match-beginning 2)
1532 (goto-char (match-end 0)))
1533 (gnus-agent-crosspost crosses (caar pos) date)))
1534 (goto-char (point-min))
1535 (if (not (re-search-forward
1536 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1537 (setq id "No-Message-ID-in-article")
1538 (setq id (buffer-substring
1539 (match-beginning 1) (match-end 1))))
1540 (write-region-as-coding-system
1541 gnus-agent-file-coding-system (point-min) (point-max)
1542 (concat dir (number-to-string (caar pos))) nil 'silent)
1544 (gnus-agent-append-to-list
1545 tail-fetched-articles (caar pos)))
1547 (setq pos (cdr pos)))))
1549 (gnus-agent-save-alist group (cdr fetched-articles) date)
1550 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1552 (gnus-message 7 ""))
1553 (cdr fetched-articles))))))
1555 (defun gnus-agent-unfetch-articles (group articles)
1556 "Delete ARTICLES that were fetched from GROUP into the agent."
1558 (gnus-agent-with-refreshed-group
1560 (gnus-agent-load-alist group)
1561 (let* ((alist (cons nil gnus-agent-article-alist))
1562 (articles (sort articles #'<))
1563 (next-possibility alist)
1564 (delete-this (pop articles)))
1565 (while (and (cdr next-possibility) delete-this)
1566 (let ((have-this (caar (cdr next-possibility))))
1567 (cond ((< delete-this have-this)
1568 (setq delete-this (pop articles)))
1569 ((= delete-this have-this)
1570 (let ((timestamp (cdar (cdr next-possibility))))
1572 (let* ((file-name (concat (gnus-agent-group-pathname group)
1573 (number-to-string have-this)))
1574 (size-file (float (or (and gnus-agent-total-fetched-hashtb
1575 (nth 7 (file-attributes file-name)))
1577 (delete-file file-name)
1578 (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1580 (setcdr next-possibility (cddr next-possibility)))
1582 (setq next-possibility (cdr next-possibility))))))
1583 (setq gnus-agent-article-alist (cdr alist))
1584 (gnus-agent-save-alist group)))))
1586 (defun gnus-agent-crosspost (crosses article &optional date)
1587 (setq date (or date t))
1589 (let (gnus-agent-article-alist group alist beg end)
1591 (set-buffer gnus-agent-overview-buffer)
1592 (when (nnheader-find-nov-line article)
1595 (setq end (progn (forward-line 1) (point)))))
1597 (setq group (caar crosses))
1598 (unless (setq alist (assoc group gnus-agent-group-alist))
1599 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1600 gnus-agent-group-alist))
1601 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1603 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1605 (when (= (point-max) (point-min))
1606 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1608 (nnheader-insert-file-contents
1609 (gnus-agent-article-name ".overview" group))))
1610 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1611 (insert (string-to-number (cdar crosses)))
1612 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1613 (gnus-agent-check-overview-buffer))
1614 (setq crosses (cdr crosses)))))
1616 (defun gnus-agent-backup-overview-buffer ()
1617 (when gnus-newsgroup-name
1618 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1621 (while (file-exists-p
1622 (setq name (concat root "~"
1623 (int-to-string (setq cnt (1+ cnt))) "~"))))
1624 (write-region (point-min) (point-max) name nil 'no-msg)
1625 (gnus-message 1 "Created backup copy of overview in %s." name)))
1628 (defun gnus-agent-check-overview-buffer (&optional buffer)
1629 "Check the overview file given for sanity.
1630 In particular, checks that the file is sorted by article number
1631 and that there are no duplicates."
1636 (set-buffer buffer))
1639 (goto-char (point-min))
1641 (while (< (point) (point-max))
1643 (cur (condition-case nil
1644 (read (current-buffer))
1647 ((or (not (integerp cur))
1648 (not (eq (char-after) ?\t)))
1650 (setq backed-up (gnus-agent-backup-overview-buffer)))
1652 "Overview buffer contains garbage '%s'."
1657 (setq backed-up (gnus-agent-backup-overview-buffer)))
1659 "Duplicate overview line for %d" cur)
1660 (delete-region (point) (progn (forward-line 1) (point))))
1663 (setq backed-up (gnus-agent-backup-overview-buffer)))
1664 (gnus-message 1 "Overview buffer not sorted!")
1665 (sort-numeric-fields 1 (point-min) (point-max))
1666 (goto-char (point-min))
1669 (setq prev-num cur)))
1670 (forward-line 1)))))))
1672 (defun gnus-agent-flush-cache ()
1674 (while gnus-agent-buffer-alist
1675 (set-buffer (cdar gnus-agent-buffer-alist))
1676 (write-region-as-coding-system
1677 gnus-agent-file-coding-system
1678 (point-min) (point-max)
1679 (gnus-agent-article-name ".overview"
1680 (caar gnus-agent-buffer-alist))
1682 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1683 (while gnus-agent-group-alist
1684 (with-temp-file (gnus-agent-article-name
1685 ".agentview" (caar gnus-agent-group-alist))
1686 (princ (cdar gnus-agent-group-alist))
1688 (princ 1 (current-buffer))
1690 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1693 (defun gnus-agent-find-parameter (group symbol)
1694 "Search for GROUPs SYMBOL in the group's parameters, the group's
1695 topic parameters, the group's category, or the customizable
1696 variables. Returns the first non-nil value found."
1697 (or (gnus-group-find-parameter group symbol t)
1698 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1702 '((agent-short-article . gnus-agent-short-article)
1703 (agent-long-article . gnus-agent-long-article)
1704 (agent-low-score . gnus-agent-low-score)
1705 (agent-high-score . gnus-agent-high-score)
1706 (agent-days-until-old . gnus-agent-expire-days)
1707 (agent-enable-expiration
1708 . gnus-agent-enable-expiration)
1709 (agent-predicate . gnus-agent-predicate)))))))
1711 (defun gnus-agent-fetch-headers (group &optional force)
1712 "Fetch interesting headers into the agent. The group's overview
1713 file will be updated to include the headers while a list of available
1714 article numbers will be returned."
1715 (let* ((fetch-all (and gnus-agent-consider-all-articles
1716 ;; Do not fetch all headers if the predicate
1717 ;; implies that we only consider unread articles.
1718 (not (gnus-predicate-implies-unread
1719 (gnus-agent-find-parameter group
1720 'agent-predicate)))))
1721 (articles (if fetch-all
1722 (gnus-uncompress-range (gnus-active group))
1723 (gnus-list-of-unread-articles group)))
1724 (gnus-decode-encoded-word-function 'identity)
1725 (file (gnus-agent-article-name ".overview" group)))
1726 ;; Check whether the number of articles is not too large.
1727 (when (and (integerp gnus-agent-large-newsgroup)
1728 (> gnus-agent-large-newsgroup 0))
1729 (setq articles (nthcdr (max (- (length articles)
1730 gnus-agent-large-newsgroup)
1734 ;; Add articles with marks to the list of article headers we want to
1735 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1736 ;; mark, but do fetch recent or seen articles if they have other, more
1737 ;; interesting marks. (We have to fetch articles with boring marks
1738 ;; because otherwise the agent will remove their marks.)
1739 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1740 (unless (memq (car arts) '(seen recent killed cache))
1741 (setq articles (gnus-range-add articles (cdr arts)))))
1742 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1744 ;; At this point, I have the list of articles to consider for
1745 ;; fetching. This is the list that I'll return to my caller. Some
1746 ;; of these articles may have already been fetched. That's OK as
1747 ;; the fetch article code will filter those out. Internally, I'll
1748 ;; filter this list to just those articles whose headers need to
1750 (let ((articles articles))
1751 ;; Remove known articles.
1752 (when (and (or gnus-agent-cache
1754 (gnus-agent-load-alist group))
1755 ;; Remove articles marked as downloaded.
1757 ;; I want to fetch all headers in the active range.
1758 ;; Therefore, exclude only those headers that are in the
1760 ;; NOTE: This is probably NOT what I want to do after
1761 ;; agent expiration in this group.
1762 (setq articles (gnus-agent-uncached-articles articles group))
1764 ;; I want to only fetch those headers that have never been
1765 ;; fetched. Therefore, exclude all headers that are, or
1766 ;; WERE, in the article alist.
1767 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1768 (high (cdr (gnus-active group))))
1769 ;; Low can be greater than High when the same group is
1770 ;; fetched twice in the same session {The first fetch will
1771 ;; fill the article alist such that (last
1772 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1773 ;; group))}. The addition of one(the 1+ above) then
1774 ;; forces Low to be greater than High. When this happens,
1775 ;; gnus-list-range-intersection returns nil which
1776 ;; indicates that no headers need to be fetched. -- Kevin
1777 (setq articles (gnus-list-range-intersection
1778 articles (list (cons low high)))))))
1781 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1782 (gnus-compress-sequence articles t))
1785 (set-buffer nntp-server-buffer)
1789 (gnus-message 7 "Fetching headers for %s..." group)
1792 (gnus-make-directory (nnheader-translate-file-chars
1793 (file-name-directory file) t))
1795 (unless (eq 'nov (gnus-retrieve-headers articles group))
1796 (nnvirtual-convert-headers))
1797 (gnus-agent-check-overview-buffer)
1798 ;; Move these headers to the overview buffer so that
1799 ;; gnus-agent-braid-nov can merge them with the contents
1802 gnus-agent-overview-buffer (point-min) (point-max))
1803 ;; NOTE: Call g-a-brand-nov even when the file does not
1804 ;; exist. As a minimum, it will validate the article
1805 ;; numbers already in the buffer.
1806 (gnus-agent-braid-nov group articles file)
1807 (gnus-agent-check-overview-buffer)
1808 (write-region-as-coding-system
1809 gnus-agent-file-coding-system
1810 (point-min) (point-max) file nil 'silent)
1811 (gnus-agent-update-view-total-fetched-for group t)
1812 (gnus-agent-save-alist group articles nil)
1816 (nnheader-insert-file-contents file)))))
1819 (defsubst gnus-agent-read-article-number ()
1820 "Reads the article number at point. Returns nil when a valid article number can not be read."
1822 ;; It is unfortunite but the read function quietly overflows
1823 ;; integer. As a result, I have to use string operations to test
1824 ;; for overflow BEFORE calling read.
1825 (when (looking-at "[0-9]+\t")
1826 (let ((len (- (match-end 0) (match-beginning 0))))
1828 (read (current-buffer)))
1830 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1831 ;; Back convert from int to string to ensure that this is one of them.
1832 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1833 (num (read (current-buffer)))
1834 (str2 (int-to-string num)))
1835 (when (equal str1 str2)
1838 (defsubst gnus-agent-copy-nov-line (article)
1839 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1841 (set-buffer gnus-agent-overview-buffer)
1842 (while (and (not (eobp))
1843 (or (not (setq art (gnus-agent-read-article-number)))
1848 (not (eq article art)))
1849 (set-buffer nntp-server-buffer)
1851 (setq e (progn (forward-line 1) (point)))
1852 (set-buffer nntp-server-buffer)
1853 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1855 (defun gnus-agent-braid-nov (group articles file)
1856 "Merge agent overview data with given file.
1857 Takes unvalidated headers for ARTICLES from
1858 `gnus-agent-overview-buffer' and validated headers from the given
1859 FILE and places the combined valid headers into
1860 `nntp-server-buffer'. This function can be used, when file
1861 doesn't exist, to valid the overview buffer."
1863 (set-buffer gnus-agent-overview-buffer)
1864 (goto-char (point-min))
1865 (set-buffer nntp-server-buffer)
1867 (when (file-exists-p file)
1868 (nnheader-insert-file-contents file))
1869 (goto-char (point-max))
1872 (unless (or (= (point-min) (point-max))
1873 (< (setq last (read (current-buffer))) (car articles)))
1874 ;; Old and new overlap -- We do it the hard way.
1875 (when (nnheader-find-nov-line (car articles))
1876 ;; Replacing existing NOV entry
1877 (delete-region (point) (progn (forward-line 1) (point))))
1878 (gnus-agent-copy-nov-line (pop articles))
1882 (while (let ((art (read (current-buffer))))
1883 (cond ((< art (car articles))
1886 ((= art (car articles))
1889 (point) (progn (forward-line 1) (point)))
1895 (gnus-agent-copy-nov-line (pop articles)))))
1897 (goto-char (point-max))
1899 ;; Append the remaining lines
1902 (set-buffer gnus-agent-overview-buffer)
1903 (setq start (point))
1904 (set-buffer nntp-server-buffer))
1907 (insert-buffer-substring gnus-agent-overview-buffer start)
1910 (setq last (or last -134217728))
1913 (setq art (gnus-agent-read-article-number))
1915 ;; Bad art num - delete this line
1917 (delete-region (point) (progn (forward-line 1) (point))))
1919 ;; Art num out of order - enable sort
1927 (sort-numeric-fields 1 (point-min) (point-max)))))))
1929 ;; Keeps the compiler from warning about the free variable in
1930 ;; gnus-agent-read-agentview.
1932 (defvar gnus-agent-read-agentview))
1934 (defun gnus-agent-load-alist (group)
1935 "Load the article-state alist for GROUP."
1936 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1937 (let ((gnus-agent-read-agentview group))
1938 (setq gnus-agent-article-alist
1939 (gnus-cache-file-contents
1940 (gnus-agent-article-name ".agentview" group)
1941 'gnus-agent-file-loading-cache
1942 'gnus-agent-read-agentview))))
1944 ;; Save format may be either 1 or 2. Two is the new, compressed
1945 ;; format that is still being tested. Format 1 is uncompressed but
1946 ;; known to be reliable.
1947 (defconst gnus-agent-article-alist-save-format 2)
1949 (defun gnus-agent-read-agentview (file)
1950 "Load FILE and do a `read' there."
1954 (nnheader-insert-file-contents file)
1955 (goto-char (point-min))
1956 (let ((alist (read (current-buffer)))
1957 (version (condition-case nil (read (current-buffer))
1963 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version))
1965 (let ((inhibit-quit t)
1967 (gnus-agent-open-history)
1968 (set-buffer (gnus-agent-history-buffer))
1969 (goto-char (point-min))
1971 (if (and (looking-at
1972 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1973 (string= (match-string 2)
1974 gnus-agent-read-agentview)
1975 (setq entry (assoc (string-to-number (match-string 3)) alist)))
1976 (setcdr entry (string-to-number (match-string 1))))
1978 (gnus-agent-close-history)
1979 (setq changed-version t)))
1981 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
1986 (let ((state (car comp-list))
1988 (gnus-uncompress-range
1990 (mapcar (lambda (article-id)
1991 (setq uncomp (cons (cons article-id state) uncomp)))
1994 (setq alist (sort uncomp 'car-less-than-car)))))
1995 (when changed-version
1996 (let ((gnus-agent-article-alist alist))
1997 (gnus-agent-save-alist gnus-agent-read-agentview)))
2001 (defun gnus-agent-save-alist (group &optional articles state)
2002 "Save the article-state alist for GROUP."
2003 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2004 (prev (cons nil gnus-agent-article-alist))
2006 print-level print-length item article)
2007 (while (setq article (pop articles))
2008 (while (and (cdr prev)
2009 (< (caadr prev) article))
2010 (setq prev (cdr prev)))
2013 (setcdr prev (list (cons article state))))
2014 ((> (caadr prev) article)
2015 (setcdr prev (cons (cons article state) (cdr prev))))
2016 ((= (caadr prev) article)
2017 (setcdr (cadr prev) state)))
2018 (setq prev (cdr prev)))
2019 (setq gnus-agent-article-alist (cdr all))
2021 (gnus-agent-set-local group
2022 (caar gnus-agent-article-alist)
2023 (caar (last gnus-agent-article-alist)))
2025 (gnus-make-directory (gnus-agent-article-name "" group))
2026 (with-temp-file (gnus-agent-article-name ".agentview" group)
2027 (cond ((eq gnus-agent-article-alist-save-format 1)
2028 (princ gnus-agent-article-alist (current-buffer)))
2029 ((eq gnus-agent-article-alist-save-format 2)
2030 (let ((compressed nil))
2031 (mapcar (lambda (pair)
2032 (let* ((article-id (car pair))
2033 (day-of-download (cdr pair))
2034 (comp-list (assq day-of-download compressed)))
2037 (cons article-id (cdr comp-list)))
2039 (cons (list day-of-download article-id)
2041 nil)) gnus-agent-article-alist)
2042 (mapcar (lambda (comp-list)
2044 (gnus-compress-sequence
2045 (nreverse (cdr comp-list)))))
2047 (princ compressed (current-buffer)))))
2049 (princ gnus-agent-article-alist-save-format (current-buffer))
2052 (gnus-agent-update-view-total-fetched-for group nil)))
2054 (defvar gnus-agent-article-local nil)
2055 (defvar gnus-agent-file-loading-local nil)
2057 (defun gnus-agent-load-local (&optional method)
2058 "Load the METHOD'S local file. The local file contains min/max
2059 article counts for each of the method's subscribed groups."
2060 (let ((gnus-command-method (or method gnus-command-method)))
2061 (setq gnus-agent-article-local
2062 (gnus-cache-file-contents
2063 (gnus-agent-lib-file "local")
2064 'gnus-agent-file-loading-local
2065 'gnus-agent-read-and-cache-local))))
2067 (defun gnus-agent-read-and-cache-local (file)
2068 "Load and read FILE then bind its contents to
2069 gnus-agent-article-local. If that variable had `dirty' (also known as
2070 modified) original contents, they are first saved to their own file."
2072 (if (and gnus-agent-article-local
2073 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2074 (gnus-agent-save-local))
2075 (gnus-agent-read-local file))
2077 (defun gnus-agent-read-local (file)
2078 "Load FILE and do a `read' there."
2079 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2084 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2085 (nnheader-insert-file-contents file))
2088 (goto-char (point-min))
2089 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2090 (while (= (following-char) ?\;)
2092 (setq line (1+ line)))
2099 (cur (current-buffer)))
2100 (setq group (read cur)
2104 (when (stringp group)
2105 (setq group (intern group my-obarray)))
2107 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2108 (set group (cons (+ 0 min) (+ 0 max))))
2110 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2111 file line (error-message-string err))))
2113 (setq line (1+ line))))
2115 (set (intern "+dirty" my-obarray) nil)
2116 (set (intern "+method" my-obarray) gnus-command-method)
2119 (defun gnus-agent-save-local (&optional force)
2120 "Save gnus-agent-article-local under it method's agent.lib directory."
2121 (let ((my-obarray gnus-agent-article-local))
2122 (when (and my-obarray
2123 (or force (symbol-value (intern "+dirty" my-obarray))))
2124 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2125 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2126 (dest (gnus-agent-lib-file "local")))
2127 (gnus-make-directory (gnus-agent-lib-file ""))
2129 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2130 (with-temp-file dest
2131 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2132 (file-name-coding-system nnmail-pathname-coding-system)
2133 print-level print-length item article
2134 (standard-output (current-buffer)))
2135 (mapatoms (lambda (symbol)
2136 (cond ((not (boundp symbol))
2138 ((member (symbol-name symbol) '("+dirty" "+method"))
2142 (let ((range (symbol-value symbol)))
2150 (defun gnus-agent-get-local (group &optional gmane method)
2151 (let* ((gmane (or gmane (gnus-group-real-name group)))
2152 (gnus-command-method (or method (gnus-find-method-for-group group)))
2153 (local (gnus-agent-load-local))
2154 (symb (intern gmane local))
2155 (minmax (and (boundp symb) (symbol-value symb))))
2157 ;; Bind these so that gnus-agent-load-alist doesn't change the
2158 ;; current alist (i.e. gnus-agent-article-alist)
2159 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2160 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2161 (alist (gnus-agent-load-alist group)))
2165 (caar (last alist))))
2166 (gnus-agent-set-local group (car minmax) (cdr minmax)
2167 gmane gnus-command-method local))))
2170 (defun gnus-agent-set-local (group min max &optional gmane method local)
2171 (let* ((gmane (or gmane (gnus-group-real-name group)))
2172 (gnus-command-method (or method (gnus-find-method-for-group group)))
2173 (local (or local (gnus-agent-load-local)))
2174 (symb (intern gmane local))
2175 (minmax (and (boundp symb) (symbol-value symb))))
2177 (if (cond ((and minmax
2178 (or (not (eq min (car minmax)))
2179 (not (eq max (cdr minmax)))))
2186 (set symb (cons min max))
2189 (unintern symb local)))
2190 (set (intern "+dirty" local) t))))
2192 (defun gnus-agent-article-name (article group)
2193 (expand-file-name article
2194 (file-name-as-directory
2195 (gnus-agent-group-pathname group))))
2197 (defun gnus-agent-batch-confirmation (msg)
2198 "Show error message and return t."
2199 (gnus-message 1 msg)
2203 (defun gnus-agent-batch-fetch ()
2204 "Start Gnus and fetch session."
2207 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2208 (gnus-agent-fetch-session))
2211 (defun gnus-agent-fetch-session ()
2212 "Fetch all articles and headers that are eligible for fetching."
2214 (unless gnus-agent-covered-methods
2215 (error "No servers are covered by the Gnus agent"))
2216 (unless gnus-plugged
2217 (error "Can't fetch articles while Gnus is unplugged"))
2218 (let ((methods (gnus-agent-covered-methods))
2219 groups group gnus-command-method)
2222 (setq gnus-command-method (car methods))
2223 (when (and (or (gnus-server-opened gnus-command-method)
2224 (gnus-open-server gnus-command-method))
2225 (gnus-online gnus-command-method))
2226 (setq groups (gnus-groups-from-server (car methods)))
2227 (gnus-agent-with-fetch
2228 (while (setq group (pop groups))
2229 (when (<= (gnus-group-level group)
2230 gnus-agent-handle-level)
2231 (if (or debug-on-error debug-on-quit)
2232 (gnus-agent-fetch-group-1
2233 group gnus-command-method)
2235 (gnus-agent-fetch-group-1
2236 group gnus-command-method)
2238 (unless (funcall gnus-agent-confirmation-function
2239 (format "Error %s while fetching session. Should gnus continue? "
2240 (error-message-string err)))
2241 (error "Cannot fetch articles into the Gnus agent")))
2243 (gnus-agent-regenerate-group group)
2244 (unless (funcall gnus-agent-confirmation-function
2246 "%s while fetching session. Should gnus continue? "
2247 (error-message-string err)))
2249 "Cannot fetch articles into the Gnus agent")))))))))
2250 (setq methods (cdr methods)))
2251 (gnus-run-hooks 'gnus-agent-fetched-hook)
2252 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2254 (defun gnus-agent-fetch-group-1 (group method)
2256 (let ((gnus-command-method method)
2257 (gnus-newsgroup-name group)
2258 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2259 (gnus-newsgroup-headers gnus-newsgroup-headers)
2260 (gnus-newsgroup-scored gnus-newsgroup-scored)
2261 (gnus-use-cache gnus-use-cache)
2262 (gnus-summary-expunge-below gnus-summary-expunge-below)
2263 (gnus-summary-mark-below gnus-summary-mark-below)
2264 (gnus-orphan-score gnus-orphan-score)
2265 ;; Maybe some other gnus-summary local variables should also
2271 category predicate info marks score-param
2273 (unless (gnus-check-group group)
2274 (error "Can't open server for %s" group))
2277 (when (or gnus-newsgroup-active
2279 (gnus-activate-group group))
2280 (let ((marked-articles gnus-newsgroup-downloadable))
2281 ;; Identify the articles marked for download
2282 (unless gnus-newsgroup-active
2283 ;; The variable gnus-newsgroup-active was selected as I need
2284 ;; a gnus-summary local variable that is NOT bound to any
2285 ;; value (its global value should default to nil).
2286 (dolist (mark gnus-agent-download-marks)
2287 (let ((arts (cdr (assq mark (gnus-info-marks
2288 (setq info (gnus-get-info group)))))))
2290 (setq marked-articles (nconc (gnus-uncompress-range arts)
2293 (setq marked-articles (sort marked-articles '<))
2295 ;; Fetch any new articles from the server
2296 (setq articles (gnus-agent-fetch-headers group))
2298 ;; Merge new articles with marked
2299 (setq articles (sort (append marked-articles articles) '<))
2302 ;; Parse them and see which articles we want to fetch.
2303 (setq gnus-newsgroup-dependencies
2304 (or gnus-newsgroup-dependencies
2305 (make-vector (length articles) 0)))
2306 (setq gnus-newsgroup-headers
2307 (or gnus-newsgroup-headers
2308 (gnus-get-newsgroup-headers-xover articles nil nil
2310 ;; `gnus-agent-overview-buffer' may be killed for
2311 ;; timeout reason. If so, recreate it.
2312 (gnus-agent-create-buffer)
2314 ;; Figure out how to select articles in this group
2315 (setq category (gnus-group-category group))
2319 (gnus-agent-find-parameter group 'agent-predicate)))
2321 ;; If the selection predicate requires scoring, score each header
2322 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2324 (gnus-agent-find-parameter group 'agent-score-file)))
2325 ;; Translate score-param into real one
2328 ((eq score-param 'file)
2329 (setq score-param (gnus-all-score-files group)))
2330 ((stringp (car score-param)))
2332 (setq score-param (list (list score-param)))))
2334 (gnus-score-headers score-param))))
2336 (unless (and (eq predicate 'gnus-agent-false)
2337 (not marked-articles))
2338 (let ((arts (list nil)))
2339 (let ((arts-tail arts)
2340 (alist (gnus-agent-load-alist group))
2341 (marked-articles marked-articles)
2342 (gnus-newsgroup-headers gnus-newsgroup-headers))
2343 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2344 (let ((num (mail-header-number gnus-headers)))
2345 ;; Determine if this article is already in the cache
2347 (> num (caar alist)))
2348 (setq alist (cdr alist)))
2350 (unless (and (eq num (caar alist))
2353 ;; Determine if this article was marked for download.
2354 (while (and marked-articles
2355 (> num (car marked-articles)))
2356 (setq marked-articles
2357 (cdr marked-articles)))
2359 ;; When this article is marked, or selected by the
2360 ;; predicate, add it to the download list
2361 (when (or (eq num (car marked-articles))
2364 (assq num gnus-newsgroup-scored))
2365 gnus-summary-default-score))
2366 (gnus-agent-long-article
2367 (gnus-agent-find-parameter
2368 group 'agent-long-article))
2369 (gnus-agent-short-article
2370 (gnus-agent-find-parameter
2371 group 'agent-short-article))
2372 (gnus-agent-low-score
2373 (gnus-agent-find-parameter
2374 group 'agent-low-score))
2375 (gnus-agent-high-score
2376 (gnus-agent-find-parameter
2377 group 'agent-high-score))
2378 (gnus-agent-expire-days
2379 (gnus-agent-find-parameter
2380 group 'agent-days-until-old)))
2381 (funcall predicate)))
2382 (gnus-agent-append-to-list arts-tail num))))))
2384 (let (fetched-articles)
2385 ;; Fetch all selected articles
2386 (setq gnus-newsgroup-undownloaded
2387 (gnus-sorted-ndifference
2388 gnus-newsgroup-undownloaded
2389 (setq fetched-articles
2391 (gnus-agent-fetch-articles group (cdr arts))
2394 (let ((unfetched-articles
2395 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2396 (if gnus-newsgroup-active
2397 ;; Update the summary buffer
2399 (dolist (article marked-articles)
2400 (gnus-summary-set-agent-mark article t))
2401 (dolist (article fetched-articles)
2402 (if gnus-agent-mark-unread-after-downloaded
2403 (gnus-summary-mark-article
2404 article gnus-unread-mark))
2405 (when (gnus-summary-goto-subject article nil t)
2406 (gnus-summary-update-download-mark article)))
2407 (dolist (article unfetched-articles)
2408 (gnus-summary-mark-article
2409 article gnus-canceled-mark)))
2411 ;; Update the group buffer.
2413 ;; When some, or all, of the marked articles came
2414 ;; from the download mark. Remove that mark. I
2415 ;; didn't do this earlier as I only want to remove
2416 ;; the marks after the fetch is completed.
2418 (dolist (mark gnus-agent-download-marks)
2419 (when (eq mark 'download)
2421 (assq mark (gnus-info-marks
2422 (setq info (gnus-get-info group))))))
2423 (when (cdr marked-arts)
2425 (delq marked-arts (gnus-info-marks info)))
2426 (gnus-info-set-marks info marks)))))
2427 (let ((read (gnus-info-read
2428 (or info (setq info (gnus-get-info group))))))
2430 info (gnus-add-to-range read unfetched-articles)))
2432 (gnus-group-update-group group t)
2436 (concat "(gnus-group-set-info '"
2437 (gnus-prin1-to-string info)
2441 ;;; Agent Category Mode
2444 (defvar gnus-category-mode-hook nil
2445 "Hook run in `gnus-category-mode' buffers.")
2447 (defvar gnus-category-line-format " %(%20c%): %g\n"
2448 "Format of category lines.
2450 Valid specifiers include:
2451 %c Topic name (string)
2452 %g The number of groups in the topic (integer)
2454 General format specifiers can also be used. See Info node
2455 `(gnus)Formatting Variables'.")
2457 (defvar gnus-category-mode-line-format "Gnus: %%b"
2458 "The format specification for the category mode line.")
2460 (defvar gnus-agent-predicate 'false
2461 "The selection predicate used when no other source is available.")
2463 (defvar gnus-agent-short-article 100
2464 "Articles that have fewer lines than this are short.")
2466 (defvar gnus-agent-long-article 200
2467 "Articles that have more lines than this are long.")
2469 (defvar gnus-agent-low-score 0
2470 "Articles that have a score lower than this have a low score.")
2472 (defvar gnus-agent-high-score 0
2473 "Articles that have a score higher than this have a high score.")
2476 ;;; Internal variables.
2478 (defvar gnus-category-buffer "*Agent Category*")
2480 (defvar gnus-category-line-format-alist
2481 `((?c gnus-tmp-name ?s)
2482 (?g gnus-tmp-groups ?d)))
2484 (defvar gnus-category-mode-line-format-alist
2485 `((?u user-defined ?s)))
2487 (defvar gnus-category-line-format-spec nil)
2488 (defvar gnus-category-mode-line-format-spec nil)
2490 (defvar gnus-category-mode-map nil)
2491 (put 'gnus-category-mode 'mode-class 'special)
2493 (unless gnus-category-mode-map
2494 (setq gnus-category-mode-map (make-sparse-keymap))
2495 (suppress-keymap gnus-category-mode-map)
2497 (gnus-define-keys gnus-category-mode-map
2498 "q" gnus-category-exit
2499 "k" gnus-category-kill
2500 "c" gnus-category-copy
2501 "a" gnus-category-add
2502 "e" gnus-agent-customize-category
2503 "p" gnus-category-edit-predicate
2504 "g" gnus-category-edit-groups
2505 "s" gnus-category-edit-score
2506 "l" gnus-category-list
2508 "\C-c\C-i" gnus-info-find-node
2509 "\C-c\C-b" gnus-bug))
2511 (defvar gnus-category-menu-hook nil
2512 "*Hook run after the creation of the menu.")
2514 (defun gnus-category-make-menu-bar ()
2515 (gnus-turn-off-edit-menu 'category)
2516 (unless (boundp 'gnus-category-menu)
2518 gnus-category-menu gnus-category-mode-map ""
2520 ["Add" gnus-category-add t]
2521 ["Kill" gnus-category-kill t]
2522 ["Copy" gnus-category-copy t]
2523 ["Edit category" gnus-agent-customize-category t]
2524 ["Edit predicate" gnus-category-edit-predicate t]
2525 ["Edit score" gnus-category-edit-score t]
2526 ["Edit groups" gnus-category-edit-groups t]
2527 ["Exit" gnus-category-exit t]))
2529 (gnus-run-hooks 'gnus-category-menu-hook)))
2531 (defun gnus-category-mode ()
2532 "Major mode for listing and editing agent categories.
2534 All normal editing commands are switched off.
2535 \\<gnus-category-mode-map>
2536 For more in-depth information on this mode, read the manual
2537 \(`\\[gnus-info-find-node]').
2539 The following commands are available:
2541 \\{gnus-category-mode-map}"
2543 (when (gnus-visual-p 'category-menu 'menu)
2544 (gnus-category-make-menu-bar))
2545 (kill-all-local-variables)
2546 (gnus-simplify-mode-line)
2547 (setq major-mode 'gnus-category-mode)
2548 (setq mode-name "Category")
2549 (gnus-set-default-directory)
2550 (setq mode-line-process nil)
2551 (use-local-map gnus-category-mode-map)
2552 (buffer-disable-undo)
2553 (setq truncate-lines t)
2554 (setq buffer-read-only t)
2555 (gnus-run-hooks 'gnus-category-mode-hook))
2557 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2559 (defun gnus-category-insert-line (category)
2560 (let* ((gnus-tmp-name (format "%s" (car category)))
2561 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2563 (gnus-add-text-properties
2567 (eval gnus-category-line-format-spec))
2568 (list 'gnus-category gnus-tmp-name))))
2570 (defun gnus-enter-category-buffer ()
2571 "Go to the Category buffer."
2573 (gnus-category-setup-buffer)
2574 (gnus-configure-windows 'category)
2575 (gnus-category-prepare))
2577 (defun gnus-category-setup-buffer ()
2578 (unless (get-buffer gnus-category-buffer)
2580 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2581 (gnus-category-mode))))
2583 (defun gnus-category-prepare ()
2584 (gnus-set-format 'category-mode)
2585 (gnus-set-format 'category t)
2586 (let ((alist gnus-category-alist)
2587 (buffer-read-only nil))
2590 (gnus-category-insert-line (pop alist)))
2591 (goto-char (point-min))
2592 (gnus-category-position-point)))
2594 (defun gnus-category-name ()
2595 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2596 (error "No category on the current line")))
2598 (defun gnus-category-read ()
2599 "Read the category alist."
2600 (setq gnus-category-alist
2604 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2605 (goto-char (point-min))
2606 ;; This code isn't temp, it will be needed so long as
2607 ;; anyone may be migrating from an older version.
2609 ;; Once we're certain that people will not revert to an
2610 ;; earlier version, we can take out the old-list code in
2611 ;; gnus-category-write.
2612 (let* ((old-list (read (current-buffer)))
2613 (new-list (ignore-errors (read (current-buffer)))))
2616 ;; Convert from a positional list to an alist.
2626 '(agent-predicate agent-score-file agent-groups))))
2629 (list (gnus-agent-cat-make 'default 'short)))))
2631 (defun gnus-category-write ()
2632 "Write the category alist."
2633 (setq gnus-category-predicate-cache nil
2634 gnus-category-group-cache nil)
2635 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2636 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2637 ;; This prin1 is temporary. It exists so that people can revert
2638 ;; to an earlier version of gnus-agent.
2639 (prin1 (mapcar (lambda (c)
2641 (cdr (assoc 'agent-predicate c))
2642 (cdr (assoc 'agent-score-file c))
2643 (cdr (assoc 'agent-groups c))))
2644 gnus-category-alist)
2647 (prin1 gnus-category-alist (current-buffer))))
2649 (defun gnus-category-edit-predicate (category)
2650 "Edit the predicate for CATEGORY."
2651 (interactive (list (gnus-category-name)))
2652 (let ((info (assq category gnus-category-alist)))
2654 (gnus-agent-cat-predicate info)
2655 (format "Editing the select predicate for category %s" category)
2656 `(lambda (predicate)
2657 ;; Avoid run-time execution of setf form
2658 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2660 ;; use its expansion instead:
2661 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2662 'agent-predicate predicate)
2664 (gnus-category-write)
2665 (gnus-category-list)))))
2667 (defun gnus-category-edit-score (category)
2668 "Edit the score expression for CATEGORY."
2669 (interactive (list (gnus-category-name)))
2670 (let ((info (assq category gnus-category-alist)))
2672 (gnus-agent-cat-score-file info)
2673 (format "Editing the score expression for category %s" category)
2674 `(lambda (score-file)
2675 ;; Avoid run-time execution of setf form
2676 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2678 ;; use its expansion instead:
2679 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2680 'agent-score-file score-file)
2682 (gnus-category-write)
2683 (gnus-category-list)))))
2685 (defun gnus-category-edit-groups (category)
2686 "Edit the group list for CATEGORY."
2687 (interactive (list (gnus-category-name)))
2688 (let ((info (assq category gnus-category-alist)))
2690 (gnus-agent-cat-groups info)
2691 (format "Editing the group list for category %s" category)
2693 ;; Avoid run-time execution of setf form
2694 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2696 ;; use its expansion instead:
2697 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2700 (gnus-category-write)
2701 (gnus-category-list)))))
2703 (defun gnus-category-kill (category)
2704 "Kill the current category."
2705 (interactive (list (gnus-category-name)))
2706 (let ((info (assq category gnus-category-alist))
2707 (buffer-read-only nil))
2709 (setq gnus-category-alist (delq info gnus-category-alist))
2710 (gnus-category-write)))
2712 (defun gnus-category-copy (category to)
2713 "Copy the current category."
2714 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2715 (let ((info (assq category gnus-category-alist)))
2716 (push (let ((newcat (gnus-copy-sequence info)))
2717 (setf (gnus-agent-cat-name newcat) to)
2718 (setf (gnus-agent-cat-groups newcat) nil)
2720 gnus-category-alist)
2721 (gnus-category-write)
2722 (gnus-category-list)))
2724 (defun gnus-category-add (category)
2725 "Create a new category."
2726 (interactive "SCategory name: ")
2727 (when (assq category gnus-category-alist)
2728 (error "Category %s already exists" category))
2729 (push (gnus-agent-cat-make category)
2730 gnus-category-alist)
2731 (gnus-category-write)
2732 (gnus-category-list))
2734 (defun gnus-category-list ()
2735 "List all categories."
2737 (gnus-category-prepare))
2739 (defun gnus-category-exit ()
2740 "Return to the group buffer."
2742 (kill-buffer (current-buffer))
2743 (gnus-configure-windows 'group t))
2745 ;; To avoid having 8-bit characters in the source file.
2746 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2748 (defvar gnus-category-predicate-alist
2749 '((spam . gnus-agent-spam-p)
2750 (short . gnus-agent-short-p)
2751 (long . gnus-agent-long-p)
2752 (low . gnus-agent-low-scored-p)
2753 (high . gnus-agent-high-scored-p)
2754 (read . gnus-agent-read-p)
2755 (true . gnus-agent-true)
2756 (false . gnus-agent-false))
2757 "Mapping from short score predicate symbols to predicate functions.")
2759 (defun gnus-agent-spam-p ()
2760 "Say whether an article is spam or not."
2761 (unless gnus-agent-spam-hashtb
2762 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2763 (if (not (equal (mail-header-references gnus-headers) ""))
2765 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2767 (gnus-gethash string gnus-agent-spam-hashtb)
2768 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2770 (defun gnus-agent-short-p ()
2771 "Say whether an article is short or not."
2772 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2774 (defun gnus-agent-long-p ()
2775 "Say whether an article is long or not."
2776 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2778 (defun gnus-agent-low-scored-p ()
2779 "Say whether an article has a low score or not."
2780 (< gnus-score gnus-agent-low-score))
2782 (defun gnus-agent-high-scored-p ()
2783 "Say whether an article has a high score or not."
2784 (> gnus-score gnus-agent-high-score))
2786 (defun gnus-agent-read-p ()
2787 "Say whether an article is read or not."
2788 (gnus-member-of-range (mail-header-number gnus-headers)
2789 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2791 (defun gnus-category-make-function (predicate)
2792 "Make a function from PREDICATE."
2793 (let ((func (gnus-category-make-function-1 predicate)))
2794 (if (and (= (length func) 1)
2795 (symbolp (car func)))
2797 (gnus-byte-compile `(lambda () ,func)))))
2799 (defun gnus-agent-true ()
2803 (defun gnus-agent-false ()
2807 (defun gnus-category-make-function-1 (predicate)
2808 "Make a function from PREDICATE."
2810 ;; Functions are just returned as is.
2811 ((or (symbolp predicate)
2812 (functionp predicate))
2813 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2815 ;; More complex predicate.
2818 ((memq (car predicate) '(& and))
2820 ((memq (car predicate) '(| or))
2822 ((memq (car predicate) gnus-category-not)
2824 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2826 (error "Unknown predicate type: %s" predicate))))
2828 (defun gnus-get-predicate (predicate)
2829 "Return the function implementing PREDICATE."
2830 (or (cdr (assoc predicate gnus-category-predicate-cache))
2831 (let ((func (gnus-category-make-function predicate)))
2832 (setq gnus-category-predicate-cache
2833 (nconc gnus-category-predicate-cache
2834 (list (cons predicate func))))
2837 (defun gnus-predicate-implies-unread (predicate)
2838 "Say whether PREDICATE implies unread articles only.
2839 It is okay to miss some cases, but there must be no false positives.
2840 That is, if this predicate returns true, then indeed the predicate must
2841 return only unread articles."
2842 (eq t (gnus-function-implies-unread-1
2843 (gnus-category-make-function-1 predicate))))
2845 (defun gnus-function-implies-unread-1 (function)
2846 "Recursively evaluate a predicate function to determine whether it can select
2847 any read articles. Returns t if the function is known to never
2848 return read articles, nil when it is known to always return read
2849 articles, and t_nil when the function may return both read and unread
2851 (let ((func (car function))
2852 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2853 (cond ((eq func 'and)
2854 (cond ((memq t args) ; if any argument returns only unread articles
2855 ;; then that argument constrains the result to only unread articles.
2857 ((memq 't_nil args) ; if any argument is indeterminate
2858 ;; then the result is indeterminate
2861 (cond ((memq nil args) ; if any argument returns read articles
2862 ;; then that argument ensures that the results includes read articles.
2864 ((memq 't_nil args) ; if any argument is indeterminate
2865 ;; then that argument ensures that the results are indeterminate
2867 (t ; if all arguments return only unread articles
2868 ;; then the result returns only unread articles
2871 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2872 ; then the result is indeterminate
2875 ; toggle the result to be the opposite of the argument
2877 ((eq func 'gnus-agent-read-p)
2878 nil) ; The read predicate NEVER returns unread articles
2879 ((eq func 'gnus-agent-false)
2880 t) ; The false predicate returns t as the empty set excludes all read articles
2881 ((eq func 'gnus-agent-true)
2882 nil) ; The true predicate ALWAYS returns read articles
2883 ((catch 'found-match
2884 (let ((alist gnus-category-predicate-alist))
2886 (if (eq func (cdar alist))
2887 (throw 'found-match t)
2888 (setq alist (cdr alist))))))
2889 't_nil) ; All other predicates return read and unread articles
2891 (error "Unknown predicate function: %s" function)))))
2893 (defun gnus-group-category (group)
2894 "Return the category GROUP belongs to."
2895 (unless gnus-category-group-cache
2896 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2897 (let ((cs gnus-category-alist)
2899 (while (setq cat (pop cs))
2900 (setq groups (gnus-agent-cat-groups cat))
2902 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2903 (or (gnus-gethash group gnus-category-group-cache)
2904 (assq 'default gnus-category-alist)))
2906 (defun gnus-agent-expire-group (group &optional articles force)
2907 "Expire all old articles in GROUP.
2908 If you want to force expiring of certain articles, this function can
2909 take ARTICLES, and FORCE parameters as well.
2911 The articles on which the expiration process runs are selected as follows:
2912 if ARTICLES is null, all read and unmarked articles.
2913 if ARTICLES is t, all articles.
2914 if ARTICLES is a list, just those articles.
2915 FORCE is equivalent to setting the expiration predicates to true."
2917 (list (let ((def (or (gnus-group-group-name)
2918 gnus-newsgroup-name)))
2919 (let ((select (read-string (if def
2920 (concat "Group Name ("
2923 (if (and (equal "" select)
2929 (gnus-agent-expire articles group force)
2930 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2931 ;; expiration statistics of this single group
2932 (gnus-agent-expire-stats (list 0 0 0.0)))
2933 (if (or (not (eq articles t))
2935 (concat "Are you sure that you want to "
2936 "expire all articles in " group ".")))
2937 (let ((gnus-command-method (gnus-find-method-for-group group))
2938 (overview (gnus-get-buffer-create " *expire overview*"))
2941 (let ((active-file (gnus-agent-lib-file "active")))
2942 (when (file-exists-p active-file)
2944 (nnheader-insert-file-contents active-file)
2945 (gnus-active-to-gnus-format
2947 (setq orig (gnus-make-hashtable
2948 (count-lines (point-min) (point-max))))))
2950 (gnus-agent-expire-group-1
2951 group overview (gnus-gethash-safe group orig)
2953 (kill-buffer overview))))
2954 (gnus-message 4 (gnus-agent-expire-done-message)))))
2956 (defun gnus-agent-expire-group-1 (group overview active articles force)
2957 ;; Internal function - requires caller to have set
2958 ;; gnus-command-method, initialized overview buffer, and to have
2959 ;; provided a non-nil active
2961 (let ((dir (gnus-agent-group-pathname group)))
2962 (gnus-agent-with-refreshed-group
2964 (when (boundp 'gnus-agent-expire-current-dirs)
2965 (set 'gnus-agent-expire-current-dirs
2967 (symbol-value 'gnus-agent-expire-current-dirs))))
2969 (if (and (not force)
2970 (eq 'DISABLE (gnus-agent-find-parameter group
2971 'agent-enable-expiration)))
2972 (gnus-message 5 "Expiry skipping over %s" group)
2973 (gnus-message 5 "Expiring articles in %s" group)
2974 (gnus-agent-load-alist group)
2975 (let* ((bytes-freed 0)
2976 (size-files-deleted 0.0)
2978 (nov-entries-deleted 0)
2979 (info (gnus-get-info group))
2980 (alist gnus-agent-article-alist)
2981 (day (- (time-to-days (current-time))
2982 (gnus-agent-find-parameter group 'agent-days-until-old)))
2983 (specials (if (and alist
2985 ;; This could be a bit of a problem. I need to
2986 ;; keep the last article to avoid refetching
2987 ;; headers when using nntp in the backend. At
2988 ;; the same time, if someone uses a backend
2989 ;; that supports article moving then I may have
2990 ;; to remove the last article to complete the
2991 ;; move. Right now, I'm going to assume that
2992 ;; FORCE overrides specials.
2993 (list (caar (last alist)))))
2994 (unreads ;; Articles that are excluded from the
2995 ;; expiration process
2996 (cond (gnus-agent-expire-all
2997 ;; All articles are marked read by global decree
3000 ;; All articles are marked read by function
3004 ;; Unread articles are marked protected from
3005 ;; expiration Don't call
3006 ;; gnus-list-of-unread-articles as it returns
3007 ;; articles that have not been fetched into the
3010 (gnus-agent-unread-articles group)))
3012 ;; All articles EXCEPT those named by the caller
3013 ;; are protected from expiration
3014 (gnus-sorted-difference
3015 (gnus-uncompress-range
3017 (caar (last alist))))
3018 (sort articles '<)))))
3019 (marked ;; More articles that are excluded from the
3020 ;; expiration process
3021 (cond (gnus-agent-expire-all
3022 ;; All articles are unmarked by global decree
3025 ;; All articles are unmarked by function
3029 ;; All articles may as well be unmarked as the
3030 ;; unreads list already names the articles we are
3034 ;; Ticked and/or dormant articles are excluded
3037 (gnus-uncompress-range
3038 (cdr (assq 'tick (gnus-info-marks info))))
3039 (gnus-uncompress-range
3041 (gnus-info-marks info))))))))
3042 (nov-file (concat dir ".overview"))
3048 ;; The normal article alist contains elements that look like
3049 ;; (article# . fetch_date) I need to combine other
3050 ;; information with this list. For example, a flag indicating
3051 ;; that a particular article MUST BE KEPT. To do this, I'm
3052 ;; going to transform the elements to look like (article#
3053 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3054 ;; the process to generate the expired article alist.
3056 ;; Convert the alist elements to (article# fetch_date nil
3058 (setq dlist (mapcar (lambda (e)
3059 (list (car e) (cdr e) nil nil)) alist))
3061 ;; Convert the keep lists to elements that look like (article#
3062 ;; nil keep_flag nil) then append it to the expanded dlist
3063 ;; These statements are sorted by ascending precidence of the
3065 (setq dlist (nconc dlist
3067 (list e nil 'unread nil))
3069 (setq dlist (nconc dlist
3071 (list e nil 'marked nil))
3073 (setq dlist (nconc dlist
3075 (list e nil 'special nil))
3078 (set-buffer overview)
3080 (buffer-disable-undo)
3081 (when (file-exists-p nov-file)
3082 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3083 (nnheader-insert-file-contents nov-file)
3084 (goto-char (point-min))
3087 (while (< (setq p (point)) (point-max))
3089 ;; If I successfully read an integer (the plus zero
3090 ;; ensures a numeric type), prepend a marker entry
3092 (push (list (+ 0 (read (current-buffer))) nil nil
3093 (set-marker (make-marker) p))
3096 (gnus-message 1 "gnus-agent-expire: read error \
3097 occurred when reading expression at %s in %s. Skipping to next \
3098 line." (point) nov-file)))
3099 ;; Whether I succeeded, or failed, it doesn't matter.
3100 ;; Move to the next line then try again.
3104 7 "gnus-agent-expire: Loading overview... Done"))
3105 (set-buffer-modified-p nil)
3107 ;; At this point, all of the information is in dlist. The
3108 ;; only problem is that much of it is spread across multiple
3109 ;; entries. Sort then MERGE!!
3110 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3111 ;; If two entries have the same article-number then sort by
3112 ;; ascending keep_flag.
3119 (cond ((< (nth 0 a) (nth 0 b))
3121 ((> (nth 0 a) (nth 0 b))
3124 (let ((a (or (symbol-value (nth 2 a))
3126 (b (or (symbol-value (nth 2 b))
3129 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3130 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3131 (let ((dlist dlist))
3132 (while (cdr dlist) ; I'm not at the end-of-list
3133 (if (eq (caar dlist) (caadr dlist))
3134 (let ((first (cdr (car dlist)))
3135 (secnd (cdr (cadr dlist))))
3136 (setcar first (or (car first)
3137 (car secnd))) ; fetch_date
3138 (setq first (cdr first)
3140 (setcar first (or (car first)
3141 (car secnd))) ; Keep_flag
3142 (setq first (cdr first)
3144 (setcar first (or (car first)
3145 (car secnd))) ; NOV_entry_marker
3147 (setcdr dlist (cddr dlist)))
3148 (setq dlist (cdr dlist)))))
3149 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3151 (let* ((len (float (length dlist)))
3155 (let ((new-completed (truncate (* 100.0
3156 (/ (setq cnt (1+ cnt))
3159 (when (> new-completed completed)
3160 (setq completed new-completed)
3161 (gnus-message 7 "%3d%% completed..." completed)))
3162 (let* ((entry (car dlist))
3163 (article-number (nth 0 entry))
3164 (fetch-date (nth 1 entry))
3165 (keep (nth 2 entry))
3166 (marker (nth 3 entry)))
3169 ;; Kept articles are unread, marked, or special.
3171 (gnus-agent-message 10
3172 "gnus-agent-expire: %s:%d: Kept %s article%s."
3173 group article-number keep (if fetch-date " and file" ""))
3175 (unless (file-exists-p
3176 (concat dir (number-to-string
3178 (setf (nth 1 entry) nil)
3179 (gnus-agent-message 3 "gnus-agent-expire cleared \
3180 download flag on %s:%d as the cached article file is missing."
3181 group (caar dlist)))
3183 (gnus-message 1 "gnus-agent-expire detected a \
3184 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3185 (gnus-agent-append-to-list
3187 (cons article-number fetch-date)))
3189 ;; The following articles are READ, UNMARKED, and
3190 ;; ORDINARY. See if they can be EXPIRED!!!
3193 ((not (integerp fetch-date))
3194 'read) ;; never fetched article (may expire
3196 ((not (file-exists-p
3197 (concat dir (number-to-string
3199 (setf (nth 1 entry) nil)
3200 'externally-expired) ;; Can't find the cached
3201 ;; article. Handle case
3202 ;; as though this article
3203 ;; was never fetched.
3205 ;; We now have the arrival day, so we see
3206 ;; whether it's old enough to be expired.
3212 ;; I found some reason to expire this entry.
3214 (let ((actions nil))
3215 (when (memq type '(forced expired))
3216 (ignore-errors ; Just being paranoid.
3217 (let* ((file-name (nnheader-concat dir (number-to-string
3219 (size (float (nth 7 (file-attributes file-name)))))
3220 (incf bytes-freed size)
3221 (incf size-files-deleted size)
3222 (incf files-deleted)
3223 (delete-file file-name))
3224 (push "expired cached article" actions))
3225 (setf (nth 1 entry) nil)
3229 (push "NOV entry removed" actions)
3232 (incf nov-entries-deleted)
3234 (let ((from (point-at-bol))
3235 (to (progn (forward-line 1) (point))))
3236 (incf bytes-freed (- to from))
3237 (delete-region from to)))
3239 ;; If considering all articles is set, I can only
3240 ;; expire article IDs that are no longer in the
3241 ;; active range (That is, articles that preceed the
3242 ;; first article in the new alist).
3243 (if (and gnus-agent-consider-all-articles
3244 (>= article-number (car active)))
3245 ;; I have to keep this ID in the alist
3246 (gnus-agent-append-to-list
3247 tail-alist (cons article-number fetch-date))
3248 (push (format "Removed %s article number from \
3249 article alist" type) actions))
3252 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3253 group article-number
3254 (mapconcat 'identity actions ", ")))))
3257 10 "gnus-agent-expire: %s:%d: Article kept as \
3258 expiration tests failed." group article-number)
3259 (gnus-agent-append-to-list
3260 tail-alist (cons article-number fetch-date)))
3263 ;; Clean up markers as I want to recycle this buffer
3264 ;; over several groups.
3266 (set-marker marker nil))
3268 (setq dlist (cdr dlist))))
3270 (setq alist (cdr alist))
3272 (let ((inhibit-quit t))
3273 (unless (equal alist gnus-agent-article-alist)
3274 (setq gnus-agent-article-alist alist)
3275 (gnus-agent-save-alist group))
3277 (when (buffer-modified-p)
3278 (gnus-make-directory dir)
3279 (write-region-as-coding-system gnus-agent-file-coding-system
3280 (point-min) (point-max) nov-file
3282 ;; clear the modified flag as that I'm not confused by
3283 ;; its status on the next pass through this routine.
3284 (set-buffer-modified-p nil)
3285 (gnus-agent-update-view-total-fetched-for group t))
3287 (when (eq articles t)
3288 (gnus-summary-update-info))))
3290 (when (boundp 'gnus-agent-expire-stats)
3291 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3292 (incf (nth 2 stats) bytes-freed)
3293 (incf (nth 1 stats) files-deleted)
3294 (incf (nth 0 stats) nov-entries-deleted)))
3296 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3298 (defun gnus-agent-expire (&optional articles group force)
3299 "Expire all old articles.
3300 If you want to force expiring of certain articles, this function can
3301 take ARTICLES, GROUP and FORCE parameters as well.
3303 The articles on which the expiration process runs are selected as follows:
3304 if ARTICLES is null, all read and unmarked articles.
3305 if ARTICLES is t, all articles.
3306 if ARTICLES is a list, just those articles.
3307 Setting GROUP will limit expiration to that group.
3308 FORCE is equivalent to setting the expiration predicates to true."
3312 (gnus-agent-expire-group group articles force)
3313 (if (or (not (eq articles t))
3314 (yes-or-no-p "Are you sure that you want to expire all \
3315 articles in every agentized group."))
3316 (let ((methods (gnus-agent-covered-methods))
3317 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3318 ;; of agent directories.
3319 (gnus-agent-expire-current-dirs nil)
3320 ;; Bind gnus-agent-expire-stats to enable tracking of
3321 ;; expiration statistics across all groups
3322 (gnus-agent-expire-stats (list 0 0 0.0))
3323 gnus-command-method overview orig)
3324 (setq overview (gnus-get-buffer-create " *expire overview*"))
3326 (while (setq gnus-command-method (pop methods))
3327 (let ((active-file (gnus-agent-lib-file "active")))
3328 (when (file-exists-p active-file)
3330 (nnheader-insert-file-contents active-file)
3331 (gnus-active-to-gnus-format
3333 (setq orig (gnus-make-hashtable
3334 (count-lines (point-min) (point-max))))))
3335 (dolist (expiring-group (gnus-groups-from-server
3336 gnus-command-method))
3338 (gnus-gethash-safe expiring-group orig)))
3342 (gnus-agent-expire-group-1
3343 expiring-group overview active articles force))))))))
3344 (kill-buffer overview))
3345 (gnus-agent-expire-unagentized-dirs)
3346 (gnus-message 4 (gnus-agent-expire-done-message))))))
3348 (defun gnus-agent-expire-done-message ()
3349 (if (and (> gnus-verbose 4)
3350 (boundp 'gnus-agent-expire-stats))
3351 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3352 (size (nth 2 stats))
3353 (units '(B KB MB GB)))
3354 (while (and (> size 1024.0)
3356 (setq size (/ size 1024.0)
3359 (format "Expiry recovered %d NOV entries, deleted %d files,\
3366 (defun gnus-agent-expire-unagentized-dirs ()
3367 (when (and gnus-agent-expire-unagentized-dirs
3368 (boundp 'gnus-agent-expire-current-dirs))
3369 (let* ((keep (gnus-make-hashtable))
3370 ;; Formally bind gnus-agent-expire-current-dirs so that the
3371 ;; compiler will not complain about free references.
3372 (gnus-agent-expire-current-dirs
3373 (symbol-value 'gnus-agent-expire-current-dirs))
3376 (gnus-sethash gnus-agent-directory t keep)
3377 (while gnus-agent-expire-current-dirs
3378 (setq dir (pop gnus-agent-expire-current-dirs))
3379 (when (and (stringp dir)
3380 (file-directory-p dir))
3381 (while (not (gnus-gethash dir keep))
3382 (gnus-sethash dir t keep)
3383 (setq dir (file-name-directory (directory-file-name dir))))))
3390 "Given a directory, check it and its subdirectories for
3391 membership in the keep hash. If it isn't found, add
3393 (let ((files (directory-files d))
3395 (while (setq file (pop files))
3396 (cond ((equal file ".") ; Ignore self
3398 ((equal file "..") ; Ignore parent
3400 ((equal file ".overview")
3401 ;; Directory must contain .overview to be
3402 ;; agent's cache of a group.
3403 (let ((d (file-name-as-directory d))
3405 ;; Search ancestor's for last directory NOT
3406 ;; found in keep hash.
3407 (while (not (gnus-gethash
3408 (setq d (file-name-directory d)) keep))
3410 d (directory-file-name d)))
3411 ;; if ANY ancestor was NOT in keep hash and
3412 ;; it it's already in to-remove, add it to
3415 (not (member r to-remove)))
3416 (push r to-remove))))
3417 ((file-directory-p (setq file (nnheader-concat d file)))
3418 (funcall checker file)))))))))
3419 (funcall checker (expand-file-name gnus-agent-directory))
3421 (when (and to-remove
3422 (or gnus-expert-user
3424 "gnus-agent-expire has identified local directories that are\
3425 not currently required by any agentized group. Do you wish to consider\
3428 (let ((dir (pop to-remove)))
3429 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3430 (let* (delete-recursive
3435 (if (file-directory-p f-or-d)
3437 (delete-directory f-or-d)
3440 (or (member f '("." ".."))
3441 (funcall delete-recursive
3444 (directory-files f-or-d))
3445 (delete-directory f-or-d)))
3446 (delete-file f-or-d)))))))
3447 (funcall delete-recursive dir))))))))))
3450 (defun gnus-agent-batch ()
3451 "Start Gnus, send queue and fetch session."
3453 (let ((init-file-user "")
3454 (gnus-always-read-dribble-file t))
3456 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3457 (gnus-group-send-queue)
3458 (gnus-agent-fetch-session)))
3460 (defun gnus-agent-unread-articles (group)
3461 (let* ((read (gnus-info-read (gnus-get-info group)))
3462 (known (gnus-agent-load-alist group))
3464 (tail-unread unread))
3465 (while (and known read)
3466 (let ((candidate (car (pop known))))
3467 (while (let* ((range (car read))
3468 (min (if (numberp range) range (car range)))
3469 (max (if (numberp range) range (cdr range))))
3470 (cond ((or (not min)
3472 (gnus-agent-append-to-list tail-unread candidate)
3475 (setq read (cdr read))
3476 ;; return t so that I always loop one more
3477 ;; time. If I just iterated off the end of
3478 ;; read, min will become nil and the current
3479 ;; candidate will be added to the unread list.
3482 (gnus-agent-append-to-list tail-unread (car (pop known))))
3485 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3486 "Restrict ARTICLES to numbers already fetched.
3487 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3488 that have already been fetched.
3489 If CACHED-HEADER is nil, articles are only excluded if the article itself
3492 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3493 ;; 'car gnus-agent-article-alist))
3495 ;; Functionally, I don't need to construct a temp list using mapcar.
3497 (if (and (or gnus-agent-cache (not gnus-plugged))
3498 (gnus-agent-load-alist group))
3499 (let* ((ref gnus-agent-article-alist)
3501 (uncached (list nil))
3502 (tail-uncached uncached))
3503 (while (and ref arts)
3504 (let ((v1 (car arts))
3506 (cond ((< v1 v2) ; v1 does not appear in the reference list
3507 (gnus-agent-append-to-list tail-uncached v1)
3508 (setq arts (cdr arts)))
3510 (unless (or cached-header (cdar ref)) ; v1 is already cached
3511 (gnus-agent-append-to-list tail-uncached v1))
3512 (setq arts (cdr arts))
3513 (setq ref (cdr ref)))
3514 (t ; reference article (v2) preceeds the list being filtered
3515 (setq ref (cdr ref))))))
3517 (gnus-agent-append-to-list tail-uncached (pop arts)))
3519 ;; if gnus-agent-load-alist fails, no articles are cached.
3522 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3524 (gnus-agent-create-buffer)
3525 (let ((gnus-decode-encoded-word-function 'identity)
3526 (file (gnus-agent-article-name ".overview" group))
3527 cached-articles uncached-articles)
3528 (gnus-make-directory (nnheader-translate-file-chars
3529 (file-name-directory file) t))
3531 ;; Populate temp buffer with known headers
3532 (when (file-exists-p file)
3533 (with-current-buffer gnus-agent-overview-buffer
3535 (let ((nnheader-file-coding-system
3536 gnus-agent-file-coding-system))
3537 (nnheader-insert-nov-file file (car articles)))))
3539 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3542 ;; Populate nntp-server-buffer with uncached headers
3543 (set-buffer nntp-server-buffer)
3545 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3546 (gnus-retrieve-headers
3547 uncached-articles group fetch-old))))
3548 (nnvirtual-convert-headers))
3549 ((eq 'nntp (car gnus-current-select-method))
3550 ;; The author of gnus-get-newsgroup-headers-xover
3551 ;; reports that the XOVER command is commonly
3552 ;; unreliable. The problem is that recently
3553 ;; posted articles may not be entered into the
3554 ;; NOV database in time to respond to my XOVER
3557 ;; I'm going to use his assumption that the NOV
3558 ;; database is updated in order of ascending
3559 ;; article ID. Therefore, a response containing
3560 ;; article ID N implies that all articles from 1
3561 ;; to N-1 are up-to-date. Therefore, missing
3562 ;; articles in that range have expired.
3564 (set-buffer nntp-server-buffer)
3565 (let* ((fetched-articles (list nil))
3566 (tail-fetched-articles fetched-articles)
3567 (min (cond ((numberp fetch-old)
3568 (max 1 (- (car articles) fetch-old)))
3573 (max (car (last articles))))
3575 ;; Get the list of articles that were fetched
3576 (goto-char (point-min))
3577 (let ((pm (point-max))
3579 (while (< (point) pm)
3580 (when (setq art (gnus-agent-read-article-number))
3581 (gnus-agent-append-to-list tail-fetched-articles art))
3584 ;; Clip this list to the headers that will
3585 ;; actually be returned
3586 (setq fetched-articles (gnus-list-range-intersection
3587 (cdr fetched-articles)
3590 ;; Clip the uncached articles list to exclude
3591 ;; IDs after the last FETCHED header. The
3592 ;; excluded IDs may be fetchable using HEAD.
3593 (if (car tail-fetched-articles)
3594 (setq uncached-articles
3595 (gnus-list-range-intersection
3597 (cons (car uncached-articles)
3598 (car tail-fetched-articles)))))
3600 ;; Create the list of articles that were
3601 ;; "successfully" fetched. Success, in this
3602 ;; case, means that the ID should not be
3603 ;; fetched again. In the case of an expired
3604 ;; article, the header will not be fetched.
3605 (setq uncached-articles
3606 (gnus-sorted-nunion fetched-articles
3610 ;; Erase the temp buffer
3611 (set-buffer gnus-agent-overview-buffer)
3614 ;; Copy the nntp-server-buffer to the temp buffer
3615 (set-buffer nntp-server-buffer)
3616 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3618 ;; Merge the temp buffer with the known headers (found on
3619 ;; disk in FILE) into the nntp-server-buffer
3620 (when uncached-articles
3621 (gnus-agent-braid-nov group uncached-articles file))
3623 ;; Save the new set of known headers to FILE
3624 (set-buffer nntp-server-buffer)
3625 (gnus-agent-check-overview-buffer)
3626 (write-region-as-coding-system
3627 gnus-agent-file-coding-system
3628 (point-min) (point-max) file nil 'silent)
3630 (gnus-agent-update-view-total-fetched-for group t)
3632 ;; Update the group's article alist to include the newly
3633 ;; fetched articles.
3634 (gnus-agent-load-alist group)
3635 (gnus-agent-save-alist group uncached-articles nil)
3638 ;; Copy the temp buffer to the nntp-server-buffer
3639 (set-buffer nntp-server-buffer)
3641 (insert-buffer-substring gnus-agent-overview-buffer)))
3644 (not (numberp fetch-old)))
3645 t ; Don't remove anything.
3646 (nnheader-nov-delete-outside-range
3647 (if fetch-old (max 1 (- (car articles) fetch-old))
3649 (car (last articles)))
3654 (defun gnus-agent-request-article (article group)
3655 "Retrieve ARTICLE in GROUP from the agent cache."
3656 (when (and gnus-agent
3657 (or gnus-agent-cache
3660 (let* ((gnus-command-method (gnus-find-method-for-group group))
3661 (file (gnus-agent-article-name (number-to-string article) group))
3662 (buffer-read-only nil))
3663 (when (and (file-exists-p file)
3664 (> (nth 7 (file-attributes file)) 0))
3666 (gnus-kill-all-overlays)
3667 (insert-file-contents-as-coding-system gnus-cache-coding-system file)
3670 (defun gnus-agent-regenerate-group (group &optional reread)
3672 If REREAD is t, all articles in the .overview are marked as unread.
3673 If REREAD is a list, the specified articles will be marked as unread.
3674 In addition, their NOV entries in .overview will be refreshed using
3675 the articles' current headers.
3676 If REREAD is not nil, downloaded articles are marked as unread."
3678 (list (let ((def (or (gnus-group-group-name)
3679 gnus-newsgroup-name)))
3680 (let ((select (read-string (if def
3681 (concat "Group Name ("
3684 (if (and (equal "" select)
3690 (cursor-in-echo-area t)
3691 (echo-keystrokes 0))
3692 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3693 (setq c (read-char-exclusive))
3695 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3697 ((or (eq c ?a) (eq c ?A))
3699 ((or (eq c ?d) (eq c ?D))
3700 (throw 'mark 'some)))
3701 (gnus-message 3 "Ignoring unexpected input")
3705 (gnus-message 5 "Regenerating in %s" group)
3706 (let* ((gnus-command-method (or gnus-command-method
3707 (gnus-find-method-for-group group)))
3708 (file (gnus-agent-article-name ".overview" group))
3709 (dir (file-name-directory file))
3711 (downloaded (if (file-exists-p dir)
3712 (sort (mapcar (lambda (name) (string-to-int name))
3713 (directory-files dir nil "^[0-9]+$" t))
3715 (progn (gnus-make-directory dir) nil)))
3720 (mm-with-unibyte-buffer
3721 (if (file-exists-p file)
3722 (let ((nnheader-file-coding-system
3723 gnus-agent-file-coding-system))
3724 (nnheader-insert-file-contents file)))
3725 (set-buffer-modified-p nil)
3727 ;; Load the article IDs found in the overview file. As a
3728 ;; side-effect, validate the file contents.
3732 (goto-char (point-min))
3733 (while (< (point) (point-max))
3734 (cond ((and (looking-at "[0-9]+\t")
3735 (<= (- (match-end 0) (match-beginning 0)) 9))
3736 (push (read (current-buffer)) nov-arts)
3738 (let ((l1 (car nov-arts))
3739 (l2 (cadr nov-arts)))
3740 (cond ((and (listp reread) (memq l1 reread))
3742 (setq nov-arts (cdr nov-arts))
3743 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3744 entry of article %s deleted." l1))
3748 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3749 entries are NOT in ascending order.")
3750 ;; Don't sort now as I haven't verified
3751 ;; that every line begins with a number
3755 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3756 entries contained duplicate of article %s. Duplicate deleted." l1)
3758 (setq nov-arts (cdr nov-arts))))))
3760 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3761 entries contained line that did not begin with an article number. Deleted\
3763 (gnus-delete-line))))
3765 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3766 entries into ascending order.")
3767 (sort-numeric-fields 1 (point-min) (point-max))
3768 (setq nov-arts nil))))
3769 (gnus-agent-check-overview-buffer)
3771 ;; Construct a new article alist whose nodes match every header
3772 ;; in the .overview file. As a side-effect, missing headers are
3773 ;; reconstructed from the downloaded article file.
3774 (while (or downloaded nov-arts)
3775 (cond ((and downloaded
3777 (> (car downloaded) (car nov-arts))))
3778 ;; This entry is missing from the overview file
3779 (gnus-message 3 "Regenerating NOV %s %d..." group
3781 (let ((file (concat dir (number-to-string (car downloaded)))))
3782 (mm-with-unibyte-buffer
3783 (nnheader-insert-file-contents file)
3784 (nnheader-remove-body)
3785 (setq header (nnheader-parse-naked-head)))
3786 (mail-header-set-number header (car downloaded))
3788 (let ((key (concat "^" (int-to-string (car nov-arts))
3790 (or (re-search-backward key nil t)
3791 (re-search-forward key))
3793 (goto-char (point-min)))
3794 (nnheader-insert-nov header))
3795 (setq nov-arts (cons (car downloaded) nov-arts)))
3796 ((eq (car downloaded) (car nov-arts))
3797 ;; This entry in the overview has been downloaded
3798 (push (cons (car downloaded)
3800 (nth 5 (file-attributes
3801 (concat dir (number-to-string
3802 (car downloaded))))))) alist)
3803 (setq downloaded (cdr downloaded))
3804 (setq nov-arts (cdr nov-arts)))
3806 ;; This entry in the overview has not been downloaded
3807 (push (cons (car nov-arts) nil) alist)
3808 (setq nov-arts (cdr nov-arts)))))
3810 ;; When gnus-agent-consider-all-articles is set,
3811 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3812 ;; the alist. Those IDs serve as markers to indicate that an
3813 ;; attempt has been made to fetch that article's header.
3815 ;; When gnus-agent-consider-all-articles is NOT set,
3816 ;; gnus-agent-regenerate-group can remove the article ID of every
3817 ;; article (with the exception of the last ID in the list - it's
3818 ;; special) that no longer appears in the overview. In this
3819 ;; situtation, the last article ID in the list implies that it,
3820 ;; and every article ID preceeding it, have been fetched from the
3823 (if gnus-agent-consider-all-articles
3824 ;; Restore all article IDs that were not found in the overview file.
3825 (let* ((n (cons nil alist))
3827 (o (gnus-agent-load-alist group)))
3829 (let ((nID (caadr n))
3832 (setq n (setcdr n (list (list oID))))
3835 (setcdr n (cons (list oID) (cdr n)))
3841 (setq n (cdr n))))))
3842 (setq alist (cdr merged)))
3843 ;; Restore the last article ID if it is not already in the new alist
3844 (let ((n (last alist))
3845 (o (last (gnus-agent-load-alist group))))
3849 (push (cons (caar o) nil) alist))
3850 ((< (caar n) (caar o))
3851 (setcdr n (list (car o)))))))
3853 (let ((inhibit-quit t))
3854 (if (setq regenerated (buffer-modified-p))
3855 (write-region-as-coding-system
3856 gnus-agent-file-coding-system
3857 (point-min) (point-max) file nil 'silent))
3859 (setq regenerated (or regenerated
3860 (and reread gnus-agent-article-alist)
3861 (not (equal alist gnus-agent-article-alist))))
3863 (setq gnus-agent-article-alist alist)
3866 (gnus-agent-save-alist group)
3868 ;; I have to alter the group's active range NOW as
3869 ;; gnus-make-ascending-articles-unread will use it to
3870 ;; recalculate the number of unread articles in the group
3872 (let ((group (gnus-group-real-name group))
3873 (group-active (or (gnus-active group)
3874 (gnus-activate-group group))))
3875 (gnus-agent-possibly-alter-active group group-active)))))
3877 (when (and reread gnus-agent-article-alist)
3878 (gnus-make-ascending-articles-unread
3882 (delq nil (mapcar (function (lambda (c)
3883 (cond ((eq reread t)
3887 gnus-agent-article-alist))))
3890 (gnus-agent-update-files-total-fetched-for group nil)))
3896 (defun gnus-agent-regenerate (&optional clean reread)
3897 "Regenerate all agent covered files.
3898 If CLEAN, obsolete (ignore)."
3901 (gnus-message 4 "Regenerating Gnus agent files...")
3902 (dolist (gnus-command-method (gnus-agent-covered-methods))
3903 (dolist (group (gnus-groups-from-server gnus-command-method))
3904 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3906 (gnus-message 4 "Regenerating Gnus agent files...done")
3910 (defun gnus-agent-go-online (&optional force)
3911 "Switch servers into online status."
3912 (interactive (list t))
3913 (dolist (server gnus-opened-servers)
3914 (when (eq (nth 1 server) 'offline)
3915 (if (if (eq force 'ask)
3917 (format "Switch %s:%s into online status? "
3918 (caar server) (cadar server)))
3920 (setcar (nthcdr 1 server) 'close)))))
3922 (defun gnus-agent-toggle-group-plugged (group)
3923 "Toggle the status of the server of the current group."
3924 (interactive (list (gnus-group-group-name)))
3925 (let* ((method (gnus-find-method-for-group group))
3926 (status (cadr (assoc method gnus-opened-servers))))
3927 (if (eq status 'offline)
3928 (gnus-server-set-status method 'closed)
3929 (gnus-close-server method)
3930 (gnus-server-set-status method 'offline))
3931 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3932 (if (eq status 'offline) 'offline 'online)
3933 (if (eq status 'offline) 'online 'offline))))
3935 (defun gnus-agent-group-covered-p (group)
3936 (gnus-agent-method-p (gnus-group-method group)))
3938 ;; Added to support XEmacs
3940 (unless (fboundp 'directory-files-and-attributes)
3941 (defun directory-files-and-attributes (directory
3942 &optional full match nosort)
3944 (dolist (file (directory-files directory full match nosort))
3945 (push (cons file (file-attributes file)) result))
3946 (nreverse result)))))
3948 (defun gnus-agent-update-files-total-fetched-for
3949 (group delta &optional method path)
3950 "Update, or set, the total disk space used by the articles that the
3952 (when gnus-agent-total-fetched-hashtb
3953 (gnus-agent-with-refreshed-group
3955 ;; if null, gnus-agent-group-pathname will calc method.
3956 (let* ((gnus-command-method method)
3957 (path (or path (gnus-agent-group-pathname group)))
3958 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
3959 (gnus-sethash path (make-list 3 0)
3960 gnus-agent-total-fetched-hashtb))))
3965 (while (setq file (pop delta))
3966 (incf sum (float (or (nth 7 (file-attributes
3970 (number-to-string file)
3974 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
3976 (while (setq file (pop info))
3977 (incf sum (float (or (nth 8 file) 0))))
3980 (setq gnus-agent-need-update-total-fetched-for t)
3981 (incf (nth 2 entry) delta)))))
3983 (defun gnus-agent-update-view-total-fetched-for
3984 (group agent-over &optional method path)
3985 "Update, or set, the total disk space used by the .agentview and
3986 .overview files. These files are calculated separately as they can be
3988 (when gnus-agent-total-fetched-hashtb
3989 (gnus-agent-with-refreshed-group
3991 ;; if null, gnus-agent-group-pathname will calc method.
3992 (let* ((gnus-command-method method)
3993 (path (or path (gnus-agent-group-pathname group)))
3994 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
3995 (gnus-sethash path (make-list 3 0)
3996 gnus-agent-total-fetched-hashtb)))
3997 (size (or (nth 7 (file-attributes
4003 (setq gnus-agent-need-update-total-fetched-for t)
4004 (setf (nth (if agent-over 1 0) entry) size)))))
4006 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4007 "Get the total disk space used by the specified GROUP."
4008 (unless gnus-agent-total-fetched-hashtb
4009 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4011 ;; if null, gnus-agent-group-pathname will calc method.
4012 (let* ((gnus-command-method method)
4013 (path (gnus-agent-group-pathname group))
4014 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4017 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4019 (gnus-agent-update-view-total-fetched-for group nil method path)
4020 (gnus-agent-update-view-total-fetched-for group t method path)
4021 (gnus-agent-update-files-total-fetched-for group nil method path))))))
4023 (provide 'gnus-agent)
4025 ;;; gnus-agent.el ends here