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