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