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)
241 (defvar gnus-headers)
248 (defun gnus-open-agent ()
250 (gnus-agent-read-servers)
252 (gnus-agent-create-buffer)
253 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
254 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
255 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
257 (defun gnus-agent-create-buffer ()
258 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
260 (setq gnus-agent-overview-buffer
261 (gnus-get-buffer-create " *Gnus agent overview*"))
262 (with-current-buffer gnus-agent-overview-buffer
263 (mm-enable-multibyte))
266 (gnus-add-shutdown 'gnus-close-agent 'gnus)
268 (defun gnus-close-agent ()
269 (setq gnus-category-predicate-cache nil
270 gnus-category-group-cache nil
271 gnus-agent-spam-hashtb nil)
272 (gnus-kill-buffer gnus-agent-overview-buffer))
275 ;;; Utility functions
278 (defun gnus-agent-read-file (file)
279 "Load FILE and do a `read' there."
282 (nnheader-insert-file-contents file)
283 (goto-char (point-min))
284 (read (current-buffer)))))
286 (defsubst gnus-agent-method ()
287 (concat (symbol-name (car gnus-command-method)) "/"
288 (if (equal (cadr gnus-command-method) "")
290 (cadr gnus-command-method))))
292 (defsubst gnus-agent-directory ()
293 "The name of the Gnus agent directory."
294 (nnheader-concat gnus-agent-directory
295 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
297 (defun gnus-agent-lib-file (file)
298 "The full name of the Gnus agent library FILE."
299 (expand-file-name file
300 (file-name-as-directory
301 (expand-file-name "agent.lib" (gnus-agent-directory)))))
303 (defun gnus-agent-cat-set-property (category property value)
305 (setcdr (or (assq property category)
306 (let ((cell (cons property nil)))
307 (setcdr category (cons cell (cdr category)))
309 (let ((category category))
310 (while (cond ((eq property (caadr category))
311 (setcdr category (cddr category))
314 (setq category (cdr category)))))))
318 (defmacro gnus-agent-cat-defaccessor (name prop-name)
319 "Define accessor and setter methods for manipulating a list of the form
320 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
321 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
322 manipulated as follows:
323 (func LIST): Returns VALUE1
324 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
325 `(progn (defmacro ,name (category)
326 (list (quote cdr) (list (quote assq)
327 (quote (quote ,prop-name)) category)))
329 (define-setf-method ,name (category)
330 (let* ((--category--temp-- (make-symbol "--category--"))
331 (--value--temp-- (make-symbol "--value--")))
332 (list (list --category--temp--) ; temporary-variables
333 (list category) ; value-forms
334 (list --value--temp--) ; store-variables
335 (let* ((category --category--temp--) ; store-form
336 (value --value--temp--))
337 (list (quote gnus-agent-cat-set-property)
339 (quote (quote ,prop-name))
341 (list (quote ,name) --category--temp--) ; access-form
345 (defmacro gnus-agent-cat-name (category)
348 (gnus-agent-cat-defaccessor
349 gnus-agent-cat-days-until-old agent-days-until-old)
350 (gnus-agent-cat-defaccessor
351 gnus-agent-cat-enable-expiration agent-enable-expiration)
352 (gnus-agent-cat-defaccessor
353 gnus-agent-cat-groups agent-groups)
354 (gnus-agent-cat-defaccessor
355 gnus-agent-cat-high-score agent-high-score)
356 (gnus-agent-cat-defaccessor
357 gnus-agent-cat-length-when-long agent-length-when-long)
358 (gnus-agent-cat-defaccessor
359 gnus-agent-cat-length-when-short agent-length-when-short)
360 (gnus-agent-cat-defaccessor
361 gnus-agent-cat-low-score agent-low-score)
362 (gnus-agent-cat-defaccessor
363 gnus-agent-cat-predicate agent-predicate)
364 (gnus-agent-cat-defaccessor
365 gnus-agent-cat-score-file agent-score-file)
366 (gnus-agent-cat-defaccessor
367 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
370 ;; This form is equivalent to defsetf except that it calls make-symbol
371 ;; whereas defsetf calls gensym (Using gensym creates a run-time
372 ;; dependency on the CL library).
375 (define-setf-method gnus-agent-cat-groups (category)
376 (let* ((--category--temp-- (make-symbol "--category--"))
377 (--groups--temp-- (make-symbol "--groups--")))
378 (list (list --category--temp--)
380 (list --groups--temp--)
381 (let* ((category --category--temp--)
382 (groups --groups--temp--))
383 (list (quote gnus-agent-set-cat-groups) category groups))
384 (list (quote gnus-agent-cat-groups) --category--temp--))))
387 (defun gnus-agent-set-cat-groups (category groups)
388 (unless (eq groups 'ignore)
390 (old-g (gnus-agent-cat-groups category)))
391 (cond ((eq new-g old-g)
392 ;; gnus-agent-add-group is fiddling with the group
393 ;; list. Still, Im done.
396 ((eq new-g (cdr old-g))
397 ;; gnus-agent-add-group is fiddling with the group list
398 (setcdr (or (assq 'agent-groups category)
399 (let ((cell (cons 'agent-groups nil)))
400 (setcdr category (cons cell (cdr category)))
403 (let ((groups groups))
405 (let* ((group (pop groups))
406 (old-category (gnus-group-category group)))
407 (if (eq category old-category)
409 (setf (gnus-agent-cat-groups old-category)
410 (delete group (gnus-agent-cat-groups
412 ;; Purge cache as preceeding loop invalidated it.
413 (setq gnus-category-group-cache nil))
415 (setcdr (or (assq 'agent-groups category)
416 (let ((cell (cons 'agent-groups nil)))
417 (setcdr category (cons cell (cdr category)))
420 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
421 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
423 ;;; Fetching setup functions.
425 (defun gnus-agent-start-fetch ()
426 "Initialize data structures for efficient fetching."
427 (gnus-agent-create-buffer))
429 (defun gnus-agent-stop-fetch ()
430 "Save all data structures and clean up."
431 (setq gnus-agent-spam-hashtb nil)
433 (set-buffer nntp-server-buffer)
436 (defmacro gnus-agent-with-fetch (&rest forms)
439 (let ((gnus-agent-fetching t))
440 (gnus-agent-start-fetch)
442 (gnus-agent-stop-fetch)))
444 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
445 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
447 (defmacro gnus-agent-append-to-list (tail value)
448 `(setq ,tail (setcdr ,tail (cons ,value nil))))
450 (defmacro gnus-agent-message (level &rest args)
451 `(if (<= ,level gnus-verbose)
458 (defvar gnus-agent-mode-hook nil
459 "Hook run when installing agent mode.")
461 (defvar gnus-agent-mode nil)
462 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
464 (defun gnus-agent-mode ()
465 "Minor mode for providing a agent support in Gnus buffers."
466 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
467 (symbol-name major-mode))
468 (match-string 1 (symbol-name major-mode))))
469 (mode (intern (format "gnus-agent-%s-mode" buffer))))
470 (set (make-local-variable 'gnus-agent-mode) t)
472 (set (make-local-variable mode) t)
474 (when (gnus-visual-p 'agent-menu 'menu)
475 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
476 (unless (assq 'gnus-agent-mode minor-mode-alist)
477 (push gnus-agent-mode-status minor-mode-alist))
478 (unless (assq mode minor-mode-map-alist)
479 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
481 minor-mode-map-alist))
482 (when (eq major-mode 'gnus-group-mode)
483 (let ((init-plugged gnus-plugged)
484 (gnus-agent-go-online nil))
485 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
486 ;; Therefore, make certain that the current value does not
487 ;; match the desired initial value.
488 (setq gnus-plugged :unknown)
489 (gnus-agent-toggle-plugged init-plugged)))
490 (gnus-run-hooks 'gnus-agent-mode-hook
491 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
493 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
494 (gnus-define-keys gnus-agent-group-mode-map
495 "Ju" gnus-agent-fetch-groups
496 "Jc" gnus-enter-category-buffer
497 "Jj" gnus-agent-toggle-plugged
498 "Js" gnus-agent-fetch-session
499 "JY" gnus-agent-synchronize-flags
500 "JS" gnus-group-send-queue
501 "Ja" gnus-agent-add-group
502 "Jr" gnus-agent-remove-group
503 "Jo" gnus-agent-toggle-group-plugged)
505 (defun gnus-agent-group-make-menu-bar ()
506 (unless (boundp 'gnus-agent-group-menu)
508 gnus-agent-group-menu gnus-agent-group-mode-map ""
510 ["Toggle plugged" gnus-agent-toggle-plugged t]
511 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
512 ["List categories" gnus-enter-category-buffer t]
513 ["Add (current) group to category" gnus-agent-add-group t]
514 ["Remove (current) group from category" gnus-agent-remove-group t]
515 ["Send queue" gnus-group-send-queue gnus-plugged]
517 ["All" gnus-agent-fetch-session gnus-plugged]
518 ["Group" gnus-agent-fetch-group gnus-plugged])
519 ["Synchronize flags" gnus-agent-synchronize-flags t]
522 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
523 (gnus-define-keys gnus-agent-summary-mode-map
524 "Jj" gnus-agent-toggle-plugged
525 "Ju" gnus-agent-summary-fetch-group
526 "JS" gnus-agent-fetch-group
527 "Js" gnus-agent-summary-fetch-series
528 "J#" gnus-agent-mark-article
529 "J\M-#" gnus-agent-unmark-article
530 "@" gnus-agent-toggle-mark
531 "Jc" gnus-agent-catchup)
533 (defun gnus-agent-summary-make-menu-bar ()
534 (unless (boundp 'gnus-agent-summary-menu)
536 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
538 ["Toggle plugged" gnus-agent-toggle-plugged t]
539 ["Mark as downloadable" gnus-agent-mark-article t]
540 ["Unmark as downloadable" gnus-agent-unmark-article t]
541 ["Toggle mark" gnus-agent-toggle-mark t]
542 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
543 ["Catchup undownloaded" gnus-agent-catchup t]))))
545 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
546 (gnus-define-keys gnus-agent-server-mode-map
547 "Jj" gnus-agent-toggle-plugged
548 "Ja" gnus-agent-add-server
549 "Jr" gnus-agent-remove-server)
551 (defun gnus-agent-server-make-menu-bar ()
552 (unless (boundp 'gnus-agent-server-menu)
554 gnus-agent-server-menu gnus-agent-server-mode-map ""
556 ["Toggle plugged" gnus-agent-toggle-plugged t]
557 ["Add" gnus-agent-add-server t]
558 ["Remove" gnus-agent-remove-server t]))))
560 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
561 (if (and (fboundp 'propertize)
562 (fboundp 'make-mode-line-mouse-map))
563 (propertize string 'local-map
564 (make-mode-line-mouse-map mouse-button mouse-func))
567 (defun gnus-agent-toggle-plugged (set-to)
568 "Toggle whether Gnus is unplugged or not."
569 (interactive (list (not gnus-plugged)))
570 (cond ((eq set-to gnus-plugged)
573 (setq gnus-plugged set-to)
574 (gnus-run-hooks 'gnus-agent-plugged-hook)
575 (setcar (cdr gnus-agent-mode-status)
576 (gnus-agent-make-mode-line-string " Plugged"
578 'gnus-agent-toggle-plugged))
579 (gnus-agent-go-online gnus-agent-go-online)
580 (gnus-agent-possibly-synchronize-flags))
582 (gnus-agent-close-connections)
583 (setq gnus-plugged set-to)
584 (gnus-run-hooks 'gnus-agent-unplugged-hook)
585 (setcar (cdr gnus-agent-mode-status)
586 (gnus-agent-make-mode-line-string " Unplugged"
588 'gnus-agent-toggle-plugged))))
589 (set-buffer-modified-p t))
591 (defmacro gnus-agent-while-plugged (&rest body)
592 `(let ((original-gnus-plugged gnus-plugged))
594 (progn (gnus-agent-toggle-plugged t)
596 (gnus-agent-toggle-plugged original-gnus-plugged))))
598 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
599 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
601 (defun gnus-agent-close-connections ()
602 "Close all methods covered by the Gnus agent."
603 (let ((methods (gnus-agent-covered-methods)))
605 (gnus-close-server (pop methods)))))
608 (defun gnus-unplugged ()
609 "Start Gnus unplugged."
611 (setq gnus-plugged nil)
615 (defun gnus-plugged ()
616 "Start Gnus plugged."
618 (setq gnus-plugged t)
622 (defun gnus-slave-unplugged (&optional arg)
623 "Read news as a slave unplugged."
625 (setq gnus-plugged nil)
626 (gnus arg nil 'slave))
629 (defun gnus-agentize ()
630 "Allow Gnus to be an offline newsreader.
632 The gnus-agentize function is now called internally by gnus when
633 gnus-agent is set. If you wish to avoid calling gnus-agentize,
634 customize gnus-agent to nil.
636 This will modify the `gnus-setup-news-hook', and
637 `message-send-mail-real-function' variables, and install the Gnus agent
638 minor mode in all Gnus buffers."
641 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
642 (unless gnus-agent-send-mail-function
643 (setq gnus-agent-send-mail-function
644 (or message-send-mail-real-function
645 (function (lambda () (funcall message-send-mail-function))))
646 message-send-mail-real-function 'gnus-agent-send-mail))
648 ;; If the servers file doesn't exist, auto-agentize some servers and
649 ;; save the servers file so this auto-agentizing isn't invoked
651 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
652 (gnus-message 3 "First time agent user, agentizing remote groups...")
654 (lambda (server-or-method)
655 (let ((method (gnus-server-to-method server-or-method)))
656 (when (memq (car method)
657 gnus-agent-auto-agentize-methods)
658 (push (gnus-method-to-server method)
659 gnus-agent-covered-methods)
660 (setq gnus-agent-method-p-cache nil))))
661 (cons gnus-select-method gnus-secondary-select-methods))
662 (gnus-agent-write-servers)))
664 (defun gnus-agent-queue-setup (&optional group-name)
665 "Make sure the queue group exists.
666 Optional arg GROUP-NAME allows to specify another group."
667 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
669 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
670 (let ((gnus-level-default-subscribed 1))
671 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
673 (gnus-group-set-parameter
674 (format "nndraft:%s" (or group-name "queue"))
675 'gnus-dummy '((gnus-draft-mode)))))
677 (defun gnus-agent-send-mail ()
678 (if (or (not gnus-agent-queue-mail)
679 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
680 (funcall gnus-agent-send-mail-function)
681 (goto-char (point-min))
683 (concat "^" (regexp-quote mail-header-separator) "\n"))
685 (gnus-agent-insert-meta-information 'mail)
686 (gnus-request-accept-article "nndraft:queue" nil t t)))
688 (defun gnus-agent-insert-meta-information (type &optional method)
689 "Insert meta-information into the message that says how it's to be posted.
690 TYPE can be either `mail' or `news'. If the latter, then METHOD can
693 (message-remove-header gnus-agent-meta-information-header)
694 (goto-char (point-min))
695 (insert gnus-agent-meta-information-header ": "
696 (symbol-name type) " " (format "%S" method)
699 (while (search-backward "\n" nil t)
700 (replace-match "\\n" t t))))
702 (defun gnus-agent-restore-gcc ()
703 "Restore GCC field from saved header."
705 (goto-char (point-min))
706 (while (re-search-forward
707 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
708 (replace-match "Gcc:" 'fixedcase))))
710 (defun gnus-agent-any-covered-gcc ()
712 (message-narrow-to-headers)
713 (let* ((gcc (mail-fetch-field "gcc" nil t))
715 (mapcar 'gnus-inews-group-method
716 (message-unquote-tokens
717 (message-tokenize-header
720 (while (and (not covered) methods)
721 (setq covered (gnus-agent-method-p (car methods))
722 methods (cdr methods)))
726 (defun gnus-agent-possibly-save-gcc ()
727 "Save GCC if Gnus is unplugged."
728 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
730 (goto-char (point-min))
731 (let ((case-fold-search t))
732 (while (re-search-forward "^gcc:" nil t)
733 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
735 (defun gnus-agent-possibly-do-gcc ()
736 "Do GCC if Gnus is plugged."
737 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
738 (gnus-inews-do-gcc)))
741 ;;; Group mode commands
744 (defun gnus-agent-fetch-groups (n)
745 "Put all new articles in the current groups into the Agent."
748 (error "Groups can't be fetched when Gnus is unplugged"))
749 (gnus-group-iterate n 'gnus-agent-fetch-group))
751 (defun gnus-agent-fetch-group (&optional group)
752 "Put all new articles in GROUP into the Agent."
753 (interactive (list (gnus-group-group-name)))
754 (setq group (or group gnus-newsgroup-name))
756 (error "No group on the current line"))
758 (gnus-agent-while-plugged
759 (let ((gnus-command-method (gnus-find-method-for-group group)))
760 (gnus-agent-with-fetch
761 (gnus-agent-fetch-group-1 group gnus-command-method)
762 (gnus-message 5 "Fetching %s...done" group)))))
764 (defun gnus-agent-add-group (category arg)
765 "Add the current group to an agent category."
771 (mapcar (lambda (cat) (list (symbol-name (car cat))))
775 (let ((cat (assq category gnus-category-alist))
777 (gnus-group-iterate arg
779 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
780 (setf (gnus-agent-cat-groups c)
781 (delete group (gnus-agent-cat-groups c))))
782 (push group groups)))
783 (setf (gnus-agent-cat-groups cat)
784 (nconc (gnus-agent-cat-groups cat) groups))
785 (gnus-category-write)))
787 (defun gnus-agent-remove-group (arg)
788 "Remove the current group from its agent category, if any."
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 (gnus-category-write)))
798 (defun gnus-agent-synchronize-flags ()
799 "Synchronize unplugged flags with servers."
802 (dolist (gnus-command-method (gnus-agent-covered-methods))
803 (when (file-exists-p (gnus-agent-lib-file "flags"))
804 (gnus-agent-synchronize-flags-server gnus-command-method)))))
806 (defun gnus-agent-possibly-synchronize-flags ()
807 "Synchronize flags according to `gnus-agent-synchronize-flags'."
810 (dolist (gnus-command-method (gnus-agent-covered-methods))
811 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
812 (not (eq (gnus-server-status gnus-command-method) 'offline)))
813 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
815 (defun gnus-agent-synchronize-flags-server (method)
816 "Synchronize flags set when unplugged for server."
817 (let ((gnus-command-method method)
819 (when (file-exists-p (gnus-agent-lib-file "flags"))
820 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
822 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
823 (cond ((null gnus-plugged)
825 1 "You must be plugged to synchronize flags with server %s"
826 (nth 1 gnus-command-method)))
827 ((null (gnus-check-server gnus-command-method))
829 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
834 (eval (read (current-buffer)))
835 (delete-region bgn (point))))
837 (delete-file (gnus-agent-lib-file "flags")))
839 (let ((file (gnus-agent-lib-file "flags")))
840 (write-region (point-min) (point-max)
841 (gnus-agent-lib-file "flags") nil 'silent)
842 (error "Couldn't set flags from file %s due to %s"
843 file (error-message-string err)))))))
846 (defun gnus-agent-possibly-synchronize-flags-server (method)
847 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
848 (when (or (and gnus-agent-synchronize-flags
849 (not (eq gnus-agent-synchronize-flags 'ask)))
850 (and (eq gnus-agent-synchronize-flags 'ask)
851 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
853 (gnus-agent-synchronize-flags-server method)))
856 (defun gnus-agent-rename-group (old-group new-group)
857 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
858 disabled, as the old agent files would corrupt gnus when the agent was
859 next enabled. Depends upon the caller to determine whether group renaming is supported."
860 (let* ((old-command-method (gnus-find-method-for-group old-group))
861 (old-path (directory-file-name
862 (let (gnus-command-method old-command-method)
863 (gnus-agent-group-pathname old-group))))
864 (new-command-method (gnus-find-method-for-group new-group))
865 (new-path (directory-file-name
866 (let (gnus-command-method new-command-method)
867 (gnus-agent-group-pathname new-group)))))
868 (gnus-rename-file old-path new-path t)
870 (let* ((old-real-group (gnus-group-real-name old-group))
871 (new-real-group (gnus-group-real-name new-group))
872 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
873 (gnus-agent-save-group-info old-command-method old-real-group nil)
874 (gnus-agent-save-group-info new-command-method new-real-group old-active)
876 (let ((old-local (gnus-agent-get-local old-group
877 old-real-group old-command-method)))
878 (gnus-agent-set-local old-group
880 old-real-group old-command-method)
881 (gnus-agent-set-local new-group
882 (car old-local) (cdr old-local)
883 new-real-group new-command-method)))))
886 (defun gnus-agent-delete-group (group)
887 "Delete fully-qualified GROUP. Always updates the agent, even when
888 disabled, as the old agent files would corrupt gnus when the agent was
889 next enabled. Depends upon the caller to determine whether group deletion is supported."
890 (let* ((command-method (gnus-find-method-for-group group))
891 (path (directory-file-name
892 (let (gnus-command-method command-method)
893 (gnus-agent-group-pathname group)))))
894 (gnus-delete-directory path)
896 (let* ((real-group (gnus-group-real-name group)))
897 (gnus-agent-save-group-info command-method real-group nil)
899 (let ((local (gnus-agent-get-local group
900 real-group command-method)))
901 (gnus-agent-set-local group
903 real-group command-method)))))
906 ;;; Server mode commands
909 (defun gnus-agent-add-server ()
910 "Enroll SERVER in the agent program."
912 (let* ((server (gnus-server-server-name))
913 (named-server (gnus-server-named-server))
915 (gnus-server-get-method nil server))))
917 (error "No server on the current line"))
919 (when (gnus-agent-method-p method)
920 (error "Server already in the agent program"))
922 (push named-server gnus-agent-covered-methods)
924 (setq gnus-agent-method-p-cache nil)
925 (gnus-server-update-server server)
926 (gnus-agent-write-servers)
927 (gnus-message 1 "Entered %s into the Agent" server)))
929 (defun gnus-agent-remove-server ()
930 "Remove SERVER from the agent program."
932 (let* ((server (gnus-server-server-name))
933 (named-server (gnus-server-named-server)))
935 (error "No server on the current line"))
937 (unless (member named-server gnus-agent-covered-methods)
938 (error "Server not in the agent program"))
940 (setq gnus-agent-covered-methods
941 (delete named-server gnus-agent-covered-methods)
942 gnus-agent-method-p-cache nil)
944 (gnus-server-update-server server)
945 (gnus-agent-write-servers)
946 (gnus-message 1 "Removed %s from the agent" server)))
948 (defun gnus-agent-read-servers ()
949 "Read the alist of covered servers."
950 (setq gnus-agent-covered-methods
951 (gnus-agent-read-file
952 (nnheader-concat gnus-agent-directory "lib/servers"))
953 gnus-agent-method-p-cache nil)
955 ;; I am called so early in start-up that I can not validate server
956 ;; names. When that is the case, I skip the validation. That is
957 ;; alright as the gnus startup code calls the validate methods
959 (if gnus-server-alist
960 (gnus-agent-read-servers-validate)))
962 (defun gnus-agent-read-servers-validate ()
963 (mapcar (lambda (server-or-method)
964 (let* ((server (if (stringp server-or-method)
966 (gnus-method-to-server server-or-method)))
967 (method (gnus-server-to-method server)))
969 (unless (member server gnus-agent-covered-methods)
970 (push server gnus-agent-covered-methods)
971 (setq gnus-agent-method-p-cache nil))
972 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
973 (prog1 gnus-agent-covered-methods
974 (setq gnus-agent-covered-methods nil))))
976 (defun gnus-agent-read-servers-validate-native (native-method)
977 (setq gnus-agent-covered-methods
978 (mapcar (lambda (method)
980 (equal method native-method))
982 method)) gnus-agent-covered-methods)))
984 (defun gnus-agent-write-servers ()
985 "Write the alist of covered servers."
986 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
987 (let ((coding-system-for-write nnheader-file-coding-system)
988 (file-name-coding-system nnmail-pathname-coding-system))
989 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
990 (prin1 gnus-agent-covered-methods
997 (defun gnus-agent-mark-article (n &optional unmark)
998 "Mark the next N articles as downloadable.
999 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1000 the mark instead. The difference between N and the actual number of
1001 articles marked is returned."
1003 (let ((backward (< n 0))
1008 (gnus-summary-set-agent-mark
1009 (gnus-summary-article-number) unmark)
1010 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1013 (gnus-message 7 "No more articles"))
1014 (gnus-summary-recenter)
1015 (gnus-summary-position-point)
1018 (defun gnus-agent-unmark-article (n)
1019 "Remove the downloadable mark from the next N articles.
1020 If N is negative, unmark backward instead. The difference between N and
1021 the actual number of articles unmarked is returned."
1023 (gnus-agent-mark-article n t))
1025 (defun gnus-agent-toggle-mark (n)
1026 "Toggle the downloadable mark from the next N articles.
1027 If N is negative, toggle backward instead. The difference between N and
1028 the actual number of articles toggled is returned."
1030 (gnus-agent-mark-article n 'toggle))
1032 (defun gnus-summary-set-agent-mark (article &optional unmark)
1033 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1034 When UNMARK is t, the article is unmarked. For any other value, the
1035 article's mark is toggled."
1036 (let ((unmark (cond ((eq nil unmark)
1041 (memq article gnus-newsgroup-downloadable)))))
1042 (when (gnus-summary-goto-subject article nil t)
1043 (gnus-summary-update-mark
1046 (setq gnus-newsgroup-downloadable
1047 (delq article gnus-newsgroup-downloadable))
1048 (gnus-article-mark article))
1049 (setq gnus-newsgroup-downloadable
1050 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1051 gnus-downloadable-mark)
1055 (defun gnus-agent-get-undownloaded-list ()
1056 "Construct list of articles that have not been downloaded."
1057 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1058 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1059 (gnus-agent-method-p gnus-command-method))
1060 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1061 (headers (sort (mapcar (lambda (h)
1062 (mail-header-number h))
1063 gnus-newsgroup-headers) '<))
1064 (cached (and gnus-use-cache gnus-newsgroup-cached))
1065 (undownloaded (list nil))
1066 (tail-undownloaded undownloaded)
1067 (unfetched (list nil))
1068 (tail-unfetched unfetched))
1069 (while (and alist headers)
1070 (let ((a (caar alist))
1073 ;; Ignore IDs in the alist that are not being
1074 ;; displayed in the summary.
1075 (setq alist (cdr alist)))
1077 ;; Headers that are not in the alist should be
1078 ;; fictious (see nnagent-retrieve-headers); they
1079 ;; imply that this article isn't in the agent.
1080 (gnus-agent-append-to-list tail-undownloaded h)
1081 (gnus-agent-append-to-list tail-unfetched h)
1082 (setq headers (cdr headers)))
1084 (setq alist (cdr alist))
1085 (setq headers (cdr headers))
1086 nil ; ignore already downloaded
1089 (setq alist (cdr alist))
1090 (setq headers (cdr headers))
1092 ;; This article isn't in the agent. Check to see
1093 ;; if it is in the cache. If it is, it's been
1095 (while (and cached (< (car cached) a))
1096 (setq cached (cdr cached)))
1097 (unless (equal a (car cached))
1098 (gnus-agent-append-to-list tail-undownloaded a))))))
1101 (let ((num (pop headers)))
1102 (gnus-agent-append-to-list tail-undownloaded num)
1103 (gnus-agent-append-to-list tail-unfetched num)))
1105 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1106 gnus-newsgroup-unfetched (cdr unfetched))))))
1108 (defun gnus-agent-catchup ()
1109 "Mark as read all unhandled articles.
1110 An article is unhandled if it is neither cached, nor downloaded, nor
1114 (let ((articles gnus-newsgroup-undownloaded))
1115 (when (or gnus-newsgroup-downloadable
1116 gnus-newsgroup-cached)
1117 (setq articles (gnus-sorted-ndifference
1118 (gnus-sorted-ndifference
1119 (gnus-copy-sequence articles)
1120 gnus-newsgroup-downloadable)
1121 gnus-newsgroup-cached)))
1124 (gnus-summary-mark-article
1125 (pop articles) gnus-catchup-mark)))
1126 (gnus-summary-position-point)))
1128 (defun gnus-agent-summary-fetch-series ()
1130 (when gnus-newsgroup-processable
1131 (setq gnus-newsgroup-downloadable
1132 (let* ((dl gnus-newsgroup-downloadable)
1133 (gnus-newsgroup-downloadable
1134 (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1135 (fetched-articles (gnus-agent-summary-fetch-group)))
1136 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1137 ;; updated gnus-newsgroup-downloadable to remove each
1138 ;; article successfully fetched.
1140 ;; For each article that I processed, remove its
1141 ;; processable mark IF the article is no longer
1142 ;; downloadable (i.e. it's already downloaded)
1143 (dolist (article gnus-newsgroup-processable)
1144 (unless (memq article gnus-newsgroup-downloadable)
1145 (gnus-summary-remove-process-mark article)))
1146 (gnus-sorted-ndifference dl fetched-articles)))))
1148 (defun gnus-agent-summary-fetch-group (&optional all)
1149 "Fetch the downloadable articles in the group.
1150 Optional arg ALL, if non-nil, means to fetch all articles."
1153 (if all gnus-newsgroup-articles
1154 gnus-newsgroup-downloadable))
1155 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1157 (gnus-agent-while-plugged
1159 (error "No articles to download"))
1160 (gnus-agent-with-fetch
1161 (setq gnus-newsgroup-undownloaded
1162 (gnus-sorted-ndifference
1163 gnus-newsgroup-undownloaded
1164 (setq fetched-articles
1165 (gnus-agent-fetch-articles
1166 gnus-newsgroup-name articles)))))
1168 (dolist (article articles)
1169 (let ((was-marked-downloadable
1170 (memq article gnus-newsgroup-downloadable)))
1171 (cond (gnus-agent-mark-unread-after-downloaded
1172 (setq gnus-newsgroup-downloadable
1173 (delq article gnus-newsgroup-downloadable))
1175 (gnus-summary-mark-article article gnus-unread-mark))
1176 (was-marked-downloadable
1177 (gnus-summary-set-agent-mark article t)))
1178 (when (gnus-summary-goto-subject article nil t)
1179 (gnus-summary-update-download-mark article))))))
1182 (defun gnus-agent-fetch-selected-article ()
1183 "Fetch the current article as it is selected.
1184 This can be added to `gnus-select-article-hook' or
1185 `gnus-mark-article-hook'."
1186 (let ((gnus-command-method gnus-current-select-method))
1187 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1188 (when (gnus-agent-fetch-articles
1190 (list gnus-current-article))
1191 (setq gnus-newsgroup-undownloaded
1192 (delq gnus-current-article gnus-newsgroup-undownloaded))
1193 (gnus-summary-update-download-mark gnus-current-article)))))
1196 ;;; Internal functions
1199 (defun gnus-agent-synchronize-group-flags (group actions server)
1200 "Update a plugged group by performing the indicated actions."
1201 (let* ((gnus-command-method (gnus-server-to-method server))
1203 ;; This initializer is required as gnus-request-set-mark
1204 ;; calls gnus-group-real-name to strip off the host name
1205 ;; before calling the backend. Now that the backend is
1206 ;; trying to call gnus-request-set-mark, I have to
1207 ;; reconstruct the original group name.
1208 (or (gnus-get-info group)
1210 (setq group (gnus-group-full-name
1211 group gnus-command-method))))))
1212 (gnus-request-set-mark group actions)
1215 (dolist (action actions)
1216 (let ((range (nth 0 action))
1217 (what (nth 1 action))
1218 (marks (nth 2 action)))
1219 (dolist (mark marks)
1220 (cond ((eq mark 'read)
1223 (funcall (if (eq what 'add)
1225 'gnus-remove-from-range)
1226 (gnus-info-read info)
1228 (gnus-get-unread-articles-in-group
1230 (gnus-active (gnus-info-group info))))
1231 ((memq mark '(tick))
1232 (let ((info-marks (assoc mark (gnus-info-marks info))))
1234 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1235 (setcdr info-marks (funcall (if (eq what 'add)
1237 'gnus-remove-from-range)
1242 (defun gnus-agent-save-active (method)
1243 (when (gnus-agent-method-p method)
1244 (let* ((gnus-command-method method)
1245 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1246 (file (gnus-agent-lib-file "active")))
1247 (gnus-active-to-gnus-format nil new)
1248 (gnus-agent-write-active file new)
1250 (nnheader-insert-file-contents file))))
1252 (defun gnus-agent-write-active (file new)
1253 (gnus-make-directory (file-name-directory file))
1254 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1255 ;; The hashtable contains real names of groups. However, do NOT
1256 ;; add the foreign server prefix as gnus-active-to-gnus-format
1257 ;; will add it while reading the file.
1258 (gnus-write-active-file file new nil)))
1261 (defun gnus-agent-possibly-alter-active (group active &optional info)
1262 "Possibly expand a group's active range to include articles
1263 downloaded into the agent."
1264 (let* ((gnus-command-method (or gnus-command-method
1265 (gnus-find-method-for-group group))))
1266 (when (gnus-agent-method-p gnus-command-method)
1267 (let* ((local (gnus-agent-get-local group))
1268 (active-min (or (car active) 0))
1269 (active-max (or (cdr active) 0))
1270 (agent-min (or (car local) active-min))
1271 (agent-max (or (cdr local) active-max)))
1273 (when (< agent-min active-min)
1274 (setcar active agent-min))
1276 (when (> agent-max active-max)
1277 (setcdr active agent-max))
1279 (when (and info (< agent-max (- active-min 100)))
1280 ;; I'm expanding the active range by such a large amount
1281 ;; that there is a gap of more than 100 articles between the
1282 ;; last article known to the agent and the first article
1283 ;; currently available on the server. This gap contains
1284 ;; articles that have been lost, mark them as read so that
1285 ;; gnus doesn't waste resources trying to fetch them.
1287 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1288 ;; want to modify the local file everytime someone restarts
1289 ;; gnus. The small gap will cause a tiny performance hit
1290 ;; when gnus tries, and fails, to retrieve the articles.
1291 ;; Still that should be smaller than opening a buffer,
1292 ;; printing this list to the buffer, and then writing it to a
1295 (let ((read (gnus-info-read info)))
1300 (list (cons (1+ agent-max)
1301 (1- active-min))))))
1303 ;; Lie about the agent's local range for this group to
1304 ;; disable the set read each time this server is opened.
1305 ;; NOTE: Opening this group will restore the valid local
1306 ;; range but it will also expand the local range to
1307 ;; incompass the new active range.
1308 (gnus-agent-set-local group agent-min (1- active-min)))))))
1310 (defun gnus-agent-save-group-info (method group active)
1311 "Update a single group's active range in the agent's copy of the server's active file."
1312 (when (gnus-agent-method-p method)
1313 (let* ((gnus-command-method (or method gnus-command-method))
1314 (coding-system-for-write nnheader-file-coding-system)
1315 (file-name-coding-system nnmail-pathname-coding-system)
1316 (file (gnus-agent-lib-file "active"))
1317 oactive-min oactive-max)
1318 (gnus-make-directory (file-name-directory file))
1319 (with-temp-file file
1320 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1321 (mm-disable-multibyte)
1322 (when (file-exists-p file)
1323 (nnheader-insert-file-contents file)
1325 (goto-char (point-min))
1326 (when (re-search-forward
1327 (concat "^" (regexp-quote group) " ") nil t)
1329 (setq oactive-max (read (current-buffer)) ;; max
1330 oactive-min (read (current-buffer)))) ;; min
1331 (gnus-delete-line)))
1333 (insert (format "%S %d %d y\n" (intern group)
1334 (max (or oactive-max (cdr active)) (cdr active))
1335 (min (or oactive-min (car active)) (car active))))
1336 (goto-char (point-max))
1337 (while (search-backward "\\." nil t)
1338 (delete-char 1)))))))
1340 (defun gnus-agent-get-group-info (method group)
1341 "Get a single group's active range in the agent's copy of the server's active file."
1342 (when (gnus-agent-method-p method)
1343 (let* ((gnus-command-method (or method gnus-command-method))
1344 (coding-system-for-write nnheader-file-coding-system)
1345 (file-name-coding-system nnmail-pathname-coding-system)
1346 (file (gnus-agent-lib-file "active"))
1347 oactive-min oactive-max)
1348 (gnus-make-directory (file-name-directory file))
1350 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1351 (mm-disable-multibyte)
1352 (when (file-exists-p file)
1353 (nnheader-insert-file-contents file)
1355 (goto-char (point-min))
1356 (when (re-search-forward
1357 (concat "^" (regexp-quote group) " ") nil t)
1359 (setq oactive-max (read (current-buffer)) ;; max
1360 oactive-min (read (current-buffer))) ;; min
1361 (cons oactive-min oactive-max))))))))
1363 (defun gnus-agent-group-path (group)
1364 "Translate GROUP into a file name."
1366 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1367 ;; The two methods must be kept synchronized, which is why
1368 ;; gnus-agent-group-pathname was added.
1371 (nnheader-translate-file-chars
1372 (nnheader-replace-duplicate-chars-in-string
1373 (nnheader-replace-chars-in-string
1374 (gnus-group-real-name (gnus-group-decoded-name group))
1377 (if (or nnmail-use-long-file-names
1378 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1380 (mm-encode-coding-string
1381 (nnheader-replace-chars-in-string group ?. ?/)
1382 nnmail-pathname-coding-system)))
1384 (defun gnus-agent-group-pathname (group)
1385 "Translate GROUP into a file name."
1386 ;; nnagent uses nnmail-group-pathname to read articles while
1387 ;; unplugged. The agent must, therefore, use the same directory
1389 (let ((gnus-command-method (or gnus-command-method
1390 (gnus-find-method-for-group group))))
1391 (nnmail-group-pathname (gnus-group-real-name
1392 (gnus-group-decoded-name group))
1393 (gnus-agent-directory))))
1395 (defun gnus-agent-get-function (method)
1396 (if (gnus-online method)
1401 (defun gnus-agent-covered-methods ()
1402 "Return the subset of methods that are covered by the agent."
1403 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1405 ;;; History functions
1407 (defun gnus-agent-history-buffer ()
1408 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1410 (defun gnus-agent-open-history ()
1412 (push (cons (gnus-agent-method)
1413 (set-buffer (gnus-get-buffer-create
1414 (format " *Gnus agent %s history*"
1415 (gnus-agent-method)))))
1416 gnus-agent-history-buffers)
1417 (mm-disable-multibyte) ;; everything is binary
1420 (let ((file (gnus-agent-lib-file "history")))
1421 (when (file-exists-p file)
1422 (nnheader-insert-file-contents file))
1423 (set (make-local-variable 'gnus-agent-file-name) file))))
1425 (defun gnus-agent-close-history ()
1426 (when (gnus-buffer-live-p gnus-agent-current-history)
1427 (kill-buffer gnus-agent-current-history)
1428 (setq gnus-agent-history-buffers
1429 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1430 gnus-agent-history-buffers))))
1436 (defun gnus-agent-fetch-articles (group articles)
1437 "Fetch ARTICLES from GROUP and put them into the Agent."
1439 (gnus-agent-load-alist group)
1440 (let* ((alist gnus-agent-article-alist)
1441 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1442 (selected-sets (list nil))
1443 (current-set-size 0)
1446 ;; Check each article
1447 (while (setq article (pop articles))
1448 ;; Skip alist entries preceeding this article
1449 (while (> article (or (caar alist) (1+ article)))
1450 (setq alist (cdr alist)))
1452 ;; Prune off articles that we have already fetched.
1453 (unless (and (eq article (caar alist))
1455 ;; Skip headers preceeding this article
1458 (let* ((header (car headers)))
1460 (mail-header-number header)
1462 (setq headers (cdr headers)))
1464 ;; Add this article to the current set
1465 (setcar selected-sets (cons article (car selected-sets)))
1467 ;; Update the set size, when the set is too large start a
1468 ;; new one. I do this after adding the article as I want at
1469 ;; least one article in each set.
1470 (when (< gnus-agent-max-fetch-size
1471 (setq current-set-size
1473 (if (= header-number article)
1474 (let ((char-size (mail-header-chars
1476 (if (<= char-size 0)
1477 ;; The char size was missing/invalid,
1478 ;; assume a worst-case situation of
1479 ;; 65 char/line. If the line count
1480 ;; is missing, arbitrarily assume a
1481 ;; size of 1000 characters.
1482 (max (* 65 (mail-header-lines
1487 (setcar selected-sets (nreverse (car selected-sets)))
1488 (setq selected-sets (cons nil selected-sets)
1489 current-set-size 0))))
1491 (when (or (cdr selected-sets) (car selected-sets))
1492 (let* ((fetched-articles (list nil))
1493 (tail-fetched-articles fetched-articles)
1494 (dir (gnus-agent-group-pathname group))
1495 (date (time-to-days (current-time)))
1496 (case-fold-search t)
1499 (setcar selected-sets (nreverse (car selected-sets)))
1500 (setq selected-sets (nreverse selected-sets))
1502 (gnus-make-directory dir)
1503 (gnus-message 7 "Fetching articles for %s..." group)
1506 (while (setq articles (pop selected-sets))
1507 ;; Fetch the articles from the backend.
1508 (if (gnus-check-backend-function 'retrieve-articles group)
1509 (setq pos (gnus-retrieve-articles articles group))
1512 (while (setq article (pop articles))
1513 (gnus-message 10 "Fetching article %s for %s..."
1516 (gnus-backlog-request-article group article
1518 (gnus-request-article article group))
1519 (goto-char (point-max))
1520 (push (cons article (point)) pos)
1521 (insert-buffer-substring nntp-server-buffer)))
1523 nntp-server-buffer (point-min) (point-max))
1524 (setq pos (nreverse pos)))))
1525 ;; Then save these articles into the Agent.
1527 (set-buffer nntp-server-buffer)
1529 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1530 (goto-char (point-min))
1531 (unless (eobp) ;; Don't save empty articles.
1532 (when (search-forward "\n\n" nil t)
1533 (when (search-backward "\nXrefs: " nil t)
1534 ;; Handle cross posting.
1535 (goto-char (match-end 0)) ; move to end of header name
1536 (skip-chars-forward "^ ") ; skip server name
1537 (skip-chars-forward " ")
1539 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1540 (push (cons (buffer-substring (match-beginning 1)
1543 (buffer-substring (match-beginning 2)
1546 (goto-char (match-end 0)))
1547 (gnus-agent-crosspost crosses (caar pos) date)))
1548 (goto-char (point-min))
1549 (if (not (re-search-forward
1550 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1551 (setq id "No-Message-ID-in-article")
1552 (setq id (buffer-substring
1553 (match-beginning 1) (match-end 1))))
1554 (let ((coding-system-for-write
1555 gnus-agent-file-coding-system))
1556 (write-region (point-min) (point-max)
1557 (concat dir (number-to-string (caar pos)))
1560 (gnus-agent-append-to-list
1561 tail-fetched-articles (caar pos)))
1563 (setq pos (cdr pos)))))
1565 (gnus-agent-save-alist group (cdr fetched-articles) date)
1566 (gnus-message 7 ""))
1567 (cdr fetched-articles))))))
1569 (defun gnus-agent-unfetch-articles (group articles)
1570 "Delete ARTICLES that were fetched from GROUP into the agent."
1572 (gnus-agent-load-alist group)
1573 (let* ((alist (cons nil gnus-agent-article-alist))
1574 (articles (sort articles #'<))
1575 (next-possibility alist)
1576 (delete-this (pop articles)))
1577 (while (and (cdr next-possibility) delete-this)
1578 (let ((have-this (caar (cdr next-possibility))))
1579 (cond ((< delete-this have-this)
1580 (setq delete-this (pop articles)))
1581 ((= delete-this have-this)
1582 (let ((timestamp (cdar (cdr next-possibility))))
1584 (let* ((file-name (concat (gnus-agent-group-pathname group)
1585 (number-to-string have-this))))
1586 (delete-file file-name))))
1588 (setcdr next-possibility (cddr next-possibility)))
1590 (setq next-possibility (cdr next-possibility))))))
1591 (setq gnus-agent-article-alist (cdr alist))
1592 (gnus-agent-save-alist group))))
1594 (defun gnus-agent-crosspost (crosses article &optional date)
1595 (setq date (or date t))
1597 (let (gnus-agent-article-alist group alist beg end)
1599 (set-buffer gnus-agent-overview-buffer)
1600 (when (nnheader-find-nov-line article)
1603 (setq end (progn (forward-line 1) (point)))))
1605 (setq group (caar crosses))
1606 (unless (setq alist (assoc group gnus-agent-group-alist))
1607 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1608 gnus-agent-group-alist))
1609 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1611 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1613 (when (= (point-max) (point-min))
1614 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1616 (nnheader-insert-file-contents
1617 (gnus-agent-article-name ".overview" group))))
1618 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1619 (insert (string-to-number (cdar crosses)))
1620 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1621 (gnus-agent-check-overview-buffer))
1622 (setq crosses (cdr crosses)))))
1624 (defun gnus-agent-backup-overview-buffer ()
1625 (when gnus-newsgroup-name
1626 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1629 (while (file-exists-p
1630 (setq name (concat root "~"
1631 (int-to-string (setq cnt (1+ cnt))) "~"))))
1632 (write-region (point-min) (point-max) name nil 'no-msg)
1633 (gnus-message 1 "Created backup copy of overview in %s." name)))
1636 (defun gnus-agent-check-overview-buffer (&optional buffer)
1637 "Check the overview file given for sanity.
1638 In particular, checks that the file is sorted by article number
1639 and that there are no duplicates."
1644 (set-buffer buffer))
1647 (goto-char (point-min))
1649 (while (< (point) (point-max))
1651 (cur (condition-case nil
1652 (read (current-buffer))
1655 ((or (not (integerp cur))
1656 (not (eq (char-after) ?\t)))
1658 (setq backed-up (gnus-agent-backup-overview-buffer)))
1660 "Overview buffer contains garbage '%s'."
1662 p (gnus-point-at-eol))))
1665 (setq backed-up (gnus-agent-backup-overview-buffer)))
1667 "Duplicate overview line for %d" cur)
1668 (delete-region p (progn (forward-line 1) (point))))
1671 (setq backed-up (gnus-agent-backup-overview-buffer)))
1672 (gnus-message 1 "Overview buffer not sorted!")
1673 (sort-numeric-fields 1 (point-min) (point-max))
1674 (goto-char (point-min))
1677 (setq prev-num cur)))
1678 (forward-line 1)))))))
1680 (defun gnus-agent-flush-cache ()
1682 (while gnus-agent-buffer-alist
1683 (set-buffer (cdar gnus-agent-buffer-alist))
1684 (let ((coding-system-for-write
1685 gnus-agent-file-coding-system))
1686 (write-region (point-min) (point-max)
1687 (gnus-agent-article-name ".overview"
1688 (caar gnus-agent-buffer-alist))
1690 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1691 (while gnus-agent-group-alist
1692 (with-temp-file (gnus-agent-article-name
1693 ".agentview" (caar gnus-agent-group-alist))
1694 (princ (cdar gnus-agent-group-alist))
1696 (princ 1 (current-buffer))
1698 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1701 (defun gnus-agent-find-parameter (group symbol)
1702 "Search for GROUPs SYMBOL in the group's parameters, the group's
1703 topic parameters, the group's category, or the customizable
1704 variables. Returns the first non-nil value found."
1705 (or (gnus-group-find-parameter group symbol t)
1706 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1710 '((agent-short-article . gnus-agent-short-article)
1711 (agent-long-article . gnus-agent-long-article)
1712 (agent-low-score . gnus-agent-low-score)
1713 (agent-high-score . gnus-agent-high-score)
1714 (agent-days-until-old . gnus-agent-expire-days)
1715 (agent-enable-expiration
1716 . gnus-agent-enable-expiration)
1717 (agent-predicate . gnus-agent-predicate)))))))
1719 (defun gnus-agent-fetch-headers (group &optional force)
1720 "Fetch interesting headers into the agent. The group's overview
1721 file will be updated to include the headers while a list of available
1722 article numbers will be returned."
1723 (let* ((fetch-all (and gnus-agent-consider-all-articles
1724 ;; Do not fetch all headers if the predicate
1725 ;; implies that we only consider unread articles.
1726 (not (gnus-predicate-implies-unread
1727 (gnus-agent-find-parameter group
1728 'agent-predicate)))))
1729 (articles (if fetch-all
1730 (gnus-uncompress-range (gnus-active group))
1731 (gnus-list-of-unread-articles group)))
1732 (gnus-decode-encoded-word-function 'identity)
1733 (file (gnus-agent-article-name ".overview" group)))
1736 ;; Add articles with marks to the list of article headers we want to
1737 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1738 ;; mark, but do fetch recent or seen articles if they have other, more
1739 ;; interesting marks. (We have to fetch articles with boring marks
1740 ;; because otherwise the agent will remove their marks.)
1741 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1742 (unless (memq (car arts) '(seen recent killed cache))
1743 (setq articles (gnus-range-add articles (cdr arts)))))
1744 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1746 ;; At this point, I have the list of articles to consider for
1747 ;; fetching. This is the list that I'll return to my caller. Some
1748 ;; of these articles may have already been fetched. That's OK as
1749 ;; the fetch article code will filter those out. Internally, I'll
1750 ;; filter this list to just those articles whose headers need to
1752 (let ((articles articles))
1753 ;; Remove known articles.
1754 (when (and (or gnus-agent-cache
1756 (gnus-agent-load-alist group))
1757 ;; Remove articles marked as downloaded.
1759 ;; I want to fetch all headers in the active range.
1760 ;; Therefore, exclude only those headers that are in the
1762 ;; NOTE: This is probably NOT what I want to do after
1763 ;; agent expiration in this group.
1764 (setq articles (gnus-agent-uncached-articles articles group))
1766 ;; I want to only fetch those headers that have never been
1767 ;; fetched. Therefore, exclude all headers that are, or
1768 ;; WERE, in the article alist.
1769 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1770 (high (cdr (gnus-active group))))
1771 ;; Low can be greater than High when the same group is
1772 ;; fetched twice in the same session {The first fetch will
1773 ;; fill the article alist such that (last
1774 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1775 ;; group))}. The addition of one(the 1+ above) then
1776 ;; forces Low to be greater than High. When this happens,
1777 ;; gnus-list-range-intersection returns nil which
1778 ;; indicates that no headers need to be fetched. -- Kevin
1779 (setq articles (gnus-list-range-intersection
1780 articles (list (cons low high)))))))
1783 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1784 (gnus-compress-sequence articles t))
1787 (set-buffer nntp-server-buffer)
1791 (gnus-message 7 "Fetching headers for %s..." group)
1794 (gnus-make-directory (nnheader-translate-file-chars
1795 (file-name-directory file) t))
1797 (unless (eq 'nov (gnus-retrieve-headers articles group))
1798 (nnvirtual-convert-headers))
1799 (gnus-agent-check-overview-buffer)
1800 ;; Move these headers to the overview buffer so that
1801 ;; gnus-agent-braid-nov can merge them with the contents
1804 gnus-agent-overview-buffer (point-min) (point-max))
1805 ;; NOTE: Call g-a-brand-nov even when the file does not
1806 ;; exist. As a minimum, it will validate the article
1807 ;; numbers already in the buffer.
1808 (gnus-agent-braid-nov group articles file)
1809 (let ((coding-system-for-write
1810 gnus-agent-file-coding-system))
1811 (gnus-agent-check-overview-buffer)
1812 (write-region (point-min) (point-max) file nil 'silent))
1813 (gnus-agent-save-alist group articles nil)
1817 (nnheader-insert-file-contents file)))))
1820 (defsubst gnus-agent-read-article-number ()
1821 "Reads the article number at point. Returns nil when a valid article number can not be read."
1823 ;; It is unfortunite but the read function quietly overflows
1824 ;; integer. As a result, I have to use string operations to test
1825 ;; for overflow BEFORE calling read.
1826 (when (looking-at "[0-9]+\t")
1827 (let ((len (- (match-end 0) (match-beginning 0))))
1829 (read (current-buffer)))
1831 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1832 ;; Back convert from int to string to ensure that this is one of them.
1833 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1834 (num (read (current-buffer)))
1835 (str2 (int-to-string num)))
1836 (when (equal str1 str2)
1839 (defsubst gnus-agent-copy-nov-line (article)
1840 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1842 (set-buffer gnus-agent-overview-buffer)
1843 (while (and (not (eobp))
1844 (or (not (setq art (gnus-agent-read-article-number)))
1849 (not (eq article art)))
1850 (set-buffer nntp-server-buffer)
1852 (setq e (progn (forward-line 1) (point)))
1853 (set-buffer nntp-server-buffer)
1854 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1856 (defun gnus-agent-braid-nov (group articles file)
1857 "Merge agent overview data with given file.
1858 Takes unvalidated headers for ARTICLES from
1859 `gnus-agent-overview-buffer' and validated headers from the given
1860 FILE and places the combined valid headers into
1861 `nntp-server-buffer'. This function can be used, when file
1862 doesn't exist, to valid the overview buffer."
1864 (set-buffer gnus-agent-overview-buffer)
1865 (goto-char (point-min))
1866 (set-buffer nntp-server-buffer)
1868 (when (file-exists-p file)
1869 (nnheader-insert-file-contents file))
1870 (goto-char (point-max))
1873 (unless (or (= (point-min) (point-max))
1874 (< (setq last (read (current-buffer))) (car articles)))
1875 ;; Old and new overlap -- We do it the hard way.
1876 (when (nnheader-find-nov-line (car articles))
1877 ;; Replacing existing NOV entry
1878 (delete-region (point) (progn (forward-line 1) (point))))
1879 (gnus-agent-copy-nov-line (pop articles))
1883 (while (let ((art (read (current-buffer))))
1884 (cond ((< art (car articles))
1887 ((= art (car articles))
1890 (point) (progn (forward-line 1) (point)))
1896 (gnus-agent-copy-nov-line (pop articles)))))
1898 (goto-char (point-max))
1900 ;; Append the remaining lines
1903 (set-buffer gnus-agent-overview-buffer)
1904 (setq start (point))
1905 (set-buffer nntp-server-buffer))
1908 (insert-buffer-substring gnus-agent-overview-buffer start)
1911 (setq last (or last -134217728))
1914 (setq art (gnus-agent-read-article-number))
1916 ;; Bad art num - delete this line
1918 (delete-region (point) (progn (forward-line 1) (point))))
1920 ;; Art num out of order - enable sort
1928 (sort-numeric-fields 1 (point-min) (point-max)))))))
1930 ;; Keeps the compiler from warning about the free variable in
1931 ;; gnus-agent-read-agentview.
1933 (defvar gnus-agent-read-agentview))
1935 (defun gnus-agent-load-alist (group)
1936 "Load the article-state alist for GROUP."
1937 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1938 (let ((gnus-agent-read-agentview group))
1939 (setq gnus-agent-article-alist
1940 (gnus-cache-file-contents
1941 (gnus-agent-article-name ".agentview" group)
1942 'gnus-agent-file-loading-cache
1943 'gnus-agent-read-agentview))))
1945 ;; Save format may be either 1 or 2. Two is the new, compressed
1946 ;; format that is still being tested. Format 1 is uncompressed but
1947 ;; known to be reliable.
1948 (defconst gnus-agent-article-alist-save-format 2)
1950 (defun gnus-agent-read-agentview (file)
1951 "Load FILE and do a `read' there."
1955 (nnheader-insert-file-contents file)
1956 (goto-char (point-min))
1957 (let ((alist (read (current-buffer)))
1958 (version (condition-case nil (read (current-buffer))
1964 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version))
1966 (let ((inhibit-quit t)
1968 (gnus-agent-open-history)
1969 (set-buffer (gnus-agent-history-buffer))
1970 (goto-char (point-min))
1972 (if (and (looking-at
1973 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1974 (string= (match-string 2)
1975 gnus-agent-read-agentview)
1976 (setq entry (assoc (string-to-number (match-string 3)) alist)))
1977 (setcdr entry (string-to-number (match-string 1))))
1979 (gnus-agent-close-history)
1980 (setq changed-version t)))
1982 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
1987 (let ((state (car comp-list))
1989 (gnus-uncompress-range
1991 (mapcar (lambda (article-id)
1992 (setq uncomp (cons (cons article-id state) uncomp)))
1995 (setq alist (sort uncomp 'car-less-than-car)))))
1996 (when changed-version
1997 (let ((gnus-agent-article-alist alist))
1998 (gnus-agent-save-alist gnus-agent-read-agentview)))
2002 (defun gnus-agent-save-alist (group &optional articles state)
2003 "Save the article-state alist for GROUP."
2004 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2005 (prev (cons nil gnus-agent-article-alist))
2007 print-level print-length item article)
2008 (while (setq article (pop articles))
2009 (while (and (cdr prev)
2010 (< (caadr prev) article))
2011 (setq prev (cdr prev)))
2014 (setcdr prev (list (cons article state))))
2015 ((> (caadr prev) article)
2016 (setcdr prev (cons (cons article state) (cdr prev))))
2017 ((= (caadr prev) article)
2018 (setcdr (cadr prev) state)))
2019 (setq prev (cdr prev)))
2020 (setq gnus-agent-article-alist (cdr all))
2022 (gnus-agent-set-local group
2023 (caar gnus-agent-article-alist)
2024 (caar (last gnus-agent-article-alist)))
2026 (gnus-make-directory (gnus-agent-article-name "" group))
2027 (with-temp-file (gnus-agent-article-name ".agentview" group)
2028 (cond ((eq gnus-agent-article-alist-save-format 1)
2029 (princ gnus-agent-article-alist (current-buffer)))
2030 ((eq gnus-agent-article-alist-save-format 2)
2031 (let ((compressed nil))
2032 (mapcar (lambda (pair)
2033 (let* ((article-id (car pair))
2034 (day-of-download (cdr pair))
2035 (comp-list (assq day-of-download compressed)))
2038 (cons article-id (cdr comp-list)))
2040 (cons (list day-of-download article-id)
2042 nil)) gnus-agent-article-alist)
2043 (mapcar (lambda (comp-list)
2045 (gnus-compress-sequence
2046 (nreverse (cdr comp-list)))))
2048 (princ compressed (current-buffer)))))
2050 (princ gnus-agent-article-alist-save-format (current-buffer))
2053 (defvar gnus-agent-article-local nil)
2054 (defvar gnus-agent-file-loading-local nil)
2056 (defun gnus-agent-load-local (&optional method)
2057 "Load the METHOD'S local file. The local file contains min/max
2058 article counts for each of the method's subscribed groups."
2059 (let ((gnus-command-method (or method gnus-command-method)))
2060 (setq gnus-agent-article-local
2061 (gnus-cache-file-contents
2062 (gnus-agent-lib-file "local")
2063 'gnus-agent-file-loading-local
2064 'gnus-agent-read-and-cache-local))))
2066 (defun gnus-agent-read-and-cache-local (file)
2067 "Load and read FILE then bind its contents to
2068 gnus-agent-article-local. If that variable had `dirty' (also known as
2069 modified) original contents, they are first saved to their own file."
2071 (if (and gnus-agent-article-local
2072 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2073 (gnus-agent-save-local))
2074 (gnus-agent-read-local file))
2076 (defun gnus-agent-read-local (file)
2077 "Load FILE and do a `read' there."
2078 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2083 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2084 (nnheader-insert-file-contents file))
2087 (goto-char (point-min))
2088 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2089 (while (= (following-char) ?\;)
2091 (setq line (1+ line)))
2098 (cur (current-buffer)))
2099 (setq group (read cur)
2103 (when (stringp group)
2104 (setq group (intern group my-obarray)))
2106 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2107 (set group (cons (+ 0 min) (+ 0 max))))
2109 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2110 file line (error-message-string err))))
2112 (setq line (1+ line))))
2114 (set (intern "+dirty" my-obarray) nil)
2115 (set (intern "+method" my-obarray) gnus-command-method)
2118 (defun gnus-agent-save-local (&optional force)
2119 "Save gnus-agent-article-local under it method's agent.lib directory."
2120 (let ((my-obarray gnus-agent-article-local))
2121 (when (and my-obarray
2122 (or force (symbol-value (intern "+dirty" my-obarray))))
2123 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2124 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2125 (dest (gnus-agent-lib-file "local")))
2126 (gnus-make-directory (gnus-agent-lib-file ""))
2128 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2129 (with-temp-file dest
2130 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2131 (file-name-coding-system nnmail-pathname-coding-system)
2132 print-level print-length item article
2133 (standard-output (current-buffer)))
2134 (mapatoms (lambda (symbol)
2135 (cond ((not (boundp symbol))
2137 ((member (symbol-name symbol) '("+dirty" "+method"))
2141 (let ((range (symbol-value symbol)))
2149 (defun gnus-agent-get-local (group &optional gmane method)
2150 (let* ((gmane (or gmane (gnus-group-real-name group)))
2151 (gnus-command-method (or method (gnus-find-method-for-group group)))
2152 (local (gnus-agent-load-local))
2153 (symb (intern gmane local))
2154 (minmax (and (boundp symb) (symbol-value symb))))
2156 ;; Bind these so that gnus-agent-load-alist doesn't change the
2157 ;; current alist (i.e. gnus-agent-article-alist)
2158 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2159 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2160 (alist (gnus-agent-load-alist group)))
2164 (caar (last alist))))
2165 (gnus-agent-set-local group (car minmax) (cdr minmax)
2166 gmane gnus-command-method local))))
2169 (defun gnus-agent-set-local (group min max &optional gmane method local)
2170 (let* ((gmane (or gmane (gnus-group-real-name group)))
2171 (gnus-command-method (or method (gnus-find-method-for-group group)))
2172 (local (or local (gnus-agent-load-local)))
2173 (symb (intern gmane local))
2174 (minmax (and (boundp symb) (symbol-value symb))))
2176 (if (cond ((and minmax
2177 (or (not (eq min (car minmax)))
2178 (not (eq max (cdr minmax)))))
2185 (set symb (cons min max))
2188 (unintern symb local)))
2189 (set (intern "+dirty" local) t))))
2191 (defun gnus-agent-article-name (article group)
2192 (expand-file-name article
2193 (file-name-as-directory
2194 (gnus-agent-group-pathname group))))
2196 (defun gnus-agent-batch-confirmation (msg)
2197 "Show error message and return t."
2198 (gnus-message 1 msg)
2202 (defun gnus-agent-batch-fetch ()
2203 "Start Gnus and fetch session."
2206 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2207 (gnus-agent-fetch-session))
2210 (defun gnus-agent-fetch-session ()
2211 "Fetch all articles and headers that are eligible for fetching."
2213 (unless gnus-agent-covered-methods
2214 (error "No servers are covered by the Gnus agent"))
2215 (unless gnus-plugged
2216 (error "Can't fetch articles while Gnus is unplugged"))
2217 (let ((methods (gnus-agent-covered-methods))
2218 groups group gnus-command-method)
2221 (setq gnus-command-method (car methods))
2222 (when (and (or (gnus-server-opened gnus-command-method)
2223 (gnus-open-server gnus-command-method))
2224 (gnus-online gnus-command-method))
2225 (setq groups (gnus-groups-from-server (car methods)))
2226 (gnus-agent-with-fetch
2227 (while (setq group (pop groups))
2228 (when (<= (gnus-group-level group)
2229 gnus-agent-handle-level)
2230 (if (or debug-on-error debug-on-quit)
2231 (gnus-agent-fetch-group-1
2232 group gnus-command-method)
2234 (gnus-agent-fetch-group-1
2235 group gnus-command-method)
2237 (unless (funcall gnus-agent-confirmation-function
2238 (format "Error %s while fetching session. Should gnus continue? "
2239 (error-message-string err)))
2240 (error "Cannot fetch articles into the Gnus agent")))
2242 (gnus-agent-regenerate-group group)
2243 (unless (funcall gnus-agent-confirmation-function
2245 "%s while fetching session. Should gnus continue? "
2246 (error-message-string err)))
2248 "Cannot fetch articles into the Gnus agent")))))))))
2249 (setq methods (cdr methods)))
2250 (gnus-run-hooks 'gnus-agent-fetched-hook)
2251 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2253 (defun gnus-agent-fetch-group-1 (group method)
2255 (let ((gnus-command-method method)
2256 (gnus-newsgroup-name group)
2257 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2258 (gnus-newsgroup-headers gnus-newsgroup-headers)
2259 (gnus-newsgroup-scored gnus-newsgroup-scored)
2260 (gnus-use-cache gnus-use-cache)
2261 (gnus-summary-expunge-below gnus-summary-expunge-below)
2262 (gnus-summary-mark-below gnus-summary-mark-below)
2263 (gnus-orphan-score gnus-orphan-score)
2264 ;; Maybe some other gnus-summary local variables should also
2270 category predicate info marks score-param
2272 (unless (gnus-check-group group)
2273 (error "Can't open server for %s" group))
2276 (when (or gnus-newsgroup-active
2278 (gnus-activate-group group))
2279 (let ((marked-articles gnus-newsgroup-downloadable))
2280 ;; Identify the articles marked for download
2281 (unless gnus-newsgroup-active
2282 ;; The variable gnus-newsgroup-active was selected as I need
2283 ;; a gnus-summary local variable that is NOT bound to any
2284 ;; value (its global value should default to nil).
2285 (dolist (mark gnus-agent-download-marks)
2286 (let ((arts (cdr (assq mark (gnus-info-marks
2287 (setq info (gnus-get-info group)))))))
2289 (setq marked-articles (nconc (gnus-uncompress-range arts)
2292 (setq marked-articles (sort marked-articles '<))
2294 ;; Fetch any new articles from the server
2295 (setq articles (gnus-agent-fetch-headers group))
2297 ;; Merge new articles with marked
2298 (setq articles (sort (append marked-articles articles) '<))
2301 ;; Parse them and see which articles we want to fetch.
2302 (setq gnus-newsgroup-dependencies
2303 (or gnus-newsgroup-dependencies
2304 (make-vector (length articles) 0)))
2305 (setq gnus-newsgroup-headers
2306 (or gnus-newsgroup-headers
2307 (gnus-get-newsgroup-headers-xover articles nil nil
2309 ;; `gnus-agent-overview-buffer' may be killed for
2310 ;; timeout reason. If so, recreate it.
2311 (gnus-agent-create-buffer)
2313 ;; Figure out how to select articles in this group
2314 (setq category (gnus-group-category group))
2318 (gnus-agent-find-parameter group 'agent-predicate)))
2320 ;; If the selection predicate requires scoring, score each header
2321 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2323 (gnus-agent-find-parameter group 'agent-score-file)))
2324 ;; Translate score-param into real one
2327 ((eq score-param 'file)
2328 (setq score-param (gnus-all-score-files group)))
2329 ((stringp (car score-param)))
2331 (setq score-param (list (list score-param)))))
2333 (gnus-score-headers score-param))))
2335 (unless (and (eq predicate 'gnus-agent-false)
2336 (not marked-articles))
2337 (let ((arts (list nil)))
2338 (let ((arts-tail arts)
2339 (alist (gnus-agent-load-alist group))
2340 (marked-articles marked-articles)
2341 (gnus-newsgroup-headers gnus-newsgroup-headers))
2342 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2343 (let ((num (mail-header-number gnus-headers)))
2344 ;; Determine if this article is already in the cache
2346 (> num (caar alist)))
2347 (setq alist (cdr alist)))
2349 (unless (and (eq num (caar alist))
2352 ;; Determine if this article was marked for download.
2353 (while (and marked-articles
2354 (> num (car marked-articles)))
2355 (setq marked-articles
2356 (cdr marked-articles)))
2358 ;; When this article is marked, or selected by the
2359 ;; predicate, add it to the download list
2360 (when (or (eq num (car marked-articles))
2363 (assq num gnus-newsgroup-scored))
2364 gnus-summary-default-score))
2365 (gnus-agent-long-article
2366 (gnus-agent-find-parameter
2367 group 'agent-long-article))
2368 (gnus-agent-short-article
2369 (gnus-agent-find-parameter
2370 group 'agent-short-article))
2371 (gnus-agent-low-score
2372 (gnus-agent-find-parameter
2373 group 'agent-low-score))
2374 (gnus-agent-high-score
2375 (gnus-agent-find-parameter
2376 group 'agent-high-score))
2377 (gnus-agent-expire-days
2378 (gnus-agent-find-parameter
2379 group 'agent-days-until-old)))
2380 (funcall predicate)))
2381 (gnus-agent-append-to-list arts-tail num))))))
2383 (let (fetched-articles)
2384 ;; Fetch all selected articles
2385 (setq gnus-newsgroup-undownloaded
2386 (gnus-sorted-ndifference
2387 gnus-newsgroup-undownloaded
2388 (setq fetched-articles
2390 (gnus-agent-fetch-articles group (cdr arts))
2393 (let ((unfetched-articles
2394 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2395 (if gnus-newsgroup-active
2396 ;; Update the summary buffer
2398 (dolist (article marked-articles)
2399 (gnus-summary-set-agent-mark article t))
2400 (dolist (article fetched-articles)
2401 (if gnus-agent-mark-unread-after-downloaded
2402 (gnus-summary-mark-article
2403 article gnus-unread-mark))
2404 (when (gnus-summary-goto-subject article nil t)
2405 (gnus-summary-update-download-mark article)))
2406 (dolist (article unfetched-articles)
2407 (gnus-summary-mark-article
2408 article gnus-canceled-mark)))
2410 ;; Update the group buffer.
2412 ;; When some, or all, of the marked articles came
2413 ;; from the download mark. Remove that mark. I
2414 ;; didn't do this earlier as I only want to remove
2415 ;; the marks after the fetch is completed.
2417 (dolist (mark gnus-agent-download-marks)
2418 (when (eq mark 'download)
2420 (assq mark (gnus-info-marks
2421 (setq info (gnus-get-info group))))))
2422 (when (cdr marked-arts)
2424 (delq marked-arts (gnus-info-marks info)))
2425 (gnus-info-set-marks info marks)))))
2426 (let ((read (gnus-info-read
2427 (or info (setq info (gnus-get-info group))))))
2429 info (gnus-add-to-range read unfetched-articles)))
2431 (gnus-group-update-group group t)
2435 (concat "(gnus-group-set-info '"
2436 (gnus-prin1-to-string info)
2440 ;;; Agent Category Mode
2443 (defvar gnus-category-mode-hook nil
2444 "Hook run in `gnus-category-mode' buffers.")
2446 (defvar gnus-category-line-format " %(%20c%): %g\n"
2447 "Format of category lines.
2449 Valid specifiers include:
2450 %c Topic name (string)
2451 %g The number of groups in the topic (integer)
2453 General format specifiers can also be used. See Info node
2454 `(gnus)Formatting Variables'.")
2456 (defvar gnus-category-mode-line-format "Gnus: %%b"
2457 "The format specification for the category mode line.")
2459 (defvar gnus-agent-predicate 'false
2460 "The selection predicate used when no other source is available.")
2462 (defvar gnus-agent-short-article 100
2463 "Articles that have fewer lines than this are short.")
2465 (defvar gnus-agent-long-article 200
2466 "Articles that have more lines than this are long.")
2468 (defvar gnus-agent-low-score 0
2469 "Articles that have a score lower than this have a low score.")
2471 (defvar gnus-agent-high-score 0
2472 "Articles that have a score higher than this have a high score.")
2475 ;;; Internal variables.
2477 (defvar gnus-category-buffer "*Agent Category*")
2479 (defvar gnus-category-line-format-alist
2480 `((?c gnus-tmp-name ?s)
2481 (?g gnus-tmp-groups ?d)))
2483 (defvar gnus-category-mode-line-format-alist
2484 `((?u user-defined ?s)))
2486 (defvar gnus-category-line-format-spec nil)
2487 (defvar gnus-category-mode-line-format-spec nil)
2489 (defvar gnus-category-mode-map nil)
2490 (put 'gnus-category-mode 'mode-class 'special)
2492 (unless gnus-category-mode-map
2493 (setq gnus-category-mode-map (make-sparse-keymap))
2494 (suppress-keymap gnus-category-mode-map)
2496 (gnus-define-keys gnus-category-mode-map
2497 "q" gnus-category-exit
2498 "k" gnus-category-kill
2499 "c" gnus-category-copy
2500 "a" gnus-category-add
2501 "e" gnus-agent-customize-category
2502 "p" gnus-category-edit-predicate
2503 "g" gnus-category-edit-groups
2504 "s" gnus-category-edit-score
2505 "l" gnus-category-list
2507 "\C-c\C-i" gnus-info-find-node
2508 "\C-c\C-b" gnus-bug))
2510 (defvar gnus-category-menu-hook nil
2511 "*Hook run after the creation of the menu.")
2513 (defun gnus-category-make-menu-bar ()
2514 (gnus-turn-off-edit-menu 'category)
2515 (unless (boundp 'gnus-category-menu)
2517 gnus-category-menu gnus-category-mode-map ""
2519 ["Add" gnus-category-add t]
2520 ["Kill" gnus-category-kill t]
2521 ["Copy" gnus-category-copy t]
2522 ["Edit category" gnus-agent-customize-category t]
2523 ["Edit predicate" gnus-category-edit-predicate t]
2524 ["Edit score" gnus-category-edit-score t]
2525 ["Edit groups" gnus-category-edit-groups t]
2526 ["Exit" gnus-category-exit t]))
2528 (gnus-run-hooks 'gnus-category-menu-hook)))
2530 (defun gnus-category-mode ()
2531 "Major mode for listing and editing agent categories.
2533 All normal editing commands are switched off.
2534 \\<gnus-category-mode-map>
2535 For more in-depth information on this mode, read the manual
2536 \(`\\[gnus-info-find-node]').
2538 The following commands are available:
2540 \\{gnus-category-mode-map}"
2542 (when (gnus-visual-p 'category-menu 'menu)
2543 (gnus-category-make-menu-bar))
2544 (kill-all-local-variables)
2545 (gnus-simplify-mode-line)
2546 (setq major-mode 'gnus-category-mode)
2547 (setq mode-name "Category")
2548 (gnus-set-default-directory)
2549 (setq mode-line-process nil)
2550 (use-local-map gnus-category-mode-map)
2551 (buffer-disable-undo)
2552 (setq truncate-lines t)
2553 (setq buffer-read-only t)
2554 (gnus-run-hooks 'gnus-category-mode-hook))
2556 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2558 (defun gnus-category-insert-line (category)
2559 (let* ((gnus-tmp-name (format "%s" (car category)))
2560 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2562 (gnus-add-text-properties
2566 (eval gnus-category-line-format-spec))
2567 (list 'gnus-category gnus-tmp-name))))
2569 (defun gnus-enter-category-buffer ()
2570 "Go to the Category buffer."
2572 (gnus-category-setup-buffer)
2573 (gnus-configure-windows 'category)
2574 (gnus-category-prepare))
2576 (defun gnus-category-setup-buffer ()
2577 (unless (get-buffer gnus-category-buffer)
2579 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2580 (gnus-category-mode))))
2582 (defun gnus-category-prepare ()
2583 (gnus-set-format 'category-mode)
2584 (gnus-set-format 'category t)
2585 (let ((alist gnus-category-alist)
2586 (buffer-read-only nil))
2589 (gnus-category-insert-line (pop alist)))
2590 (goto-char (point-min))
2591 (gnus-category-position-point)))
2593 (defun gnus-category-name ()
2594 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2595 (error "No category on the current line")))
2597 (defun gnus-category-read ()
2598 "Read the category alist."
2599 (setq gnus-category-alist
2603 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2604 (goto-char (point-min))
2605 ;; This code isn't temp, it will be needed so long as
2606 ;; anyone may be migrating from an older version.
2608 ;; Once we're certain that people will not revert to an
2609 ;; earlier version, we can take out the old-list code in
2610 ;; gnus-category-write.
2611 (let* ((old-list (read (current-buffer)))
2612 (new-list (ignore-errors (read (current-buffer)))))
2615 ;; Convert from a positional list to an alist.
2625 '(agent-predicate agent-score-file agent-groups))))
2628 (list (gnus-agent-cat-make 'default 'short)))))
2630 (defun gnus-category-write ()
2631 "Write the category alist."
2632 (setq gnus-category-predicate-cache nil
2633 gnus-category-group-cache nil)
2634 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2635 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2636 ;; This prin1 is temporary. It exists so that people can revert
2637 ;; to an earlier version of gnus-agent.
2638 (prin1 (mapcar (lambda (c)
2640 (cdr (assoc 'agent-predicate c))
2641 (cdr (assoc 'agent-score-file c))
2642 (cdr (assoc 'agent-groups c))))
2643 gnus-category-alist)
2646 (prin1 gnus-category-alist (current-buffer))))
2648 (defun gnus-category-edit-predicate (category)
2649 "Edit the predicate for CATEGORY."
2650 (interactive (list (gnus-category-name)))
2651 (let ((info (assq category gnus-category-alist)))
2653 (gnus-agent-cat-predicate info)
2654 (format "Editing the select predicate for category %s" category)
2655 `(lambda (predicate)
2656 ;; Avoid run-time execution of setf form
2657 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2659 ;; use its expansion instead:
2660 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2661 'agent-predicate predicate)
2663 (gnus-category-write)
2664 (gnus-category-list)))))
2666 (defun gnus-category-edit-score (category)
2667 "Edit the score expression for CATEGORY."
2668 (interactive (list (gnus-category-name)))
2669 (let ((info (assq category gnus-category-alist)))
2671 (gnus-agent-cat-score-file info)
2672 (format "Editing the score expression for category %s" category)
2673 `(lambda (score-file)
2674 ;; Avoid run-time execution of setf form
2675 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2677 ;; use its expansion instead:
2678 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2679 'agent-score-file score-file)
2681 (gnus-category-write)
2682 (gnus-category-list)))))
2684 (defun gnus-category-edit-groups (category)
2685 "Edit the group list for CATEGORY."
2686 (interactive (list (gnus-category-name)))
2687 (let ((info (assq category gnus-category-alist)))
2689 (gnus-agent-cat-groups info)
2690 (format "Editing the group list for category %s" category)
2692 ;; Avoid run-time execution of setf form
2693 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2695 ;; use its expansion instead:
2696 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2699 (gnus-category-write)
2700 (gnus-category-list)))))
2702 (defun gnus-category-kill (category)
2703 "Kill the current category."
2704 (interactive (list (gnus-category-name)))
2705 (let ((info (assq category gnus-category-alist))
2706 (buffer-read-only nil))
2708 (setq gnus-category-alist (delq info gnus-category-alist))
2709 (gnus-category-write)))
2711 (defun gnus-category-copy (category to)
2712 "Copy the current category."
2713 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2714 (let ((info (assq category gnus-category-alist)))
2715 (push (let ((newcat (gnus-copy-sequence info)))
2716 (setf (gnus-agent-cat-name newcat) to)
2717 (setf (gnus-agent-cat-groups newcat) nil)
2719 gnus-category-alist)
2720 (gnus-category-write)
2721 (gnus-category-list)))
2723 (defun gnus-category-add (category)
2724 "Create a new category."
2725 (interactive "SCategory name: ")
2726 (when (assq category gnus-category-alist)
2727 (error "Category %s already exists" category))
2728 (push (gnus-agent-cat-make category)
2729 gnus-category-alist)
2730 (gnus-category-write)
2731 (gnus-category-list))
2733 (defun gnus-category-list ()
2734 "List all categories."
2736 (gnus-category-prepare))
2738 (defun gnus-category-exit ()
2739 "Return to the group buffer."
2741 (kill-buffer (current-buffer))
2742 (gnus-configure-windows 'group t))
2744 ;; To avoid having 8-bit characters in the source file.
2745 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2747 (defvar gnus-category-predicate-alist
2748 '((spam . gnus-agent-spam-p)
2749 (short . gnus-agent-short-p)
2750 (long . gnus-agent-long-p)
2751 (low . gnus-agent-low-scored-p)
2752 (high . gnus-agent-high-scored-p)
2753 (read . gnus-agent-read-p)
2754 (true . gnus-agent-true)
2755 (false . gnus-agent-false))
2756 "Mapping from short score predicate symbols to predicate functions.")
2758 (defun gnus-agent-spam-p ()
2759 "Say whether an article is spam or not."
2760 (unless gnus-agent-spam-hashtb
2761 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2762 (if (not (equal (mail-header-references gnus-headers) ""))
2764 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2766 (gnus-gethash string gnus-agent-spam-hashtb)
2767 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2769 (defun gnus-agent-short-p ()
2770 "Say whether an article is short or not."
2771 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2773 (defun gnus-agent-long-p ()
2774 "Say whether an article is long or not."
2775 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2777 (defun gnus-agent-low-scored-p ()
2778 "Say whether an article has a low score or not."
2779 (< gnus-score gnus-agent-low-score))
2781 (defun gnus-agent-high-scored-p ()
2782 "Say whether an article has a high score or not."
2783 (> gnus-score gnus-agent-high-score))
2785 (defun gnus-agent-read-p ()
2786 "Say whether an article is read or not."
2787 (gnus-member-of-range (mail-header-number gnus-headers)
2788 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2790 (defun gnus-category-make-function (predicate)
2791 "Make a function from PREDICATE."
2792 (let ((func (gnus-category-make-function-1 predicate)))
2793 (if (and (= (length func) 1)
2794 (symbolp (car func)))
2796 (gnus-byte-compile `(lambda () ,func)))))
2798 (defun gnus-agent-true ()
2802 (defun gnus-agent-false ()
2806 (defun gnus-category-make-function-1 (predicate)
2807 "Make a function from PREDICATE."
2809 ;; Functions are just returned as is.
2810 ((or (symbolp predicate)
2811 (functionp predicate))
2812 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2814 ;; More complex predicate.
2817 ((memq (car predicate) '(& and))
2819 ((memq (car predicate) '(| or))
2821 ((memq (car predicate) gnus-category-not)
2823 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2825 (error "Unknown predicate type: %s" predicate))))
2827 (defun gnus-get-predicate (predicate)
2828 "Return the function implementing PREDICATE."
2829 (or (cdr (assoc predicate gnus-category-predicate-cache))
2830 (let ((func (gnus-category-make-function predicate)))
2831 (setq gnus-category-predicate-cache
2832 (nconc gnus-category-predicate-cache
2833 (list (cons predicate func))))
2836 (defun gnus-predicate-implies-unread (predicate)
2837 "Say whether PREDICATE implies unread articles only.
2838 It is okay to miss some cases, but there must be no false positives.
2839 That is, if this predicate returns true, then indeed the predicate must
2840 return only unread articles."
2841 (eq t (gnus-function-implies-unread-1
2842 (gnus-category-make-function-1 predicate))))
2844 (defun gnus-function-implies-unread-1 (function)
2845 "Recursively evaluate a predicate function to determine whether it can select
2846 any read articles. Returns t if the function is known to never
2847 return read articles, nil when it is known to always return read
2848 articles, and t_nil when the function may return both read and unread
2850 (let ((func (car function))
2851 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2852 (cond ((eq func 'and)
2853 (cond ((memq t args) ; if any argument returns only unread articles
2854 ;; then that argument constrains the result to only unread articles.
2856 ((memq 't_nil args) ; if any argument is indeterminate
2857 ;; then the result is indeterminate
2860 (cond ((memq nil args) ; if any argument returns read articles
2861 ;; then that argument ensures that the results includes read articles.
2863 ((memq 't_nil args) ; if any argument is indeterminate
2864 ;; then that argument ensures that the results are indeterminate
2866 (t ; if all arguments return only unread articles
2867 ;; then the result returns only unread articles
2870 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2871 ; then the result is indeterminate
2874 ; toggle the result to be the opposite of the argument
2876 ((eq func 'gnus-agent-read-p)
2877 nil) ; The read predicate NEVER returns unread articles
2878 ((eq func 'gnus-agent-false)
2879 t) ; The false predicate returns t as the empty set excludes all read articles
2880 ((eq func 'gnus-agent-true)
2881 nil) ; The true predicate ALWAYS returns read articles
2882 ((catch 'found-match
2883 (let ((alist gnus-category-predicate-alist))
2885 (if (eq func (cdar alist))
2886 (throw 'found-match t)
2887 (setq alist (cdr alist))))))
2888 't_nil) ; All other predicates return read and unread articles
2890 (error "Unknown predicate function: %s" function)))))
2892 (defun gnus-group-category (group)
2893 "Return the category GROUP belongs to."
2894 (unless gnus-category-group-cache
2895 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2896 (let ((cs gnus-category-alist)
2898 (while (setq cat (pop cs))
2899 (setq groups (gnus-agent-cat-groups cat))
2901 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2902 (or (gnus-gethash group gnus-category-group-cache)
2903 (assq 'default gnus-category-alist)))
2905 (defun gnus-agent-expire-group (group &optional articles force)
2906 "Expire all old articles in GROUP.
2907 If you want to force expiring of certain articles, this function can
2908 take ARTICLES, and FORCE parameters as well.
2910 The articles on which the expiration process runs are selected as follows:
2911 if ARTICLES is null, all read and unmarked articles.
2912 if ARTICLES is t, all articles.
2913 if ARTICLES is a list, just those articles.
2914 FORCE is equivalent to setting the expiration predicates to true."
2916 (list (let ((def (or (gnus-group-group-name)
2917 gnus-newsgroup-name)))
2918 (let ((select (read-string (if def
2919 (concat "Group Name ("
2922 (if (and (equal "" select)
2928 (gnus-agent-expire articles group force)
2929 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2930 ;; expiration statistics of this single group
2931 (gnus-agent-expire-stats (list 0 0 0.0)))
2932 (if (or (not (eq articles t))
2934 (concat "Are you sure that you want to "
2935 "expire all articles in " group ".")))
2936 (let ((gnus-command-method (gnus-find-method-for-group group))
2937 (overview (gnus-get-buffer-create " *expire overview*"))
2940 (let ((active-file (gnus-agent-lib-file "active")))
2941 (when (file-exists-p active-file)
2943 (nnheader-insert-file-contents active-file)
2944 (gnus-active-to-gnus-format
2946 (setq orig (gnus-make-hashtable
2947 (count-lines (point-min) (point-max))))))
2949 (gnus-agent-expire-group-1
2950 group overview (gnus-gethash-safe group orig)
2952 (kill-buffer overview))))
2953 (gnus-message 4 (gnus-agent-expire-done-message)))))
2955 (defun gnus-agent-expire-group-1 (group overview active articles force)
2956 ;; Internal function - requires caller to have set
2957 ;; gnus-command-method, initialized overview buffer, and to have
2958 ;; provided a non-nil active
2960 (let ((dir (gnus-agent-group-pathname group)))
2961 (when (boundp 'gnus-agent-expire-current-dirs)
2962 (set 'gnus-agent-expire-current-dirs
2964 (symbol-value 'gnus-agent-expire-current-dirs))))
2966 (if (and (not force)
2967 (eq 'DISABLE (gnus-agent-find-parameter group
2968 'agent-enable-expiration)))
2969 (gnus-message 5 "Expiry skipping over %s" group)
2970 (gnus-message 5 "Expiring articles in %s" group)
2971 (gnus-agent-load-alist group)
2972 (let* ((bytes-freed 0)
2974 (nov-entries-deleted 0)
2975 (info (gnus-get-info group))
2976 (alist gnus-agent-article-alist)
2977 (day (- (time-to-days (current-time))
2978 (gnus-agent-find-parameter group 'agent-days-until-old)))
2979 (specials (if (and alist
2981 ;; This could be a bit of a problem. I need to
2982 ;; keep the last article to avoid refetching
2983 ;; headers when using nntp in the backend. At
2984 ;; the same time, if someone uses a backend
2985 ;; that supports article moving then I may have
2986 ;; to remove the last article to complete the
2987 ;; move. Right now, I'm going to assume that
2988 ;; FORCE overrides specials.
2989 (list (caar (last alist)))))
2990 (unreads ;; Articles that are excluded from the
2991 ;; expiration process
2992 (cond (gnus-agent-expire-all
2993 ;; All articles are marked read by global decree
2996 ;; All articles are marked read by function
3000 ;; Unread articles are marked protected from
3001 ;; expiration Don't call
3002 ;; gnus-list-of-unread-articles as it returns
3003 ;; articles that have not been fetched into the
3006 (gnus-agent-unread-articles group)))
3008 ;; All articles EXCEPT those named by the caller
3009 ;; are protected from expiration
3010 (gnus-sorted-difference
3011 (gnus-uncompress-range
3013 (caar (last alist))))
3014 (sort articles '<)))))
3015 (marked ;; More articles that are excluded from the
3016 ;; expiration process
3017 (cond (gnus-agent-expire-all
3018 ;; All articles are unmarked by global decree
3021 ;; All articles are unmarked by function
3025 ;; All articles may as well be unmarked as the
3026 ;; unreads list already names the articles we are
3030 ;; Ticked and/or dormant articles are excluded
3033 (gnus-uncompress-range
3034 (cdr (assq 'tick (gnus-info-marks info))))
3035 (gnus-uncompress-range
3037 (gnus-info-marks info))))))))
3038 (nov-file (concat dir ".overview"))
3044 ;; The normal article alist contains elements that look like
3045 ;; (article# . fetch_date) I need to combine other
3046 ;; information with this list. For example, a flag indicating
3047 ;; that a particular article MUST BE KEPT. To do this, I'm
3048 ;; going to transform the elements to look like (article#
3049 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3050 ;; the process to generate the expired article alist.
3052 ;; Convert the alist elements to (article# fetch_date nil
3054 (setq dlist (mapcar (lambda (e)
3055 (list (car e) (cdr e) nil nil)) alist))
3057 ;; Convert the keep lists to elements that look like (article#
3058 ;; nil keep_flag nil) then append it to the expanded dlist
3059 ;; These statements are sorted by ascending precidence of the
3061 (setq dlist (nconc dlist
3063 (list e nil 'unread nil))
3065 (setq dlist (nconc dlist
3067 (list e nil 'marked nil))
3069 (setq dlist (nconc dlist
3071 (list e nil 'special nil))
3074 (set-buffer overview)
3076 (buffer-disable-undo)
3077 (when (file-exists-p nov-file)
3078 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3079 (nnheader-insert-file-contents nov-file)
3080 (goto-char (point-min))
3083 (while (< (setq p (point)) (point-max))
3085 ;; If I successfully read an integer (the plus zero
3086 ;; ensures a numeric type), prepend a marker entry
3088 (push (list (+ 0 (read (current-buffer))) nil nil
3089 (set-marker (make-marker) p))
3092 (gnus-message 1 "gnus-agent-expire: read error \
3093 occurred when reading expression at %s in %s. Skipping to next \
3094 line." (point) nov-file)))
3095 ;; Whether I succeeded, or failed, it doesn't matter.
3096 ;; Move to the next line then try again.
3100 7 "gnus-agent-expire: Loading overview... Done"))
3101 (set-buffer-modified-p nil)
3103 ;; At this point, all of the information is in dlist. The
3104 ;; only problem is that much of it is spread across multiple
3105 ;; entries. Sort then MERGE!!
3106 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3107 ;; If two entries have the same article-number then sort by
3108 ;; ascending keep_flag.
3115 (cond ((< (nth 0 a) (nth 0 b))
3117 ((> (nth 0 a) (nth 0 b))
3120 (let ((a (or (symbol-value (nth 2 a))
3122 (b (or (symbol-value (nth 2 b))
3125 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3126 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3127 (let ((dlist dlist))
3128 (while (cdr dlist) ; I'm not at the end-of-list
3129 (if (eq (caar dlist) (caadr dlist))
3130 (let ((first (cdr (car dlist)))
3131 (secnd (cdr (cadr dlist))))
3132 (setcar first (or (car first)
3133 (car secnd))) ; fetch_date
3134 (setq first (cdr first)
3136 (setcar first (or (car first)
3137 (car secnd))) ; Keep_flag
3138 (setq first (cdr first)
3140 (setcar first (or (car first)
3141 (car secnd))) ; NOV_entry_marker
3143 (setcdr dlist (cddr dlist)))
3144 (setq dlist (cdr dlist)))))
3145 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3147 (let* ((len (float (length dlist)))
3151 (let ((new-completed (truncate (* 100.0
3152 (/ (setq cnt (1+ cnt))
3155 (when (> new-completed completed)
3156 (setq completed new-completed)
3157 (gnus-message 7 "%3d%% completed..." completed)))
3158 (let* ((entry (car dlist))
3159 (article-number (nth 0 entry))
3160 (fetch-date (nth 1 entry))
3161 (keep (nth 2 entry))
3162 (marker (nth 3 entry)))
3165 ;; Kept articles are unread, marked, or special.
3167 (gnus-agent-message 10
3168 "gnus-agent-expire: %s:%d: Kept %s article%s."
3169 group article-number keep (if fetch-date " and file" ""))
3171 (unless (file-exists-p
3172 (concat dir (number-to-string
3174 (setf (nth 1 entry) nil)
3175 (gnus-agent-message 3 "gnus-agent-expire cleared \
3176 download flag on %s:%d as the cached article file is missing."
3177 group (caar dlist)))
3179 (gnus-message 1 "gnus-agent-expire detected a \
3180 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3181 (gnus-agent-append-to-list
3183 (cons article-number fetch-date)))
3185 ;; The following articles are READ, UNMARKED, and
3186 ;; ORDINARY. See if they can be EXPIRED!!!
3189 ((not (integerp fetch-date))
3190 'read) ;; never fetched article (may expire
3192 ((not (file-exists-p
3193 (concat dir (number-to-string
3195 (setf (nth 1 entry) nil)
3196 'externally-expired) ;; Can't find the cached
3197 ;; article. Handle case
3198 ;; as though this article
3199 ;; was never fetched.
3201 ;; We now have the arrival day, so we see
3202 ;; whether it's old enough to be expired.
3208 ;; I found some reason to expire this entry.
3210 (let ((actions nil))
3211 (when (memq type '(forced expired))
3212 (ignore-errors ; Just being paranoid.
3213 (let* ((file-name (nnheader-concat dir (number-to-string
3215 (size (float (nth 7 (file-attributes file-name)))))
3216 (incf bytes-freed size)
3217 (incf files-deleted)
3218 (delete-file file-name))
3219 (push "expired cached article" actions))
3220 (setf (nth 1 entry) nil)
3224 (push "NOV entry removed" actions)
3227 (incf nov-entries-deleted)
3229 (let ((from (gnus-point-at-bol))
3230 (to (progn (forward-line 1) (point))))
3231 (incf bytes-freed (- to from))
3232 (delete-region from to)))
3234 ;; If considering all articles is set, I can only
3235 ;; expire article IDs that are no longer in the
3236 ;; active range (That is, articles that preceed the
3237 ;; first article in the new alist).
3238 (if (and gnus-agent-consider-all-articles
3239 (>= article-number (car active)))
3240 ;; I have to keep this ID in the alist
3241 (gnus-agent-append-to-list
3242 tail-alist (cons article-number fetch-date))
3243 (push (format "Removed %s article number from \
3244 article alist" type) actions))
3247 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3248 group article-number
3249 (mapconcat 'identity actions ", ")))))
3252 10 "gnus-agent-expire: %s:%d: Article kept as \
3253 expiration tests failed." group article-number)
3254 (gnus-agent-append-to-list
3255 tail-alist (cons article-number fetch-date)))
3258 ;; Clean up markers as I want to recycle this buffer
3259 ;; over several groups.
3261 (set-marker marker nil))
3263 (setq dlist (cdr dlist))))
3265 (setq alist (cdr alist))
3267 (let ((inhibit-quit t))
3268 (unless (equal alist gnus-agent-article-alist)
3269 (setq gnus-agent-article-alist alist)
3270 (gnus-agent-save-alist group))
3272 (when (buffer-modified-p)
3273 (let ((coding-system-for-write
3274 gnus-agent-file-coding-system))
3275 (gnus-make-directory dir)
3276 (write-region (point-min) (point-max) nov-file nil
3278 ;; clear the modified flag as that I'm not confused by
3279 ;; its status on the next pass through this routine.
3280 (set-buffer-modified-p nil)))
3282 (when (eq articles t)
3283 (gnus-summary-update-info))))
3285 (when (boundp 'gnus-agent-expire-stats)
3286 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3287 (incf (nth 2 stats) bytes-freed)
3288 (incf (nth 1 stats) files-deleted)
3289 (incf (nth 0 stats) nov-entries-deleted)))
3292 (defun gnus-agent-expire (&optional articles group force)
3293 "Expire all old articles.
3294 If you want to force expiring of certain articles, this function can
3295 take ARTICLES, GROUP and FORCE parameters as well.
3297 The articles on which the expiration process runs are selected as follows:
3298 if ARTICLES is null, all read and unmarked articles.
3299 if ARTICLES is t, all articles.
3300 if ARTICLES is a list, just those articles.
3301 Setting GROUP will limit expiration to that group.
3302 FORCE is equivalent to setting the expiration predicates to true."
3306 (gnus-agent-expire-group group articles force)
3307 (if (or (not (eq articles t))
3308 (yes-or-no-p "Are you sure that you want to expire all \
3309 articles in every agentized group."))
3310 (let ((methods (gnus-agent-covered-methods))
3311 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3312 ;; of agent directories.
3313 (gnus-agent-expire-current-dirs nil)
3314 ;; Bind gnus-agent-expire-stats to enable tracking of
3315 ;; expiration statistics across all groups
3316 (gnus-agent-expire-stats (list 0 0 0.0))
3317 gnus-command-method overview orig)
3318 (setq overview (gnus-get-buffer-create " *expire overview*"))
3320 (while (setq gnus-command-method (pop methods))
3321 (let ((active-file (gnus-agent-lib-file "active")))
3322 (when (file-exists-p active-file)
3324 (nnheader-insert-file-contents active-file)
3325 (gnus-active-to-gnus-format
3327 (setq orig (gnus-make-hashtable
3328 (count-lines (point-min) (point-max))))))
3329 (dolist (expiring-group (gnus-groups-from-server
3330 gnus-command-method))
3332 (gnus-gethash-safe expiring-group orig)))
3336 (gnus-agent-expire-group-1
3337 expiring-group overview active articles force))))))))
3338 (kill-buffer overview))
3339 (gnus-agent-expire-unagentized-dirs)
3340 (gnus-message 4 (gnus-agent-expire-done-message))))))
3342 (defun gnus-agent-expire-done-message ()
3343 (if (and (> gnus-verbose 4)
3344 (boundp 'gnus-agent-expire-stats))
3345 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3346 (size (nth 2 stats))
3347 (units '(B KB MB GB)))
3348 (while (and (> size 1024.0)
3350 (setq size (/ size 1024.0)
3353 (format "Expiry recovered %d NOV entries, deleted %d files,\
3360 (defun gnus-agent-expire-unagentized-dirs ()
3361 (when (and gnus-agent-expire-unagentized-dirs
3362 (boundp 'gnus-agent-expire-current-dirs))
3363 (let* ((keep (gnus-make-hashtable))
3364 ;; Formally bind gnus-agent-expire-current-dirs so that the
3365 ;; compiler will not complain about free references.
3366 (gnus-agent-expire-current-dirs
3367 (symbol-value 'gnus-agent-expire-current-dirs))
3370 (gnus-sethash gnus-agent-directory t keep)
3371 (while gnus-agent-expire-current-dirs
3372 (setq dir (pop gnus-agent-expire-current-dirs))
3373 (when (and (stringp dir)
3374 (file-directory-p dir))
3375 (while (not (gnus-gethash dir keep))
3376 (gnus-sethash dir t keep)
3377 (setq dir (file-name-directory (directory-file-name dir))))))
3384 "Given a directory, check it and its subdirectories for
3385 membership in the keep hash. If it isn't found, add
3387 (let ((files (directory-files d))
3389 (while (setq file (pop files))
3390 (cond ((equal file ".") ; Ignore self
3392 ((equal file "..") ; Ignore parent
3394 ((equal file ".overview")
3395 ;; Directory must contain .overview to be
3396 ;; agent's cache of a group.
3397 (let ((d (file-name-as-directory d))
3399 ;; Search ancestor's for last directory NOT
3400 ;; found in keep hash.
3401 (while (not (gnus-gethash
3402 (setq d (file-name-directory d)) keep))
3404 d (directory-file-name d)))
3405 ;; if ANY ancestor was NOT in keep hash and
3406 ;; it it's already in to-remove, add it to
3409 (not (member r to-remove)))
3410 (push r to-remove))))
3411 ((file-directory-p (setq file (nnheader-concat d file)))
3412 (funcall checker file)))))))))
3413 (funcall checker (expand-file-name gnus-agent-directory))
3415 (when (and to-remove
3416 (or gnus-expert-user
3418 "gnus-agent-expire has identified local directories that are\
3419 not currently required by any agentized group. Do you wish to consider\
3422 (let ((dir (pop to-remove)))
3423 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3424 (let* (delete-recursive
3429 (if (file-directory-p f-or-d)
3431 (delete-directory f-or-d)
3434 (or (member f '("." ".."))
3435 (funcall delete-recursive
3438 (directory-files f-or-d))
3439 (delete-directory f-or-d)))
3440 (delete-file f-or-d)))))))
3441 (funcall delete-recursive dir))))))))))
3444 (defun gnus-agent-batch ()
3445 "Start Gnus, send queue and fetch session."
3447 (let ((init-file-user "")
3448 (gnus-always-read-dribble-file t))
3450 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3451 (gnus-group-send-queue)
3452 (gnus-agent-fetch-session)))
3454 (defun gnus-agent-unread-articles (group)
3455 (let* ((read (gnus-info-read (gnus-get-info group)))
3456 (known (gnus-agent-load-alist group))
3458 (tail-unread unread))
3459 (while (and known read)
3460 (let ((candidate (car (pop known))))
3461 (while (let* ((range (car read))
3462 (min (if (numberp range) range (car range)))
3463 (max (if (numberp range) range (cdr range))))
3464 (cond ((or (not min)
3466 (gnus-agent-append-to-list tail-unread candidate)
3469 (setq read (cdr read))
3470 ;; return t so that I always loop one more
3471 ;; time. If I just iterated off the end of
3472 ;; read, min will become nil and the current
3473 ;; candidate will be added to the unread list.
3476 (gnus-agent-append-to-list tail-unread (car (pop known))))
3479 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3480 "Restrict ARTICLES to numbers already fetched.
3481 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3482 that have already been fetched.
3483 If CACHED-HEADER is nil, articles are only excluded if the article itself
3486 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3487 ;; 'car gnus-agent-article-alist))
3489 ;; Functionally, I don't need to construct a temp list using mapcar.
3491 (if (and (or gnus-agent-cache (not gnus-plugged))
3492 (gnus-agent-load-alist group))
3493 (let* ((ref gnus-agent-article-alist)
3495 (uncached (list nil))
3496 (tail-uncached uncached))
3497 (while (and ref arts)
3498 (let ((v1 (car arts))
3500 (cond ((< v1 v2) ; v1 does not appear in the reference list
3501 (gnus-agent-append-to-list tail-uncached v1)
3502 (setq arts (cdr arts)))
3504 (unless (or cached-header (cdar ref)) ; v1 is already cached
3505 (gnus-agent-append-to-list tail-uncached v1))
3506 (setq arts (cdr arts))
3507 (setq ref (cdr ref)))
3508 (t ; reference article (v2) preceeds the list being filtered
3509 (setq ref (cdr ref))))))
3511 (gnus-agent-append-to-list tail-uncached (pop arts)))
3513 ;; if gnus-agent-load-alist fails, no articles are cached.
3516 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3518 (gnus-agent-create-buffer)
3519 (let ((gnus-decode-encoded-word-function 'identity)
3520 (file (gnus-agent-article-name ".overview" group))
3521 cached-articles uncached-articles)
3522 (gnus-make-directory (nnheader-translate-file-chars
3523 (file-name-directory file) t))
3525 ;; Populate temp buffer with known headers
3526 (when (file-exists-p file)
3527 (with-current-buffer gnus-agent-overview-buffer
3529 (let ((nnheader-file-coding-system
3530 gnus-agent-file-coding-system))
3531 (nnheader-insert-nov-file file (car articles)))))
3533 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3536 ;; Populate nntp-server-buffer with uncached headers
3537 (set-buffer nntp-server-buffer)
3539 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3540 (gnus-retrieve-headers
3541 uncached-articles group fetch-old))))
3542 (nnvirtual-convert-headers))
3543 ((eq 'nntp (car gnus-current-select-method))
3544 ;; The author of gnus-get-newsgroup-headers-xover
3545 ;; reports that the XOVER command is commonly
3546 ;; unreliable. The problem is that recently
3547 ;; posted articles may not be entered into the
3548 ;; NOV database in time to respond to my XOVER
3551 ;; I'm going to use his assumption that the NOV
3552 ;; database is updated in order of ascending
3553 ;; article ID. Therefore, a response containing
3554 ;; article ID N implies that all articles from 1
3555 ;; to N-1 are up-to-date. Therefore, missing
3556 ;; articles in that range have expired.
3558 (set-buffer nntp-server-buffer)
3559 (let* ((fetched-articles (list nil))
3560 (tail-fetched-articles fetched-articles)
3561 (min (cond ((numberp fetch-old)
3562 (max 1 (- (car articles) fetch-old)))
3567 (max (car (last articles))))
3569 ;; Get the list of articles that were fetched
3570 (goto-char (point-min))
3571 (let ((pm (point-max))
3573 (while (< (point) pm)
3574 (when (setq art (gnus-agent-read-article-number))
3575 (gnus-agent-append-to-list tail-fetched-articles art))
3578 ;; Clip this list to the headers that will
3579 ;; actually be returned
3580 (setq fetched-articles (gnus-list-range-intersection
3581 (cdr fetched-articles)
3584 ;; Clip the uncached articles list to exclude
3585 ;; IDs after the last FETCHED header. The
3586 ;; excluded IDs may be fetchable using HEAD.
3587 (if (car tail-fetched-articles)
3588 (setq uncached-articles
3589 (gnus-list-range-intersection
3591 (cons (car uncached-articles)
3592 (car tail-fetched-articles)))))
3594 ;; Create the list of articles that were
3595 ;; "successfully" fetched. Success, in this
3596 ;; case, means that the ID should not be
3597 ;; fetched again. In the case of an expired
3598 ;; article, the header will not be fetched.
3599 (setq uncached-articles
3600 (gnus-sorted-nunion fetched-articles
3604 ;; Erase the temp buffer
3605 (set-buffer gnus-agent-overview-buffer)
3608 ;; Copy the nntp-server-buffer to the temp buffer
3609 (set-buffer nntp-server-buffer)
3610 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3612 ;; Merge the temp buffer with the known headers (found on
3613 ;; disk in FILE) into the nntp-server-buffer
3614 (when uncached-articles
3615 (gnus-agent-braid-nov group uncached-articles file))
3617 ;; Save the new set of known headers to FILE
3618 (set-buffer nntp-server-buffer)
3619 (let ((coding-system-for-write
3620 gnus-agent-file-coding-system))
3621 (gnus-agent-check-overview-buffer)
3622 (write-region (point-min) (point-max) file nil 'silent))
3624 ;; Update the group's article alist to include the newly
3625 ;; fetched articles.
3626 (gnus-agent-load-alist group)
3627 (gnus-agent-save-alist group uncached-articles nil)
3630 ;; Copy the temp buffer to the nntp-server-buffer
3631 (set-buffer nntp-server-buffer)
3633 (insert-buffer-substring gnus-agent-overview-buffer)))
3636 (not (numberp fetch-old)))
3637 t ; Don't remove anything.
3638 (nnheader-nov-delete-outside-range
3639 (if fetch-old (max 1 (- (car articles) fetch-old))
3641 (car (last articles)))
3646 (defun gnus-agent-request-article (article group)
3647 "Retrieve ARTICLE in GROUP from the agent cache."
3648 (when (and gnus-agent
3649 (or gnus-agent-cache
3652 (let* ((gnus-command-method (gnus-find-method-for-group group))
3653 (file (gnus-agent-article-name (number-to-string article) group))
3654 (buffer-read-only nil))
3655 (when (and (file-exists-p file)
3656 (> (nth 7 (file-attributes file)) 0))
3658 (gnus-kill-all-overlays)
3659 (let ((coding-system-for-read gnus-cache-coding-system))
3660 (insert-file-contents file))
3663 (defun gnus-agent-regenerate-group (group &optional reread)
3665 If REREAD is t, all articles in the .overview are marked as unread.
3666 If REREAD is a list, the specified articles will be marked as unread.
3667 In addition, their NOV entries in .overview will be refreshed using
3668 the articles' current headers.
3669 If REREAD is not nil, downloaded articles are marked as unread."
3671 (list (let ((def (or (gnus-group-group-name)
3672 gnus-newsgroup-name)))
3673 (let ((select (read-string (if def
3674 (concat "Group Name ("
3677 (if (and (equal "" select)
3683 (cursor-in-echo-area t)
3684 (echo-keystrokes 0))
3685 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3686 (setq c (read-char-exclusive))
3688 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3690 ((or (eq c ?a) (eq c ?A))
3692 ((or (eq c ?d) (eq c ?D))
3693 (throw 'mark 'some)))
3694 (gnus-message 3 "Ignoring unexpected input")
3698 (gnus-message 5 "Regenerating in %s" group)
3699 (let* ((gnus-command-method (or gnus-command-method
3700 (gnus-find-method-for-group group)))
3701 (file (gnus-agent-article-name ".overview" group))
3702 (dir (file-name-directory file))
3704 (downloaded (if (file-exists-p dir)
3705 (sort (mapcar (lambda (name) (string-to-int name))
3706 (directory-files dir nil "^[0-9]+$" t))
3708 (progn (gnus-make-directory dir) nil)))
3713 (mm-with-unibyte-buffer
3714 (if (file-exists-p file)
3715 (let ((nnheader-file-coding-system
3716 gnus-agent-file-coding-system))
3717 (nnheader-insert-file-contents file)))
3718 (set-buffer-modified-p nil)
3720 ;; Load the article IDs found in the overview file. As a
3721 ;; side-effect, validate the file contents.
3725 (goto-char (point-min))
3726 (while (< (point) (point-max))
3727 (cond ((and (looking-at "[0-9]+\t")
3728 (<= (- (match-end 0) (match-beginning 0)) 9))
3729 (push (read (current-buffer)) nov-arts)
3731 (let ((l1 (car nov-arts))
3732 (l2 (cadr nov-arts)))
3733 (cond ((and (listp reread) (memq l1 reread))
3735 (setq nov-arts (cdr nov-arts))
3736 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3737 entry of article %s deleted." l1))
3741 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3742 entries are NOT in ascending order.")
3743 ;; Don't sort now as I haven't verified
3744 ;; that every line begins with a number
3748 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3749 entries contained duplicate of article %s. Duplicate deleted." l1)
3751 (setq nov-arts (cdr nov-arts))))))
3753 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3754 entries contained line that did not begin with an article number. Deleted\
3756 (gnus-delete-line))))
3758 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3759 entries into ascending order.")
3760 (sort-numeric-fields 1 (point-min) (point-max))
3761 (setq nov-arts nil))))
3762 (gnus-agent-check-overview-buffer)
3764 ;; Construct a new article alist whose nodes match every header
3765 ;; in the .overview file. As a side-effect, missing headers are
3766 ;; reconstructed from the downloaded article file.
3767 (while (or downloaded nov-arts)
3768 (cond ((and downloaded
3770 (> (car downloaded) (car nov-arts))))
3771 ;; This entry is missing from the overview file
3772 (gnus-message 3 "Regenerating NOV %s %d..." group
3774 (let ((file (concat dir (number-to-string (car downloaded)))))
3775 (mm-with-unibyte-buffer
3776 (nnheader-insert-file-contents file)
3777 (nnheader-remove-body)
3778 (setq header (nnheader-parse-naked-head)))
3779 (mail-header-set-number header (car downloaded))
3781 (let ((key (concat "^" (int-to-string (car nov-arts))
3783 (or (re-search-backward key nil t)
3784 (re-search-forward key))
3786 (goto-char (point-min)))
3787 (nnheader-insert-nov header))
3788 (setq nov-arts (cons (car downloaded) nov-arts)))
3789 ((eq (car downloaded) (car nov-arts))
3790 ;; This entry in the overview has been downloaded
3791 (push (cons (car downloaded)
3793 (nth 5 (file-attributes
3794 (concat dir (number-to-string
3795 (car downloaded))))))) alist)
3796 (setq downloaded (cdr downloaded))
3797 (setq nov-arts (cdr nov-arts)))
3799 ;; This entry in the overview has not been downloaded
3800 (push (cons (car nov-arts) nil) alist)
3801 (setq nov-arts (cdr nov-arts)))))
3803 ;; When gnus-agent-consider-all-articles is set,
3804 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3805 ;; the alist. Those IDs serve as markers to indicate that an
3806 ;; attempt has been made to fetch that article's header.
3808 ;; When gnus-agent-consider-all-articles is NOT set,
3809 ;; gnus-agent-regenerate-group can remove the article ID of every
3810 ;; article (with the exception of the last ID in the list - it's
3811 ;; special) that no longer appears in the overview. In this
3812 ;; situtation, the last article ID in the list implies that it,
3813 ;; and every article ID preceeding it, have been fetched from the
3816 (if gnus-agent-consider-all-articles
3817 ;; Restore all article IDs that were not found in the overview file.
3818 (let* ((n (cons nil alist))
3820 (o (gnus-agent-load-alist group)))
3822 (let ((nID (caadr n))
3825 (setq n (setcdr n (list (list oID))))
3828 (setcdr n (cons (list oID) (cdr n)))
3834 (setq n (cdr n))))))
3835 (setq alist (cdr merged)))
3836 ;; Restore the last article ID if it is not already in the new alist
3837 (let ((n (last alist))
3838 (o (last (gnus-agent-load-alist group))))
3842 (push (cons (caar o) nil) alist))
3843 ((< (caar n) (caar o))
3844 (setcdr n (list (car o)))))))
3846 (let ((inhibit-quit t))
3847 (if (setq regenerated (buffer-modified-p))
3848 (let ((coding-system-for-write gnus-agent-file-coding-system))
3849 (write-region (point-min) (point-max) file nil 'silent)))
3851 (setq regenerated (or regenerated
3852 (and reread gnus-agent-article-alist)
3853 (not (equal alist gnus-agent-article-alist))))
3855 (setq gnus-agent-article-alist alist)
3858 (gnus-agent-save-alist group)
3860 ;; I have to alter the group's active range NOW as
3861 ;; gnus-make-ascending-articles-unread will use it to
3862 ;; recalculate the number of unread articles in the group
3864 (let ((group (gnus-group-real-name group))
3865 (group-active (or (gnus-active group)
3866 (gnus-activate-group group))))
3867 (gnus-agent-possibly-alter-active group group-active)))))
3869 (when (and reread gnus-agent-article-alist)
3870 (gnus-make-ascending-articles-unread
3874 (delq nil (mapcar (function (lambda (c)
3875 (cond ((eq reread t)
3879 gnus-agent-article-alist))))
3881 (when (gnus-buffer-live-p gnus-group-buffer)
3882 (gnus-group-update-group group t)))
3888 (defun gnus-agent-regenerate (&optional clean reread)
3889 "Regenerate all agent covered files.
3890 If CLEAN, obsolete (ignore)."
3893 (gnus-message 4 "Regenerating Gnus agent files...")
3894 (dolist (gnus-command-method (gnus-agent-covered-methods))
3895 (dolist (group (gnus-groups-from-server gnus-command-method))
3896 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3898 (gnus-message 4 "Regenerating Gnus agent files...done")
3902 (defun gnus-agent-go-online (&optional force)
3903 "Switch servers into online status."
3904 (interactive (list t))
3905 (dolist (server gnus-opened-servers)
3906 (when (eq (nth 1 server) 'offline)
3907 (if (if (eq force 'ask)
3909 (format "Switch %s:%s into online status? "
3910 (caar server) (cadar server)))
3912 (setcar (nthcdr 1 server) 'close)))))
3914 (defun gnus-agent-toggle-group-plugged (group)
3915 "Toggle the status of the server of the current group."
3916 (interactive (list (gnus-group-group-name)))
3917 (let* ((method (gnus-find-method-for-group group))
3918 (status (cadr (assoc method gnus-opened-servers))))
3919 (if (eq status 'offline)
3920 (gnus-server-set-status method 'closed)
3921 (gnus-close-server method)
3922 (gnus-server-set-status method 'offline))
3923 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3924 (if (eq status 'offline) 'offline 'online)
3925 (if (eq status 'offline) 'online 'offline))))
3927 (defun gnus-agent-group-covered-p (group)
3928 (gnus-agent-method-p (gnus-group-method group)))
3930 (provide 'gnus-agent)
3932 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
3933 ;;; gnus-agent.el ends here