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