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 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
36 (if (featurep 'xemacs)
42 (autoload 'gnus-server-update-server "gnus-srvr")
43 (autoload 'gnus-agent-customize-category "gnus-cus")
46 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
47 "Where the Gnus agent will store its files."
51 (defcustom gnus-agent-plugged-hook nil
52 "Hook run when plugging into the network."
56 (defcustom gnus-agent-unplugged-hook nil
57 "Hook run when unplugging from the network."
61 (defcustom gnus-agent-fetched-hook nil
62 "Hook run when finished fetching articles."
67 (defcustom gnus-agent-handle-level gnus-level-subscribed
68 "Groups on levels higher than this variable will be ignored by the Agent."
72 (defcustom gnus-agent-expire-days 7
73 "Read articles older than this will be expired.
74 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
76 :type '(number :tag "days"))
78 (defcustom gnus-agent-expire-all nil
79 "If non-nil, also expire unread, ticked and dormant articles.
80 If nil, only read articles will be expired."
84 (defcustom gnus-agent-group-mode-hook nil
85 "Hook run in Agent group minor modes."
89 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
90 (when (featurep 'xemacs)
91 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
93 (defcustom gnus-agent-summary-mode-hook nil
94 "Hook run in Agent summary minor modes."
98 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
99 (when (featurep 'xemacs)
100 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
102 (defcustom gnus-agent-server-mode-hook nil
103 "Hook run in Agent summary minor modes."
107 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
108 (when (featurep 'xemacs)
109 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
111 (defcustom gnus-agent-confirmation-function 'y-or-n-p
112 "Function to confirm when error happens."
117 (defcustom gnus-agent-synchronize-flags nil
118 "Indicate if flags are synchronized when you plug in.
119 If this is `ask' the hook will query the user."
121 :type '(choice (const :tag "Always" t)
122 (const :tag "Never" nil)
123 (const :tag "Ask" ask))
126 (defcustom gnus-agent-go-online 'ask
127 "Indicate if offline servers go online when you plug in.
128 If this is `ask' the hook will query the user."
130 :type '(choice (const :tag "Always" t)
131 (const :tag "Never" nil)
132 (const :tag "Ask" ask))
135 (defcustom gnus-agent-mark-unread-after-downloaded t
136 "Indicate whether to mark articles unread after downloaded."
141 (defcustom gnus-agent-download-marks '(download)
142 "Marks for downloading."
144 :type '(repeat (symbol :tag "Mark"))
147 (defcustom gnus-agent-consider-all-articles nil
148 "When non-nil, the agent will let the agent predicate decide
149 whether articles need to be downloaded or not, for all articles. When
150 nil, the default, the agent will only let the predicate decide
151 whether unread articles are downloaded or not. If you enable this,
152 groups with large active ranges may open slower and you may also want
153 to look into the agent expiry settings to block the expiration of
154 read articles as they would just be downloaded again."
159 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
160 "Chunk size for `gnus-agent-fetch-session'.
161 The function will split its article fetches into chunks smaller than
167 (defcustom gnus-agent-enable-expiration 'ENABLE
168 "The default expiration state for each group.
169 When set to ENABLE, the default, `gnus-agent-expire' will expire old
170 contents from a group's local storage. This value may be overridden
171 to disable expiration in specific categories, topics, and groups. Of
172 course, you could change gnus-agent-enable-expiration to DISABLE then
173 enable expiration per categories, topics, and groups."
176 :type '(radio (const :format "Enable " ENABLE)
177 (const :format "Disable " DISABLE)))
179 (defcustom gnus-agent-expire-unagentized-dirs t
180 "*Whether expiration should expire in unagentized directories.
181 Have gnus-agent-expire scan the directories under
182 \(gnus-agent-directory) for groups that are no longer agentized.
183 When found, offer to remove them."
188 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
189 "Initially, all servers from these methods are agentized.
190 The user may remove or add servers using the Server buffer.
191 See Info node `(gnus)Server Buffer'."
193 :type '(repeat symbol)
196 (defcustom gnus-agent-queue-mail t
197 "Whether and when outgoing mail should be queued by the agent.
198 When `always', always queue outgoing mail. When nil, never
199 queue. Otherwise, queue if and only if unplugged."
202 :type '(radio (const :format "Always" always)
203 (const :format "Never" nil)
204 (const :format "When plugged" t)))
206 (defcustom gnus-agent-prompt-send-queue nil
207 "If non-nil, `gnus-group-send-queue' will prompt if called when
213 ;;; Internal variables
215 (defvar gnus-agent-history-buffers nil)
216 (defvar gnus-agent-buffer-alist nil)
217 (defvar gnus-agent-article-alist nil
218 "An assoc list identifying the articles whose headers have been fetched.
219 If successfully fetched, these headers will be stored in the group's overview
220 file. The key of each assoc pair is the article ID, the value of each assoc
221 pair is a flag indicating whether the identified article has been downloaded
222 \(gnus-agent-fetch-articles sets the value to the day of the download).
224 1) The last element of this list can not be expired as some
225 routines (for example, get-agent-fetch-headers) use the last
226 value to track which articles have had their headers retrieved.
227 2) The function `gnus-agent-regenerate' may destructively modify the value.")
228 (defvar gnus-agent-group-alist nil)
229 (defvar gnus-category-alist nil)
230 (defvar gnus-agent-current-history nil)
231 (defvar gnus-agent-overview-buffer nil)
232 (defvar gnus-category-predicate-cache nil)
233 (defvar gnus-category-group-cache nil)
234 (defvar gnus-agent-spam-hashtb nil)
235 (defvar gnus-agent-file-name nil)
236 (defvar gnus-agent-send-mail-function nil)
237 (defvar gnus-agent-file-coding-system 'raw-text)
238 (defvar gnus-agent-file-loading-cache nil)
239 (defvar gnus-agent-total-fetched-hashtb nil)
240 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
241 (defvar gnus-agent-need-update-total-fetched-for nil)
244 (defvar gnus-headers)
251 (defun gnus-open-agent ()
253 (gnus-agent-read-servers)
255 (gnus-agent-create-buffer)
256 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
257 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
258 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
260 (defun gnus-agent-create-buffer ()
261 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
263 (setq gnus-agent-overview-buffer
264 (gnus-get-buffer-create " *Gnus agent overview*"))
265 (with-current-buffer gnus-agent-overview-buffer
266 (mm-enable-multibyte))
269 (gnus-add-shutdown 'gnus-close-agent 'gnus)
271 (defun gnus-close-agent ()
272 (setq gnus-category-predicate-cache nil
273 gnus-category-group-cache nil
274 gnus-agent-spam-hashtb nil)
275 (gnus-kill-buffer gnus-agent-overview-buffer))
278 ;;; Utility functions
281 (defmacro gnus-agent-with-refreshed-group (group &rest body)
282 "Performs the body then updates the group's line in the group
283 buffer. Automatically blocks multiple updates due to recursion."
284 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
285 (when (and gnus-agent-need-update-total-fetched-for
286 (not gnus-agent-inhibit-update-total-fetched-for))
288 (set-buffer gnus-group-buffer)
289 (setq gnus-agent-need-update-total-fetched-for nil)
290 (gnus-group-update-group ,group t)))))
292 (defun gnus-agent-read-file (file)
293 "Load FILE and do a `read' there."
296 (nnheader-insert-file-contents file)
297 (goto-char (point-min))
298 (read (current-buffer)))))
300 (defsubst gnus-agent-method ()
301 (concat (symbol-name (car gnus-command-method)) "/"
302 (if (equal (cadr gnus-command-method) "")
304 (cadr gnus-command-method))))
306 (defsubst gnus-agent-directory ()
307 "The name of the Gnus agent directory."
308 (nnheader-concat gnus-agent-directory
309 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
311 (defun gnus-agent-lib-file (file)
312 "The full name of the Gnus agent library FILE."
313 (expand-file-name file
314 (file-name-as-directory
315 (expand-file-name "agent.lib" (gnus-agent-directory)))))
317 (defun gnus-agent-cat-set-property (category property value)
319 (setcdr (or (assq property category)
320 (let ((cell (cons property nil)))
321 (setcdr category (cons cell (cdr category)))
323 (let ((category category))
324 (while (cond ((eq property (caadr category))
325 (setcdr category (cddr category))
328 (setq category (cdr category)))))))
332 (defmacro gnus-agent-cat-defaccessor (name prop-name)
333 "Define accessor and setter methods for manipulating a list of the form
334 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
335 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
336 manipulated as follows:
337 (func LIST): Returns VALUE1
338 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
339 `(progn (defmacro ,name (category)
340 (list (quote cdr) (list (quote assq)
341 (quote (quote ,prop-name)) category)))
343 (define-setf-method ,name (category)
344 (let* ((--category--temp-- (make-symbol "--category--"))
345 (--value--temp-- (make-symbol "--value--")))
346 (list (list --category--temp--) ; temporary-variables
347 (list category) ; value-forms
348 (list --value--temp--) ; store-variables
349 (let* ((category --category--temp--) ; store-form
350 (value --value--temp--))
351 (list (quote gnus-agent-cat-set-property)
353 (quote (quote ,prop-name))
355 (list (quote ,name) --category--temp--) ; access-form
359 (defmacro gnus-agent-cat-name (category)
362 (gnus-agent-cat-defaccessor
363 gnus-agent-cat-days-until-old agent-days-until-old)
364 (gnus-agent-cat-defaccessor
365 gnus-agent-cat-enable-expiration agent-enable-expiration)
366 (gnus-agent-cat-defaccessor
367 gnus-agent-cat-groups agent-groups)
368 (gnus-agent-cat-defaccessor
369 gnus-agent-cat-high-score agent-high-score)
370 (gnus-agent-cat-defaccessor
371 gnus-agent-cat-length-when-long agent-long-article)
372 (gnus-agent-cat-defaccessor
373 gnus-agent-cat-length-when-short agent-short-article)
374 (gnus-agent-cat-defaccessor
375 gnus-agent-cat-low-score agent-low-score)
376 (gnus-agent-cat-defaccessor
377 gnus-agent-cat-predicate agent-predicate)
378 (gnus-agent-cat-defaccessor
379 gnus-agent-cat-score-file agent-score)
380 (gnus-agent-cat-defaccessor
381 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
384 ;; This form is equivalent to defsetf except that it calls make-symbol
385 ;; whereas defsetf calls gensym (Using gensym creates a run-time
386 ;; dependency on the CL library).
389 (define-setf-method gnus-agent-cat-groups (category)
390 (let* ((--category--temp-- (make-symbol "--category--"))
391 (--groups--temp-- (make-symbol "--groups--")))
392 (list (list --category--temp--)
394 (list --groups--temp--)
395 (let* ((category --category--temp--)
396 (groups --groups--temp--))
397 (list (quote gnus-agent-set-cat-groups) category groups))
398 (list (quote gnus-agent-cat-groups) --category--temp--))))
401 (defun gnus-agent-set-cat-groups (category groups)
402 (unless (eq groups 'ignore)
404 (old-g (gnus-agent-cat-groups category)))
405 (cond ((eq new-g old-g)
406 ;; gnus-agent-add-group is fiddling with the group
407 ;; list. Still, Im done.
410 ((eq new-g (cdr old-g))
411 ;; gnus-agent-add-group is fiddling with the group list
412 (setcdr (or (assq 'agent-groups category)
413 (let ((cell (cons 'agent-groups nil)))
414 (setcdr category (cons cell (cdr category)))
417 (let ((groups groups))
419 (let* ((group (pop groups))
420 (old-category (gnus-group-category group)))
421 (if (eq category old-category)
423 (setf (gnus-agent-cat-groups old-category)
424 (delete group (gnus-agent-cat-groups
426 ;; Purge cache as preceeding loop invalidated it.
427 (setq gnus-category-group-cache nil))
429 (setcdr (or (assq 'agent-groups category)
430 (let ((cell (cons 'agent-groups nil)))
431 (setcdr category (cons cell (cdr category)))
434 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
435 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
437 ;;; Fetching setup functions.
439 (defun gnus-agent-start-fetch ()
440 "Initialize data structures for efficient fetching."
441 (gnus-agent-create-buffer))
443 (defun gnus-agent-stop-fetch ()
444 "Save all data structures and clean up."
445 (setq gnus-agent-spam-hashtb nil)
447 (set-buffer nntp-server-buffer)
450 (defmacro gnus-agent-with-fetch (&rest forms)
453 (let ((gnus-agent-fetching t))
454 (gnus-agent-start-fetch)
456 (gnus-agent-stop-fetch)))
458 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
459 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
461 (defmacro gnus-agent-append-to-list (tail value)
462 `(setq ,tail (setcdr ,tail (cons ,value nil))))
464 (defmacro gnus-agent-message (level &rest args)
465 `(if (<= ,level gnus-verbose)
472 (defvar gnus-agent-mode-hook nil
473 "Hook run when installing agent mode.")
475 (defvar gnus-agent-mode nil)
476 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
478 (defun gnus-agent-mode ()
479 "Minor mode for providing a agent support in Gnus buffers."
480 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
481 (symbol-name major-mode))
482 (match-string 1 (symbol-name major-mode))))
483 (mode (intern (format "gnus-agent-%s-mode" buffer))))
484 (set (make-local-variable 'gnus-agent-mode) t)
486 (set (make-local-variable mode) t)
488 (when (gnus-visual-p 'agent-menu 'menu)
489 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
490 (unless (assq 'gnus-agent-mode minor-mode-alist)
491 (push gnus-agent-mode-status minor-mode-alist))
492 (unless (assq mode minor-mode-map-alist)
493 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
495 minor-mode-map-alist))
496 (when (eq major-mode 'gnus-group-mode)
497 (let ((init-plugged gnus-plugged)
498 (gnus-agent-go-online nil))
499 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
500 ;; Therefore, make certain that the current value does not
501 ;; match the desired initial value.
502 (setq gnus-plugged :unknown)
503 (gnus-agent-toggle-plugged init-plugged)))
504 (gnus-run-hooks 'gnus-agent-mode-hook
505 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
507 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
508 (gnus-define-keys gnus-agent-group-mode-map
509 "Ju" gnus-agent-fetch-groups
510 "Jc" gnus-enter-category-buffer
511 "Jj" gnus-agent-toggle-plugged
512 "Js" gnus-agent-fetch-session
513 "JY" gnus-agent-synchronize-flags
514 "JS" gnus-group-send-queue
515 "Ja" gnus-agent-add-group
516 "Jr" gnus-agent-remove-group
517 "Jo" gnus-agent-toggle-group-plugged)
519 (defun gnus-agent-group-make-menu-bar ()
520 (unless (boundp 'gnus-agent-group-menu)
522 gnus-agent-group-menu gnus-agent-group-mode-map ""
524 ["Toggle plugged" gnus-agent-toggle-plugged t]
525 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
526 ["List categories" gnus-enter-category-buffer t]
527 ["Add (current) group to category" gnus-agent-add-group t]
528 ["Remove (current) group from category" gnus-agent-remove-group t]
529 ["Send queue" gnus-group-send-queue gnus-plugged]
531 ["All" gnus-agent-fetch-session gnus-plugged]
532 ["Group" gnus-agent-fetch-group gnus-plugged])
533 ["Synchronize flags" gnus-agent-synchronize-flags t]
536 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
537 (gnus-define-keys gnus-agent-summary-mode-map
538 "Jj" gnus-agent-toggle-plugged
539 "Ju" gnus-agent-summary-fetch-group
540 "JS" gnus-agent-fetch-group
541 "Js" gnus-agent-summary-fetch-series
542 "J#" gnus-agent-mark-article
543 "J\M-#" gnus-agent-unmark-article
544 "@" gnus-agent-toggle-mark
545 "Jc" gnus-agent-catchup)
547 (defun gnus-agent-summary-make-menu-bar ()
548 (unless (boundp 'gnus-agent-summary-menu)
550 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
552 ["Toggle plugged" gnus-agent-toggle-plugged t]
553 ["Mark as downloadable" gnus-agent-mark-article t]
554 ["Unmark as downloadable" gnus-agent-unmark-article t]
555 ["Toggle mark" gnus-agent-toggle-mark t]
556 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
557 ["Catchup undownloaded" gnus-agent-catchup t]))))
559 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
560 (gnus-define-keys gnus-agent-server-mode-map
561 "Jj" gnus-agent-toggle-plugged
562 "Ja" gnus-agent-add-server
563 "Jr" gnus-agent-remove-server)
565 (defun gnus-agent-server-make-menu-bar ()
566 (unless (boundp 'gnus-agent-server-menu)
568 gnus-agent-server-menu gnus-agent-server-mode-map ""
570 ["Toggle plugged" gnus-agent-toggle-plugged t]
571 ["Add" gnus-agent-add-server t]
572 ["Remove" gnus-agent-remove-server t]))))
574 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
575 (if (and (fboundp 'propertize)
576 (fboundp 'make-mode-line-mouse-map))
577 (propertize string 'local-map
578 (make-mode-line-mouse-map mouse-button mouse-func))
581 (defun gnus-agent-toggle-plugged (set-to)
582 "Toggle whether Gnus is unplugged or not."
583 (interactive (list (not gnus-plugged)))
584 (cond ((eq set-to gnus-plugged)
587 (setq gnus-plugged set-to)
588 (gnus-run-hooks 'gnus-agent-plugged-hook)
589 (setcar (cdr gnus-agent-mode-status)
590 (gnus-agent-make-mode-line-string " Plugged"
592 'gnus-agent-toggle-plugged))
593 (gnus-agent-go-online gnus-agent-go-online)
594 (gnus-agent-possibly-synchronize-flags))
596 (gnus-agent-close-connections)
597 (setq gnus-plugged set-to)
598 (gnus-run-hooks 'gnus-agent-unplugged-hook)
599 (setcar (cdr gnus-agent-mode-status)
600 (gnus-agent-make-mode-line-string " Unplugged"
602 'gnus-agent-toggle-plugged))))
603 (set-buffer-modified-p t))
605 (defmacro gnus-agent-while-plugged (&rest body)
606 `(let ((original-gnus-plugged gnus-plugged))
608 (progn (gnus-agent-toggle-plugged t)
610 (gnus-agent-toggle-plugged original-gnus-plugged))))
612 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
613 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
615 (defun gnus-agent-close-connections ()
616 "Close all methods covered by the Gnus agent."
617 (let ((methods (gnus-agent-covered-methods)))
619 (gnus-close-server (pop methods)))))
622 (defun gnus-unplugged ()
623 "Start Gnus unplugged."
625 (setq gnus-plugged nil)
629 (defun gnus-plugged ()
630 "Start Gnus plugged."
632 (setq gnus-plugged t)
636 (defun gnus-slave-unplugged (&optional arg)
637 "Read news as a slave unplugged."
639 (setq gnus-plugged nil)
640 (gnus arg nil 'slave))
643 (defun gnus-agentize ()
644 "Allow Gnus to be an offline newsreader.
646 The gnus-agentize function is now called internally by gnus when
647 gnus-agent is set. If you wish to avoid calling gnus-agentize,
648 customize gnus-agent to nil.
650 This will modify the `gnus-setup-news-hook', and
651 `message-send-mail-real-function' variables, and install the Gnus agent
652 minor mode in all Gnus buffers."
655 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
656 (unless gnus-agent-send-mail-function
657 (setq gnus-agent-send-mail-function
658 (or message-send-mail-real-function
659 (function (lambda () (funcall message-send-mail-function))))
660 message-send-mail-real-function 'gnus-agent-send-mail))
662 ;; If the servers file doesn't exist, auto-agentize some servers and
663 ;; save the servers file so this auto-agentizing isn't invoked
665 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
666 (gnus-message 3 "First time agent user, agentizing remote groups...")
668 (lambda (server-or-method)
669 (let ((method (gnus-server-to-method server-or-method)))
670 (when (memq (car method)
671 gnus-agent-auto-agentize-methods)
672 (push (gnus-method-to-server method)
673 gnus-agent-covered-methods)
674 (setq gnus-agent-method-p-cache nil))))
675 (cons gnus-select-method gnus-secondary-select-methods))
676 (gnus-agent-write-servers)))
678 (defun gnus-agent-queue-setup (&optional group-name)
679 "Make sure the queue group exists.
680 Optional arg GROUP-NAME allows to specify another group."
681 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
683 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
684 (let ((gnus-level-default-subscribed 1))
685 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
687 (gnus-group-set-parameter
688 (format "nndraft:%s" (or group-name "queue"))
689 'gnus-dummy '((gnus-draft-mode)))))
691 (defun gnus-agent-send-mail ()
692 (if (or (not gnus-agent-queue-mail)
693 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
694 (funcall gnus-agent-send-mail-function)
695 (goto-char (point-min))
697 (concat "^" (regexp-quote mail-header-separator) "\n"))
699 (gnus-agent-insert-meta-information 'mail)
700 (gnus-request-accept-article "nndraft:queue" nil t t)))
702 (defun gnus-agent-insert-meta-information (type &optional method)
703 "Insert meta-information into the message that says how it's to be posted.
704 TYPE can be either `mail' or `news'. If the latter, then METHOD can
707 (message-remove-header gnus-agent-meta-information-header)
708 (goto-char (point-min))
709 (insert gnus-agent-meta-information-header ": "
710 (symbol-name type) " " (format "%S" method)
713 (while (search-backward "\n" nil t)
714 (replace-match "\\n" t t))))
716 (defun gnus-agent-restore-gcc ()
717 "Restore GCC field from saved header."
719 (goto-char (point-min))
720 (while (re-search-forward
721 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
722 (replace-match "Gcc:" 'fixedcase))))
724 (defun gnus-agent-any-covered-gcc ()
726 (message-narrow-to-headers)
727 (let* ((gcc (mail-fetch-field "gcc" nil t))
729 (mapcar 'gnus-inews-group-method
730 (message-unquote-tokens
731 (message-tokenize-header
734 (while (and (not covered) methods)
735 (setq covered (gnus-agent-method-p (car methods))
736 methods (cdr methods)))
740 (defun gnus-agent-possibly-save-gcc ()
741 "Save GCC if Gnus is unplugged."
742 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
744 (goto-char (point-min))
745 (let ((case-fold-search t))
746 (while (re-search-forward "^gcc:" nil t)
747 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
749 (defun gnus-agent-possibly-do-gcc ()
750 "Do GCC if Gnus is plugged."
751 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
752 (gnus-inews-do-gcc)))
755 ;;; Group mode commands
758 (defun gnus-agent-fetch-groups (n)
759 "Put all new articles in the current groups into the Agent."
762 (error "Groups can't be fetched when Gnus is unplugged"))
763 (gnus-group-iterate n 'gnus-agent-fetch-group))
765 (defun gnus-agent-fetch-group (&optional group)
766 "Put all new articles in GROUP into the Agent."
767 (interactive (list (gnus-group-group-name)))
768 (setq group (or group gnus-newsgroup-name))
770 (error "No group on the current line"))
772 (gnus-agent-while-plugged
773 (let ((gnus-command-method (gnus-find-method-for-group group)))
774 (gnus-agent-with-fetch
775 (gnus-agent-fetch-group-1 group gnus-command-method)
776 (gnus-message 5 "Fetching %s...done" group)))))
778 (defun gnus-agent-add-group (category arg)
779 "Add the current group to an agent category."
785 (mapcar (lambda (cat) (list (symbol-name (car cat))))
789 (let ((cat (assq category gnus-category-alist))
791 (gnus-group-iterate arg
793 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
794 (setf (gnus-agent-cat-groups c)
795 (delete group (gnus-agent-cat-groups c))))
796 (push group groups)))
797 (setf (gnus-agent-cat-groups cat)
798 (nconc (gnus-agent-cat-groups cat) groups))
799 (gnus-category-write)))
801 (defun gnus-agent-remove-group (arg)
802 "Remove the current group from its agent category, if any."
805 (gnus-group-iterate arg
807 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
808 (setf (gnus-agent-cat-groups c)
809 (delete group (gnus-agent-cat-groups c))))))
810 (gnus-category-write)))
812 (defun gnus-agent-synchronize-flags ()
813 "Synchronize unplugged flags with servers."
816 (dolist (gnus-command-method (gnus-agent-covered-methods))
817 (when (file-exists-p (gnus-agent-lib-file "flags"))
818 (gnus-agent-synchronize-flags-server gnus-command-method)))))
820 (defun gnus-agent-possibly-synchronize-flags ()
821 "Synchronize flags according to `gnus-agent-synchronize-flags'."
824 (dolist (gnus-command-method (gnus-agent-covered-methods))
825 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
826 (not (eq (gnus-server-status gnus-command-method) 'offline)))
827 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
829 (defun gnus-agent-synchronize-flags-server (method)
830 "Synchronize flags set when unplugged for server."
831 (let ((gnus-command-method method)
833 (when (file-exists-p (gnus-agent-lib-file "flags"))
834 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
836 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
837 (cond ((null gnus-plugged)
839 1 "You must be plugged to synchronize flags with server %s"
840 (nth 1 gnus-command-method)))
841 ((null (gnus-check-server gnus-command-method))
843 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
848 (eval (read (current-buffer)))
849 (delete-region bgn (point))))
851 (delete-file (gnus-agent-lib-file "flags")))
853 (let ((file (gnus-agent-lib-file "flags")))
854 (write-region (point-min) (point-max)
855 (gnus-agent-lib-file "flags") nil 'silent)
856 (error "Couldn't set flags from file %s due to %s"
857 file (error-message-string err)))))))
860 (defun gnus-agent-possibly-synchronize-flags-server (method)
861 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
862 (when (or (and gnus-agent-synchronize-flags
863 (not (eq gnus-agent-synchronize-flags 'ask)))
864 (and (eq gnus-agent-synchronize-flags 'ask)
865 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
867 (gnus-agent-synchronize-flags-server method)))
870 (defun gnus-agent-rename-group (old-group new-group)
871 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
872 disabled, as the old agent files would corrupt gnus when the agent was
873 next enabled. Depends upon the caller to determine whether group renaming is supported."
874 (let* ((old-command-method (gnus-find-method-for-group old-group))
875 (old-path (directory-file-name
876 (let (gnus-command-method old-command-method)
877 (gnus-agent-group-pathname old-group))))
878 (new-command-method (gnus-find-method-for-group new-group))
879 (new-path (directory-file-name
880 (let (gnus-command-method new-command-method)
881 (gnus-agent-group-pathname new-group)))))
882 (gnus-rename-file old-path new-path t)
884 (let* ((old-real-group (gnus-group-real-name old-group))
885 (new-real-group (gnus-group-real-name new-group))
886 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
887 (gnus-agent-save-group-info old-command-method old-real-group nil)
888 (gnus-agent-save-group-info new-command-method new-real-group old-active)
890 (let ((old-local (gnus-agent-get-local old-group
891 old-real-group old-command-method)))
892 (gnus-agent-set-local old-group
894 old-real-group old-command-method)
895 (gnus-agent-set-local new-group
896 (car old-local) (cdr old-local)
897 new-real-group new-command-method)))))
900 (defun gnus-agent-delete-group (group)
901 "Delete fully-qualified GROUP. Always updates the agent, even when
902 disabled, as the old agent files would corrupt gnus when the agent was
903 next enabled. Depends upon the caller to determine whether group deletion is supported."
904 (let* ((command-method (gnus-find-method-for-group group))
905 (path (directory-file-name
906 (let (gnus-command-method command-method)
907 (gnus-agent-group-pathname group)))))
908 (gnus-delete-directory path)
910 (let* ((real-group (gnus-group-real-name group)))
911 (gnus-agent-save-group-info command-method real-group nil)
913 (let ((local (gnus-agent-get-local group
914 real-group command-method)))
915 (gnus-agent-set-local group
917 real-group command-method)))))
920 ;;; Server mode commands
923 (defun gnus-agent-add-server ()
924 "Enroll SERVER in the agent program."
926 (let* ((server (gnus-server-server-name))
927 (named-server (gnus-server-named-server))
929 (gnus-server-get-method nil server))))
931 (error "No server on the current line"))
933 (when (gnus-agent-method-p method)
934 (error "Server already in the agent program"))
936 (push named-server gnus-agent-covered-methods)
938 (setq gnus-agent-method-p-cache nil)
939 (gnus-server-update-server server)
940 (gnus-agent-write-servers)
941 (gnus-message 1 "Entered %s into the Agent" server)))
943 (defun gnus-agent-remove-server ()
944 "Remove SERVER from the agent program."
946 (let* ((server (gnus-server-server-name))
947 (named-server (gnus-server-named-server)))
949 (error "No server on the current line"))
951 (unless (member named-server gnus-agent-covered-methods)
952 (error "Server not in the agent program"))
954 (setq gnus-agent-covered-methods
955 (delete named-server gnus-agent-covered-methods)
956 gnus-agent-method-p-cache nil)
958 (gnus-server-update-server server)
959 (gnus-agent-write-servers)
960 (gnus-message 1 "Removed %s from the agent" server)))
962 (defun gnus-agent-read-servers ()
963 "Read the alist of covered servers."
964 (setq gnus-agent-covered-methods
965 (gnus-agent-read-file
966 (nnheader-concat gnus-agent-directory "lib/servers"))
967 gnus-agent-method-p-cache nil)
969 ;; I am called so early in start-up that I can not validate server
970 ;; names. When that is the case, I skip the validation. That is
971 ;; alright as the gnus startup code calls the validate methods
973 (if gnus-server-alist
974 (gnus-agent-read-servers-validate)))
976 (defun gnus-agent-read-servers-validate ()
977 (mapcar (lambda (server-or-method)
978 (let* ((server (if (stringp server-or-method)
980 (gnus-method-to-server server-or-method)))
981 (method (gnus-server-to-method server)))
983 (unless (member server gnus-agent-covered-methods)
984 (push server gnus-agent-covered-methods)
985 (setq gnus-agent-method-p-cache nil))
986 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
987 (prog1 gnus-agent-covered-methods
988 (setq gnus-agent-covered-methods nil))))
990 (defun gnus-agent-read-servers-validate-native (native-method)
991 (setq gnus-agent-covered-methods
992 (mapcar (lambda (method)
994 (equal method native-method))
996 method)) gnus-agent-covered-methods)))
998 (defun gnus-agent-write-servers ()
999 "Write the alist of covered servers."
1000 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1001 (let ((coding-system-for-write nnheader-file-coding-system)
1002 (file-name-coding-system nnmail-pathname-coding-system))
1003 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1004 (prin1 gnus-agent-covered-methods
1005 (current-buffer)))))
1008 ;;; Summary commands
1011 (defun gnus-agent-mark-article (n &optional unmark)
1012 "Mark the next N articles as downloadable.
1013 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1014 the mark instead. The difference between N and the actual number of
1015 articles marked is returned."
1017 (let ((backward (< n 0))
1022 (gnus-summary-set-agent-mark
1023 (gnus-summary-article-number) unmark)
1024 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1027 (gnus-message 7 "No more articles"))
1028 (gnus-summary-recenter)
1029 (gnus-summary-position-point)
1032 (defun gnus-agent-unmark-article (n)
1033 "Remove the downloadable mark from the next N articles.
1034 If N is negative, unmark backward instead. The difference between N and
1035 the actual number of articles unmarked is returned."
1037 (gnus-agent-mark-article n t))
1039 (defun gnus-agent-toggle-mark (n)
1040 "Toggle the downloadable mark from the next N articles.
1041 If N is negative, toggle backward instead. The difference between N and
1042 the actual number of articles toggled is returned."
1044 (gnus-agent-mark-article n 'toggle))
1046 (defun gnus-summary-set-agent-mark (article &optional unmark)
1047 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1048 When UNMARK is t, the article is unmarked. For any other value, the
1049 article's mark is toggled."
1050 (let ((unmark (cond ((eq nil unmark)
1055 (memq article gnus-newsgroup-downloadable)))))
1056 (when (gnus-summary-goto-subject article nil t)
1057 (gnus-summary-update-mark
1060 (setq gnus-newsgroup-downloadable
1061 (delq article gnus-newsgroup-downloadable))
1062 (gnus-article-mark article))
1063 (setq gnus-newsgroup-downloadable
1064 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1065 gnus-downloadable-mark)
1069 (defun gnus-agent-get-undownloaded-list ()
1070 "Construct list of articles that have not been downloaded."
1071 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1072 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1073 (gnus-agent-method-p gnus-command-method))
1074 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1075 (headers (sort (mapcar (lambda (h)
1076 (mail-header-number h))
1077 gnus-newsgroup-headers) '<))
1078 (cached (and gnus-use-cache gnus-newsgroup-cached))
1079 (undownloaded (list nil))
1080 (tail-undownloaded undownloaded)
1081 (unfetched (list nil))
1082 (tail-unfetched unfetched))
1083 (while (and alist headers)
1084 (let ((a (caar alist))
1087 ;; Ignore IDs in the alist that are not being
1088 ;; displayed in the summary.
1089 (setq alist (cdr alist)))
1091 ;; Headers that are not in the alist should be
1092 ;; fictious (see nnagent-retrieve-headers); they
1093 ;; imply that this article isn't in the agent.
1094 (gnus-agent-append-to-list tail-undownloaded h)
1095 (gnus-agent-append-to-list tail-unfetched h)
1096 (setq headers (cdr headers)))
1098 (setq alist (cdr alist))
1099 (setq headers (cdr headers))
1100 nil ; ignore already downloaded
1103 (setq alist (cdr alist))
1104 (setq headers (cdr headers))
1106 ;; This article isn't in the agent. Check to see
1107 ;; if it is in the cache. If it is, it's been
1109 (while (and cached (< (car cached) a))
1110 (setq cached (cdr cached)))
1111 (unless (equal a (car cached))
1112 (gnus-agent-append-to-list tail-undownloaded a))))))
1115 (let ((num (pop headers)))
1116 (gnus-agent-append-to-list tail-undownloaded num)
1117 (gnus-agent-append-to-list tail-unfetched num)))
1119 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1120 gnus-newsgroup-unfetched (cdr unfetched))))))
1122 (defun gnus-agent-catchup ()
1123 "Mark as read all unhandled articles.
1124 An article is unhandled if it is neither cached, nor downloaded, nor
1128 (let ((articles gnus-newsgroup-undownloaded))
1129 (when (or gnus-newsgroup-downloadable
1130 gnus-newsgroup-cached)
1131 (setq articles (gnus-sorted-ndifference
1132 (gnus-sorted-ndifference
1133 (gnus-copy-sequence articles)
1134 gnus-newsgroup-downloadable)
1135 gnus-newsgroup-cached)))
1138 (gnus-summary-mark-article
1139 (pop articles) gnus-catchup-mark)))
1140 (gnus-summary-position-point)))
1142 (defun gnus-agent-summary-fetch-series ()
1144 (when gnus-newsgroup-processable
1145 (setq gnus-newsgroup-downloadable
1146 (let* ((dl gnus-newsgroup-downloadable)
1147 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1148 (gnus-newsgroup-downloadable processable))
1149 (gnus-agent-summary-fetch-group)
1151 ;; For each article that I processed that is no longer
1152 ;; undownloaded, remove its processable mark.
1154 (mapc #'gnus-summary-remove-process-mark
1155 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1157 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1158 ;; updated the temporary gnus-newsgroup-downloadable to
1159 ;; remove each article successfully fetched. Now, I
1160 ;; update the real gnus-newsgroup-downloadable to only
1161 ;; include undownloaded articles.
1162 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1164 (defun gnus-agent-summary-fetch-group (&optional all)
1165 "Fetch the downloadable articles in the group.
1166 Optional arg ALL, if non-nil, means to fetch all articles."
1169 (if all gnus-newsgroup-articles
1170 gnus-newsgroup-downloadable))
1171 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1173 (gnus-agent-while-plugged
1175 (error "No articles to download"))
1176 (gnus-agent-with-fetch
1177 (setq gnus-newsgroup-undownloaded
1178 (gnus-sorted-ndifference
1179 gnus-newsgroup-undownloaded
1180 (setq fetched-articles
1181 (gnus-agent-fetch-articles
1182 gnus-newsgroup-name articles)))))
1184 (dolist (article articles)
1185 (let ((was-marked-downloadable
1186 (memq article gnus-newsgroup-downloadable)))
1187 (cond (gnus-agent-mark-unread-after-downloaded
1188 (setq gnus-newsgroup-downloadable
1189 (delq article gnus-newsgroup-downloadable))
1191 (gnus-summary-mark-article article gnus-unread-mark))
1192 (was-marked-downloadable
1193 (gnus-summary-set-agent-mark article t)))
1194 (when (gnus-summary-goto-subject article nil t)
1195 (gnus-summary-update-download-mark article))))))
1198 (defun gnus-agent-fetch-selected-article ()
1199 "Fetch the current article as it is selected.
1200 This can be added to `gnus-select-article-hook' or
1201 `gnus-mark-article-hook'."
1202 (let ((gnus-command-method gnus-current-select-method))
1203 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1204 (when (gnus-agent-fetch-articles
1206 (list gnus-current-article))
1207 (setq gnus-newsgroup-undownloaded
1208 (delq gnus-current-article gnus-newsgroup-undownloaded))
1209 (gnus-summary-update-download-mark gnus-current-article)))))
1212 ;;; Internal functions
1215 (defun gnus-agent-synchronize-group-flags (group actions server)
1216 "Update a plugged group by performing the indicated actions."
1217 (let* ((gnus-command-method (gnus-server-to-method server))
1219 ;; This initializer is required as gnus-request-set-mark
1220 ;; calls gnus-group-real-name to strip off the host name
1221 ;; before calling the backend. Now that the backend is
1222 ;; trying to call gnus-request-set-mark, I have to
1223 ;; reconstruct the original group name.
1224 (or (gnus-get-info group)
1226 (setq group (gnus-group-full-name
1227 group gnus-command-method))))))
1228 (gnus-request-set-mark group actions)
1231 (dolist (action actions)
1232 (let ((range (nth 0 action))
1233 (what (nth 1 action))
1234 (marks (nth 2 action)))
1235 (dolist (mark marks)
1236 (cond ((eq mark 'read)
1239 (funcall (if (eq what 'add)
1241 'gnus-remove-from-range)
1242 (gnus-info-read info)
1244 (gnus-get-unread-articles-in-group
1246 (gnus-active (gnus-info-group info))))
1247 ((memq mark '(tick))
1248 (let ((info-marks (assoc mark (gnus-info-marks info))))
1250 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1251 (setcdr info-marks (funcall (if (eq what 'add)
1253 'gnus-remove-from-range)
1257 ;;Marks can be synchronized at any time by simply toggling from
1258 ;;unplugged to plugged. If that is what is happening right now, make
1259 ;;sure that the group buffer is up to date.
1260 (when (gnus-buffer-live-p gnus-group-buffer)
1261 (gnus-group-update-group group t)))
1264 (defun gnus-agent-save-active (method)
1265 (when (gnus-agent-method-p method)
1266 (let* ((gnus-command-method method)
1267 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1268 (file (gnus-agent-lib-file "active")))
1269 (gnus-active-to-gnus-format nil new)
1270 (gnus-agent-write-active file new)
1272 (nnheader-insert-file-contents file))))
1274 (defun gnus-agent-write-active (file new)
1275 (gnus-make-directory (file-name-directory file))
1276 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1277 ;; The hashtable contains real names of groups. However, do NOT
1278 ;; add the foreign server prefix as gnus-active-to-gnus-format
1279 ;; will add it while reading the file.
1280 (gnus-write-active-file file new nil)))
1283 (defun gnus-agent-possibly-alter-active (group active &optional info)
1284 "Possibly expand a group's active range to include articles
1285 downloaded into the agent."
1286 (let* ((gnus-command-method (or gnus-command-method
1287 (gnus-find-method-for-group group))))
1288 (when (gnus-agent-method-p gnus-command-method)
1289 (let* ((local (gnus-agent-get-local group))
1290 (active-min (or (car active) 0))
1291 (active-max (or (cdr active) 0))
1292 (agent-min (or (car local) active-min))
1293 (agent-max (or (cdr local) active-max)))
1295 (when (< agent-min active-min)
1296 (setcar active agent-min))
1298 (when (> agent-max active-max)
1299 (setcdr active agent-max))
1301 (when (and info (< agent-max (- active-min 100)))
1302 ;; I'm expanding the active range by such a large amount
1303 ;; that there is a gap of more than 100 articles between the
1304 ;; last article known to the agent and the first article
1305 ;; currently available on the server. This gap contains
1306 ;; articles that have been lost, mark them as read so that
1307 ;; gnus doesn't waste resources trying to fetch them.
1309 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1310 ;; want to modify the local file everytime someone restarts
1311 ;; gnus. The small gap will cause a tiny performance hit
1312 ;; when gnus tries, and fails, to retrieve the articles.
1313 ;; Still that should be smaller than opening a buffer,
1314 ;; printing this list to the buffer, and then writing it to a
1317 (let ((read (gnus-info-read info)))
1322 (list (cons (1+ agent-max)
1323 (1- active-min))))))
1325 ;; Lie about the agent's local range for this group to
1326 ;; disable the set read each time this server is opened.
1327 ;; NOTE: Opening this group will restore the valid local
1328 ;; range but it will also expand the local range to
1329 ;; incompass the new active range.
1330 (gnus-agent-set-local group agent-min (1- active-min)))))))
1332 (defun gnus-agent-save-group-info (method group active)
1333 "Update a single group's active range in the agent's copy of the server's active file."
1334 (when (gnus-agent-method-p method)
1335 (let* ((gnus-command-method (or method gnus-command-method))
1336 (coding-system-for-write nnheader-file-coding-system)
1337 (file-name-coding-system nnmail-pathname-coding-system)
1338 (file (gnus-agent-lib-file "active"))
1339 oactive-min oactive-max)
1340 (gnus-make-directory (file-name-directory file))
1341 (with-temp-file file
1342 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1343 (mm-disable-multibyte)
1344 (when (file-exists-p file)
1345 (nnheader-insert-file-contents file)
1347 (goto-char (point-min))
1348 (when (re-search-forward
1349 (concat "^" (regexp-quote group) " ") nil t)
1351 (setq oactive-max (read (current-buffer)) ;; max
1352 oactive-min (read (current-buffer)))) ;; min
1353 (gnus-delete-line)))
1355 (insert (format "%S %d %d y\n" (intern group)
1356 (max (or oactive-max (cdr active)) (cdr active))
1357 (min (or oactive-min (car active)) (car active))))
1358 (goto-char (point-max))
1359 (while (search-backward "\\." nil t)
1360 (delete-char 1)))))))
1362 (defun gnus-agent-get-group-info (method group)
1363 "Get a single group's active range in the agent's copy of the server's active file."
1364 (when (gnus-agent-method-p method)
1365 (let* ((gnus-command-method (or method gnus-command-method))
1366 (coding-system-for-write nnheader-file-coding-system)
1367 (file-name-coding-system nnmail-pathname-coding-system)
1368 (file (gnus-agent-lib-file "active"))
1369 oactive-min oactive-max)
1370 (gnus-make-directory (file-name-directory file))
1372 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1373 (mm-disable-multibyte)
1374 (when (file-exists-p file)
1375 (nnheader-insert-file-contents file)
1377 (goto-char (point-min))
1378 (when (re-search-forward
1379 (concat "^" (regexp-quote group) " ") nil t)
1381 (setq oactive-max (read (current-buffer)) ;; max
1382 oactive-min (read (current-buffer))) ;; min
1383 (cons oactive-min oactive-max))))))))
1385 (defun gnus-agent-group-path (group)
1386 "Translate GROUP into a file name."
1388 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1389 ;; The two methods must be kept synchronized, which is why
1390 ;; gnus-agent-group-pathname was added.
1393 (nnheader-translate-file-chars
1394 (nnheader-replace-duplicate-chars-in-string
1395 (nnheader-replace-chars-in-string
1396 (gnus-group-real-name (gnus-group-decoded-name group))
1399 (if (or nnmail-use-long-file-names
1400 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1402 (mm-encode-coding-string
1403 (nnheader-replace-chars-in-string group ?. ?/)
1404 nnmail-pathname-coding-system)))
1406 (defun gnus-agent-group-pathname (group)
1407 "Translate GROUP into a file name."
1408 ;; nnagent uses nnmail-group-pathname to read articles while
1409 ;; unplugged. The agent must, therefore, use the same directory
1411 (let ((gnus-command-method (or gnus-command-method
1412 (gnus-find-method-for-group group))))
1413 (nnmail-group-pathname (gnus-group-real-name
1414 (gnus-group-decoded-name group))
1415 (gnus-agent-directory))))
1417 (defun gnus-agent-get-function (method)
1418 (if (gnus-online method)
1423 (defun gnus-agent-covered-methods ()
1424 "Return the subset of methods that are covered by the agent."
1425 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1427 ;;; History functions
1429 (defun gnus-agent-history-buffer ()
1430 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1432 (defun gnus-agent-open-history ()
1434 (push (cons (gnus-agent-method)
1435 (set-buffer (gnus-get-buffer-create
1436 (format " *Gnus agent %s history*"
1437 (gnus-agent-method)))))
1438 gnus-agent-history-buffers)
1439 (mm-disable-multibyte) ;; everything is binary
1442 (let ((file (gnus-agent-lib-file "history")))
1443 (when (file-exists-p file)
1444 (nnheader-insert-file-contents file))
1445 (set (make-local-variable 'gnus-agent-file-name) file))))
1447 (defun gnus-agent-close-history ()
1448 (when (gnus-buffer-live-p gnus-agent-current-history)
1449 (kill-buffer gnus-agent-current-history)
1450 (setq gnus-agent-history-buffers
1451 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1452 gnus-agent-history-buffers))))
1458 (defun gnus-agent-fetch-articles (group articles)
1459 "Fetch ARTICLES from GROUP and put them into the Agent."
1461 (gnus-agent-load-alist group)
1462 (let* ((alist gnus-agent-article-alist)
1463 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1464 (selected-sets (list nil))
1465 (current-set-size 0)
1468 ;; Check each article
1469 (while (setq article (pop articles))
1470 ;; Skip alist entries preceeding this article
1471 (while (> article (or (caar alist) (1+ article)))
1472 (setq alist (cdr alist)))
1474 ;; Prune off articles that we have already fetched.
1475 (unless (and (eq article (caar alist))
1477 ;; Skip headers preceeding this article
1480 (let* ((header (car headers)))
1482 (mail-header-number header)
1484 (setq headers (cdr headers)))
1486 ;; Add this article to the current set
1487 (setcar selected-sets (cons article (car selected-sets)))
1489 ;; Update the set size, when the set is too large start a
1490 ;; new one. I do this after adding the article as I want at
1491 ;; least one article in each set.
1492 (when (< gnus-agent-max-fetch-size
1493 (setq current-set-size
1495 (if (= header-number article)
1496 (let ((char-size (mail-header-chars
1498 (if (<= char-size 0)
1499 ;; The char size was missing/invalid,
1500 ;; assume a worst-case situation of
1501 ;; 65 char/line. If the line count
1502 ;; is missing, arbitrarily assume a
1503 ;; size of 1000 characters.
1504 (max (* 65 (mail-header-lines
1509 (setcar selected-sets (nreverse (car selected-sets)))
1510 (setq selected-sets (cons nil selected-sets)
1511 current-set-size 0))))
1513 (when (or (cdr selected-sets) (car selected-sets))
1514 (let* ((fetched-articles (list nil))
1515 (tail-fetched-articles fetched-articles)
1516 (dir (gnus-agent-group-pathname group))
1517 (date (time-to-days (current-time)))
1518 (case-fold-search t)
1521 (setcar selected-sets (nreverse (car selected-sets)))
1522 (setq selected-sets (nreverse selected-sets))
1524 (gnus-make-directory dir)
1525 (gnus-message 7 "Fetching articles for %s..." group)
1528 (while (setq articles (pop selected-sets))
1529 ;; Fetch the articles from the backend.
1530 (if (gnus-check-backend-function 'retrieve-articles group)
1531 (setq pos (gnus-retrieve-articles articles group))
1534 (while (setq article (pop articles))
1535 (gnus-message 10 "Fetching article %s for %s..."
1538 (gnus-backlog-request-article group article
1540 (gnus-request-article article group))
1541 (goto-char (point-max))
1542 (push (cons article (point)) pos)
1543 (insert-buffer-substring nntp-server-buffer)))
1545 nntp-server-buffer (point-min) (point-max))
1546 (setq pos (nreverse pos)))))
1547 ;; Then save these articles into the Agent.
1549 (set-buffer nntp-server-buffer)
1551 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1552 (goto-char (point-min))
1553 (unless (eobp) ;; Don't save empty articles.
1554 (when (search-forward "\n\n" nil t)
1555 (when (search-backward "\nXrefs: " nil t)
1556 ;; Handle cross posting.
1557 (goto-char (match-end 0)) ; move to end of header name
1558 (skip-chars-forward "^ ") ; skip server name
1559 (skip-chars-forward " ")
1561 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1562 (push (cons (buffer-substring (match-beginning 1)
1565 (buffer-substring (match-beginning 2)
1568 (goto-char (match-end 0)))
1569 (gnus-agent-crosspost crosses (caar pos) date)))
1570 (goto-char (point-min))
1571 (if (not (re-search-forward
1572 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1573 (setq id "No-Message-ID-in-article")
1574 (setq id (buffer-substring
1575 (match-beginning 1) (match-end 1))))
1576 (let ((coding-system-for-write
1577 gnus-agent-file-coding-system))
1578 (write-region (point-min) (point-max)
1579 (concat dir (number-to-string (caar pos)))
1582 (gnus-agent-append-to-list
1583 tail-fetched-articles (caar pos)))
1585 (setq pos (cdr pos)))))
1587 (gnus-agent-save-alist group (cdr fetched-articles) date)
1588 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1590 (gnus-message 7 ""))
1591 (cdr fetched-articles))))))
1593 (defun gnus-agent-unfetch-articles (group articles)
1594 "Delete ARTICLES that were fetched from GROUP into the agent."
1596 (gnus-agent-with-refreshed-group
1598 (gnus-agent-load-alist group)
1599 (let* ((alist (cons nil gnus-agent-article-alist))
1600 (articles (sort articles #'<))
1601 (next-possibility alist)
1602 (delete-this (pop articles)))
1603 (while (and (cdr next-possibility) delete-this)
1604 (let ((have-this (caar (cdr next-possibility))))
1605 (cond ((< delete-this have-this)
1606 (setq delete-this (pop articles)))
1607 ((= delete-this have-this)
1608 (let ((timestamp (cdar (cdr next-possibility))))
1610 (let* ((file-name (concat (gnus-agent-group-pathname group)
1611 (number-to-string have-this)))
1612 (size-file (float (or (and gnus-agent-total-fetched-hashtb
1613 (nth 7 (file-attributes file-name)))
1615 (delete-file file-name)
1616 (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1618 (setcdr next-possibility (cddr next-possibility)))
1620 (setq next-possibility (cdr next-possibility))))))
1621 (setq gnus-agent-article-alist (cdr alist))
1622 (gnus-agent-save-alist group)))))
1624 (defun gnus-agent-crosspost (crosses article &optional date)
1625 (setq date (or date t))
1627 (let (gnus-agent-article-alist group alist beg end)
1629 (set-buffer gnus-agent-overview-buffer)
1630 (when (nnheader-find-nov-line article)
1633 (setq end (progn (forward-line 1) (point)))))
1635 (setq group (caar crosses))
1636 (unless (setq alist (assoc group gnus-agent-group-alist))
1637 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1638 gnus-agent-group-alist))
1639 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1641 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1643 (when (= (point-max) (point-min))
1644 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1646 (nnheader-insert-file-contents
1647 (gnus-agent-article-name ".overview" group))))
1648 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1649 (insert (string-to-number (cdar crosses)))
1650 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1651 (gnus-agent-check-overview-buffer))
1652 (setq crosses (cdr crosses)))))
1654 (defun gnus-agent-backup-overview-buffer ()
1655 (when gnus-newsgroup-name
1656 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1659 (while (file-exists-p
1660 (setq name (concat root "~"
1661 (int-to-string (setq cnt (1+ cnt))) "~"))))
1662 (write-region (point-min) (point-max) name nil 'no-msg)
1663 (gnus-message 1 "Created backup copy of overview in %s." name)))
1666 (defun gnus-agent-check-overview-buffer (&optional buffer)
1667 "Check the overview file given for sanity.
1668 In particular, checks that the file is sorted by article number
1669 and that there are no duplicates."
1674 (set-buffer buffer))
1677 (goto-char (point-min))
1679 (while (< (point) (point-max))
1681 (cur (condition-case nil
1682 (read (current-buffer))
1685 ((or (not (integerp cur))
1686 (not (eq (char-after) ?\t)))
1688 (setq backed-up (gnus-agent-backup-overview-buffer)))
1690 "Overview buffer contains garbage '%s'."
1695 (setq backed-up (gnus-agent-backup-overview-buffer)))
1697 "Duplicate overview line for %d" cur)
1698 (delete-region p (progn (forward-line 1) (point))))
1701 (setq backed-up (gnus-agent-backup-overview-buffer)))
1702 (gnus-message 1 "Overview buffer not sorted!")
1703 (sort-numeric-fields 1 (point-min) (point-max))
1704 (goto-char (point-min))
1707 (setq prev-num cur)))
1708 (forward-line 1)))))))
1710 (defun gnus-agent-flush-cache ()
1712 (while gnus-agent-buffer-alist
1713 (set-buffer (cdar gnus-agent-buffer-alist))
1714 (let ((coding-system-for-write
1715 gnus-agent-file-coding-system))
1716 (write-region (point-min) (point-max)
1717 (gnus-agent-article-name ".overview"
1718 (caar gnus-agent-buffer-alist))
1720 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1721 (while gnus-agent-group-alist
1722 (with-temp-file (gnus-agent-article-name
1723 ".agentview" (caar gnus-agent-group-alist))
1724 (princ (cdar gnus-agent-group-alist))
1726 (princ 1 (current-buffer))
1728 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1731 (defun gnus-agent-find-parameter (group symbol)
1732 "Search for GROUPs SYMBOL in the group's parameters, the group's
1733 topic parameters, the group's category, or the customizable
1734 variables. Returns the first non-nil value found."
1735 (or (gnus-group-find-parameter group symbol t)
1736 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1740 '((agent-short-article . gnus-agent-short-article)
1741 (agent-long-article . gnus-agent-long-article)
1742 (agent-low-score . gnus-agent-low-score)
1743 (agent-high-score . gnus-agent-high-score)
1744 (agent-days-until-old . gnus-agent-expire-days)
1745 (agent-enable-expiration
1746 . gnus-agent-enable-expiration)
1747 (agent-predicate . gnus-agent-predicate)))))))
1749 (defun gnus-agent-fetch-headers (group &optional force)
1750 "Fetch interesting headers into the agent. The group's overview
1751 file will be updated to include the headers while a list of available
1752 article numbers will be returned."
1753 (let* ((fetch-all (and gnus-agent-consider-all-articles
1754 ;; Do not fetch all headers if the predicate
1755 ;; implies that we only consider unread articles.
1756 (not (gnus-predicate-implies-unread
1757 (gnus-agent-find-parameter group
1758 'agent-predicate)))))
1759 (articles (if fetch-all
1760 (gnus-uncompress-range (gnus-active group))
1761 (gnus-list-of-unread-articles group)))
1762 (gnus-decode-encoded-word-function 'identity)
1763 (file (gnus-agent-article-name ".overview" group)))
1766 ;; Add articles with marks to the list of article headers we want to
1767 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1768 ;; mark, but do fetch recent or seen articles if they have other, more
1769 ;; interesting marks. (We have to fetch articles with boring marks
1770 ;; because otherwise the agent will remove their marks.)
1771 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1772 (unless (memq (car arts) '(seen recent killed cache))
1773 (setq articles (gnus-range-add articles (cdr arts)))))
1774 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1776 ;; At this point, I have the list of articles to consider for
1777 ;; fetching. This is the list that I'll return to my caller. Some
1778 ;; of these articles may have already been fetched. That's OK as
1779 ;; the fetch article code will filter those out. Internally, I'll
1780 ;; filter this list to just those articles whose headers need to
1782 (let ((articles articles))
1783 ;; Remove known articles.
1784 (when (and (or gnus-agent-cache
1786 (gnus-agent-load-alist group))
1787 ;; Remove articles marked as downloaded.
1789 ;; I want to fetch all headers in the active range.
1790 ;; Therefore, exclude only those headers that are in the
1792 ;; NOTE: This is probably NOT what I want to do after
1793 ;; agent expiration in this group.
1794 (setq articles (gnus-agent-uncached-articles articles group))
1796 ;; I want to only fetch those headers that have never been
1797 ;; fetched. Therefore, exclude all headers that are, or
1798 ;; WERE, in the article alist.
1799 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1800 (high (cdr (gnus-active group))))
1801 ;; Low can be greater than High when the same group is
1802 ;; fetched twice in the same session {The first fetch will
1803 ;; fill the article alist such that (last
1804 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1805 ;; group))}. The addition of one(the 1+ above) then
1806 ;; forces Low to be greater than High. When this happens,
1807 ;; gnus-list-range-intersection returns nil which
1808 ;; indicates that no headers need to be fetched. -- Kevin
1809 (setq articles (gnus-list-range-intersection
1810 articles (list (cons low high)))))))
1813 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1814 (gnus-compress-sequence articles t))
1817 (set-buffer nntp-server-buffer)
1821 (gnus-message 7 "Fetching headers for %s..." group)
1824 (gnus-make-directory (nnheader-translate-file-chars
1825 (file-name-directory file) t))
1827 (unless (eq 'nov (gnus-retrieve-headers articles group))
1828 (nnvirtual-convert-headers))
1829 (gnus-agent-check-overview-buffer)
1830 ;; Move these headers to the overview buffer so that
1831 ;; gnus-agent-braid-nov can merge them with the contents
1834 gnus-agent-overview-buffer (point-min) (point-max))
1835 ;; NOTE: Call g-a-brand-nov even when the file does not
1836 ;; exist. As a minimum, it will validate the article
1837 ;; numbers already in the buffer.
1838 (gnus-agent-braid-nov group articles file)
1839 (let ((coding-system-for-write
1840 gnus-agent-file-coding-system))
1841 (gnus-agent-check-overview-buffer)
1842 (write-region (point-min) (point-max) file nil 'silent))
1843 (gnus-agent-update-view-total-fetched-for group t)
1844 (gnus-agent-save-alist group articles nil)
1848 (nnheader-insert-file-contents file)))))
1851 (defsubst gnus-agent-read-article-number ()
1852 "Reads the article number at point. Returns nil when a valid article number can not be read."
1854 ;; It is unfortunite but the read function quietly overflows
1855 ;; integer. As a result, I have to use string operations to test
1856 ;; for overflow BEFORE calling read.
1857 (when (looking-at "[0-9]+\t")
1858 (let ((len (- (match-end 0) (match-beginning 0))))
1860 (read (current-buffer)))
1862 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1863 ;; Back convert from int to string to ensure that this is one of them.
1864 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1865 (num (read (current-buffer)))
1866 (str2 (int-to-string num)))
1867 (when (equal str1 str2)
1870 (defsubst gnus-agent-copy-nov-line (article)
1871 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1873 (set-buffer gnus-agent-overview-buffer)
1874 (while (and (not (eobp))
1875 (or (not (setq art (gnus-agent-read-article-number)))
1880 (not (eq article art)))
1881 (set-buffer nntp-server-buffer)
1883 (setq e (progn (forward-line 1) (point)))
1884 (set-buffer nntp-server-buffer)
1885 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1887 (defun gnus-agent-braid-nov (group articles file)
1888 "Merge agent overview data with given file.
1889 Takes unvalidated headers for ARTICLES from
1890 `gnus-agent-overview-buffer' and validated headers from the given
1891 FILE and places the combined valid headers into
1892 `nntp-server-buffer'. This function can be used, when file
1893 doesn't exist, to valid the overview buffer."
1895 (set-buffer gnus-agent-overview-buffer)
1896 (goto-char (point-min))
1897 (set-buffer nntp-server-buffer)
1899 (when (file-exists-p file)
1900 (nnheader-insert-file-contents file))
1901 (goto-char (point-max))
1904 (unless (or (= (point-min) (point-max))
1905 (< (setq last (read (current-buffer))) (car articles)))
1906 ;; Old and new overlap -- We do it the hard way.
1907 (when (nnheader-find-nov-line (car articles))
1908 ;; Replacing existing NOV entry
1909 (delete-region (point) (progn (forward-line 1) (point))))
1910 (gnus-agent-copy-nov-line (pop articles))
1914 (while (let ((art (read (current-buffer))))
1915 (cond ((< art (car articles))
1918 ((= art (car articles))
1921 (point) (progn (forward-line 1) (point)))
1927 (gnus-agent-copy-nov-line (pop articles)))))
1929 (goto-char (point-max))
1931 ;; Append the remaining lines
1934 (set-buffer gnus-agent-overview-buffer)
1935 (setq start (point))
1936 (set-buffer nntp-server-buffer))
1939 (insert-buffer-substring gnus-agent-overview-buffer start)
1942 (setq last (or last -134217728))
1945 (setq art (gnus-agent-read-article-number))
1947 ;; Bad art num - delete this line
1949 (delete-region (point) (progn (forward-line 1) (point))))
1951 ;; Art num out of order - enable sort
1959 (sort-numeric-fields 1 (point-min) (point-max)))))))
1961 ;; Keeps the compiler from warning about the free variable in
1962 ;; gnus-agent-read-agentview.
1964 (defvar gnus-agent-read-agentview))
1966 (defun gnus-agent-load-alist (group)
1967 "Load the article-state alist for GROUP."
1968 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1969 (let ((gnus-agent-read-agentview group))
1970 (setq gnus-agent-article-alist
1971 (gnus-cache-file-contents
1972 (gnus-agent-article-name ".agentview" group)
1973 'gnus-agent-file-loading-cache
1974 'gnus-agent-read-agentview))))
1976 ;; Save format may be either 1 or 2. Two is the new, compressed
1977 ;; format that is still being tested. Format 1 is uncompressed but
1978 ;; known to be reliable.
1979 (defconst gnus-agent-article-alist-save-format 2)
1981 (defun gnus-agent-read-agentview (file)
1982 "Load FILE and do a `read' there."
1986 (nnheader-insert-file-contents file)
1987 (goto-char (point-min))
1988 (let ((alist (read (current-buffer)))
1989 (version (condition-case nil (read (current-buffer))
1995 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version))
1997 (let ((inhibit-quit t)
1999 (gnus-agent-open-history)
2000 (set-buffer (gnus-agent-history-buffer))
2001 (goto-char (point-min))
2003 (if (and (looking-at
2004 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2005 (string= (match-string 2)
2006 gnus-agent-read-agentview)
2007 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2008 (setcdr entry (string-to-number (match-string 1))))
2010 (gnus-agent-close-history)
2011 (setq changed-version t)))
2013 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2018 (let ((state (car comp-list))
2020 (gnus-uncompress-range
2022 (mapcar (lambda (article-id)
2023 (setq uncomp (cons (cons article-id state) uncomp)))
2026 (setq alist (sort uncomp 'car-less-than-car)))))
2027 (when changed-version
2028 (let ((gnus-agent-article-alist alist))
2029 (gnus-agent-save-alist gnus-agent-read-agentview)))
2033 (defun gnus-agent-save-alist (group &optional articles state)
2034 "Save the article-state alist for GROUP."
2035 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2036 (prev (cons nil gnus-agent-article-alist))
2038 print-level print-length item article)
2039 (while (setq article (pop articles))
2040 (while (and (cdr prev)
2041 (< (caadr prev) article))
2042 (setq prev (cdr prev)))
2045 (setcdr prev (list (cons article state))))
2046 ((> (caadr prev) article)
2047 (setcdr prev (cons (cons article state) (cdr prev))))
2048 ((= (caadr prev) article)
2049 (setcdr (cadr prev) state)))
2050 (setq prev (cdr prev)))
2051 (setq gnus-agent-article-alist (cdr all))
2053 (gnus-agent-set-local group
2054 (caar gnus-agent-article-alist)
2055 (caar (last gnus-agent-article-alist)))
2057 (gnus-make-directory (gnus-agent-article-name "" group))
2058 (with-temp-file (gnus-agent-article-name ".agentview" group)
2059 (cond ((eq gnus-agent-article-alist-save-format 1)
2060 (princ gnus-agent-article-alist (current-buffer)))
2061 ((eq gnus-agent-article-alist-save-format 2)
2062 (let ((compressed nil))
2063 (mapcar (lambda (pair)
2064 (let* ((article-id (car pair))
2065 (day-of-download (cdr pair))
2066 (comp-list (assq day-of-download compressed)))
2069 (cons article-id (cdr comp-list)))
2071 (cons (list day-of-download article-id)
2073 nil)) gnus-agent-article-alist)
2074 (mapcar (lambda (comp-list)
2076 (gnus-compress-sequence
2077 (nreverse (cdr comp-list)))))
2079 (princ compressed (current-buffer)))))
2081 (princ gnus-agent-article-alist-save-format (current-buffer))
2084 (gnus-agent-update-view-total-fetched-for group nil)))
2086 (defvar gnus-agent-article-local nil)
2087 (defvar gnus-agent-file-loading-local nil)
2089 (defun gnus-agent-load-local (&optional method)
2090 "Load the METHOD'S local file. The local file contains min/max
2091 article counts for each of the method's subscribed groups."
2092 (let ((gnus-command-method (or method gnus-command-method)))
2093 (setq gnus-agent-article-local
2094 (gnus-cache-file-contents
2095 (gnus-agent-lib-file "local")
2096 'gnus-agent-file-loading-local
2097 'gnus-agent-read-and-cache-local))))
2099 (defun gnus-agent-read-and-cache-local (file)
2100 "Load and read FILE then bind its contents to
2101 gnus-agent-article-local. If that variable had `dirty' (also known as
2102 modified) original contents, they are first saved to their own file."
2104 (if (and gnus-agent-article-local
2105 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2106 (gnus-agent-save-local))
2107 (gnus-agent-read-local file))
2109 (defun gnus-agent-read-local (file)
2110 "Load FILE and do a `read' there."
2111 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2116 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2117 (nnheader-insert-file-contents file))
2120 (goto-char (point-min))
2121 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2122 (while (= (following-char) ?\;)
2124 (setq line (1+ line)))
2131 (cur (current-buffer)))
2132 (setq group (read cur)
2136 (when (stringp group)
2137 (setq group (intern group my-obarray)))
2139 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2140 (set group (cons (+ 0 min) (+ 0 max))))
2142 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2143 file line (error-message-string err))))
2145 (setq line (1+ line))))
2147 (set (intern "+dirty" my-obarray) nil)
2148 (set (intern "+method" my-obarray) gnus-command-method)
2151 (defun gnus-agent-save-local (&optional force)
2152 "Save gnus-agent-article-local under it method's agent.lib directory."
2153 (let ((my-obarray gnus-agent-article-local))
2154 (when (and my-obarray
2155 (or force (symbol-value (intern "+dirty" my-obarray))))
2156 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2157 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2158 (dest (gnus-agent-lib-file "local")))
2159 (gnus-make-directory (gnus-agent-lib-file ""))
2161 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2162 (with-temp-file dest
2163 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2164 (file-name-coding-system nnmail-pathname-coding-system)
2165 print-level print-length item article
2166 (standard-output (current-buffer)))
2167 (mapatoms (lambda (symbol)
2168 (cond ((not (boundp symbol))
2170 ((member (symbol-name symbol) '("+dirty" "+method"))
2174 (let ((range (symbol-value symbol)))
2182 (defun gnus-agent-get-local (group &optional gmane method)
2183 (let* ((gmane (or gmane (gnus-group-real-name group)))
2184 (gnus-command-method (or method (gnus-find-method-for-group group)))
2185 (local (gnus-agent-load-local))
2186 (symb (intern gmane local))
2187 (minmax (and (boundp symb) (symbol-value symb))))
2189 ;; Bind these so that gnus-agent-load-alist doesn't change the
2190 ;; current alist (i.e. gnus-agent-article-alist)
2191 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2192 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2193 (alist (gnus-agent-load-alist group)))
2197 (caar (last alist))))
2198 (gnus-agent-set-local group (car minmax) (cdr minmax)
2199 gmane gnus-command-method local))))
2202 (defun gnus-agent-set-local (group min max &optional gmane method local)
2203 (let* ((gmane (or gmane (gnus-group-real-name group)))
2204 (gnus-command-method (or method (gnus-find-method-for-group group)))
2205 (local (or local (gnus-agent-load-local)))
2206 (symb (intern gmane local))
2207 (minmax (and (boundp symb) (symbol-value symb))))
2209 (if (cond ((and minmax
2210 (or (not (eq min (car minmax)))
2211 (not (eq max (cdr minmax)))))
2218 (set symb (cons min max))
2221 (unintern symb local)))
2222 (set (intern "+dirty" local) t))))
2224 (defun gnus-agent-article-name (article group)
2225 (expand-file-name article
2226 (file-name-as-directory
2227 (gnus-agent-group-pathname group))))
2229 (defun gnus-agent-batch-confirmation (msg)
2230 "Show error message and return t."
2231 (gnus-message 1 msg)
2235 (defun gnus-agent-batch-fetch ()
2236 "Start Gnus and fetch session."
2239 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2240 (gnus-agent-fetch-session))
2243 (defun gnus-agent-fetch-session ()
2244 "Fetch all articles and headers that are eligible for fetching."
2246 (unless gnus-agent-covered-methods
2247 (error "No servers are covered by the Gnus agent"))
2248 (unless gnus-plugged
2249 (error "Can't fetch articles while Gnus is unplugged"))
2250 (let ((methods (gnus-agent-covered-methods))
2251 groups group gnus-command-method)
2254 (setq gnus-command-method (car methods))
2255 (when (and (or (gnus-server-opened gnus-command-method)
2256 (gnus-open-server gnus-command-method))
2257 (gnus-online gnus-command-method))
2258 (setq groups (gnus-groups-from-server (car methods)))
2259 (gnus-agent-with-fetch
2260 (while (setq group (pop groups))
2261 (when (<= (gnus-group-level group)
2262 gnus-agent-handle-level)
2263 (if (or debug-on-error debug-on-quit)
2264 (gnus-agent-fetch-group-1
2265 group gnus-command-method)
2267 (gnus-agent-fetch-group-1
2268 group gnus-command-method)
2270 (unless (funcall gnus-agent-confirmation-function
2271 (format "Error %s while fetching session. Should gnus continue? "
2272 (error-message-string err)))
2273 (error "Cannot fetch articles into the Gnus agent")))
2275 (gnus-agent-regenerate-group group)
2276 (unless (funcall gnus-agent-confirmation-function
2278 "%s while fetching session. Should gnus continue? "
2279 (error-message-string err)))
2281 "Cannot fetch articles into the Gnus agent")))))))))
2282 (setq methods (cdr methods)))
2283 (gnus-run-hooks 'gnus-agent-fetched-hook)
2284 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2286 (defun gnus-agent-fetch-group-1 (group method)
2288 (let ((gnus-command-method method)
2289 (gnus-newsgroup-name group)
2290 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2291 (gnus-newsgroup-headers gnus-newsgroup-headers)
2292 (gnus-newsgroup-scored gnus-newsgroup-scored)
2293 (gnus-use-cache gnus-use-cache)
2294 (gnus-summary-expunge-below gnus-summary-expunge-below)
2295 (gnus-summary-mark-below gnus-summary-mark-below)
2296 (gnus-orphan-score gnus-orphan-score)
2297 ;; Maybe some other gnus-summary local variables should also
2303 category predicate info marks score-param
2305 (unless (gnus-check-group group)
2306 (error "Can't open server for %s" group))
2309 (when (or gnus-newsgroup-active
2311 (gnus-activate-group group))
2312 (let ((marked-articles gnus-newsgroup-downloadable))
2313 ;; Identify the articles marked for download
2314 (unless gnus-newsgroup-active
2315 ;; The variable gnus-newsgroup-active was selected as I need
2316 ;; a gnus-summary local variable that is NOT bound to any
2317 ;; value (its global value should default to nil).
2318 (dolist (mark gnus-agent-download-marks)
2319 (let ((arts (cdr (assq mark (gnus-info-marks
2320 (setq info (gnus-get-info group)))))))
2322 (setq marked-articles (nconc (gnus-uncompress-range arts)
2325 (setq marked-articles (sort marked-articles '<))
2327 ;; Fetch any new articles from the server
2328 (setq articles (gnus-agent-fetch-headers group))
2330 ;; Merge new articles with marked
2331 (setq articles (sort (append marked-articles articles) '<))
2334 ;; Parse them and see which articles we want to fetch.
2335 (setq gnus-newsgroup-dependencies
2336 (or gnus-newsgroup-dependencies
2337 (make-vector (length articles) 0)))
2338 (setq gnus-newsgroup-headers
2339 (or gnus-newsgroup-headers
2340 (gnus-get-newsgroup-headers-xover articles nil nil
2342 ;; `gnus-agent-overview-buffer' may be killed for
2343 ;; timeout reason. If so, recreate it.
2344 (gnus-agent-create-buffer)
2346 ;; Figure out how to select articles in this group
2347 (setq category (gnus-group-category group))
2351 (gnus-agent-find-parameter group 'agent-predicate)))
2353 ;; If the selection predicate requires scoring, score each header
2354 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2356 (gnus-agent-find-parameter group 'agent-score-file)))
2357 ;; Translate score-param into real one
2360 ((eq score-param 'file)
2361 (setq score-param (gnus-all-score-files group)))
2362 ((stringp (car score-param)))
2364 (setq score-param (list (list score-param)))))
2366 (gnus-score-headers score-param))))
2368 (unless (and (eq predicate 'gnus-agent-false)
2369 (not marked-articles))
2370 (let ((arts (list nil)))
2371 (let ((arts-tail arts)
2372 (alist (gnus-agent-load-alist group))
2373 (marked-articles marked-articles)
2374 (gnus-newsgroup-headers gnus-newsgroup-headers))
2375 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2376 (let ((num (mail-header-number gnus-headers)))
2377 ;; Determine if this article is already in the cache
2379 (> num (caar alist)))
2380 (setq alist (cdr alist)))
2382 (unless (and (eq num (caar alist))
2385 ;; Determine if this article was marked for download.
2386 (while (and marked-articles
2387 (> num (car marked-articles)))
2388 (setq marked-articles
2389 (cdr marked-articles)))
2391 ;; When this article is marked, or selected by the
2392 ;; predicate, add it to the download list
2393 (when (or (eq num (car marked-articles))
2396 (assq num gnus-newsgroup-scored))
2397 gnus-summary-default-score))
2398 (gnus-agent-long-article
2399 (gnus-agent-find-parameter
2400 group 'agent-long-article))
2401 (gnus-agent-short-article
2402 (gnus-agent-find-parameter
2403 group 'agent-short-article))
2404 (gnus-agent-low-score
2405 (gnus-agent-find-parameter
2406 group 'agent-low-score))
2407 (gnus-agent-high-score
2408 (gnus-agent-find-parameter
2409 group 'agent-high-score))
2410 (gnus-agent-expire-days
2411 (gnus-agent-find-parameter
2412 group 'agent-days-until-old)))
2413 (funcall predicate)))
2414 (gnus-agent-append-to-list arts-tail num))))))
2416 (let (fetched-articles)
2417 ;; Fetch all selected articles
2418 (setq gnus-newsgroup-undownloaded
2419 (gnus-sorted-ndifference
2420 gnus-newsgroup-undownloaded
2421 (setq fetched-articles
2423 (gnus-agent-fetch-articles group (cdr arts))
2426 (let ((unfetched-articles
2427 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2428 (if gnus-newsgroup-active
2429 ;; Update the summary buffer
2431 (dolist (article marked-articles)
2432 (gnus-summary-set-agent-mark article t))
2433 (dolist (article fetched-articles)
2434 (when gnus-agent-mark-unread-after-downloaded
2435 (setq gnus-newsgroup-downloadable
2436 (delq article gnus-newsgroup-downloadable))
2437 (gnus-summary-mark-article
2438 article gnus-unread-mark))
2439 (when (gnus-summary-goto-subject article nil t)
2440 (gnus-summary-update-download-mark article)))
2441 (dolist (article unfetched-articles)
2442 (gnus-summary-mark-article
2443 article gnus-canceled-mark)))
2445 ;; Update the group buffer.
2447 ;; When some, or all, of the marked articles came
2448 ;; from the download mark. Remove that mark. I
2449 ;; didn't do this earlier as I only want to remove
2450 ;; the marks after the fetch is completed.
2452 (dolist (mark gnus-agent-download-marks)
2453 (when (eq mark 'download)
2455 (assq mark (gnus-info-marks
2456 (setq info (gnus-get-info group))))))
2457 (when (cdr marked-arts)
2459 (delq marked-arts (gnus-info-marks info)))
2460 (gnus-info-set-marks info marks)))))
2461 (let ((read (gnus-info-read
2462 (or info (setq info (gnus-get-info group))))))
2464 info (gnus-add-to-range read unfetched-articles)))
2466 (gnus-group-update-group group t)
2470 (concat "(gnus-group-set-info '"
2471 (gnus-prin1-to-string info)
2475 ;;; Agent Category Mode
2478 (defvar gnus-category-mode-hook nil
2479 "Hook run in `gnus-category-mode' buffers.")
2481 (defvar gnus-category-line-format " %(%20c%): %g\n"
2482 "Format of category lines.
2484 Valid specifiers include:
2485 %c Topic name (string)
2486 %g The number of groups in the topic (integer)
2488 General format specifiers can also be used. See Info node
2489 `(gnus)Formatting Variables'.")
2491 (defvar gnus-category-mode-line-format "Gnus: %%b"
2492 "The format specification for the category mode line.")
2494 (defvar gnus-agent-predicate 'false
2495 "The selection predicate used when no other source is available.")
2497 (defvar gnus-agent-short-article 100
2498 "Articles that have fewer lines than this are short.")
2500 (defvar gnus-agent-long-article 200
2501 "Articles that have more lines than this are long.")
2503 (defvar gnus-agent-low-score 0
2504 "Articles that have a score lower than this have a low score.")
2506 (defvar gnus-agent-high-score 0
2507 "Articles that have a score higher than this have a high score.")
2510 ;;; Internal variables.
2512 (defvar gnus-category-buffer "*Agent Category*")
2514 (defvar gnus-category-line-format-alist
2515 `((?c gnus-tmp-name ?s)
2516 (?g gnus-tmp-groups ?d)))
2518 (defvar gnus-category-mode-line-format-alist
2519 `((?u user-defined ?s)))
2521 (defvar gnus-category-line-format-spec nil)
2522 (defvar gnus-category-mode-line-format-spec nil)
2524 (defvar gnus-category-mode-map nil)
2525 (put 'gnus-category-mode 'mode-class 'special)
2527 (unless gnus-category-mode-map
2528 (setq gnus-category-mode-map (make-sparse-keymap))
2529 (suppress-keymap gnus-category-mode-map)
2531 (gnus-define-keys gnus-category-mode-map
2532 "q" gnus-category-exit
2533 "k" gnus-category-kill
2534 "c" gnus-category-copy
2535 "a" gnus-category-add
2536 "e" gnus-agent-customize-category
2537 "p" gnus-category-edit-predicate
2538 "g" gnus-category-edit-groups
2539 "s" gnus-category-edit-score
2540 "l" gnus-category-list
2542 "\C-c\C-i" gnus-info-find-node
2543 "\C-c\C-b" gnus-bug))
2545 (defvar gnus-category-menu-hook nil
2546 "*Hook run after the creation of the menu.")
2548 (defun gnus-category-make-menu-bar ()
2549 (gnus-turn-off-edit-menu 'category)
2550 (unless (boundp 'gnus-category-menu)
2552 gnus-category-menu gnus-category-mode-map ""
2554 ["Add" gnus-category-add t]
2555 ["Kill" gnus-category-kill t]
2556 ["Copy" gnus-category-copy t]
2557 ["Edit category" gnus-agent-customize-category t]
2558 ["Edit predicate" gnus-category-edit-predicate t]
2559 ["Edit score" gnus-category-edit-score t]
2560 ["Edit groups" gnus-category-edit-groups t]
2561 ["Exit" gnus-category-exit t]))
2563 (gnus-run-hooks 'gnus-category-menu-hook)))
2565 (defun gnus-category-mode ()
2566 "Major mode for listing and editing agent categories.
2568 All normal editing commands are switched off.
2569 \\<gnus-category-mode-map>
2570 For more in-depth information on this mode, read the manual
2571 \(`\\[gnus-info-find-node]').
2573 The following commands are available:
2575 \\{gnus-category-mode-map}"
2577 (when (gnus-visual-p 'category-menu 'menu)
2578 (gnus-category-make-menu-bar))
2579 (kill-all-local-variables)
2580 (gnus-simplify-mode-line)
2581 (setq major-mode 'gnus-category-mode)
2582 (setq mode-name "Category")
2583 (gnus-set-default-directory)
2584 (setq mode-line-process nil)
2585 (use-local-map gnus-category-mode-map)
2586 (buffer-disable-undo)
2587 (setq truncate-lines t)
2588 (setq buffer-read-only t)
2589 (gnus-run-hooks 'gnus-category-mode-hook))
2591 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2593 (defun gnus-category-insert-line (category)
2594 (let* ((gnus-tmp-name (format "%s" (car category)))
2595 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2597 (gnus-add-text-properties
2601 (eval gnus-category-line-format-spec))
2602 (list 'gnus-category gnus-tmp-name))))
2604 (defun gnus-enter-category-buffer ()
2605 "Go to the Category buffer."
2607 (gnus-category-setup-buffer)
2608 (gnus-configure-windows 'category)
2609 (gnus-category-prepare))
2611 (defun gnus-category-setup-buffer ()
2612 (unless (get-buffer gnus-category-buffer)
2614 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2615 (gnus-category-mode))))
2617 (defun gnus-category-prepare ()
2618 (gnus-set-format 'category-mode)
2619 (gnus-set-format 'category t)
2620 (let ((alist gnus-category-alist)
2621 (buffer-read-only nil))
2624 (gnus-category-insert-line (pop alist)))
2625 (goto-char (point-min))
2626 (gnus-category-position-point)))
2628 (defun gnus-category-name ()
2629 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2630 (error "No category on the current line")))
2632 (defun gnus-category-read ()
2633 "Read the category alist."
2634 (setq gnus-category-alist
2638 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2639 (goto-char (point-min))
2640 ;; This code isn't temp, it will be needed so long as
2641 ;; anyone may be migrating from an older version.
2643 ;; Once we're certain that people will not revert to an
2644 ;; earlier version, we can take out the old-list code in
2645 ;; gnus-category-write.
2646 (let* ((old-list (read (current-buffer)))
2647 (new-list (ignore-errors (read (current-buffer)))))
2650 ;; Convert from a positional list to an alist.
2660 '(agent-predicate agent-score-file agent-groups))))
2663 (list (gnus-agent-cat-make 'default 'short)))))
2665 (defun gnus-category-write ()
2666 "Write the category alist."
2667 (setq gnus-category-predicate-cache nil
2668 gnus-category-group-cache nil)
2669 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2670 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2671 ;; This prin1 is temporary. It exists so that people can revert
2672 ;; to an earlier version of gnus-agent.
2673 (prin1 (mapcar (lambda (c)
2675 (cdr (assoc 'agent-predicate c))
2676 (cdr (assoc 'agent-score-file c))
2677 (cdr (assoc 'agent-groups c))))
2678 gnus-category-alist)
2681 (prin1 gnus-category-alist (current-buffer))))
2683 (defun gnus-category-edit-predicate (category)
2684 "Edit the predicate for CATEGORY."
2685 (interactive (list (gnus-category-name)))
2686 (let ((info (assq category gnus-category-alist)))
2688 (gnus-agent-cat-predicate info)
2689 (format "Editing the select predicate for category %s" category)
2690 `(lambda (predicate)
2691 ;; Avoid run-time execution of setf form
2692 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2694 ;; use its expansion instead:
2695 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2696 'agent-predicate predicate)
2698 (gnus-category-write)
2699 (gnus-category-list)))))
2701 (defun gnus-category-edit-score (category)
2702 "Edit the score expression for CATEGORY."
2703 (interactive (list (gnus-category-name)))
2704 (let ((info (assq category gnus-category-alist)))
2706 (gnus-agent-cat-score-file info)
2707 (format "Editing the score expression for category %s" category)
2708 `(lambda (score-file)
2709 ;; Avoid run-time execution of setf form
2710 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2712 ;; use its expansion instead:
2713 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2714 'agent-score-file score-file)
2716 (gnus-category-write)
2717 (gnus-category-list)))))
2719 (defun gnus-category-edit-groups (category)
2720 "Edit the group list for CATEGORY."
2721 (interactive (list (gnus-category-name)))
2722 (let ((info (assq category gnus-category-alist)))
2724 (gnus-agent-cat-groups info)
2725 (format "Editing the group list for category %s" category)
2727 ;; Avoid run-time execution of setf form
2728 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2730 ;; use its expansion instead:
2731 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2734 (gnus-category-write)
2735 (gnus-category-list)))))
2737 (defun gnus-category-kill (category)
2738 "Kill the current category."
2739 (interactive (list (gnus-category-name)))
2740 (let ((info (assq category gnus-category-alist))
2741 (buffer-read-only nil))
2743 (setq gnus-category-alist (delq info gnus-category-alist))
2744 (gnus-category-write)))
2746 (defun gnus-category-copy (category to)
2747 "Copy the current category."
2748 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2749 (let ((info (assq category gnus-category-alist)))
2750 (push (let ((newcat (gnus-copy-sequence info)))
2751 (setf (gnus-agent-cat-name newcat) to)
2752 (setf (gnus-agent-cat-groups newcat) nil)
2754 gnus-category-alist)
2755 (gnus-category-write)
2756 (gnus-category-list)))
2758 (defun gnus-category-add (category)
2759 "Create a new category."
2760 (interactive "SCategory name: ")
2761 (when (assq category gnus-category-alist)
2762 (error "Category %s already exists" category))
2763 (push (gnus-agent-cat-make category)
2764 gnus-category-alist)
2765 (gnus-category-write)
2766 (gnus-category-list))
2768 (defun gnus-category-list ()
2769 "List all categories."
2771 (gnus-category-prepare))
2773 (defun gnus-category-exit ()
2774 "Return to the group buffer."
2776 (kill-buffer (current-buffer))
2777 (gnus-configure-windows 'group t))
2779 ;; To avoid having 8-bit characters in the source file.
2780 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2782 (defvar gnus-category-predicate-alist
2783 '((spam . gnus-agent-spam-p)
2784 (short . gnus-agent-short-p)
2785 (long . gnus-agent-long-p)
2786 (low . gnus-agent-low-scored-p)
2787 (high . gnus-agent-high-scored-p)
2788 (read . gnus-agent-read-p)
2789 (true . gnus-agent-true)
2790 (false . gnus-agent-false))
2791 "Mapping from short score predicate symbols to predicate functions.")
2793 (defun gnus-agent-spam-p ()
2794 "Say whether an article is spam or not."
2795 (unless gnus-agent-spam-hashtb
2796 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2797 (if (not (equal (mail-header-references gnus-headers) ""))
2799 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2801 (gnus-gethash string gnus-agent-spam-hashtb)
2802 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2804 (defun gnus-agent-short-p ()
2805 "Say whether an article is short or not."
2806 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2808 (defun gnus-agent-long-p ()
2809 "Say whether an article is long or not."
2810 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2812 (defun gnus-agent-low-scored-p ()
2813 "Say whether an article has a low score or not."
2814 (< gnus-score gnus-agent-low-score))
2816 (defun gnus-agent-high-scored-p ()
2817 "Say whether an article has a high score or not."
2818 (> gnus-score gnus-agent-high-score))
2820 (defun gnus-agent-read-p ()
2821 "Say whether an article is read or not."
2822 (gnus-member-of-range (mail-header-number gnus-headers)
2823 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2825 (defun gnus-category-make-function (predicate)
2826 "Make a function from PREDICATE."
2827 (let ((func (gnus-category-make-function-1 predicate)))
2828 (if (and (= (length func) 1)
2829 (symbolp (car func)))
2831 (gnus-byte-compile `(lambda () ,func)))))
2833 (defun gnus-agent-true ()
2837 (defun gnus-agent-false ()
2841 (defun gnus-category-make-function-1 (predicate)
2842 "Make a function from PREDICATE."
2844 ;; Functions are just returned as is.
2845 ((or (symbolp predicate)
2846 (functionp predicate))
2847 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2849 ;; More complex predicate.
2852 ((memq (car predicate) '(& and))
2854 ((memq (car predicate) '(| or))
2856 ((memq (car predicate) gnus-category-not)
2858 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2860 (error "Unknown predicate type: %s" predicate))))
2862 (defun gnus-get-predicate (predicate)
2863 "Return the function implementing PREDICATE."
2864 (or (cdr (assoc predicate gnus-category-predicate-cache))
2865 (let ((func (gnus-category-make-function predicate)))
2866 (setq gnus-category-predicate-cache
2867 (nconc gnus-category-predicate-cache
2868 (list (cons predicate func))))
2871 (defun gnus-predicate-implies-unread (predicate)
2872 "Say whether PREDICATE implies unread articles only.
2873 It is okay to miss some cases, but there must be no false positives.
2874 That is, if this predicate returns true, then indeed the predicate must
2875 return only unread articles."
2876 (eq t (gnus-function-implies-unread-1
2877 (gnus-category-make-function-1 predicate))))
2879 (defun gnus-function-implies-unread-1 (function)
2880 "Recursively evaluate a predicate function to determine whether it can select
2881 any read articles. Returns t if the function is known to never
2882 return read articles, nil when it is known to always return read
2883 articles, and t_nil when the function may return both read and unread
2885 (let ((func (car function))
2886 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2887 (cond ((eq func 'and)
2888 (cond ((memq t args) ; if any argument returns only unread articles
2889 ;; then that argument constrains the result to only unread articles.
2891 ((memq 't_nil args) ; if any argument is indeterminate
2892 ;; then the result is indeterminate
2895 (cond ((memq nil args) ; if any argument returns read articles
2896 ;; then that argument ensures that the results includes read articles.
2898 ((memq 't_nil args) ; if any argument is indeterminate
2899 ;; then that argument ensures that the results are indeterminate
2901 (t ; if all arguments return only unread articles
2902 ;; then the result returns only unread articles
2905 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2906 ; then the result is indeterminate
2909 ; toggle the result to be the opposite of the argument
2911 ((eq func 'gnus-agent-read-p)
2912 nil) ; The read predicate NEVER returns unread articles
2913 ((eq func 'gnus-agent-false)
2914 t) ; The false predicate returns t as the empty set excludes all read articles
2915 ((eq func 'gnus-agent-true)
2916 nil) ; The true predicate ALWAYS returns read articles
2917 ((catch 'found-match
2918 (let ((alist gnus-category-predicate-alist))
2920 (if (eq func (cdar alist))
2921 (throw 'found-match t)
2922 (setq alist (cdr alist))))))
2923 't_nil) ; All other predicates return read and unread articles
2925 (error "Unknown predicate function: %s" function)))))
2927 (defun gnus-group-category (group)
2928 "Return the category GROUP belongs to."
2929 (unless gnus-category-group-cache
2930 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2931 (let ((cs gnus-category-alist)
2933 (while (setq cat (pop cs))
2934 (setq groups (gnus-agent-cat-groups cat))
2936 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2937 (or (gnus-gethash group gnus-category-group-cache)
2938 (assq 'default gnus-category-alist)))
2940 (defun gnus-agent-expire-group (group &optional articles force)
2941 "Expire all old articles in GROUP.
2942 If you want to force expiring of certain articles, this function can
2943 take ARTICLES, and FORCE parameters as well.
2945 The articles on which the expiration process runs are selected as follows:
2946 if ARTICLES is null, all read and unmarked articles.
2947 if ARTICLES is t, all articles.
2948 if ARTICLES is a list, just those articles.
2949 FORCE is equivalent to setting the expiration predicates to true."
2951 (list (let ((def (or (gnus-group-group-name)
2952 gnus-newsgroup-name)))
2953 (let ((select (read-string (if def
2954 (concat "Group Name ("
2957 (if (and (equal "" select)
2963 (gnus-agent-expire articles group force)
2964 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2965 ;; expiration statistics of this single group
2966 (gnus-agent-expire-stats (list 0 0 0.0)))
2967 (if (or (not (eq articles t))
2969 (concat "Are you sure that you want to "
2970 "expire all articles in " group ".")))
2971 (let ((gnus-command-method (gnus-find-method-for-group group))
2972 (overview (gnus-get-buffer-create " *expire overview*"))
2975 (let ((active-file (gnus-agent-lib-file "active")))
2976 (when (file-exists-p active-file)
2978 (nnheader-insert-file-contents active-file)
2979 (gnus-active-to-gnus-format
2981 (setq orig (gnus-make-hashtable
2982 (count-lines (point-min) (point-max))))))
2984 (gnus-agent-expire-group-1
2985 group overview (gnus-gethash-safe group orig)
2987 (kill-buffer overview))))
2988 (gnus-message 4 (gnus-agent-expire-done-message)))))
2990 (defun gnus-agent-expire-group-1 (group overview active articles force)
2991 ;; Internal function - requires caller to have set
2992 ;; gnus-command-method, initialized overview buffer, and to have
2993 ;; provided a non-nil active
2995 (let ((dir (gnus-agent-group-pathname group)))
2996 (gnus-agent-with-refreshed-group
2998 (when (boundp 'gnus-agent-expire-current-dirs)
2999 (set 'gnus-agent-expire-current-dirs
3001 (symbol-value 'gnus-agent-expire-current-dirs))))
3003 (if (and (not force)
3004 (eq 'DISABLE (gnus-agent-find-parameter group
3005 'agent-enable-expiration)))
3006 (gnus-message 5 "Expiry skipping over %s" group)
3007 (gnus-message 5 "Expiring articles in %s" group)
3008 (gnus-agent-load-alist group)
3009 (let* ((bytes-freed 0)
3010 (size-files-deleted 0.0)
3012 (nov-entries-deleted 0)
3013 (info (gnus-get-info group))
3014 (alist gnus-agent-article-alist)
3015 (day (- (time-to-days (current-time))
3016 (gnus-agent-find-parameter group 'agent-days-until-old)))
3017 (specials (if (and alist
3019 ;; This could be a bit of a problem. I need to
3020 ;; keep the last article to avoid refetching
3021 ;; headers when using nntp in the backend. At
3022 ;; the same time, if someone uses a backend
3023 ;; that supports article moving then I may have
3024 ;; to remove the last article to complete the
3025 ;; move. Right now, I'm going to assume that
3026 ;; FORCE overrides specials.
3027 (list (caar (last alist)))))
3028 (unreads ;; Articles that are excluded from the
3029 ;; expiration process
3030 (cond (gnus-agent-expire-all
3031 ;; All articles are marked read by global decree
3034 ;; All articles are marked read by function
3038 ;; Unread articles are marked protected from
3039 ;; expiration Don't call
3040 ;; gnus-list-of-unread-articles as it returns
3041 ;; articles that have not been fetched into the
3044 (gnus-agent-unread-articles group)))
3046 ;; All articles EXCEPT those named by the caller
3047 ;; are protected from expiration
3048 (gnus-sorted-difference
3049 (gnus-uncompress-range
3051 (caar (last alist))))
3052 (sort articles '<)))))
3053 (marked ;; More articles that are excluded from the
3054 ;; expiration process
3055 (cond (gnus-agent-expire-all
3056 ;; All articles are unmarked by global decree
3059 ;; All articles are unmarked by function
3063 ;; All articles may as well be unmarked as the
3064 ;; unreads list already names the articles we are
3068 ;; Ticked and/or dormant articles are excluded
3071 (gnus-uncompress-range
3072 (cdr (assq 'tick (gnus-info-marks info))))
3073 (gnus-uncompress-range
3075 (gnus-info-marks info))))))))
3076 (nov-file (concat dir ".overview"))
3082 ;; The normal article alist contains elements that look like
3083 ;; (article# . fetch_date) I need to combine other
3084 ;; information with this list. For example, a flag indicating
3085 ;; that a particular article MUST BE KEPT. To do this, I'm
3086 ;; going to transform the elements to look like (article#
3087 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3088 ;; the process to generate the expired article alist.
3090 ;; Convert the alist elements to (article# fetch_date nil
3092 (setq dlist (mapcar (lambda (e)
3093 (list (car e) (cdr e) nil nil)) alist))
3095 ;; Convert the keep lists to elements that look like (article#
3096 ;; nil keep_flag nil) then append it to the expanded dlist
3097 ;; These statements are sorted by ascending precidence of the
3099 (setq dlist (nconc dlist
3101 (list e nil 'unread nil))
3103 (setq dlist (nconc dlist
3105 (list e nil 'marked nil))
3107 (setq dlist (nconc dlist
3109 (list e nil 'special nil))
3112 (set-buffer overview)
3114 (buffer-disable-undo)
3115 (when (file-exists-p nov-file)
3116 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3117 (nnheader-insert-file-contents nov-file)
3118 (goto-char (point-min))
3121 (while (< (setq p (point)) (point-max))
3123 ;; If I successfully read an integer (the plus zero
3124 ;; ensures a numeric type), append the position
3126 (push (list (+ 0 (read (current-buffer))) nil nil
3130 (gnus-message 1 "gnus-agent-expire: read error \
3131 occurred when reading expression at %s in %s. Skipping to next \
3132 line." (point) nov-file)))
3133 ;; Whether I succeeded, or failed, it doesn't matter.
3134 ;; Move to the next line then try again.
3138 7 "gnus-agent-expire: Loading overview... Done"))
3139 (set-buffer-modified-p nil)
3141 ;; At this point, all of the information is in dlist. The
3142 ;; only problem is that much of it is spread across multiple
3143 ;; entries. Sort then MERGE!!
3144 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3145 ;; If two entries have the same article-number then sort by
3146 ;; ascending keep_flag.
3153 (cond ((< (nth 0 a) (nth 0 b))
3155 ((> (nth 0 a) (nth 0 b))
3158 (let ((a (or (symbol-value (nth 2 a))
3160 (b (or (symbol-value (nth 2 b))
3163 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3164 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3165 (let ((dlist dlist))
3166 (while (cdr dlist) ; I'm not at the end-of-list
3167 (if (eq (caar dlist) (caadr dlist))
3168 (let ((first (cdr (car dlist)))
3169 (secnd (cdr (cadr dlist))))
3170 (setcar first (or (car first)
3171 (car secnd))) ; fetch_date
3172 (setq first (cdr first)
3174 (setcar first (or (car first)
3175 (car secnd))) ; Keep_flag
3176 (setq first (cdr first)
3178 (setcar first (or (car first)
3179 (car secnd))) ; NOV_entry_position
3181 (setcdr dlist (cddr dlist)))
3182 (setq dlist (cdr dlist)))))
3184 ;; Check the order of the entry positions. They should be in
3185 ;; ascending order. If they aren't, the positions must be
3186 ;; converted to markers.
3187 (when (let ((dlist dlist)
3191 (if (setq pos (nth 3 (pop dlist)))
3192 (if (< pos prev-pos)
3193 (throw 'sort-results 'unsorted)
3194 (setq prev-pos pos)))))
3195 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3196 (mapcar (lambda (entry)
3197 (let ((pos (nth 3 entry)))
3200 (set-marker (make-marker)
3204 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3206 (let* ((len (float (length dlist)))
3213 (let ((new-completed (truncate (* 100.0
3214 (/ (setq cnt (1+ cnt))
3217 (when (> new-completed completed)
3218 (setq completed new-completed)
3219 (gnus-message 7 "%3d%% completed..." completed)))
3220 (let* ((entry (car dlist))
3221 (article-number (nth 0 entry))
3222 (fetch-date (nth 1 entry))
3223 (keep (nth 2 entry))
3224 (marker (nth 3 entry)))
3227 ;; Kept articles are unread, marked, or special.
3229 (gnus-agent-message 10
3230 "gnus-agent-expire: %s:%d: Kept %s article%s."
3231 group article-number keep (if fetch-date " and file" ""))
3233 (unless (file-exists-p
3234 (concat dir (number-to-string
3236 (setf (nth 1 entry) nil)
3237 (gnus-agent-message 3 "gnus-agent-expire cleared \
3238 download flag on %s:%d as the cached article file is missing."
3239 group (caar dlist)))
3241 (gnus-message 1 "gnus-agent-expire detected a \
3242 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3243 (gnus-agent-append-to-list
3245 (cons article-number fetch-date)))
3247 ;; The following articles are READ, UNMARKED, and
3248 ;; ORDINARY. See if they can be EXPIRED!!!
3251 ((not (integerp fetch-date))
3252 'read) ;; never fetched article (may expire
3254 ((not (file-exists-p
3255 (concat dir (number-to-string
3257 (setf (nth 1 entry) nil)
3258 'externally-expired) ;; Can't find the cached
3259 ;; article. Handle case
3260 ;; as though this article
3261 ;; was never fetched.
3263 ;; We now have the arrival day, so we see
3264 ;; whether it's old enough to be expired.
3270 ;; I found some reason to expire this entry.
3272 (let ((actions nil))
3273 (when (memq type '(forced expired))
3274 (ignore-errors ; Just being paranoid.
3275 (let* ((file-name (nnheader-concat dir (number-to-string
3277 (size (float (nth 7 (file-attributes file-name)))))
3278 (incf bytes-freed size)
3279 (incf size-files-deleted size)
3280 (incf files-deleted)
3281 (delete-file file-name))
3282 (push "expired cached article" actions))
3283 (setf (nth 1 entry) nil)
3287 (push "NOV entry removed" actions)
3289 (goto-char (if (markerp marker)
3291 (- marker position-offset)))
3293 (incf nov-entries-deleted)
3295 (let* ((from (point-at-bol))
3296 (to (progn (forward-line 1) (point)))
3297 (freed (- to from)))
3298 (incf bytes-freed freed)
3299 (incf position-offset freed)
3300 (delete-region from to)))
3302 ;; If considering all articles is set, I can only
3303 ;; expire article IDs that are no longer in the
3304 ;; active range (That is, articles that preceed the
3305 ;; first article in the new alist).
3306 (if (and gnus-agent-consider-all-articles
3307 (>= article-number (car active)))
3308 ;; I have to keep this ID in the alist
3309 (gnus-agent-append-to-list
3310 tail-alist (cons article-number fetch-date))
3311 (push (format "Removed %s article number from \
3312 article alist" type) actions))
3315 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3316 group article-number
3317 (mapconcat 'identity actions ", ")))))
3320 10 "gnus-agent-expire: %s:%d: Article kept as \
3321 expiration tests failed." group article-number)
3322 (gnus-agent-append-to-list
3323 tail-alist (cons article-number fetch-date)))
3326 ;; Remove markers as I intend to reuse this buffer again.
3329 (set-marker marker nil))
3331 (setq dlist (cdr dlist))))
3333 (setq alist (cdr alist))
3335 (let ((inhibit-quit t))
3336 (unless (equal alist gnus-agent-article-alist)
3337 (setq gnus-agent-article-alist alist)
3338 (gnus-agent-save-alist group))
3340 (when (buffer-modified-p)
3341 (let ((coding-system-for-write
3342 gnus-agent-file-coding-system))
3343 (gnus-make-directory dir)
3344 (write-region (point-min) (point-max) nov-file nil
3346 ;; clear the modified flag as that I'm not confused by
3347 ;; its status on the next pass through this routine.
3348 (set-buffer-modified-p nil)
3349 (gnus-agent-update-view-total-fetched-for group t)))
3351 (when (eq articles t)
3352 (gnus-summary-update-info))))
3354 (when (boundp 'gnus-agent-expire-stats)
3355 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3356 (incf (nth 2 stats) bytes-freed)
3357 (incf (nth 1 stats) files-deleted)
3358 (incf (nth 0 stats) nov-entries-deleted)))
3360 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3362 (defun gnus-agent-expire (&optional articles group force)
3363 "Expire all old articles.
3364 If you want to force expiring of certain articles, this function can
3365 take ARTICLES, GROUP and FORCE parameters as well.
3367 The articles on which the expiration process runs are selected as follows:
3368 if ARTICLES is null, all read and unmarked articles.
3369 if ARTICLES is t, all articles.
3370 if ARTICLES is a list, just those articles.
3371 Setting GROUP will limit expiration to that group.
3372 FORCE is equivalent to setting the expiration predicates to true."
3376 (gnus-agent-expire-group group articles force)
3377 (if (or (not (eq articles t))
3378 (yes-or-no-p "Are you sure that you want to expire all \
3379 articles in every agentized group."))
3380 (let ((methods (gnus-agent-covered-methods))
3381 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3382 ;; of agent directories.
3383 (gnus-agent-expire-current-dirs nil)
3384 ;; Bind gnus-agent-expire-stats to enable tracking of
3385 ;; expiration statistics across all groups
3386 (gnus-agent-expire-stats (list 0 0 0.0))
3387 gnus-command-method overview orig)
3388 (setq overview (gnus-get-buffer-create " *expire overview*"))
3390 (while (setq gnus-command-method (pop methods))
3391 (let ((active-file (gnus-agent-lib-file "active")))
3392 (when (file-exists-p active-file)
3394 (nnheader-insert-file-contents active-file)
3395 (gnus-active-to-gnus-format
3397 (setq orig (gnus-make-hashtable
3398 (count-lines (point-min) (point-max))))))
3399 (dolist (expiring-group (gnus-groups-from-server
3400 gnus-command-method))
3402 (gnus-gethash-safe expiring-group orig)))
3406 (gnus-agent-expire-group-1
3407 expiring-group overview active articles force))))))))
3408 (kill-buffer overview))
3409 (gnus-agent-expire-unagentized-dirs)
3410 (gnus-message 4 (gnus-agent-expire-done-message))))))
3412 (defun gnus-agent-expire-done-message ()
3413 (if (and (> gnus-verbose 4)
3414 (boundp 'gnus-agent-expire-stats))
3415 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3416 (size (nth 2 stats))
3417 (units '(B KB MB GB)))
3418 (while (and (> size 1024.0)
3420 (setq size (/ size 1024.0)
3423 (format "Expiry recovered %d NOV entries, deleted %d files,\
3430 (defun gnus-agent-expire-unagentized-dirs ()
3431 (when (and gnus-agent-expire-unagentized-dirs
3432 (boundp 'gnus-agent-expire-current-dirs))
3433 (let* ((keep (gnus-make-hashtable))
3434 ;; Formally bind gnus-agent-expire-current-dirs so that the
3435 ;; compiler will not complain about free references.
3436 (gnus-agent-expire-current-dirs
3437 (symbol-value 'gnus-agent-expire-current-dirs))
3440 (gnus-sethash gnus-agent-directory t keep)
3441 (while gnus-agent-expire-current-dirs
3442 (setq dir (pop gnus-agent-expire-current-dirs))
3443 (when (and (stringp dir)
3444 (file-directory-p dir))
3445 (while (not (gnus-gethash dir keep))
3446 (gnus-sethash dir t keep)
3447 (setq dir (file-name-directory (directory-file-name dir))))))
3454 "Given a directory, check it and its subdirectories for
3455 membership in the keep hash. If it isn't found, add
3457 (let ((files (directory-files d))
3459 (while (setq file (pop files))
3460 (cond ((equal file ".") ; Ignore self
3462 ((equal file "..") ; Ignore parent
3464 ((equal file ".overview")
3465 ;; Directory must contain .overview to be
3466 ;; agent's cache of a group.
3467 (let ((d (file-name-as-directory d))
3469 ;; Search ancestor's for last directory NOT
3470 ;; found in keep hash.
3471 (while (not (gnus-gethash
3472 (setq d (file-name-directory d)) keep))
3474 d (directory-file-name d)))
3475 ;; if ANY ancestor was NOT in keep hash and
3476 ;; it it's already in to-remove, add it to
3479 (not (member r to-remove)))
3480 (push r to-remove))))
3481 ((file-directory-p (setq file (nnheader-concat d file)))
3482 (funcall checker file)))))))))
3483 (funcall checker (expand-file-name gnus-agent-directory))
3485 (when (and to-remove
3486 (or gnus-expert-user
3488 "gnus-agent-expire has identified local directories that are\
3489 not currently required by any agentized group. Do you wish to consider\
3492 (let ((dir (pop to-remove)))
3493 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3494 (let* (delete-recursive
3499 (if (file-directory-p f-or-d)
3501 (delete-directory f-or-d)
3504 (or (member f '("." ".."))
3505 (funcall delete-recursive
3508 (directory-files f-or-d))
3509 (delete-directory f-or-d)))
3510 (delete-file f-or-d)))))))
3511 (funcall delete-recursive dir))))))))))
3514 (defun gnus-agent-batch ()
3515 "Start Gnus, send queue and fetch session."
3517 (let ((init-file-user "")
3518 (gnus-always-read-dribble-file t))
3520 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3521 (gnus-group-send-queue)
3522 (gnus-agent-fetch-session)))
3524 (defun gnus-agent-unread-articles (group)
3525 (let* ((read (gnus-info-read (gnus-get-info group)))
3526 (known (gnus-agent-load-alist group))
3528 (tail-unread unread))
3529 (while (and known read)
3530 (let ((candidate (car (pop known))))
3531 (while (let* ((range (car read))
3532 (min (if (numberp range) range (car range)))
3533 (max (if (numberp range) range (cdr range))))
3534 (cond ((or (not min)
3536 (gnus-agent-append-to-list tail-unread candidate)
3539 (setq read (cdr read))
3540 ;; return t so that I always loop one more
3541 ;; time. If I just iterated off the end of
3542 ;; read, min will become nil and the current
3543 ;; candidate will be added to the unread list.
3546 (gnus-agent-append-to-list tail-unread (car (pop known))))
3549 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3550 "Restrict ARTICLES to numbers already fetched.
3551 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3552 that have already been fetched.
3553 If CACHED-HEADER is nil, articles are only excluded if the article itself
3556 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3557 ;; 'car gnus-agent-article-alist))
3559 ;; Functionally, I don't need to construct a temp list using mapcar.
3561 (if (and (or gnus-agent-cache (not gnus-plugged))
3562 (gnus-agent-load-alist group))
3563 (let* ((ref gnus-agent-article-alist)
3565 (uncached (list nil))
3566 (tail-uncached uncached))
3567 (while (and ref arts)
3568 (let ((v1 (car arts))
3570 (cond ((< v1 v2) ; v1 does not appear in the reference list
3571 (gnus-agent-append-to-list tail-uncached v1)
3572 (setq arts (cdr arts)))
3574 (unless (or cached-header (cdar ref)) ; v1 is already cached
3575 (gnus-agent-append-to-list tail-uncached v1))
3576 (setq arts (cdr arts))
3577 (setq ref (cdr ref)))
3578 (t ; reference article (v2) preceeds the list being filtered
3579 (setq ref (cdr ref))))))
3581 (gnus-agent-append-to-list tail-uncached (pop arts)))
3583 ;; if gnus-agent-load-alist fails, no articles are cached.
3586 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3588 (gnus-agent-create-buffer)
3589 (let ((gnus-decode-encoded-word-function 'identity)
3590 (file (gnus-agent-article-name ".overview" group))
3591 cached-articles uncached-articles)
3592 (gnus-make-directory (nnheader-translate-file-chars
3593 (file-name-directory file) t))
3595 ;; Populate temp buffer with known headers
3596 (when (file-exists-p file)
3597 (with-current-buffer gnus-agent-overview-buffer
3599 (let ((nnheader-file-coding-system
3600 gnus-agent-file-coding-system))
3601 (nnheader-insert-nov-file file (car articles)))))
3603 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3606 ;; Populate nntp-server-buffer with uncached headers
3607 (set-buffer nntp-server-buffer)
3609 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3610 (gnus-retrieve-headers
3611 uncached-articles group fetch-old))))
3612 (nnvirtual-convert-headers))
3613 ((eq 'nntp (car gnus-current-select-method))
3614 ;; The author of gnus-get-newsgroup-headers-xover
3615 ;; reports that the XOVER command is commonly
3616 ;; unreliable. The problem is that recently
3617 ;; posted articles may not be entered into the
3618 ;; NOV database in time to respond to my XOVER
3621 ;; I'm going to use his assumption that the NOV
3622 ;; database is updated in order of ascending
3623 ;; article ID. Therefore, a response containing
3624 ;; article ID N implies that all articles from 1
3625 ;; to N-1 are up-to-date. Therefore, missing
3626 ;; articles in that range have expired.
3628 (set-buffer nntp-server-buffer)
3629 (let* ((fetched-articles (list nil))
3630 (tail-fetched-articles fetched-articles)
3631 (min (cond ((numberp fetch-old)
3632 (max 1 (- (car articles) fetch-old)))
3637 (max (car (last articles))))
3639 ;; Get the list of articles that were fetched
3640 (goto-char (point-min))
3641 (let ((pm (point-max))
3643 (while (< (point) pm)
3644 (when (setq art (gnus-agent-read-article-number))
3645 (gnus-agent-append-to-list tail-fetched-articles art))
3648 ;; Clip this list to the headers that will
3649 ;; actually be returned
3650 (setq fetched-articles (gnus-list-range-intersection
3651 (cdr fetched-articles)
3654 ;; Clip the uncached articles list to exclude
3655 ;; IDs after the last FETCHED header. The
3656 ;; excluded IDs may be fetchable using HEAD.
3657 (if (car tail-fetched-articles)
3658 (setq uncached-articles
3659 (gnus-list-range-intersection
3661 (cons (car uncached-articles)
3662 (car tail-fetched-articles)))))
3664 ;; Create the list of articles that were
3665 ;; "successfully" fetched. Success, in this
3666 ;; case, means that the ID should not be
3667 ;; fetched again. In the case of an expired
3668 ;; article, the header will not be fetched.
3669 (setq uncached-articles
3670 (gnus-sorted-nunion fetched-articles
3674 ;; Erase the temp buffer
3675 (set-buffer gnus-agent-overview-buffer)
3678 ;; Copy the nntp-server-buffer to the temp buffer
3679 (set-buffer nntp-server-buffer)
3680 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3682 ;; Merge the temp buffer with the known headers (found on
3683 ;; disk in FILE) into the nntp-server-buffer
3684 (when uncached-articles
3685 (gnus-agent-braid-nov group uncached-articles file))
3687 ;; Save the new set of known headers to FILE
3688 (set-buffer nntp-server-buffer)
3689 (let ((coding-system-for-write
3690 gnus-agent-file-coding-system))
3691 (gnus-agent-check-overview-buffer)
3692 (write-region (point-min) (point-max) file nil 'silent))
3694 (gnus-agent-update-view-total-fetched-for group t)
3696 ;; Update the group's article alist to include the newly
3697 ;; fetched articles.
3698 (gnus-agent-load-alist group)
3699 (gnus-agent-save-alist group uncached-articles nil)
3702 ;; Copy the temp buffer to the nntp-server-buffer
3703 (set-buffer nntp-server-buffer)
3705 (insert-buffer-substring gnus-agent-overview-buffer)))
3708 (not (numberp fetch-old)))
3709 t ; Don't remove anything.
3710 (nnheader-nov-delete-outside-range
3711 (if fetch-old (max 1 (- (car articles) fetch-old))
3713 (car (last articles)))
3718 (defun gnus-agent-request-article (article group)
3719 "Retrieve ARTICLE in GROUP from the agent cache."
3720 (when (and gnus-agent
3721 (or gnus-agent-cache
3724 (let* ((gnus-command-method (gnus-find-method-for-group group))
3725 (file (gnus-agent-article-name (number-to-string article) group))
3726 (buffer-read-only nil))
3727 (when (and (file-exists-p file)
3728 (> (nth 7 (file-attributes file)) 0))
3730 (gnus-kill-all-overlays)
3731 (let ((coding-system-for-read gnus-cache-coding-system))
3732 (insert-file-contents file))
3735 (defun gnus-agent-regenerate-group (group &optional reread)
3737 If REREAD is t, all articles in the .overview are marked as unread.
3738 If REREAD is a list, the specified articles will be marked as unread.
3739 In addition, their NOV entries in .overview will be refreshed using
3740 the articles' current headers.
3741 If REREAD is not nil, downloaded articles are marked as unread."
3743 (list (let ((def (or (gnus-group-group-name)
3744 gnus-newsgroup-name)))
3745 (let ((select (read-string (if def
3746 (concat "Group Name ("
3749 (if (and (equal "" select)
3755 (cursor-in-echo-area t)
3756 (echo-keystrokes 0))
3757 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3758 (setq c (read-char-exclusive))
3760 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3762 ((or (eq c ?a) (eq c ?A))
3764 ((or (eq c ?d) (eq c ?D))
3765 (throw 'mark 'some)))
3766 (gnus-message 3 "Ignoring unexpected input")
3770 (gnus-message 5 "Regenerating in %s" group)
3771 (let* ((gnus-command-method (or gnus-command-method
3772 (gnus-find-method-for-group group)))
3773 (file (gnus-agent-article-name ".overview" group))
3774 (dir (file-name-directory file))
3776 (downloaded (if (file-exists-p dir)
3777 (sort (mapcar (lambda (name) (string-to-int name))
3778 (directory-files dir nil "^[0-9]+$" t))
3780 (progn (gnus-make-directory dir) nil)))
3785 (mm-with-unibyte-buffer
3786 (if (file-exists-p file)
3787 (let ((nnheader-file-coding-system
3788 gnus-agent-file-coding-system))
3789 (nnheader-insert-file-contents file)))
3790 (set-buffer-modified-p nil)
3792 ;; Load the article IDs found in the overview file. As a
3793 ;; side-effect, validate the file contents.
3797 (goto-char (point-min))
3798 (while (< (point) (point-max))
3799 (cond ((and (looking-at "[0-9]+\t")
3800 (<= (- (match-end 0) (match-beginning 0)) 9))
3801 (push (read (current-buffer)) nov-arts)
3803 (let ((l1 (car nov-arts))
3804 (l2 (cadr nov-arts)))
3805 (cond ((and (listp reread) (memq l1 reread))
3807 (setq nov-arts (cdr nov-arts))
3808 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3809 entry of article %s deleted." l1))
3813 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3814 entries are NOT in ascending order.")
3815 ;; Don't sort now as I haven't verified
3816 ;; that every line begins with a number
3820 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3821 entries contained duplicate of article %s. Duplicate deleted." l1)
3823 (setq nov-arts (cdr nov-arts))))))
3825 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3826 entries contained line that did not begin with an article number. Deleted\
3828 (gnus-delete-line))))
3830 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3831 entries into ascending order.")
3832 (sort-numeric-fields 1 (point-min) (point-max))
3833 (setq nov-arts nil))))
3834 (gnus-agent-check-overview-buffer)
3836 ;; Construct a new article alist whose nodes match every header
3837 ;; in the .overview file. As a side-effect, missing headers are
3838 ;; reconstructed from the downloaded article file.
3839 (while (or downloaded nov-arts)
3840 (cond ((and downloaded
3842 (> (car downloaded) (car nov-arts))))
3843 ;; This entry is missing from the overview file
3844 (gnus-message 3 "Regenerating NOV %s %d..." group
3846 (let ((file (concat dir (number-to-string (car downloaded)))))
3847 (mm-with-unibyte-buffer
3848 (nnheader-insert-file-contents file)
3849 (nnheader-remove-body)
3850 (setq header (nnheader-parse-naked-head)))
3851 (mail-header-set-number header (car downloaded))
3853 (let ((key (concat "^" (int-to-string (car nov-arts))
3855 (or (re-search-backward key nil t)
3856 (re-search-forward key))
3858 (goto-char (point-min)))
3859 (nnheader-insert-nov header))
3860 (setq nov-arts (cons (car downloaded) nov-arts)))
3861 ((eq (car downloaded) (car nov-arts))
3862 ;; This entry in the overview has been downloaded
3863 (push (cons (car downloaded)
3865 (nth 5 (file-attributes
3866 (concat dir (number-to-string
3867 (car downloaded))))))) alist)
3868 (setq downloaded (cdr downloaded))
3869 (setq nov-arts (cdr nov-arts)))
3871 ;; This entry in the overview has not been downloaded
3872 (push (cons (car nov-arts) nil) alist)
3873 (setq nov-arts (cdr nov-arts)))))
3875 ;; When gnus-agent-consider-all-articles is set,
3876 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3877 ;; the alist. Those IDs serve as markers to indicate that an
3878 ;; attempt has been made to fetch that article's header.
3880 ;; When gnus-agent-consider-all-articles is NOT set,
3881 ;; gnus-agent-regenerate-group can remove the article ID of every
3882 ;; article (with the exception of the last ID in the list - it's
3883 ;; special) that no longer appears in the overview. In this
3884 ;; situtation, the last article ID in the list implies that it,
3885 ;; and every article ID preceeding it, have been fetched from the
3888 (if gnus-agent-consider-all-articles
3889 ;; Restore all article IDs that were not found in the overview file.
3890 (let* ((n (cons nil alist))
3892 (o (gnus-agent-load-alist group)))
3894 (let ((nID (caadr n))
3897 (setq n (setcdr n (list (list oID))))
3900 (setcdr n (cons (list oID) (cdr n)))
3906 (setq n (cdr n))))))
3907 (setq alist (cdr merged)))
3908 ;; Restore the last article ID if it is not already in the new alist
3909 (let ((n (last alist))
3910 (o (last (gnus-agent-load-alist group))))
3914 (push (cons (caar o) nil) alist))
3915 ((< (caar n) (caar o))
3916 (setcdr n (list (car o)))))))
3918 (let ((inhibit-quit t))
3919 (if (setq regenerated (buffer-modified-p))
3920 (let ((coding-system-for-write gnus-agent-file-coding-system))
3921 (write-region (point-min) (point-max) file nil 'silent)))
3923 (setq regenerated (or regenerated
3924 (and reread gnus-agent-article-alist)
3925 (not (equal alist gnus-agent-article-alist))))
3927 (setq gnus-agent-article-alist alist)
3930 (gnus-agent-save-alist group)
3932 ;; I have to alter the group's active range NOW as
3933 ;; gnus-make-ascending-articles-unread will use it to
3934 ;; recalculate the number of unread articles in the group
3936 (let ((group (gnus-group-real-name group))
3937 (group-active (or (gnus-active group)
3938 (gnus-activate-group group))))
3939 (gnus-agent-possibly-alter-active group group-active)))))
3941 (when (and reread gnus-agent-article-alist)
3942 (gnus-agent-synchronize-group-flags
3947 (delq nil (mapcar (function (lambda (c)
3948 (cond ((eq reread t)
3952 gnus-agent-article-alist)))
3954 gnus-command-method)
3957 (gnus-agent-update-files-total-fetched-for group nil)))
3963 (defun gnus-agent-regenerate (&optional clean reread)
3964 "Regenerate all agent covered files.
3965 If CLEAN, obsolete (ignore)."
3968 (gnus-message 4 "Regenerating Gnus agent files...")
3969 (dolist (gnus-command-method (gnus-agent-covered-methods))
3970 (dolist (group (gnus-groups-from-server gnus-command-method))
3971 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3973 (gnus-message 4 "Regenerating Gnus agent files...done")
3977 (defun gnus-agent-go-online (&optional force)
3978 "Switch servers into online status."
3979 (interactive (list t))
3980 (dolist (server gnus-opened-servers)
3981 (when (eq (nth 1 server) 'offline)
3982 (if (if (eq force 'ask)
3984 (format "Switch %s:%s into online status? "
3985 (caar server) (cadar server)))
3987 (setcar (nthcdr 1 server) 'close)))))
3989 (defun gnus-agent-toggle-group-plugged (group)
3990 "Toggle the status of the server of the current group."
3991 (interactive (list (gnus-group-group-name)))
3992 (let* ((method (gnus-find-method-for-group group))
3993 (status (cadr (assoc method gnus-opened-servers))))
3994 (if (eq status 'offline)
3995 (gnus-server-set-status method 'closed)
3996 (gnus-close-server method)
3997 (gnus-server-set-status method 'offline))
3998 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3999 (if (eq status 'offline) 'offline 'online)
4000 (if (eq status 'offline) 'online 'offline))))
4002 (defun gnus-agent-group-covered-p (group)
4003 (gnus-agent-method-p (gnus-group-method group)))
4005 ;; Added to support XEmacs
4007 (unless (fboundp 'directory-files-and-attributes)
4008 (defun directory-files-and-attributes (directory
4009 &optional full match nosort)
4011 (dolist (file (directory-files directory full match nosort))
4012 (push (cons file (file-attributes file)) result))
4013 (nreverse result)))))
4015 (defun gnus-agent-update-files-total-fetched-for
4016 (group delta &optional method path)
4017 "Update, or set, the total disk space used by the articles that the
4019 (when gnus-agent-total-fetched-hashtb
4020 (gnus-agent-with-refreshed-group
4022 ;; if null, gnus-agent-group-pathname will calc method.
4023 (let* ((gnus-command-method method)
4024 (path (or path (gnus-agent-group-pathname group)))
4025 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4026 (gnus-sethash path (make-list 3 0)
4027 gnus-agent-total-fetched-hashtb))))
4032 (while (setq file (pop delta))
4033 (incf sum (float (or (nth 7 (file-attributes
4037 (number-to-string file)
4040 (let ((sum (- (nth 2 entry)))
4041 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4043 (while (setq file (pop info))
4044 (incf sum (float (or (nth 8 file) 0))))
4047 (setq gnus-agent-need-update-total-fetched-for t)
4048 (incf (nth 2 entry) delta)))))
4050 (defun gnus-agent-update-view-total-fetched-for
4051 (group agent-over &optional method path)
4052 "Update, or set, the total disk space used by the .agentview and
4053 .overview files. These files are calculated separately as they can be
4055 (when gnus-agent-total-fetched-hashtb
4056 (gnus-agent-with-refreshed-group
4058 ;; if null, gnus-agent-group-pathname will calc method.
4059 (let* ((gnus-command-method method)
4060 (path (or path (gnus-agent-group-pathname group)))
4061 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4062 (gnus-sethash path (make-list 3 0)
4063 gnus-agent-total-fetched-hashtb)))
4064 (size (or (nth 7 (file-attributes
4070 (setq gnus-agent-need-update-total-fetched-for t)
4071 (setf (nth (if agent-over 1 0) entry) size)))))
4073 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4074 "Get the total disk space used by the specified GROUP."
4075 (unless gnus-agent-total-fetched-hashtb
4076 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4078 ;; if null, gnus-agent-group-pathname will calc method.
4079 (let* ((gnus-command-method method)
4080 (path (gnus-agent-group-pathname group))
4081 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4084 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4086 (gnus-agent-update-view-total-fetched-for group nil method path)
4087 (gnus-agent-update-view-total-fetched-for group t method path)
4088 (gnus-agent-update-files-total-fetched-for group nil method path))))))
4090 (provide 'gnus-agent)
4092 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
4093 ;;; gnus-agent.el ends here