1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, 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 nil
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
178 (defcustom gnus-agent-enable-expiration 'ENABLE
179 "The default expiration state for each group.
180 When set to ENABLE, the default, `gnus-agent-expire' will expire old
181 contents from a group's local storage. This value may be overridden
182 to disable expiration in specific categories, topics, and groups. Of
183 course, you could change gnus-agent-enable-expiration to DISABLE then
184 enable expiration per categories, topics, and groups."
187 :type '(radio (const :format "Enable " ENABLE)
188 (const :format "Disable " DISABLE)))
190 (defcustom gnus-agent-expire-unagentized-dirs t
191 "*Whether expiration should expire in unagentized directories.
192 Have gnus-agent-expire scan the directories under
193 \(gnus-agent-directory) for groups that are no longer agentized.
194 When found, offer to remove them."
199 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
200 "Initially, all servers from these methods are agentized.
201 The user may remove or add servers using the Server buffer.
202 See Info node `(gnus)Server Buffer'."
204 :type '(repeat symbol)
207 (defcustom gnus-agent-queue-mail t
208 "Whether and when outgoing mail should be queued by the agent.
209 When `always', always queue outgoing mail. When nil, never
210 queue. Otherwise, queue if and only if unplugged."
213 :type '(radio (const :format "Always" always)
214 (const :format "Never" nil)
215 (const :format "When plugged" t)))
217 (defcustom gnus-agent-prompt-send-queue nil
218 "If non-nil, `gnus-group-send-queue' will prompt if called when
224 ;;; Internal variables
226 (defvar gnus-agent-history-buffers nil)
227 (defvar gnus-agent-buffer-alist nil)
228 (defvar gnus-agent-article-alist nil
229 "An assoc list identifying the articles whose headers have been fetched.
230 If successfully fetched, these headers will be stored in the group's overview
231 file. The key of each assoc pair is the article ID, the value of each assoc
232 pair is a flag indicating whether the identified article has been downloaded
233 \(gnus-agent-fetch-articles sets the value to the day of the download).
235 1) The last element of this list can not be expired as some
236 routines (for example, get-agent-fetch-headers) use the last
237 value to track which articles have had their headers retrieved.
238 2) The function `gnus-agent-regenerate' may destructively modify the value.")
239 (defvar gnus-agent-group-alist nil)
240 (defvar gnus-category-alist nil)
241 (defvar gnus-agent-current-history nil)
242 (defvar gnus-agent-overview-buffer nil)
243 (defvar gnus-category-predicate-cache nil)
244 (defvar gnus-category-group-cache nil)
245 (defvar gnus-agent-spam-hashtb nil)
246 (defvar gnus-agent-file-name nil)
247 (defvar gnus-agent-send-mail-function nil)
248 (defvar gnus-agent-file-coding-system 'raw-text)
249 (defvar gnus-agent-file-loading-cache nil)
250 (defvar gnus-agent-total-fetched-hashtb nil)
251 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
252 (defvar gnus-agent-need-update-total-fetched-for nil)
255 (defvar gnus-headers)
262 (defun gnus-open-agent ()
264 (gnus-agent-read-servers)
266 (gnus-agent-create-buffer)
267 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
268 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
269 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
271 (defun gnus-agent-create-buffer ()
272 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
274 (setq gnus-agent-overview-buffer
275 (gnus-get-buffer-create " *Gnus agent overview*"))
276 (with-current-buffer gnus-agent-overview-buffer
277 (set-buffer-multibyte t))
280 (gnus-add-shutdown 'gnus-close-agent 'gnus)
282 (defun gnus-close-agent ()
283 (setq gnus-category-predicate-cache nil
284 gnus-category-group-cache nil
285 gnus-agent-spam-hashtb nil)
286 (gnus-kill-buffer gnus-agent-overview-buffer))
289 ;;; Utility functions
292 (defmacro gnus-agent-with-refreshed-group (group &rest body)
293 "Performs the body then updates the group's line in the group
294 buffer. Automatically blocks multiple updates due to recursion."
295 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
296 (when (and gnus-agent-need-update-total-fetched-for
297 (not gnus-agent-inhibit-update-total-fetched-for))
299 (set-buffer gnus-group-buffer)
300 (setq gnus-agent-need-update-total-fetched-for nil)
301 (gnus-group-update-group ,group t)))))
303 (defun gnus-agent-read-file (file)
304 "Load FILE and do a `read' there."
307 (nnheader-insert-file-contents file)
308 (goto-char (point-min))
309 (read (current-buffer)))))
311 (defsubst gnus-agent-method ()
312 (concat (symbol-name (car gnus-command-method)) "/"
313 (if (equal (cadr gnus-command-method) "")
315 (cadr gnus-command-method))))
317 (defsubst gnus-agent-directory ()
318 "The name of the Gnus agent directory."
319 (nnheader-concat gnus-agent-directory
320 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
322 (defun gnus-agent-lib-file (file)
323 "The full name of the Gnus agent library FILE."
324 (expand-file-name file
325 (file-name-as-directory
326 (expand-file-name "agent.lib" (gnus-agent-directory)))))
328 (defun gnus-agent-cat-set-property (category property value)
330 (setcdr (or (assq property category)
331 (let ((cell (cons property nil)))
332 (setcdr category (cons cell (cdr category)))
334 (let ((category category))
335 (while (cond ((eq property (caadr category))
336 (setcdr category (cddr category))
339 (setq category (cdr category)))))))
343 (defmacro gnus-agent-cat-defaccessor (name prop-name)
344 "Define accessor and setter methods for manipulating a list of the form
345 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
346 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
347 manipulated as follows:
348 (func LIST): Returns VALUE1
349 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
350 `(progn (defmacro ,name (category)
351 (list (quote cdr) (list (quote assq)
352 (quote (quote ,prop-name)) category)))
354 (define-setf-method ,name (category)
355 (let* ((--category--temp-- (make-symbol "--category--"))
356 (--value--temp-- (make-symbol "--value--")))
357 (list (list --category--temp--) ; temporary-variables
358 (list category) ; value-forms
359 (list --value--temp--) ; store-variables
360 (let* ((category --category--temp--) ; store-form
361 (value --value--temp--))
362 (list (quote gnus-agent-cat-set-property)
364 (quote (quote ,prop-name))
366 (list (quote ,name) --category--temp--) ; access-form
370 (defmacro gnus-agent-cat-name (category)
373 (gnus-agent-cat-defaccessor
374 gnus-agent-cat-days-until-old agent-days-until-old)
375 (gnus-agent-cat-defaccessor
376 gnus-agent-cat-enable-expiration agent-enable-expiration)
377 (gnus-agent-cat-defaccessor
378 gnus-agent-cat-groups agent-groups)
379 (gnus-agent-cat-defaccessor
380 gnus-agent-cat-high-score agent-high-score)
381 (gnus-agent-cat-defaccessor
382 gnus-agent-cat-length-when-long agent-long-article)
383 (gnus-agent-cat-defaccessor
384 gnus-agent-cat-length-when-short agent-short-article)
385 (gnus-agent-cat-defaccessor
386 gnus-agent-cat-low-score agent-low-score)
387 (gnus-agent-cat-defaccessor
388 gnus-agent-cat-predicate agent-predicate)
389 (gnus-agent-cat-defaccessor
390 gnus-agent-cat-score-file agent-score)
391 (gnus-agent-cat-defaccessor
392 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
395 ;; This form is equivalent to defsetf except that it calls make-symbol
396 ;; whereas defsetf calls gensym (Using gensym creates a run-time
397 ;; dependency on the CL library).
400 (define-setf-method gnus-agent-cat-groups (category)
401 (let* ((--category--temp-- (make-symbol "--category--"))
402 (--groups--temp-- (make-symbol "--groups--")))
403 (list (list --category--temp--)
405 (list --groups--temp--)
406 (let* ((category --category--temp--)
407 (groups --groups--temp--))
408 (list (quote gnus-agent-set-cat-groups) category groups))
409 (list (quote gnus-agent-cat-groups) --category--temp--))))
412 (defun gnus-agent-set-cat-groups (category groups)
413 (unless (eq groups 'ignore)
415 (old-g (gnus-agent-cat-groups category)))
416 (cond ((eq new-g old-g)
417 ;; gnus-agent-add-group is fiddling with the group
418 ;; list. Still, Im done.
421 ((eq new-g (cdr old-g))
422 ;; gnus-agent-add-group is fiddling with the group list
423 (setcdr (or (assq 'agent-groups category)
424 (let ((cell (cons 'agent-groups nil)))
425 (setcdr category (cons cell (cdr category)))
428 (let ((groups groups))
430 (let* ((group (pop groups))
431 (old-category (gnus-group-category group)))
432 (if (eq category old-category)
434 (setf (gnus-agent-cat-groups old-category)
435 (delete group (gnus-agent-cat-groups
437 ;; Purge cache as preceeding loop invalidated it.
438 (setq gnus-category-group-cache nil))
440 (setcdr (or (assq 'agent-groups category)
441 (let ((cell (cons 'agent-groups nil)))
442 (setcdr category (cons cell (cdr category)))
445 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
446 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
448 ;;; Fetching setup functions.
450 (defun gnus-agent-start-fetch ()
451 "Initialize data structures for efficient fetching."
452 (gnus-agent-create-buffer))
454 (defun gnus-agent-stop-fetch ()
455 "Save all data structures and clean up."
456 (setq gnus-agent-spam-hashtb nil)
458 (set-buffer nntp-server-buffer)
461 (defmacro gnus-agent-with-fetch (&rest forms)
464 (let ((gnus-agent-fetching t))
465 (gnus-agent-start-fetch)
467 (gnus-agent-stop-fetch)))
469 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
470 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
472 (defmacro gnus-agent-append-to-list (tail value)
473 `(setq ,tail (setcdr ,tail (cons ,value nil))))
475 (defmacro gnus-agent-message (level &rest args)
476 `(if (<= ,level gnus-verbose)
483 (defvar gnus-agent-mode-hook nil
484 "Hook run when installing agent mode.")
486 (defvar gnus-agent-mode nil)
487 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
489 (defun gnus-agent-mode ()
490 "Minor mode for providing a agent support in Gnus buffers."
491 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
492 (symbol-name major-mode))
493 (match-string 1 (symbol-name major-mode))))
494 (mode (intern (format "gnus-agent-%s-mode" buffer))))
495 (set (make-local-variable 'gnus-agent-mode) t)
497 (set (make-local-variable mode) t)
499 (when (gnus-visual-p 'agent-menu 'menu)
500 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
501 (unless (assq 'gnus-agent-mode minor-mode-alist)
502 (push gnus-agent-mode-status minor-mode-alist))
503 (unless (assq mode minor-mode-map-alist)
504 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
506 minor-mode-map-alist))
507 (when (eq major-mode 'gnus-group-mode)
508 (let ((init-plugged gnus-plugged)
509 (gnus-agent-go-online nil))
510 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
511 ;; Therefore, make certain that the current value does not
512 ;; match the desired initial value.
513 (setq gnus-plugged :unknown)
514 (gnus-agent-toggle-plugged init-plugged)))
515 (gnus-run-hooks 'gnus-agent-mode-hook
516 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
518 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
519 (gnus-define-keys gnus-agent-group-mode-map
520 "Ju" gnus-agent-fetch-groups
521 "Jc" gnus-enter-category-buffer
522 "Jj" gnus-agent-toggle-plugged
523 "Js" gnus-agent-fetch-session
524 "JY" gnus-agent-synchronize-flags
525 "JS" gnus-group-send-queue
526 "Ja" gnus-agent-add-group
527 "Jr" gnus-agent-remove-group
528 "Jo" gnus-agent-toggle-group-plugged)
530 (defun gnus-agent-group-make-menu-bar ()
531 (unless (boundp 'gnus-agent-group-menu)
533 gnus-agent-group-menu gnus-agent-group-mode-map ""
535 ["Toggle plugged" gnus-agent-toggle-plugged t]
536 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
537 ["List categories" gnus-enter-category-buffer t]
538 ["Add (current) group to category" gnus-agent-add-group t]
539 ["Remove (current) group from category" gnus-agent-remove-group t]
540 ["Send queue" gnus-group-send-queue gnus-plugged]
542 ["All" gnus-agent-fetch-session gnus-plugged]
543 ["Group" gnus-agent-fetch-group gnus-plugged])
544 ["Synchronize flags" gnus-agent-synchronize-flags t]
547 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
548 (gnus-define-keys gnus-agent-summary-mode-map
549 "Jj" gnus-agent-toggle-plugged
550 "Ju" gnus-agent-summary-fetch-group
551 "JS" gnus-agent-fetch-group
552 "Js" gnus-agent-summary-fetch-series
553 "J#" gnus-agent-mark-article
554 "J\M-#" gnus-agent-unmark-article
555 "@" gnus-agent-toggle-mark
556 "Jc" gnus-agent-catchup)
558 (defun gnus-agent-summary-make-menu-bar ()
559 (unless (boundp 'gnus-agent-summary-menu)
561 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
563 ["Toggle plugged" gnus-agent-toggle-plugged t]
564 ["Mark as downloadable" gnus-agent-mark-article t]
565 ["Unmark as downloadable" gnus-agent-unmark-article t]
566 ["Toggle mark" gnus-agent-toggle-mark t]
567 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
568 ["Catchup undownloaded" gnus-agent-catchup t]))))
570 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
571 (gnus-define-keys gnus-agent-server-mode-map
572 "Jj" gnus-agent-toggle-plugged
573 "Ja" gnus-agent-add-server
574 "Jr" gnus-agent-remove-server)
576 (defun gnus-agent-server-make-menu-bar ()
577 (unless (boundp 'gnus-agent-server-menu)
579 gnus-agent-server-menu gnus-agent-server-mode-map ""
581 ["Toggle plugged" gnus-agent-toggle-plugged t]
582 ["Add" gnus-agent-add-server t]
583 ["Remove" gnus-agent-remove-server t]))))
585 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
586 (if (and (fboundp 'propertize)
587 (fboundp 'make-mode-line-mouse-map))
588 (propertize string 'local-map
589 (make-mode-line-mouse-map mouse-button mouse-func)
590 'mouse-face 'mode-line-highlight)
593 (defun gnus-agent-toggle-plugged (set-to)
594 "Toggle whether Gnus is unplugged or not."
595 (interactive (list (not gnus-plugged)))
596 (cond ((eq set-to gnus-plugged)
599 (setq gnus-plugged set-to)
600 (gnus-run-hooks 'gnus-agent-plugged-hook)
601 (setcar (cdr gnus-agent-mode-status)
602 (gnus-agent-make-mode-line-string " Plugged"
604 'gnus-agent-toggle-plugged))
605 (gnus-agent-go-online gnus-agent-go-online)
606 (gnus-agent-possibly-synchronize-flags))
608 (gnus-agent-close-connections)
609 (setq gnus-plugged set-to)
610 (gnus-run-hooks 'gnus-agent-unplugged-hook)
611 (setcar (cdr gnus-agent-mode-status)
612 (gnus-agent-make-mode-line-string " Unplugged"
614 'gnus-agent-toggle-plugged))))
615 (force-mode-line-update)
616 (set-buffer-modified-p t))
618 (defmacro gnus-agent-while-plugged (&rest body)
619 `(let ((original-gnus-plugged gnus-plugged))
621 (progn (gnus-agent-toggle-plugged t)
623 (gnus-agent-toggle-plugged original-gnus-plugged))))
625 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
626 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
628 (defun gnus-agent-close-connections ()
629 "Close all methods covered by the Gnus agent."
630 (let ((methods (gnus-agent-covered-methods)))
632 (gnus-close-server (pop methods)))))
635 (defun gnus-unplugged ()
636 "Start Gnus unplugged."
638 (setq gnus-plugged nil)
642 (defun gnus-plugged ()
643 "Start Gnus plugged."
645 (setq gnus-plugged t)
649 (defun gnus-slave-unplugged (&optional arg)
650 "Read news as a slave unplugged."
652 (setq gnus-plugged nil)
653 (gnus arg nil 'slave))
656 (defun gnus-agentize ()
657 "Allow Gnus to be an offline newsreader.
659 The gnus-agentize function is now called internally by gnus when
660 gnus-agent is set. If you wish to avoid calling gnus-agentize,
661 customize gnus-agent to nil.
663 This will modify the `gnus-setup-news-hook', and
664 `message-send-mail-real-function' variables, and install the Gnus agent
665 minor mode in all Gnus buffers."
668 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
669 (unless gnus-agent-send-mail-function
670 (setq gnus-agent-send-mail-function
671 (or message-send-mail-real-function
672 (function (lambda () (funcall message-send-mail-function))))
673 message-send-mail-real-function 'gnus-agent-send-mail))
675 ;; If the servers file doesn't exist, auto-agentize some servers and
676 ;; save the servers file so this auto-agentizing isn't invoked
678 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
679 (gnus-message 3 "First time agent user, agentizing remote groups...")
681 (lambda (server-or-method)
682 (let ((method (gnus-server-to-method server-or-method)))
683 (when (memq (car method)
684 gnus-agent-auto-agentize-methods)
685 (push (gnus-method-to-server method)
686 gnus-agent-covered-methods)
687 (setq gnus-agent-method-p-cache nil))))
688 (cons gnus-select-method gnus-secondary-select-methods))
689 (gnus-agent-write-servers)))
691 (defun gnus-agent-queue-setup (&optional group-name)
692 "Make sure the queue group exists.
693 Optional arg GROUP-NAME allows to specify another group."
694 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
696 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
697 (let ((gnus-level-default-subscribed 1))
698 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
700 (gnus-group-set-parameter
701 (format "nndraft:%s" (or group-name "queue"))
702 'gnus-dummy '((gnus-draft-mode)))))
704 (defun gnus-agent-send-mail ()
705 (if (or (not gnus-agent-queue-mail)
706 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
707 (funcall gnus-agent-send-mail-function)
708 (goto-char (point-min))
710 (concat "^" (regexp-quote mail-header-separator) "\n"))
712 (gnus-agent-insert-meta-information 'mail)
713 (gnus-request-accept-article "nndraft:queue" nil t t)))
715 (defun gnus-agent-insert-meta-information (type &optional method)
716 "Insert meta-information into the message that says how it's to be posted.
717 TYPE can be either `mail' or `news'. If the latter, then METHOD can
720 (message-remove-header gnus-agent-meta-information-header)
721 (goto-char (point-min))
722 (insert gnus-agent-meta-information-header ": "
723 (symbol-name type) " " (format "%S" method)
726 (while (search-backward "\n" nil t)
727 (replace-match "\\n" t t))))
729 (defun gnus-agent-restore-gcc ()
730 "Restore GCC field from saved header."
732 (goto-char (point-min))
733 (while (re-search-forward
734 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
735 (replace-match "Gcc:" 'fixedcase))))
737 (defun gnus-agent-any-covered-gcc ()
739 (message-narrow-to-headers)
740 (let* ((gcc (mail-fetch-field "gcc" nil t))
742 (mapcar 'gnus-inews-group-method
743 (message-unquote-tokens
744 (message-tokenize-header
747 (while (and (not covered) methods)
748 (setq covered (gnus-agent-method-p (car methods))
749 methods (cdr methods)))
753 (defun gnus-agent-possibly-save-gcc ()
754 "Save GCC if Gnus is unplugged."
755 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
757 (goto-char (point-min))
758 (let ((case-fold-search t))
759 (while (re-search-forward "^gcc:" nil t)
760 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
762 (defun gnus-agent-possibly-do-gcc ()
763 "Do GCC if Gnus is plugged."
764 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
765 (gnus-inews-do-gcc)))
768 ;;; Group mode commands
771 (defun gnus-agent-fetch-groups (n)
772 "Put all new articles in the current groups into the Agent."
775 (error "Groups can't be fetched when Gnus is unplugged"))
776 (gnus-group-iterate n 'gnus-agent-fetch-group))
778 (defun gnus-agent-fetch-group (&optional group)
779 "Put all new articles in GROUP into the Agent."
780 (interactive (list (gnus-group-group-name)))
781 (setq group (or group gnus-newsgroup-name))
783 (error "No group on the current line"))
785 (gnus-agent-while-plugged
786 (let ((gnus-command-method (gnus-find-method-for-group group)))
787 (gnus-agent-with-fetch
788 (gnus-agent-fetch-group-1 group gnus-command-method)
789 (gnus-message 5 "Fetching %s...done" group)))))
791 (defun gnus-agent-add-group (category arg)
792 "Add the current group to an agent category."
798 (mapcar (lambda (cat) (list (symbol-name (car cat))))
802 (let ((cat (assq category gnus-category-alist))
804 (gnus-group-iterate arg
806 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
807 (setf (gnus-agent-cat-groups c)
808 (delete group (gnus-agent-cat-groups c))))
809 (push group groups)))
810 (setf (gnus-agent-cat-groups cat)
811 (nconc (gnus-agent-cat-groups cat) groups))
812 (gnus-category-write)))
814 (defun gnus-agent-remove-group (arg)
815 "Remove the current group from its agent category, if any."
818 (gnus-group-iterate arg
820 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
821 (setf (gnus-agent-cat-groups c)
822 (delete group (gnus-agent-cat-groups c))))))
823 (gnus-category-write)))
825 (defun gnus-agent-synchronize-flags ()
826 "Synchronize unplugged flags with servers."
829 (dolist (gnus-command-method (gnus-agent-covered-methods))
830 (when (file-exists-p (gnus-agent-lib-file "flags"))
831 (gnus-agent-synchronize-flags-server gnus-command-method)))))
833 (defun gnus-agent-possibly-synchronize-flags ()
834 "Synchronize flags according to `gnus-agent-synchronize-flags'."
837 (dolist (gnus-command-method (gnus-agent-covered-methods))
838 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
839 (not (eq (gnus-server-status gnus-command-method) 'offline)))
840 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
842 (defun gnus-agent-synchronize-flags-server (method)
843 "Synchronize flags set when unplugged for server."
844 (let ((gnus-command-method method)
846 (when (file-exists-p (gnus-agent-lib-file "flags"))
847 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
849 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
850 (cond ((null gnus-plugged)
852 1 "You must be plugged to synchronize flags with server %s"
853 (nth 1 gnus-command-method)))
854 ((null (gnus-check-server gnus-command-method))
856 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
861 (eval (read (current-buffer)))
862 (delete-region bgn (point))))
864 (delete-file (gnus-agent-lib-file "flags")))
866 (let ((file (gnus-agent-lib-file "flags")))
867 (write-region (point-min) (point-max)
868 (gnus-agent-lib-file "flags") nil 'silent)
869 (error "Couldn't set flags from file %s due to %s"
870 file (error-message-string err)))))))
873 (defun gnus-agent-possibly-synchronize-flags-server (method)
874 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
875 (when (or (and gnus-agent-synchronize-flags
876 (not (eq gnus-agent-synchronize-flags 'ask)))
877 (and (eq gnus-agent-synchronize-flags 'ask)
878 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
880 (gnus-agent-synchronize-flags-server method)))
883 (defun gnus-agent-rename-group (old-group new-group)
884 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
885 disabled, as the old agent files would corrupt gnus when the agent was
886 next enabled. Depends upon the caller to determine whether group renaming is supported."
887 (let* ((old-command-method (gnus-find-method-for-group old-group))
888 (old-path (directory-file-name
889 (let (gnus-command-method old-command-method)
890 (gnus-agent-group-pathname old-group))))
891 (new-command-method (gnus-find-method-for-group new-group))
892 (new-path (directory-file-name
893 (let (gnus-command-method new-command-method)
894 (gnus-agent-group-pathname new-group)))))
895 (gnus-rename-file old-path new-path t)
897 (let* ((old-real-group (gnus-group-real-name old-group))
898 (new-real-group (gnus-group-real-name new-group))
899 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
900 (gnus-agent-save-group-info old-command-method old-real-group nil)
901 (gnus-agent-save-group-info new-command-method new-real-group old-active)
903 (let ((old-local (gnus-agent-get-local old-group
904 old-real-group old-command-method)))
905 (gnus-agent-set-local old-group
907 old-real-group old-command-method)
908 (gnus-agent-set-local new-group
909 (car old-local) (cdr old-local)
910 new-real-group new-command-method)))))
913 (defun gnus-agent-delete-group (group)
914 "Delete fully-qualified GROUP. Always updates the agent, even when
915 disabled, as the old agent files would corrupt gnus when the agent was
916 next enabled. Depends upon the caller to determine whether group deletion is supported."
917 (let* ((command-method (gnus-find-method-for-group group))
918 (path (directory-file-name
919 (let (gnus-command-method command-method)
920 (gnus-agent-group-pathname group)))))
921 (gnus-delete-directory path)
923 (let* ((real-group (gnus-group-real-name group)))
924 (gnus-agent-save-group-info command-method real-group nil)
926 (let ((local (gnus-agent-get-local group
927 real-group command-method)))
928 (gnus-agent-set-local group
930 real-group command-method)))))
933 ;;; Server mode commands
936 (defun gnus-agent-add-server ()
937 "Enroll SERVER in the agent program."
939 (let* ((server (gnus-server-server-name))
940 (named-server (gnus-server-named-server))
942 (gnus-server-get-method nil server))))
944 (error "No server on the current line"))
946 (when (gnus-agent-method-p method)
947 (error "Server already in the agent program"))
949 (push named-server gnus-agent-covered-methods)
951 (setq gnus-agent-method-p-cache nil)
952 (gnus-server-update-server server)
953 (gnus-agent-write-servers)
954 (gnus-message 1 "Entered %s into the Agent" server)))
956 (defun gnus-agent-remove-server ()
957 "Remove SERVER from the agent program."
959 (let* ((server (gnus-server-server-name))
960 (named-server (gnus-server-named-server)))
962 (error "No server on the current line"))
964 (unless (member named-server gnus-agent-covered-methods)
965 (error "Server not in the agent program"))
967 (setq gnus-agent-covered-methods
968 (delete named-server gnus-agent-covered-methods)
969 gnus-agent-method-p-cache nil)
971 (gnus-server-update-server server)
972 (gnus-agent-write-servers)
973 (gnus-message 1 "Removed %s from the agent" server)))
975 (defun gnus-agent-read-servers ()
976 "Read the alist of covered servers."
977 (setq gnus-agent-covered-methods
978 (gnus-agent-read-file
979 (nnheader-concat gnus-agent-directory "lib/servers"))
980 gnus-agent-method-p-cache nil)
982 ;; I am called so early in start-up that I can not validate server
983 ;; names. When that is the case, I skip the validation. That is
984 ;; alright as the gnus startup code calls the validate methods
986 (if gnus-server-alist
987 (gnus-agent-read-servers-validate)))
989 (defun gnus-agent-read-servers-validate ()
990 (mapcar (lambda (server-or-method)
991 (let* ((server (if (stringp server-or-method)
993 (gnus-method-to-server server-or-method)))
994 (method (gnus-server-to-method server)))
996 (unless (member server gnus-agent-covered-methods)
997 (push server gnus-agent-covered-methods)
998 (setq gnus-agent-method-p-cache nil))
999 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1000 (prog1 gnus-agent-covered-methods
1001 (setq gnus-agent-covered-methods nil))))
1003 (defun gnus-agent-read-servers-validate-native (native-method)
1004 (setq gnus-agent-covered-methods
1005 (mapcar (lambda (method)
1006 (if (or (not method)
1007 (equal method native-method))
1009 method)) gnus-agent-covered-methods)))
1011 (defun gnus-agent-write-servers ()
1012 "Write the alist of covered servers."
1013 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1014 (let ((coding-system-for-write nnheader-file-coding-system)
1015 (file-name-coding-system nnmail-pathname-coding-system))
1016 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1017 (prin1 gnus-agent-covered-methods
1018 (current-buffer)))))
1021 ;;; Summary commands
1024 (defun gnus-agent-mark-article (n &optional unmark)
1025 "Mark the next N articles as downloadable.
1026 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1027 the mark instead. The difference between N and the actual number of
1028 articles marked is returned."
1030 (let ((backward (< n 0))
1035 (gnus-summary-set-agent-mark
1036 (gnus-summary-article-number) unmark)
1037 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1040 (gnus-message 7 "No more articles"))
1041 (gnus-summary-recenter)
1042 (gnus-summary-position-point)
1045 (defun gnus-agent-unmark-article (n)
1046 "Remove the downloadable mark from the next N articles.
1047 If N is negative, unmark backward instead. The difference between N and
1048 the actual number of articles unmarked is returned."
1050 (gnus-agent-mark-article n t))
1052 (defun gnus-agent-toggle-mark (n)
1053 "Toggle the downloadable mark from the next N articles.
1054 If N is negative, toggle backward instead. The difference between N and
1055 the actual number of articles toggled is returned."
1057 (gnus-agent-mark-article n 'toggle))
1059 (defun gnus-summary-set-agent-mark (article &optional unmark)
1060 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1061 When UNMARK is t, the article is unmarked. For any other value, the
1062 article's mark is toggled."
1063 (let ((unmark (cond ((eq nil unmark)
1068 (memq article gnus-newsgroup-downloadable)))))
1069 (when (gnus-summary-goto-subject article nil t)
1070 (gnus-summary-update-mark
1073 (setq gnus-newsgroup-downloadable
1074 (delq article gnus-newsgroup-downloadable))
1075 (gnus-article-mark article))
1076 (setq gnus-newsgroup-downloadable
1077 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1078 gnus-downloadable-mark)
1082 (defun gnus-agent-get-undownloaded-list ()
1083 "Construct list of articles that have not been downloaded."
1084 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1085 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1086 (gnus-agent-method-p gnus-command-method))
1087 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1088 (headers (sort (mapcar (lambda (h)
1089 (mail-header-number h))
1090 gnus-newsgroup-headers) '<))
1091 (cached (and gnus-use-cache gnus-newsgroup-cached))
1092 (undownloaded (list nil))
1093 (tail-undownloaded undownloaded)
1094 (unfetched (list nil))
1095 (tail-unfetched unfetched))
1096 (while (and alist headers)
1097 (let ((a (caar alist))
1100 ;; Ignore IDs in the alist that are not being
1101 ;; displayed in the summary.
1102 (setq alist (cdr alist)))
1104 ;; Headers that are not in the alist should be
1105 ;; fictious (see nnagent-retrieve-headers); they
1106 ;; imply that this article isn't in the agent.
1107 (gnus-agent-append-to-list tail-undownloaded h)
1108 (gnus-agent-append-to-list tail-unfetched h)
1109 (setq headers (cdr headers)))
1111 (setq alist (cdr alist))
1112 (setq headers (cdr headers))
1113 nil ; ignore already downloaded
1116 (setq alist (cdr alist))
1117 (setq headers (cdr headers))
1119 ;; This article isn't in the agent. Check to see
1120 ;; if it is in the cache. If it is, it's been
1122 (while (and cached (< (car cached) a))
1123 (setq cached (cdr cached)))
1124 (unless (equal a (car cached))
1125 (gnus-agent-append-to-list tail-undownloaded a))))))
1128 (let ((num (pop headers)))
1129 (gnus-agent-append-to-list tail-undownloaded num)
1130 (gnus-agent-append-to-list tail-unfetched num)))
1132 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1133 gnus-newsgroup-unfetched (cdr unfetched))))))
1135 (defun gnus-agent-catchup ()
1136 "Mark as read all unhandled articles.
1137 An article is unhandled if it is neither cached, nor downloaded, nor
1141 (let ((articles gnus-newsgroup-undownloaded))
1142 (when (or gnus-newsgroup-downloadable
1143 gnus-newsgroup-cached)
1144 (setq articles (gnus-sorted-ndifference
1145 (gnus-sorted-ndifference
1146 (gnus-copy-sequence articles)
1147 gnus-newsgroup-downloadable)
1148 gnus-newsgroup-cached)))
1151 (gnus-summary-mark-article
1152 (pop articles) gnus-catchup-mark)))
1153 (gnus-summary-position-point)))
1155 (defun gnus-agent-summary-fetch-series ()
1157 (when gnus-newsgroup-processable
1158 (setq gnus-newsgroup-downloadable
1159 (let* ((dl gnus-newsgroup-downloadable)
1160 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1161 (gnus-newsgroup-downloadable processable))
1162 (gnus-agent-summary-fetch-group)
1164 ;; For each article that I processed that is no longer
1165 ;; undownloaded, remove its processable mark.
1167 (mapc #'gnus-summary-remove-process-mark
1168 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1170 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1171 ;; updated the temporary gnus-newsgroup-downloadable to
1172 ;; remove each article successfully fetched. Now, I
1173 ;; update the real gnus-newsgroup-downloadable to only
1174 ;; include undownloaded articles.
1175 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1177 (defun gnus-agent-summary-fetch-group (&optional all)
1178 "Fetch the downloadable articles in the group.
1179 Optional arg ALL, if non-nil, means to fetch all articles."
1182 (if all gnus-newsgroup-articles
1183 gnus-newsgroup-downloadable))
1184 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1186 (gnus-agent-while-plugged
1188 (error "No articles to download"))
1189 (gnus-agent-with-fetch
1190 (setq gnus-newsgroup-undownloaded
1191 (gnus-sorted-ndifference
1192 gnus-newsgroup-undownloaded
1193 (setq fetched-articles
1194 (gnus-agent-fetch-articles
1195 gnus-newsgroup-name articles)))))
1197 (dolist (article articles)
1198 (let ((was-marked-downloadable
1199 (memq article gnus-newsgroup-downloadable)))
1200 (cond (gnus-agent-mark-unread-after-downloaded
1201 (setq gnus-newsgroup-downloadable
1202 (delq article gnus-newsgroup-downloadable))
1204 (gnus-summary-mark-article article gnus-unread-mark))
1205 (was-marked-downloadable
1206 (gnus-summary-set-agent-mark article t)))
1207 (when (gnus-summary-goto-subject article nil t)
1208 (gnus-summary-update-download-mark article))))))
1211 (defun gnus-agent-fetch-selected-article ()
1212 "Fetch the current article as it is selected.
1213 This can be added to `gnus-select-article-hook' or
1214 `gnus-mark-article-hook'."
1215 (let ((gnus-command-method gnus-current-select-method))
1216 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1217 (when (gnus-agent-fetch-articles
1219 (list gnus-current-article))
1220 (setq gnus-newsgroup-undownloaded
1221 (delq gnus-current-article gnus-newsgroup-undownloaded))
1222 (gnus-summary-update-download-mark gnus-current-article)))))
1225 ;;; Internal functions
1228 (defun gnus-agent-synchronize-group-flags (group actions server)
1229 "Update a plugged group by performing the indicated actions."
1230 (let* ((gnus-command-method (gnus-server-to-method server))
1232 ;; This initializer is required as gnus-request-set-mark
1233 ;; calls gnus-group-real-name to strip off the host name
1234 ;; before calling the backend. Now that the backend is
1235 ;; trying to call gnus-request-set-mark, I have to
1236 ;; reconstruct the original group name.
1237 (or (gnus-get-info group)
1239 (setq group (gnus-group-full-name
1240 group gnus-command-method))))))
1241 (gnus-request-set-mark group actions)
1244 (dolist (action actions)
1245 (let ((range (nth 0 action))
1246 (what (nth 1 action))
1247 (marks (nth 2 action)))
1248 (dolist (mark marks)
1249 (cond ((eq mark 'read)
1252 (funcall (if (eq what 'add)
1254 'gnus-remove-from-range)
1255 (gnus-info-read info)
1257 (gnus-get-unread-articles-in-group
1259 (gnus-active (gnus-info-group info))))
1260 ((memq mark '(tick))
1261 (let ((info-marks (assoc mark (gnus-info-marks info))))
1263 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1264 (setcdr info-marks (funcall (if (eq what 'add)
1266 'gnus-remove-from-range)
1270 ;;Marks can be synchronized at any time by simply toggling from
1271 ;;unplugged to plugged. If that is what is happening right now, make
1272 ;;sure that the group buffer is up to date.
1273 (when (gnus-buffer-live-p gnus-group-buffer)
1274 (gnus-group-update-group group t)))
1277 (defun gnus-agent-save-active (method)
1278 (when (gnus-agent-method-p method)
1279 (let* ((gnus-command-method method)
1280 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1281 (file (gnus-agent-lib-file "active")))
1282 (gnus-active-to-gnus-format nil new)
1283 (gnus-agent-write-active file new)
1285 (nnheader-insert-file-contents file))))
1287 (defun gnus-agent-write-active (file new)
1288 (gnus-make-directory (file-name-directory file))
1289 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1290 ;; The hashtable contains real names of groups. However, do NOT
1291 ;; add the foreign server prefix as gnus-active-to-gnus-format
1292 ;; will add it while reading the file.
1293 (gnus-write-active-file file new nil)))
1296 (defun gnus-agent-possibly-alter-active (group active &optional info)
1297 "Possibly expand a group's active range to include articles
1298 downloaded into the agent."
1299 (let* ((gnus-command-method (or gnus-command-method
1300 (gnus-find-method-for-group group))))
1301 (when (gnus-agent-method-p gnus-command-method)
1302 (let* ((local (gnus-agent-get-local group))
1303 (active-min (or (car active) 0))
1304 (active-max (or (cdr active) 0))
1305 (agent-min (or (car local) active-min))
1306 (agent-max (or (cdr local) active-max)))
1308 (when (< agent-min active-min)
1309 (setcar active agent-min))
1311 (when (> agent-max active-max)
1312 (setcdr active agent-max))
1314 (when (and info (< agent-max (- active-min 100)))
1315 ;; I'm expanding the active range by such a large amount
1316 ;; that there is a gap of more than 100 articles between the
1317 ;; last article known to the agent and the first article
1318 ;; currently available on the server. This gap contains
1319 ;; articles that have been lost, mark them as read so that
1320 ;; gnus doesn't waste resources trying to fetch them.
1322 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1323 ;; want to modify the local file everytime someone restarts
1324 ;; gnus. The small gap will cause a tiny performance hit
1325 ;; when gnus tries, and fails, to retrieve the articles.
1326 ;; Still that should be smaller than opening a buffer,
1327 ;; printing this list to the buffer, and then writing it to a
1330 (let ((read (gnus-info-read info)))
1335 (list (cons (1+ agent-max)
1336 (1- active-min))))))
1338 ;; Lie about the agent's local range for this group to
1339 ;; disable the set read each time this server is opened.
1340 ;; NOTE: Opening this group will restore the valid local
1341 ;; range but it will also expand the local range to
1342 ;; incompass the new active range.
1343 (gnus-agent-set-local group agent-min (1- active-min)))))))
1345 (defun gnus-agent-save-group-info (method group active)
1346 "Update a single group's active range in the agent's copy of the server's active file."
1347 (when (gnus-agent-method-p method)
1348 (let* ((gnus-command-method (or method gnus-command-method))
1349 (coding-system-for-write nnheader-file-coding-system)
1350 (file-name-coding-system nnmail-pathname-coding-system)
1351 (file (gnus-agent-lib-file "active"))
1352 oactive-min oactive-max)
1353 (gnus-make-directory (file-name-directory file))
1354 (with-temp-file file
1355 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1356 (set-buffer-multibyte nil)
1357 (when (file-exists-p file)
1358 (nnheader-insert-file-contents file)
1360 (goto-char (point-min))
1361 (when (re-search-forward
1362 (concat "^" (regexp-quote group) " ") nil t)
1364 (setq oactive-max (read (current-buffer)) ;; max
1365 oactive-min (read (current-buffer)))) ;; min
1366 (gnus-delete-line)))
1368 (insert (format "%S %d %d y\n" (intern group)
1369 (max (or oactive-max (cdr active)) (cdr active))
1370 (min (or oactive-min (car active)) (car active))))
1371 (goto-char (point-max))
1372 (while (search-backward "\\." nil t)
1373 (delete-char 1)))))))
1375 (defun gnus-agent-get-group-info (method group)
1376 "Get a single group's active range in the agent's copy of the server's active file."
1377 (when (gnus-agent-method-p method)
1378 (let* ((gnus-command-method (or method gnus-command-method))
1379 (coding-system-for-write nnheader-file-coding-system)
1380 (file-name-coding-system nnmail-pathname-coding-system)
1381 (file (gnus-agent-lib-file "active"))
1382 oactive-min oactive-max)
1383 (gnus-make-directory (file-name-directory file))
1385 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1386 (mm-disable-multibyte)
1387 (when (file-exists-p file)
1388 (nnheader-insert-file-contents file)
1390 (goto-char (point-min))
1391 (when (re-search-forward
1392 (concat "^" (regexp-quote group) " ") nil t)
1394 (setq oactive-max (read (current-buffer)) ;; max
1395 oactive-min (read (current-buffer))) ;; min
1396 (cons oactive-min oactive-max))))))))
1398 (defun gnus-agent-group-path (group)
1399 "Translate GROUP into a file name."
1401 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1402 ;; The two methods must be kept synchronized, which is why
1403 ;; gnus-agent-group-pathname was added.
1406 (nnheader-translate-file-chars
1407 (nnheader-replace-duplicate-chars-in-string
1408 (nnheader-replace-chars-in-string
1409 (gnus-group-real-name (gnus-group-decoded-name group))
1412 (if (or nnmail-use-long-file-names
1413 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1415 (mm-encode-coding-string
1416 (nnheader-replace-chars-in-string group ?. ?/)
1417 nnmail-pathname-coding-system)))
1419 (defun gnus-agent-group-pathname (group)
1420 "Translate GROUP into a file name."
1421 ;; nnagent uses nnmail-group-pathname to read articles while
1422 ;; unplugged. The agent must, therefore, use the same directory
1424 (let ((gnus-command-method (or gnus-command-method
1425 (gnus-find-method-for-group group))))
1426 (nnmail-group-pathname (gnus-group-real-name
1427 (gnus-group-decoded-name group))
1428 (gnus-agent-directory))))
1430 (defun gnus-agent-get-function (method)
1431 (if (gnus-online method)
1436 (defun gnus-agent-covered-methods ()
1437 "Return the subset of methods that are covered by the agent."
1438 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1440 ;;; History functions
1442 (defun gnus-agent-history-buffer ()
1443 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1445 (defun gnus-agent-open-history ()
1447 (push (cons (gnus-agent-method)
1448 (set-buffer (gnus-get-buffer-create
1449 (format " *Gnus agent %s history*"
1450 (gnus-agent-method)))))
1451 gnus-agent-history-buffers)
1452 (set-buffer-multibyte nil) ;; everything is binary
1455 (let ((file (gnus-agent-lib-file "history")))
1456 (when (file-exists-p file)
1457 (nnheader-insert-file-contents file))
1458 (set (make-local-variable 'gnus-agent-file-name) file))))
1460 (defun gnus-agent-close-history ()
1461 (when (gnus-buffer-live-p gnus-agent-current-history)
1462 (kill-buffer gnus-agent-current-history)
1463 (setq gnus-agent-history-buffers
1464 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1465 gnus-agent-history-buffers))))
1471 (defun gnus-agent-fetch-articles (group articles)
1472 "Fetch ARTICLES from GROUP and put them into the Agent."
1474 (gnus-agent-load-alist group)
1475 (let* ((alist gnus-agent-article-alist)
1476 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1477 (selected-sets (list nil))
1478 (current-set-size 0)
1481 ;; Check each article
1482 (while (setq article (pop articles))
1483 ;; Skip alist entries preceeding this article
1484 (while (> article (or (caar alist) (1+ article)))
1485 (setq alist (cdr alist)))
1487 ;; Prune off articles that we have already fetched.
1488 (unless (and (eq article (caar alist))
1490 ;; Skip headers preceeding this article
1493 (let* ((header (car headers)))
1495 (mail-header-number header)
1497 (setq headers (cdr headers)))
1499 ;; Add this article to the current set
1500 (setcar selected-sets (cons article (car selected-sets)))
1502 ;; Update the set size, when the set is too large start a
1503 ;; new one. I do this after adding the article as I want at
1504 ;; least one article in each set.
1505 (when (< gnus-agent-max-fetch-size
1506 (setq current-set-size
1508 (if (= header-number article)
1509 (let ((char-size (mail-header-chars
1511 (if (<= char-size 0)
1512 ;; The char size was missing/invalid,
1513 ;; assume a worst-case situation of
1514 ;; 65 char/line. If the line count
1515 ;; is missing, arbitrarily assume a
1516 ;; size of 1000 characters.
1517 (max (* 65 (mail-header-lines
1522 (setcar selected-sets (nreverse (car selected-sets)))
1523 (setq selected-sets (cons nil selected-sets)
1524 current-set-size 0))))
1526 (when (or (cdr selected-sets) (car selected-sets))
1527 (let* ((fetched-articles (list nil))
1528 (tail-fetched-articles fetched-articles)
1529 (dir (gnus-agent-group-pathname group))
1530 (date (time-to-days (current-time)))
1531 (case-fold-search t)
1534 (setcar selected-sets (nreverse (car selected-sets)))
1535 (setq selected-sets (nreverse selected-sets))
1537 (gnus-make-directory dir)
1538 (gnus-message 7 "Fetching articles for %s..." group)
1541 (while (setq articles (pop selected-sets))
1542 ;; Fetch the articles from the backend.
1543 (if (gnus-check-backend-function 'retrieve-articles group)
1544 (setq pos (gnus-retrieve-articles articles group))
1547 (while (setq article (pop articles))
1548 (gnus-message 10 "Fetching article %s for %s..."
1551 (gnus-backlog-request-article group article
1553 (gnus-request-article article group))
1554 (goto-char (point-max))
1555 (push (cons article (point)) pos)
1556 (insert-buffer-substring nntp-server-buffer)))
1558 nntp-server-buffer (point-min) (point-max))
1559 (setq pos (nreverse pos)))))
1560 ;; Then save these articles into the Agent.
1562 (set-buffer nntp-server-buffer)
1564 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1565 (goto-char (point-min))
1566 (unless (eobp) ;; Don't save empty articles.
1567 (when (search-forward "\n\n" nil t)
1568 (when (search-backward "\nXrefs: " nil t)
1569 ;; Handle cross posting.
1570 (goto-char (match-end 0)) ; move to end of header name
1571 (skip-chars-forward "^ ") ; skip server name
1572 (skip-chars-forward " ")
1574 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1575 (push (cons (buffer-substring (match-beginning 1)
1578 (buffer-substring (match-beginning 2)
1581 (goto-char (match-end 0)))
1582 (gnus-agent-crosspost crosses (caar pos) date)))
1583 (goto-char (point-min))
1584 (if (not (re-search-forward
1585 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1586 (setq id "No-Message-ID-in-article")
1587 (setq id (buffer-substring
1588 (match-beginning 1) (match-end 1))))
1589 (write-region-as-coding-system
1590 gnus-agent-file-coding-system (point-min) (point-max)
1591 (concat dir (number-to-string (caar pos))) nil 'silent)
1593 (gnus-agent-append-to-list
1594 tail-fetched-articles (caar pos)))
1596 (setq pos (cdr pos)))))
1598 (gnus-agent-save-alist group (cdr fetched-articles) date)
1599 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1601 (gnus-message 7 ""))
1602 (cdr fetched-articles))))))
1604 (defun gnus-agent-unfetch-articles (group articles)
1605 "Delete ARTICLES that were fetched from GROUP into the agent."
1607 (gnus-agent-with-refreshed-group
1609 (gnus-agent-load-alist group)
1610 (let* ((alist (cons nil gnus-agent-article-alist))
1611 (articles (sort articles #'<))
1612 (next-possibility alist)
1613 (delete-this (pop articles)))
1614 (while (and (cdr next-possibility) delete-this)
1615 (let ((have-this (caar (cdr next-possibility))))
1616 (cond ((< delete-this have-this)
1617 (setq delete-this (pop articles)))
1618 ((= delete-this have-this)
1619 (let ((timestamp (cdar (cdr next-possibility))))
1621 (let* ((file-name (concat (gnus-agent-group-pathname group)
1622 (number-to-string have-this)))
1623 (size-file (float (or (and gnus-agent-total-fetched-hashtb
1624 (nth 7 (file-attributes file-name)))
1626 (delete-file file-name)
1627 (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1629 (setcdr next-possibility (cddr next-possibility)))
1631 (setq next-possibility (cdr next-possibility))))))
1632 (setq gnus-agent-article-alist (cdr alist))
1633 (gnus-agent-save-alist group)))))
1635 (defun gnus-agent-crosspost (crosses article &optional date)
1636 (setq date (or date t))
1638 (let (gnus-agent-article-alist group alist beg end)
1640 (set-buffer gnus-agent-overview-buffer)
1641 (when (nnheader-find-nov-line article)
1644 (setq end (progn (forward-line 1) (point)))))
1646 (setq group (caar crosses))
1647 (unless (setq alist (assoc group gnus-agent-group-alist))
1648 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1649 gnus-agent-group-alist))
1650 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1652 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1654 (when (= (point-max) (point-min))
1655 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1657 (nnheader-insert-file-contents
1658 (gnus-agent-article-name ".overview" group))))
1659 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1660 (insert (string-to-number (cdar crosses)))
1661 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1662 (gnus-agent-check-overview-buffer))
1663 (setq crosses (cdr crosses)))))
1665 (defun gnus-agent-backup-overview-buffer ()
1666 (when gnus-newsgroup-name
1667 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1670 (while (file-exists-p
1671 (setq name (concat root "~"
1672 (int-to-string (setq cnt (1+ cnt))) "~"))))
1673 (write-region (point-min) (point-max) name nil 'no-msg)
1674 (gnus-message 1 "Created backup copy of overview in %s." name)))
1677 (defun gnus-agent-check-overview-buffer (&optional buffer)
1678 "Check the overview file given for sanity.
1679 In particular, checks that the file is sorted by article number
1680 and that there are no duplicates."
1685 (set-buffer buffer))
1688 (goto-char (point-min))
1690 (while (< (point) (point-max))
1692 (cur (condition-case nil
1693 (read (current-buffer))
1696 ((or (not (integerp cur))
1697 (not (eq (char-after) ?\t)))
1699 (setq backed-up (gnus-agent-backup-overview-buffer)))
1701 "Overview buffer contains garbage '%s'."
1706 (setq backed-up (gnus-agent-backup-overview-buffer)))
1708 "Duplicate overview line for %d" cur)
1709 (delete-region p (progn (forward-line 1) (point))))
1712 (setq backed-up (gnus-agent-backup-overview-buffer)))
1713 (gnus-message 1 "Overview buffer not sorted!")
1714 (sort-numeric-fields 1 (point-min) (point-max))
1715 (goto-char (point-min))
1718 (setq prev-num cur)))
1719 (forward-line 1)))))))
1721 (defun gnus-agent-flush-cache ()
1723 (while gnus-agent-buffer-alist
1724 (set-buffer (cdar gnus-agent-buffer-alist))
1725 (write-region-as-coding-system
1726 gnus-agent-file-coding-system
1727 (point-min) (point-max)
1728 (gnus-agent-article-name ".overview"
1729 (caar gnus-agent-buffer-alist))
1731 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1732 (while gnus-agent-group-alist
1733 (with-temp-file (gnus-agent-article-name
1734 ".agentview" (caar gnus-agent-group-alist))
1735 (princ (cdar gnus-agent-group-alist))
1737 (princ 1 (current-buffer))
1739 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1742 (defun gnus-agent-find-parameter (group symbol)
1743 "Search for GROUPs SYMBOL in the group's parameters, the group's
1744 topic parameters, the group's category, or the customizable
1745 variables. Returns the first non-nil value found."
1746 (or (gnus-group-find-parameter group symbol t)
1747 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1751 '((agent-short-article . gnus-agent-short-article)
1752 (agent-long-article . gnus-agent-long-article)
1753 (agent-low-score . gnus-agent-low-score)
1754 (agent-high-score . gnus-agent-high-score)
1755 (agent-days-until-old . gnus-agent-expire-days)
1756 (agent-enable-expiration
1757 . gnus-agent-enable-expiration)
1758 (agent-predicate . gnus-agent-predicate)))))))
1760 (defun gnus-agent-fetch-headers (group &optional force)
1761 "Fetch interesting headers into the agent. The group's overview
1762 file will be updated to include the headers while a list of available
1763 article numbers will be returned."
1764 (let* ((fetch-all (and gnus-agent-consider-all-articles
1765 ;; Do not fetch all headers if the predicate
1766 ;; implies that we only consider unread articles.
1767 (not (gnus-predicate-implies-unread
1768 (gnus-agent-find-parameter group
1769 'agent-predicate)))))
1770 (articles (if fetch-all
1771 (gnus-uncompress-range (gnus-active group))
1772 (gnus-list-of-unread-articles group)))
1773 (gnus-decode-encoded-word-function 'identity)
1774 (file (gnus-agent-article-name ".overview" group)))
1775 ;; Check whether the number of articles is not too large.
1776 (when (and (integerp gnus-agent-large-newsgroup)
1777 (> gnus-agent-large-newsgroup 0))
1778 (setq articles (nthcdr (max (- (length articles)
1779 gnus-agent-large-newsgroup)
1783 ;; Add articles with marks to the list of article headers we want to
1784 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1785 ;; mark, but do fetch recent or seen articles if they have other, more
1786 ;; interesting marks. (We have to fetch articles with boring marks
1787 ;; because otherwise the agent will remove their marks.)
1788 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1789 (unless (memq (car arts) '(seen recent killed cache))
1790 (setq articles (gnus-range-add articles (cdr arts)))))
1791 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1793 ;; At this point, I have the list of articles to consider for
1794 ;; fetching. This is the list that I'll return to my caller. Some
1795 ;; of these articles may have already been fetched. That's OK as
1796 ;; the fetch article code will filter those out. Internally, I'll
1797 ;; filter this list to just those articles whose headers need to
1799 (let ((articles articles))
1800 ;; Remove known articles.
1801 (when (and (or gnus-agent-cache
1803 (gnus-agent-load-alist group))
1804 ;; Remove articles marked as downloaded.
1806 ;; I want to fetch all headers in the active range.
1807 ;; Therefore, exclude only those headers that are in the
1809 ;; NOTE: This is probably NOT what I want to do after
1810 ;; agent expiration in this group.
1811 (setq articles (gnus-agent-uncached-articles articles group))
1813 ;; I want to only fetch those headers that have never been
1814 ;; fetched. Therefore, exclude all headers that are, or
1815 ;; WERE, in the article alist.
1816 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1817 (high (cdr (gnus-active group))))
1818 ;; Low can be greater than High when the same group is
1819 ;; fetched twice in the same session {The first fetch will
1820 ;; fill the article alist such that (last
1821 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1822 ;; group))}. The addition of one(the 1+ above) then
1823 ;; forces Low to be greater than High. When this happens,
1824 ;; gnus-list-range-intersection returns nil which
1825 ;; indicates that no headers need to be fetched. -- Kevin
1826 (setq articles (gnus-list-range-intersection
1827 articles (list (cons low high)))))))
1830 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1831 (gnus-compress-sequence articles t))
1834 (set-buffer nntp-server-buffer)
1838 (gnus-message 7 "Fetching headers for %s..." group)
1841 (gnus-make-directory (nnheader-translate-file-chars
1842 (file-name-directory file) t))
1844 (unless (eq 'nov (gnus-retrieve-headers articles group))
1845 (nnvirtual-convert-headers))
1846 (gnus-agent-check-overview-buffer)
1847 ;; Move these headers to the overview buffer so that
1848 ;; gnus-agent-braid-nov can merge them with the contents
1851 gnus-agent-overview-buffer (point-min) (point-max))
1852 ;; NOTE: Call g-a-brand-nov even when the file does not
1853 ;; exist. As a minimum, it will validate the article
1854 ;; numbers already in the buffer.
1855 (gnus-agent-braid-nov group articles file)
1856 (gnus-agent-check-overview-buffer)
1857 (write-region-as-coding-system
1858 gnus-agent-file-coding-system
1859 (point-min) (point-max) file nil 'silent)
1860 (gnus-agent-update-view-total-fetched-for group t)
1861 (gnus-agent-save-alist group articles nil)
1865 (nnheader-insert-file-contents file)))))
1868 (defsubst gnus-agent-read-article-number ()
1869 "Reads the article number at point. Returns nil when a valid article number can not be read."
1871 ;; It is unfortunite but the read function quietly overflows
1872 ;; integer. As a result, I have to use string operations to test
1873 ;; for overflow BEFORE calling read.
1874 (when (looking-at "[0-9]+\t")
1875 (let ((len (- (match-end 0) (match-beginning 0))))
1877 (read (current-buffer)))
1879 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1880 ;; Back convert from int to string to ensure that this is one of them.
1881 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1882 (num (read (current-buffer)))
1883 (str2 (int-to-string num)))
1884 (when (equal str1 str2)
1887 (defsubst gnus-agent-copy-nov-line (article)
1888 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1890 (set-buffer gnus-agent-overview-buffer)
1891 (while (and (not (eobp))
1892 (or (not (setq art (gnus-agent-read-article-number)))
1897 (not (eq article art)))
1898 (set-buffer nntp-server-buffer)
1900 (setq e (progn (forward-line 1) (point)))
1901 (set-buffer nntp-server-buffer)
1902 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1904 (defun gnus-agent-braid-nov (group articles file)
1905 "Merge agent overview data with given file.
1906 Takes unvalidated headers for ARTICLES from
1907 `gnus-agent-overview-buffer' and validated headers from the given
1908 FILE and places the combined valid headers into
1909 `nntp-server-buffer'. This function can be used, when file
1910 doesn't exist, to valid the overview buffer."
1912 (set-buffer gnus-agent-overview-buffer)
1913 (goto-char (point-min))
1914 (set-buffer nntp-server-buffer)
1916 (when (file-exists-p file)
1917 (nnheader-insert-file-contents file))
1918 (goto-char (point-max))
1921 (unless (or (= (point-min) (point-max))
1922 (< (setq last (read (current-buffer))) (car articles)))
1923 ;; Old and new overlap -- We do it the hard way.
1924 (when (nnheader-find-nov-line (car articles))
1925 ;; Replacing existing NOV entry
1926 (delete-region (point) (progn (forward-line 1) (point))))
1927 (gnus-agent-copy-nov-line (pop articles))
1931 (while (let ((art (read (current-buffer))))
1932 (cond ((< art (car articles))
1935 ((= art (car articles))
1938 (point) (progn (forward-line 1) (point)))
1944 (gnus-agent-copy-nov-line (pop articles)))))
1946 (goto-char (point-max))
1948 ;; Append the remaining lines
1951 (set-buffer gnus-agent-overview-buffer)
1952 (setq start (point))
1953 (set-buffer nntp-server-buffer))
1956 (insert-buffer-substring gnus-agent-overview-buffer start)
1959 (setq last (or last -134217728))
1962 (setq art (gnus-agent-read-article-number))
1964 ;; Bad art num - delete this line
1966 (delete-region (point) (progn (forward-line 1) (point))))
1968 ;; Art num out of order - enable sort
1976 (sort-numeric-fields 1 (point-min) (point-max)))))))
1978 ;; Keeps the compiler from warning about the free variable in
1979 ;; gnus-agent-read-agentview.
1981 (defvar gnus-agent-read-agentview))
1983 (defun gnus-agent-load-alist (group)
1984 "Load the article-state alist for GROUP."
1985 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1986 (let ((gnus-agent-read-agentview group))
1987 (setq gnus-agent-article-alist
1988 (gnus-cache-file-contents
1989 (gnus-agent-article-name ".agentview" group)
1990 'gnus-agent-file-loading-cache
1991 'gnus-agent-read-agentview))))
1993 ;; Save format may be either 1 or 2. Two is the new, compressed
1994 ;; format that is still being tested. Format 1 is uncompressed but
1995 ;; known to be reliable.
1996 (defconst gnus-agent-article-alist-save-format 2)
1998 (defun gnus-agent-read-agentview (file)
1999 "Load FILE and do a `read' there."
2003 (nnheader-insert-file-contents file)
2004 (goto-char (point-min))
2005 (let ((alist (read (current-buffer)))
2006 (version (condition-case nil (read (current-buffer))
2012 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version))
2014 (let ((inhibit-quit t)
2016 (gnus-agent-open-history)
2017 (set-buffer (gnus-agent-history-buffer))
2018 (goto-char (point-min))
2020 (if (and (looking-at
2021 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2022 (string= (match-string 2)
2023 gnus-agent-read-agentview)
2024 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2025 (setcdr entry (string-to-number (match-string 1))))
2027 (gnus-agent-close-history)
2028 (setq changed-version t)))
2030 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2035 (let ((state (car comp-list))
2037 (gnus-uncompress-range
2039 (mapcar (lambda (article-id)
2040 (setq uncomp (cons (cons article-id state) uncomp)))
2043 (setq alist (sort uncomp 'car-less-than-car)))))
2044 (when changed-version
2045 (let ((gnus-agent-article-alist alist))
2046 (gnus-agent-save-alist gnus-agent-read-agentview)))
2050 (defun gnus-agent-save-alist (group &optional articles state)
2051 "Save the article-state alist for GROUP."
2052 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2053 (prev (cons nil gnus-agent-article-alist))
2055 print-level print-length item article)
2056 (while (setq article (pop articles))
2057 (while (and (cdr prev)
2058 (< (caadr prev) article))
2059 (setq prev (cdr prev)))
2062 (setcdr prev (list (cons article state))))
2063 ((> (caadr prev) article)
2064 (setcdr prev (cons (cons article state) (cdr prev))))
2065 ((= (caadr prev) article)
2066 (setcdr (cadr prev) state)))
2067 (setq prev (cdr prev)))
2068 (setq gnus-agent-article-alist (cdr all))
2070 (gnus-agent-set-local group
2071 (caar gnus-agent-article-alist)
2072 (caar (last gnus-agent-article-alist)))
2074 (gnus-make-directory (gnus-agent-article-name "" group))
2075 (with-temp-file (gnus-agent-article-name ".agentview" group)
2076 (cond ((eq gnus-agent-article-alist-save-format 1)
2077 (princ gnus-agent-article-alist (current-buffer)))
2078 ((eq gnus-agent-article-alist-save-format 2)
2079 (let ((compressed nil))
2080 (mapcar (lambda (pair)
2081 (let* ((article-id (car pair))
2082 (day-of-download (cdr pair))
2083 (comp-list (assq day-of-download compressed)))
2086 (cons article-id (cdr comp-list)))
2088 (cons (list day-of-download article-id)
2090 nil)) gnus-agent-article-alist)
2091 (mapcar (lambda (comp-list)
2093 (gnus-compress-sequence
2094 (nreverse (cdr comp-list)))))
2096 (princ compressed (current-buffer)))))
2098 (princ gnus-agent-article-alist-save-format (current-buffer))
2101 (gnus-agent-update-view-total-fetched-for group nil)))
2103 (defvar gnus-agent-article-local nil)
2104 (defvar gnus-agent-file-loading-local nil)
2106 (defun gnus-agent-load-local (&optional method)
2107 "Load the METHOD'S local file. The local file contains min/max
2108 article counts for each of the method's subscribed groups."
2109 (let ((gnus-command-method (or method gnus-command-method)))
2110 (setq gnus-agent-article-local
2111 (gnus-cache-file-contents
2112 (gnus-agent-lib-file "local")
2113 'gnus-agent-file-loading-local
2114 'gnus-agent-read-and-cache-local))))
2116 (defun gnus-agent-read-and-cache-local (file)
2117 "Load and read FILE then bind its contents to
2118 gnus-agent-article-local. If that variable had `dirty' (also known as
2119 modified) original contents, they are first saved to their own file."
2121 (if (and gnus-agent-article-local
2122 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2123 (gnus-agent-save-local))
2124 (gnus-agent-read-local file))
2126 (defun gnus-agent-read-local (file)
2127 "Load FILE and do a `read' there."
2128 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2133 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2134 (nnheader-insert-file-contents file))
2137 (goto-char (point-min))
2138 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2139 (while (= (following-char) ?\;)
2141 (setq line (1+ line)))
2148 (cur (current-buffer)))
2149 (setq group (read cur)
2153 (when (stringp group)
2154 (setq group (intern group my-obarray)))
2156 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2157 (set group (cons (+ 0 min) (+ 0 max))))
2159 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2160 file line (error-message-string err))))
2162 (setq line (1+ line))))
2164 (set (intern "+dirty" my-obarray) nil)
2165 (set (intern "+method" my-obarray) gnus-command-method)
2168 (defun gnus-agent-save-local (&optional force)
2169 "Save gnus-agent-article-local under it method's agent.lib directory."
2170 (let ((my-obarray gnus-agent-article-local))
2171 (when (and my-obarray
2172 (or force (symbol-value (intern "+dirty" my-obarray))))
2173 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2174 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2175 (dest (gnus-agent-lib-file "local")))
2176 (gnus-make-directory (gnus-agent-lib-file ""))
2178 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2179 (with-temp-file dest
2180 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2181 (file-name-coding-system nnmail-pathname-coding-system)
2182 print-level print-length item article
2183 (standard-output (current-buffer)))
2184 (mapatoms (lambda (symbol)
2185 (cond ((not (boundp symbol))
2187 ((member (symbol-name symbol) '("+dirty" "+method"))
2191 (let ((range (symbol-value symbol)))
2199 (defun gnus-agent-get-local (group &optional gmane method)
2200 (let* ((gmane (or gmane (gnus-group-real-name group)))
2201 (gnus-command-method (or method (gnus-find-method-for-group group)))
2202 (local (gnus-agent-load-local))
2203 (symb (intern gmane local))
2204 (minmax (and (boundp symb) (symbol-value symb))))
2206 ;; Bind these so that gnus-agent-load-alist doesn't change the
2207 ;; current alist (i.e. gnus-agent-article-alist)
2208 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2209 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2210 (alist (gnus-agent-load-alist group)))
2214 (caar (last alist))))
2215 (gnus-agent-set-local group (car minmax) (cdr minmax)
2216 gmane gnus-command-method local))))
2219 (defun gnus-agent-set-local (group min max &optional gmane method local)
2220 (let* ((gmane (or gmane (gnus-group-real-name group)))
2221 (gnus-command-method (or method (gnus-find-method-for-group group)))
2222 (local (or local (gnus-agent-load-local)))
2223 (symb (intern gmane local))
2224 (minmax (and (boundp symb) (symbol-value symb))))
2226 (if (cond ((and minmax
2227 (or (not (eq min (car minmax)))
2228 (not (eq max (cdr minmax)))))
2235 (set symb (cons min max))
2238 (unintern symb local)))
2239 (set (intern "+dirty" local) t))))
2241 (defun gnus-agent-article-name (article group)
2242 (expand-file-name article
2243 (file-name-as-directory
2244 (gnus-agent-group-pathname group))))
2246 (defun gnus-agent-batch-confirmation (msg)
2247 "Show error message and return t."
2248 (gnus-message 1 msg)
2252 (defun gnus-agent-batch-fetch ()
2253 "Start Gnus and fetch session."
2256 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2257 (gnus-agent-fetch-session))
2260 (defun gnus-agent-fetch-session ()
2261 "Fetch all articles and headers that are eligible for fetching."
2263 (unless gnus-agent-covered-methods
2264 (error "No servers are covered by the Gnus agent"))
2265 (unless gnus-plugged
2266 (error "Can't fetch articles while Gnus is unplugged"))
2267 (let ((methods (gnus-agent-covered-methods))
2268 groups group gnus-command-method)
2271 (setq gnus-command-method (car methods))
2272 (when (and (or (gnus-server-opened gnus-command-method)
2273 (gnus-open-server gnus-command-method))
2274 (gnus-online gnus-command-method))
2275 (setq groups (gnus-groups-from-server (car methods)))
2276 (gnus-agent-with-fetch
2277 (while (setq group (pop groups))
2278 (when (<= (gnus-group-level group)
2279 gnus-agent-handle-level)
2280 (if (or debug-on-error debug-on-quit)
2281 (gnus-agent-fetch-group-1
2282 group gnus-command-method)
2284 (gnus-agent-fetch-group-1
2285 group gnus-command-method)
2287 (unless (funcall gnus-agent-confirmation-function
2288 (format "Error %s while fetching session. Should gnus continue? "
2289 (error-message-string err)))
2290 (error "Cannot fetch articles into the Gnus agent")))
2292 (gnus-agent-regenerate-group group)
2293 (unless (funcall gnus-agent-confirmation-function
2295 "%s while fetching session. Should gnus continue? "
2296 (error-message-string err)))
2298 "Cannot fetch articles into the Gnus agent")))))))))
2299 (setq methods (cdr methods)))
2300 (gnus-run-hooks 'gnus-agent-fetched-hook)
2301 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2303 (defun gnus-agent-fetch-group-1 (group method)
2305 (let ((gnus-command-method method)
2306 (gnus-newsgroup-name group)
2307 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2308 (gnus-newsgroup-headers gnus-newsgroup-headers)
2309 (gnus-newsgroup-scored gnus-newsgroup-scored)
2310 (gnus-use-cache gnus-use-cache)
2311 (gnus-summary-expunge-below gnus-summary-expunge-below)
2312 (gnus-summary-mark-below gnus-summary-mark-below)
2313 (gnus-orphan-score gnus-orphan-score)
2314 ;; Maybe some other gnus-summary local variables should also
2320 category predicate info marks score-param
2322 (unless (gnus-check-group group)
2323 (error "Can't open server for %s" group))
2326 (when (or gnus-newsgroup-active
2328 (gnus-activate-group group))
2329 (let ((marked-articles gnus-newsgroup-downloadable))
2330 ;; Identify the articles marked for download
2331 (unless gnus-newsgroup-active
2332 ;; The variable gnus-newsgroup-active was selected as I need
2333 ;; a gnus-summary local variable that is NOT bound to any
2334 ;; value (its global value should default to nil).
2335 (dolist (mark gnus-agent-download-marks)
2336 (let ((arts (cdr (assq mark (gnus-info-marks
2337 (setq info (gnus-get-info group)))))))
2339 (setq marked-articles (nconc (gnus-uncompress-range arts)
2342 (setq marked-articles (sort marked-articles '<))
2344 ;; Fetch any new articles from the server
2345 (setq articles (gnus-agent-fetch-headers group))
2347 ;; Merge new articles with marked
2348 (setq articles (sort (append marked-articles articles) '<))
2351 ;; Parse them and see which articles we want to fetch.
2352 (setq gnus-newsgroup-dependencies
2353 (or gnus-newsgroup-dependencies
2354 (make-vector (length articles) 0)))
2355 (setq gnus-newsgroup-headers
2356 (or gnus-newsgroup-headers
2357 (gnus-get-newsgroup-headers-xover articles nil nil
2359 ;; `gnus-agent-overview-buffer' may be killed for
2360 ;; timeout reason. If so, recreate it.
2361 (gnus-agent-create-buffer)
2363 ;; Figure out how to select articles in this group
2364 (setq category (gnus-group-category group))
2368 (gnus-agent-find-parameter group 'agent-predicate)))
2370 ;; If the selection predicate requires scoring, score each header
2371 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2373 (gnus-agent-find-parameter group 'agent-score-file)))
2374 ;; Translate score-param into real one
2377 ((eq score-param 'file)
2378 (setq score-param (gnus-all-score-files group)))
2379 ((stringp (car score-param)))
2381 (setq score-param (list (list score-param)))))
2383 (gnus-score-headers score-param))))
2385 (unless (and (eq predicate 'gnus-agent-false)
2386 (not marked-articles))
2387 (let ((arts (list nil)))
2388 (let ((arts-tail arts)
2389 (alist (gnus-agent-load-alist group))
2390 (marked-articles marked-articles)
2391 (gnus-newsgroup-headers gnus-newsgroup-headers))
2392 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2393 (let ((num (mail-header-number gnus-headers)))
2394 ;; Determine if this article is already in the cache
2396 (> num (caar alist)))
2397 (setq alist (cdr alist)))
2399 (unless (and (eq num (caar alist))
2402 ;; Determine if this article was marked for download.
2403 (while (and marked-articles
2404 (> num (car marked-articles)))
2405 (setq marked-articles
2406 (cdr marked-articles)))
2408 ;; When this article is marked, or selected by the
2409 ;; predicate, add it to the download list
2410 (when (or (eq num (car marked-articles))
2413 (assq num gnus-newsgroup-scored))
2414 gnus-summary-default-score))
2415 (gnus-agent-long-article
2416 (gnus-agent-find-parameter
2417 group 'agent-long-article))
2418 (gnus-agent-short-article
2419 (gnus-agent-find-parameter
2420 group 'agent-short-article))
2421 (gnus-agent-low-score
2422 (gnus-agent-find-parameter
2423 group 'agent-low-score))
2424 (gnus-agent-high-score
2425 (gnus-agent-find-parameter
2426 group 'agent-high-score))
2427 (gnus-agent-expire-days
2428 (gnus-agent-find-parameter
2429 group 'agent-days-until-old)))
2430 (funcall predicate)))
2431 (gnus-agent-append-to-list arts-tail num))))))
2433 (let (fetched-articles)
2434 ;; Fetch all selected articles
2435 (setq gnus-newsgroup-undownloaded
2436 (gnus-sorted-ndifference
2437 gnus-newsgroup-undownloaded
2438 (setq fetched-articles
2440 (gnus-agent-fetch-articles group (cdr arts))
2443 (let ((unfetched-articles
2444 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2445 (if gnus-newsgroup-active
2446 ;; Update the summary buffer
2448 (dolist (article marked-articles)
2449 (gnus-summary-set-agent-mark article t))
2450 (dolist (article fetched-articles)
2451 (when gnus-agent-mark-unread-after-downloaded
2452 (setq gnus-newsgroup-downloadable
2453 (delq article gnus-newsgroup-downloadable))
2454 (gnus-summary-mark-article
2455 article gnus-unread-mark))
2456 (when (gnus-summary-goto-subject article nil t)
2457 (gnus-summary-update-download-mark article)))
2458 (dolist (article unfetched-articles)
2459 (gnus-summary-mark-article
2460 article gnus-canceled-mark)))
2462 ;; Update the group buffer.
2464 ;; When some, or all, of the marked articles came
2465 ;; from the download mark. Remove that mark. I
2466 ;; didn't do this earlier as I only want to remove
2467 ;; the marks after the fetch is completed.
2469 (dolist (mark gnus-agent-download-marks)
2470 (when (eq mark 'download)
2472 (assq mark (gnus-info-marks
2473 (setq info (gnus-get-info group))))))
2474 (when (cdr marked-arts)
2476 (delq marked-arts (gnus-info-marks info)))
2477 (gnus-info-set-marks info marks)))))
2478 (let ((read (gnus-info-read
2479 (or info (setq info (gnus-get-info group))))))
2481 info (gnus-add-to-range read unfetched-articles)))
2483 (gnus-group-update-group group t)
2487 (concat "(gnus-group-set-info '"
2488 (gnus-prin1-to-string info)
2492 ;;; Agent Category Mode
2495 (defvar gnus-category-mode-hook nil
2496 "Hook run in `gnus-category-mode' buffers.")
2498 (defvar gnus-category-line-format " %(%20c%): %g\n"
2499 "Format of category lines.
2501 Valid specifiers include:
2502 %c Topic name (string)
2503 %g The number of groups in the topic (integer)
2505 General format specifiers can also be used. See Info node
2506 `(gnus)Formatting Variables'.")
2508 (defvar gnus-category-mode-line-format "Gnus: %%b"
2509 "The format specification for the category mode line.")
2511 (defvar gnus-agent-predicate 'false
2512 "The selection predicate used when no other source is available.")
2514 (defvar gnus-agent-short-article 100
2515 "Articles that have fewer lines than this are short.")
2517 (defvar gnus-agent-long-article 200
2518 "Articles that have more lines than this are long.")
2520 (defvar gnus-agent-low-score 0
2521 "Articles that have a score lower than this have a low score.")
2523 (defvar gnus-agent-high-score 0
2524 "Articles that have a score higher than this have a high score.")
2527 ;;; Internal variables.
2529 (defvar gnus-category-buffer "*Agent Category*")
2531 (defvar gnus-category-line-format-alist
2532 `((?c gnus-tmp-name ?s)
2533 (?g gnus-tmp-groups ?d)))
2535 (defvar gnus-category-mode-line-format-alist
2536 `((?u user-defined ?s)))
2538 (defvar gnus-category-line-format-spec nil)
2539 (defvar gnus-category-mode-line-format-spec nil)
2541 (defvar gnus-category-mode-map nil)
2542 (put 'gnus-category-mode 'mode-class 'special)
2544 (unless gnus-category-mode-map
2545 (setq gnus-category-mode-map (make-sparse-keymap))
2546 (suppress-keymap gnus-category-mode-map)
2548 (gnus-define-keys gnus-category-mode-map
2549 "q" gnus-category-exit
2550 "k" gnus-category-kill
2551 "c" gnus-category-copy
2552 "a" gnus-category-add
2553 "e" gnus-agent-customize-category
2554 "p" gnus-category-edit-predicate
2555 "g" gnus-category-edit-groups
2556 "s" gnus-category-edit-score
2557 "l" gnus-category-list
2559 "\C-c\C-i" gnus-info-find-node
2560 "\C-c\C-b" gnus-bug))
2562 (defvar gnus-category-menu-hook nil
2563 "*Hook run after the creation of the menu.")
2565 (defun gnus-category-make-menu-bar ()
2566 (gnus-turn-off-edit-menu 'category)
2567 (unless (boundp 'gnus-category-menu)
2569 gnus-category-menu gnus-category-mode-map ""
2571 ["Add" gnus-category-add t]
2572 ["Kill" gnus-category-kill t]
2573 ["Copy" gnus-category-copy t]
2574 ["Edit category" gnus-agent-customize-category t]
2575 ["Edit predicate" gnus-category-edit-predicate t]
2576 ["Edit score" gnus-category-edit-score t]
2577 ["Edit groups" gnus-category-edit-groups t]
2578 ["Exit" gnus-category-exit t]))
2580 (gnus-run-hooks 'gnus-category-menu-hook)))
2582 (defun gnus-category-mode ()
2583 "Major mode for listing and editing agent categories.
2585 All normal editing commands are switched off.
2586 \\<gnus-category-mode-map>
2587 For more in-depth information on this mode, read the manual
2588 \(`\\[gnus-info-find-node]').
2590 The following commands are available:
2592 \\{gnus-category-mode-map}"
2594 (when (gnus-visual-p 'category-menu 'menu)
2595 (gnus-category-make-menu-bar))
2596 (kill-all-local-variables)
2597 (gnus-simplify-mode-line)
2598 (setq major-mode 'gnus-category-mode)
2599 (setq mode-name "Category")
2600 (gnus-set-default-directory)
2601 (setq mode-line-process nil)
2602 (use-local-map gnus-category-mode-map)
2603 (buffer-disable-undo)
2604 (setq truncate-lines t)
2605 (setq buffer-read-only t)
2606 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2608 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2610 (defun gnus-category-insert-line (category)
2611 (let* ((gnus-tmp-name (format "%s" (car category)))
2612 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2614 (gnus-add-text-properties
2618 (eval gnus-category-line-format-spec))
2619 (list 'gnus-category gnus-tmp-name))))
2621 (defun gnus-enter-category-buffer ()
2622 "Go to the Category buffer."
2624 (gnus-category-setup-buffer)
2625 (gnus-configure-windows 'category)
2626 (gnus-category-prepare))
2628 (defun gnus-category-setup-buffer ()
2629 (unless (get-buffer gnus-category-buffer)
2631 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2632 (gnus-category-mode))))
2634 (defun gnus-category-prepare ()
2635 (gnus-set-format 'category-mode)
2636 (gnus-set-format 'category t)
2637 (let ((alist gnus-category-alist)
2638 (buffer-read-only nil))
2641 (gnus-category-insert-line (pop alist)))
2642 (goto-char (point-min))
2643 (gnus-category-position-point)))
2645 (defun gnus-category-name ()
2646 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2647 (error "No category on the current line")))
2649 (defun gnus-category-read ()
2650 "Read the category alist."
2651 (setq gnus-category-alist
2655 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2656 (goto-char (point-min))
2657 ;; This code isn't temp, it will be needed so long as
2658 ;; anyone may be migrating from an older version.
2660 ;; Once we're certain that people will not revert to an
2661 ;; earlier version, we can take out the old-list code in
2662 ;; gnus-category-write.
2663 (let* ((old-list (read (current-buffer)))
2664 (new-list (ignore-errors (read (current-buffer)))))
2667 ;; Convert from a positional list to an alist.
2677 '(agent-predicate agent-score-file agent-groups))))
2680 (list (gnus-agent-cat-make 'default 'short)))))
2682 (defun gnus-category-write ()
2683 "Write the category alist."
2684 (setq gnus-category-predicate-cache nil
2685 gnus-category-group-cache nil)
2686 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2687 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2688 ;; This prin1 is temporary. It exists so that people can revert
2689 ;; to an earlier version of gnus-agent.
2690 (prin1 (mapcar (lambda (c)
2692 (cdr (assoc 'agent-predicate c))
2693 (cdr (assoc 'agent-score-file c))
2694 (cdr (assoc 'agent-groups c))))
2695 gnus-category-alist)
2698 (prin1 gnus-category-alist (current-buffer))))
2700 (defun gnus-category-edit-predicate (category)
2701 "Edit the predicate for CATEGORY."
2702 (interactive (list (gnus-category-name)))
2703 (let ((info (assq category gnus-category-alist)))
2705 (gnus-agent-cat-predicate info)
2706 (format "Editing the select predicate for category %s" category)
2707 `(lambda (predicate)
2708 ;; Avoid run-time execution of setf form
2709 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2711 ;; use its expansion instead:
2712 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2713 'agent-predicate predicate)
2715 (gnus-category-write)
2716 (gnus-category-list)))))
2718 (defun gnus-category-edit-score (category)
2719 "Edit the score expression for CATEGORY."
2720 (interactive (list (gnus-category-name)))
2721 (let ((info (assq category gnus-category-alist)))
2723 (gnus-agent-cat-score-file info)
2724 (format "Editing the score expression for category %s" category)
2725 `(lambda (score-file)
2726 ;; Avoid run-time execution of setf form
2727 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2729 ;; use its expansion instead:
2730 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2731 'agent-score-file score-file)
2733 (gnus-category-write)
2734 (gnus-category-list)))))
2736 (defun gnus-category-edit-groups (category)
2737 "Edit the group list for CATEGORY."
2738 (interactive (list (gnus-category-name)))
2739 (let ((info (assq category gnus-category-alist)))
2741 (gnus-agent-cat-groups info)
2742 (format "Editing the group list for category %s" category)
2744 ;; Avoid run-time execution of setf form
2745 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2747 ;; use its expansion instead:
2748 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2751 (gnus-category-write)
2752 (gnus-category-list)))))
2754 (defun gnus-category-kill (category)
2755 "Kill the current category."
2756 (interactive (list (gnus-category-name)))
2757 (let ((info (assq category gnus-category-alist))
2758 (buffer-read-only nil))
2760 (setq gnus-category-alist (delq info gnus-category-alist))
2761 (gnus-category-write)))
2763 (defun gnus-category-copy (category to)
2764 "Copy the current category."
2765 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2766 (let ((info (assq category gnus-category-alist)))
2767 (push (let ((newcat (gnus-copy-sequence info)))
2768 (setf (gnus-agent-cat-name newcat) to)
2769 (setf (gnus-agent-cat-groups newcat) nil)
2771 gnus-category-alist)
2772 (gnus-category-write)
2773 (gnus-category-list)))
2775 (defun gnus-category-add (category)
2776 "Create a new category."
2777 (interactive "SCategory name: ")
2778 (when (assq category gnus-category-alist)
2779 (error "Category %s already exists" category))
2780 (push (gnus-agent-cat-make category)
2781 gnus-category-alist)
2782 (gnus-category-write)
2783 (gnus-category-list))
2785 (defun gnus-category-list ()
2786 "List all categories."
2788 (gnus-category-prepare))
2790 (defun gnus-category-exit ()
2791 "Return to the group buffer."
2793 (kill-buffer (current-buffer))
2794 (gnus-configure-windows 'group t))
2796 ;; To avoid having 8-bit characters in the source file.
2797 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2799 (defvar gnus-category-predicate-alist
2800 '((spam . gnus-agent-spam-p)
2801 (short . gnus-agent-short-p)
2802 (long . gnus-agent-long-p)
2803 (low . gnus-agent-low-scored-p)
2804 (high . gnus-agent-high-scored-p)
2805 (read . gnus-agent-read-p)
2806 (true . gnus-agent-true)
2807 (false . gnus-agent-false))
2808 "Mapping from short score predicate symbols to predicate functions.")
2810 (defun gnus-agent-spam-p ()
2811 "Say whether an article is spam or not."
2812 (unless gnus-agent-spam-hashtb
2813 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2814 (if (not (equal (mail-header-references gnus-headers) ""))
2816 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2818 (gnus-gethash string gnus-agent-spam-hashtb)
2819 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2821 (defun gnus-agent-short-p ()
2822 "Say whether an article is short or not."
2823 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2825 (defun gnus-agent-long-p ()
2826 "Say whether an article is long or not."
2827 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2829 (defun gnus-agent-low-scored-p ()
2830 "Say whether an article has a low score or not."
2831 (< gnus-score gnus-agent-low-score))
2833 (defun gnus-agent-high-scored-p ()
2834 "Say whether an article has a high score or not."
2835 (> gnus-score gnus-agent-high-score))
2837 (defun gnus-agent-read-p ()
2838 "Say whether an article is read or not."
2839 (gnus-member-of-range (mail-header-number gnus-headers)
2840 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2842 (defun gnus-category-make-function (predicate)
2843 "Make a function from PREDICATE."
2844 (let ((func (gnus-category-make-function-1 predicate)))
2845 (if (and (= (length func) 1)
2846 (symbolp (car func)))
2848 (gnus-byte-compile `(lambda () ,func)))))
2850 (defun gnus-agent-true ()
2854 (defun gnus-agent-false ()
2858 (defun gnus-category-make-function-1 (predicate)
2859 "Make a function from PREDICATE."
2861 ;; Functions are just returned as is.
2862 ((or (symbolp predicate)
2863 (functionp predicate))
2864 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2866 ;; More complex predicate.
2869 ((memq (car predicate) '(& and))
2871 ((memq (car predicate) '(| or))
2873 ((memq (car predicate) gnus-category-not)
2875 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2877 (error "Unknown predicate type: %s" predicate))))
2879 (defun gnus-get-predicate (predicate)
2880 "Return the function implementing PREDICATE."
2881 (or (cdr (assoc predicate gnus-category-predicate-cache))
2882 (let ((func (gnus-category-make-function predicate)))
2883 (setq gnus-category-predicate-cache
2884 (nconc gnus-category-predicate-cache
2885 (list (cons predicate func))))
2888 (defun gnus-predicate-implies-unread (predicate)
2889 "Say whether PREDICATE implies unread articles only.
2890 It is okay to miss some cases, but there must be no false positives.
2891 That is, if this predicate returns true, then indeed the predicate must
2892 return only unread articles."
2893 (eq t (gnus-function-implies-unread-1
2894 (gnus-category-make-function-1 predicate))))
2896 (defun gnus-function-implies-unread-1 (function)
2897 "Recursively evaluate a predicate function to determine whether it can select
2898 any read articles. Returns t if the function is known to never
2899 return read articles, nil when it is known to always return read
2900 articles, and t_nil when the function may return both read and unread
2902 (let ((func (car function))
2903 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2904 (cond ((eq func 'and)
2905 (cond ((memq t args) ; if any argument returns only unread articles
2906 ;; then that argument constrains the result to only unread articles.
2908 ((memq 't_nil args) ; if any argument is indeterminate
2909 ;; then the result is indeterminate
2912 (cond ((memq nil args) ; if any argument returns read articles
2913 ;; then that argument ensures that the results includes read articles.
2915 ((memq 't_nil args) ; if any argument is indeterminate
2916 ;; then that argument ensures that the results are indeterminate
2918 (t ; if all arguments return only unread articles
2919 ;; then the result returns only unread articles
2922 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2923 ; then the result is indeterminate
2926 ; toggle the result to be the opposite of the argument
2928 ((eq func 'gnus-agent-read-p)
2929 nil) ; The read predicate NEVER returns unread articles
2930 ((eq func 'gnus-agent-false)
2931 t) ; The false predicate returns t as the empty set excludes all read articles
2932 ((eq func 'gnus-agent-true)
2933 nil) ; The true predicate ALWAYS returns read articles
2934 ((catch 'found-match
2935 (let ((alist gnus-category-predicate-alist))
2937 (if (eq func (cdar alist))
2938 (throw 'found-match t)
2939 (setq alist (cdr alist))))))
2940 't_nil) ; All other predicates return read and unread articles
2942 (error "Unknown predicate function: %s" function)))))
2944 (defun gnus-group-category (group)
2945 "Return the category GROUP belongs to."
2946 (unless gnus-category-group-cache
2947 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2948 (let ((cs gnus-category-alist)
2950 (while (setq cat (pop cs))
2951 (setq groups (gnus-agent-cat-groups cat))
2953 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2954 (or (gnus-gethash group gnus-category-group-cache)
2955 (assq 'default gnus-category-alist)))
2957 (defun gnus-agent-expire-group (group &optional articles force)
2958 "Expire all old articles in GROUP.
2959 If you want to force expiring of certain articles, this function can
2960 take ARTICLES, and FORCE parameters as well.
2962 The articles on which the expiration process runs are selected as follows:
2963 if ARTICLES is null, all read and unmarked articles.
2964 if ARTICLES is t, all articles.
2965 if ARTICLES is a list, just those articles.
2966 FORCE is equivalent to setting the expiration predicates to true."
2968 (list (let ((def (or (gnus-group-group-name)
2969 gnus-newsgroup-name)))
2970 (let ((select (read-string (if def
2971 (concat "Group Name ("
2974 (if (and (equal "" select)
2980 (gnus-agent-expire articles group force)
2981 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2982 ;; expiration statistics of this single group
2983 (gnus-agent-expire-stats (list 0 0 0.0)))
2984 (if (or (not (eq articles t))
2986 (concat "Are you sure that you want to "
2987 "expire all articles in " group ".")))
2988 (let ((gnus-command-method (gnus-find-method-for-group group))
2989 (overview (gnus-get-buffer-create " *expire overview*"))
2992 (let ((active-file (gnus-agent-lib-file "active")))
2993 (when (file-exists-p active-file)
2995 (nnheader-insert-file-contents active-file)
2996 (gnus-active-to-gnus-format
2998 (setq orig (gnus-make-hashtable
2999 (count-lines (point-min) (point-max))))))
3001 (gnus-agent-expire-group-1
3002 group overview (gnus-gethash-safe group orig)
3004 (kill-buffer overview))))
3005 (gnus-message 4 (gnus-agent-expire-done-message)))))
3007 (defun gnus-agent-expire-group-1 (group overview active articles force)
3008 ;; Internal function - requires caller to have set
3009 ;; gnus-command-method, initialized overview buffer, and to have
3010 ;; provided a non-nil active
3012 (let ((dir (gnus-agent-group-pathname group)))
3013 (gnus-agent-with-refreshed-group
3015 (when (boundp 'gnus-agent-expire-current-dirs)
3016 (set 'gnus-agent-expire-current-dirs
3018 (symbol-value 'gnus-agent-expire-current-dirs))))
3020 (if (and (not force)
3021 (eq 'DISABLE (gnus-agent-find-parameter group
3022 'agent-enable-expiration)))
3023 (gnus-message 5 "Expiry skipping over %s" group)
3024 (gnus-message 5 "Expiring articles in %s" group)
3025 (gnus-agent-load-alist group)
3026 (let* ((bytes-freed 0)
3027 (size-files-deleted 0.0)
3029 (nov-entries-deleted 0)
3030 (info (gnus-get-info group))
3031 (alist gnus-agent-article-alist)
3032 (day (- (time-to-days (current-time))
3033 (gnus-agent-find-parameter group 'agent-days-until-old)))
3034 (specials (if (and alist
3036 ;; This could be a bit of a problem. I need to
3037 ;; keep the last article to avoid refetching
3038 ;; headers when using nntp in the backend. At
3039 ;; the same time, if someone uses a backend
3040 ;; that supports article moving then I may have
3041 ;; to remove the last article to complete the
3042 ;; move. Right now, I'm going to assume that
3043 ;; FORCE overrides specials.
3044 (list (caar (last alist)))))
3045 (unreads ;; Articles that are excluded from the
3046 ;; expiration process
3047 (cond (gnus-agent-expire-all
3048 ;; All articles are marked read by global decree
3051 ;; All articles are marked read by function
3055 ;; Unread articles are marked protected from
3056 ;; expiration Don't call
3057 ;; gnus-list-of-unread-articles as it returns
3058 ;; articles that have not been fetched into the
3061 (gnus-agent-unread-articles group)))
3063 ;; All articles EXCEPT those named by the caller
3064 ;; are protected from expiration
3065 (gnus-sorted-difference
3066 (gnus-uncompress-range
3068 (caar (last alist))))
3069 (sort articles '<)))))
3070 (marked ;; More articles that are excluded from the
3071 ;; expiration process
3072 (cond (gnus-agent-expire-all
3073 ;; All articles are unmarked by global decree
3076 ;; All articles are unmarked by function
3080 ;; All articles may as well be unmarked as the
3081 ;; unreads list already names the articles we are
3085 ;; Ticked and/or dormant articles are excluded
3088 (gnus-uncompress-range
3089 (cdr (assq 'tick (gnus-info-marks info))))
3090 (gnus-uncompress-range
3092 (gnus-info-marks info))))))))
3093 (nov-file (concat dir ".overview"))
3099 ;; The normal article alist contains elements that look like
3100 ;; (article# . fetch_date) I need to combine other
3101 ;; information with this list. For example, a flag indicating
3102 ;; that a particular article MUST BE KEPT. To do this, I'm
3103 ;; going to transform the elements to look like (article#
3104 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3105 ;; the process to generate the expired article alist.
3107 ;; Convert the alist elements to (article# fetch_date nil
3109 (setq dlist (mapcar (lambda (e)
3110 (list (car e) (cdr e) nil nil)) alist))
3112 ;; Convert the keep lists to elements that look like (article#
3113 ;; nil keep_flag nil) then append it to the expanded dlist
3114 ;; These statements are sorted by ascending precidence of the
3116 (setq dlist (nconc dlist
3118 (list e nil 'unread nil))
3120 (setq dlist (nconc dlist
3122 (list e nil 'marked nil))
3124 (setq dlist (nconc dlist
3126 (list e nil 'special nil))
3129 (set-buffer overview)
3131 (buffer-disable-undo)
3132 (when (file-exists-p nov-file)
3133 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3134 (nnheader-insert-file-contents nov-file)
3135 (goto-char (point-min))
3138 (while (< (setq p (point)) (point-max))
3140 ;; If I successfully read an integer (the plus zero
3141 ;; ensures a numeric type), append the position
3143 (push (list (+ 0 (read (current-buffer))) nil nil
3147 (gnus-message 1 "gnus-agent-expire: read error \
3148 occurred when reading expression at %s in %s. Skipping to next \
3149 line." (point) nov-file)))
3150 ;; Whether I succeeded, or failed, it doesn't matter.
3151 ;; Move to the next line then try again.
3155 7 "gnus-agent-expire: Loading overview... Done"))
3156 (set-buffer-modified-p nil)
3158 ;; At this point, all of the information is in dlist. The
3159 ;; only problem is that much of it is spread across multiple
3160 ;; entries. Sort then MERGE!!
3161 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3162 ;; If two entries have the same article-number then sort by
3163 ;; ascending keep_flag.
3170 (cond ((< (nth 0 a) (nth 0 b))
3172 ((> (nth 0 a) (nth 0 b))
3175 (let ((a (or (symbol-value (nth 2 a))
3177 (b (or (symbol-value (nth 2 b))
3180 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3181 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3182 (let ((dlist dlist))
3183 (while (cdr dlist) ; I'm not at the end-of-list
3184 (if (eq (caar dlist) (caadr dlist))
3185 (let ((first (cdr (car dlist)))
3186 (secnd (cdr (cadr dlist))))
3187 (setcar first (or (car first)
3188 (car secnd))) ; fetch_date
3189 (setq first (cdr first)
3191 (setcar first (or (car first)
3192 (car secnd))) ; Keep_flag
3193 (setq first (cdr first)
3195 (setcar first (or (car first)
3196 (car secnd))) ; NOV_entry_position
3198 (setcdr dlist (cddr dlist)))
3199 (setq dlist (cdr dlist)))))
3201 ;; Check the order of the entry positions. They should be in
3202 ;; ascending order. If they aren't, the positions must be
3203 ;; converted to markers.
3204 (when (let ((dlist dlist)
3208 (if (setq pos (nth 3 (pop dlist)))
3209 (if (< pos prev-pos)
3210 (throw 'sort-results 'unsorted)
3211 (setq prev-pos pos)))))
3212 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3213 (mapcar (lambda (entry)
3214 (let ((pos (nth 3 entry)))
3217 (set-marker (make-marker)
3221 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3223 (let* ((len (float (length dlist)))
3230 (let ((new-completed (truncate (* 100.0
3231 (/ (setq cnt (1+ cnt))
3234 (when (> new-completed completed)
3235 (setq completed new-completed)
3236 (gnus-message 7 "%3d%% completed..." completed)))
3237 (let* ((entry (car dlist))
3238 (article-number (nth 0 entry))
3239 (fetch-date (nth 1 entry))
3240 (keep (nth 2 entry))
3241 (marker (nth 3 entry)))
3244 ;; Kept articles are unread, marked, or special.
3246 (gnus-agent-message 10
3247 "gnus-agent-expire: %s:%d: Kept %s article%s."
3248 group article-number keep (if fetch-date " and file" ""))
3250 (unless (file-exists-p
3251 (concat dir (number-to-string
3253 (setf (nth 1 entry) nil)
3254 (gnus-agent-message 3 "gnus-agent-expire cleared \
3255 download flag on %s:%d as the cached article file is missing."
3256 group (caar dlist)))
3258 (gnus-message 1 "gnus-agent-expire detected a \
3259 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3260 (gnus-agent-append-to-list
3262 (cons article-number fetch-date)))
3264 ;; The following articles are READ, UNMARKED, and
3265 ;; ORDINARY. See if they can be EXPIRED!!!
3268 ((not (integerp fetch-date))
3269 'read) ;; never fetched article (may expire
3271 ((not (file-exists-p
3272 (concat dir (number-to-string
3274 (setf (nth 1 entry) nil)
3275 'externally-expired) ;; Can't find the cached
3276 ;; article. Handle case
3277 ;; as though this article
3278 ;; was never fetched.
3280 ;; We now have the arrival day, so we see
3281 ;; whether it's old enough to be expired.
3287 ;; I found some reason to expire this entry.
3289 (let ((actions nil))
3290 (when (memq type '(forced expired))
3291 (ignore-errors ; Just being paranoid.
3292 (let* ((file-name (nnheader-concat dir (number-to-string
3294 (size (float (nth 7 (file-attributes file-name)))))
3295 (incf bytes-freed size)
3296 (incf size-files-deleted size)
3297 (incf files-deleted)
3298 (delete-file file-name))
3299 (push "expired cached article" actions))
3300 (setf (nth 1 entry) nil)
3304 (push "NOV entry removed" actions)
3306 (goto-char (if (markerp marker)
3308 (- marker position-offset)))
3310 (incf nov-entries-deleted)
3312 (let* ((from (point-at-bol))
3313 (to (progn (forward-line 1) (point)))
3314 (freed (- to from)))
3315 (incf bytes-freed freed)
3316 (incf position-offset freed)
3317 (delete-region from to)))
3319 ;; If considering all articles is set, I can only
3320 ;; expire article IDs that are no longer in the
3321 ;; active range (That is, articles that preceed the
3322 ;; first article in the new alist).
3323 (if (and gnus-agent-consider-all-articles
3324 (>= article-number (car active)))
3325 ;; I have to keep this ID in the alist
3326 (gnus-agent-append-to-list
3327 tail-alist (cons article-number fetch-date))
3328 (push (format "Removed %s article number from \
3329 article alist" type) actions))
3332 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3333 group article-number
3334 (mapconcat 'identity actions ", ")))))
3337 10 "gnus-agent-expire: %s:%d: Article kept as \
3338 expiration tests failed." group article-number)
3339 (gnus-agent-append-to-list
3340 tail-alist (cons article-number fetch-date)))
3343 ;; Remove markers as I intend to reuse this buffer again.
3346 (set-marker marker nil))
3348 (setq dlist (cdr dlist))))
3350 (setq alist (cdr alist))
3352 (let ((inhibit-quit t))
3353 (unless (equal alist gnus-agent-article-alist)
3354 (setq gnus-agent-article-alist alist)
3355 (gnus-agent-save-alist group))
3357 (when (buffer-modified-p)
3358 (gnus-make-directory dir)
3359 (write-region-as-coding-system gnus-agent-file-coding-system
3360 (point-min) (point-max) nov-file
3362 ;; clear the modified flag as that I'm not confused by
3363 ;; its status on the next pass through this routine.
3364 (set-buffer-modified-p nil)
3365 (gnus-agent-update-view-total-fetched-for group t))
3367 (when (eq articles t)
3368 (gnus-summary-update-info))))
3370 (when (boundp 'gnus-agent-expire-stats)
3371 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3372 (incf (nth 2 stats) bytes-freed)
3373 (incf (nth 1 stats) files-deleted)
3374 (incf (nth 0 stats) nov-entries-deleted)))
3376 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3378 (defun gnus-agent-expire (&optional articles group force)
3379 "Expire all old articles.
3380 If you want to force expiring of certain articles, this function can
3381 take ARTICLES, GROUP and FORCE parameters as well.
3383 The articles on which the expiration process runs are selected as follows:
3384 if ARTICLES is null, all read and unmarked articles.
3385 if ARTICLES is t, all articles.
3386 if ARTICLES is a list, just those articles.
3387 Setting GROUP will limit expiration to that group.
3388 FORCE is equivalent to setting the expiration predicates to true."
3392 (gnus-agent-expire-group group articles force)
3393 (if (or (not (eq articles t))
3394 (yes-or-no-p "Are you sure that you want to expire all \
3395 articles in every agentized group."))
3396 (let ((methods (gnus-agent-covered-methods))
3397 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3398 ;; of agent directories.
3399 (gnus-agent-expire-current-dirs nil)
3400 ;; Bind gnus-agent-expire-stats to enable tracking of
3401 ;; expiration statistics across all groups
3402 (gnus-agent-expire-stats (list 0 0 0.0))
3403 gnus-command-method overview orig)
3404 (setq overview (gnus-get-buffer-create " *expire overview*"))
3406 (while (setq gnus-command-method (pop methods))
3407 (let ((active-file (gnus-agent-lib-file "active")))
3408 (when (file-exists-p active-file)
3410 (nnheader-insert-file-contents active-file)
3411 (gnus-active-to-gnus-format
3413 (setq orig (gnus-make-hashtable
3414 (count-lines (point-min) (point-max))))))
3415 (dolist (expiring-group (gnus-groups-from-server
3416 gnus-command-method))
3418 (gnus-gethash-safe expiring-group orig)))
3422 (gnus-agent-expire-group-1
3423 expiring-group overview active articles force))))))))
3424 (kill-buffer overview))
3425 (gnus-agent-expire-unagentized-dirs)
3426 (gnus-message 4 (gnus-agent-expire-done-message))))))
3428 (defun gnus-agent-expire-done-message ()
3429 (if (and (> gnus-verbose 4)
3430 (boundp 'gnus-agent-expire-stats))
3431 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3432 (size (nth 2 stats))
3433 (units '(B KB MB GB)))
3434 (while (and (> size 1024.0)
3436 (setq size (/ size 1024.0)
3439 (format "Expiry recovered %d NOV entries, deleted %d files,\
3446 (defun gnus-agent-expire-unagentized-dirs ()
3447 (when (and gnus-agent-expire-unagentized-dirs
3448 (boundp 'gnus-agent-expire-current-dirs))
3449 (let* ((keep (gnus-make-hashtable))
3450 ;; Formally bind gnus-agent-expire-current-dirs so that the
3451 ;; compiler will not complain about free references.
3452 (gnus-agent-expire-current-dirs
3453 (symbol-value 'gnus-agent-expire-current-dirs))
3456 (gnus-sethash gnus-agent-directory t keep)
3457 (while gnus-agent-expire-current-dirs
3458 (setq dir (pop gnus-agent-expire-current-dirs))
3459 (when (and (stringp dir)
3460 (file-directory-p dir))
3461 (while (not (gnus-gethash dir keep))
3462 (gnus-sethash dir t keep)
3463 (setq dir (file-name-directory (directory-file-name dir))))))
3470 "Given a directory, check it and its subdirectories for
3471 membership in the keep hash. If it isn't found, add
3473 (let ((files (directory-files d))
3475 (while (setq file (pop files))
3476 (cond ((equal file ".") ; Ignore self
3478 ((equal file "..") ; Ignore parent
3480 ((equal file ".overview")
3481 ;; Directory must contain .overview to be
3482 ;; agent's cache of a group.
3483 (let ((d (file-name-as-directory d))
3485 ;; Search ancestor's for last directory NOT
3486 ;; found in keep hash.
3487 (while (not (gnus-gethash
3488 (setq d (file-name-directory d)) keep))
3490 d (directory-file-name d)))
3491 ;; if ANY ancestor was NOT in keep hash and
3492 ;; it it's already in to-remove, add it to
3495 (not (member r to-remove)))
3496 (push r to-remove))))
3497 ((file-directory-p (setq file (nnheader-concat d file)))
3498 (funcall checker file)))))))))
3499 (funcall checker (expand-file-name gnus-agent-directory))
3501 (when (and to-remove
3502 (or gnus-expert-user
3504 "gnus-agent-expire has identified local directories that are\
3505 not currently required by any agentized group. Do you wish to consider\
3508 (let ((dir (pop to-remove)))
3509 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3510 (let* (delete-recursive
3515 (if (file-directory-p f-or-d)
3517 (delete-directory f-or-d)
3520 (or (member f '("." ".."))
3521 (funcall delete-recursive
3524 (directory-files f-or-d))
3525 (delete-directory f-or-d)))
3526 (delete-file f-or-d)))))))
3527 (funcall delete-recursive dir))))))))))
3530 (defun gnus-agent-batch ()
3531 "Start Gnus, send queue and fetch session."
3533 (let ((init-file-user "")
3534 (gnus-always-read-dribble-file t))
3536 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3537 (gnus-group-send-queue)
3538 (gnus-agent-fetch-session)))
3540 (defun gnus-agent-unread-articles (group)
3541 (let* ((read (gnus-info-read (gnus-get-info group)))
3542 (known (gnus-agent-load-alist group))
3544 (tail-unread unread))
3545 (while (and known read)
3546 (let ((candidate (car (pop known))))
3547 (while (let* ((range (car read))
3548 (min (if (numberp range) range (car range)))
3549 (max (if (numberp range) range (cdr range))))
3550 (cond ((or (not min)
3552 (gnus-agent-append-to-list tail-unread candidate)
3555 (setq read (cdr read))
3556 ;; return t so that I always loop one more
3557 ;; time. If I just iterated off the end of
3558 ;; read, min will become nil and the current
3559 ;; candidate will be added to the unread list.
3562 (gnus-agent-append-to-list tail-unread (car (pop known))))
3565 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3566 "Restrict ARTICLES to numbers already fetched.
3567 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3568 that have already been fetched.
3569 If CACHED-HEADER is nil, articles are only excluded if the article itself
3572 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3573 ;; 'car gnus-agent-article-alist))
3575 ;; Functionally, I don't need to construct a temp list using mapcar.
3577 (if (and (or gnus-agent-cache (not gnus-plugged))
3578 (gnus-agent-load-alist group))
3579 (let* ((ref gnus-agent-article-alist)
3581 (uncached (list nil))
3582 (tail-uncached uncached))
3583 (while (and ref arts)
3584 (let ((v1 (car arts))
3586 (cond ((< v1 v2) ; v1 does not appear in the reference list
3587 (gnus-agent-append-to-list tail-uncached v1)
3588 (setq arts (cdr arts)))
3590 (unless (or cached-header (cdar ref)) ; v1 is already cached
3591 (gnus-agent-append-to-list tail-uncached v1))
3592 (setq arts (cdr arts))
3593 (setq ref (cdr ref)))
3594 (t ; reference article (v2) preceeds the list being filtered
3595 (setq ref (cdr ref))))))
3597 (gnus-agent-append-to-list tail-uncached (pop arts)))
3599 ;; if gnus-agent-load-alist fails, no articles are cached.
3602 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3604 (gnus-agent-create-buffer)
3605 (let ((gnus-decode-encoded-word-function 'identity)
3606 (file (gnus-agent-article-name ".overview" group))
3607 cached-articles uncached-articles)
3608 (gnus-make-directory (nnheader-translate-file-chars
3609 (file-name-directory file) t))
3611 ;; Populate temp buffer with known headers
3612 (when (file-exists-p file)
3613 (with-current-buffer gnus-agent-overview-buffer
3615 (let ((nnheader-file-coding-system
3616 gnus-agent-file-coding-system))
3617 (nnheader-insert-nov-file file (car articles)))))
3619 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3622 ;; Populate nntp-server-buffer with uncached headers
3623 (set-buffer nntp-server-buffer)
3625 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3626 (gnus-retrieve-headers
3627 uncached-articles group fetch-old))))
3628 (nnvirtual-convert-headers))
3629 ((eq 'nntp (car gnus-current-select-method))
3630 ;; The author of gnus-get-newsgroup-headers-xover
3631 ;; reports that the XOVER command is commonly
3632 ;; unreliable. The problem is that recently
3633 ;; posted articles may not be entered into the
3634 ;; NOV database in time to respond to my XOVER
3637 ;; I'm going to use his assumption that the NOV
3638 ;; database is updated in order of ascending
3639 ;; article ID. Therefore, a response containing
3640 ;; article ID N implies that all articles from 1
3641 ;; to N-1 are up-to-date. Therefore, missing
3642 ;; articles in that range have expired.
3644 (set-buffer nntp-server-buffer)
3645 (let* ((fetched-articles (list nil))
3646 (tail-fetched-articles fetched-articles)
3647 (min (cond ((numberp fetch-old)
3648 (max 1 (- (car articles) fetch-old)))
3653 (max (car (last articles))))
3655 ;; Get the list of articles that were fetched
3656 (goto-char (point-min))
3657 (let ((pm (point-max))
3659 (while (< (point) pm)
3660 (when (setq art (gnus-agent-read-article-number))
3661 (gnus-agent-append-to-list tail-fetched-articles art))
3664 ;; Clip this list to the headers that will
3665 ;; actually be returned
3666 (setq fetched-articles (gnus-list-range-intersection
3667 (cdr fetched-articles)
3670 ;; Clip the uncached articles list to exclude
3671 ;; IDs after the last FETCHED header. The
3672 ;; excluded IDs may be fetchable using HEAD.
3673 (if (car tail-fetched-articles)
3674 (setq uncached-articles
3675 (gnus-list-range-intersection
3677 (cons (car uncached-articles)
3678 (car tail-fetched-articles)))))
3680 ;; Create the list of articles that were
3681 ;; "successfully" fetched. Success, in this
3682 ;; case, means that the ID should not be
3683 ;; fetched again. In the case of an expired
3684 ;; article, the header will not be fetched.
3685 (setq uncached-articles
3686 (gnus-sorted-nunion fetched-articles
3690 ;; Erase the temp buffer
3691 (set-buffer gnus-agent-overview-buffer)
3694 ;; Copy the nntp-server-buffer to the temp buffer
3695 (set-buffer nntp-server-buffer)
3696 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3698 ;; Merge the temp buffer with the known headers (found on
3699 ;; disk in FILE) into the nntp-server-buffer
3700 (when uncached-articles
3701 (gnus-agent-braid-nov group uncached-articles file))
3703 ;; Save the new set of known headers to FILE
3704 (set-buffer nntp-server-buffer)
3705 (gnus-agent-check-overview-buffer)
3706 (write-region-as-coding-system
3707 gnus-agent-file-coding-system
3708 (point-min) (point-max) file nil 'silent)
3710 (gnus-agent-update-view-total-fetched-for group t)
3712 ;; Update the group's article alist to include the newly
3713 ;; fetched articles.
3714 (gnus-agent-load-alist group)
3715 (gnus-agent-save-alist group uncached-articles nil)
3718 ;; Copy the temp buffer to the nntp-server-buffer
3719 (set-buffer nntp-server-buffer)
3721 (insert-buffer-substring gnus-agent-overview-buffer)))
3724 (not (numberp fetch-old)))
3725 t ; Don't remove anything.
3726 (nnheader-nov-delete-outside-range
3727 (if fetch-old (max 1 (- (car articles) fetch-old))
3729 (car (last articles)))
3734 (defun gnus-agent-request-article (article group)
3735 "Retrieve ARTICLE in GROUP from the agent cache."
3736 (when (and gnus-agent
3737 (or gnus-agent-cache
3740 (let* ((gnus-command-method (gnus-find-method-for-group group))
3741 (file (gnus-agent-article-name (number-to-string article) group))
3742 (buffer-read-only nil))
3743 (when (and (file-exists-p file)
3744 (> (nth 7 (file-attributes file)) 0))
3746 (gnus-kill-all-overlays)
3747 (insert-file-contents-as-coding-system gnus-cache-coding-system file)
3750 (defun gnus-agent-regenerate-group (group &optional reread)
3752 If REREAD is t, all articles in the .overview are marked as unread.
3753 If REREAD is a list, the specified articles will be marked as unread.
3754 In addition, their NOV entries in .overview will be refreshed using
3755 the articles' current headers.
3756 If REREAD is not nil, downloaded articles are marked as unread."
3758 (list (let ((def (or (gnus-group-group-name)
3759 gnus-newsgroup-name)))
3760 (let ((select (read-string (if def
3761 (concat "Group Name ("
3764 (if (and (equal "" select)
3770 (cursor-in-echo-area t)
3771 (echo-keystrokes 0))
3772 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3773 (setq c (read-char-exclusive))
3775 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3777 ((or (eq c ?a) (eq c ?A))
3779 ((or (eq c ?d) (eq c ?D))
3780 (throw 'mark 'some)))
3781 (gnus-message 3 "Ignoring unexpected input")
3785 (gnus-message 5 "Regenerating in %s" group)
3786 (let* ((gnus-command-method (or gnus-command-method
3787 (gnus-find-method-for-group group)))
3788 (file (gnus-agent-article-name ".overview" group))
3789 (dir (file-name-directory file))
3791 (downloaded (if (file-exists-p dir)
3792 (sort (mapcar (lambda (name) (string-to-number name))
3793 (directory-files dir nil "^[0-9]+$" t))
3795 (progn (gnus-make-directory dir) nil)))
3800 (mm-with-unibyte-buffer
3801 (if (file-exists-p file)
3802 (let ((nnheader-file-coding-system
3803 gnus-agent-file-coding-system))
3804 (nnheader-insert-file-contents file)))
3805 (set-buffer-modified-p nil)
3807 ;; Load the article IDs found in the overview file. As a
3808 ;; side-effect, validate the file contents.
3812 (goto-char (point-min))
3813 (while (< (point) (point-max))
3814 (cond ((and (looking-at "[0-9]+\t")
3815 (<= (- (match-end 0) (match-beginning 0)) 9))
3816 (push (read (current-buffer)) nov-arts)
3818 (let ((l1 (car nov-arts))
3819 (l2 (cadr nov-arts)))
3820 (cond ((and (listp reread) (memq l1 reread))
3822 (setq nov-arts (cdr nov-arts))
3823 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3824 entry of article %s deleted." l1))
3828 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3829 entries are NOT in ascending order.")
3830 ;; Don't sort now as I haven't verified
3831 ;; that every line begins with a number
3835 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3836 entries contained duplicate of article %s. Duplicate deleted." l1)
3838 (setq nov-arts (cdr nov-arts))))))
3840 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3841 entries contained line that did not begin with an article number. Deleted\
3843 (gnus-delete-line))))
3845 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3846 entries into ascending order.")
3847 (sort-numeric-fields 1 (point-min) (point-max))
3848 (setq nov-arts nil))))
3849 (gnus-agent-check-overview-buffer)
3851 ;; Construct a new article alist whose nodes match every header
3852 ;; in the .overview file. As a side-effect, missing headers are
3853 ;; reconstructed from the downloaded article file.
3854 (while (or downloaded nov-arts)
3855 (cond ((and downloaded
3857 (> (car downloaded) (car nov-arts))))
3858 ;; This entry is missing from the overview file
3859 (gnus-message 3 "Regenerating NOV %s %d..." group
3861 (let ((file (concat dir (number-to-string (car downloaded)))))
3862 (mm-with-unibyte-buffer
3863 (nnheader-insert-file-contents file)
3864 (nnheader-remove-body)
3865 (setq header (nnheader-parse-naked-head)))
3866 (mail-header-set-number header (car downloaded))
3868 (let ((key (concat "^" (int-to-string (car nov-arts))
3870 (or (re-search-backward key nil t)
3871 (re-search-forward key))
3873 (goto-char (point-min)))
3874 (nnheader-insert-nov header))
3875 (setq nov-arts (cons (car downloaded) nov-arts)))
3876 ((eq (car downloaded) (car nov-arts))
3877 ;; This entry in the overview has been downloaded
3878 (push (cons (car downloaded)
3880 (nth 5 (file-attributes
3881 (concat dir (number-to-string
3882 (car downloaded))))))) alist)
3883 (setq downloaded (cdr downloaded))
3884 (setq nov-arts (cdr nov-arts)))
3886 ;; This entry in the overview has not been downloaded
3887 (push (cons (car nov-arts) nil) alist)
3888 (setq nov-arts (cdr nov-arts)))))
3890 ;; When gnus-agent-consider-all-articles is set,
3891 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3892 ;; the alist. Those IDs serve as markers to indicate that an
3893 ;; attempt has been made to fetch that article's header.
3895 ;; When gnus-agent-consider-all-articles is NOT set,
3896 ;; gnus-agent-regenerate-group can remove the article ID of every
3897 ;; article (with the exception of the last ID in the list - it's
3898 ;; special) that no longer appears in the overview. In this
3899 ;; situtation, the last article ID in the list implies that it,
3900 ;; and every article ID preceeding it, have been fetched from the
3903 (if gnus-agent-consider-all-articles
3904 ;; Restore all article IDs that were not found in the overview file.
3905 (let* ((n (cons nil alist))
3907 (o (gnus-agent-load-alist group)))
3909 (let ((nID (caadr n))
3912 (setq n (setcdr n (list (list oID))))
3915 (setcdr n (cons (list oID) (cdr n)))
3921 (setq n (cdr n))))))
3922 (setq alist (cdr merged)))
3923 ;; Restore the last article ID if it is not already in the new alist
3924 (let ((n (last alist))
3925 (o (last (gnus-agent-load-alist group))))
3929 (push (cons (caar o) nil) alist))
3930 ((< (caar n) (caar o))
3931 (setcdr n (list (car o)))))))
3933 (let ((inhibit-quit t))
3934 (if (setq regenerated (buffer-modified-p))
3935 (write-region-as-coding-system
3936 gnus-agent-file-coding-system
3937 (point-min) (point-max) file nil 'silent))
3939 (setq regenerated (or regenerated
3940 (and reread gnus-agent-article-alist)
3941 (not (equal alist gnus-agent-article-alist))))
3943 (setq gnus-agent-article-alist alist)
3946 (gnus-agent-save-alist group)
3948 ;; I have to alter the group's active range NOW as
3949 ;; gnus-make-ascending-articles-unread will use it to
3950 ;; recalculate the number of unread articles in the group
3952 (let ((group (gnus-group-real-name group))
3953 (group-active (or (gnus-active group)
3954 (gnus-activate-group group))))
3955 (gnus-agent-possibly-alter-active group group-active)))))
3957 (when (and reread gnus-agent-article-alist)
3958 (gnus-agent-synchronize-group-flags
3963 (delq nil (mapcar (function (lambda (c)
3964 (cond ((eq reread t)
3968 gnus-agent-article-alist)))
3970 gnus-command-method)
3973 (gnus-agent-update-files-total-fetched-for group nil)))
3979 (defun gnus-agent-regenerate (&optional clean reread)
3980 "Regenerate all agent covered files.
3981 If CLEAN, obsolete (ignore)."
3984 (gnus-message 4 "Regenerating Gnus agent files...")
3985 (dolist (gnus-command-method (gnus-agent-covered-methods))
3986 (dolist (group (gnus-groups-from-server gnus-command-method))
3987 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3989 (gnus-message 4 "Regenerating Gnus agent files...done")
3993 (defun gnus-agent-go-online (&optional force)
3994 "Switch servers into online status."
3995 (interactive (list t))
3996 (dolist (server gnus-opened-servers)
3997 (when (eq (nth 1 server) 'offline)
3998 (if (if (eq force 'ask)
4000 (format "Switch %s:%s into online status? "
4001 (caar server) (cadar server)))
4003 (setcar (nthcdr 1 server) 'close)))))
4005 (defun gnus-agent-toggle-group-plugged (group)
4006 "Toggle the status of the server of the current group."
4007 (interactive (list (gnus-group-group-name)))
4008 (let* ((method (gnus-find-method-for-group group))
4009 (status (cadr (assoc method gnus-opened-servers))))
4010 (if (eq status 'offline)
4011 (gnus-server-set-status method 'closed)
4012 (gnus-close-server method)
4013 (gnus-server-set-status method 'offline))
4014 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4015 (if (eq status 'offline) 'offline 'online)
4016 (if (eq status 'offline) 'online 'offline))))
4018 (defun gnus-agent-group-covered-p (group)
4019 (gnus-agent-method-p (gnus-group-method group)))
4021 ;; Added to support XEmacs
4023 (unless (fboundp 'directory-files-and-attributes)
4024 (defun directory-files-and-attributes (directory
4025 &optional full match nosort)
4027 (dolist (file (directory-files directory full match nosort))
4028 (push (cons file (file-attributes file)) result))
4029 (nreverse result)))))
4031 (defun gnus-agent-update-files-total-fetched-for
4032 (group delta &optional method path)
4033 "Update, or set, the total disk space used by the articles that the
4035 (when gnus-agent-total-fetched-hashtb
4036 (gnus-agent-with-refreshed-group
4038 ;; if null, gnus-agent-group-pathname will calc method.
4039 (let* ((gnus-command-method method)
4040 (path (or path (gnus-agent-group-pathname group)))
4041 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4042 (gnus-sethash path (make-list 3 0)
4043 gnus-agent-total-fetched-hashtb))))
4048 (while (setq file (pop delta))
4049 (incf sum (float (or (nth 7 (file-attributes
4053 (number-to-string file)
4056 (let ((sum (- (nth 2 entry)))
4057 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4059 (while (setq file (pop info))
4060 (incf sum (float (or (nth 8 file) 0))))
4063 (setq gnus-agent-need-update-total-fetched-for t)
4064 (incf (nth 2 entry) delta)))))
4066 (defun gnus-agent-update-view-total-fetched-for
4067 (group agent-over &optional method path)
4068 "Update, or set, the total disk space used by the .agentview and
4069 .overview files. These files are calculated separately as they can be
4071 (when gnus-agent-total-fetched-hashtb
4072 (gnus-agent-with-refreshed-group
4074 ;; if null, gnus-agent-group-pathname will calc method.
4075 (let* ((gnus-command-method method)
4076 (path (or path (gnus-agent-group-pathname group)))
4077 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4078 (gnus-sethash path (make-list 3 0)
4079 gnus-agent-total-fetched-hashtb)))
4080 (size (or (nth 7 (file-attributes
4086 (setq gnus-agent-need-update-total-fetched-for t)
4087 (setf (nth (if agent-over 1 0) entry) size)))))
4089 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4090 "Get the total disk space used by the specified GROUP."
4091 (unless gnus-agent-total-fetched-hashtb
4092 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4094 ;; if null, gnus-agent-group-pathname will calc method.
4095 (let* ((gnus-command-method method)
4096 (path (gnus-agent-group-pathname group))
4097 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4100 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4102 (gnus-agent-update-view-total-fetched-for group nil method path)
4103 (gnus-agent-update-view-total-fetched-for group t method path)
4104 (gnus-agent-update-files-total-fetched-for group nil method path))))))
4106 (provide 'gnus-agent)
4108 ;;; gnus-agent.el ends here