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