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