* src/eval.c (Fcalled_interactively_p): Doc fix. (Bug#11747)
[emacs.git] / lisp / gnus / gnus-agent.el
blobbbd3c95265d681fd00528510d2ba0d3cc81fd8f5
1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997-2012 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 3 of the License, or
11 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;;; Code:
25 (require 'gnus)
26 (require 'gnus-cache)
27 (require 'nnmail)
28 (require 'nnvirtual)
29 (require 'gnus-sum)
30 (require 'gnus-score)
31 (require 'gnus-srvr)
32 (require 'gnus-util)
33 (eval-when-compile
34 (if (featurep 'xemacs)
35 (require 'itimer)
36 (require 'timer))
37 (require 'cl))
39 (autoload 'gnus-server-update-server "gnus-srvr")
40 (autoload 'gnus-agent-customize-category "gnus-cus")
42 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
43 "Where the Gnus agent will store its files."
44 :group 'gnus-agent
45 :type 'directory)
47 (defcustom gnus-agent-plugged-hook nil
48 "Hook run when plugging into the network."
49 :group 'gnus-agent
50 :type 'hook)
52 (defcustom gnus-agent-unplugged-hook nil
53 "Hook run when unplugging from the network."
54 :group 'gnus-agent
55 :type 'hook)
57 (defcustom gnus-agent-fetched-hook nil
58 "Hook run when finished fetching articles."
59 :version "22.1"
60 :group 'gnus-agent
61 :type 'hook)
63 (defcustom gnus-agent-handle-level gnus-level-subscribed
64 "Groups on levels higher than this variable will be ignored by the Agent."
65 :group 'gnus-agent
66 :type 'integer)
68 (defcustom gnus-agent-expire-days 7
69 "Read articles older than this will be expired.
70 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
71 :group 'gnus-agent
72 :type '(number :tag "days"))
74 (defcustom gnus-agent-expire-all nil
75 "If non-nil, also expire unread, ticked and dormant articles.
76 If nil, only read articles will be expired."
77 :group 'gnus-agent
78 :type 'boolean)
80 (defcustom gnus-agent-group-mode-hook nil
81 "Hook run in Agent group minor modes."
82 :group 'gnus-agent
83 :type 'hook)
85 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
86 (when (featurep 'xemacs)
87 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
89 (defcustom gnus-agent-summary-mode-hook nil
90 "Hook run in Agent summary minor modes."
91 :group 'gnus-agent
92 :type 'hook)
94 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
95 (when (featurep 'xemacs)
96 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
98 (defcustom gnus-agent-server-mode-hook nil
99 "Hook run in Agent summary minor modes."
100 :group 'gnus-agent
101 :type 'hook)
103 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
104 (when (featurep 'xemacs)
105 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
107 (defcustom gnus-agent-confirmation-function 'y-or-n-p
108 "Function to confirm when error happens."
109 :version "21.1"
110 :group 'gnus-agent
111 :type 'function)
113 (defcustom gnus-agent-synchronize-flags nil
114 "Indicate if flags are synchronized when you plug in.
115 If this is `ask' the hook will query the user."
116 ;; If the default switches to something else than nil, then the function
117 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
118 :version "21.1"
119 :type '(choice (const :tag "Always" t)
120 (const :tag "Never" nil)
121 (const :tag "Ask" ask))
122 :group 'gnus-agent)
124 (defcustom gnus-agent-go-online 'ask
125 "Indicate if offline servers go online when you plug in.
126 If this is `ask' the hook will query the user."
127 :version "21.3"
128 :type '(choice (const :tag "Always" t)
129 (const :tag "Never" nil)
130 (const :tag "Ask" ask))
131 :group 'gnus-agent)
133 (defcustom gnus-agent-mark-unread-after-downloaded t
134 "Indicate whether to mark articles unread after downloaded."
135 :version "21.1"
136 :type 'boolean
137 :group 'gnus-agent)
139 (defcustom gnus-agent-download-marks '(download)
140 "Marks for downloading."
141 :version "21.1"
142 :type '(repeat (symbol :tag "Mark"))
143 :group 'gnus-agent)
145 (defcustom gnus-agent-consider-all-articles nil
146 "When non-nil, the agent will let the agent predicate decide
147 whether articles need to be downloaded or not, for all articles. When
148 nil, the default, the agent will only let the predicate decide
149 whether unread articles are downloaded or not. If you enable this,
150 groups with large active ranges may open slower and you may also want
151 to look into the agent expiry settings to block the expiration of
152 read articles as they would just be downloaded again."
153 :version "22.1"
154 :type 'boolean
155 :group 'gnus-agent)
157 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
158 "Chunk size for `gnus-agent-fetch-session'.
159 The function will split its article fetches into chunks smaller than
160 this limit."
161 :version "22.1"
162 :group 'gnus-agent
163 :type 'integer)
165 (defcustom gnus-agent-enable-expiration 'ENABLE
166 "The default expiration state for each group.
167 When set to ENABLE, the default, `gnus-agent-expire' will expire old
168 contents from a group's local storage. This value may be overridden
169 to disable expiration in specific categories, topics, and groups. Of
170 course, you could change gnus-agent-enable-expiration to DISABLE then
171 enable expiration per categories, topics, and groups."
172 :version "22.1"
173 :group 'gnus-agent
174 :type '(radio (const :format "Enable " ENABLE)
175 (const :format "Disable " DISABLE)))
177 (defcustom gnus-agent-expire-unagentized-dirs t
178 "*Whether expiration should expire in unagentized directories.
179 Have gnus-agent-expire scan the directories under
180 \(gnus-agent-directory) for groups that are no longer agentized.
181 When found, offer to remove them."
182 :version "22.1"
183 :type 'boolean
184 :group 'gnus-agent)
186 (defcustom gnus-agent-auto-agentize-methods nil
187 "Initially, all servers from these methods are agentized.
188 The user may remove or add servers using the Server buffer.
189 See Info nodes `(gnus)Server Buffer', `(gnus)Agent Variables'."
190 :version "22.1"
191 :type '(repeat symbol)
192 :group 'gnus-agent)
194 (defcustom gnus-agent-queue-mail t
195 "Whether and when outgoing mail should be queued by the agent.
196 When `always', always queue outgoing mail. When nil, never
197 queue. Otherwise, queue if and only if unplugged."
198 :version "22.1"
199 :group 'gnus-agent
200 :type '(radio (const :format "Always" always)
201 (const :format "Never" nil)
202 (const :format "When unplugged" t)))
204 (defcustom gnus-agent-prompt-send-queue nil
205 "If non-nil, `gnus-group-send-queue' will prompt if called when unplugged."
206 :version "22.1"
207 :group 'gnus-agent
208 :type 'boolean)
210 (defcustom gnus-agent-article-alist-save-format 1
211 "Indicates whether to use compression(2), versus no
212 compression(1), when writing agentview files. The compressed
213 files do save space but load times are 6-7 times higher. A group
214 must be opened then closed for the agentview to be updated using
215 the new format."
216 ;; Wouldn't symbols instead numbers be nicer? --rsteib
217 :version "22.1"
218 :group 'gnus-agent
219 :type '(radio (const :format "Compressed" 2)
220 (const :format "Uncompressed" 1)))
222 ;;; Internal variables
224 (defvar gnus-agent-history-buffers nil)
225 (defvar gnus-agent-buffer-alist nil)
226 (defvar gnus-agent-article-alist nil
227 "An assoc list identifying the articles whose headers have been fetched.
228 If successfully fetched, these headers will be stored in the group's overview
229 file. The key of each assoc pair is the article ID, the value of each assoc
230 pair is a flag indicating whether the identified article has been downloaded
231 \(gnus-agent-fetch-articles sets the value to the day of the download).
232 NOTES:
233 1) The last element of this list can not be expired as some
234 routines (for example, get-agent-fetch-headers) use the last
235 value to track which articles have had their headers retrieved.
236 2) The function `gnus-agent-regenerate' may destructively modify the value.")
237 (defvar gnus-agent-group-alist nil)
238 (defvar gnus-category-alist nil)
239 (defvar gnus-agent-current-history nil)
240 (defvar gnus-agent-overview-buffer nil)
241 (defvar gnus-category-predicate-cache nil)
242 (defvar gnus-category-group-cache nil)
243 (defvar gnus-agent-spam-hashtb nil)
244 (defvar gnus-agent-file-name nil)
245 (defvar gnus-agent-send-mail-function nil)
246 (defvar gnus-agent-file-coding-system 'raw-text)
247 (defvar gnus-agent-file-loading-cache nil)
248 (defvar gnus-agent-total-fetched-hashtb nil)
249 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
250 (defvar gnus-agent-need-update-total-fetched-for nil)
252 ;; Dynamic variables
253 (defvar gnus-headers)
254 (defvar gnus-score)
256 ;; Added to support XEmacs
257 (eval-and-compile
258 (unless (fboundp 'directory-files-and-attributes)
259 (defun directory-files-and-attributes (directory
260 &optional full match nosort)
261 (let (result)
262 (dolist (file (directory-files directory full match nosort))
263 (push (cons file (file-attributes file)) result))
264 (nreverse result)))))
267 ;;; Setup
270 (defun gnus-open-agent ()
271 (setq gnus-agent t)
272 (gnus-agent-read-servers)
273 (gnus-category-read)
274 (gnus-agent-create-buffer)
275 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
276 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
277 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
279 (defun gnus-agent-create-buffer ()
280 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
282 (setq gnus-agent-overview-buffer
283 (gnus-get-buffer-create " *Gnus agent overview*"))
284 (with-current-buffer gnus-agent-overview-buffer
285 (mm-enable-multibyte))
286 nil))
288 (gnus-add-shutdown 'gnus-close-agent 'gnus)
290 (defun gnus-close-agent ()
291 (setq gnus-category-predicate-cache nil
292 gnus-category-group-cache nil
293 gnus-agent-spam-hashtb nil)
294 (gnus-kill-buffer gnus-agent-overview-buffer))
297 ;;; Utility functions
300 (defmacro gnus-agent-with-refreshed-group (group &rest body)
301 "Performs the body then updates the group's line in the group
302 buffer. Automatically blocks multiple updates due to recursion."
303 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
304 (when (and gnus-agent-need-update-total-fetched-for
305 (not gnus-agent-inhibit-update-total-fetched-for))
306 (with-current-buffer gnus-group-buffer
307 (setq gnus-agent-need-update-total-fetched-for nil)
308 (gnus-group-update-group ,group t)))))
310 (defun gnus-agent-read-file (file)
311 "Load FILE and do a `read' there."
312 (with-temp-buffer
313 (ignore-errors
314 (nnheader-insert-file-contents file)
315 (goto-char (point-min))
316 (read (current-buffer)))))
318 (defsubst gnus-agent-method ()
319 (concat (symbol-name (car gnus-command-method)) "/"
320 (if (equal (cadr gnus-command-method) "")
321 "unnamed"
322 (cadr gnus-command-method))))
324 (defsubst gnus-agent-directory ()
325 "The name of the Gnus agent directory."
326 (nnheader-concat gnus-agent-directory
327 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
329 (defun gnus-agent-lib-file (file)
330 "The full name of the Gnus agent library FILE."
331 (expand-file-name file
332 (file-name-as-directory
333 (expand-file-name "agent.lib" (gnus-agent-directory)))))
335 (defun gnus-agent-cat-set-property (category property value)
336 (if value
337 (setcdr (or (assq property category)
338 (let ((cell (cons property nil)))
339 (setcdr category (cons cell (cdr category)))
340 cell)) value)
341 (let ((category category))
342 (while (cond ((eq property (caadr category))
343 (setcdr category (cddr category))
344 nil)
346 (setq category (cdr category)))))))
347 category)
349 (eval-when-compile
350 (defmacro gnus-agent-cat-defaccessor (name prop-name)
351 "Define accessor and setter methods for manipulating a list of the form
352 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
353 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
354 manipulated as follows:
355 (func LIST): Returns VALUE1
356 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
357 `(progn (defmacro ,name (category)
358 (list (quote cdr) (list (quote assq)
359 (quote (quote ,prop-name)) category)))
361 (define-setf-method ,name (category)
362 (let* ((--category--temp-- (make-symbol "--category--"))
363 (--value--temp-- (make-symbol "--value--")))
364 (list (list --category--temp--) ; temporary-variables
365 (list category) ; value-forms
366 (list --value--temp--) ; store-variables
367 (let* ((category --category--temp--) ; store-form
368 (value --value--temp--))
369 (list (quote gnus-agent-cat-set-property)
370 category
371 (quote (quote ,prop-name))
372 value))
373 (list (quote ,name) --category--temp--) ; access-form
374 )))))
377 (defmacro gnus-agent-cat-name (category)
378 `(car ,category))
380 (gnus-agent-cat-defaccessor
381 gnus-agent-cat-days-until-old agent-days-until-old)
382 (gnus-agent-cat-defaccessor
383 gnus-agent-cat-enable-expiration agent-enable-expiration)
384 (gnus-agent-cat-defaccessor
385 gnus-agent-cat-groups agent-groups)
386 (gnus-agent-cat-defaccessor
387 gnus-agent-cat-high-score agent-high-score)
388 (gnus-agent-cat-defaccessor
389 gnus-agent-cat-length-when-long agent-long-article)
390 (gnus-agent-cat-defaccessor
391 gnus-agent-cat-length-when-short agent-short-article)
392 (gnus-agent-cat-defaccessor
393 gnus-agent-cat-low-score agent-low-score)
394 (gnus-agent-cat-defaccessor
395 gnus-agent-cat-predicate agent-predicate)
396 (gnus-agent-cat-defaccessor
397 gnus-agent-cat-score-file agent-score)
398 (gnus-agent-cat-defaccessor
399 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
402 ;; This form is equivalent to defsetf except that it calls make-symbol
403 ;; whereas defsetf calls gensym (Using gensym creates a run-time
404 ;; dependency on the CL library).
406 (eval-and-compile
407 (define-setf-method gnus-agent-cat-groups (category)
408 (let* ((--category--temp-- (make-symbol "--category--"))
409 (--groups--temp-- (make-symbol "--groups--")))
410 (list (list --category--temp--)
411 (list category)
412 (list --groups--temp--)
413 (let* ((category --category--temp--)
414 (groups --groups--temp--))
415 (list (quote gnus-agent-set-cat-groups) category groups))
416 (list (quote gnus-agent-cat-groups) --category--temp--))))
419 (defun gnus-agent-set-cat-groups (category groups)
420 (unless (eq groups 'ignore)
421 (let ((new-g groups)
422 (old-g (gnus-agent-cat-groups category)))
423 (cond ((eq new-g old-g)
424 ;; gnus-agent-add-group is fiddling with the group
425 ;; list. Still, Im done.
428 ((eq new-g (cdr old-g))
429 ;; gnus-agent-add-group is fiddling with the group list
430 (setcdr (or (assq 'agent-groups category)
431 (let ((cell (cons 'agent-groups nil)))
432 (setcdr category (cons cell (cdr category)))
433 cell)) new-g))
435 (let ((groups groups))
436 (while groups
437 (let* ((group (pop groups))
438 (old-category (gnus-group-category group)))
439 (if (eq category old-category)
441 (setf (gnus-agent-cat-groups old-category)
442 (delete group (gnus-agent-cat-groups
443 old-category))))))
444 ;; Purge cache as preceding loop invalidated it.
445 (setq gnus-category-group-cache nil))
447 (setcdr (or (assq 'agent-groups category)
448 (let ((cell (cons 'agent-groups nil)))
449 (setcdr category (cons cell (cdr category)))
450 cell)) groups))))))
452 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
453 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
455 (defun gnus-agent-read-group ()
456 "Read a group name in the minibuffer, with completion."
457 (let ((def (or (gnus-group-group-name) gnus-newsgroup-name)))
458 (when def
459 (setq def (gnus-group-decoded-name def)))
460 (gnus-group-completing-read nil nil t nil nil def)))
462 ;;; Fetching setup functions.
464 (defun gnus-agent-start-fetch ()
465 "Initialize data structures for efficient fetching."
466 (gnus-agent-create-buffer))
468 (defun gnus-agent-stop-fetch ()
469 "Save all data structures and clean up."
470 (setq gnus-agent-spam-hashtb nil)
471 (with-current-buffer nntp-server-buffer
472 (widen)))
474 (defmacro gnus-agent-with-fetch (&rest forms)
475 "Do FORMS safely."
476 `(unwind-protect
477 (let ((gnus-agent-fetching t))
478 (gnus-agent-start-fetch)
479 ,@forms)
480 (gnus-agent-stop-fetch)))
482 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
483 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
485 (defmacro gnus-agent-append-to-list (tail value)
486 `(setq ,tail (setcdr ,tail (cons ,value nil))))
488 (defmacro gnus-agent-message (level &rest args)
489 `(if (<= ,level gnus-verbose)
490 (message ,@args)))
493 ;;; Mode infestation
496 (defvar gnus-agent-mode-hook nil
497 "Hook run when installing agent mode.")
499 (defvar gnus-agent-mode nil)
500 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
502 (defun gnus-agent-mode ()
503 "Minor mode for providing a agent support in Gnus buffers."
504 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
505 (symbol-name major-mode))
506 (match-string 1 (symbol-name major-mode))))
507 (mode (intern (format "gnus-agent-%s-mode" buffer))))
508 (set (make-local-variable 'gnus-agent-mode) t)
509 (set mode nil)
510 (set (make-local-variable mode) t)
511 ;; Set up the menu.
512 (when (gnus-visual-p 'agent-menu 'menu)
513 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
514 (unless (assq mode minor-mode-alist)
515 (push (cons mode (cdr gnus-agent-mode-status)) minor-mode-alist))
516 (unless (assq mode minor-mode-map-alist)
517 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
518 buffer))))
519 minor-mode-map-alist))
520 (when (eq major-mode 'gnus-group-mode)
521 (let ((init-plugged gnus-plugged)
522 (gnus-agent-go-online nil))
523 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
524 ;; Therefore, make certain that the current value does not
525 ;; match the desired initial value.
526 (setq gnus-plugged :unknown)
527 (gnus-agent-toggle-plugged init-plugged)))
528 (gnus-run-hooks 'gnus-agent-mode-hook
529 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
531 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
532 (gnus-define-keys gnus-agent-group-mode-map
533 "Ju" gnus-agent-fetch-groups
534 "Jc" gnus-enter-category-buffer
535 "Jj" gnus-agent-toggle-plugged
536 "Js" gnus-agent-fetch-session
537 "JY" gnus-agent-synchronize-flags
538 "JS" gnus-group-send-queue
539 "Ja" gnus-agent-add-group
540 "Jr" gnus-agent-remove-group
541 "Jo" gnus-agent-toggle-group-plugged)
543 (defun gnus-agent-group-make-menu-bar ()
544 (unless (boundp 'gnus-agent-group-menu)
545 (easy-menu-define
546 gnus-agent-group-menu gnus-agent-group-mode-map ""
547 '("Agent"
548 ["Toggle plugged" gnus-agent-toggle-plugged t]
549 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
550 ["List categories" gnus-enter-category-buffer t]
551 ["Add (current) group to category" gnus-agent-add-group t]
552 ["Remove (current) group from category" gnus-agent-remove-group t]
553 ["Send queue" gnus-group-send-queue gnus-plugged]
554 ("Fetch"
555 ["All" gnus-agent-fetch-session gnus-plugged]
556 ["Group" gnus-agent-fetch-group gnus-plugged])
557 ["Synchronize flags" gnus-agent-synchronize-flags t]
558 ))))
560 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
561 (gnus-define-keys gnus-agent-summary-mode-map
562 "Jj" gnus-agent-toggle-plugged
563 "Ju" gnus-agent-summary-fetch-group
564 "JS" gnus-agent-fetch-group
565 "Js" gnus-agent-summary-fetch-series
566 "J#" gnus-agent-mark-article
567 "J\M-#" gnus-agent-unmark-article
568 "@" gnus-agent-toggle-mark
569 "Jc" gnus-agent-catchup)
571 (defun gnus-agent-summary-make-menu-bar ()
572 (unless (boundp 'gnus-agent-summary-menu)
573 (easy-menu-define
574 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
575 '("Agent"
576 ["Toggle plugged" gnus-agent-toggle-plugged t]
577 ["Mark as downloadable" gnus-agent-mark-article t]
578 ["Unmark as downloadable" gnus-agent-unmark-article t]
579 ["Toggle mark" gnus-agent-toggle-mark t]
580 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
581 ["Catchup undownloaded" gnus-agent-catchup t]))))
583 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
584 (gnus-define-keys gnus-agent-server-mode-map
585 "Jj" gnus-agent-toggle-plugged
586 "Ja" gnus-agent-add-server
587 "Jr" gnus-agent-remove-server)
589 (defun gnus-agent-server-make-menu-bar ()
590 (unless (boundp 'gnus-agent-server-menu)
591 (easy-menu-define
592 gnus-agent-server-menu gnus-agent-server-mode-map ""
593 '("Agent"
594 ["Toggle plugged" gnus-agent-toggle-plugged t]
595 ["Add" gnus-agent-add-server t]
596 ["Remove" gnus-agent-remove-server t]))))
598 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
599 (if (and (fboundp 'propertize)
600 (fboundp 'make-mode-line-mouse-map))
601 (propertize string 'local-map
602 (make-mode-line-mouse-map mouse-button mouse-func)
603 'mouse-face
604 (if (and (featurep 'xemacs)
605 ;; XEmacs's `facep' only checks for a face
606 ;; object, not for a face name, so it's useless
607 ;; to check with `facep'.
608 (find-face 'modeline))
609 'modeline
610 'mode-line-highlight))
611 string))
613 (defun gnus-agent-toggle-plugged (set-to)
614 "Toggle whether Gnus is unplugged or not."
615 (interactive (list (not gnus-plugged)))
616 (cond ((eq set-to gnus-plugged)
617 nil)
618 (set-to
619 (setq gnus-plugged set-to)
620 (gnus-run-hooks 'gnus-agent-plugged-hook)
621 (setcar (cdr gnus-agent-mode-status)
622 (gnus-agent-make-mode-line-string " Plugged"
623 'mouse-2
624 'gnus-agent-toggle-plugged))
625 (gnus-agent-go-online gnus-agent-go-online))
627 (gnus-agent-close-connections)
628 (setq gnus-plugged set-to)
629 (gnus-run-hooks 'gnus-agent-unplugged-hook)
630 (setcar (cdr gnus-agent-mode-status)
631 (gnus-agent-make-mode-line-string " Unplugged"
632 'mouse-2
633 'gnus-agent-toggle-plugged))))
634 (set-buffer-modified-p t))
636 (defmacro gnus-agent-while-plugged (&rest body)
637 `(let ((original-gnus-plugged gnus-plugged))
638 (unwind-protect
639 (progn (gnus-agent-toggle-plugged t)
640 ,@body)
641 (gnus-agent-toggle-plugged original-gnus-plugged))))
643 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
644 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
646 (defun gnus-agent-close-connections ()
647 "Close all methods covered by the Gnus agent."
648 (let ((methods (gnus-agent-covered-methods)))
649 (while methods
650 (gnus-close-server (pop methods)))))
652 ;;;###autoload
653 (defun gnus-unplugged ()
654 "Start Gnus unplugged."
655 (interactive)
656 (setq gnus-plugged nil)
657 (gnus))
659 ;;;###autoload
660 (defun gnus-plugged ()
661 "Start Gnus plugged."
662 (interactive)
663 (setq gnus-plugged t)
664 (gnus))
666 ;;;###autoload
667 (defun gnus-slave-unplugged (&optional arg)
668 "Read news as a slave unplugged."
669 (interactive "P")
670 (setq gnus-plugged nil)
671 (gnus arg nil 'slave))
673 ;;;###autoload
674 (defun gnus-agentize ()
675 "Allow Gnus to be an offline newsreader.
677 The gnus-agentize function is now called internally by gnus when
678 gnus-agent is set. If you wish to avoid calling gnus-agentize,
679 customize gnus-agent to nil.
681 This will modify the `gnus-setup-news-hook', and
682 `message-send-mail-real-function' variables, and install the Gnus agent
683 minor mode in all Gnus buffers."
684 (interactive)
685 (gnus-open-agent)
686 (unless gnus-agent-send-mail-function
687 (setq gnus-agent-send-mail-function
688 (or message-send-mail-real-function
689 (function (lambda () (funcall message-send-mail-function))))
690 message-send-mail-real-function 'gnus-agent-send-mail))
692 ;; If the servers file doesn't exist, auto-agentize some servers and
693 ;; save the servers file so this auto-agentizing isn't invoked
694 ;; again.
695 (when (and (not (file-exists-p (nnheader-concat
696 gnus-agent-directory "lib/servers")))
697 gnus-agent-auto-agentize-methods)
698 (gnus-message 3 "First time agent user, agentizing remote groups...")
699 (mapc
700 (lambda (server-or-method)
701 (let ((method (gnus-server-to-method server-or-method)))
702 (when (memq (car method)
703 gnus-agent-auto-agentize-methods)
704 (push (gnus-method-to-server method)
705 gnus-agent-covered-methods)
706 (setq gnus-agent-method-p-cache nil))))
707 (cons gnus-select-method gnus-secondary-select-methods))
708 (gnus-agent-write-servers)))
710 (defun gnus-agent-queue-setup (&optional group-name)
711 "Make sure the queue group exists.
712 Optional arg GROUP-NAME allows to specify another group."
713 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
714 gnus-newsrc-hashtb)
715 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
716 (let ((gnus-level-default-subscribed 1))
717 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
718 nil '(nndraft "")))
719 (gnus-group-set-parameter
720 (format "nndraft:%s" (or group-name "queue"))
721 'gnus-dummy '((gnus-draft-mode)))))
723 (defun gnus-agent-send-mail ()
724 (if (or (not gnus-agent-queue-mail)
725 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
726 (funcall gnus-agent-send-mail-function)
727 (goto-char (point-min))
728 (re-search-forward
729 (concat "^" (regexp-quote mail-header-separator) "\n"))
730 (replace-match "\n")
731 (gnus-agent-insert-meta-information 'mail)
732 (gnus-request-accept-article "nndraft:queue" nil t t)
733 (gnus-group-refresh-group "nndraft:queue")))
735 (defun gnus-agent-insert-meta-information (type &optional method)
736 "Insert meta-information into the message that says how it's to be posted.
737 TYPE can be either `mail' or `news'. If the latter, then METHOD can
738 be a select method."
739 (save-excursion
740 (message-remove-header gnus-agent-meta-information-header)
741 (goto-char (point-min))
742 (insert gnus-agent-meta-information-header ": "
743 (symbol-name type) " " (format "%S" method)
744 "\n")
745 (forward-char -1)
746 (while (search-backward "\n" nil t)
747 (replace-match "\\n" t t))))
749 (defun gnus-agent-restore-gcc ()
750 "Restore GCC field from saved header."
751 (save-excursion
752 (goto-char (point-min))
753 (while (re-search-forward
754 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
755 (replace-match "Gcc:" 'fixedcase))))
757 (defun gnus-agent-any-covered-gcc ()
758 (save-restriction
759 (message-narrow-to-headers)
760 (let* ((gcc (mail-fetch-field "gcc" nil t))
761 (methods (and gcc
762 (mapcar 'gnus-inews-group-method
763 (message-unquote-tokens
764 (message-tokenize-header
765 gcc " ,")))))
766 covered)
767 (while (and (not covered) methods)
768 (setq covered (gnus-agent-method-p (car methods))
769 methods (cdr methods)))
770 covered)))
772 ;;;###autoload
773 (defun gnus-agent-possibly-save-gcc ()
774 "Save GCC if Gnus is unplugged."
775 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
776 (save-excursion
777 (goto-char (point-min))
778 (let ((case-fold-search t))
779 (while (re-search-forward "^gcc:" nil t)
780 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
782 (defun gnus-agent-possibly-do-gcc ()
783 "Do GCC if Gnus is plugged."
784 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
785 (gnus-inews-do-gcc)))
788 ;;; Group mode commands
791 (defun gnus-agent-fetch-groups (n)
792 "Put all new articles in the current groups into the Agent."
793 (interactive "P")
794 (unless gnus-plugged
795 (error "Groups can't be fetched when Gnus is unplugged"))
796 (gnus-group-iterate n 'gnus-agent-fetch-group))
798 (defun gnus-agent-fetch-group (&optional group)
799 "Put all new articles in GROUP into the Agent."
800 (interactive (list (gnus-group-group-name)))
801 (setq group (or group gnus-newsgroup-name))
802 (unless group
803 (error "No group on the current line"))
804 (if (not (gnus-agent-group-covered-p group))
805 (message "%s isn't covered by the agent" group)
806 (gnus-agent-while-plugged
807 (let ((gnus-command-method (gnus-find-method-for-group group)))
808 (gnus-agent-with-fetch
809 (gnus-agent-fetch-group-1 group gnus-command-method)
810 (gnus-message 5 "Fetching %s...done" group))))))
812 (defun gnus-agent-add-group (category arg)
813 "Add the current group to an agent category."
814 (interactive
815 (list
816 (intern
817 (gnus-completing-read
818 "Add to category"
819 (mapcar (lambda (cat) (symbol-name (car cat)))
820 gnus-category-alist)
822 current-prefix-arg))
823 (let ((cat (assq category gnus-category-alist))
824 c groups)
825 (gnus-group-iterate arg
826 (lambda (group)
827 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
828 (setf (gnus-agent-cat-groups c)
829 (delete group (gnus-agent-cat-groups c))))
830 (push group groups)))
831 (setf (gnus-agent-cat-groups cat)
832 (nconc (gnus-agent-cat-groups cat) groups))
833 (gnus-category-write)))
835 (defun gnus-agent-remove-group (arg)
836 "Remove the current group from its agent category, if any."
837 (interactive "P")
838 (let (c)
839 (gnus-group-iterate arg
840 (lambda (group)
841 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
842 (setf (gnus-agent-cat-groups c)
843 (delete group (gnus-agent-cat-groups c))))))
844 (gnus-category-write)))
846 (defun gnus-agent-synchronize-flags ()
847 "Synchronize unplugged flags with servers."
848 (interactive)
849 (save-excursion
850 (dolist (gnus-command-method (gnus-agent-covered-methods))
851 (when (file-exists-p (gnus-agent-lib-file "flags"))
852 (gnus-agent-synchronize-flags-server gnus-command-method)))))
854 (defun gnus-agent-possibly-synchronize-flags ()
855 "Synchronize flags according to `gnus-agent-synchronize-flags'."
856 (interactive)
857 (save-excursion
858 (dolist (gnus-command-method (gnus-agent-covered-methods))
859 (when (eq (gnus-server-status gnus-command-method) 'ok)
860 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
862 (defun gnus-agent-synchronize-flags-server (method)
863 "Synchronize flags set when unplugged for server."
864 (let ((gnus-command-method method)
865 (gnus-agent nil))
866 (when (file-exists-p (gnus-agent-lib-file "flags"))
867 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
868 (erase-buffer)
869 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
870 (cond ((null gnus-plugged)
871 (gnus-message
872 1 "You must be plugged to synchronize flags with server %s"
873 (nth 1 gnus-command-method)))
874 ((null (gnus-check-server gnus-command-method))
875 (gnus-message
876 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
878 (condition-case err
879 (while t
880 (let ((bgn (point)))
881 (eval (read (current-buffer)))
882 (delete-region bgn (point))))
883 (end-of-file
884 (delete-file (gnus-agent-lib-file "flags")))
885 (error
886 (let ((file (gnus-agent-lib-file "flags")))
887 (write-region (point-min) (point-max)
888 (gnus-agent-lib-file "flags") nil 'silent)
889 (error "Couldn't set flags from file %s due to %s"
890 file (error-message-string err)))))))
891 (kill-buffer nil))))
893 (defun gnus-agent-possibly-synchronize-flags-server (method)
894 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
895 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
896 (or (and gnus-agent-synchronize-flags
897 (not (eq gnus-agent-synchronize-flags 'ask)))
898 (and (eq gnus-agent-synchronize-flags 'ask)
899 (gnus-y-or-n-p
900 (format "Synchronize flags on server `%s'? "
901 (cadr method))))))
902 (gnus-agent-synchronize-flags-server method)))
904 ;;;###autoload
905 (defun gnus-agent-rename-group (old-group new-group)
906 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
907 Always updates the agent, even when disabled, as the old agent
908 files would corrupt gnus when the agent was next enabled.
909 Depends upon the caller to determine whether group renaming is
910 supported."
911 (let* ((old-command-method (gnus-find-method-for-group old-group))
912 (old-path (directory-file-name
913 (let (gnus-command-method old-command-method)
914 (gnus-agent-group-pathname old-group))))
915 (new-command-method (gnus-find-method-for-group new-group))
916 (new-path (directory-file-name
917 (let (gnus-command-method new-command-method)
918 (gnus-agent-group-pathname new-group))))
919 (file-name-coding-system nnmail-pathname-coding-system))
920 (gnus-rename-file old-path new-path t)
922 (let* ((old-real-group (gnus-group-real-name old-group))
923 (new-real-group (gnus-group-real-name new-group))
924 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
925 (gnus-agent-save-group-info old-command-method old-real-group nil)
926 (gnus-agent-save-group-info new-command-method new-real-group old-active)
928 (let ((old-local (gnus-agent-get-local old-group
929 old-real-group old-command-method)))
930 (gnus-agent-set-local old-group
931 nil nil
932 old-real-group old-command-method)
933 (gnus-agent-set-local new-group
934 (car old-local) (cdr old-local)
935 new-real-group new-command-method)))))
937 ;;;###autoload
938 (defun gnus-agent-delete-group (group)
939 "Delete fully-qualified GROUP.
940 Always updates the agent, even when disabled, as the old agent
941 files would corrupt gnus when the agent was next enabled.
942 Depends upon the caller to determine whether group deletion is
943 supported."
944 (let* ((command-method (gnus-find-method-for-group group))
945 (path (directory-file-name
946 (let (gnus-command-method command-method)
947 (gnus-agent-group-pathname group))))
948 (file-name-coding-system nnmail-pathname-coding-system))
949 (gnus-delete-directory path)
951 (let* ((real-group (gnus-group-real-name group)))
952 (gnus-agent-save-group-info command-method real-group nil)
954 (let ((local (gnus-agent-get-local group
955 real-group command-method)))
956 (gnus-agent-set-local group
957 nil nil
958 real-group command-method)))))
961 ;;; Server mode commands
964 (defun gnus-agent-add-server ()
965 "Enroll SERVER in the agent program."
966 (interactive)
967 (let* ((server (gnus-server-server-name))
968 (named-server (gnus-server-named-server))
969 (method (and server
970 (gnus-server-get-method nil server))))
971 (unless server
972 (error "No server on the current line"))
974 (when (gnus-agent-method-p method)
975 (error "Server already in the agent program"))
977 (push named-server gnus-agent-covered-methods)
979 (setq gnus-agent-method-p-cache nil)
980 (gnus-server-update-server server)
981 (gnus-agent-write-servers)
982 (gnus-message 1 "Entered %s into the Agent" server)))
984 (defun gnus-agent-remove-server ()
985 "Remove SERVER from the agent program."
986 (interactive)
987 (let* ((server (gnus-server-server-name))
988 (named-server (gnus-server-named-server)))
989 (unless server
990 (error "No server on the current line"))
992 (unless (member named-server gnus-agent-covered-methods)
993 (error "Server not in the agent program"))
995 (setq gnus-agent-covered-methods
996 (delete named-server gnus-agent-covered-methods)
997 gnus-agent-method-p-cache nil)
999 (gnus-server-update-server server)
1000 (gnus-agent-write-servers)
1001 (gnus-message 1 "Removed %s from the agent" server)))
1003 (defun gnus-agent-read-servers ()
1004 "Read the alist of covered servers."
1005 (setq gnus-agent-covered-methods
1006 (gnus-agent-read-file
1007 (nnheader-concat gnus-agent-directory "lib/servers"))
1008 gnus-agent-method-p-cache nil)
1010 ;; I am called so early in start-up that I can not validate server
1011 ;; names. When that is the case, I skip the validation. That is
1012 ;; alright as the gnus startup code calls the validate methods
1013 ;; directly.
1014 (if gnus-server-alist
1015 (gnus-agent-read-servers-validate)))
1017 (defun gnus-agent-read-servers-validate ()
1018 (mapcar (lambda (server-or-method)
1019 (let* ((server (if (stringp server-or-method)
1020 server-or-method
1021 (gnus-method-to-server server-or-method)))
1022 (method (gnus-server-to-method server)))
1023 (if method
1024 (unless (member server gnus-agent-covered-methods)
1025 (push server gnus-agent-covered-methods)
1026 (setq gnus-agent-method-p-cache nil))
1027 (gnus-message 8 "Ignoring disappeared server `%s'" server))))
1028 (prog1 gnus-agent-covered-methods
1029 (setq gnus-agent-covered-methods nil))))
1031 (defun gnus-agent-read-servers-validate-native (native-method)
1032 (setq gnus-agent-covered-methods
1033 (mapcar (lambda (method)
1034 (if (or (not method)
1035 (equal method native-method))
1036 "native"
1037 method)) gnus-agent-covered-methods)))
1039 (defun gnus-agent-write-servers ()
1040 "Write the alist of covered servers."
1041 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1042 (let ((coding-system-for-write nnheader-file-coding-system)
1043 (file-name-coding-system nnmail-pathname-coding-system))
1044 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1045 (prin1 gnus-agent-covered-methods
1046 (current-buffer)))))
1049 ;;; Summary commands
1052 (defun gnus-agent-mark-article (n &optional unmark)
1053 "Mark the next N articles as downloadable.
1054 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1055 the mark instead. The difference between N and the actual number of
1056 articles marked is returned."
1057 (interactive "p")
1058 (let ((backward (< n 0))
1059 (n (abs n)))
1060 (while (and
1061 (> n 0)
1062 (progn
1063 (gnus-summary-set-agent-mark
1064 (gnus-summary-article-number) unmark)
1065 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1066 (setq n (1- n)))
1067 (when (/= 0 n)
1068 (gnus-message 7 "No more articles"))
1069 (gnus-summary-recenter)
1070 (gnus-summary-position-point)
1073 (defun gnus-agent-unmark-article (n)
1074 "Remove the downloadable mark from the next N articles.
1075 If N is negative, unmark backward instead. The difference between N and
1076 the actual number of articles unmarked is returned."
1077 (interactive "p")
1078 (gnus-agent-mark-article n t))
1080 (defun gnus-agent-toggle-mark (n)
1081 "Toggle the downloadable mark from the next N articles.
1082 If N is negative, toggle backward instead. The difference between N and
1083 the actual number of articles toggled is returned."
1084 (interactive "p")
1085 (gnus-agent-mark-article n 'toggle))
1087 (defun gnus-summary-set-agent-mark (article &optional unmark)
1088 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1089 When UNMARK is t, the article is unmarked. For any other value, the
1090 article's mark is toggled."
1091 (let ((unmark (cond ((eq nil unmark)
1092 nil)
1093 ((eq t unmark)
1096 (memq article gnus-newsgroup-downloadable)))))
1097 (when (gnus-summary-goto-subject article nil t)
1098 (gnus-summary-update-mark
1099 (if unmark
1100 (progn
1101 (setq gnus-newsgroup-downloadable
1102 (delq article gnus-newsgroup-downloadable))
1103 (gnus-article-mark article))
1104 (setq gnus-newsgroup-downloadable
1105 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1106 gnus-downloadable-mark)
1107 'unread))))
1109 ;;;###autoload
1110 (defun gnus-agent-get-undownloaded-list ()
1111 "Construct list of articles that have not been downloaded."
1112 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1113 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1114 (gnus-agent-method-p gnus-command-method))
1115 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1116 (headers (sort (mapcar (lambda (h)
1117 (mail-header-number h))
1118 gnus-newsgroup-headers) '<))
1119 (cached (and gnus-use-cache gnus-newsgroup-cached))
1120 (undownloaded (list nil))
1121 (tail-undownloaded undownloaded)
1122 (unfetched (list nil))
1123 (tail-unfetched unfetched))
1124 (while (and alist headers)
1125 (let ((a (caar alist))
1126 (h (car headers)))
1127 (cond ((< a h)
1128 ;; Ignore IDs in the alist that are not being
1129 ;; displayed in the summary.
1130 (setq alist (cdr alist)))
1131 ((> a h)
1132 ;; Headers that are not in the alist should be
1133 ;; fictitious (see nnagent-retrieve-headers); they
1134 ;; imply that this article isn't in the agent.
1135 (gnus-agent-append-to-list tail-undownloaded h)
1136 (gnus-agent-append-to-list tail-unfetched h)
1137 (setq headers (cdr headers)))
1138 ((cdar alist)
1139 (setq alist (cdr alist))
1140 (setq headers (cdr headers))
1141 nil ; ignore already downloaded
1144 (setq alist (cdr alist))
1145 (setq headers (cdr headers))
1147 ;; This article isn't in the agent. Check to see
1148 ;; if it is in the cache. If it is, it's been
1149 ;; downloaded.
1150 (while (and cached (< (car cached) a))
1151 (setq cached (cdr cached)))
1152 (unless (equal a (car cached))
1153 (gnus-agent-append-to-list tail-undownloaded a))))))
1155 (while headers
1156 (let ((num (pop headers)))
1157 (gnus-agent-append-to-list tail-undownloaded num)
1158 (gnus-agent-append-to-list tail-unfetched num)))
1160 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1161 gnus-newsgroup-unfetched (cdr unfetched))))))
1163 (defun gnus-agent-catchup ()
1164 "Mark as read all unhandled articles.
1165 An article is unhandled if it is neither cached, nor downloaded, nor
1166 downloadable."
1167 (interactive)
1168 (save-excursion
1169 (let ((articles gnus-newsgroup-undownloaded))
1170 (when (or gnus-newsgroup-downloadable
1171 gnus-newsgroup-cached)
1172 (setq articles (gnus-sorted-ndifference
1173 (gnus-sorted-ndifference
1174 (gnus-copy-sequence articles)
1175 gnus-newsgroup-downloadable)
1176 gnus-newsgroup-cached)))
1178 (while articles
1179 (gnus-summary-mark-article
1180 (pop articles) gnus-catchup-mark)))
1181 (gnus-summary-position-point)))
1183 (defun gnus-agent-summary-fetch-series ()
1184 "Fetch the process-marked articles into the Agent."
1185 (interactive)
1186 (when gnus-newsgroup-processable
1187 (setq gnus-newsgroup-downloadable
1188 (let* ((dl gnus-newsgroup-downloadable)
1189 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1190 (gnus-newsgroup-downloadable processable))
1191 (gnus-agent-summary-fetch-group)
1193 ;; For each article that I processed that is no longer
1194 ;; undownloaded, remove its processable mark.
1196 (mapc #'gnus-summary-remove-process-mark
1197 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1199 ;; The preceding call to (gnus-agent-summary-fetch-group)
1200 ;; updated the temporary gnus-newsgroup-downloadable to
1201 ;; remove each article successfully fetched. Now, I
1202 ;; update the real gnus-newsgroup-downloadable to only
1203 ;; include undownloaded articles.
1204 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1206 (defun gnus-agent-summary-fetch-group (&optional all)
1207 "Fetch the downloadable articles in the group.
1208 Optional arg ALL, if non-nil, means to fetch all articles."
1209 (interactive "P")
1210 (let ((articles
1211 (if all gnus-newsgroup-articles
1212 gnus-newsgroup-downloadable))
1213 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1214 fetched-articles)
1215 (gnus-agent-while-plugged
1216 (unless articles
1217 (error "No articles to download"))
1218 (gnus-agent-with-fetch
1219 (setq gnus-newsgroup-undownloaded
1220 (gnus-sorted-ndifference
1221 gnus-newsgroup-undownloaded
1222 (setq fetched-articles
1223 (gnus-agent-fetch-articles
1224 gnus-newsgroup-name articles)))))
1225 (save-excursion
1226 (dolist (article articles)
1227 (let ((was-marked-downloadable
1228 (memq article gnus-newsgroup-downloadable)))
1229 (cond (gnus-agent-mark-unread-after-downloaded
1230 (setq gnus-newsgroup-downloadable
1231 (delq article gnus-newsgroup-downloadable))
1232 (when (and (not (member article gnus-newsgroup-dormant))
1233 (not (member article gnus-newsgroup-marked)))
1234 (gnus-summary-mark-article article gnus-unread-mark)))
1235 (was-marked-downloadable
1236 (gnus-summary-set-agent-mark article t)))
1237 (when (gnus-summary-goto-subject article nil t)
1238 (gnus-summary-update-download-mark article))))))
1239 fetched-articles))
1241 (defun gnus-agent-fetch-selected-article ()
1242 "Fetch the current article as it is selected.
1243 This can be added to `gnus-select-article-hook' or
1244 `gnus-mark-article-hook'."
1245 (let ((gnus-command-method gnus-current-select-method))
1246 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1247 (when (gnus-agent-fetch-articles
1248 gnus-newsgroup-name
1249 (list gnus-current-article))
1250 (setq gnus-newsgroup-undownloaded
1251 (delq gnus-current-article gnus-newsgroup-undownloaded))
1252 (gnus-summary-update-download-mark gnus-current-article)))))
1255 ;;; Internal functions
1258 (defun gnus-agent-synchronize-group-flags (group actions server)
1259 "Update a plugged group by performing the indicated actions."
1260 (let* ((gnus-command-method (gnus-server-to-method server))
1261 (info
1262 ;; This initializer is required as gnus-request-set-mark
1263 ;; calls gnus-group-real-name to strip off the host name
1264 ;; before calling the backend. Now that the backend is
1265 ;; trying to call gnus-request-set-mark, I have to
1266 ;; reconstruct the original group name.
1267 (or (gnus-get-info group)
1268 (gnus-get-info
1269 (setq group (gnus-group-full-name
1270 group gnus-command-method))))))
1271 (gnus-request-set-mark group actions)
1273 (when info
1274 (dolist (action actions)
1275 (let ((range (nth 0 action))
1276 (what (nth 1 action))
1277 (marks (nth 2 action)))
1278 (dolist (mark marks)
1279 (cond ((eq mark 'read)
1280 (gnus-info-set-read
1281 info
1282 (funcall (if (eq what 'add)
1283 'gnus-range-add
1284 'gnus-remove-from-range)
1285 (gnus-info-read info)
1286 range))
1287 (gnus-get-unread-articles-in-group
1288 info
1289 (gnus-active (gnus-info-group info))))
1290 ((memq mark '(tick))
1291 (let ((info-marks (assoc mark (gnus-info-marks info))))
1292 (unless info-marks
1293 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1294 (setcdr info-marks (funcall (if (eq what 'add)
1295 'gnus-range-add
1296 'gnus-remove-from-range)
1297 (cdr info-marks)
1298 range))))))))
1300 ;;Marks can be synchronized at any time by simply toggling from
1301 ;;unplugged to plugged. If that is what is happening right now, make
1302 ;;sure that the group buffer is up to date.
1303 (when (gnus-buffer-live-p gnus-group-buffer)
1304 (gnus-group-update-group group t)))
1305 nil))
1307 (defun gnus-agent-save-active (method)
1308 (when (gnus-agent-method-p method)
1309 (let* ((gnus-command-method method)
1310 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1311 (file (gnus-agent-lib-file "active")))
1312 (gnus-active-to-gnus-format nil new)
1313 (gnus-agent-write-active file new)
1314 (erase-buffer)
1315 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
1316 (nnheader-insert-file-contents file)))))
1318 (defun gnus-agent-write-active (file new)
1319 (gnus-make-directory (file-name-directory file))
1320 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1321 ;; The hashtable contains real names of groups. However, do NOT
1322 ;; add the foreign server prefix as gnus-active-to-gnus-format
1323 ;; will add it while reading the file.
1324 (gnus-write-active-file file new nil)))
1326 ;;;###autoload
1327 (defun gnus-agent-possibly-alter-active (group active &optional info)
1328 "Possibly expand a group's active range to include articles
1329 downloaded into the agent."
1330 (let* ((gnus-command-method (or gnus-command-method
1331 (gnus-find-method-for-group group))))
1332 (when (gnus-agent-method-p gnus-command-method)
1333 (let* ((local (gnus-agent-get-local group))
1334 (active-min (or (car active) 0))
1335 (active-max (or (cdr active) 0))
1336 (agent-min (or (car local) active-min))
1337 (agent-max (or (cdr local) active-max)))
1339 (when (< agent-min active-min)
1340 (setcar active agent-min))
1342 (when (> agent-max active-max)
1343 (setcdr active agent-max))
1345 (when (and info (< agent-max (- active-min 100)))
1346 ;; I'm expanding the active range by such a large amount
1347 ;; that there is a gap of more than 100 articles between the
1348 ;; last article known to the agent and the first article
1349 ;; currently available on the server. This gap contains
1350 ;; articles that have been lost, mark them as read so that
1351 ;; gnus doesn't waste resources trying to fetch them.
1353 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1354 ;; want to modify the local file everytime someone restarts
1355 ;; gnus. The small gap will cause a tiny performance hit
1356 ;; when gnus tries, and fails, to retrieve the articles.
1357 ;; Still that should be smaller than opening a buffer,
1358 ;; printing this list to the buffer, and then writing it to a
1359 ;; file.
1361 (let ((read (gnus-info-read info)))
1362 (gnus-info-set-read
1363 info
1364 (gnus-range-add
1365 read
1366 (list (cons (1+ agent-max)
1367 (1- active-min))))))
1369 ;; Lie about the agent's local range for this group to
1370 ;; disable the set read each time this server is opened.
1371 ;; NOTE: Opening this group will restore the valid local
1372 ;; range but it will also expand the local range to
1373 ;; encompass the new active range.
1374 (gnus-agent-set-local group agent-min (1- active-min)))))))
1376 (defun gnus-agent-save-group-info (method group active)
1377 "Update a single group's active range in the agent's copy of the server's active file."
1378 (when (gnus-agent-method-p method)
1379 (let* ((gnus-command-method (or method gnus-command-method))
1380 (coding-system-for-write nnheader-file-coding-system)
1381 (file-name-coding-system nnmail-pathname-coding-system)
1382 (file (gnus-agent-lib-file "active"))
1383 oactive-min oactive-max)
1384 (gnus-make-directory (file-name-directory file))
1385 (with-temp-file file
1386 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1387 (mm-disable-multibyte)
1388 (when (file-exists-p file)
1389 (nnheader-insert-file-contents file)
1391 (goto-char (point-min))
1392 (when (re-search-forward
1393 (concat "^" (regexp-quote group) " ") nil t)
1394 (save-excursion
1395 (setq oactive-max (read (current-buffer)) ;; max
1396 oactive-min (read (current-buffer)))) ;; min
1397 (gnus-delete-line)))
1398 (when active
1399 (insert (format "%S %d %d y\n" (intern group)
1400 (max (or oactive-max (cdr active)) (cdr active))
1401 (min (or oactive-min (car active)) (car active))))
1402 (goto-char (point-max))
1403 (while (search-backward "\\." nil t)
1404 (delete-char 1)))))))
1406 (defun gnus-agent-get-group-info (method group)
1407 "Get a single group's active range in the agent's copy of the server's active file."
1408 (when (gnus-agent-method-p method)
1409 (let* ((gnus-command-method (or method gnus-command-method))
1410 (coding-system-for-write nnheader-file-coding-system)
1411 (file-name-coding-system nnmail-pathname-coding-system)
1412 (file (gnus-agent-lib-file "active"))
1413 oactive-min oactive-max)
1414 (gnus-make-directory (file-name-directory file))
1415 (with-temp-buffer
1416 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1417 (mm-disable-multibyte)
1418 (when (file-exists-p file)
1419 (nnheader-insert-file-contents file)
1421 (goto-char (point-min))
1422 (when (re-search-forward
1423 (concat "^" (regexp-quote group) " ") nil t)
1424 (save-excursion
1425 (setq oactive-max (read (current-buffer)) ;; max
1426 oactive-min (read (current-buffer))) ;; min
1427 (cons oactive-min oactive-max))))))))
1429 (defvar gnus-agent-decoded-group-names nil
1430 "Alist of non-ASCII group names and decoded ones.")
1432 (defun gnus-agent-decoded-group-name (group)
1433 "Return a decoded group name of GROUP."
1434 (or (cdr (assoc group gnus-agent-decoded-group-names))
1435 (if (string-match "[^\000-\177]" group)
1436 (let ((decoded (gnus-group-decoded-name group)))
1437 (push (cons group decoded) gnus-agent-decoded-group-names)
1438 decoded)
1439 group)))
1441 (defun gnus-agent-group-path (group)
1442 "Translate GROUP into a file name."
1444 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1445 ;; The two methods must be kept synchronized, which is why
1446 ;; gnus-agent-group-pathname was added.
1448 (setq group
1449 (nnheader-translate-file-chars
1450 (nnheader-replace-duplicate-chars-in-string
1451 (nnheader-replace-chars-in-string
1452 (gnus-group-real-name (gnus-agent-decoded-group-name group))
1453 ?/ ?_)
1454 ?. ?_)))
1455 (if (or nnmail-use-long-file-names
1456 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1457 group
1458 (nnheader-replace-chars-in-string group ?. ?/)))
1460 (defun gnus-agent-group-pathname (group)
1461 "Translate GROUP into a file name."
1462 ;; nnagent uses nnmail-group-pathname to read articles while
1463 ;; unplugged. The agent must, therefore, use the same directory
1464 ;; while plugged.
1465 (nnmail-group-pathname
1466 (gnus-group-real-name (gnus-agent-decoded-group-name group))
1467 (if gnus-command-method
1468 (gnus-agent-directory)
1469 (let ((gnus-command-method (gnus-find-method-for-group group)))
1470 (gnus-agent-directory)))))
1472 (defun gnus-agent-get-function (method)
1473 (if (gnus-online method)
1474 (car method)
1475 (require 'nnagent)
1476 'nnagent))
1478 (defun gnus-agent-covered-methods ()
1479 "Return the subset of methods that are covered by the agent."
1480 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1482 ;;; History functions
1484 (defun gnus-agent-history-buffer ()
1485 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1487 (defun gnus-agent-open-history ()
1488 (save-excursion
1489 (push (cons (gnus-agent-method)
1490 (set-buffer (gnus-get-buffer-create
1491 (format " *Gnus agent %s history*"
1492 (gnus-agent-method)))))
1493 gnus-agent-history-buffers)
1494 (mm-disable-multibyte) ;; everything is binary
1495 (erase-buffer)
1496 (insert "\n")
1497 (let ((file (gnus-agent-lib-file "history")))
1498 (when (file-exists-p file)
1499 (nnheader-insert-file-contents file))
1500 (set (make-local-variable 'gnus-agent-file-name) file))))
1502 (defun gnus-agent-close-history ()
1503 (when (gnus-buffer-live-p gnus-agent-current-history)
1504 (kill-buffer gnus-agent-current-history)
1505 (setq gnus-agent-history-buffers
1506 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1507 gnus-agent-history-buffers))))
1510 ;;; Fetching
1513 (defun gnus-agent-fetch-articles (group articles)
1514 "Fetch ARTICLES from GROUP and put them into the Agent."
1515 (when articles
1516 (gnus-agent-load-alist group)
1517 (let* ((alist gnus-agent-article-alist)
1518 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1519 (selected-sets (list nil))
1520 (current-set-size 0)
1521 article
1522 header-number)
1523 ;; Check each article
1524 (while (setq article (pop articles))
1525 ;; Skip alist entries preceding this article
1526 (while (> article (or (caar alist) (1+ article)))
1527 (setq alist (cdr alist)))
1529 ;; Prune off articles that we have already fetched.
1530 (unless (and (eq article (caar alist))
1531 (cdar alist))
1532 ;; Skip headers preceding this article
1533 (while (> article
1534 (setq header-number
1535 (let* ((header (car headers)))
1536 (if header
1537 (mail-header-number header)
1538 (1+ article)))))
1539 (setq headers (cdr headers)))
1541 ;; Add this article to the current set
1542 (setcar selected-sets (cons article (car selected-sets)))
1544 ;; Update the set size, when the set is too large start a
1545 ;; new one. I do this after adding the article as I want at
1546 ;; least one article in each set.
1547 (when (< gnus-agent-max-fetch-size
1548 (setq current-set-size
1549 (+ current-set-size
1550 (if (= header-number article)
1551 (let ((char-size (mail-header-chars
1552 (car headers))))
1553 (if (<= char-size 0)
1554 ;; The char size was missing/invalid,
1555 ;; assume a worst-case situation of
1556 ;; 65 char/line. If the line count
1557 ;; is missing, arbitrarily assume a
1558 ;; size of 1000 characters.
1559 (max (* 65 (mail-header-lines
1560 (car headers)))
1561 1000)
1562 char-size))
1563 0))))
1564 (setcar selected-sets (nreverse (car selected-sets)))
1565 (setq selected-sets (cons nil selected-sets)
1566 current-set-size 0))))
1568 (when (or (cdr selected-sets) (car selected-sets))
1569 (let* ((fetched-articles (list nil))
1570 (tail-fetched-articles fetched-articles)
1571 (dir (gnus-agent-group-pathname group))
1572 (date (time-to-days (current-time)))
1573 (case-fold-search t)
1574 pos crosses id
1575 (file-name-coding-system nnmail-pathname-coding-system))
1577 (setcar selected-sets (nreverse (car selected-sets)))
1578 (setq selected-sets (nreverse selected-sets))
1580 (gnus-make-directory dir)
1581 (gnus-message 7 "Fetching articles for %s..."
1582 (gnus-agent-decoded-group-name group))
1584 (unwind-protect
1585 (while (setq articles (pop selected-sets))
1586 ;; Fetch the articles from the backend.
1587 (if (gnus-check-backend-function 'retrieve-articles group)
1588 (setq pos (gnus-retrieve-articles articles group))
1589 (with-temp-buffer
1590 (let (article)
1591 (while (setq article (pop articles))
1592 (gnus-message 10 "Fetching article %s for %s..."
1593 article
1594 (gnus-agent-decoded-group-name group))
1595 (when (or
1596 (gnus-backlog-request-article group article
1597 nntp-server-buffer)
1598 (gnus-request-article article group))
1599 (goto-char (point-max))
1600 (push (cons article (point)) pos)
1601 (insert-buffer-substring nntp-server-buffer)))
1602 (copy-to-buffer
1603 nntp-server-buffer (point-min) (point-max))
1604 (setq pos (nreverse pos)))))
1605 ;; Then save these articles into the Agent.
1606 (with-current-buffer nntp-server-buffer
1607 (while pos
1608 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1609 (goto-char (point-min))
1610 (unless (eobp) ;; Don't save empty articles.
1611 (when (search-forward "\n\n" nil t)
1612 (when (search-backward "\nXrefs: " nil t)
1613 ;; Handle cross posting.
1614 (goto-char (match-end 0)) ; move to end of header name
1615 (skip-chars-forward "^ ") ; skip server name
1616 (skip-chars-forward " ")
1617 (setq crosses nil)
1618 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1619 (push (cons (buffer-substring (match-beginning 1)
1620 (match-end 1))
1621 (string-to-number
1622 (buffer-substring (match-beginning 2)
1623 (match-end 2))))
1624 crosses)
1625 (goto-char (match-end 0)))
1626 (gnus-agent-crosspost crosses (caar pos) date)))
1627 (goto-char (point-min))
1628 (if (not (re-search-forward
1629 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1630 (setq id "No-Message-ID-in-article")
1631 (setq id (buffer-substring
1632 (match-beginning 1) (match-end 1))))
1633 (let ((coding-system-for-write
1634 gnus-agent-file-coding-system))
1635 (write-region (point-min) (point-max)
1636 (concat dir (number-to-string (caar pos)))
1637 nil 'silent))
1639 (gnus-agent-append-to-list
1640 tail-fetched-articles (caar pos)))
1641 (widen)
1642 (setq pos (cdr pos)))))
1644 (gnus-agent-save-alist group (cdr fetched-articles) date)
1645 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1647 (gnus-message 7 ""))
1648 (cdr fetched-articles))))))
1650 (defun gnus-agent-unfetch-articles (group articles)
1651 "Delete ARTICLES that were fetched from GROUP into the agent."
1652 (when articles
1653 (gnus-agent-with-refreshed-group
1654 group
1655 (gnus-agent-load-alist group)
1656 (let* ((alist (cons nil gnus-agent-article-alist))
1657 (articles (sort articles #'<))
1658 (next-possibility alist)
1659 (delete-this (pop articles)))
1660 (while (and (cdr next-possibility) delete-this)
1661 (let ((have-this (caar (cdr next-possibility))))
1662 (cond
1663 ((< delete-this have-this)
1664 (setq delete-this (pop articles)))
1665 ((= delete-this have-this)
1666 (let ((timestamp (cdar (cdr next-possibility))))
1667 (when timestamp
1668 (let* ((file-name (concat (gnus-agent-group-pathname group)
1669 (number-to-string have-this)))
1670 (size-file
1671 (float (or (and gnus-agent-total-fetched-hashtb
1672 (nth 7 (file-attributes file-name)))
1673 0)))
1674 (file-name-coding-system
1675 nnmail-pathname-coding-system))
1676 (delete-file file-name)
1677 (gnus-agent-update-files-total-fetched-for
1678 group (- size-file)))))
1680 (setcdr next-possibility (cddr next-possibility)))
1682 (setq next-possibility (cdr next-possibility))))))
1683 (setq gnus-agent-article-alist (cdr alist))
1684 (gnus-agent-save-alist group)))))
1686 (defun gnus-agent-crosspost (crosses article &optional date)
1687 (setq date (or date t))
1689 (let (gnus-agent-article-alist group alist beg end)
1690 (with-current-buffer gnus-agent-overview-buffer
1691 (when (nnheader-find-nov-line article)
1692 (forward-word 1)
1693 (setq beg (point))
1694 (setq end (progn (forward-line 1) (point)))))
1695 (while crosses
1696 (setq group (caar crosses))
1697 (unless (setq alist (assoc group gnus-agent-group-alist))
1698 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1699 gnus-agent-group-alist))
1700 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1701 (with-current-buffer (gnus-get-buffer-create
1702 (format " *Gnus agent overview %s*"group))
1703 (when (= (point-max) (point-min))
1704 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1705 (ignore-errors
1706 (let ((file-name-coding-system nnmail-pathname-coding-system))
1707 (nnheader-insert-file-contents
1708 (gnus-agent-article-name ".overview" group)))))
1709 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1710 (insert (string-to-number (cdar crosses)))
1711 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1712 (gnus-agent-check-overview-buffer))
1713 (setq crosses (cdr crosses)))))
1715 (defun gnus-agent-backup-overview-buffer ()
1716 (when gnus-newsgroup-name
1717 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1718 (cnt 0)
1719 name
1720 (file-name-coding-system nnmail-pathname-coding-system))
1721 (while (file-exists-p
1722 (setq name (concat root "~"
1723 (int-to-string (setq cnt (1+ cnt))) "~"))))
1724 (write-region (point-min) (point-max) name nil 'no-msg)
1725 (gnus-message 1 "Created backup copy of overview in %s." name)))
1728 (defun gnus-agent-check-overview-buffer (&optional buffer)
1729 "Check the overview file given for sanity.
1730 In particular, checks that the file is sorted by article number
1731 and that there are no duplicates."
1732 (let ((prev-num -1)
1733 (backed-up nil))
1734 (save-excursion
1735 (when buffer
1736 (set-buffer buffer))
1737 (save-restriction
1738 (widen)
1739 (goto-char (point-min))
1741 (while (< (point) (point-max))
1742 (let ((p (point))
1743 (cur (condition-case nil
1744 (read (current-buffer))
1745 (error nil))))
1746 (cond
1747 ((or (not (integerp cur))
1748 (not (eq (char-after) ?\t)))
1749 (or backed-up
1750 (setq backed-up (gnus-agent-backup-overview-buffer)))
1751 (gnus-message 1
1752 "Overview buffer contains garbage '%s'."
1753 (buffer-substring
1754 p (point-at-eol))))
1755 ((= cur prev-num)
1756 (or backed-up
1757 (setq backed-up (gnus-agent-backup-overview-buffer)))
1758 (gnus-message 1
1759 "Duplicate overview line for %d" cur)
1760 (delete-region p (progn (forward-line 1) (point))))
1761 ((< cur prev-num)
1762 (or backed-up
1763 (setq backed-up (gnus-agent-backup-overview-buffer)))
1764 (gnus-message 1 "Overview buffer not sorted!")
1765 (sort-numeric-fields 1 (point-min) (point-max))
1766 (goto-char (point-min))
1767 (setq prev-num -1))
1769 (setq prev-num cur)))
1770 (forward-line 1)))))))
1772 (defun gnus-agent-flush-server (&optional server-or-method)
1773 "Flush all agent index files for every subscribed group within
1774 the given SERVER-OR-METHOD. When called with nil, the current
1775 value of gnus-command-method identifies the server."
1776 (let* ((gnus-command-method (if server-or-method
1777 (gnus-server-to-method server-or-method)
1778 gnus-command-method))
1779 (alist gnus-newsrc-alist))
1780 (while alist
1781 (let ((entry (pop alist)))
1782 (when (gnus-methods-equal-p gnus-command-method (gnus-info-method entry))
1783 (gnus-agent-flush-group (gnus-info-group entry)))))))
1785 (defun gnus-agent-flush-group (group)
1786 "Flush the agent's index files such that the GROUP no longer
1787 appears to have any local content. The actual content, the
1788 article files, may then be deleted using gnus-agent-expire-group.
1789 If flushing was a mistake, the gnus-agent-regenerate-group method
1790 provides an undo mechanism by reconstructing the index files from
1791 the article files."
1792 (interactive (list (gnus-agent-read-group)))
1794 (let* ((gnus-command-method (or gnus-command-method
1795 (gnus-find-method-for-group group)))
1796 (overview (gnus-agent-article-name ".overview" group))
1797 (agentview (gnus-agent-article-name ".agentview" group))
1798 (file-name-coding-system nnmail-pathname-coding-system))
1800 (if (file-exists-p overview)
1801 (delete-file overview))
1802 (if (file-exists-p agentview)
1803 (delete-file agentview))
1805 (gnus-agent-update-view-total-fetched-for group nil gnus-command-method)
1806 (gnus-agent-update-view-total-fetched-for group t gnus-command-method)
1808 ;(gnus-agent-set-local group nil nil)
1809 ;(gnus-agent-save-local t)
1810 (gnus-agent-save-group-info nil group nil)))
1812 (defun gnus-agent-flush-cache ()
1813 "Flush the agent's index files such that the group no longer
1814 appears to have any local content. The actual content, the
1815 article files, is then deleted using gnus-agent-expire-group. The
1816 gnus-agent-regenerate-group method provides an undo mechanism by
1817 reconstructing the index files from the article files."
1818 (interactive)
1819 (save-excursion
1820 (let ((file-name-coding-system nnmail-pathname-coding-system))
1821 (while gnus-agent-buffer-alist
1822 (set-buffer (cdar gnus-agent-buffer-alist))
1823 (let ((coding-system-for-write gnus-agent-file-coding-system))
1824 (write-region (point-min) (point-max)
1825 (gnus-agent-article-name ".overview"
1826 (caar gnus-agent-buffer-alist))
1827 nil 'silent))
1828 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1829 (while gnus-agent-group-alist
1830 (with-temp-file (gnus-agent-article-name
1831 ".agentview" (caar gnus-agent-group-alist))
1832 (princ (cdar gnus-agent-group-alist))
1833 (insert "\n")
1834 (princ 1 (current-buffer))
1835 (insert "\n"))
1836 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist))))))
1838 ;;;###autoload
1839 (defun gnus-agent-find-parameter (group symbol)
1840 "Search for GROUPs SYMBOL in the group's parameters, the group's
1841 topic parameters, the group's category, or the customizable
1842 variables. Returns the first non-nil value found."
1843 (or (gnus-group-find-parameter group symbol t)
1844 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1845 (symbol-value
1846 (cdr
1847 (assq symbol
1848 '((agent-short-article . gnus-agent-short-article)
1849 (agent-long-article . gnus-agent-long-article)
1850 (agent-low-score . gnus-agent-low-score)
1851 (agent-high-score . gnus-agent-high-score)
1852 (agent-days-until-old . gnus-agent-expire-days)
1853 (agent-enable-expiration
1854 . gnus-agent-enable-expiration)
1855 (agent-predicate . gnus-agent-predicate)))))))
1857 (defun gnus-agent-fetch-headers (group &optional force)
1858 "Fetch interesting headers into the agent. The group's overview
1859 file will be updated to include the headers while a list of available
1860 article numbers will be returned."
1861 (let* ((fetch-all (and gnus-agent-consider-all-articles
1862 ;; Do not fetch all headers if the predicate
1863 ;; implies that we only consider unread articles.
1864 (not (gnus-predicate-implies-unread
1865 (gnus-agent-find-parameter group
1866 'agent-predicate)))))
1867 (articles (if fetch-all
1868 (if gnus-newsgroup-maximum-articles
1869 (let ((active (gnus-active group)))
1870 (gnus-uncompress-range
1871 (cons (max (car active)
1872 (- (cdr active)
1873 gnus-newsgroup-maximum-articles
1874 -1))
1875 (cdr active))))
1876 (gnus-uncompress-range (gnus-active group)))
1877 (gnus-list-of-unread-articles group)))
1878 (gnus-decode-encoded-word-function 'identity)
1879 (gnus-decode-encoded-address-function 'identity)
1880 (file (gnus-agent-article-name ".overview" group))
1881 (file-name-coding-system nnmail-pathname-coding-system))
1883 (unless fetch-all
1884 ;; Add articles with marks to the list of article headers we want to
1885 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1886 ;; mark, but do fetch recent or seen articles if they have other, more
1887 ;; interesting marks. (We have to fetch articles with boring marks
1888 ;; because otherwise the agent will remove their marks.)
1889 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1890 (unless (memq (car arts) '(seen recent killed cache))
1891 (setq articles (gnus-range-add articles (cdr arts)))))
1892 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1894 ;; At this point, I have the list of articles to consider for
1895 ;; fetching. This is the list that I'll return to my caller. Some
1896 ;; of these articles may have already been fetched. That's OK as
1897 ;; the fetch article code will filter those out. Internally, I'll
1898 ;; filter this list to just those articles whose headers need to
1899 ;; be fetched.
1900 (let ((articles articles))
1901 ;; Remove known articles.
1902 (when (and (or gnus-agent-cache
1903 (not gnus-plugged))
1904 (gnus-agent-load-alist group))
1905 ;; Remove articles marked as downloaded.
1906 (if fetch-all
1907 ;; I want to fetch all headers in the active range.
1908 ;; Therefore, exclude only those headers that are in the
1909 ;; article alist.
1910 ;; NOTE: This is probably NOT what I want to do after
1911 ;; agent expiration in this group.
1912 (setq articles (gnus-agent-uncached-articles articles group))
1914 ;; I want to only fetch those headers that have never been
1915 ;; fetched. Therefore, exclude all headers that are, or
1916 ;; WERE, in the article alist.
1917 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1918 (high (cdr (gnus-active group))))
1919 ;; Low can be greater than High when the same group is
1920 ;; fetched twice in the same session {The first fetch will
1921 ;; fill the article alist such that (last
1922 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1923 ;; group))}. The addition of one(the 1+ above) then
1924 ;; forces Low to be greater than High. When this happens,
1925 ;; gnus-list-range-intersection returns nil which
1926 ;; indicates that no headers need to be fetched. -- Kevin
1927 (setq articles (gnus-list-range-intersection
1928 articles (list (cons low high)))))))
1930 (when articles
1931 (gnus-message
1932 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1933 (gnus-compress-sequence articles t)))
1935 (with-current-buffer nntp-server-buffer
1936 (if articles
1937 (progn
1938 (gnus-message 8 "Fetching headers for %s..."
1939 (gnus-agent-decoded-group-name group))
1941 ;; Fetch them.
1942 (gnus-make-directory (nnheader-translate-file-chars
1943 (file-name-directory file) t))
1945 (unless (eq 'nov (gnus-retrieve-headers articles group))
1946 (nnvirtual-convert-headers))
1947 (gnus-agent-check-overview-buffer)
1948 ;; Move these headers to the overview buffer so that
1949 ;; gnus-agent-braid-nov can merge them with the contents
1950 ;; of FILE.
1951 (copy-to-buffer
1952 gnus-agent-overview-buffer (point-min) (point-max))
1953 ;; NOTE: Call g-a-brand-nov even when the file does not
1954 ;; exist. As a minimum, it will validate the article
1955 ;; numbers already in the buffer.
1956 (gnus-agent-braid-nov group articles file)
1957 (let ((coding-system-for-write
1958 gnus-agent-file-coding-system))
1959 (gnus-agent-check-overview-buffer)
1960 (write-region (point-min) (point-max) file nil 'silent))
1961 (gnus-agent-update-view-total-fetched-for group t)
1962 (gnus-agent-save-alist group articles nil)
1963 articles)
1964 (ignore-errors
1965 (erase-buffer)
1966 (nnheader-insert-file-contents file)))))
1967 articles))
1969 (defsubst gnus-agent-read-article-number ()
1970 "Reads the article number at point. Returns nil when a valid article number can not be read."
1972 ;; It is unfortunate but the read function quietly overflows
1973 ;; integer. As a result, I have to use string operations to test
1974 ;; for overflow BEFORE calling read.
1975 (when (looking-at "[0-9]+\t")
1976 (let ((len (- (match-end 0) (match-beginning 0))))
1977 (cond ((< len 9)
1978 (read (current-buffer)))
1979 ((= len 9)
1980 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1981 ;; Back convert from int to string to ensure that this is one of them.
1982 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1983 (num (read (current-buffer)))
1984 (str2 (int-to-string num)))
1985 (when (equal str1 str2)
1986 num)))))))
1988 (defsubst gnus-agent-copy-nov-line (article)
1989 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1990 (let (art b e)
1991 (set-buffer gnus-agent-overview-buffer)
1992 (while (and (not (eobp))
1993 (or (not (setq art (gnus-agent-read-article-number)))
1994 (< art article)))
1995 (forward-line 1))
1996 (beginning-of-line)
1997 (if (or (eobp)
1998 (not (eq article art)))
1999 (set-buffer nntp-server-buffer)
2000 (setq b (point))
2001 (setq e (progn (forward-line 1) (point)))
2002 (set-buffer nntp-server-buffer)
2003 (insert-buffer-substring gnus-agent-overview-buffer b e))))
2005 (defun gnus-agent-braid-nov (group articles file)
2006 "Merge agent overview data with given file.
2007 Takes unvalidated headers for ARTICLES from
2008 `gnus-agent-overview-buffer' and validated headers from the given
2009 FILE and places the combined valid headers into
2010 `nntp-server-buffer'. This function can be used, when file
2011 doesn't exist, to valid the overview buffer."
2012 (let (start last)
2013 (set-buffer gnus-agent-overview-buffer)
2014 (goto-char (point-min))
2015 (set-buffer nntp-server-buffer)
2016 (erase-buffer)
2017 (when (file-exists-p file)
2018 (nnheader-insert-file-contents file))
2019 (goto-char (point-max))
2020 (forward-line -1)
2022 (unless (or (= (point-min) (point-max))
2023 (< (setq last (read (current-buffer))) (car articles)))
2024 ;; Old and new overlap -- We do it the hard way.
2025 (when (nnheader-find-nov-line (car articles))
2026 ;; Replacing existing NOV entry
2027 (delete-region (point) (progn (forward-line 1) (point))))
2028 (gnus-agent-copy-nov-line (pop articles))
2030 (ignore-errors
2031 (while articles
2032 (while (let ((art (read (current-buffer))))
2033 (cond ((< art (car articles))
2034 (forward-line 1)
2036 ((= art (car articles))
2037 (beginning-of-line)
2038 (delete-region
2039 (point) (progn (forward-line 1) (point)))
2040 nil)
2042 (beginning-of-line)
2043 nil))))
2045 (gnus-agent-copy-nov-line (pop articles)))))
2047 (goto-char (point-max))
2049 ;; Append the remaining lines
2050 (when articles
2051 (when last
2052 (set-buffer gnus-agent-overview-buffer)
2053 (setq start (point))
2054 (set-buffer nntp-server-buffer))
2056 (let ((p (point)))
2057 (insert-buffer-substring gnus-agent-overview-buffer start)
2058 (goto-char p))
2060 (setq last (or last -134217728))
2061 (while (catch 'problems
2062 (let (sort art)
2063 (while (not (eobp))
2064 (setq art (gnus-agent-read-article-number))
2065 (cond ((not art)
2066 ;; Bad art num - delete this line
2067 (beginning-of-line)
2068 (delete-region (point) (progn (forward-line 1) (point))))
2069 ((< art last)
2070 ;; Art num out of order - enable sort
2071 (setq sort t)
2072 (forward-line 1))
2073 ((= art last)
2074 ;; Bad repeat of art number - delete this line
2075 (beginning-of-line)
2076 (delete-region (point) (progn (forward-line 1) (point))))
2078 ;; Good art num
2079 (setq last art)
2080 (forward-line 1))))
2081 (when sort
2082 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
2083 ;; First, we'll fix the sort.
2084 (sort-numeric-fields 1 (point-min) (point-max))
2086 ;; but now we have to consider that we may have duplicate rows...
2087 ;; so reset to beginning of file
2088 (goto-char (point-min))
2089 (setq last -134217728)
2091 ;; and throw a code that restarts this scan
2092 (throw 'problems t))
2093 nil))))))
2095 ;; Keeps the compiler from warning about the free variable in
2096 ;; gnus-agent-read-agentview.
2097 (defvar gnus-agent-read-agentview)
2099 (defun gnus-agent-load-alist (group)
2100 "Load the article-state alist for GROUP."
2101 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
2102 (let* ((gnus-agent-read-agentview group)
2103 (file-name-coding-system nnmail-pathname-coding-system)
2104 (agentview (gnus-agent-article-name ".agentview" group)))
2105 (setq gnus-agent-article-alist
2106 (and (file-exists-p agentview)
2107 (gnus-cache-file-contents
2108 agentview
2109 'gnus-agent-file-loading-cache
2110 'gnus-agent-read-agentview)))))
2112 (defun gnus-agent-read-agentview (file)
2113 "Load FILE and do a `read' there."
2114 (with-temp-buffer
2115 (condition-case nil
2116 (progn
2117 (nnheader-insert-file-contents file)
2118 (goto-char (point-min))
2119 (let ((alist (read (current-buffer)))
2120 (version (condition-case nil (read (current-buffer))
2121 (end-of-file 0)))
2122 changed-version)
2124 (cond
2125 ((= version 0)
2126 (let ((inhibit-quit t)
2127 entry)
2128 (gnus-agent-open-history)
2129 (set-buffer (gnus-agent-history-buffer))
2130 (goto-char (point-min))
2131 (while (not (eobp))
2132 (if (and (looking-at
2133 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2134 (string= (match-string 2)
2135 gnus-agent-read-agentview)
2136 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2137 (setcdr entry (string-to-number (match-string 1))))
2138 (forward-line 1))
2139 (gnus-agent-close-history)
2140 (setq changed-version t)))
2141 ((= version 1)
2142 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2143 ((= version 2)
2144 (let (state sequence uncomp)
2145 (while alist
2146 (setq state (caar alist)
2147 sequence (inline (gnus-uncompress-range (cdar alist)))
2148 alist (cdr alist))
2149 (while sequence
2150 (push (cons (pop sequence) state) uncomp)))
2151 (setq alist (sort uncomp 'car-less-than-car)))
2152 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2153 (when changed-version
2154 (let ((gnus-agent-article-alist alist))
2155 (gnus-agent-save-alist gnus-agent-read-agentview)))
2156 alist))
2157 ((end-of-file file-error)
2158 ;; The agentview file is missing.
2159 (condition-case nil
2160 ;; If the agent directory exists, attempt to perform a brute-force
2161 ;; reconstruction of its contents.
2162 (let* (alist
2163 (file-name-coding-system nnmail-pathname-coding-system)
2164 (file-attributes (directory-files-and-attributes
2165 (gnus-agent-article-name ""
2166 gnus-agent-read-agentview) nil "^[0-9]+$" t)))
2167 (while file-attributes
2168 (let ((fa (pop file-attributes)))
2169 (unless (nth 1 fa)
2170 (push (cons (string-to-number (nth 0 fa)) (time-to-days (nth 5 fa))) alist))))
2171 alist)
2172 (file-error nil))))))
2174 (defun gnus-agent-save-alist (group &optional articles state)
2175 "Save the article-state alist for GROUP."
2176 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2177 (prev (cons nil gnus-agent-article-alist))
2178 (all prev)
2179 print-level print-length item article)
2180 (while (setq article (pop articles))
2181 (while (and (cdr prev)
2182 (< (caadr prev) article))
2183 (setq prev (cdr prev)))
2184 (cond
2185 ((not (cdr prev))
2186 (setcdr prev (list (cons article state))))
2187 ((> (caadr prev) article)
2188 (setcdr prev (cons (cons article state) (cdr prev))))
2189 ((= (caadr prev) article)
2190 (setcdr (cadr prev) state)))
2191 (setq prev (cdr prev)))
2192 (setq gnus-agent-article-alist (cdr all))
2194 (gnus-agent-set-local group
2195 (caar gnus-agent-article-alist)
2196 (caar (last gnus-agent-article-alist)))
2198 (gnus-make-directory (gnus-agent-article-name "" group))
2199 (with-temp-file (gnus-agent-article-name ".agentview" group)
2200 (cond ((eq gnus-agent-article-alist-save-format 1)
2201 (princ gnus-agent-article-alist (current-buffer)))
2202 ((eq gnus-agent-article-alist-save-format 2)
2203 (let ((alist gnus-agent-article-alist)
2204 article-id day-of-download comp-list compressed)
2205 (while alist
2206 (setq article-id (caar alist)
2207 day-of-download (cdar alist)
2208 comp-list (assq day-of-download compressed)
2209 alist (cdr alist))
2210 (if comp-list
2211 (setcdr comp-list (cons article-id (cdr comp-list)))
2212 (push (list day-of-download article-id) compressed)))
2213 (setq alist compressed)
2214 (while alist
2215 (setq comp-list (pop alist))
2216 (setcdr comp-list
2217 (gnus-compress-sequence (nreverse (cdr comp-list)))))
2218 (princ compressed (current-buffer)))))
2219 (insert "\n")
2220 (princ gnus-agent-article-alist-save-format (current-buffer))
2221 (insert "\n"))
2223 (gnus-agent-update-view-total-fetched-for group nil)))
2225 (defvar gnus-agent-article-local nil)
2226 (defvar gnus-agent-article-local-times nil)
2227 (defvar gnus-agent-file-loading-local nil)
2229 (defun gnus-agent-load-local (&optional method)
2230 "Load the METHOD'S local file. The local file contains min/max
2231 article counts for each of the method's subscribed groups."
2232 (let ((gnus-command-method (or method gnus-command-method)))
2233 (when (or (null gnus-agent-article-local-times)
2234 (zerop gnus-agent-article-local-times)
2235 (not (gnus-methods-equal-p
2236 gnus-command-method
2237 (symbol-value (intern "+method" gnus-agent-article-local)))))
2238 (setq gnus-agent-article-local
2239 (gnus-cache-file-contents
2240 (gnus-agent-lib-file "local")
2241 'gnus-agent-file-loading-local
2242 'gnus-agent-read-and-cache-local))
2243 (when gnus-agent-article-local-times
2244 (incf gnus-agent-article-local-times)))
2245 gnus-agent-article-local))
2247 (defun gnus-agent-read-and-cache-local (file)
2248 "Load and read FILE then bind its contents to
2249 gnus-agent-article-local. If that variable had `dirty' (also known as
2250 modified) original contents, they are first saved to their own file."
2251 (if (and gnus-agent-article-local
2252 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2253 (gnus-agent-save-local))
2254 (gnus-agent-read-local file))
2256 (defun gnus-agent-read-local (file)
2257 "Load FILE and do a `read' there."
2258 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2259 (point-max))))
2260 (line 1))
2261 (with-temp-buffer
2262 (condition-case nil
2263 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2264 (nnheader-insert-file-contents file))
2265 (file-error))
2267 (goto-char (point-min))
2268 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2269 (while (= (following-char) ?\;)
2270 (forward-line 1)
2271 (setq line (1+ line)))
2273 (while (not (eobp))
2274 (condition-case err
2275 (let (group
2278 (cur (current-buffer))
2279 (obarray my-obarray))
2280 (setq group (read cur)
2281 min (read cur)
2282 max (read cur))
2284 (when (stringp group)
2285 (setq group (intern group my-obarray)))
2287 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2288 (set group (cons (+ 0 min) (+ 0 max))))
2289 (error
2290 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2291 file line (error-message-string err))))
2292 (forward-line 1)
2293 (setq line (1+ line))))
2295 (set (intern "+dirty" my-obarray) nil)
2296 (set (intern "+method" my-obarray) gnus-command-method)
2297 my-obarray))
2299 (defun gnus-agent-save-local (&optional force)
2300 "Save gnus-agent-article-local under it method's agent.lib directory."
2301 (let ((my-obarray gnus-agent-article-local))
2302 (when (and my-obarray
2303 (or force (symbol-value (intern "+dirty" my-obarray))))
2304 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2305 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2306 (dest (gnus-agent-lib-file "local")))
2307 (gnus-make-directory (gnus-agent-lib-file ""))
2309 (let ((coding-system-for-write gnus-agent-file-coding-system)
2310 (file-name-coding-system nnmail-pathname-coding-system))
2311 (with-temp-file dest
2312 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2313 print-level print-length item article
2314 (standard-output (current-buffer)))
2315 (mapatoms (lambda (symbol)
2316 (cond ((not (boundp symbol))
2317 nil)
2318 ((member (symbol-name symbol) '("+dirty" "+method"))
2319 nil)
2321 (let ((range (symbol-value symbol)))
2322 (when range
2323 (prin1 symbol)
2324 (princ " ")
2325 (princ (car range))
2326 (princ " ")
2327 (princ (cdr range))
2328 (princ "\n"))))))
2329 my-obarray))))))))
2331 (defun gnus-agent-get-local (group &optional gmane method)
2332 (let* ((gmane (or gmane (gnus-group-real-name group)))
2333 (gnus-command-method (or method (gnus-find-method-for-group group)))
2334 (local (gnus-agent-load-local))
2335 (symb (intern gmane local))
2336 (minmax (and (boundp symb) (symbol-value symb))))
2337 (unless minmax
2338 ;; Bind these so that gnus-agent-load-alist doesn't change the
2339 ;; current alist (i.e. gnus-agent-article-alist)
2340 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2341 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2342 (alist (gnus-agent-load-alist group)))
2343 (when alist
2344 (setq minmax
2345 (cons (caar alist)
2346 (caar (last alist))))
2347 (gnus-agent-set-local group (car minmax) (cdr minmax)
2348 gmane gnus-command-method local))))
2349 minmax))
2351 (defun gnus-agent-set-local (group min max &optional gmane method local)
2352 (let* ((gmane (or gmane (gnus-group-real-name group)))
2353 (gnus-command-method (or method (gnus-find-method-for-group group)))
2354 (local (or local (gnus-agent-load-local)))
2355 (symb (intern gmane local))
2356 (minmax (and (boundp symb) (symbol-value symb))))
2357 (if (cond ((and minmax
2358 (or (not (eq min (car minmax)))
2359 (not (eq max (cdr minmax))))
2361 max)
2362 (setcar minmax min)
2363 (setcdr minmax max)
2365 (minmax
2366 nil)
2367 ((and min max)
2368 (set symb (cons min max))
2371 (unintern symb local)))
2372 (set (intern "+dirty" local) t))))
2374 (defun gnus-agent-article-name (article group)
2375 (expand-file-name article
2376 (file-name-as-directory
2377 (gnus-agent-group-pathname group))))
2379 (defun gnus-agent-batch-confirmation (msg)
2380 "Show error message and return t."
2381 (gnus-message 1 "%s" msg)
2384 ;;;###autoload
2385 (defun gnus-agent-batch-fetch ()
2386 "Start Gnus and fetch session."
2387 (interactive)
2388 (gnus)
2389 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2390 (gnus-agent-fetch-session))
2391 (gnus-group-exit))
2393 (defun gnus-agent-fetch-session ()
2394 "Fetch all articles and headers that are eligible for fetching."
2395 (interactive)
2396 (unless gnus-agent-covered-methods
2397 (error "No servers are covered by the Gnus agent"))
2398 (unless gnus-plugged
2399 (error "Can't fetch articles while Gnus is unplugged"))
2400 (let ((methods (gnus-agent-covered-methods))
2401 groups group gnus-command-method)
2402 (save-excursion
2403 (while methods
2404 (setq gnus-command-method (car methods))
2405 (when (and (or (gnus-server-opened gnus-command-method)
2406 (gnus-open-server gnus-command-method))
2407 (gnus-online gnus-command-method))
2408 (setq groups (gnus-groups-from-server (car methods)))
2409 (gnus-agent-with-fetch
2410 (while (setq group (pop groups))
2411 (when (<= (gnus-group-level group)
2412 gnus-agent-handle-level)
2413 (if (or debug-on-error debug-on-quit)
2414 (gnus-agent-fetch-group-1
2415 group gnus-command-method)
2416 (condition-case err
2417 (gnus-agent-fetch-group-1
2418 group gnus-command-method)
2419 (error
2420 (unless (funcall gnus-agent-confirmation-function
2421 (format "Error %s while fetching session. Should gnus continue? "
2422 (error-message-string err)))
2423 (error "Cannot fetch articles into the Gnus agent")))
2424 (quit
2425 (gnus-agent-regenerate-group group)
2426 (unless (funcall gnus-agent-confirmation-function
2427 (format
2428 "%s while fetching session. Should gnus continue? "
2429 (error-message-string err)))
2430 (signal 'quit
2431 "Cannot fetch articles into the Gnus agent")))))))))
2432 (setq methods (cdr methods)))
2433 (gnus-run-hooks 'gnus-agent-fetched-hook)
2434 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2436 (defun gnus-agent-fetch-group-1 (group method)
2437 "Fetch GROUP."
2438 (let ((gnus-command-method method)
2439 (gnus-newsgroup-name group)
2440 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2441 (gnus-newsgroup-headers gnus-newsgroup-headers)
2442 (gnus-newsgroup-scored gnus-newsgroup-scored)
2443 (gnus-use-cache gnus-use-cache)
2444 (gnus-summary-expunge-below gnus-summary-expunge-below)
2445 (gnus-summary-mark-below gnus-summary-mark-below)
2446 (gnus-orphan-score gnus-orphan-score)
2447 ;; Maybe some other gnus-summary local variables should also
2448 ;; be put here.
2450 gnus-headers
2451 gnus-score
2452 articles arts
2453 category predicate info marks score-param
2455 (unless (gnus-check-group group)
2456 (error "Can't open server for %s" group))
2458 ;; Fetch headers.
2459 (when (or gnus-newsgroup-active
2460 (gnus-active group)
2461 (gnus-activate-group group))
2462 (let ((marked-articles gnus-newsgroup-downloadable))
2463 ;; Identify the articles marked for download
2464 (unless gnus-newsgroup-active
2465 ;; The variable gnus-newsgroup-active was selected as I need
2466 ;; a gnus-summary local variable that is NOT bound to any
2467 ;; value (its global value should default to nil).
2468 (dolist (mark gnus-agent-download-marks)
2469 (let ((arts (cdr (assq mark (gnus-info-marks
2470 (setq info (gnus-get-info group)))))))
2471 (when arts
2472 (setq marked-articles (nconc (gnus-uncompress-range arts)
2473 marked-articles))
2474 ))))
2475 (setq marked-articles (sort marked-articles '<))
2477 ;; Fetch any new articles from the server
2478 (setq articles (gnus-agent-fetch-headers group))
2480 ;; Merge new articles with marked
2481 (setq articles (sort (append marked-articles articles) '<))
2483 (when articles
2484 ;; Parse them and see which articles we want to fetch.
2485 (setq gnus-newsgroup-dependencies
2486 (or gnus-newsgroup-dependencies
2487 (make-vector (length articles) 0)))
2488 (setq gnus-newsgroup-headers
2489 (or gnus-newsgroup-headers
2490 (gnus-get-newsgroup-headers-xover articles nil nil
2491 group)))
2492 ;; `gnus-agent-overview-buffer' may be killed for
2493 ;; timeout reason. If so, recreate it.
2494 (gnus-agent-create-buffer)
2496 ;; Figure out how to select articles in this group
2497 (setq category (gnus-group-category group))
2499 (setq predicate
2500 (gnus-get-predicate
2501 (gnus-agent-find-parameter group 'agent-predicate)))
2503 ;; If the selection predicate requires scoring, score each header
2504 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2505 (let ((score-param
2506 (gnus-agent-find-parameter group 'agent-score-file)))
2507 ;; Translate score-param into real one
2508 (cond
2509 ((not score-param))
2510 ((eq score-param 'file)
2511 (setq score-param (gnus-all-score-files group)))
2512 ((stringp (car score-param)))
2514 (setq score-param (list (list score-param)))))
2515 (when score-param
2516 (gnus-score-headers score-param))))
2518 (unless (and (eq predicate 'gnus-agent-false)
2519 (not marked-articles))
2520 (let ((arts (list nil)))
2521 (let ((arts-tail arts)
2522 (alist (gnus-agent-load-alist group))
2523 (marked-articles marked-articles)
2524 (gnus-newsgroup-headers gnus-newsgroup-headers))
2525 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2526 (let ((num (mail-header-number gnus-headers)))
2527 ;; Determine if this article is already in the cache
2528 (while (and alist
2529 (> num (caar alist)))
2530 (setq alist (cdr alist)))
2532 (unless (and (eq num (caar alist))
2533 (cdar alist))
2535 ;; Determine if this article was marked for download.
2536 (while (and marked-articles
2537 (> num (car marked-articles)))
2538 (setq marked-articles
2539 (cdr marked-articles)))
2541 ;; When this article is marked, or selected by the
2542 ;; predicate, add it to the download list
2543 (when (or (eq num (car marked-articles))
2544 (let ((gnus-score
2545 (or (cdr
2546 (assq num gnus-newsgroup-scored))
2547 gnus-summary-default-score))
2548 (gnus-agent-long-article
2549 (gnus-agent-find-parameter
2550 group 'agent-long-article))
2551 (gnus-agent-short-article
2552 (gnus-agent-find-parameter
2553 group 'agent-short-article))
2554 (gnus-agent-low-score
2555 (gnus-agent-find-parameter
2556 group 'agent-low-score))
2557 (gnus-agent-high-score
2558 (gnus-agent-find-parameter
2559 group 'agent-high-score))
2560 (gnus-agent-expire-days
2561 (gnus-agent-find-parameter
2562 group 'agent-days-until-old)))
2563 (funcall predicate)))
2564 (gnus-agent-append-to-list arts-tail num))))))
2566 (let (fetched-articles)
2567 ;; Fetch all selected articles
2568 (setq gnus-newsgroup-undownloaded
2569 (gnus-sorted-ndifference
2570 gnus-newsgroup-undownloaded
2571 (setq fetched-articles
2572 (if (cdr arts)
2573 (gnus-agent-fetch-articles group (cdr arts))
2574 nil))))
2576 (let ((unfetched-articles
2577 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2578 (if gnus-newsgroup-active
2579 ;; Update the summary buffer
2580 (progn
2581 (dolist (article marked-articles)
2582 (gnus-summary-set-agent-mark article t))
2583 (dolist (article fetched-articles)
2584 (when gnus-agent-mark-unread-after-downloaded
2585 (setq gnus-newsgroup-downloadable
2586 (delq article gnus-newsgroup-downloadable))
2587 (gnus-summary-mark-article
2588 article gnus-unread-mark))
2589 (when (gnus-summary-goto-subject article nil t)
2590 (gnus-summary-update-download-mark article)))
2591 (dolist (article unfetched-articles)
2592 (gnus-summary-mark-article
2593 article gnus-canceled-mark)))
2595 ;; Update the group buffer.
2597 ;; When some, or all, of the marked articles came
2598 ;; from the download mark. Remove that mark. I
2599 ;; didn't do this earlier as I only want to remove
2600 ;; the marks after the fetch is completed.
2602 (dolist (mark gnus-agent-download-marks)
2603 (when (eq mark 'download)
2604 (let ((marked-arts
2605 (assq mark (gnus-info-marks
2606 (setq info (gnus-get-info group))))))
2607 (when (cdr marked-arts)
2608 (setq marks
2609 (delq marked-arts (gnus-info-marks info)))
2610 (gnus-info-set-marks info marks)))))
2611 (let ((read (gnus-info-read
2612 (or info (setq info (gnus-get-info group))))))
2613 (gnus-info-set-read
2614 info (gnus-add-to-range read unfetched-articles)))
2616 (gnus-group-update-group group t)
2617 (sit-for 0)
2619 (gnus-dribble-enter
2620 (concat "(gnus-group-set-info '"
2621 (gnus-prin1-to-string info)
2622 ")")
2623 (concat "^(gnus-group-set-info '(\""
2624 (regexp-quote group) "\""))))))))))))
2627 ;;; Agent Category Mode
2630 (defvar gnus-category-mode-hook nil
2631 "Hook run in `gnus-category-mode' buffers.")
2633 (defvar gnus-category-line-format " %(%20c%): %g\n"
2634 "Format of category lines.
2636 Valid specifiers include:
2637 %c Topic name (string)
2638 %g The number of groups in the topic (integer)
2640 General format specifiers can also be used. See Info node
2641 `(gnus)Formatting Variables'.")
2643 (defvar gnus-category-mode-line-format "Gnus: %%b"
2644 "The format specification for the category mode line.")
2646 (defvar gnus-agent-predicate 'false
2647 "The selection predicate used when no other source is available.")
2649 (defvar gnus-agent-short-article 500
2650 "Articles that have fewer lines than this are short.")
2652 (defvar gnus-agent-long-article 1000
2653 "Articles that have more lines than this are long.")
2655 (defvar gnus-agent-low-score 0
2656 "Articles that have a score lower than this have a low score.")
2658 (defvar gnus-agent-high-score 0
2659 "Articles that have a score higher than this have a high score.")
2662 ;;; Internal variables.
2664 (defvar gnus-category-buffer "*Agent Category*")
2666 (defvar gnus-category-line-format-alist
2667 `((?c gnus-tmp-name ?s)
2668 (?g gnus-tmp-groups ?d)))
2670 (defvar gnus-category-mode-line-format-alist
2671 `((?u user-defined ?s)))
2673 (defvar gnus-category-line-format-spec nil)
2674 (defvar gnus-category-mode-line-format-spec nil)
2676 (defvar gnus-category-mode-map nil)
2677 (put 'gnus-category-mode 'mode-class 'special)
2679 (unless gnus-category-mode-map
2680 (setq gnus-category-mode-map (make-sparse-keymap))
2681 (suppress-keymap gnus-category-mode-map)
2683 (gnus-define-keys gnus-category-mode-map
2684 "q" gnus-category-exit
2685 "k" gnus-category-kill
2686 "c" gnus-category-copy
2687 "a" gnus-category-add
2688 "e" gnus-agent-customize-category
2689 "p" gnus-category-edit-predicate
2690 "g" gnus-category-edit-groups
2691 "s" gnus-category-edit-score
2692 "l" gnus-category-list
2694 "\C-c\C-i" gnus-info-find-node
2695 "\C-c\C-b" gnus-bug))
2697 (defvar gnus-category-menu-hook nil
2698 "*Hook run after the creation of the menu.")
2700 (defun gnus-category-make-menu-bar ()
2701 (gnus-turn-off-edit-menu 'category)
2702 (unless (boundp 'gnus-category-menu)
2703 (easy-menu-define
2704 gnus-category-menu gnus-category-mode-map ""
2705 '("Categories"
2706 ["Add" gnus-category-add t]
2707 ["Kill" gnus-category-kill t]
2708 ["Copy" gnus-category-copy t]
2709 ["Edit category" gnus-agent-customize-category t]
2710 ["Edit predicate" gnus-category-edit-predicate t]
2711 ["Edit score" gnus-category-edit-score t]
2712 ["Edit groups" gnus-category-edit-groups t]
2713 ["Exit" gnus-category-exit t]))
2715 (gnus-run-hooks 'gnus-category-menu-hook)))
2717 (defun gnus-category-mode ()
2718 "Major mode for listing and editing agent categories.
2720 All normal editing commands are switched off.
2721 \\<gnus-category-mode-map>
2722 For more in-depth information on this mode, read the manual
2723 \(`\\[gnus-info-find-node]').
2725 The following commands are available:
2727 \\{gnus-category-mode-map}"
2728 (interactive)
2729 (when (gnus-visual-p 'category-menu 'menu)
2730 (gnus-category-make-menu-bar))
2731 (kill-all-local-variables)
2732 (gnus-simplify-mode-line)
2733 (setq major-mode 'gnus-category-mode)
2734 (setq mode-name "Category")
2735 (gnus-set-default-directory)
2736 (setq mode-line-process nil)
2737 (use-local-map gnus-category-mode-map)
2738 (buffer-disable-undo)
2739 (setq truncate-lines t)
2740 (setq buffer-read-only t)
2741 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2743 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2745 (defun gnus-category-insert-line (category)
2746 (let* ((gnus-tmp-name (format "%s" (car category)))
2747 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2748 (beginning-of-line)
2749 (gnus-add-text-properties
2750 (point)
2751 (prog1 (1+ (point))
2752 ;; Insert the text.
2753 (eval gnus-category-line-format-spec))
2754 (list 'gnus-category gnus-tmp-name))))
2756 (defun gnus-enter-category-buffer ()
2757 "Go to the Category buffer."
2758 (interactive)
2759 (gnus-category-setup-buffer)
2760 (gnus-configure-windows 'category)
2761 (gnus-category-prepare))
2763 (defun gnus-category-setup-buffer ()
2764 (unless (get-buffer gnus-category-buffer)
2765 (with-current-buffer (gnus-get-buffer-create gnus-category-buffer)
2766 (gnus-category-mode))))
2768 (defun gnus-category-prepare ()
2769 (gnus-set-format 'category-mode)
2770 (gnus-set-format 'category t)
2771 (let ((alist gnus-category-alist)
2772 (buffer-read-only nil))
2773 (erase-buffer)
2774 (while alist
2775 (gnus-category-insert-line (pop alist)))
2776 (goto-char (point-min))
2777 (gnus-category-position-point)))
2779 (defun gnus-category-name ()
2780 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2781 (error "No category on the current line")))
2783 (defun gnus-category-read ()
2784 "Read the category alist."
2785 (setq gnus-category-alist
2787 (with-temp-buffer
2788 (ignore-errors
2789 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2790 (goto-char (point-min))
2791 ;; This code isn't temp, it will be needed so long as
2792 ;; anyone may be migrating from an older version.
2794 ;; Once we're certain that people will not revert to an
2795 ;; earlier version, we can take out the old-list code in
2796 ;; gnus-category-write.
2797 (let* ((old-list (read (current-buffer)))
2798 (new-list (ignore-errors (read (current-buffer)))))
2799 (if new-list
2800 new-list
2801 ;; Convert from a positional list to an alist.
2802 (mapcar
2803 (lambda (c)
2804 (setcdr c
2805 (delq nil
2806 (gnus-mapcar
2807 (lambda (valu symb)
2808 (if valu
2809 (cons symb valu)))
2810 (cdr c)
2811 '(agent-predicate agent-score-file agent-groups))))
2813 old-list)))))
2814 (list (gnus-agent-cat-make 'default 'short)))))
2816 (defun gnus-category-write ()
2817 "Write the category alist."
2818 (setq gnus-category-predicate-cache nil
2819 gnus-category-group-cache nil)
2820 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2821 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2822 ;; This prin1 is temporary. It exists so that people can revert
2823 ;; to an earlier version of gnus-agent.
2824 (prin1 (mapcar (lambda (c)
2825 (list (car c)
2826 (cdr (assoc 'agent-predicate c))
2827 (cdr (assoc 'agent-score-file c))
2828 (cdr (assoc 'agent-groups c))))
2829 gnus-category-alist)
2830 (current-buffer))
2831 (newline)
2832 (prin1 gnus-category-alist (current-buffer))))
2834 (defun gnus-category-edit-predicate (category)
2835 "Edit the predicate for CATEGORY."
2836 (interactive (list (gnus-category-name)))
2837 (let ((info (assq category gnus-category-alist)))
2838 (gnus-edit-form
2839 (gnus-agent-cat-predicate info)
2840 (format "Editing the select predicate for category %s" category)
2841 `(lambda (predicate)
2842 ;; Avoid run-time execution of setf form
2843 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2844 ;; predicate)
2845 ;; use its expansion instead:
2846 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2847 'agent-predicate predicate)
2849 (gnus-category-write)
2850 (gnus-category-list)))))
2852 (defun gnus-category-edit-score (category)
2853 "Edit the score expression for CATEGORY."
2854 (interactive (list (gnus-category-name)))
2855 (let ((info (assq category gnus-category-alist)))
2856 (gnus-edit-form
2857 (gnus-agent-cat-score-file info)
2858 (format "Editing the score expression for category %s" category)
2859 `(lambda (score-file)
2860 ;; Avoid run-time execution of setf form
2861 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2862 ;; score-file)
2863 ;; use its expansion instead:
2864 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2865 'agent-score-file score-file)
2867 (gnus-category-write)
2868 (gnus-category-list)))))
2870 (defun gnus-category-edit-groups (category)
2871 "Edit the group list for CATEGORY."
2872 (interactive (list (gnus-category-name)))
2873 (let ((info (assq category gnus-category-alist)))
2874 (gnus-edit-form
2875 (gnus-agent-cat-groups info)
2876 (format "Editing the group list for category %s" category)
2877 `(lambda (groups)
2878 ;; Avoid run-time execution of setf form
2879 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2880 ;; groups)
2881 ;; use its expansion instead:
2882 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2883 groups)
2885 (gnus-category-write)
2886 (gnus-category-list)))))
2888 (defun gnus-category-kill (category)
2889 "Kill the current category."
2890 (interactive (list (gnus-category-name)))
2891 (let ((info (assq category gnus-category-alist))
2892 (buffer-read-only nil))
2893 (gnus-delete-line)
2894 (setq gnus-category-alist (delq info gnus-category-alist))
2895 (gnus-category-write)))
2897 (defun gnus-category-copy (category to)
2898 "Copy the current category."
2899 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2900 (let ((info (assq category gnus-category-alist)))
2901 (push (let ((newcat (gnus-copy-sequence info)))
2902 (setf (gnus-agent-cat-name newcat) to)
2903 (setf (gnus-agent-cat-groups newcat) nil)
2904 newcat)
2905 gnus-category-alist)
2906 (gnus-category-write)
2907 (gnus-category-list)))
2909 (defun gnus-category-add (category)
2910 "Create a new category."
2911 (interactive "SCategory name: ")
2912 (when (assq category gnus-category-alist)
2913 (error "Category %s already exists" category))
2914 (push (gnus-agent-cat-make category)
2915 gnus-category-alist)
2916 (gnus-category-write)
2917 (gnus-category-list))
2919 (defun gnus-category-list ()
2920 "List all categories."
2921 (interactive)
2922 (gnus-category-prepare))
2924 (defun gnus-category-exit ()
2925 "Return to the group buffer."
2926 (interactive)
2927 (kill-buffer (current-buffer))
2928 (gnus-configure-windows 'group t))
2930 ;; To avoid having 8-bit characters in the source file.
2931 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2933 (defvar gnus-category-predicate-alist
2934 '((spam . gnus-agent-spam-p)
2935 (short . gnus-agent-short-p)
2936 (long . gnus-agent-long-p)
2937 (low . gnus-agent-low-scored-p)
2938 (high . gnus-agent-high-scored-p)
2939 (read . gnus-agent-read-p)
2940 (true . gnus-agent-true)
2941 (false . gnus-agent-false))
2942 "Mapping from short score predicate symbols to predicate functions.")
2944 (defun gnus-agent-spam-p ()
2945 "Say whether an article is spam or not."
2946 (unless gnus-agent-spam-hashtb
2947 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2948 (if (not (equal (mail-header-references gnus-headers) ""))
2950 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2951 (prog1
2952 (gnus-gethash string gnus-agent-spam-hashtb)
2953 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2955 (defun gnus-agent-short-p ()
2956 "Say whether an article is short or not."
2957 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2959 (defun gnus-agent-long-p ()
2960 "Say whether an article is long or not."
2961 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2963 (defun gnus-agent-low-scored-p ()
2964 "Say whether an article has a low score or not."
2965 (< gnus-score gnus-agent-low-score))
2967 (defun gnus-agent-high-scored-p ()
2968 "Say whether an article has a high score or not."
2969 (> gnus-score gnus-agent-high-score))
2971 (defun gnus-agent-read-p ()
2972 "Say whether an article is read or not."
2973 (gnus-member-of-range (mail-header-number gnus-headers)
2974 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2976 (defun gnus-category-make-function (predicate)
2977 "Make a function from PREDICATE."
2978 (let ((func (gnus-category-make-function-1 predicate)))
2979 (if (and (= (length func) 1)
2980 (symbolp (car func)))
2981 (car func)
2982 (gnus-byte-compile `(lambda () ,func)))))
2984 (defun gnus-agent-true ()
2985 "Return t."
2988 (defun gnus-agent-false ()
2989 "Return nil."
2990 nil)
2992 (defun gnus-category-make-function-1 (predicate)
2993 "Make a function from PREDICATE."
2994 (cond
2995 ;; Functions are just returned as is.
2996 ((or (symbolp predicate)
2997 (functionp predicate))
2998 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2999 predicate)))
3000 ;; More complex predicate.
3001 ((consp predicate)
3002 `(,(cond
3003 ((memq (car predicate) '(& and))
3004 'and)
3005 ((memq (car predicate) '(| or))
3006 'or)
3007 ((memq (car predicate) gnus-category-not)
3008 'not))
3009 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
3011 (error "Unknown predicate type: %s" predicate))))
3013 (defun gnus-get-predicate (predicate)
3014 "Return the function implementing PREDICATE."
3015 (or (cdr (assoc predicate gnus-category-predicate-cache))
3016 (let ((func (gnus-category-make-function predicate)))
3017 (setq gnus-category-predicate-cache
3018 (nconc gnus-category-predicate-cache
3019 (list (cons predicate func))))
3020 func)))
3022 (defun gnus-predicate-implies-unread (predicate)
3023 "Say whether PREDICATE implies unread articles only.
3024 It is okay to miss some cases, but there must be no false positives.
3025 That is, if this predicate returns true, then indeed the predicate must
3026 return only unread articles."
3027 (eq t (gnus-function-implies-unread-1
3028 (gnus-category-make-function-1 predicate))))
3030 (defun gnus-function-implies-unread-1 (function)
3031 "Recursively evaluate a predicate function to determine whether it can select
3032 any read articles. Returns t if the function is known to never
3033 return read articles, nil when it is known to always return read
3034 articles, and t_nil when the function may return both read and unread
3035 articles."
3036 (let ((func (car function))
3037 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
3038 (cond ((eq func 'and)
3039 (cond ((memq t args) ; if any argument returns only unread articles
3040 ;; then that argument constrains the result to only unread articles.
3042 ((memq 't_nil args) ; if any argument is indeterminate
3043 ;; then the result is indeterminate
3044 't_nil)))
3045 ((eq func 'or)
3046 (cond ((memq nil args) ; if any argument returns read articles
3047 ;; then that argument ensures that the results includes read articles.
3048 nil)
3049 ((memq 't_nil args) ; if any argument is indeterminate
3050 ;; then that argument ensures that the results are indeterminate
3051 't_nil)
3052 (t ; if all arguments return only unread articles
3053 ;; then the result returns only unread articles
3054 t)))
3055 ((eq func 'not)
3056 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
3057 ; then the result is indeterminate
3058 (car args))
3059 (t ; otherwise
3060 ; toggle the result to be the opposite of the argument
3061 (not (car args)))))
3062 ((eq func 'gnus-agent-read-p)
3063 nil) ; The read predicate NEVER returns unread articles
3064 ((eq func 'gnus-agent-false)
3065 t) ; The false predicate returns t as the empty set excludes all read articles
3066 ((eq func 'gnus-agent-true)
3067 nil) ; The true predicate ALWAYS returns read articles
3068 ((catch 'found-match
3069 (let ((alist gnus-category-predicate-alist))
3070 (while alist
3071 (if (eq func (cdar alist))
3072 (throw 'found-match t)
3073 (setq alist (cdr alist))))))
3074 't_nil) ; All other predicates return read and unread articles
3076 (error "Unknown predicate function: %s" function)))))
3078 (defun gnus-group-category (group)
3079 "Return the category GROUP belongs to."
3080 (unless gnus-category-group-cache
3081 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
3082 (let ((cs gnus-category-alist)
3083 groups cat)
3084 (while (setq cat (pop cs))
3085 (setq groups (gnus-agent-cat-groups cat))
3086 (while groups
3087 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
3088 (or (gnus-gethash group gnus-category-group-cache)
3089 (assq 'default gnus-category-alist)))
3091 (defun gnus-agent-expire-group (group &optional articles force)
3092 "Expire all old articles in GROUP.
3093 If you want to force expiring of certain articles, this function can
3094 take ARTICLES, and FORCE parameters as well.
3096 The articles on which the expiration process runs are selected as follows:
3097 if ARTICLES is null, all read and unmarked articles.
3098 if ARTICLES is t, all articles.
3099 if ARTICLES is a list, just those articles.
3100 FORCE is equivalent to setting the expiration predicates to true."
3101 (interactive (list (gnus-agent-read-group)))
3103 (if (not group)
3104 (gnus-agent-expire articles group force)
3105 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
3106 ;; expiration statistics of this single group
3107 (gnus-agent-expire-stats (list 0 0 0.0)))
3108 (if (or (not (eq articles t))
3109 (yes-or-no-p
3110 (concat "Are you sure that you want to "
3111 "expire all articles in " group "? ")))
3112 (let ((gnus-command-method (gnus-find-method-for-group group))
3113 (overview (gnus-get-buffer-create " *expire overview*"))
3114 orig)
3115 (unwind-protect
3116 (let ((active-file (gnus-agent-lib-file "active")))
3117 (when (file-exists-p active-file)
3118 (with-temp-buffer
3119 (nnheader-insert-file-contents active-file)
3120 (gnus-active-to-gnus-format
3121 gnus-command-method
3122 (setq orig (gnus-make-hashtable
3123 (count-lines (point-min) (point-max))))))
3124 (save-excursion
3125 (gnus-agent-expire-group-1
3126 group overview (gnus-gethash-safe group orig)
3127 articles force))))
3128 (kill-buffer overview))))
3129 (gnus-message 4 "%s" (gnus-agent-expire-done-message)))))
3131 (defun gnus-agent-expire-group-1 (group overview active articles force)
3132 ;; Internal function - requires caller to have set
3133 ;; gnus-command-method, initialized overview buffer, and to have
3134 ;; provided a non-nil active
3136 (let ((dir (gnus-agent-group-pathname group))
3137 (file-name-coding-system nnmail-pathname-coding-system)
3138 (decoded (gnus-agent-decoded-group-name group)))
3139 (gnus-agent-with-refreshed-group
3140 group
3141 (when (boundp 'gnus-agent-expire-current-dirs)
3142 (set 'gnus-agent-expire-current-dirs
3143 (cons dir
3144 (symbol-value 'gnus-agent-expire-current-dirs))))
3146 (if (and (not force)
3147 (eq 'DISABLE (gnus-agent-find-parameter group
3148 'agent-enable-expiration)))
3149 (gnus-message 5 "Expiry skipping over %s" decoded)
3150 (gnus-message 5 "Expiring articles in %s" decoded)
3151 (gnus-agent-load-alist group)
3152 (let* ((bytes-freed 0)
3153 (size-files-deleted 0.0)
3154 (files-deleted 0)
3155 (nov-entries-deleted 0)
3156 (info (gnus-get-info group))
3157 (alist gnus-agent-article-alist)
3158 (day (- (time-to-days (current-time))
3159 (gnus-agent-find-parameter group 'agent-days-until-old)))
3160 (specials (if (and alist
3161 (not force))
3162 ;; This could be a bit of a problem. I need to
3163 ;; keep the last article to avoid refetching
3164 ;; headers when using nntp in the backend. At
3165 ;; the same time, if someone uses a backend
3166 ;; that supports article moving then I may have
3167 ;; to remove the last article to complete the
3168 ;; move. Right now, I'm going to assume that
3169 ;; FORCE overrides specials.
3170 (list (caar (last alist)))))
3171 (unreads ;; Articles that are excluded from the
3172 ;; expiration process
3173 (cond (gnus-agent-expire-all
3174 ;; All articles are marked read by global decree
3175 nil)
3176 ((eq articles t)
3177 ;; All articles are marked read by function
3178 ;; parameter
3179 nil)
3180 ((not articles)
3181 ;; Unread articles are marked protected from
3182 ;; expiration Don't call
3183 ;; gnus-list-of-unread-articles as it returns
3184 ;; articles that have not been fetched into the
3185 ;; agent.
3186 (ignore-errors
3187 (gnus-agent-unread-articles group)))
3189 ;; All articles EXCEPT those named by the caller
3190 ;; are protected from expiration
3191 (gnus-sorted-difference
3192 (gnus-uncompress-range
3193 (cons (caar alist)
3194 (caar (last alist))))
3195 (sort articles '<)))))
3196 (marked ;; More articles that are excluded from the
3197 ;; expiration process
3198 (cond (gnus-agent-expire-all
3199 ;; All articles are unmarked by global decree
3200 nil)
3201 ((eq articles t)
3202 ;; All articles are unmarked by function
3203 ;; parameter
3204 nil)
3205 (articles
3206 ;; All articles may as well be unmarked as the
3207 ;; unreads list already names the articles we are
3208 ;; going to keep
3209 nil)
3211 ;; Ticked and/or dormant articles are excluded
3212 ;; from expiration
3213 (nconc
3214 (gnus-uncompress-range
3215 (cdr (assq 'tick (gnus-info-marks info))))
3216 (gnus-uncompress-range
3217 (cdr (assq 'dormant
3218 (gnus-info-marks info))))))))
3219 (nov-file (concat dir ".overview"))
3220 (cnt 0)
3221 (completed -1)
3222 dlist
3223 type)
3225 ;; The normal article alist contains elements that look like
3226 ;; (article# . fetch_date) I need to combine other
3227 ;; information with this list. For example, a flag indicating
3228 ;; that a particular article MUST BE KEPT. To do this, I'm
3229 ;; going to transform the elements to look like (article#
3230 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3231 ;; the process to generate the expired article alist.
3233 ;; Convert the alist elements to (article# fetch_date nil
3234 ;; nil).
3235 (setq dlist (mapcar (lambda (e)
3236 (list (car e) (cdr e) nil nil)) alist))
3238 ;; Convert the keep lists to elements that look like (article#
3239 ;; nil keep_flag nil) then append it to the expanded dlist
3240 ;; These statements are sorted by ascending precedence of the
3241 ;; keep_flag.
3242 (setq dlist (nconc dlist
3243 (mapcar (lambda (e)
3244 (list e nil 'unread nil))
3245 unreads)))
3246 (setq dlist (nconc dlist
3247 (mapcar (lambda (e)
3248 (list e nil 'marked nil))
3249 marked)))
3250 (setq dlist (nconc dlist
3251 (mapcar (lambda (e)
3252 (list e nil 'special nil))
3253 specials)))
3255 (set-buffer overview)
3256 (erase-buffer)
3257 (buffer-disable-undo)
3258 (when (file-exists-p nov-file)
3259 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3260 (nnheader-insert-file-contents nov-file)
3261 (goto-char (point-min))
3263 (let (p)
3264 (while (< (setq p (point)) (point-max))
3265 (condition-case nil
3266 ;; If I successfully read an integer (the plus zero
3267 ;; ensures a numeric type), append the position
3268 ;; to the list
3269 (push (list (+ 0 (read (current-buffer))) nil nil
3271 dlist)
3272 (error
3273 (gnus-message 1 "gnus-agent-expire: read error \
3274 occurred when reading expression at %s in %s. Skipping to next \
3275 line." (point) nov-file)))
3276 ;; Whether I succeeded, or failed, it doesn't matter.
3277 ;; Move to the next line then try again.
3278 (forward-line 1)))
3280 (gnus-message
3281 7 "gnus-agent-expire: Loading overview... Done"))
3282 (set-buffer-modified-p nil)
3284 ;; At this point, all of the information is in dlist. The
3285 ;; only problem is that much of it is spread across multiple
3286 ;; entries. Sort then MERGE!!
3287 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3288 ;; If two entries have the same article-number then sort by
3289 ;; ascending keep_flag.
3290 (let ((special 0)
3291 (marked 1)
3292 (unread 2))
3293 (setq dlist
3294 (sort dlist
3295 (lambda (a b)
3296 (cond ((< (nth 0 a) (nth 0 b))
3298 ((> (nth 0 a) (nth 0 b))
3299 nil)
3301 (let ((a (or (symbol-value (nth 2 a))
3303 (b (or (symbol-value (nth 2 b))
3304 3)))
3305 (<= a b))))))))
3306 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3307 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3308 (let ((dlist dlist))
3309 (while (cdr dlist) ; I'm not at the end-of-list
3310 (if (eq (caar dlist) (caadr dlist))
3311 (let ((first (cdr (car dlist)))
3312 (secnd (cdr (cadr dlist))))
3313 (setcar first (or (car first)
3314 (car secnd))) ; fetch_date
3315 (setq first (cdr first)
3316 secnd (cdr secnd))
3317 (setcar first (or (car first)
3318 (car secnd))) ; Keep_flag
3319 (setq first (cdr first)
3320 secnd (cdr secnd))
3321 (setcar first (or (car first)
3322 (car secnd))) ; NOV_entry_position
3324 (setcdr dlist (cddr dlist)))
3325 (setq dlist (cdr dlist)))))
3327 ;; Check the order of the entry positions. They should be in
3328 ;; ascending order. If they aren't, the positions must be
3329 ;; converted to markers.
3330 (when (catch 'sort-results
3331 (let ((dlist dlist)
3332 (prev-pos -1)
3333 pos)
3334 (while dlist
3335 (if (setq pos (nth 3 (pop dlist)))
3336 (if (< pos prev-pos)
3337 (throw 'sort-results 'unsorted)
3338 (setq prev-pos pos))))))
3339 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3340 (mapc (lambda (entry)
3341 (let ((pos (nth 3 entry)))
3342 (if pos
3343 (setf (nth 3 entry)
3344 (set-marker (make-marker)
3345 pos)))))
3346 dlist))
3348 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3350 (let* ((len (float (length dlist)))
3351 (alist (list nil))
3352 (tail-alist alist)
3353 (position-offset 0)
3356 (while dlist
3357 (let ((new-completed (truncate (* 100.0
3358 (/ (setq cnt (1+ cnt))
3359 len))))
3360 message-log-max)
3361 (when (> new-completed completed)
3362 (setq completed new-completed)
3363 (gnus-message 7 "%3d%% completed..." completed)))
3364 (let* ((entry (car dlist))
3365 (article-number (nth 0 entry))
3366 (fetch-date (nth 1 entry))
3367 (keep (nth 2 entry))
3368 (marker (nth 3 entry)))
3370 (cond
3371 ;; Kept articles are unread, marked, or special.
3372 (keep
3373 (gnus-agent-message 10
3374 "gnus-agent-expire: %s:%d: Kept %s article%s."
3375 decoded article-number keep (if fetch-date " and file" ""))
3376 (when fetch-date
3377 (unless (file-exists-p
3378 (concat dir (number-to-string
3379 article-number)))
3380 (setf (nth 1 entry) nil)
3381 (gnus-agent-message 3 "gnus-agent-expire cleared \
3382 download flag on %s:%d as the cached article file is missing."
3383 decoded (caar dlist)))
3384 (unless marker
3385 (gnus-message 1 "gnus-agent-expire detected a \
3386 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3387 (gnus-agent-append-to-list
3388 tail-alist
3389 (cons article-number fetch-date)))
3391 ;; The following articles are READ, UNMARKED, and
3392 ;; ORDINARY. See if they can be EXPIRED!!!
3393 ((setq type
3394 (cond
3395 ((not (integerp fetch-date))
3396 'read) ;; never fetched article (may expire
3397 ;; right now)
3398 ((not (file-exists-p
3399 (concat dir (number-to-string
3400 article-number))))
3401 (setf (nth 1 entry) nil)
3402 'externally-expired) ;; Can't find the cached
3403 ;; article. Handle case
3404 ;; as though this article
3405 ;; was never fetched.
3407 ;; We now have the arrival day, so we see
3408 ;; whether it's old enough to be expired.
3409 ((< fetch-date day)
3410 'expired)
3411 (force
3412 'forced)))
3414 ;; I found some reason to expire this entry.
3416 (let ((actions nil))
3417 (when (memq type '(forced expired))
3418 (ignore-errors ; Just being paranoid.
3419 (let* ((file-name (nnheader-concat dir (number-to-string
3420 article-number)))
3421 (size (float (nth 7 (file-attributes file-name)))))
3422 (incf bytes-freed size)
3423 (incf size-files-deleted size)
3424 (incf files-deleted)
3425 (delete-file file-name))
3426 (push "expired cached article" actions))
3427 (setf (nth 1 entry) nil)
3430 (when marker
3431 (push "NOV entry removed" actions)
3433 (goto-char (if (markerp marker)
3434 marker
3435 (- marker position-offset)))
3437 (incf nov-entries-deleted)
3439 (let* ((from (point-at-bol))
3440 (to (progn (forward-line 1) (point)))
3441 (freed (- to from)))
3442 (incf bytes-freed freed)
3443 (incf position-offset freed)
3444 (delete-region from to)))
3446 ;; If considering all articles is set, I can only
3447 ;; expire article IDs that are no longer in the
3448 ;; active range (That is, articles that precede the
3449 ;; first article in the new alist).
3450 (if (and gnus-agent-consider-all-articles
3451 (>= article-number (car active)))
3452 ;; I have to keep this ID in the alist
3453 (gnus-agent-append-to-list
3454 tail-alist (cons article-number fetch-date))
3455 (push (format "Removed %s article number from \
3456 article alist" type) actions))
3458 (when actions
3459 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3460 decoded article-number
3461 (mapconcat 'identity actions ", ")))))
3463 (gnus-agent-message
3464 10 "gnus-agent-expire: %s:%d: Article kept as \
3465 expiration tests failed." decoded article-number)
3466 (gnus-agent-append-to-list
3467 tail-alist (cons article-number fetch-date)))
3470 ;; Remove markers as I intend to reuse this buffer again.
3471 (when (and marker
3472 (markerp marker))
3473 (set-marker marker nil))
3475 (setq dlist (cdr dlist))))
3477 (setq alist (cdr alist))
3479 (let ((inhibit-quit t))
3480 (unless (equal alist gnus-agent-article-alist)
3481 (setq gnus-agent-article-alist alist)
3482 (gnus-agent-save-alist group))
3484 (when (buffer-modified-p)
3485 (let ((coding-system-for-write
3486 gnus-agent-file-coding-system))
3487 (gnus-make-directory dir)
3488 (write-region (point-min) (point-max) nov-file nil
3489 'silent)
3490 ;; clear the modified flag as that I'm not confused by
3491 ;; its status on the next pass through this routine.
3492 (set-buffer-modified-p nil)
3493 (gnus-agent-update-view-total-fetched-for group t)))
3495 (when (eq articles t)
3496 (gnus-summary-update-info))))
3498 (when (boundp 'gnus-agent-expire-stats)
3499 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3500 (incf (nth 2 stats) bytes-freed)
3501 (incf (nth 1 stats) files-deleted)
3502 (incf (nth 0 stats) nov-entries-deleted)))
3504 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3506 (defun gnus-agent-expire (&optional articles group force)
3507 "Expire all old articles.
3508 If you want to force expiring of certain articles, this function can
3509 take ARTICLES, GROUP and FORCE parameters as well.
3511 The articles on which the expiration process runs are selected as follows:
3512 if ARTICLES is null, all read and unmarked articles.
3513 if ARTICLES is t, all articles.
3514 if ARTICLES is a list, just those articles.
3515 Setting GROUP will limit expiration to that group.
3516 FORCE is equivalent to setting the expiration predicates to true."
3517 (interactive)
3519 (if group
3520 (gnus-agent-expire-group group articles force)
3521 (if (or (not (eq articles t))
3522 (yes-or-no-p "Are you sure that you want to expire all \
3523 articles in every agentized group? "))
3524 (let ((methods (gnus-agent-covered-methods))
3525 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3526 ;; of agent directories.
3527 (gnus-agent-expire-current-dirs nil)
3528 ;; Bind gnus-agent-expire-stats to enable tracking of
3529 ;; expiration statistics across all groups
3530 (gnus-agent-expire-stats (list 0 0 0.0))
3531 gnus-command-method overview orig)
3532 (setq overview (gnus-get-buffer-create " *expire overview*"))
3533 (unwind-protect
3534 (while (setq gnus-command-method (pop methods))
3535 (let ((active-file (gnus-agent-lib-file "active")))
3536 (when (file-exists-p active-file)
3537 (with-temp-buffer
3538 (nnheader-insert-file-contents active-file)
3539 (gnus-active-to-gnus-format
3540 gnus-command-method
3541 (setq orig (gnus-make-hashtable
3542 (count-lines (point-min) (point-max))))))
3543 (dolist (expiring-group (gnus-groups-from-server
3544 gnus-command-method))
3545 (let* ((active
3546 (gnus-gethash-safe expiring-group orig)))
3548 (when active
3549 (save-excursion
3550 (gnus-agent-expire-group-1
3551 expiring-group overview active articles force))))))))
3552 (kill-buffer overview))
3553 (gnus-agent-expire-unagentized-dirs)
3554 (gnus-message 4 "%s" (gnus-agent-expire-done-message))))))
3556 (defun gnus-agent-expire-done-message ()
3557 (if (and (> gnus-verbose 4)
3558 (boundp 'gnus-agent-expire-stats))
3559 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3560 (size (nth 2 stats))
3561 (units '(B KB MB GB)))
3562 (while (and (> size 1024.0)
3563 (cdr units))
3564 (setq size (/ size 1024.0)
3565 units (cdr units)))
3567 (format "Expiry recovered %d NOV entries, deleted %d files,\
3568 and freed %.f %s."
3569 (nth 0 stats)
3570 (nth 1 stats)
3571 size (car units)))
3572 "Expiry...done"))
3574 (defun gnus-agent-expire-unagentized-dirs ()
3575 (when (and gnus-agent-expire-unagentized-dirs
3576 (boundp 'gnus-agent-expire-current-dirs))
3577 (let* ((keep (gnus-make-hashtable))
3578 ;; Formally bind gnus-agent-expire-current-dirs so that the
3579 ;; compiler will not complain about free references.
3580 (gnus-agent-expire-current-dirs
3581 (symbol-value 'gnus-agent-expire-current-dirs))
3583 (file-name-coding-system nnmail-pathname-coding-system))
3585 (gnus-sethash gnus-agent-directory t keep)
3586 (while gnus-agent-expire-current-dirs
3587 (setq dir (pop gnus-agent-expire-current-dirs))
3588 (when (and (stringp dir)
3589 (file-directory-p dir))
3590 (while (not (gnus-gethash dir keep))
3591 (gnus-sethash dir t keep)
3592 (setq dir (file-name-directory (directory-file-name dir))))))
3594 (let* (to-remove
3595 checker
3596 (checker
3597 (function
3598 (lambda (d)
3599 "Given a directory, check it and its subdirectories for
3600 membership in the keep hash. If it isn't found, add
3601 it to to-remove."
3602 (let ((files (directory-files d))
3603 file)
3604 (while (setq file (pop files))
3605 (cond ((equal file ".") ; Ignore self
3606 nil)
3607 ((equal file "..") ; Ignore parent
3608 nil)
3609 ((equal file ".overview")
3610 ;; Directory must contain .overview to be
3611 ;; agent's cache of a group.
3612 (let ((d (file-name-as-directory d))
3614 ;; Search ancestor's for last directory NOT
3615 ;; found in keep hash.
3616 (while (not (gnus-gethash
3617 (setq d (file-name-directory d)) keep))
3618 (setq r d
3619 d (directory-file-name d)))
3620 ;; if ANY ancestor was NOT in keep hash and
3621 ;; it's not already in to-remove, add it to
3622 ;; to-remove.
3623 (if (and r
3624 (not (member r to-remove)))
3625 (push r to-remove))))
3626 ((file-directory-p (setq file (nnheader-concat d file)))
3627 (funcall checker file)))))))))
3628 (funcall checker (expand-file-name gnus-agent-directory))
3630 (when (and to-remove
3631 (or gnus-expert-user
3632 (gnus-y-or-n-p
3633 "gnus-agent-expire has identified local directories that are\
3634 not currently required by any agentized group. Do you wish to consider\
3635 deleting them?")))
3636 (while to-remove
3637 (let ((dir (pop to-remove)))
3638 (if (or gnus-expert-user
3639 (gnus-y-or-n-p (format "Delete %s? " dir)))
3640 (let* (delete-recursive
3641 files f
3642 (delete-recursive
3643 (function
3644 (lambda (f-or-d)
3645 (ignore-errors
3646 (if (file-directory-p f-or-d)
3647 (condition-case nil
3648 (delete-directory f-or-d)
3649 (file-error
3650 (setq files (directory-files f-or-d))
3651 (while files
3652 (setq f (pop files))
3653 (or (member f '("." ".."))
3654 (funcall delete-recursive
3655 (nnheader-concat
3656 f-or-d f))))
3657 (delete-directory f-or-d)))
3658 (delete-file f-or-d)))))))
3659 (funcall delete-recursive dir))))))))))
3661 ;;;###autoload
3662 (defun gnus-agent-batch ()
3663 "Start Gnus, send queue and fetch session."
3664 (interactive)
3665 (let ((init-file-user "")
3666 (gnus-always-read-dribble-file t))
3667 (gnus))
3668 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3669 (gnus-group-send-queue)
3670 (gnus-agent-fetch-session)))
3672 (defun gnus-agent-unread-articles (group)
3673 (let* ((read (gnus-info-read (gnus-get-info group)))
3674 (known (gnus-agent-load-alist group))
3675 (unread (list nil))
3676 (tail-unread unread))
3677 (while (and known read)
3678 (let ((candidate (car (pop known))))
3679 (while (let* ((range (car read))
3680 (min (if (numberp range) range (car range)))
3681 (max (if (numberp range) range (cdr range))))
3682 (cond ((or (not min)
3683 (< candidate min))
3684 (gnus-agent-append-to-list tail-unread candidate)
3685 nil)
3686 ((> candidate max)
3687 (setq read (cdr read))
3688 ;; return t so that I always loop one more
3689 ;; time. If I just iterated off the end of
3690 ;; read, min will become nil and the current
3691 ;; candidate will be added to the unread list.
3692 t))))))
3693 (while known
3694 (gnus-agent-append-to-list tail-unread (car (pop known))))
3695 (cdr unread)))
3697 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3698 "Restrict ARTICLES to numbers already fetched.
3699 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3700 that have already been fetched.
3701 If CACHED-HEADER is nil, articles are only excluded if the article itself
3702 has been fetched."
3704 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3705 ;; 'car gnus-agent-article-alist))
3707 ;; Functionally, I don't need to construct a temp list using mapcar.
3709 (if (and (or gnus-agent-cache (not gnus-plugged))
3710 (gnus-agent-load-alist group))
3711 (let* ((ref gnus-agent-article-alist)
3712 (arts articles)
3713 (uncached (list nil))
3714 (tail-uncached uncached))
3715 (while (and ref arts)
3716 (let ((v1 (car arts))
3717 (v2 (caar ref)))
3718 (cond ((< v1 v2) ; v1 does not appear in the reference list
3719 (gnus-agent-append-to-list tail-uncached v1)
3720 (setq arts (cdr arts)))
3721 ((= v1 v2)
3722 (unless (or cached-header (cdar ref)) ; v1 is already cached
3723 (gnus-agent-append-to-list tail-uncached v1))
3724 (setq arts (cdr arts))
3725 (setq ref (cdr ref)))
3726 (t ; reference article (v2) precedes the list being filtered
3727 (setq ref (cdr ref))))))
3728 (while arts
3729 (gnus-agent-append-to-list tail-uncached (pop arts)))
3730 (cdr uncached))
3731 ;; if gnus-agent-load-alist fails, no articles are cached.
3732 articles))
3734 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3735 (save-excursion
3736 (gnus-agent-create-buffer)
3737 (let ((gnus-decode-encoded-word-function 'identity)
3738 (gnus-decode-encoded-address-function 'identity)
3739 (file (gnus-agent-article-name ".overview" group))
3740 cached-articles uncached-articles
3741 (file-name-coding-system nnmail-pathname-coding-system))
3742 (gnus-make-directory (nnheader-translate-file-chars
3743 (file-name-directory file) t))
3745 ;; Populate temp buffer with known headers
3746 (when (file-exists-p file)
3747 (with-current-buffer gnus-agent-overview-buffer
3748 (erase-buffer)
3749 (let ((nnheader-file-coding-system
3750 gnus-agent-file-coding-system))
3751 (nnheader-insert-nov-file file (car articles)))))
3753 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3755 (progn
3756 ;; Populate nntp-server-buffer with uncached headers
3757 (set-buffer nntp-server-buffer)
3758 (erase-buffer)
3759 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3760 (gnus-retrieve-headers
3761 uncached-articles group))))
3762 (nnvirtual-convert-headers))
3763 ((eq 'nntp (car gnus-current-select-method))
3764 ;; The author of gnus-get-newsgroup-headers-xover
3765 ;; reports that the XOVER command is commonly
3766 ;; unreliable. The problem is that recently
3767 ;; posted articles may not be entered into the
3768 ;; NOV database in time to respond to my XOVER
3769 ;; query.
3771 ;; I'm going to use his assumption that the NOV
3772 ;; database is updated in order of ascending
3773 ;; article ID. Therefore, a response containing
3774 ;; article ID N implies that all articles from 1
3775 ;; to N-1 are up-to-date. Therefore, missing
3776 ;; articles in that range have expired.
3778 (set-buffer nntp-server-buffer)
3779 (let* ((fetched-articles (list nil))
3780 (tail-fetched-articles fetched-articles)
3781 (min (cond ((numberp fetch-old)
3782 (max 1 (- (car articles) fetch-old)))
3783 (fetch-old
3786 (car articles))))
3787 (max (car (last articles))))
3789 ;; Get the list of articles that were fetched
3790 (goto-char (point-min))
3791 (let ((pm (point-max))
3792 art)
3793 (while (< (point) pm)
3794 (when (setq art (gnus-agent-read-article-number))
3795 (gnus-agent-append-to-list tail-fetched-articles art))
3796 (forward-line 1)))
3798 ;; Clip this list to the headers that will
3799 ;; actually be returned
3800 (setq fetched-articles (gnus-list-range-intersection
3801 (cdr fetched-articles)
3802 (cons min max)))
3804 ;; Clip the uncached articles list to exclude
3805 ;; IDs after the last FETCHED header. The
3806 ;; excluded IDs may be fetchable using HEAD.
3807 (if (car tail-fetched-articles)
3808 (setq uncached-articles
3809 (gnus-list-range-intersection
3810 uncached-articles
3811 (cons (car uncached-articles)
3812 (car tail-fetched-articles)))))
3814 ;; Create the list of articles that were
3815 ;; "successfully" fetched. Success, in this
3816 ;; case, means that the ID should not be
3817 ;; fetched again. In the case of an expired
3818 ;; article, the header will not be fetched.
3819 (setq uncached-articles
3820 (gnus-sorted-nunion fetched-articles
3821 uncached-articles))
3824 ;; Erase the temp buffer
3825 (set-buffer gnus-agent-overview-buffer)
3826 (erase-buffer)
3828 ;; Copy the nntp-server-buffer to the temp buffer
3829 (set-buffer nntp-server-buffer)
3830 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3832 ;; Merge the temp buffer with the known headers (found on
3833 ;; disk in FILE) into the nntp-server-buffer
3834 (when uncached-articles
3835 (gnus-agent-braid-nov group uncached-articles file))
3837 ;; Save the new set of known headers to FILE
3838 (set-buffer nntp-server-buffer)
3839 (let ((coding-system-for-write
3840 gnus-agent-file-coding-system))
3841 (gnus-agent-check-overview-buffer)
3842 (write-region (point-min) (point-max) file nil 'silent))
3844 (gnus-agent-update-view-total-fetched-for group t)
3846 ;; Update the group's article alist to include the newly
3847 ;; fetched articles.
3848 (gnus-agent-load-alist group)
3849 (gnus-agent-save-alist group uncached-articles nil)
3852 ;; Copy the temp buffer to the nntp-server-buffer
3853 (set-buffer nntp-server-buffer)
3854 (erase-buffer)
3855 (insert-buffer-substring gnus-agent-overview-buffer)))
3857 (if (and fetch-old
3858 (not (numberp fetch-old)))
3859 t ; Don't remove anything.
3860 (nnheader-nov-delete-outside-range
3861 (if fetch-old (max 1 (- (car articles) fetch-old))
3862 (car articles))
3863 (car (last articles)))
3866 'nov))
3868 (defun gnus-agent-request-article (article group)
3869 "Retrieve ARTICLE in GROUP from the agent cache."
3870 (when (and gnus-agent
3871 (or gnus-agent-cache
3872 (not gnus-plugged))
3873 (numberp article))
3874 (let* ((gnus-command-method (gnus-find-method-for-group group))
3875 (file (gnus-agent-article-name (number-to-string article) group))
3876 (buffer-read-only nil)
3877 (file-name-coding-system nnmail-pathname-coding-system))
3878 (when (and (file-exists-p file)
3879 (> (nth 7 (file-attributes file)) 0))
3880 (erase-buffer)
3881 (gnus-kill-all-overlays)
3882 (let ((coding-system-for-read gnus-cache-coding-system))
3883 (insert-file-contents file))
3884 t))))
3886 (defun gnus-agent-store-article (article group)
3887 (let* ((gnus-command-method (gnus-find-method-for-group group))
3888 (file (gnus-agent-article-name (number-to-string article) group))
3889 (file-name-coding-system nnmail-pathname-coding-system)
3890 (coding-system-for-write gnus-cache-coding-system))
3891 (when (not (file-exists-p file))
3892 (gnus-make-directory (file-name-directory file))
3893 (write-region (point-min) (point-max) file nil 'silent)
3894 ;; Tell the Agent when the article was fetched, so that it can
3895 ;; be expired later.
3896 (gnus-agent-load-alist group)
3897 (gnus-agent-save-alist group (list article)
3898 (time-to-days (current-time))))))
3900 (defun gnus-agent-regenerate-group (group &optional reread)
3901 "Regenerate GROUP.
3902 If REREAD is t, all articles in the .overview are marked as unread.
3903 If REREAD is a list, the specified articles will be marked as unread.
3904 In addition, their NOV entries in .overview will be refreshed using
3905 the articles' current headers.
3906 If REREAD is not nil, downloaded articles are marked as unread."
3907 (interactive
3908 (list (gnus-agent-read-group)
3909 (catch 'mark
3910 (while (let (c
3911 (cursor-in-echo-area t)
3912 (echo-keystrokes 0))
3913 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3914 (setq c (read-char-exclusive))
3916 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3917 (throw 'mark nil))
3918 ((or (eq c ?a) (eq c ?A))
3919 (throw 'mark t))
3920 ((or (eq c ?d) (eq c ?D))
3921 (throw 'mark 'some)))
3922 (gnus-message 3 "Ignoring unexpected input")
3923 (sit-for 1)
3924 t)))))
3925 (when group
3926 (gnus-message 5 "Regenerating in %s" (gnus-agent-decoded-group-name group))
3927 (let* ((gnus-command-method (or gnus-command-method
3928 (gnus-find-method-for-group group)))
3929 (file (gnus-agent-article-name ".overview" group))
3930 (dir (file-name-directory file))
3931 point
3932 (file-name-coding-system nnmail-pathname-coding-system)
3933 (downloaded (if (file-exists-p dir)
3934 (sort (delq nil (mapcar (lambda (name)
3935 (and (not (file-directory-p (nnheader-concat dir name)))
3936 (string-to-number name)))
3937 (directory-files dir nil "^[0-9]+$" t)))
3939 (progn (gnus-make-directory dir) nil)))
3940 dl nov-arts
3941 alist header
3942 regenerated)
3944 (mm-with-unibyte-buffer
3945 (if (file-exists-p file)
3946 (let ((nnheader-file-coding-system
3947 gnus-agent-file-coding-system))
3948 (nnheader-insert-file-contents file)))
3949 (set-buffer-modified-p nil)
3951 ;; Load the article IDs found in the overview file. As a
3952 ;; side-effect, validate the file contents.
3953 (let ((load t))
3954 (while load
3955 (setq load nil)
3956 (goto-char (point-min))
3957 (while (< (point) (point-max))
3958 (cond ((and (looking-at "[0-9]+\t")
3959 (<= (- (match-end 0) (match-beginning 0)) 9))
3960 (push (read (current-buffer)) nov-arts)
3961 (forward-line 1)
3962 (let ((l1 (car nov-arts))
3963 (l2 (cadr nov-arts)))
3964 (cond ((and (listp reread) (memq l1 reread))
3965 (gnus-delete-line)
3966 (setq nov-arts (cdr nov-arts))
3967 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3968 entry of article %s deleted." l1))
3969 ((not l2)
3970 nil)
3971 ((< l1 l2)
3972 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3973 entries are NOT in ascending order.")
3974 ;; Don't sort now as I haven't verified
3975 ;; that every line begins with a number
3976 (setq load t))
3977 ((= l1 l2)
3978 (forward-line -1)
3979 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3980 entries contained duplicate of article %s. Duplicate deleted." l1)
3981 (gnus-delete-line)
3982 (setq nov-arts (cdr nov-arts))))))
3984 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3985 entries contained line that did not begin with an article number. Deleted\
3986 line.")
3987 (gnus-delete-line))))
3988 (when load
3989 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3990 entries into ascending order.")
3991 (sort-numeric-fields 1 (point-min) (point-max))
3992 (setq nov-arts nil))))
3993 (gnus-agent-check-overview-buffer)
3995 ;; Construct a new article alist whose nodes match every header
3996 ;; in the .overview file. As a side-effect, missing headers are
3997 ;; reconstructed from the downloaded article file.
3998 (while (or downloaded nov-arts)
3999 (cond ((and downloaded
4000 (or (not nov-arts)
4001 (> (car downloaded) (car nov-arts))))
4002 ;; This entry is missing from the overview file
4003 (gnus-message 3 "Regenerating NOV %s %d..."
4004 (gnus-agent-decoded-group-name group)
4005 (car downloaded))
4006 (let ((file (concat dir (number-to-string (car downloaded)))))
4007 (mm-with-unibyte-buffer
4008 (nnheader-insert-file-contents file)
4009 (nnheader-remove-body)
4010 (setq header (nnheader-parse-naked-head)))
4011 (mail-header-set-number header (car downloaded))
4012 (if nov-arts
4013 (let ((key (concat "^" (int-to-string (car nov-arts))
4014 "\t")))
4015 (or (re-search-backward key nil t)
4016 (re-search-forward key))
4017 (forward-line 1))
4018 (goto-char (point-min)))
4019 (nnheader-insert-nov header))
4020 (setq nov-arts (cons (car downloaded) nov-arts)))
4021 ((eq (car downloaded) (car nov-arts))
4022 ;; This entry in the overview has been downloaded
4023 (push (cons (car downloaded)
4024 (time-to-days
4025 (nth 5 (file-attributes
4026 (concat dir (number-to-string
4027 (car downloaded))))))) alist)
4028 (setq downloaded (cdr downloaded))
4029 (setq nov-arts (cdr nov-arts)))
4031 ;; This entry in the overview has not been downloaded
4032 (push (cons (car nov-arts) nil) alist)
4033 (setq nov-arts (cdr nov-arts)))))
4035 ;; When gnus-agent-consider-all-articles is set,
4036 ;; gnus-agent-regenerate-group should NOT remove article IDs from
4037 ;; the alist. Those IDs serve as markers to indicate that an
4038 ;; attempt has been made to fetch that article's header.
4040 ;; When gnus-agent-consider-all-articles is NOT set,
4041 ;; gnus-agent-regenerate-group can remove the article ID of every
4042 ;; article (with the exception of the last ID in the list - it's
4043 ;; special) that no longer appears in the overview. In this
4044 ;; situation, the last article ID in the list implies that it,
4045 ;; and every article ID preceding it, have been fetched from the
4046 ;; server.
4048 (if gnus-agent-consider-all-articles
4049 ;; Restore all article IDs that were not found in the overview file.
4050 (let* ((n (cons nil alist))
4051 (merged n)
4052 (o (gnus-agent-load-alist group)))
4053 (while o
4054 (let ((nID (caadr n))
4055 (oID (caar o)))
4056 (cond ((not nID)
4057 (setq n (setcdr n (list (list oID))))
4058 (setq o (cdr o)))
4059 ((< oID nID)
4060 (setcdr n (cons (list oID) (cdr n)))
4061 (setq o (cdr o)))
4062 ((= oID nID)
4063 (setq o (cdr o))
4064 (setq n (cdr n)))
4066 (setq n (cdr n))))))
4067 (setq alist (cdr merged)))
4068 ;; Restore the last article ID if it is not already in the new alist
4069 (let ((n (last alist))
4070 (o (last (gnus-agent-load-alist group))))
4071 (cond ((not o)
4072 nil)
4073 ((not n)
4074 (push (cons (caar o) nil) alist))
4075 ((< (caar n) (caar o))
4076 (setcdr n (list (car o)))))))
4078 (let ((inhibit-quit t))
4079 (if (setq regenerated (buffer-modified-p))
4080 (let ((coding-system-for-write gnus-agent-file-coding-system))
4081 (write-region (point-min) (point-max) file nil 'silent)))
4083 (setq regenerated (or regenerated
4084 (and reread gnus-agent-article-alist)
4085 (not (equal alist gnus-agent-article-alist))))
4087 (setq gnus-agent-article-alist alist)
4089 (when regenerated
4090 (gnus-agent-save-alist group)
4092 ;; I have to alter the group's active range NOW as
4093 ;; gnus-make-ascending-articles-unread will use it to
4094 ;; recalculate the number of unread articles in the group
4096 (let ((group (gnus-group-real-name group))
4097 (group-active (or (gnus-active group)
4098 (gnus-activate-group group))))
4099 (gnus-agent-possibly-alter-active group group-active)))))
4101 (when (and reread gnus-agent-article-alist)
4102 (gnus-agent-synchronize-group-flags
4103 group
4104 (list (list
4105 (if (listp reread)
4106 reread
4107 (delq nil (mapcar (function (lambda (c)
4108 (cond ((eq reread t)
4109 (car c))
4110 ((cdr c)
4111 (car c)))))
4112 gnus-agent-article-alist)))
4113 'del '(read)))
4114 gnus-command-method)
4116 (when regenerated
4117 (gnus-agent-update-files-total-fetched-for group nil)))
4119 (gnus-message 5 "")
4120 regenerated)))
4122 ;;;###autoload
4123 (defun gnus-agent-regenerate (&optional clean reread)
4124 "Regenerate all agent covered files.
4125 If CLEAN, obsolete (ignore)."
4126 (interactive "P")
4127 (let (regenerated)
4128 (gnus-message 4 "Regenerating Gnus agent files...")
4129 (dolist (gnus-command-method (gnus-agent-covered-methods))
4130 (dolist (group (gnus-groups-from-server gnus-command-method))
4131 (setq regenerated (or (gnus-agent-regenerate-group group reread)
4132 regenerated))))
4133 (gnus-message 4 "Regenerating Gnus agent files...done")
4135 regenerated))
4137 (defun gnus-agent-go-online (&optional force)
4138 "Switch servers into online status."
4139 (interactive (list t))
4140 (dolist (server gnus-opened-servers)
4141 (when (eq (nth 1 server) 'offline)
4142 (if (if (eq force 'ask)
4143 (gnus-y-or-n-p
4144 (format "Switch %s:%s into online status? "
4145 (caar server) (cadar server)))
4146 force)
4147 (setcar (nthcdr 1 server) 'close)))))
4149 (defun gnus-agent-toggle-group-plugged (group)
4150 "Toggle the status of the server of the current group."
4151 (interactive (list (gnus-group-group-name)))
4152 (let* ((method (gnus-find-method-for-group group))
4153 (status (cadr (assoc method gnus-opened-servers))))
4154 (if (eq status 'offline)
4155 (gnus-server-set-status method 'closed)
4156 (gnus-close-server method)
4157 (gnus-server-set-status method 'offline))
4158 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4159 (if (eq status 'offline) 'offline 'online)
4160 (if (eq status 'offline) 'online 'offline))))
4162 (defun gnus-agent-group-covered-p (group)
4163 (gnus-agent-method-p (gnus-group-method group)))
4165 (defun gnus-agent-update-files-total-fetched-for
4166 (group delta &optional method path)
4167 "Update, or set, the total disk space used by the articles that the
4168 agent has fetched."
4169 (when gnus-agent-total-fetched-hashtb
4170 (gnus-agent-with-refreshed-group
4171 group
4172 ;; if null, gnus-agent-group-pathname will calc method.
4173 (let* ((gnus-command-method method)
4174 (path (or path (gnus-agent-group-pathname group)))
4175 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4176 (gnus-sethash path (make-list 3 0)
4177 gnus-agent-total-fetched-hashtb)))
4178 (file-name-coding-system nnmail-pathname-coding-system))
4179 (when (listp delta)
4180 (if delta
4181 (let ((sum 0.0)
4182 file)
4183 (while (setq file (pop delta))
4184 (incf sum (float (or (nth 7 (file-attributes
4185 (nnheader-concat
4186 path
4187 (if (numberp file)
4188 (number-to-string file)
4189 file)))) 0))))
4190 (setq delta sum))
4191 (let ((sum (- (nth 2 entry)))
4192 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4193 file)
4194 (while (setq file (pop info))
4195 (incf sum (float (or (nth 8 file) 0))))
4196 (setq delta sum))))
4198 (setq gnus-agent-need-update-total-fetched-for t)
4199 (incf (nth 2 entry) delta)))))
4201 (defun gnus-agent-update-view-total-fetched-for
4202 (group agent-over &optional method path)
4203 "Update, or set, the total disk space used by the .agentview and
4204 .overview files. These files are calculated separately as they can be
4205 modified."
4206 (when gnus-agent-total-fetched-hashtb
4207 (gnus-agent-with-refreshed-group
4208 group
4209 ;; if null, gnus-agent-group-pathname will calc method.
4210 (let* ((gnus-command-method method)
4211 (path (or path (gnus-agent-group-pathname group)))
4212 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4213 (gnus-sethash path (make-list 3 0)
4214 gnus-agent-total-fetched-hashtb)))
4215 (file-name-coding-system nnmail-pathname-coding-system)
4216 (size (or (nth 7 (file-attributes
4217 (nnheader-concat
4218 path (if agent-over
4219 ".overview"
4220 ".agentview"))))
4221 0)))
4222 (setq gnus-agent-need-update-total-fetched-for t)
4223 (setf (nth (if agent-over 1 0) entry) size)))))
4225 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4226 "Get the total disk space used by the specified GROUP."
4227 (unless (equal group "dummy.group")
4228 (unless gnus-agent-total-fetched-hashtb
4229 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4231 ;; if null, gnus-agent-group-pathname will calc method.
4232 (let* ((gnus-command-method method)
4233 (path (gnus-agent-group-pathname group))
4234 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4235 (if entry
4236 (apply '+ entry)
4237 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4239 (gnus-agent-update-view-total-fetched-for group nil method path)
4240 (gnus-agent-update-view-total-fetched-for group t method path)
4241 (gnus-agent-update-files-total-fetched-for group nil method path)))))))
4243 (provide 'gnus-agent)
4245 ;;; gnus-agent.el ends here