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