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