Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / gnus / gnus-agent.el
CommitLineData
df80b09f 1;;; gnus-agent.el --- unplugged support for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
e3fe4da0 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
df80b09f
LMI
5
6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
5a9dffec 11;; the Free Software Foundation; either version 3, or (at your option)
df80b09f
LMI
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
655591a7 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
df80b09f
LMI
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
21;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22;; Boston, MA 02110-1301, USA.
df80b09f
LMI
23
24;;; Commentary:
25
26;;; Code:
27
28(require 'gnus)
29(require 'gnus-cache)
23f87bed 30(require 'nnmail)
df80b09f
LMI
31(require 'nnvirtual)
32(require 'gnus-sum)
03f20b47 33(require 'gnus-score)
23f87bed
MB
34(require 'gnus-srvr)
35(require 'gnus-util)
16409b0b 36(eval-when-compile
03f20b47
DL
37 (if (featurep 'xemacs)
38 (require 'itimer)
39 (require 'timer))
40 (require 'cl))
df80b09f 41
23f87bed
MB
42(eval-and-compile
43 (autoload 'gnus-server-update-server "gnus-srvr")
44 (autoload 'gnus-agent-customize-category "gnus-cus")
45)
46
df80b09f
LMI
47(defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48 "Where the Gnus agent will store its files."
49 :group 'gnus-agent
50 :type 'directory)
51
52(defcustom gnus-agent-plugged-hook nil
53 "Hook run when plugging into the network."
54 :group 'gnus-agent
55 :type 'hook)
56
57(defcustom gnus-agent-unplugged-hook nil
58 "Hook run when unplugging from the network."
59 :group 'gnus-agent
60 :type 'hook)
61
23f87bed
MB
62(defcustom gnus-agent-fetched-hook nil
63 "Hook run when finished fetching articles."
bf247b6e 64 :version "22.1"
23f87bed
MB
65 :group 'gnus-agent
66 :type 'hook)
67
df80b09f
LMI
68(defcustom gnus-agent-handle-level gnus-level-subscribed
69 "Groups on levels higher than this variable will be ignored by the Agent."
70 :group 'gnus-agent
71 :type 'integer)
72
73(defcustom gnus-agent-expire-days 7
23f87bed
MB
74 "Read articles older than this will be expired.
75If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
df80b09f 76 :group 'gnus-agent
23f87bed 77 :type '(number :tag "days"))
df80b09f
LMI
78
79(defcustom gnus-agent-expire-all nil
80 "If non-nil, also expire unread, ticked and dormant articles.
81If nil, only read articles will be expired."
82 :group 'gnus-agent
83 :type 'boolean)
84
85(defcustom gnus-agent-group-mode-hook nil
86 "Hook run in Agent group minor modes."
87 :group 'gnus-agent
88 :type 'hook)
89
23f87bed
MB
90;; Extracted from gnus-xmas-redefine in order to preserve user settings
91(when (featurep 'xemacs)
92 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
93
df80b09f
LMI
94(defcustom gnus-agent-summary-mode-hook nil
95 "Hook run in Agent summary minor modes."
96 :group 'gnus-agent
97 :type 'hook)
98
23f87bed
MB
99;; Extracted from gnus-xmas-redefine in order to preserve user settings
100(when (featurep 'xemacs)
101 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
102
df80b09f
LMI
103(defcustom gnus-agent-server-mode-hook nil
104 "Hook run in Agent summary minor modes."
105 :group 'gnus-agent
106 :type 'hook)
107
23f87bed
MB
108;; Extracted from gnus-xmas-redefine in order to preserve user settings
109(when (featurep 'xemacs)
110 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
111
16409b0b
GM
112(defcustom gnus-agent-confirmation-function 'y-or-n-p
113 "Function to confirm when error happens."
850846fd 114 :version "21.1"
16409b0b
GM
115 :group 'gnus-agent
116 :type 'function)
df80b09f 117
01c52d31 118(defcustom gnus-agent-synchronize-flags nil
03f20b47
DL
119 "Indicate if flags are synchronized when you plug in.
120If this is `ask' the hook will query the user."
5fae1cae
MB
121 ;; If the default switches to something else than nil, then the function
122 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
850846fd 123 :version "21.1"
03f20b47
DL
124 :type '(choice (const :tag "Always" t)
125 (const :tag "Never" nil)
126 (const :tag "Ask" ask))
127 :group 'gnus-agent)
128
23f87bed
MB
129(defcustom gnus-agent-go-online 'ask
130 "Indicate if offline servers go online when you plug in.
131If this is `ask' the hook will query the user."
a08b59c9 132 :version "21.3"
23f87bed
MB
133 :type '(choice (const :tag "Always" t)
134 (const :tag "Never" nil)
135 (const :tag "Ask" ask))
136 :group 'gnus-agent)
137
138(defcustom gnus-agent-mark-unread-after-downloaded t
139 "Indicate whether to mark articles unread after downloaded."
140 :version "21.1"
141 :type 'boolean
142 :group 'gnus-agent)
143
144(defcustom gnus-agent-download-marks '(download)
145 "Marks for downloading."
146 :version "21.1"
147 :type '(repeat (symbol :tag "Mark"))
148 :group 'gnus-agent)
149
150(defcustom gnus-agent-consider-all-articles nil
151 "When non-nil, the agent will let the agent predicate decide
152whether articles need to be downloaded or not, for all articles. When
153nil, the default, the agent will only let the predicate decide
154whether unread articles are downloaded or not. If you enable this,
155groups with large active ranges may open slower and you may also want
156to look into the agent expiry settings to block the expiration of
157read articles as they would just be downloaded again."
bf247b6e 158 :version "22.1"
23f87bed
MB
159 :type 'boolean
160 :group 'gnus-agent)
161
162(defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
163 "Chunk size for `gnus-agent-fetch-session'.
164The function will split its article fetches into chunks smaller than
165this limit."
bf247b6e 166 :version "22.1"
23f87bed
MB
167 :group 'gnus-agent
168 :type 'integer)
169
170(defcustom gnus-agent-enable-expiration 'ENABLE
171 "The default expiration state for each group.
172When set to ENABLE, the default, `gnus-agent-expire' will expire old
173contents from a group's local storage. This value may be overridden
174to disable expiration in specific categories, topics, and groups. Of
175course, you could change gnus-agent-enable-expiration to DISABLE then
176enable expiration per categories, topics, and groups."
bf247b6e 177 :version "22.1"
23f87bed
MB
178 :group 'gnus-agent
179 :type '(radio (const :format "Enable " ENABLE)
180 (const :format "Disable " DISABLE)))
181
182(defcustom gnus-agent-expire-unagentized-dirs t
183 "*Whether expiration should expire in unagentized directories.
184Have gnus-agent-expire scan the directories under
185\(gnus-agent-directory) for groups that are no longer agentized.
186When found, offer to remove them."
bf247b6e 187 :version "22.1"
23f87bed
MB
188 :type 'boolean
189 :group 'gnus-agent)
190
191(defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
192 "Initially, all servers from these methods are agentized.
193The user may remove or add servers using the Server buffer.
194See Info node `(gnus)Server Buffer'."
bf247b6e 195 :version "22.1"
23f87bed
MB
196 :type '(repeat symbol)
197 :group 'gnus-agent)
198
199(defcustom gnus-agent-queue-mail t
200 "Whether and when outgoing mail should be queued by the agent.
201When `always', always queue outgoing mail. When nil, never
202queue. Otherwise, queue if and only if unplugged."
bf247b6e 203 :version "22.1"
23f87bed
MB
204 :group 'gnus-agent
205 :type '(radio (const :format "Always" always)
206 (const :format "Never" nil)
f5e92214 207 (const :format "When unplugged" t)))
23f87bed
MB
208
209(defcustom gnus-agent-prompt-send-queue nil
210 "If non-nil, `gnus-group-send-queue' will prompt if called when
211unplugged."
bf247b6e 212 :version "22.1"
23f87bed
MB
213 :group 'gnus-agent
214 :type 'boolean)
215
58090a8d 216(defcustom gnus-agent-article-alist-save-format 1
3031d8b0
MB
217 "Indicates whether to use compression(2), versus no
218compression(1), when writing agentview files. The compressed
219files do save space but load times are 6-7 times higher. A group
220must be opened then closed for the agentview to be updated using
221the new format."
222 ;; Wouldn't symbols instead numbers be nicer? --rsteib
58090a8d
MB
223 :version "22.1"
224 :group 'gnus-agent
225 :type '(radio (const :format "Compressed" 2)
226 (const :format "Uncompressed" 1)))
227
16409b0b 228;;; Internal variables
df80b09f
LMI
229
230(defvar gnus-agent-history-buffers nil)
231(defvar gnus-agent-buffer-alist nil)
23f87bed 232(defvar gnus-agent-article-alist nil
bf247b6e 233 "An assoc list identifying the articles whose headers have been fetched.
23f87bed
MB
234If successfully fetched, these headers will be stored in the group's overview
235file. The key of each assoc pair is the article ID, the value of each assoc
236pair is a flag indicating whether the identified article has been downloaded
237\(gnus-agent-fetch-articles sets the value to the day of the download).
238NOTES:
bf247b6e 2391) The last element of this list can not be expired as some
23f87bed
MB
240 routines (for example, get-agent-fetch-headers) use the last
241 value to track which articles have had their headers retrieved.
2422) The function `gnus-agent-regenerate' may destructively modify the value.")
df80b09f 243(defvar gnus-agent-group-alist nil)
df80b09f
LMI
244(defvar gnus-category-alist nil)
245(defvar gnus-agent-current-history nil)
246(defvar gnus-agent-overview-buffer nil)
247(defvar gnus-category-predicate-cache nil)
248(defvar gnus-category-group-cache nil)
249(defvar gnus-agent-spam-hashtb nil)
250(defvar gnus-agent-file-name nil)
251(defvar gnus-agent-send-mail-function nil)
16409b0b 252(defvar gnus-agent-file-coding-system 'raw-text)
23f87bed 253(defvar gnus-agent-file-loading-cache nil)
01c52d31
MB
254(defvar gnus-agent-total-fetched-hashtb nil)
255(defvar gnus-agent-inhibit-update-total-fetched-for nil)
256(defvar gnus-agent-need-update-total-fetched-for nil)
16409b0b 257
df80b09f
LMI
258;; Dynamic variables
259(defvar gnus-headers)
260(defvar gnus-score)
261
01c52d31
MB
262;; Added to support XEmacs
263(eval-and-compile
264 (unless (fboundp 'directory-files-and-attributes)
265 (defun directory-files-and-attributes (directory
266 &optional full match nosort)
267 (let (result)
268 (dolist (file (directory-files directory full match nosort))
269 (push (cons file (file-attributes file)) result))
270 (nreverse result)))))
271
df80b09f
LMI
272;;;
273;;; Setup
274;;;
275
276(defun gnus-open-agent ()
277 (setq gnus-agent t)
278 (gnus-agent-read-servers)
279 (gnus-category-read)
16409b0b 280 (gnus-agent-create-buffer)
df80b09f
LMI
281 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
282 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
283 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
284
16409b0b
GM
285(defun gnus-agent-create-buffer ()
286 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
287 t
288 (setq gnus-agent-overview-buffer
289 (gnus-get-buffer-create " *Gnus agent overview*"))
290 (with-current-buffer gnus-agent-overview-buffer
291 (mm-enable-multibyte))
292 nil))
293
df80b09f
LMI
294(gnus-add-shutdown 'gnus-close-agent 'gnus)
295
296(defun gnus-close-agent ()
23f87bed 297 (setq gnus-category-predicate-cache nil
df80b09f
LMI
298 gnus-category-group-cache nil
299 gnus-agent-spam-hashtb nil)
300 (gnus-kill-buffer gnus-agent-overview-buffer))
301
302;;;
303;;; Utility functions
304;;;
305
01c52d31
MB
306(defmacro gnus-agent-with-refreshed-group (group &rest body)
307 "Performs the body then updates the group's line in the group
308buffer. Automatically blocks multiple updates due to recursion."
309`(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
310 (when (and gnus-agent-need-update-total-fetched-for
311 (not gnus-agent-inhibit-update-total-fetched-for))
312 (save-excursion
313 (set-buffer gnus-group-buffer)
314 (setq gnus-agent-need-update-total-fetched-for nil)
315 (gnus-group-update-group ,group t)))))
316
df80b09f
LMI
317(defun gnus-agent-read-file (file)
318 "Load FILE and do a `read' there."
16409b0b 319 (with-temp-buffer
df80b09f
LMI
320 (ignore-errors
321 (nnheader-insert-file-contents file)
322 (goto-char (point-min))
323 (read (current-buffer)))))
324
325(defsubst gnus-agent-method ()
326 (concat (symbol-name (car gnus-command-method)) "/"
327 (if (equal (cadr gnus-command-method) "")
328 "unnamed"
329 (cadr gnus-command-method))))
330
331(defsubst gnus-agent-directory ()
35ef97a5 332 "The name of the Gnus agent directory."
df80b09f
LMI
333 (nnheader-concat gnus-agent-directory
334 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
335
336(defun gnus-agent-lib-file (file)
35ef97a5 337 "The full name of the Gnus agent library FILE."
850846fd
DL
338 (expand-file-name file
339 (file-name-as-directory
340 (expand-file-name "agent.lib" (gnus-agent-directory)))))
df80b09f 341
23f87bed
MB
342(defun gnus-agent-cat-set-property (category property value)
343 (if value
344 (setcdr (or (assq property category)
345 (let ((cell (cons property nil)))
346 (setcdr category (cons cell (cdr category)))
347 cell)) value)
348 (let ((category category))
349 (while (cond ((eq property (caadr category))
350 (setcdr category (cddr category))
351 nil)
352 (t
353 (setq category (cdr category)))))))
354 category)
355
356(eval-when-compile
357 (defmacro gnus-agent-cat-defaccessor (name prop-name)
358 "Define accessor and setter methods for manipulating a list of the form
359\(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
360Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
361manipulated as follows:
362 (func LIST): Returns VALUE1
363 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
364 `(progn (defmacro ,name (category)
365 (list (quote cdr) (list (quote assq)
366 (quote (quote ,prop-name)) category)))
367
368 (define-setf-method ,name (category)
369 (let* ((--category--temp-- (make-symbol "--category--"))
370 (--value--temp-- (make-symbol "--value--")))
371 (list (list --category--temp--) ; temporary-variables
01c52d31
MB
372 (list category) ; value-forms
373 (list --value--temp--) ; store-variables
23f87bed
MB
374 (let* ((category --category--temp--) ; store-form
375 (value --value--temp--))
376 (list (quote gnus-agent-cat-set-property)
377 category
378 (quote (quote ,prop-name))
379 value))
380 (list (quote ,name) --category--temp--) ; access-form
381 )))))
382 )
383
384(defmacro gnus-agent-cat-name (category)
385 `(car ,category))
386
387(gnus-agent-cat-defaccessor
388 gnus-agent-cat-days-until-old agent-days-until-old)
389(gnus-agent-cat-defaccessor
390 gnus-agent-cat-enable-expiration agent-enable-expiration)
391(gnus-agent-cat-defaccessor
392 gnus-agent-cat-groups agent-groups)
393(gnus-agent-cat-defaccessor
394 gnus-agent-cat-high-score agent-high-score)
395(gnus-agent-cat-defaccessor
58090a8d 396 gnus-agent-cat-length-when-long agent-long-article)
23f87bed 397(gnus-agent-cat-defaccessor
58090a8d 398 gnus-agent-cat-length-when-short agent-short-article)
23f87bed
MB
399(gnus-agent-cat-defaccessor
400 gnus-agent-cat-low-score agent-low-score)
401(gnus-agent-cat-defaccessor
402 gnus-agent-cat-predicate agent-predicate)
403(gnus-agent-cat-defaccessor
58090a8d 404 gnus-agent-cat-score-file agent-score)
23f87bed 405(gnus-agent-cat-defaccessor
58090a8d 406 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
23f87bed 407
54506618
MB
408
409;; This form is equivalent to defsetf except that it calls make-symbol
410;; whereas defsetf calls gensym (Using gensym creates a run-time
411;; dependency on the CL library).
412
23f87bed 413(eval-and-compile
54506618
MB
414 (define-setf-method gnus-agent-cat-groups (category)
415 (let* ((--category--temp-- (make-symbol "--category--"))
416 (--groups--temp-- (make-symbol "--groups--")))
417 (list (list --category--temp--)
418 (list category)
419 (list --groups--temp--)
420 (let* ((category --category--temp--)
421 (groups --groups--temp--))
422 (list (quote gnus-agent-set-cat-groups) category groups))
423 (list (quote gnus-agent-cat-groups) --category--temp--))))
424 )
23f87bed
MB
425
426(defun gnus-agent-set-cat-groups (category groups)
427 (unless (eq groups 'ignore)
428 (let ((new-g groups)
429 (old-g (gnus-agent-cat-groups category)))
430 (cond ((eq new-g old-g)
431 ;; gnus-agent-add-group is fiddling with the group
432 ;; list. Still, Im done.
433 nil
434 )
435 ((eq new-g (cdr old-g))
436 ;; gnus-agent-add-group is fiddling with the group list
437 (setcdr (or (assq 'agent-groups category)
438 (let ((cell (cons 'agent-groups nil)))
439 (setcdr category (cons cell (cdr category)))
440 cell)) new-g))
441 (t
442 (let ((groups groups))
443 (while groups
444 (let* ((group (pop groups))
445 (old-category (gnus-group-category group)))
446 (if (eq category old-category)
447 nil
448 (setf (gnus-agent-cat-groups old-category)
449 (delete group (gnus-agent-cat-groups
450 old-category))))))
451 ;; Purge cache as preceeding loop invalidated it.
452 (setq gnus-category-group-cache nil))
453
454 (setcdr (or (assq 'agent-groups category)
455 (let ((cell (cons 'agent-groups nil)))
456 (setcdr category (cons cell (cdr category)))
457 cell)) groups))))))
458
459(defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
460 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
461
01c52d31
MB
462(defun gnus-agent-read-group ()
463 "Read a group name in the minibuffer, with completion."
464 (let ((def (or (gnus-group-group-name) gnus-newsgroup-name)))
465 (when def
466 (setq def (gnus-group-decoded-name def)))
467 (gnus-group-completing-read (if def
468 (concat "Group Name (" def "): ")
469 "Group Name: ")
470 nil nil t nil nil def)))
471
df80b09f
LMI
472;;; Fetching setup functions.
473
474(defun gnus-agent-start-fetch ()
475 "Initialize data structures for efficient fetching."
16409b0b 476 (gnus-agent-create-buffer))
df80b09f
LMI
477
478(defun gnus-agent-stop-fetch ()
479 "Save all data structures and clean up."
df80b09f
LMI
480 (setq gnus-agent-spam-hashtb nil)
481 (save-excursion
482 (set-buffer nntp-server-buffer)
483 (widen)))
484
485(defmacro gnus-agent-with-fetch (&rest forms)
486 "Do FORMS safely."
487 `(unwind-protect
03f20b47 488 (let ((gnus-agent-fetching t))
df80b09f
LMI
489 (gnus-agent-start-fetch)
490 ,@forms)
491 (gnus-agent-stop-fetch)))
492
493(put 'gnus-agent-with-fetch 'lisp-indent-function 0)
494(put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
495
23f87bed
MB
496(defmacro gnus-agent-append-to-list (tail value)
497 `(setq ,tail (setcdr ,tail (cons ,value nil))))
498
499(defmacro gnus-agent-message (level &rest args)
500 `(if (<= ,level gnus-verbose)
501 (message ,@args)))
502
df80b09f
LMI
503;;;
504;;; Mode infestation
505;;;
506
507(defvar gnus-agent-mode-hook nil
508 "Hook run when installing agent mode.")
509
510(defvar gnus-agent-mode nil)
511(defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
512
513(defun gnus-agent-mode ()
514 "Minor mode for providing a agent support in Gnus buffers."
515 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
516 (symbol-name major-mode))
517 (match-string 1 (symbol-name major-mode))))
518 (mode (intern (format "gnus-agent-%s-mode" buffer))))
519 (set (make-local-variable 'gnus-agent-mode) t)
520 (set mode nil)
521 (set (make-local-variable mode) t)
522 ;; Set up the menu.
523 (when (gnus-visual-p 'agent-menu 'menu)
524 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
525 (unless (assq 'gnus-agent-mode minor-mode-alist)
526 (push gnus-agent-mode-status minor-mode-alist))
527 (unless (assq mode minor-mode-map-alist)
528 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
529 buffer))))
530 minor-mode-map-alist))
531 (when (eq major-mode 'gnus-group-mode)
23f87bed
MB
532 (let ((init-plugged gnus-plugged)
533 (gnus-agent-go-online nil))
534 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
535 ;; Therefore, make certain that the current value does not
536 ;; match the desired initial value.
537 (setq gnus-plugged :unknown)
538 (gnus-agent-toggle-plugged init-plugged)))
df80b09f
LMI
539 (gnus-run-hooks 'gnus-agent-mode-hook
540 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
541
542(defvar gnus-agent-group-mode-map (make-sparse-keymap))
543(gnus-define-keys gnus-agent-group-mode-map
544 "Ju" gnus-agent-fetch-groups
545 "Jc" gnus-enter-category-buffer
546 "Jj" gnus-agent-toggle-plugged
547 "Js" gnus-agent-fetch-session
03f20b47 548 "JY" gnus-agent-synchronize-flags
23f87bed 549 "JS" gnus-group-send-queue
16409b0b 550 "Ja" gnus-agent-add-group
23f87bed
MB
551 "Jr" gnus-agent-remove-group
552 "Jo" gnus-agent-toggle-group-plugged)
df80b09f
LMI
553
554(defun gnus-agent-group-make-menu-bar ()
555 (unless (boundp 'gnus-agent-group-menu)
556 (easy-menu-define
557 gnus-agent-group-menu gnus-agent-group-mode-map ""
558 '("Agent"
559 ["Toggle plugged" gnus-agent-toggle-plugged t]
23f87bed 560 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
df80b09f 561 ["List categories" gnus-enter-category-buffer t]
23f87bed
MB
562 ["Add (current) group to category" gnus-agent-add-group t]
563 ["Remove (current) group from category" gnus-agent-remove-group t]
564 ["Send queue" gnus-group-send-queue gnus-plugged]
df80b09f
LMI
565 ("Fetch"
566 ["All" gnus-agent-fetch-session gnus-plugged]
23f87bed
MB
567 ["Group" gnus-agent-fetch-group gnus-plugged])
568 ["Synchronize flags" gnus-agent-synchronize-flags t]
569 ))))
df80b09f
LMI
570
571(defvar gnus-agent-summary-mode-map (make-sparse-keymap))
572(gnus-define-keys gnus-agent-summary-mode-map
573 "Jj" gnus-agent-toggle-plugged
23f87bed
MB
574 "Ju" gnus-agent-summary-fetch-group
575 "JS" gnus-agent-fetch-group
576 "Js" gnus-agent-summary-fetch-series
df80b09f
LMI
577 "J#" gnus-agent-mark-article
578 "J\M-#" gnus-agent-unmark-article
579 "@" gnus-agent-toggle-mark
580 "Jc" gnus-agent-catchup)
581
582(defun gnus-agent-summary-make-menu-bar ()
583 (unless (boundp 'gnus-agent-summary-menu)
584 (easy-menu-define
585 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
586 '("Agent"
587 ["Toggle plugged" gnus-agent-toggle-plugged t]
588 ["Mark as downloadable" gnus-agent-mark-article t]
589 ["Unmark as downloadable" gnus-agent-unmark-article t]
590 ["Toggle mark" gnus-agent-toggle-mark t]
23f87bed 591 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
df80b09f
LMI
592 ["Catchup undownloaded" gnus-agent-catchup t]))))
593
594(defvar gnus-agent-server-mode-map (make-sparse-keymap))
595(gnus-define-keys gnus-agent-server-mode-map
596 "Jj" gnus-agent-toggle-plugged
597 "Ja" gnus-agent-add-server
598 "Jr" gnus-agent-remove-server)
599
600(defun gnus-agent-server-make-menu-bar ()
601 (unless (boundp 'gnus-agent-server-menu)
602 (easy-menu-define
603 gnus-agent-server-menu gnus-agent-server-mode-map ""
604 '("Agent"
605 ["Toggle plugged" gnus-agent-toggle-plugged t]
606 ["Add" gnus-agent-add-server t]
607 ["Remove" gnus-agent-remove-server t]))))
608
23f87bed
MB
609(defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
610 (if (and (fboundp 'propertize)
611 (fboundp 'make-mode-line-mouse-map))
612 (propertize string 'local-map
27a0c0bc 613 (make-mode-line-mouse-map mouse-button mouse-func)
b90a6149
MB
614 'mouse-face
615 (cond ((and (featurep 'xemacs)
616 ;; XEmacs' `facep' only checks for a face
617 ;; object, not for a face name, so it's useless
618 ;; to check with `facep'.
619 (find-face 'modeline))
620 'modeline)
621 ((facep 'mode-line-highlight) ;; Emacs 22
622 'mode-line-highlight)
623 ((facep 'mode-line) ;; Emacs 21
624 'mode-line)) )
23f87bed
MB
625 string))
626
627(defun gnus-agent-toggle-plugged (set-to)
df80b09f
LMI
628 "Toggle whether Gnus is unplugged or not."
629 (interactive (list (not gnus-plugged)))
23f87bed
MB
630 (cond ((eq set-to gnus-plugged)
631 nil)
632 (set-to
633 (setq gnus-plugged set-to)
634 (gnus-run-hooks 'gnus-agent-plugged-hook)
635 (setcar (cdr gnus-agent-mode-status)
636 (gnus-agent-make-mode-line-string " Plugged"
637 'mouse-2
638 'gnus-agent-toggle-plugged))
b890d447 639 (gnus-agent-go-online gnus-agent-go-online))
23f87bed
MB
640 (t
641 (gnus-agent-close-connections)
642 (setq gnus-plugged set-to)
643 (gnus-run-hooks 'gnus-agent-unplugged-hook)
644 (setcar (cdr gnus-agent-mode-status)
645 (gnus-agent-make-mode-line-string " Unplugged"
646 'mouse-2
647 'gnus-agent-toggle-plugged))))
df80b09f
LMI
648 (set-buffer-modified-p t))
649
23f87bed
MB
650(defmacro gnus-agent-while-plugged (&rest body)
651 `(let ((original-gnus-plugged gnus-plugged))
652 (unwind-protect
653 (progn (gnus-agent-toggle-plugged t)
654 ,@body)
655 (gnus-agent-toggle-plugged original-gnus-plugged))))
656
657(put 'gnus-agent-while-plugged 'lisp-indent-function 0)
658(put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
659
df80b09f
LMI
660(defun gnus-agent-close-connections ()
661 "Close all methods covered by the Gnus agent."
23f87bed 662 (let ((methods (gnus-agent-covered-methods)))
df80b09f
LMI
663 (while methods
664 (gnus-close-server (pop methods)))))
665
666;;;###autoload
667(defun gnus-unplugged ()
668 "Start Gnus unplugged."
669 (interactive)
670 (setq gnus-plugged nil)
671 (gnus))
672
673;;;###autoload
674(defun gnus-plugged ()
675 "Start Gnus plugged."
676 (interactive)
677 (setq gnus-plugged t)
678 (gnus))
679
23f87bed
MB
680;;;###autoload
681(defun gnus-slave-unplugged (&optional arg)
682 "Read news as a slave unplugged."
683 (interactive "P")
684 (setq gnus-plugged nil)
685 (gnus arg nil 'slave))
686
df80b09f
LMI
687;;;###autoload
688(defun gnus-agentize ()
689 "Allow Gnus to be an offline newsreader.
df80b09f 690
23f87bed
MB
691The gnus-agentize function is now called internally by gnus when
692gnus-agent is set. If you wish to avoid calling gnus-agentize,
693customize gnus-agent to nil.
df80b09f 694
23f87bed
MB
695This will modify the `gnus-setup-news-hook', and
696`message-send-mail-real-function' variables, and install the Gnus agent
697minor mode in all Gnus buffers."
df80b09f
LMI
698 (interactive)
699 (gnus-open-agent)
700 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
701 (unless gnus-agent-send-mail-function
23f87bed
MB
702 (setq gnus-agent-send-mail-function
703 (or message-send-mail-real-function
54506618 704 (function (lambda () (funcall message-send-mail-function))))
23f87bed
MB
705 message-send-mail-real-function 'gnus-agent-send-mail))
706
707 ;; If the servers file doesn't exist, auto-agentize some servers and
708 ;; save the servers file so this auto-agentizing isn't invoked
709 ;; again.
710 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
711 (gnus-message 3 "First time agent user, agentizing remote groups...")
712 (mapc
713 (lambda (server-or-method)
714 (let ((method (gnus-server-to-method server-or-method)))
715 (when (memq (car method)
716 gnus-agent-auto-agentize-methods)
717 (push (gnus-method-to-server method)
718 gnus-agent-covered-methods)
719 (setq gnus-agent-method-p-cache nil))))
720 (cons gnus-select-method gnus-secondary-select-methods))
721 (gnus-agent-write-servers)))
722
723(defun gnus-agent-queue-setup (&optional group-name)
724 "Make sure the queue group exists.
725Optional arg GROUP-NAME allows to specify another group."
726 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
727 gnus-newsrc-hashtb)
728 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
df80b09f 729 (let ((gnus-level-default-subscribed 1))
23f87bed
MB
730 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
731 nil '(nndraft "")))
df80b09f 732 (gnus-group-set-parameter
23f87bed
MB
733 (format "nndraft:%s" (or group-name "queue"))
734 'gnus-dummy '((gnus-draft-mode)))))
df80b09f
LMI
735
736(defun gnus-agent-send-mail ()
23f87bed
MB
737 (if (or (not gnus-agent-queue-mail)
738 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
df80b09f
LMI
739 (funcall gnus-agent-send-mail-function)
740 (goto-char (point-min))
741 (re-search-forward
742 (concat "^" (regexp-quote mail-header-separator) "\n"))
743 (replace-match "\n")
744 (gnus-agent-insert-meta-information 'mail)
16409b0b 745 (gnus-request-accept-article "nndraft:queue" nil t t)))
df80b09f
LMI
746
747(defun gnus-agent-insert-meta-information (type &optional method)
748 "Insert meta-information into the message that says how it's to be posted.
23f87bed 749TYPE can be either `mail' or `news'. If the latter, then METHOD can
df80b09f
LMI
750be a select method."
751 (save-excursion
752 (message-remove-header gnus-agent-meta-information-header)
753 (goto-char (point-min))
754 (insert gnus-agent-meta-information-header ": "
755 (symbol-name type) " " (format "%S" method)
756 "\n")
757 (forward-char -1)
758 (while (search-backward "\n" nil t)
759 (replace-match "\\n" t t))))
760
03f20b47
DL
761(defun gnus-agent-restore-gcc ()
762 "Restore GCC field from saved header."
763 (save-excursion
764 (goto-char (point-min))
23f87bed
MB
765 (while (re-search-forward
766 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
03f20b47
DL
767 (replace-match "Gcc:" 'fixedcase))))
768
769(defun gnus-agent-any-covered-gcc ()
770 (save-restriction
771 (message-narrow-to-headers)
772 (let* ((gcc (mail-fetch-field "gcc" nil t))
a1506d29 773 (methods (and gcc
03f20b47
DL
774 (mapcar 'gnus-inews-group-method
775 (message-unquote-tokens
a1506d29 776 (message-tokenize-header
03f20b47
DL
777 gcc " ,")))))
778 covered)
779 (while (and (not covered) methods)
23f87bed 780 (setq covered (gnus-agent-method-p (car methods))
03f20b47
DL
781 methods (cdr methods)))
782 covered)))
783
23f87bed 784;;;###autoload
03f20b47
DL
785(defun gnus-agent-possibly-save-gcc ()
786 "Save GCC if Gnus is unplugged."
787 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
788 (save-excursion
789 (goto-char (point-min))
790 (let ((case-fold-search t))
791 (while (re-search-forward "^gcc:" nil t)
792 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
793
794(defun gnus-agent-possibly-do-gcc ()
795 "Do GCC if Gnus is plugged."
796 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
797 (gnus-inews-do-gcc)))
798
df80b09f
LMI
799;;;
800;;; Group mode commands
801;;;
802
803(defun gnus-agent-fetch-groups (n)
804 "Put all new articles in the current groups into the Agent."
805 (interactive "P")
16409b0b
GM
806 (unless gnus-plugged
807 (error "Groups can't be fetched when Gnus is unplugged"))
df80b09f
LMI
808 (gnus-group-iterate n 'gnus-agent-fetch-group))
809
23f87bed 810(defun gnus-agent-fetch-group (&optional group)
df80b09f
LMI
811 "Put all new articles in GROUP into the Agent."
812 (interactive (list (gnus-group-group-name)))
23f87bed 813 (setq group (or group gnus-newsgroup-name))
df80b09f
LMI
814 (unless group
815 (error "No group on the current line"))
23f87bed
MB
816
817 (gnus-agent-while-plugged
818 (let ((gnus-command-method (gnus-find-method-for-group group)))
819 (gnus-agent-with-fetch
820 (gnus-agent-fetch-group-1 group gnus-command-method)
821 (gnus-message 5 "Fetching %s...done" group)))))
df80b09f
LMI
822
823(defun gnus-agent-add-group (category arg)
824 "Add the current group to an agent category."
825 (interactive
826 (list
827 (intern
828 (completing-read
829 "Add to category: "
830 (mapcar (lambda (cat) (list (symbol-name (car cat))))
831 gnus-category-alist)
832 nil t))
833 current-prefix-arg))
834 (let ((cat (assq category gnus-category-alist))
835 c groups)
836 (gnus-group-iterate arg
837 (lambda (group)
23f87bed
MB
838 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
839 (setf (gnus-agent-cat-groups c)
840 (delete group (gnus-agent-cat-groups c))))
df80b09f 841 (push group groups)))
23f87bed
MB
842 (setf (gnus-agent-cat-groups cat)
843 (nconc (gnus-agent-cat-groups cat) groups))
df80b09f
LMI
844 (gnus-category-write)))
845
16409b0b
GM
846(defun gnus-agent-remove-group (arg)
847 "Remove the current group from its agent category, if any."
848 (interactive "P")
849 (let (c)
850 (gnus-group-iterate arg
851 (lambda (group)
23f87bed
MB
852 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
853 (setf (gnus-agent-cat-groups c)
854 (delete group (gnus-agent-cat-groups c))))))
16409b0b
GM
855 (gnus-category-write)))
856
03f20b47
DL
857(defun gnus-agent-synchronize-flags ()
858 "Synchronize unplugged flags with servers."
859 (interactive)
860 (save-excursion
23f87bed 861 (dolist (gnus-command-method (gnus-agent-covered-methods))
03f20b47
DL
862 (when (file-exists-p (gnus-agent-lib-file "flags"))
863 (gnus-agent-synchronize-flags-server gnus-command-method)))))
864
865(defun gnus-agent-possibly-synchronize-flags ()
866 "Synchronize flags according to `gnus-agent-synchronize-flags'."
16409b0b
GM
867 (interactive)
868 (save-excursion
23f87bed 869 (dolist (gnus-command-method (gnus-agent-covered-methods))
b890d447 870 (when (eq (gnus-server-status gnus-command-method) 'ok)
03f20b47
DL
871 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
872
873(defun gnus-agent-synchronize-flags-server (method)
874 "Synchronize flags set when unplugged for server."
54506618
MB
875 (let ((gnus-command-method method)
876 (gnus-agent nil))
03f20b47
DL
877 (when (file-exists-p (gnus-agent-lib-file "flags"))
878 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
879 (erase-buffer)
880 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
54506618 881 (cond ((null gnus-plugged)
bf247b6e
KS
882 (gnus-message
883 1 "You must be plugged to synchronize flags with server %s"
54506618
MB
884 (nth 1 gnus-command-method)))
885 ((null (gnus-check-server gnus-command-method))
bf247b6e 886 (gnus-message
54506618
MB
887 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
888 (t
889 (condition-case err
890 (while t
891 (let ((bgn (point)))
892 (eval (read (current-buffer)))
893 (delete-region bgn (point))))
894 (end-of-file
895 (delete-file (gnus-agent-lib-file "flags")))
896 (error
897 (let ((file (gnus-agent-lib-file "flags")))
898 (write-region (point-min) (point-max)
899 (gnus-agent-lib-file "flags") nil 'silent)
900 (error "Couldn't set flags from file %s due to %s"
901 file (error-message-string err)))))))
03f20b47
DL
902 (kill-buffer nil))))
903
904(defun gnus-agent-possibly-synchronize-flags-server (method)
905 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
b890d447
MB
906 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
907 (or (and gnus-agent-synchronize-flags
908 (not (eq gnus-agent-synchronize-flags 'ask)))
909 (and (eq gnus-agent-synchronize-flags 'ask)
910 (gnus-y-or-n-p
911 (format "Synchronize flags on server `%s'? "
912 (cadr method))))))
03f20b47 913 (gnus-agent-synchronize-flags-server method)))
16409b0b 914
54506618
MB
915;;;###autoload
916(defun gnus-agent-rename-group (old-group new-group)
58090a8d
MB
917 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
918Always updates the agent, even when disabled, as the old agent
919files would corrupt gnus when the agent was next enabled.
920Depends upon the caller to determine whether group renaming is
921supported."
54506618
MB
922 (let* ((old-command-method (gnus-find-method-for-group old-group))
923 (old-path (directory-file-name
924 (let (gnus-command-method old-command-method)
925 (gnus-agent-group-pathname old-group))))
926 (new-command-method (gnus-find-method-for-group new-group))
927 (new-path (directory-file-name
928 (let (gnus-command-method new-command-method)
01c52d31
MB
929 (gnus-agent-group-pathname new-group))))
930 (file-name-coding-system nnmail-pathname-coding-system))
54506618
MB
931 (gnus-rename-file old-path new-path t)
932
933 (let* ((old-real-group (gnus-group-real-name old-group))
934 (new-real-group (gnus-group-real-name new-group))
935 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
936 (gnus-agent-save-group-info old-command-method old-real-group nil)
937 (gnus-agent-save-group-info new-command-method new-real-group old-active)
938
bf247b6e 939 (let ((old-local (gnus-agent-get-local old-group
54506618
MB
940 old-real-group old-command-method)))
941 (gnus-agent-set-local old-group
942 nil nil
943 old-real-group old-command-method)
944 (gnus-agent-set-local new-group
945 (car old-local) (cdr old-local)
946 new-real-group new-command-method)))))
947
948;;;###autoload
949(defun gnus-agent-delete-group (group)
58090a8d
MB
950 "Delete fully-qualified GROUP.
951Always updates the agent, even when disabled, as the old agent
952files would corrupt gnus when the agent was next enabled.
953Depends upon the caller to determine whether group deletion is
954supported."
54506618
MB
955 (let* ((command-method (gnus-find-method-for-group group))
956 (path (directory-file-name
957 (let (gnus-command-method command-method)
01c52d31
MB
958 (gnus-agent-group-pathname group))))
959 (file-name-coding-system nnmail-pathname-coding-system))
aa0a8561 960 (gnus-delete-directory path)
54506618
MB
961
962 (let* ((real-group (gnus-group-real-name group)))
963 (gnus-agent-save-group-info command-method real-group nil)
964
bf247b6e 965 (let ((local (gnus-agent-get-local group
54506618
MB
966 real-group command-method)))
967 (gnus-agent-set-local group
968 nil nil
969 real-group command-method)))))
970
df80b09f
LMI
971;;;
972;;; Server mode commands
973;;;
974
23f87bed 975(defun gnus-agent-add-server ()
df80b09f 976 "Enroll SERVER in the agent program."
23f87bed
MB
977 (interactive)
978 (let* ((server (gnus-server-server-name))
979 (named-server (gnus-server-named-server))
980 (method (and server
981 (gnus-server-get-method nil server))))
982 (unless server
983 (error "No server on the current line"))
984
985 (when (gnus-agent-method-p method)
df80b09f 986 (error "Server already in the agent program"))
23f87bed
MB
987
988 (push named-server gnus-agent-covered-methods)
989
990 (setq gnus-agent-method-p-cache nil)
991 (gnus-server-update-server server)
df80b09f 992 (gnus-agent-write-servers)
23f87bed 993 (gnus-message 1 "Entered %s into the Agent" server)))
df80b09f 994
23f87bed 995(defun gnus-agent-remove-server ()
df80b09f 996 "Remove SERVER from the agent program."
23f87bed
MB
997 (interactive)
998 (let* ((server (gnus-server-server-name))
999 (named-server (gnus-server-named-server)))
1000 (unless server
1001 (error "No server on the current line"))
1002
1003 (unless (member named-server gnus-agent-covered-methods)
df80b09f 1004 (error "Server not in the agent program"))
23f87bed 1005
bf247b6e 1006 (setq gnus-agent-covered-methods
23f87bed
MB
1007 (delete named-server gnus-agent-covered-methods)
1008 gnus-agent-method-p-cache nil)
1009
1010 (gnus-server-update-server server)
df80b09f 1011 (gnus-agent-write-servers)
23f87bed 1012 (gnus-message 1 "Removed %s from the agent" server)))
df80b09f
LMI
1013
1014(defun gnus-agent-read-servers ()
1015 "Read the alist of covered servers."
bf247b6e 1016 (setq gnus-agent-covered-methods
23f87bed
MB
1017 (gnus-agent-read-file
1018 (nnheader-concat gnus-agent-directory "lib/servers"))
1019 gnus-agent-method-p-cache nil)
1020
1021 ;; I am called so early in start-up that I can not validate server
1022 ;; names. When that is the case, I skip the validation. That is
1023 ;; alright as the gnus startup code calls the validate methods
1024 ;; directly.
1025 (if gnus-server-alist
1026 (gnus-agent-read-servers-validate)))
1027
1028(defun gnus-agent-read-servers-validate ()
1029 (mapcar (lambda (server-or-method)
1030 (let* ((server (if (stringp server-or-method)
1031 server-or-method
1032 (gnus-method-to-server server-or-method)))
1033 (method (gnus-server-to-method server)))
1034 (if method
1035 (unless (member server gnus-agent-covered-methods)
1036 (push server gnus-agent-covered-methods)
1037 (setq gnus-agent-method-p-cache nil))
1038 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1039 (prog1 gnus-agent-covered-methods
1040 (setq gnus-agent-covered-methods nil))))
1041
1042(defun gnus-agent-read-servers-validate-native (native-method)
df80b09f 1043 (setq gnus-agent-covered-methods
23f87bed
MB
1044 (mapcar (lambda (method)
1045 (if (or (not method)
1046 (equal method native-method))
1047 "native"
1048 method)) gnus-agent-covered-methods)))
df80b09f
LMI
1049
1050(defun gnus-agent-write-servers ()
1051 "Write the alist of covered servers."
16409b0b
GM
1052 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1053 (let ((coding-system-for-write nnheader-file-coding-system)
1054 (file-name-coding-system nnmail-pathname-coding-system))
1055 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
23f87bed
MB
1056 (prin1 gnus-agent-covered-methods
1057 (current-buffer)))))
df80b09f
LMI
1058
1059;;;
1060;;; Summary commands
1061;;;
1062
1063(defun gnus-agent-mark-article (n &optional unmark)
1064 "Mark the next N articles as downloadable.
1065If N is negative, mark backward instead. If UNMARK is non-nil, remove
1066the mark instead. The difference between N and the actual number of
1067articles marked is returned."
1068 (interactive "p")
1069 (let ((backward (< n 0))
1070 (n (abs n)))
1071 (while (and
1072 (> n 0)
1073 (progn
1074 (gnus-summary-set-agent-mark
1075 (gnus-summary-article-number) unmark)
1076 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1077 (setq n (1- n)))
1078 (when (/= 0 n)
1079 (gnus-message 7 "No more articles"))
1080 (gnus-summary-recenter)
1081 (gnus-summary-position-point)
1082 n))
1083
1084(defun gnus-agent-unmark-article (n)
1085 "Remove the downloadable mark from the next N articles.
1086If N is negative, unmark backward instead. The difference between N and
1087the actual number of articles unmarked is returned."
1088 (interactive "p")
1089 (gnus-agent-mark-article n t))
1090
1091(defun gnus-agent-toggle-mark (n)
1092 "Toggle the downloadable mark from the next N articles.
1093If N is negative, toggle backward instead. The difference between N and
1094the actual number of articles toggled is returned."
1095 (interactive "p")
1096 (gnus-agent-mark-article n 'toggle))
1097
1098(defun gnus-summary-set-agent-mark (article &optional unmark)
23f87bed
MB
1099 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1100When UNMARK is t, the article is unmarked. For any other value, the
1101article's mark is toggled."
1102 (let ((unmark (cond ((eq nil unmark)
1103 nil)
1104 ((eq t unmark)
1105 t)
1106 (t
1107 (memq article gnus-newsgroup-downloadable)))))
1108 (when (gnus-summary-goto-subject article nil t)
1109 (gnus-summary-update-mark
1110 (if unmark
1111 (progn
1112 (setq gnus-newsgroup-downloadable
1113 (delq article gnus-newsgroup-downloadable))
1114 (gnus-article-mark article))
1115 (setq gnus-newsgroup-downloadable
1116 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1117 gnus-downloadable-mark)
1118 'unread))))
df80b09f 1119
54506618 1120;;;###autoload
df80b09f 1121(defun gnus-agent-get-undownloaded-list ()
23f87bed 1122 "Construct list of articles that have not been downloaded."
df80b09f 1123 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
23f87bed
MB
1124 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1125 (gnus-agent-method-p gnus-command-method))
1126 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1127 (headers (sort (mapcar (lambda (h)
1128 (mail-header-number h))
1129 gnus-newsgroup-headers) '<))
1130 (cached (and gnus-use-cache gnus-newsgroup-cached))
1131 (undownloaded (list nil))
1132 (tail-undownloaded undownloaded)
1133 (unfetched (list nil))
1134 (tail-unfetched unfetched))
1135 (while (and alist headers)
1136 (let ((a (caar alist))
1137 (h (car headers)))
1138 (cond ((< a h)
1139 ;; Ignore IDs in the alist that are not being
1140 ;; displayed in the summary.
1141 (setq alist (cdr alist)))
1142 ((> a h)
1143 ;; Headers that are not in the alist should be
1144 ;; fictious (see nnagent-retrieve-headers); they
1145 ;; imply that this article isn't in the agent.
1146 (gnus-agent-append-to-list tail-undownloaded h)
1147 (gnus-agent-append-to-list tail-unfetched h)
bf247b6e 1148 (setq headers (cdr headers)))
23f87bed
MB
1149 ((cdar alist)
1150 (setq alist (cdr alist))
1151 (setq headers (cdr headers))
1152 nil ; ignore already downloaded
1153 )
1154 (t
1155 (setq alist (cdr alist))
1156 (setq headers (cdr headers))
bf247b6e 1157
23f87bed
MB
1158 ;; This article isn't in the agent. Check to see
1159 ;; if it is in the cache. If it is, it's been
1160 ;; downloaded.
1161 (while (and cached (< (car cached) a))
1162 (setq cached (cdr cached)))
1163 (unless (equal a (car cached))
1164 (gnus-agent-append-to-list tail-undownloaded a))))))
1165
1166 (while headers
1167 (let ((num (pop headers)))
1168 (gnus-agent-append-to-list tail-undownloaded num)
1169 (gnus-agent-append-to-list tail-unfetched num)))
1170
1171 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1172 gnus-newsgroup-unfetched (cdr unfetched))))))
df80b09f
LMI
1173
1174(defun gnus-agent-catchup ()
23f87bed
MB
1175 "Mark as read all unhandled articles.
1176An article is unhandled if it is neither cached, nor downloaded, nor
1177downloadable."
df80b09f
LMI
1178 (interactive)
1179 (save-excursion
23f87bed
MB
1180 (let ((articles gnus-newsgroup-undownloaded))
1181 (when (or gnus-newsgroup-downloadable
1182 gnus-newsgroup-cached)
1183 (setq articles (gnus-sorted-ndifference
1184 (gnus-sorted-ndifference
1185 (gnus-copy-sequence articles)
1186 gnus-newsgroup-downloadable)
1187 gnus-newsgroup-cached)))
1188
1189 (while articles
1190 (gnus-summary-mark-article
1191 (pop articles) gnus-catchup-mark)))
1192 (gnus-summary-position-point)))
1193
1194(defun gnus-agent-summary-fetch-series ()
1195 (interactive)
1196 (when gnus-newsgroup-processable
1197 (setq gnus-newsgroup-downloadable
1198 (let* ((dl gnus-newsgroup-downloadable)
58090a8d
MB
1199 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1200 (gnus-newsgroup-downloadable processable))
1201 (gnus-agent-summary-fetch-group)
23f87bed 1202
58090a8d
MB
1203 ;; For each article that I processed that is no longer
1204 ;; undownloaded, remove its processable mark.
1205
655591a7 1206 (mapc #'gnus-summary-remove-process-mark
58090a8d
MB
1207 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1208
1209 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1210 ;; updated the temporary gnus-newsgroup-downloadable to
1211 ;; remove each article successfully fetched. Now, I
1212 ;; update the real gnus-newsgroup-downloadable to only
1213 ;; include undownloaded articles.
1214 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
23f87bed
MB
1215
1216(defun gnus-agent-summary-fetch-group (&optional all)
1217 "Fetch the downloadable articles in the group.
1218Optional arg ALL, if non-nil, means to fetch all articles."
1219 (interactive "P")
1220 (let ((articles
1221 (if all gnus-newsgroup-articles
1222 gnus-newsgroup-downloadable))
1223 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1224 fetched-articles)
1225 (gnus-agent-while-plugged
1226 (unless articles
1227 (error "No articles to download"))
1228 (gnus-agent-with-fetch
1229 (setq gnus-newsgroup-undownloaded
1230 (gnus-sorted-ndifference
1231 gnus-newsgroup-undownloaded
1232 (setq fetched-articles
1233 (gnus-agent-fetch-articles
1234 gnus-newsgroup-name articles)))))
1235 (save-excursion
1236 (dolist (article articles)
bf247b6e 1237 (let ((was-marked-downloadable
23f87bed
MB
1238 (memq article gnus-newsgroup-downloadable)))
1239 (cond (gnus-agent-mark-unread-after-downloaded
1240 (setq gnus-newsgroup-downloadable
1241 (delq article gnus-newsgroup-downloadable))
1242
1243 (gnus-summary-mark-article article gnus-unread-mark))
1244 (was-marked-downloadable
1245 (gnus-summary-set-agent-mark article t)))
1246 (when (gnus-summary-goto-subject article nil t)
1247 (gnus-summary-update-download-mark article))))))
1248 fetched-articles))
1249
1250(defun gnus-agent-fetch-selected-article ()
1251 "Fetch the current article as it is selected.
1252This can be added to `gnus-select-article-hook' or
1253`gnus-mark-article-hook'."
1254 (let ((gnus-command-method gnus-current-select-method))
1255 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1256 (when (gnus-agent-fetch-articles
1257 gnus-newsgroup-name
1258 (list gnus-current-article))
1259 (setq gnus-newsgroup-undownloaded
1260 (delq gnus-current-article gnus-newsgroup-undownloaded))
1261 (gnus-summary-update-download-mark gnus-current-article)))))
df80b09f
LMI
1262
1263;;;
1264;;; Internal functions
1265;;;
1266
54506618
MB
1267(defun gnus-agent-synchronize-group-flags (group actions server)
1268"Update a plugged group by performing the indicated actions."
1269 (let* ((gnus-command-method (gnus-server-to-method server))
1270 (info
1271 ;; This initializer is required as gnus-request-set-mark
1272 ;; calls gnus-group-real-name to strip off the host name
1273 ;; before calling the backend. Now that the backend is
1274 ;; trying to call gnus-request-set-mark, I have to
1275 ;; reconstruct the original group name.
1276 (or (gnus-get-info group)
bf247b6e
KS
1277 (gnus-get-info
1278 (setq group (gnus-group-full-name
54506618
MB
1279 group gnus-command-method))))))
1280 (gnus-request-set-mark group actions)
1281
1282 (when info
1283 (dolist (action actions)
1284 (let ((range (nth 0 action))
1285 (what (nth 1 action))
1286 (marks (nth 2 action)))
1287 (dolist (mark marks)
1288 (cond ((eq mark 'read)
bf247b6e 1289 (gnus-info-set-read
54506618
MB
1290 info
1291 (funcall (if (eq what 'add)
1292 'gnus-range-add
1293 'gnus-remove-from-range)
1294 (gnus-info-read info)
1295 range))
bf247b6e 1296 (gnus-get-unread-articles-in-group
54506618
MB
1297 info
1298 (gnus-active (gnus-info-group info))))
1299 ((memq mark '(tick))
1300 (let ((info-marks (assoc mark (gnus-info-marks info))))
1301 (unless info-marks
1302 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1303 (setcdr info-marks (funcall (if (eq what 'add)
1304 'gnus-range-add
1305 'gnus-remove-from-range)
1306 (cdr info-marks)
58090a8d
MB
1307 range))))))))
1308
1309 ;;Marks can be synchronized at any time by simply toggling from
1310 ;;unplugged to plugged. If that is what is happening right now, make
1311 ;;sure that the group buffer is up to date.
1312 (when (gnus-buffer-live-p gnus-group-buffer)
1313 (gnus-group-update-group group t)))
54506618
MB
1314 nil))
1315
df80b09f
LMI
1316(defun gnus-agent-save-active (method)
1317 (when (gnus-agent-method-p method)
1318 (let* ((gnus-command-method method)
16409b0b 1319 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
df80b09f 1320 (file (gnus-agent-lib-file "active")))
23f87bed 1321 (gnus-active-to-gnus-format nil new)
16409b0b
GM
1322 (gnus-agent-write-active file new)
1323 (erase-buffer)
01c52d31
MB
1324 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
1325 (nnheader-insert-file-contents file)))))
16409b0b
GM
1326
1327(defun gnus-agent-write-active (file new)
df80b09f 1328 (gnus-make-directory (file-name-directory file))
23f87bed
MB
1329 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1330 ;; The hashtable contains real names of groups. However, do NOT
1331 ;; add the foreign server prefix as gnus-active-to-gnus-format
1332 ;; will add it while reading the file.
1333 (gnus-write-active-file file new nil)))
1334
54506618 1335;;;###autoload
23f87bed
MB
1336(defun gnus-agent-possibly-alter-active (group active &optional info)
1337 "Possibly expand a group's active range to include articles
1338downloaded into the agent."
1339 (let* ((gnus-command-method (or gnus-command-method
1340 (gnus-find-method-for-group group))))
1341 (when (gnus-agent-method-p gnus-command-method)
1342 (let* ((local (gnus-agent-get-local group))
1343 (active-min (or (car active) 0))
1344 (active-max (or (cdr active) 0))
1345 (agent-min (or (car local) active-min))
1346 (agent-max (or (cdr local) active-max)))
1347
1348 (when (< agent-min active-min)
1349 (setcar active agent-min))
1350
1351 (when (> agent-max active-max)
1352 (setcdr active agent-max))
1353
1354 (when (and info (< agent-max (- active-min 100)))
1355 ;; I'm expanding the active range by such a large amount
1356 ;; that there is a gap of more than 100 articles between the
1357 ;; last article known to the agent and the first article
1358 ;; currently available on the server. This gap contains
1359 ;; articles that have been lost, mark them as read so that
1360 ;; gnus doesn't waste resources trying to fetch them.
1361
1362 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1363 ;; want to modify the local file everytime someone restarts
1364 ;; gnus. The small gap will cause a tiny performance hit
1365 ;; when gnus tries, and fails, to retrieve the articles.
1366 ;; Still that should be smaller than opening a buffer,
1367 ;; printing this list to the buffer, and then writing it to a
1368 ;; file.
1369
1370 (let ((read (gnus-info-read info)))
bf247b6e
KS
1371 (gnus-info-set-read
1372 info
1373 (gnus-range-add
1374 read
1375 (list (cons (1+ agent-max)
23f87bed
MB
1376 (1- active-min))))))
1377
1378 ;; Lie about the agent's local range for this group to
1379 ;; disable the set read each time this server is opened.
1380 ;; NOTE: Opening this group will restore the valid local
1381 ;; range but it will also expand the local range to
1382 ;; incompass the new active range.
1383 (gnus-agent-set-local group agent-min (1- active-min)))))))
df80b09f
LMI
1384
1385(defun gnus-agent-save-group-info (method group active)
23f87bed 1386 "Update a single group's active range in the agent's copy of the server's active file."
df80b09f 1387 (when (gnus-agent-method-p method)
54506618 1388 (let* ((gnus-command-method (or method gnus-command-method))
16409b0b
GM
1389 (coding-system-for-write nnheader-file-coding-system)
1390 (file-name-coding-system nnmail-pathname-coding-system)
1391 (file (gnus-agent-lib-file "active"))
23f87bed 1392 oactive-min oactive-max)
df80b09f 1393 (gnus-make-directory (file-name-directory file))
16409b0b
GM
1394 (with-temp-file file
1395 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
a1506d29 1396 (mm-disable-multibyte)
df80b09f 1397 (when (file-exists-p file)
23f87bed
MB
1398 (nnheader-insert-file-contents file)
1399
1400 (goto-char (point-min))
1401 (when (re-search-forward
1402 (concat "^" (regexp-quote group) " ") nil t)
1403 (save-excursion
58090a8d 1404 (setq oactive-max (read (current-buffer)) ;; max
23f87bed
MB
1405 oactive-min (read (current-buffer)))) ;; min
1406 (gnus-delete-line)))
54506618
MB
1407 (when active
1408 (insert (format "%S %d %d y\n" (intern group)
1409 (max (or oactive-max (cdr active)) (cdr active))
1410 (min (or oactive-min (car active)) (car active))))
1411 (goto-char (point-max))
1412 (while (search-backward "\\." nil t)
1413 (delete-char 1)))))))
1414
1415(defun gnus-agent-get-group-info (method group)
1416 "Get a single group's active range in the agent's copy of the server's active file."
1417 (when (gnus-agent-method-p method)
1418 (let* ((gnus-command-method (or method gnus-command-method))
1419 (coding-system-for-write nnheader-file-coding-system)
1420 (file-name-coding-system nnmail-pathname-coding-system)
1421 (file (gnus-agent-lib-file "active"))
1422 oactive-min oactive-max)
1423 (gnus-make-directory (file-name-directory file))
1424 (with-temp-buffer
1425 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1426 (mm-disable-multibyte)
1427 (when (file-exists-p file)
1428 (nnheader-insert-file-contents file)
1429
1430 (goto-char (point-min))
1431 (when (re-search-forward
1432 (concat "^" (regexp-quote group) " ") nil t)
1433 (save-excursion
1434 (setq oactive-max (read (current-buffer)) ;; max
1435 oactive-min (read (current-buffer))) ;; min
1436 (cons oactive-min oactive-max))))))))
df80b09f 1437
01c52d31
MB
1438(defvar gnus-agent-decoded-group-names nil
1439 "Alist of non-ASCII group names and decoded ones.")
1440
1441(defun gnus-agent-decoded-group-name (group)
1442 "Return a decoded group name of GROUP."
1443 (or (cdr (assoc group gnus-agent-decoded-group-names))
1444 (if (string-match "[^\000-\177]" group)
1445 (let ((decoded (gnus-group-decoded-name group)))
1446 (push (cons group decoded) gnus-agent-decoded-group-names)
1447 decoded)
1448 group)))
1449
df80b09f 1450(defun gnus-agent-group-path (group)
35ef97a5 1451 "Translate GROUP into a file name."
23f87bed
MB
1452
1453 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1454 ;; The two methods must be kept synchronized, which is why
1455 ;; gnus-agent-group-pathname was added.
1456
1457 (setq group
1458 (nnheader-translate-file-chars
1459 (nnheader-replace-duplicate-chars-in-string
bf247b6e 1460 (nnheader-replace-chars-in-string
01c52d31 1461 (gnus-group-real-name (gnus-agent-decoded-group-name group))
23f87bed
MB
1462 ?/ ?_)
1463 ?. ?_)))
1464 (if (or nnmail-use-long-file-names
1465 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1466 group
01c52d31 1467 (nnheader-replace-chars-in-string group ?. ?/)))
23f87bed
MB
1468
1469(defun gnus-agent-group-pathname (group)
1470 "Translate GROUP into a file name."
1471 ;; nnagent uses nnmail-group-pathname to read articles while
1472 ;; unplugged. The agent must, therefore, use the same directory
1473 ;; while plugged.
01c52d31
MB
1474 (nnmail-group-pathname
1475 (gnus-group-real-name (gnus-agent-decoded-group-name group))
1476 (if gnus-command-method
1477 (gnus-agent-directory)
1478 (let ((gnus-command-method (gnus-find-method-for-group group)))
1479 (gnus-agent-directory)))))
df80b09f
LMI
1480
1481(defun gnus-agent-get-function (method)
23f87bed
MB
1482 (if (gnus-online method)
1483 (car method)
1484 (require 'nnagent)
1485 'nnagent))
1486
1487(defun gnus-agent-covered-methods ()
1488 "Return the subset of methods that are covered by the agent."
1489 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
df80b09f
LMI
1490
1491;;; History functions
1492
1493(defun gnus-agent-history-buffer ()
1494 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1495
1496(defun gnus-agent-open-history ()
1497 (save-excursion
1498 (push (cons (gnus-agent-method)
1499 (set-buffer (gnus-get-buffer-create
1500 (format " *Gnus agent %s history*"
1501 (gnus-agent-method)))))
1502 gnus-agent-history-buffers)
16409b0b 1503 (mm-disable-multibyte) ;; everything is binary
df80b09f
LMI
1504 (erase-buffer)
1505 (insert "\n")
1506 (let ((file (gnus-agent-lib-file "history")))
1507 (when (file-exists-p file)
16409b0b 1508 (nnheader-insert-file-contents file))
df80b09f
LMI
1509 (set (make-local-variable 'gnus-agent-file-name) file))))
1510
df80b09f
LMI
1511(defun gnus-agent-close-history ()
1512 (when (gnus-buffer-live-p gnus-agent-current-history)
1513 (kill-buffer gnus-agent-current-history)
1514 (setq gnus-agent-history-buffers
1515 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1516 gnus-agent-history-buffers))))
1517
df80b09f
LMI
1518;;;
1519;;; Fetching
1520;;;
1521
1522(defun gnus-agent-fetch-articles (group articles)
1523 "Fetch ARTICLES from GROUP and put them into the Agent."
1524 (when articles
23f87bed
MB
1525 (gnus-agent-load-alist group)
1526 (let* ((alist gnus-agent-article-alist)
1527 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1528 (selected-sets (list nil))
1529 (current-set-size 0)
1530 article
1531 header-number)
1532 ;; Check each article
1533 (while (setq article (pop articles))
1534 ;; Skip alist entries preceeding this article
1535 (while (> article (or (caar alist) (1+ article)))
1536 (setq alist (cdr alist)))
1537
1538 ;; Prune off articles that we have already fetched.
1539 (unless (and (eq article (caar alist))
1540 (cdar alist))
1541 ;; Skip headers preceeding this article
bf247b6e 1542 (while (> article
23f87bed
MB
1543 (setq header-number
1544 (let* ((header (car headers)))
1545 (if header
1546 (mail-header-number header)
1547 (1+ article)))))
1548 (setq headers (cdr headers)))
1549
1550 ;; Add this article to the current set
1551 (setcar selected-sets (cons article (car selected-sets)))
1552
1553 ;; Update the set size, when the set is too large start a
1554 ;; new one. I do this after adding the article as I want at
1555 ;; least one article in each set.
1556 (when (< gnus-agent-max-fetch-size
1557 (setq current-set-size
1558 (+ current-set-size
1559 (if (= header-number article)
1560 (let ((char-size (mail-header-chars
1561 (car headers))))
1562 (if (<= char-size 0)
1563 ;; The char size was missing/invalid,
1564 ;; assume a worst-case situation of
1565 ;; 65 char/line. If the line count
1566 ;; is missing, arbitrarily assume a
1567 ;; size of 1000 characters.
1568 (max (* 65 (mail-header-lines
1569 (car headers)))
1570 1000)
1571 char-size))
1572 0))))
1573 (setcar selected-sets (nreverse (car selected-sets)))
1574 (setq selected-sets (cons nil selected-sets)
1575 current-set-size 0))))
1576
1577 (when (or (cdr selected-sets) (car selected-sets))
1578 (let* ((fetched-articles (list nil))
1579 (tail-fetched-articles fetched-articles)
1580 (dir (gnus-agent-group-pathname group))
1581 (date (time-to-days (current-time)))
1582 (case-fold-search t)
01c52d31
MB
1583 pos crosses id
1584 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
1585
1586 (setcar selected-sets (nreverse (car selected-sets)))
1587 (setq selected-sets (nreverse selected-sets))
1588
1589 (gnus-make-directory dir)
1590 (gnus-message 7 "Fetching articles for %s..." group)
1591
1592 (unwind-protect
1593 (while (setq articles (pop selected-sets))
1594 ;; Fetch the articles from the backend.
1595 (if (gnus-check-backend-function 'retrieve-articles group)
1596 (setq pos (gnus-retrieve-articles articles group))
1597 (with-temp-buffer
1598 (let (article)
1599 (while (setq article (pop articles))
1600 (gnus-message 10 "Fetching article %s for %s..."
1601 article group)
1602 (when (or
1603 (gnus-backlog-request-article group article
1604 nntp-server-buffer)
1605 (gnus-request-article article group))
1606 (goto-char (point-max))
1607 (push (cons article (point)) pos)
1608 (insert-buffer-substring nntp-server-buffer)))
1609 (copy-to-buffer
1610 nntp-server-buffer (point-min) (point-max))
1611 (setq pos (nreverse pos)))))
1612 ;; Then save these articles into the Agent.
1613 (save-excursion
1614 (set-buffer nntp-server-buffer)
1615 (while pos
1616 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1617 (goto-char (point-min))
1618 (unless (eobp) ;; Don't save empty articles.
1619 (when (search-forward "\n\n" nil t)
1620 (when (search-backward "\nXrefs: " nil t)
1621 ;; Handle cross posting.
1622 (goto-char (match-end 0)) ; move to end of header name
1623 (skip-chars-forward "^ ") ; skip server name
1624 (skip-chars-forward " ")
1625 (setq crosses nil)
1626 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1627 (push (cons (buffer-substring (match-beginning 1)
1628 (match-end 1))
e9bd5782 1629 (string-to-number
23f87bed
MB
1630 (buffer-substring (match-beginning 2)
1631 (match-end 2))))
1632 crosses)
1633 (goto-char (match-end 0)))
1634 (gnus-agent-crosspost crosses (caar pos) date)))
1635 (goto-char (point-min))
1636 (if (not (re-search-forward
1637 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1638 (setq id "No-Message-ID-in-article")
1639 (setq id (buffer-substring
1640 (match-beginning 1) (match-end 1))))
1641 (let ((coding-system-for-write
1642 gnus-agent-file-coding-system))
1643 (write-region (point-min) (point-max)
1644 (concat dir (number-to-string (caar pos)))
1645 nil 'silent))
1646
1647 (gnus-agent-append-to-list
1648 tail-fetched-articles (caar pos)))
1649 (widen)
1650 (setq pos (cdr pos)))))
1651
1652 (gnus-agent-save-alist group (cdr fetched-articles) date)
01c52d31
MB
1653 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1654
23f87bed
MB
1655 (gnus-message 7 ""))
1656 (cdr fetched-articles))))))
1657
54506618
MB
1658(defun gnus-agent-unfetch-articles (group articles)
1659 "Delete ARTICLES that were fetched from GROUP into the agent."
1660 (when articles
01c52d31
MB
1661 (gnus-agent-with-refreshed-group
1662 group
1663 (gnus-agent-load-alist group)
1664 (let* ((alist (cons nil gnus-agent-article-alist))
1665 (articles (sort articles #'<))
1666 (next-possibility alist)
1667 (delete-this (pop articles)))
1668 (while (and (cdr next-possibility) delete-this)
1669 (let ((have-this (caar (cdr next-possibility))))
1670 (cond
1671 ((< delete-this have-this)
1672 (setq delete-this (pop articles)))
1673 ((= delete-this have-this)
1674 (let ((timestamp (cdar (cdr next-possibility))))
1675 (when timestamp
1676 (let* ((file-name (concat (gnus-agent-group-pathname group)
1677 (number-to-string have-this)))
1678 (size-file
1679 (float (or (and gnus-agent-total-fetched-hashtb
1680 (nth 7 (file-attributes file-name)))
1681 0)))
1682 (file-name-coding-system
1683 nnmail-pathname-coding-system))
1684 (delete-file file-name)
1685 (gnus-agent-update-files-total-fetched-for
1686 group (- size-file)))))
1687
1688 (setcdr next-possibility (cddr next-possibility)))
1689 (t
1690 (setq next-possibility (cdr next-possibility))))))
1691 (setq gnus-agent-article-alist (cdr alist))
1692 (gnus-agent-save-alist group)))))
54506618 1693
23f87bed
MB
1694(defun gnus-agent-crosspost (crosses article &optional date)
1695 (setq date (or date t))
1696
df80b09f
LMI
1697 (let (gnus-agent-article-alist group alist beg end)
1698 (save-excursion
1699 (set-buffer gnus-agent-overview-buffer)
1700 (when (nnheader-find-nov-line article)
1701 (forward-word 1)
1702 (setq beg (point))
1703 (setq end (progn (forward-line 1) (point)))))
1704 (while crosses
1705 (setq group (caar crosses))
1706 (unless (setq alist (assoc group gnus-agent-group-alist))
1707 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1708 gnus-agent-group-alist))
23f87bed 1709 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
df80b09f
LMI
1710 (save-excursion
1711 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
16409b0b 1712 group)))
df80b09f
LMI
1713 (when (= (point-max) (point-min))
1714 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1715 (ignore-errors
01c52d31
MB
1716 (let ((file-name-coding-system nnmail-pathname-coding-system))
1717 (nnheader-insert-file-contents
1718 (gnus-agent-article-name ".overview" group)))))
df80b09f
LMI
1719 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1720 (insert (string-to-number (cdar crosses)))
23f87bed
MB
1721 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1722 (gnus-agent-check-overview-buffer))
1723 (setq crosses (cdr crosses)))))
1724
1725(defun gnus-agent-backup-overview-buffer ()
1726 (when gnus-newsgroup-name
1727 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1728 (cnt 0)
01c52d31
MB
1729 name
1730 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
1731 (while (file-exists-p
1732 (setq name (concat root "~"
1733 (int-to-string (setq cnt (1+ cnt))) "~"))))
1734 (write-region (point-min) (point-max) name nil 'no-msg)
1735 (gnus-message 1 "Created backup copy of overview in %s." name)))
1736 t)
1737
1738(defun gnus-agent-check-overview-buffer (&optional buffer)
1739 "Check the overview file given for sanity.
1740In particular, checks that the file is sorted by article number
1741and that there are no duplicates."
1742 (let ((prev-num -1)
1743 (backed-up nil))
1744 (save-excursion
1745 (when buffer
1746 (set-buffer buffer))
1747 (save-restriction
1748 (widen)
1749 (goto-char (point-min))
1750
1751 (while (< (point) (point-max))
1752 (let ((p (point))
1753 (cur (condition-case nil
1754 (read (current-buffer))
1755 (error nil))))
1756 (cond
1757 ((or (not (integerp cur))
1758 (not (eq (char-after) ?\t)))
1759 (or backed-up
1760 (setq backed-up (gnus-agent-backup-overview-buffer)))
1761 (gnus-message 1
1762 "Overview buffer contains garbage '%s'."
1763 (buffer-substring
01c52d31 1764 p (point-at-eol))))
23f87bed
MB
1765 ((= cur prev-num)
1766 (or backed-up
1767 (setq backed-up (gnus-agent-backup-overview-buffer)))
1768 (gnus-message 1
1769 "Duplicate overview line for %d" cur)
54506618 1770 (delete-region p (progn (forward-line 1) (point))))
23f87bed
MB
1771 ((< cur prev-num)
1772 (or backed-up
1773 (setq backed-up (gnus-agent-backup-overview-buffer)))
1774 (gnus-message 1 "Overview buffer not sorted!")
1775 (sort-numeric-fields 1 (point-min) (point-max))
1776 (goto-char (point-min))
1777 (setq prev-num -1))
1778 (t
1779 (setq prev-num cur)))
1780 (forward-line 1)))))))
df80b09f 1781
01c52d31
MB
1782(defun gnus-agent-flush-server (&optional server-or-method)
1783 "Flush all agent index files for every subscribed group within
1784 the given SERVER-OR-METHOD. When called with nil, the current
1785 value of gnus-command-method identifies the server."
1786 (let* ((gnus-command-method (if server-or-method
1787 (gnus-server-to-method server-or-method)
1788 gnus-command-method))
1789 (alist gnus-newsrc-alist))
1790 (while alist
1791 (let ((entry (pop alist)))
1792 (when (gnus-methods-equal-p gnus-command-method (gnus-info-method entry))
1793 (gnus-agent-flush-group (gnus-info-group entry)))))))
1794
1795(defun gnus-agent-flush-group (group)
1796 "Flush the agent's index files such that the GROUP no longer
1797appears to have any local content. The actual content, the
1798article files, may then be deleted using gnus-agent-expire-group.
1799If flushing was a mistake, the gnus-agent-regenerate-group method
1800provides an undo mechanism by reconstructing the index files from
1801the article files."
1802 (interactive (list (gnus-agent-read-group)))
1803
1804 (let* ((gnus-command-method (or gnus-command-method
1805 (gnus-find-method-for-group group)))
1806 (overview (gnus-agent-article-name ".overview" group))
1807 (agentview (gnus-agent-article-name ".agentview" group))
1808 (file-name-coding-system nnmail-pathname-coding-system))
1809
1810 (if (file-exists-p overview)
1811 (delete-file overview))
1812 (if (file-exists-p agentview)
1813 (delete-file agentview))
1814
1815 (gnus-agent-update-view-total-fetched-for group nil gnus-command-method)
1816 (gnus-agent-update-view-total-fetched-for group t gnus-command-method)
1817
1818 ;(gnus-agent-set-local group nil nil)
1819 ;(gnus-agent-save-local t)
1820 (gnus-agent-save-group-info nil group nil)))
1821
df80b09f 1822(defun gnus-agent-flush-cache ()
01c52d31
MB
1823 "Flush the agent's index files such that the group no longer
1824appears to have any local content. The actual content, the
1825article files, is then deleted using gnus-agent-expire-group. The
1826gnus-agent-regenerate-group method provides an undo mechanism by
1827reconstructing the index files from the article files."
1828 (interactive)
df80b09f 1829 (save-excursion
01c52d31
MB
1830 (let ((file-name-coding-system nnmail-pathname-coding-system))
1831 (while gnus-agent-buffer-alist
1832 (set-buffer (cdar gnus-agent-buffer-alist))
1833 (let ((coding-system-for-write gnus-agent-file-coding-system))
1834 (write-region (point-min) (point-max)
1835 (gnus-agent-article-name ".overview"
1836 (caar gnus-agent-buffer-alist))
1837 nil 'silent))
1838 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1839 (while gnus-agent-group-alist
1840 (with-temp-file (gnus-agent-article-name
1841 ".agentview" (caar gnus-agent-group-alist))
1842 (princ (cdar gnus-agent-group-alist))
1843 (insert "\n")
1844 (princ 1 (current-buffer))
1845 (insert "\n"))
1846 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist))))))
23f87bed 1847
54506618 1848;;;###autoload
23f87bed
MB
1849(defun gnus-agent-find-parameter (group symbol)
1850 "Search for GROUPs SYMBOL in the group's parameters, the group's
1851topic parameters, the group's category, or the customizable
1852variables. Returns the first non-nil value found."
1853 (or (gnus-group-find-parameter group symbol t)
1854 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1855 (symbol-value
1856 (cdr
1857 (assq symbol
1858 '((agent-short-article . gnus-agent-short-article)
1859 (agent-long-article . gnus-agent-long-article)
1860 (agent-low-score . gnus-agent-low-score)
1861 (agent-high-score . gnus-agent-high-score)
1862 (agent-days-until-old . gnus-agent-expire-days)
1863 (agent-enable-expiration
1864 . gnus-agent-enable-expiration)
1865 (agent-predicate . gnus-agent-predicate)))))))
16409b0b 1866
df80b09f 1867(defun gnus-agent-fetch-headers (group &optional force)
23f87bed
MB
1868 "Fetch interesting headers into the agent. The group's overview
1869file will be updated to include the headers while a list of available
1870article numbers will be returned."
1871 (let* ((fetch-all (and gnus-agent-consider-all-articles
1872 ;; Do not fetch all headers if the predicate
1873 ;; implies that we only consider unread articles.
1874 (not (gnus-predicate-implies-unread
1875 (gnus-agent-find-parameter group
1876 'agent-predicate)))))
1877 (articles (if fetch-all
4b70e299 1878 (if gnus-newsgroup-maximum-articles
11abff8e
MB
1879 (let ((active (gnus-active group)))
1880 (gnus-uncompress-range
1881 (cons (max (car active)
1882 (- (cdr active)
4b70e299
MB
1883 gnus-newsgroup-maximum-articles
1884 -1))
11abff8e
MB
1885 (cdr active))))
1886 (gnus-uncompress-range (gnus-active group)))
23f87bed
MB
1887 (gnus-list-of-unread-articles group)))
1888 (gnus-decode-encoded-word-function 'identity)
5cedca8d 1889 (gnus-decode-encoded-address-function 'identity)
01c52d31
MB
1890 (file (gnus-agent-article-name ".overview" group))
1891 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
1892
1893 (unless fetch-all
1894 ;; Add articles with marks to the list of article headers we want to
1895 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1896 ;; mark, but do fetch recent or seen articles if they have other, more
1897 ;; interesting marks. (We have to fetch articles with boring marks
1898 ;; because otherwise the agent will remove their marks.)
1899 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1900 (unless (memq (car arts) '(seen recent killed cache))
1901 (setq articles (gnus-range-add articles (cdr arts)))))
1902 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1903
1904 ;; At this point, I have the list of articles to consider for
1905 ;; fetching. This is the list that I'll return to my caller. Some
1906 ;; of these articles may have already been fetched. That's OK as
1907 ;; the fetch article code will filter those out. Internally, I'll
1908 ;; filter this list to just those articles whose headers need to
1909 ;; be fetched.
1910 (let ((articles articles))
1911 ;; Remove known articles.
1912 (when (and (or gnus-agent-cache
1913 (not gnus-plugged))
1914 (gnus-agent-load-alist group))
1915 ;; Remove articles marked as downloaded.
1916 (if fetch-all
1917 ;; I want to fetch all headers in the active range.
1918 ;; Therefore, exclude only those headers that are in the
1919 ;; article alist.
1920 ;; NOTE: This is probably NOT what I want to do after
1921 ;; agent expiration in this group.
1922 (setq articles (gnus-agent-uncached-articles articles group))
1923
1924 ;; I want to only fetch those headers that have never been
1925 ;; fetched. Therefore, exclude all headers that are, or
1926 ;; WERE, in the article alist.
1927 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1928 (high (cdr (gnus-active group))))
1929 ;; Low can be greater than High when the same group is
1930 ;; fetched twice in the same session {The first fetch will
1931 ;; fill the article alist such that (last
1932 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1933 ;; group))}. The addition of one(the 1+ above) then
1934 ;; forces Low to be greater than High. When this happens,
1935 ;; gnus-list-range-intersection returns nil which
1936 ;; indicates that no headers need to be fetched. -- Kevin
1937 (setq articles (gnus-list-range-intersection
1938 articles (list (cons low high)))))))
1939
1940 (gnus-message
1941 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1942 (gnus-compress-sequence articles t))
1943
df80b09f 1944 (save-excursion
23f87bed
MB
1945 (set-buffer nntp-server-buffer)
1946
1947 (if articles
1948 (progn
1949 (gnus-message 7 "Fetching headers for %s..." group)
1950
1951 ;; Fetch them.
1952 (gnus-make-directory (nnheader-translate-file-chars
1953 (file-name-directory file) t))
1954
1955 (unless (eq 'nov (gnus-retrieve-headers articles group))
1956 (nnvirtual-convert-headers))
1957 (gnus-agent-check-overview-buffer)
1958 ;; Move these headers to the overview buffer so that
1959 ;; gnus-agent-braid-nov can merge them with the contents
1960 ;; of FILE.
1961 (copy-to-buffer
1962 gnus-agent-overview-buffer (point-min) (point-max))
54506618
MB
1963 ;; NOTE: Call g-a-brand-nov even when the file does not
1964 ;; exist. As a minimum, it will validate the article
1965 ;; numbers already in the buffer.
1966 (gnus-agent-braid-nov group articles file)
23f87bed
MB
1967 (let ((coding-system-for-write
1968 gnus-agent-file-coding-system))
1969 (gnus-agent-check-overview-buffer)
1970 (write-region (point-min) (point-max) file nil 'silent))
01c52d31 1971 (gnus-agent-update-view-total-fetched-for group t)
23f87bed
MB
1972 (gnus-agent-save-alist group articles nil)
1973 articles)
1974 (ignore-errors
1975 (erase-buffer)
1976 (nnheader-insert-file-contents file)))))
1977 articles))
df80b09f 1978
54506618
MB
1979(defsubst gnus-agent-read-article-number ()
1980 "Reads the article number at point. Returns nil when a valid article number can not be read."
1981
58090a8d 1982 ;; It is unfortunate but the read function quietly overflows
54506618
MB
1983 ;; integer. As a result, I have to use string operations to test
1984 ;; for overflow BEFORE calling read.
1985 (when (looking-at "[0-9]+\t")
1986 (let ((len (- (match-end 0) (match-beginning 0))))
1987 (cond ((< len 9)
1988 (read (current-buffer)))
1989 ((= len 9)
1990 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1991 ;; Back convert from int to string to ensure that this is one of them.
1992 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1993 (num (read (current-buffer)))
1994 (str2 (int-to-string num)))
1995 (when (equal str1 str2)
1996 num)))))))
1997
df80b09f 1998(defsubst gnus-agent-copy-nov-line (article)
54506618 1999 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
23f87bed 2000 (let (art b e)
df80b09f 2001 (set-buffer gnus-agent-overview-buffer)
23f87bed 2002 (while (and (not (eobp))
54506618
MB
2003 (or (not (setq art (gnus-agent-read-article-number)))
2004 (< art article)))
23f87bed
MB
2005 (forward-line 1))
2006 (beginning-of-line)
2007 (if (or (eobp)
2008 (not (eq article art)))
2009 (set-buffer nntp-server-buffer)
2010 (setq b (point))
2011 (setq e (progn (forward-line 1) (point)))
2012 (set-buffer nntp-server-buffer)
2013 (insert-buffer-substring gnus-agent-overview-buffer b e))))
df80b09f
LMI
2014
2015(defun gnus-agent-braid-nov (group articles file)
23f87bed 2016 "Merge agent overview data with given file.
54506618
MB
2017Takes unvalidated headers for ARTICLES from
2018`gnus-agent-overview-buffer' and validated headers from the given
2019FILE and places the combined valid headers into
2020`nntp-server-buffer'. This function can be used, when file
2021doesn't exist, to valid the overview buffer."
23f87bed
MB
2022 (let (start last)
2023 (set-buffer gnus-agent-overview-buffer)
2024 (goto-char (point-min))
2025 (set-buffer nntp-server-buffer)
2026 (erase-buffer)
54506618
MB
2027 (when (file-exists-p file)
2028 (nnheader-insert-file-contents file))
23f87bed
MB
2029 (goto-char (point-max))
2030 (forward-line -1)
54506618 2031
23f87bed
MB
2032 (unless (or (= (point-min) (point-max))
2033 (< (setq last (read (current-buffer))) (car articles)))
54506618 2034 ;; Old and new overlap -- We do it the hard way.
23f87bed
MB
2035 (when (nnheader-find-nov-line (car articles))
2036 ;; Replacing existing NOV entry
2037 (delete-region (point) (progn (forward-line 1) (point))))
2038 (gnus-agent-copy-nov-line (pop articles))
2039
2040 (ignore-errors
01c52d31
MB
2041 (while articles
2042 (while (let ((art (read (current-buffer))))
2043 (cond ((< art (car articles))
2044 (forward-line 1)
2045 t)
2046 ((= art (car articles))
2047 (beginning-of-line)
2048 (delete-region
2049 (point) (progn (forward-line 1) (point)))
2050 nil)
2051 (t
2052 (beginning-of-line)
2053 nil))))
54506618 2054
01c52d31 2055 (gnus-agent-copy-nov-line (pop articles)))))
54506618 2056
23f87bed 2057 (goto-char (point-max))
54506618
MB
2058
2059 ;; Append the remaining lines
df80b09f 2060 (when articles
23f87bed 2061 (when last
df80b09f 2062 (set-buffer gnus-agent-overview-buffer)
23f87bed
MB
2063 (setq start (point))
2064 (set-buffer nntp-server-buffer))
54506618
MB
2065
2066 (let ((p (point)))
2067 (insert-buffer-substring gnus-agent-overview-buffer start)
2068 (goto-char p))
2069
2070 (setq last (or last -134217728))
58090a8d 2071 (while (catch 'problems
01c52d31
MB
2072 (let (sort art)
2073 (while (not (eobp))
2074 (setq art (gnus-agent-read-article-number))
2075 (cond ((not art)
2076 ;; Bad art num - delete this line
2077 (beginning-of-line)
2078 (delete-region (point) (progn (forward-line 1) (point))))
2079 ((< art last)
2080 ;; Art num out of order - enable sort
2081 (setq sort t)
2082 (forward-line 1))
58090a8d
MB
2083 ((= art last)
2084 ;; Bad repeat of art number - delete this line
2085 (beginning-of-line)
2086 (delete-region (point) (progn (forward-line 1) (point))))
01c52d31
MB
2087 (t
2088 ;; Good art num
2089 (setq last art)
2090 (forward-line 1))))
2091 (when sort
58090a8d
MB
2092 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
2093 ;; First, we'll fix the sort.
2094 (sort-numeric-fields 1 (point-min) (point-max))
2095
655591a7 2096 ;; but now we have to consider that we may have duplicate rows...
58090a8d
MB
2097 ;; so reset to beginning of file
2098 (goto-char (point-min))
2099 (setq last -134217728)
655591a7 2100
58090a8d
MB
2101 ;; and throw a code that restarts this scan
2102 (throw 'problems t))
2103 nil))))))
df80b09f 2104
23f87bed
MB
2105;; Keeps the compiler from warning about the free variable in
2106;; gnus-agent-read-agentview.
9efa445f 2107(defvar gnus-agent-read-agentview)
df80b09f 2108
23f87bed
MB
2109(defun gnus-agent-load-alist (group)
2110 "Load the article-state alist for GROUP."
2111 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
01c52d31
MB
2112 (let ((gnus-agent-read-agentview group)
2113 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
2114 (setq gnus-agent-article-alist
2115 (gnus-cache-file-contents
2116 (gnus-agent-article-name ".agentview" group)
2117 'gnus-agent-file-loading-cache
2118 'gnus-agent-read-agentview))))
2119
23f87bed
MB
2120(defun gnus-agent-read-agentview (file)
2121 "Load FILE and do a `read' there."
2122 (with-temp-buffer
54506618 2123 (condition-case nil
01c52d31
MB
2124 (progn
2125 (nnheader-insert-file-contents file)
2126 (goto-char (point-min))
2127 (let ((alist (read (current-buffer)))
2128 (version (condition-case nil (read (current-buffer))
2129 (end-of-file 0)))
2130 changed-version)
2131
2132 (cond
2133 ((= version 0)
2134 (let ((inhibit-quit t)
2135 entry)
2136 (gnus-agent-open-history)
2137 (set-buffer (gnus-agent-history-buffer))
2138 (goto-char (point-min))
2139 (while (not (eobp))
2140 (if (and (looking-at
2141 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2142 (string= (match-string 2)
2143 gnus-agent-read-agentview)
2144 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2145 (setcdr entry (string-to-number (match-string 1))))
2146 (forward-line 1))
2147 (gnus-agent-close-history)
2148 (setq changed-version t)))
2149 ((= version 1)
2150 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2151 ((= version 2)
2152 (let (state sequence uncomp)
2153 (while alist
2154 (setq state (caar alist)
2155 sequence (inline (gnus-uncompress-range (cdar alist)))
2156 alist (cdr alist))
2157 (while sequence
2158 (push (cons (pop sequence) state) uncomp)))
58090a8d
MB
2159 (setq alist (sort uncomp 'car-less-than-car)))
2160 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
01c52d31
MB
2161 (when changed-version
2162 (let ((gnus-agent-article-alist alist))
2163 (gnus-agent-save-alist gnus-agent-read-agentview)))
2164 alist))
2165 ((end-of-file file-error)
2166 ;; The agentview file is missing.
2167 (condition-case nil
2168 ;; If the agent directory exists, attempt to perform a brute-force
2169 ;; reconstruction of its contents.
2170 (let* (alist
2171 (file-name-coding-system nnmail-pathname-coding-system)
2172 (file-attributes (directory-files-and-attributes
2173 (gnus-agent-article-name ""
2174 gnus-agent-read-agentview) nil "^[0-9]+$" t)))
2175 (while file-attributes
2176 (let ((fa (pop file-attributes)))
2177 (unless (nth 1 fa)
2178 (push (cons (string-to-number (nth 0 fa)) (time-to-days (nth 5 fa))) alist))))
2179 alist)
2180 (file-error nil))))))
23f87bed
MB
2181
2182(defun gnus-agent-save-alist (group &optional articles state)
df80b09f 2183 "Save the article-state alist for GROUP."
23f87bed
MB
2184 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2185 (prev (cons nil gnus-agent-article-alist))
2186 (all prev)
2187 print-level print-length item article)
2188 (while (setq article (pop articles))
2189 (while (and (cdr prev)
2190 (< (caadr prev) article))
2191 (setq prev (cdr prev)))
2192 (cond
2193 ((not (cdr prev))
2194 (setcdr prev (list (cons article state))))
2195 ((> (caadr prev) article)
2196 (setcdr prev (cons (cons article state) (cdr prev))))
2197 ((= (caadr prev) article)
2198 (setcdr (cadr prev) state)))
2199 (setq prev (cdr prev)))
2200 (setq gnus-agent-article-alist (cdr all))
2201
bf247b6e
KS
2202 (gnus-agent-set-local group
2203 (caar gnus-agent-article-alist)
23f87bed
MB
2204 (caar (last gnus-agent-article-alist)))
2205
2206 (gnus-make-directory (gnus-agent-article-name "" group))
2207 (with-temp-file (gnus-agent-article-name ".agentview" group)
2208 (cond ((eq gnus-agent-article-alist-save-format 1)
2209 (princ gnus-agent-article-alist (current-buffer)))
2210 ((eq gnus-agent-article-alist-save-format 2)
01c52d31
MB
2211 (let ((alist gnus-agent-article-alist)
2212 article-id day-of-download comp-list compressed)
2213 (while alist
2214 (setq article-id (caar alist)
2215 day-of-download (cdar alist)
2216 comp-list (assq day-of-download compressed)
2217 alist (cdr alist))
2218 (if comp-list
2219 (setcdr comp-list (cons article-id (cdr comp-list)))
2220 (push (list day-of-download article-id) compressed)))
2221 (setq alist compressed)
2222 (while alist
2223 (setq comp-list (pop alist))
2224 (setcdr comp-list
2225 (gnus-compress-sequence (nreverse (cdr comp-list)))))
23f87bed
MB
2226 (princ compressed (current-buffer)))))
2227 (insert "\n")
2228 (princ gnus-agent-article-alist-save-format (current-buffer))
01c52d31
MB
2229 (insert "\n"))
2230
2231 (gnus-agent-update-view-total-fetched-for group nil)))
23f87bed
MB
2232
2233(defvar gnus-agent-article-local nil)
2234(defvar gnus-agent-file-loading-local nil)
2235
2236(defun gnus-agent-load-local (&optional method)
2237 "Load the METHOD'S local file. The local file contains min/max
2238article counts for each of the method's subscribed groups."
2239 (let ((gnus-command-method (or method gnus-command-method)))
2240 (setq gnus-agent-article-local
2241 (gnus-cache-file-contents
2242 (gnus-agent-lib-file "local")
2243 'gnus-agent-file-loading-local
2244 'gnus-agent-read-and-cache-local))))
2245
2246(defun gnus-agent-read-and-cache-local (file)
2247 "Load and read FILE then bind its contents to
2248gnus-agent-article-local. If that variable had `dirty' (also known as
2249modified) original contents, they are first saved to their own file."
2250
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))
2255
2256(defun gnus-agent-read-local (file)
2257 "Load FILE and do a `read' there."
bf247b6e 2258 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
23f87bed
MB
2259 (point-max))))
2260 (line 1))
2261 (with-temp-buffer
2262 (condition-case nil
54506618
MB
2263 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2264 (nnheader-insert-file-contents file))
23f87bed
MB
2265 (file-error))
2266
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)))
2272
2273 (while (not (eobp))
2274 (condition-case err
bf247b6e 2275 (let (group
23f87bed
MB
2276 min
2277 max
26c9afc3
MB
2278 (cur (current-buffer))
2279 (obarray my-obarray))
23f87bed
MB
2280 (setq group (read cur)
2281 min (read cur)
2282 max (read cur))
2283
2284 (when (stringp group)
2285 (setq group (intern group my-obarray)))
2286
2287 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2288 (set group (cons (+ 0 min) (+ 0 max))))
2289 (error
58090a8d 2290 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
23f87bed
MB
2291 file line (error-message-string err))))
2292 (forward-line 1)
2293 (setq line (1+ line))))
bf247b6e 2294
23f87bed
MB
2295 (set (intern "+dirty" my-obarray) nil)
2296 (set (intern "+method" my-obarray) gnus-command-method)
2297 my-obarray))
2298
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 ""))
54506618 2308
01c52d31
MB
2309 (let ((coding-system-for-write gnus-agent-file-coding-system)
2310 (file-name-coding-system nnmail-pathname-coding-system))
54506618
MB
2311 (with-temp-file dest
2312 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
54506618
MB
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)
2320 (t
54506618 2321 (let ((range (symbol-value symbol)))
58090a8d 2322 (when range
01c52d31
MB
2323 (prin1 symbol)
2324 (princ " ")
2325 (princ (car range))
2326 (princ " ")
2327 (princ (cdr range))
58090a8d 2328 (princ "\n"))))))
54506618
MB
2329 my-obarray))))))))
2330
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)))
23f87bed
MB
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))))
bf247b6e 2347 (gnus-agent-set-local group (car minmax) (cdr minmax)
23f87bed
MB
2348 gmane gnus-command-method local))))
2349 minmax))
2350
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))))
bf247b6e 2357
23f87bed
MB
2358 (if (cond ((and minmax
2359 (or (not (eq min (car minmax)))
26c9afc3
MB
2360 (not (eq max (cdr minmax))))
2361 min
2362 max)
23f87bed
MB
2363 (setcar minmax min)
2364 (setcdr minmax max)
2365 t)
2366 (minmax
2367 nil)
2368 ((and min max)
2369 (set symb (cons min max))
54506618
MB
2370 t)
2371 (t
2372 (unintern symb local)))
23f87bed 2373 (set (intern "+dirty" local) t))))
df80b09f
LMI
2374
2375(defun gnus-agent-article-name (article group)
23f87bed 2376 (expand-file-name article
850846fd 2377 (file-name-as-directory
23f87bed 2378 (gnus-agent-group-pathname group))))
df80b09f 2379
16409b0b
GM
2380(defun gnus-agent-batch-confirmation (msg)
2381 "Show error message and return t."
2382 (gnus-message 1 msg)
2383 t)
2384
df80b09f
LMI
2385;;;###autoload
2386(defun gnus-agent-batch-fetch ()
2387 "Start Gnus and fetch session."
2388 (interactive)
2389 (gnus)
16409b0b
GM
2390 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2391 (gnus-agent-fetch-session))
df80b09f
LMI
2392 (gnus-group-exit))
2393
2394(defun gnus-agent-fetch-session ()
2395 "Fetch all articles and headers that are eligible for fetching."
2396 (interactive)
2397 (unless gnus-agent-covered-methods
2398 (error "No servers are covered by the Gnus agent"))
2399 (unless gnus-plugged
2400 (error "Can't fetch articles while Gnus is unplugged"))
23f87bed 2401 (let ((methods (gnus-agent-covered-methods))
df80b09f
LMI
2402 groups group gnus-command-method)
2403 (save-excursion
2404 (while methods
23f87bed
MB
2405 (setq gnus-command-method (car methods))
2406 (when (and (or (gnus-server-opened gnus-command-method)
2407 (gnus-open-server gnus-command-method))
2408 (gnus-online gnus-command-method))
2409 (setq groups (gnus-groups-from-server (car methods)))
2410 (gnus-agent-with-fetch
2411 (while (setq group (pop groups))
2412 (when (<= (gnus-group-level group)
2413 gnus-agent-handle-level)
2414 (if (or debug-on-error debug-on-quit)
2415 (gnus-agent-fetch-group-1
2416 group gnus-command-method)
2417 (condition-case err
2418 (gnus-agent-fetch-group-1
2419 group gnus-command-method)
2420 (error
2421 (unless (funcall gnus-agent-confirmation-function
54506618 2422 (format "Error %s while fetching session. Should gnus continue? "
23f87bed
MB
2423 (error-message-string err)))
2424 (error "Cannot fetch articles into the Gnus agent")))
2425 (quit
54506618 2426 (gnus-agent-regenerate-group group)
23f87bed
MB
2427 (unless (funcall gnus-agent-confirmation-function
2428 (format
54506618 2429 "%s while fetching session. Should gnus continue? "
23f87bed
MB
2430 (error-message-string err)))
2431 (signal 'quit
2432 "Cannot fetch articles into the Gnus agent")))))))))
2433 (setq methods (cdr methods)))
2434 (gnus-run-hooks 'gnus-agent-fetched-hook)
df80b09f
LMI
2435 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2436
2437(defun gnus-agent-fetch-group-1 (group method)
2438 "Fetch GROUP."
2439 (let ((gnus-command-method method)
16409b0b 2440 (gnus-newsgroup-name group)
23f87bed
MB
2441 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2442 (gnus-newsgroup-headers gnus-newsgroup-headers)
2443 (gnus-newsgroup-scored gnus-newsgroup-scored)
2444 (gnus-use-cache gnus-use-cache)
16409b0b
GM
2445 (gnus-summary-expunge-below gnus-summary-expunge-below)
2446 (gnus-summary-mark-below gnus-summary-mark-below)
2447 (gnus-orphan-score gnus-orphan-score)
2448 ;; Maybe some other gnus-summary local variables should also
2449 ;; be put here.
23f87bed
MB
2450
2451 gnus-headers
2452 gnus-score
2453 articles arts
2454 category predicate info marks score-param
16409b0b
GM
2455 )
2456 (unless (gnus-check-group group)
2457 (error "Can't open server for %s" group))
23f87bed 2458
df80b09f 2459 ;; Fetch headers.
23f87bed
MB
2460 (when (or gnus-newsgroup-active
2461 (gnus-active group)
2462 (gnus-activate-group group))
2463 (let ((marked-articles gnus-newsgroup-downloadable))
2464 ;; Identify the articles marked for download
2465 (unless gnus-newsgroup-active
2466 ;; The variable gnus-newsgroup-active was selected as I need
2467 ;; a gnus-summary local variable that is NOT bound to any
2468 ;; value (its global value should default to nil).
2469 (dolist (mark gnus-agent-download-marks)
2470 (let ((arts (cdr (assq mark (gnus-info-marks
2471 (setq info (gnus-get-info group)))))))
2472 (when arts
2473 (setq marked-articles (nconc (gnus-uncompress-range arts)
2474 marked-articles))
2475 ))))
2476 (setq marked-articles (sort marked-articles '<))
2477
2478 ;; Fetch any new articles from the server
2479 (setq articles (gnus-agent-fetch-headers group))
2480
2481 ;; Merge new articles with marked
2482 (setq articles (sort (append marked-articles articles) '<))
2483
2484 (when articles
2485 ;; Parse them and see which articles we want to fetch.
2486 (setq gnus-newsgroup-dependencies
2487 (or gnus-newsgroup-dependencies
2488 (make-vector (length articles) 0)))
2489 (setq gnus-newsgroup-headers
2490 (or gnus-newsgroup-headers
2491 (gnus-get-newsgroup-headers-xover articles nil nil
2492 group)))
2493 ;; `gnus-agent-overview-buffer' may be killed for
2494 ;; timeout reason. If so, recreate it.
2495 (gnus-agent-create-buffer)
2496
2497 ;; Figure out how to select articles in this group
2498 (setq category (gnus-group-category group))
2499
2500 (setq predicate
2501 (gnus-get-predicate
2502 (gnus-agent-find-parameter group 'agent-predicate)))
2503
2504 ;; If the selection predicate requires scoring, score each header
2505 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2506 (let ((score-param
2507 (gnus-agent-find-parameter group 'agent-score-file)))
2508 ;; Translate score-param into real one
2509 (cond
2510 ((not score-param))
2511 ((eq score-param 'file)
2512 (setq score-param (gnus-all-score-files group)))
2513 ((stringp (car score-param)))
2514 (t
2515 (setq score-param (list (list score-param)))))
2516 (when score-param
2517 (gnus-score-headers score-param))))
2518
2519 (unless (and (eq predicate 'gnus-agent-false)
2520 (not marked-articles))
2521 (let ((arts (list nil)))
2522 (let ((arts-tail arts)
2523 (alist (gnus-agent-load-alist group))
2524 (marked-articles marked-articles)
2525 (gnus-newsgroup-headers gnus-newsgroup-headers))
2526 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2527 (let ((num (mail-header-number gnus-headers)))
2528 ;; Determine if this article is already in the cache
2529 (while (and alist
2530 (> num (caar alist)))
2531 (setq alist (cdr alist)))
2532
2533 (unless (and (eq num (caar alist))
2534 (cdar alist))
2535
2536 ;; Determine if this article was marked for download.
2537 (while (and marked-articles
2538 (> num (car marked-articles)))
2539 (setq marked-articles
2540 (cdr marked-articles)))
2541
2542 ;; When this article is marked, or selected by the
2543 ;; predicate, add it to the download list
2544 (when (or (eq num (car marked-articles))
2545 (let ((gnus-score
2546 (or (cdr
2547 (assq num gnus-newsgroup-scored))
2548 gnus-summary-default-score))
2549 (gnus-agent-long-article
2550 (gnus-agent-find-parameter
2551 group 'agent-long-article))
2552 (gnus-agent-short-article
2553 (gnus-agent-find-parameter
2554 group 'agent-short-article))
2555 (gnus-agent-low-score
2556 (gnus-agent-find-parameter
2557 group 'agent-low-score))
2558 (gnus-agent-high-score
2559 (gnus-agent-find-parameter
2560 group 'agent-high-score))
2561 (gnus-agent-expire-days
2562 (gnus-agent-find-parameter
2563 group 'agent-days-until-old)))
2564 (funcall predicate)))
2565 (gnus-agent-append-to-list arts-tail num))))))
2566
2567 (let (fetched-articles)
2568 ;; Fetch all selected articles
2569 (setq gnus-newsgroup-undownloaded
2570 (gnus-sorted-ndifference
2571 gnus-newsgroup-undownloaded
2572 (setq fetched-articles
2573 (if (cdr arts)
2574 (gnus-agent-fetch-articles group (cdr arts))
2575 nil))))
2576
2577 (let ((unfetched-articles
2578 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2579 (if gnus-newsgroup-active
2580 ;; Update the summary buffer
2581 (progn
2582 (dolist (article marked-articles)
2583 (gnus-summary-set-agent-mark article t))
2584 (dolist (article fetched-articles)
58090a8d
MB
2585 (when gnus-agent-mark-unread-after-downloaded
2586 (setq gnus-newsgroup-downloadable
2587 (delq article gnus-newsgroup-downloadable))
01c52d31
MB
2588 (gnus-summary-mark-article
2589 article gnus-unread-mark))
23f87bed
MB
2590 (when (gnus-summary-goto-subject article nil t)
2591 (gnus-summary-update-download-mark article)))
2592 (dolist (article unfetched-articles)
2593 (gnus-summary-mark-article
2594 article gnus-canceled-mark)))
2595
2596 ;; Update the group buffer.
2597
2598 ;; When some, or all, of the marked articles came
2599 ;; from the download mark. Remove that mark. I
2600 ;; didn't do this earlier as I only want to remove
2601 ;; the marks after the fetch is completed.
2602
2603 (dolist (mark gnus-agent-download-marks)
2604 (when (eq mark 'download)
2605 (let ((marked-arts
2606 (assq mark (gnus-info-marks
2607 (setq info (gnus-get-info group))))))
2608 (when (cdr marked-arts)
2609 (setq marks
2610 (delq marked-arts (gnus-info-marks info)))
2611 (gnus-info-set-marks info marks)))))
2612 (let ((read (gnus-info-read
2613 (or info (setq info (gnus-get-info group))))))
2614 (gnus-info-set-read
2615 info (gnus-add-to-range read unfetched-articles)))
2616
2617 (gnus-group-update-group group t)
2618 (sit-for 0)
2619
2620 (gnus-dribble-enter
2621 (concat "(gnus-group-set-info '"
2622 (gnus-prin1-to-string info)
2623 ")"))))))))))))
df80b09f
LMI
2624
2625;;;
2626;;; Agent Category Mode
2627;;;
2628
2629(defvar gnus-category-mode-hook nil
2630 "Hook run in `gnus-category-mode' buffers.")
2631
2632(defvar gnus-category-line-format " %(%20c%): %g\n"
23f87bed
MB
2633 "Format of category lines.
2634
2635Valid specifiers include:
2636%c Topic name (string)
2637%g The number of groups in the topic (integer)
2638
2639General format specifiers can also be used. See Info node
2640`(gnus)Formatting Variables'.")
df80b09f
LMI
2641
2642(defvar gnus-category-mode-line-format "Gnus: %%b"
2643 "The format specification for the category mode line.")
2644
23f87bed
MB
2645(defvar gnus-agent-predicate 'false
2646 "The selection predicate used when no other source is available.")
2647
df80b09f
LMI
2648(defvar gnus-agent-short-article 100
2649 "Articles that have fewer lines than this are short.")
2650
2651(defvar gnus-agent-long-article 200
2652 "Articles that have more lines than this are long.")
2653
2654(defvar gnus-agent-low-score 0
2655 "Articles that have a score lower than this have a low score.")
2656
2657(defvar gnus-agent-high-score 0
2658 "Articles that have a score higher than this have a high score.")
2659
2660
2661;;; Internal variables.
2662
2663(defvar gnus-category-buffer "*Agent Category*")
2664
2665(defvar gnus-category-line-format-alist
2666 `((?c gnus-tmp-name ?s)
2667 (?g gnus-tmp-groups ?d)))
2668
2669(defvar gnus-category-mode-line-format-alist
2670 `((?u user-defined ?s)))
2671
2672(defvar gnus-category-line-format-spec nil)
2673(defvar gnus-category-mode-line-format-spec nil)
2674
2675(defvar gnus-category-mode-map nil)
2676(put 'gnus-category-mode 'mode-class 'special)
2677
2678(unless gnus-category-mode-map
2679 (setq gnus-category-mode-map (make-sparse-keymap))
2680 (suppress-keymap gnus-category-mode-map)
2681
2682 (gnus-define-keys gnus-category-mode-map
2683 "q" gnus-category-exit
2684 "k" gnus-category-kill
2685 "c" gnus-category-copy
2686 "a" gnus-category-add
23f87bed 2687 "e" gnus-agent-customize-category
df80b09f
LMI
2688 "p" gnus-category-edit-predicate
2689 "g" gnus-category-edit-groups
2690 "s" gnus-category-edit-score
2691 "l" gnus-category-list
2692
2693 "\C-c\C-i" gnus-info-find-node
2694 "\C-c\C-b" gnus-bug))
2695
2696(defvar gnus-category-menu-hook nil
2697 "*Hook run after the creation of the menu.")
2698
2699(defun gnus-category-make-menu-bar ()
2700 (gnus-turn-off-edit-menu 'category)
2701 (unless (boundp 'gnus-category-menu)
2702 (easy-menu-define
2703 gnus-category-menu gnus-category-mode-map ""
2704 '("Categories"
2705 ["Add" gnus-category-add t]
2706 ["Kill" gnus-category-kill t]
2707 ["Copy" gnus-category-copy t]
23f87bed 2708 ["Edit category" gnus-agent-customize-category t]
df80b09f
LMI
2709 ["Edit predicate" gnus-category-edit-predicate t]
2710 ["Edit score" gnus-category-edit-score t]
2711 ["Edit groups" gnus-category-edit-groups t]
2712 ["Exit" gnus-category-exit t]))
2713
2714 (gnus-run-hooks 'gnus-category-menu-hook)))
2715
2716(defun gnus-category-mode ()
2717 "Major mode for listing and editing agent categories.
2718
2719All normal editing commands are switched off.
2720\\<gnus-category-mode-map>
2721For more in-depth information on this mode, read the manual
23f87bed 2722\(`\\[gnus-info-find-node]').
df80b09f
LMI
2723
2724The following commands are available:
2725
2726\\{gnus-category-mode-map}"
2727 (interactive)
2728 (when (gnus-visual-p 'category-menu 'menu)
2729 (gnus-category-make-menu-bar))
2730 (kill-all-local-variables)
2731 (gnus-simplify-mode-line)
2732 (setq major-mode 'gnus-category-mode)
2733 (setq mode-name "Category")
2734 (gnus-set-default-directory)
2735 (setq mode-line-process nil)
2736 (use-local-map gnus-category-mode-map)
16409b0b 2737 (buffer-disable-undo)
df80b09f
LMI
2738 (setq truncate-lines t)
2739 (setq buffer-read-only t)
cfcd5c91 2740 (gnus-run-mode-hooks 'gnus-category-mode-hook))
df80b09f
LMI
2741
2742(defalias 'gnus-category-position-point 'gnus-goto-colon)
2743
2744(defun gnus-category-insert-line (category)
23f87bed
MB
2745 (let* ((gnus-tmp-name (format "%s" (car category)))
2746 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
df80b09f
LMI
2747 (beginning-of-line)
2748 (gnus-add-text-properties
2749 (point)
2750 (prog1 (1+ (point))
2751 ;; Insert the text.
2752 (eval gnus-category-line-format-spec))
2753 (list 'gnus-category gnus-tmp-name))))
2754
2755(defun gnus-enter-category-buffer ()
2756 "Go to the Category buffer."
2757 (interactive)
2758 (gnus-category-setup-buffer)
2759 (gnus-configure-windows 'category)
2760 (gnus-category-prepare))
2761
2762(defun gnus-category-setup-buffer ()
2763 (unless (get-buffer gnus-category-buffer)
2764 (save-excursion
2765 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2766 (gnus-category-mode))))
2767
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)))
2778
2779(defun gnus-category-name ()
01c52d31 2780 (or (intern (get-text-property (point-at-bol) 'gnus-category))
df80b09f
LMI
2781 (error "No category on the current line")))
2782
2783(defun gnus-category-read ()
2784 "Read the category alist."
2785 (setq gnus-category-alist
23f87bed
MB
2786 (or
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.
2793
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))))
2812 c)
2813 old-list)))))
2814 (list (gnus-agent-cat-make 'default 'short)))))
df80b09f
LMI
2815
2816(defun gnus-category-write ()
2817 "Write the category alist."
2818 (setq gnus-category-predicate-cache nil
2819 gnus-category-group-cache nil)
16409b0b
GM
2820 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2821 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
23f87bed
MB
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)
df80b09f
LMI
2832 (prin1 gnus-category-alist (current-buffer))))
2833
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
23f87bed
MB
2839 (gnus-agent-cat-predicate info)
2840 (format "Editing the select predicate for category %s" category)
df80b09f 2841 `(lambda (predicate)
23f87bed
MB
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)
2848
df80b09f
LMI
2849 (gnus-category-write)
2850 (gnus-category-list)))))
2851
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
23f87bed 2857 (gnus-agent-cat-score-file info)
df80b09f 2858 (format "Editing the score expression for category %s" category)
23f87bed
MB
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)
2866
df80b09f
LMI
2867 (gnus-category-write)
2868 (gnus-category-list)))))
2869
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
23f87bed
MB
2875 (gnus-agent-cat-groups info)
2876 (format "Editing the group list for category %s" category)
df80b09f 2877 `(lambda (groups)
23f87bed
MB
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)
2884
df80b09f
LMI
2885 (gnus-category-write)
2886 (gnus-category-list)))))
2887
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)
16409b0b
GM
2894 (setq gnus-category-alist (delq info gnus-category-alist))
2895 (gnus-category-write)))
df80b09f
LMI
2896
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)))
23f87bed
MB
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)
df80b09f
LMI
2905 gnus-category-alist)
2906 (gnus-category-write)
2907 (gnus-category-list)))
2908
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))
23f87bed 2914 (push (gnus-agent-cat-make category)
df80b09f
LMI
2915 gnus-category-alist)
2916 (gnus-category-write)
2917 (gnus-category-list))
2918
2919(defun gnus-category-list ()
2920 "List all categories."
2921 (interactive)
2922 (gnus-category-prepare))
2923
2924(defun gnus-category-exit ()
2925 "Return to the group buffer."
2926 (interactive)
2927 (kill-buffer (current-buffer))
2928 (gnus-configure-windows 'group t))
2929
2930;; To avoid having 8-bit characters in the source file.
2931(defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2932
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)
23f87bed 2939 (read . gnus-agent-read-p)
df80b09f
LMI
2940 (true . gnus-agent-true)
2941 (false . gnus-agent-false))
2942 "Mapping from short score predicate symbols to predicate functions.")
2943
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) ""))
2949 nil
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)))))
2954
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))
2958
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))
2962
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))
2966
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))
2970
23f87bed
MB
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))))
2975
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)))))
df80b09f
LMI
2983
2984(defun gnus-agent-true ()
2985 "Return t."
2986 t)
2987
2988(defun gnus-agent-false ()
2989 "Return nil."
2990 nil)
2991
23f87bed
MB
2992(defun gnus-category-make-function-1 (predicate)
2993 "Make a function from PREDICATE."
df80b09f
LMI
2994 (cond
2995 ;; Functions are just returned as is.
23f87bed
MB
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)
df80b09f 3002 `(,(cond
23f87bed 3003 ((memq (car predicate) '(& and))
df80b09f 3004 'and)
23f87bed 3005 ((memq (car predicate) '(| or))
df80b09f 3006 'or)
23f87bed 3007 ((memq (car predicate) gnus-category-not)
df80b09f 3008 'not))
23f87bed 3009 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
df80b09f 3010 (t
23f87bed 3011 (error "Unknown predicate type: %s" predicate))))
df80b09f
LMI
3012
3013(defun gnus-get-predicate (predicate)
23f87bed 3014 "Return the function implementing PREDICATE."
df80b09f 3015 (or (cdr (assoc predicate gnus-category-predicate-cache))
23f87bed
MB
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)))
3021
3022(defun gnus-predicate-implies-unread (predicate)
3023 "Say whether PREDICATE implies unread articles only.
3024It is okay to miss some cases, but there must be no false positives.
3025That is, if this predicate returns true, then indeed the predicate must
3026return only unread articles."
bf247b6e 3027 (eq t (gnus-function-implies-unread-1
23f87bed
MB
3028 (gnus-category-make-function-1 predicate))))
3029
3030(defun gnus-function-implies-unread-1 (function)
3031 "Recursively evaluate a predicate function to determine whether it can select
3032any read articles. Returns t if the function is known to never
3033return read articles, nil when it is known to always return read
3034articles, and t_nil when the function may return both read and unread
3035articles."
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.
3041 t)
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
3075 (t
3076 (error "Unknown predicate function: %s" function)))))
df80b09f
LMI
3077
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))
23f87bed 3085 (setq groups (gnus-agent-cat-groups cat))
df80b09f
LMI
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)))
3090
23f87bed
MB
3091(defun gnus-agent-expire-group (group &optional articles force)
3092 "Expire all old articles in GROUP.
3093If you want to force expiring of certain articles, this function can
3094take ARTICLES, and FORCE parameters as well.
3095
3096The 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.
3100FORCE is equivalent to setting the expiration predicates to true."
01c52d31 3101 (interactive (list (gnus-agent-read-group)))
23f87bed
MB
3102
3103 (if (not group)
3104 (gnus-agent-expire articles group force)
3105 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
01c52d31 3106 ;; expiration statistics of this single group
23f87bed
MB
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 "
66841476 3111 "expire all articles in " group "? ")))
23f87bed
MB
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 (gnus-agent-expire-done-message)))))
3130
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
3135
01c52d31
MB
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))))
3145
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)))
3188 (t
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)
3210 (t
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)
3224
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.
3232
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))
3237
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 precidence 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)))
3254
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))
3262
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
3270 p)
3271 dlist)
3272 (error
3273 (gnus-message 1 "gnus-agent-expire: read error \
23f87bed
MB
3274occurred when reading expression at %s in %s. Skipping to next \
3275line." (point) nov-file)))
01c52d31
MB
3276 ;; Whether I succeeded, or failed, it doesn't matter.
3277 ;; Move to the next line then try again.
3278 (forward-line 1)))
3279
3280 (gnus-message
3281 7 "gnus-agent-expire: Loading overview... Done"))
3282 (set-buffer-modified-p nil)
3283
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))
3297 t)
3298 ((> (nth 0 a) (nth 0 b))
3299 nil)
3300 (t
3301 (let ((a (or (symbol-value (nth 2 a))
3302 3))
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
3323
3324 (setcdr dlist (cddr dlist)))
3325 (setq dlist (cdr dlist)))))
3326
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))
3347
3348 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3349
3350 (let* ((len (float (length dlist)))
3351 (alist (list nil))
3352 (tail-alist alist)
3353 (position-offset 0)
3354 )
3355
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)))
3369
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 \
23f87bed 3382download flag on %s:%d as the cached article file is missing."
01c52d31
MB
3383 decoded (caar dlist)))
3384 (unless marker
3385 (gnus-message 1 "gnus-agent-expire detected a \
23f87bed 3386missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
01c52d31
MB
3387 (gnus-agent-append-to-list
3388 tail-alist
3389 (cons article-number fetch-date)))
3390
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))
58090a8d 3396 'read) ;; never fetched article (may expire
01c52d31
MB
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.
3406
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)))
3413
3414 ;; I found some reason to expire this entry.
3415
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)
3428 )
3429
3430 (when marker
3431 (push "NOV entry removed" actions)
3432
3433 (goto-char (if (markerp marker)
3434 marker
3435 (- marker position-offset)))
3436
3437 (incf nov-entries-deleted)
3438
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)))
3445
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 preceed 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 \
23f87bed
MB
3456article alist" type) actions))
3457
01c52d31
MB
3458 (when actions
3459 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3460 decoded article-number
3461 (mapconcat 'identity actions ", ")))))
3462 (t
3463 (gnus-agent-message
3464 10 "gnus-agent-expire: %s:%d: Article kept as \
3465expiration tests failed." decoded article-number)
3466 (gnus-agent-append-to-list
3467 tail-alist (cons article-number fetch-date)))
3468 )
3469
3470 ;; Remove markers as I intend to reuse this buffer again.
3471 (when (and marker
3472 (markerp marker))
3473 (set-marker marker nil))
3474
3475 (setq dlist (cdr dlist))))
3476
3477 (setq alist (cdr alist))
3478
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))
3483
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)))
3494
3495 (when (eq articles t)
3496 (gnus-summary-update-info))))
3497
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)))
3503
3504 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
23f87bed
MB
3505
3506(defun gnus-agent-expire (&optional articles group force)
3507 "Expire all old articles.
3508If you want to force expiring of certain articles, this function can
3509take ARTICLES, GROUP and FORCE parameters as well.
3510
3511The 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.
3515Setting GROUP will limit expiration to that group.
3516FORCE is equivalent to setting the expiration predicates to true."
df80b09f 3517 (interactive)
bf247b6e 3518
23f87bed
MB
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 \
66841476 3523articles in every agentized group? "))
23f87bed
MB
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)))
bf247b6e 3547
23f87bed
MB
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 (gnus-agent-expire-done-message))))))
3555
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)))
3566
3567 (format "Expiry recovered %d NOV entries, deleted %d files,\
bf247b6e
KS
3568 and freed %f %s."
3569 (nth 0 stats)
3570 (nth 1 stats)
23f87bed
MB
3571 size (car units)))
3572 "Expiry...done"))
3573
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))
01c52d31
MB
3582 dir
3583 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
3584
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))))))
3593
3594 (let* (to-remove
3595 checker
3596 (checker
3597 (function
3598 (lambda (d)
bf247b6e
KS
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."
23f87bed
MB
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)
bf247b6e 3609 ((equal file ".overview")
23f87bed
MB
3610 ;; Directory must contain .overview to be
3611 ;; agent's cache of a group.
3612 (let ((d (file-name-as-directory d))
3613 r)
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 it's already in to-remove, add it to
bf247b6e 3622 ;; to-remove.
23f87bed
MB
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))
3629
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\
655591a7 3634 not currently required by any agentized group. Do you wish to consider\
23f87bed
MB
3635 deleting them?")))
3636 (while to-remove
3637 (let ((dir (pop to-remove)))
3638 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3639 (let* (delete-recursive
01c52d31 3640 files f
23f87bed
MB
3641 (delete-recursive
3642 (function
3643 (lambda (f-or-d)
3644 (ignore-errors
3645 (if (file-directory-p f-or-d)
3646 (condition-case nil
3647 (delete-directory f-or-d)
3648 (file-error
01c52d31
MB
3649 (setq files (directory-files f-or-d))
3650 (while files
3651 (setq f (pop files))
3652 (or (member f '("." ".."))
3653 (funcall delete-recursive
3654 (nnheader-concat
3655 f-or-d f))))
23f87bed
MB
3656 (delete-directory f-or-d)))
3657 (delete-file f-or-d)))))))
3658 (funcall delete-recursive dir))))))))))
df80b09f
LMI
3659
3660;;;###autoload
3661(defun gnus-agent-batch ()
23f87bed 3662 "Start Gnus, send queue and fetch session."
df80b09f
LMI
3663 (interactive)
3664 (let ((init-file-user "")
3665 (gnus-always-read-dribble-file t))
3666 (gnus))
23f87bed
MB
3667 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3668 (gnus-group-send-queue)
3669 (gnus-agent-fetch-session)))
3670
3671(defun gnus-agent-unread-articles (group)
3672 (let* ((read (gnus-info-read (gnus-get-info group)))
3673 (known (gnus-agent-load-alist group))
3674 (unread (list nil))
3675 (tail-unread unread))
3676 (while (and known read)
3677 (let ((candidate (car (pop known))))
3678 (while (let* ((range (car read))
3679 (min (if (numberp range) range (car range)))
3680 (max (if (numberp range) range (cdr range))))
3681 (cond ((or (not min)
3682 (< candidate min))
3683 (gnus-agent-append-to-list tail-unread candidate)
3684 nil)
3685 ((> candidate max)
3686 (setq read (cdr read))
3687 ;; return t so that I always loop one more
3688 ;; time. If I just iterated off the end of
3689 ;; read, min will become nil and the current
3690 ;; candidate will be added to the unread list.
3691 t))))))
3692 (while known
3693 (gnus-agent-append-to-list tail-unread (car (pop known))))
3694 (cdr unread)))
3695
3696(defun gnus-agent-uncached-articles (articles group &optional cached-header)
3697 "Restrict ARTICLES to numbers already fetched.
54506618 3698Returns a sublist of ARTICLES that excludes those article ids in GROUP
23f87bed
MB
3699that have already been fetched.
3700If CACHED-HEADER is nil, articles are only excluded if the article itself
3701has been fetched."
3702
3703 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3704 ;; 'car gnus-agent-article-alist))
3705
3706 ;; Functionally, I don't need to construct a temp list using mapcar.
3707
3708 (if (and (or gnus-agent-cache (not gnus-plugged))
3709 (gnus-agent-load-alist group))
3710 (let* ((ref gnus-agent-article-alist)
3711 (arts articles)
3712 (uncached (list nil))
3713 (tail-uncached uncached))
3714 (while (and ref arts)
3715 (let ((v1 (car arts))
3716 (v2 (caar ref)))
3717 (cond ((< v1 v2) ; v1 does not appear in the reference list
3718 (gnus-agent-append-to-list tail-uncached v1)
3719 (setq arts (cdr arts)))
3720 ((= v1 v2)
3721 (unless (or cached-header (cdar ref)) ; v1 is already cached
3722 (gnus-agent-append-to-list tail-uncached v1))
3723 (setq arts (cdr arts))
3724 (setq ref (cdr ref)))
3725 (t ; reference article (v2) preceeds the list being filtered
3726 (setq ref (cdr ref))))))
3727 (while arts
3728 (gnus-agent-append-to-list tail-uncached (pop arts)))
3729 (cdr uncached))
3730 ;; if gnus-agent-load-alist fails, no articles are cached.
3731 articles))
3732
3733(defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3734 (save-excursion
3735 (gnus-agent-create-buffer)
3736 (let ((gnus-decode-encoded-word-function 'identity)
5cedca8d 3737 (gnus-decode-encoded-address-function 'identity)
23f87bed 3738 (file (gnus-agent-article-name ".overview" group))
01c52d31
MB
3739 cached-articles uncached-articles
3740 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
3741 (gnus-make-directory (nnheader-translate-file-chars
3742 (file-name-directory file) t))
3743
3744 ;; Populate temp buffer with known headers
3745 (when (file-exists-p file)
3746 (with-current-buffer gnus-agent-overview-buffer
3747 (erase-buffer)
3748 (let ((nnheader-file-coding-system
3749 gnus-agent-file-coding-system))
3750 (nnheader-insert-nov-file file (car articles)))))
3751
3752 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3753 t))
3754 (progn
3755 ;; Populate nntp-server-buffer with uncached headers
3756 (set-buffer nntp-server-buffer)
3757 (erase-buffer)
3758 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3759 (gnus-retrieve-headers
3760 uncached-articles group fetch-old))))
3761 (nnvirtual-convert-headers))
3762 ((eq 'nntp (car gnus-current-select-method))
3763 ;; The author of gnus-get-newsgroup-headers-xover
3764 ;; reports that the XOVER command is commonly
3765 ;; unreliable. The problem is that recently
3766 ;; posted articles may not be entered into the
3767 ;; NOV database in time to respond to my XOVER
3768 ;; query.
3769 ;;
3770 ;; I'm going to use his assumption that the NOV
3771 ;; database is updated in order of ascending
3772 ;; article ID. Therefore, a response containing
3773 ;; article ID N implies that all articles from 1
3774 ;; to N-1 are up-to-date. Therefore, missing
3775 ;; articles in that range have expired.
3776
3777 (set-buffer nntp-server-buffer)
3778 (let* ((fetched-articles (list nil))
3779 (tail-fetched-articles fetched-articles)
3780 (min (cond ((numberp fetch-old)
3781 (max 1 (- (car articles) fetch-old)))
3782 (fetch-old
3783 1)
3784 (t
3785 (car articles))))
3786 (max (car (last articles))))
3787
3788 ;; Get the list of articles that were fetched
3789 (goto-char (point-min))
54506618
MB
3790 (let ((pm (point-max))
3791 art)
23f87bed 3792 (while (< (point) pm)
54506618
MB
3793 (when (setq art (gnus-agent-read-article-number))
3794 (gnus-agent-append-to-list tail-fetched-articles art))
23f87bed
MB
3795 (forward-line 1)))
3796
3797 ;; Clip this list to the headers that will
3798 ;; actually be returned
3799 (setq fetched-articles (gnus-list-range-intersection
3800 (cdr fetched-articles)
3801 (cons min max)))
3802
3803 ;; Clip the uncached articles list to exclude
3804 ;; IDs after the last FETCHED header. The
3805 ;; excluded IDs may be fetchable using HEAD.
3806 (if (car tail-fetched-articles)
3807 (setq uncached-articles
3808 (gnus-list-range-intersection
3809 uncached-articles
3810 (cons (car uncached-articles)
3811 (car tail-fetched-articles)))))
3812
3813 ;; Create the list of articles that were
3814 ;; "successfully" fetched. Success, in this
3815 ;; case, means that the ID should not be
3816 ;; fetched again. In the case of an expired
3817 ;; article, the header will not be fetched.
3818 (setq uncached-articles
3819 (gnus-sorted-nunion fetched-articles
3820 uncached-articles))
3821 )))
3822
3823 ;; Erase the temp buffer
3824 (set-buffer gnus-agent-overview-buffer)
3825 (erase-buffer)
3826
3827 ;; Copy the nntp-server-buffer to the temp buffer
3828 (set-buffer nntp-server-buffer)
3829 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3830
54506618
MB
3831 ;; Merge the temp buffer with the known headers (found on
3832 ;; disk in FILE) into the nntp-server-buffer
3833 (when uncached-articles
23f87bed
MB
3834 (gnus-agent-braid-nov group uncached-articles file))
3835
54506618 3836 ;; Save the new set of known headers to FILE
23f87bed
MB
3837 (set-buffer nntp-server-buffer)
3838 (let ((coding-system-for-write
3839 gnus-agent-file-coding-system))
3840 (gnus-agent-check-overview-buffer)
3841 (write-region (point-min) (point-max) file nil 'silent))
3842
01c52d31
MB
3843 (gnus-agent-update-view-total-fetched-for group t)
3844
23f87bed
MB
3845 ;; Update the group's article alist to include the newly
3846 ;; fetched articles.
3847 (gnus-agent-load-alist group)
3848 (gnus-agent-save-alist group uncached-articles nil)
3849 )
3850
3851 ;; Copy the temp buffer to the nntp-server-buffer
3852 (set-buffer nntp-server-buffer)
3853 (erase-buffer)
3854 (insert-buffer-substring gnus-agent-overview-buffer)))
3855
3856 (if (and fetch-old
3857 (not (numberp fetch-old)))
3858 t ; Don't remove anything.
3859 (nnheader-nov-delete-outside-range
3860 (if fetch-old (max 1 (- (car articles) fetch-old))
3861 (car articles))
3862 (car (last articles)))
3863 t)
3864
3865 'nov))
3866
3867(defun gnus-agent-request-article (article group)
3868 "Retrieve ARTICLE in GROUP from the agent cache."
3869 (when (and gnus-agent
3870 (or gnus-agent-cache
3871 (not gnus-plugged))
3872 (numberp article))
3873 (let* ((gnus-command-method (gnus-find-method-for-group group))
3874 (file (gnus-agent-article-name (number-to-string article) group))
01c52d31
MB
3875 (buffer-read-only nil)
3876 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
3877 (when (and (file-exists-p file)
3878 (> (nth 7 (file-attributes file)) 0))
3879 (erase-buffer)
3880 (gnus-kill-all-overlays)
3881 (let ((coding-system-for-read gnus-cache-coding-system))
3882 (insert-file-contents file))
3883 t))))
3884
3885(defun gnus-agent-regenerate-group (group &optional reread)
3886 "Regenerate GROUP.
3887If REREAD is t, all articles in the .overview are marked as unread.
3888If REREAD is a list, the specified articles will be marked as unread.
3889In addition, their NOV entries in .overview will be refreshed using
3890the articles' current headers.
3891If REREAD is not nil, downloaded articles are marked as unread."
3892 (interactive
01c52d31 3893 (list (gnus-agent-read-group)
23f87bed
MB
3894 (catch 'mark
3895 (while (let (c
3896 (cursor-in-echo-area t)
3897 (echo-keystrokes 0))
3898 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3899 (setq c (read-char-exclusive))
3900
3901 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3902 (throw 'mark nil))
3903 ((or (eq c ?a) (eq c ?A))
3904 (throw 'mark t))
3905 ((or (eq c ?d) (eq c ?D))
3906 (throw 'mark 'some)))
3907 (gnus-message 3 "Ignoring unexpected input")
3908 (sit-for 1)
3909 t)))))
23f87bed 3910 (when group
01c52d31
MB
3911 (gnus-message 5 "Regenerating in %s" group)
3912 (let* ((gnus-command-method (or gnus-command-method
3913 (gnus-find-method-for-group group)))
3914 (file (gnus-agent-article-name ".overview" group))
3915 (dir (file-name-directory file))
3916 point
3917 (file-name-coding-system nnmail-pathname-coding-system)
3918 (downloaded (if (file-exists-p dir)
655591a7 3919 (sort (delq nil (mapcar (lambda (name)
26c9afc3
MB
3920 (and (not (file-directory-p (nnheader-concat dir name)))
3921 (string-to-number name)))
3922 (directory-files dir nil "^[0-9]+$" t)))
01c52d31
MB
3923 '>)
3924 (progn (gnus-make-directory dir) nil)))
3925 dl nov-arts
3926 alist header
3927 regenerated)
3928
3929 (mm-with-unibyte-buffer
3930 (if (file-exists-p file)
3931 (let ((nnheader-file-coding-system
3932 gnus-agent-file-coding-system))
3933 (nnheader-insert-file-contents file)))
3934 (set-buffer-modified-p nil)
3935
3936 ;; Load the article IDs found in the overview file. As a
3937 ;; side-effect, validate the file contents.
3938 (let ((load t))
3939 (while load
3940 (setq load nil)
3941 (goto-char (point-min))
3942 (while (< (point) (point-max))
3943 (cond ((and (looking-at "[0-9]+\t")
3944 (<= (- (match-end 0) (match-beginning 0)) 9))
3945 (push (read (current-buffer)) nov-arts)
3946 (forward-line 1)
3947 (let ((l1 (car nov-arts))
3948 (l2 (cadr nov-arts)))
3949 (cond ((and (listp reread) (memq l1 reread))
3950 (gnus-delete-line)
3951 (setq nov-arts (cdr nov-arts))
3952 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
54506618 3953 entry of article %s deleted." l1))
01c52d31
MB
3954 ((not l2)
3955 nil)
3956 ((< l1 l2)
3957 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
23f87bed 3958 entries are NOT in ascending order.")
01c52d31
MB
3959 ;; Don't sort now as I haven't verified
3960 ;; that every line begins with a number
3961 (setq load t))
3962 ((= l1 l2)
3963 (forward-line -1)
3964 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3965 entries contained duplicate of article %s. Duplicate deleted." l1)
3966 (gnus-delete-line)
3967 (setq nov-arts (cdr nov-arts))))))
3968 (t
3969 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
23f87bed
MB
3970 entries contained line that did not begin with an article number. Deleted\
3971 line.")
01c52d31
MB
3972 (gnus-delete-line))))
3973 (when load
3974 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
23f87bed 3975 entries into ascending order.")
01c52d31
MB
3976 (sort-numeric-fields 1 (point-min) (point-max))
3977 (setq nov-arts nil))))
3978 (gnus-agent-check-overview-buffer)
3979
3980 ;; Construct a new article alist whose nodes match every header
3981 ;; in the .overview file. As a side-effect, missing headers are
3982 ;; reconstructed from the downloaded article file.
3983 (while (or downloaded nov-arts)
3984 (cond ((and downloaded
3985 (or (not nov-arts)
3986 (> (car downloaded) (car nov-arts))))
3987 ;; This entry is missing from the overview file
3988 (gnus-message 3 "Regenerating NOV %s %d..." group
3989 (car downloaded))
3990 (let ((file (concat dir (number-to-string (car downloaded)))))
3991 (mm-with-unibyte-buffer
3992 (nnheader-insert-file-contents file)
3993 (nnheader-remove-body)
3994 (setq header (nnheader-parse-naked-head)))
3995 (mail-header-set-number header (car downloaded))
3996 (if nov-arts
3997 (let ((key (concat "^" (int-to-string (car nov-arts))
3998 "\t")))
3999 (or (re-search-backward key nil t)
4000 (re-search-forward key))
4001 (forward-line 1))
4002 (goto-char (point-min)))
4003 (nnheader-insert-nov header))
4004 (setq nov-arts (cons (car downloaded) nov-arts)))
4005 ((eq (car downloaded) (car nov-arts))
4006 ;; This entry in the overview has been downloaded
4007 (push (cons (car downloaded)
4008 (time-to-days
4009 (nth 5 (file-attributes
4010 (concat dir (number-to-string
4011 (car downloaded))))))) alist)
4012 (setq downloaded (cdr downloaded))
4013 (setq nov-arts (cdr nov-arts)))
4014 (t
4015 ;; This entry in the overview has not been downloaded
4016 (push (cons (car nov-arts) nil) alist)
4017 (setq nov-arts (cdr nov-arts)))))
4018
4019 ;; When gnus-agent-consider-all-articles is set,
4020 ;; gnus-agent-regenerate-group should NOT remove article IDs from
4021 ;; the alist. Those IDs serve as markers to indicate that an
4022 ;; attempt has been made to fetch that article's header.
4023
4024 ;; When gnus-agent-consider-all-articles is NOT set,
4025 ;; gnus-agent-regenerate-group can remove the article ID of every
4026 ;; article (with the exception of the last ID in the list - it's
4027 ;; special) that no longer appears in the overview. In this
4028 ;; situtation, the last article ID in the list implies that it,
4029 ;; and every article ID preceeding it, have been fetched from the
4030 ;; server.
4031
4032 (if gnus-agent-consider-all-articles
4033 ;; Restore all article IDs that were not found in the overview file.
4034 (let* ((n (cons nil alist))
4035 (merged n)
4036 (o (gnus-agent-load-alist group)))
4037 (while o
4038 (let ((nID (caadr n))
4039 (oID (caar o)))
4040 (cond ((not nID)
4041 (setq n (setcdr n (list (list oID))))
4042 (setq o (cdr o)))
4043 ((< oID nID)
4044 (setcdr n (cons (list oID) (cdr n)))
4045 (setq o (cdr o)))
4046 ((= oID nID)
4047 (setq o (cdr o))
4048 (setq n (cdr n)))
4049 (t
4050 (setq n (cdr n))))))
4051 (setq alist (cdr merged)))
4052 ;; Restore the last article ID if it is not already in the new alist
4053 (let ((n (last alist))
4054 (o (last (gnus-agent-load-alist group))))
4055 (cond ((not o)
4056 nil)
4057 ((not n)
4058 (push (cons (caar o) nil) alist))
4059 ((< (caar n) (caar o))
4060 (setcdr n (list (car o)))))))
4061
4062 (let ((inhibit-quit t))
4063 (if (setq regenerated (buffer-modified-p))
4064 (let ((coding-system-for-write gnus-agent-file-coding-system))
4065 (write-region (point-min) (point-max) file nil 'silent)))
4066
4067 (setq regenerated (or regenerated
4068 (and reread gnus-agent-article-alist)
4069 (not (equal alist gnus-agent-article-alist))))
4070
4071 (setq gnus-agent-article-alist alist)
4072
4073 (when regenerated
4074 (gnus-agent-save-alist group)
4075
4076 ;; I have to alter the group's active range NOW as
4077 ;; gnus-make-ascending-articles-unread will use it to
4078 ;; recalculate the number of unread articles in the group
4079
4080 (let ((group (gnus-group-real-name group))
4081 (group-active (or (gnus-active group)
4082 (gnus-activate-group group))))
4083 (gnus-agent-possibly-alter-active group group-active)))))
4084
4085 (when (and reread gnus-agent-article-alist)
655591a7 4086 (gnus-agent-synchronize-group-flags
01c52d31 4087 group
58090a8d 4088 (list (list
01c52d31
MB
4089 (if (listp reread)
4090 reread
4091 (delq nil (mapcar (function (lambda (c)
4092 (cond ((eq reread t)
4093 (car c))
4094 ((cdr c)
4095 (car c)))))
58090a8d
MB
4096 gnus-agent-article-alist)))
4097 'del '(read)))
4098 gnus-command-method)
23f87bed 4099
01c52d31
MB
4100 (when regenerated
4101 (gnus-agent-update-files-total-fetched-for group nil)))
23f87bed 4102
01c52d31
MB
4103 (gnus-message 5 "")
4104 regenerated)))
23f87bed
MB
4105
4106;;;###autoload
4107(defun gnus-agent-regenerate (&optional clean reread)
4108 "Regenerate all agent covered files.
4109If CLEAN, obsolete (ignore)."
4110 (interactive "P")
4111 (let (regenerated)
4112 (gnus-message 4 "Regenerating Gnus agent files...")
4113 (dolist (gnus-command-method (gnus-agent-covered-methods))
4114 (dolist (group (gnus-groups-from-server gnus-command-method))
4115 (setq regenerated (or (gnus-agent-regenerate-group group reread)
4116 regenerated))))
4117 (gnus-message 4 "Regenerating Gnus agent files...done")
4118
4119 regenerated))
4120
4121(defun gnus-agent-go-online (&optional force)
4122 "Switch servers into online status."
4123 (interactive (list t))
4124 (dolist (server gnus-opened-servers)
4125 (when (eq (nth 1 server) 'offline)
4126 (if (if (eq force 'ask)
4127 (gnus-y-or-n-p
4128 (format "Switch %s:%s into online status? "
4129 (caar server) (cadar server)))
4130 force)
4131 (setcar (nthcdr 1 server) 'close)))))
4132
4133(defun gnus-agent-toggle-group-plugged (group)
4134 "Toggle the status of the server of the current group."
4135 (interactive (list (gnus-group-group-name)))
4136 (let* ((method (gnus-find-method-for-group group))
4137 (status (cadr (assoc method gnus-opened-servers))))
4138 (if (eq status 'offline)
4139 (gnus-server-set-status method 'closed)
4140 (gnus-close-server method)
4141 (gnus-server-set-status method 'offline))
4142 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4143 (if (eq status 'offline) 'offline 'online)
4144 (if (eq status 'offline) 'online 'offline))))
4145
4146(defun gnus-agent-group-covered-p (group)
4147 (gnus-agent-method-p (gnus-group-method group)))
4148
01c52d31
MB
4149(defun gnus-agent-update-files-total-fetched-for
4150 (group delta &optional method path)
4151 "Update, or set, the total disk space used by the articles that the
4152agent has fetched."
4153 (when gnus-agent-total-fetched-hashtb
4154 (gnus-agent-with-refreshed-group
4155 group
4156 ;; if null, gnus-agent-group-pathname will calc method.
4157 (let* ((gnus-command-method method)
4158 (path (or path (gnus-agent-group-pathname group)))
4159 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4160 (gnus-sethash path (make-list 3 0)
4161 gnus-agent-total-fetched-hashtb)))
4162 (file-name-coding-system nnmail-pathname-coding-system))
4163 (when (listp delta)
4164 (if delta
4165 (let ((sum 0.0)
4166 file)
4167 (while (setq file (pop delta))
4168 (incf sum (float (or (nth 7 (file-attributes
4169 (nnheader-concat
4170 path
4171 (if (numberp file)
4172 (number-to-string file)
4173 file)))) 0))))
4174 (setq delta sum))
4175 (let ((sum (- (nth 2 entry)))
4176 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4177 file)
4178 (while (setq file (pop info))
4179 (incf sum (float (or (nth 8 file) 0))))
4180 (setq delta sum))))
4181
4182 (setq gnus-agent-need-update-total-fetched-for t)
4183 (incf (nth 2 entry) delta)))))
4184
4185(defun gnus-agent-update-view-total-fetched-for
4186 (group agent-over &optional method path)
4187 "Update, or set, the total disk space used by the .agentview and
4188.overview files. These files are calculated separately as they can be
4189modified."
4190 (when gnus-agent-total-fetched-hashtb
4191 (gnus-agent-with-refreshed-group
4192 group
4193 ;; if null, gnus-agent-group-pathname will calc method.
4194 (let* ((gnus-command-method method)
4195 (path (or path (gnus-agent-group-pathname group)))
4196 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4197 (gnus-sethash path (make-list 3 0)
4198 gnus-agent-total-fetched-hashtb)))
4199 (file-name-coding-system nnmail-pathname-coding-system)
4200 (size (or (nth 7 (file-attributes
4201 (nnheader-concat
4202 path (if agent-over
4203 ".overview"
4204 ".agentview"))))
4205 0)))
4206 (setq gnus-agent-need-update-total-fetched-for t)
4207 (setf (nth (if agent-over 1 0) entry) size)))))
4208
4209(defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4210 "Get the total disk space used by the specified GROUP."
4211 (unless (equal group "dummy.group")
4212 (unless gnus-agent-total-fetched-hashtb
4213 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4214
4215 ;; if null, gnus-agent-group-pathname will calc method.
4216 (let* ((gnus-command-method method)
4217 (path (gnus-agent-group-pathname group))
4218 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4219 (if entry
4220 (apply '+ entry)
4221 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4222 (+
4223 (gnus-agent-update-view-total-fetched-for group nil method path)
4224 (gnus-agent-update-view-total-fetched-for group t method path)
4225 (gnus-agent-update-files-total-fetched-for group nil method path)))))))
4226
df80b09f
LMI
4227(provide 'gnus-agent)
4228
cbee283d 4229;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
df80b09f 4230;;; gnus-agent.el ends here