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