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