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