(dired-mark-prompt): Don't count/display the t element.
[emacs.git] / lisp / gnus / gnus-agent.el
blobb373039cfc632a133f2bd3c7ca05b2cffec81572
1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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-newsgroup-maximum-articles
1769 (let ((active (gnus-active group)))
1770 (gnus-uncompress-range
1771 (cons (max (car active)
1772 (- (cdr active)
1773 gnus-newsgroup-maximum-articles
1774 -1))
1775 (cdr active))))
1776 (gnus-uncompress-range (gnus-active group)))
1777 (gnus-list-of-unread-articles group)))
1778 (gnus-decode-encoded-word-function 'identity)
1779 (gnus-decode-encoded-address-function 'identity)
1780 (file (gnus-agent-article-name ".overview" group)))
1782 (unless fetch-all
1783 ;; Add articles with marks to the list of article headers we want to
1784 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1785 ;; mark, but do fetch recent or seen articles if they have other, more
1786 ;; interesting marks. (We have to fetch articles with boring marks
1787 ;; because otherwise the agent will remove their marks.)
1788 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1789 (unless (memq (car arts) '(seen recent killed cache))
1790 (setq articles (gnus-range-add articles (cdr arts)))))
1791 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1793 ;; At this point, I have the list of articles to consider for
1794 ;; fetching. This is the list that I'll return to my caller. Some
1795 ;; of these articles may have already been fetched. That's OK as
1796 ;; the fetch article code will filter those out. Internally, I'll
1797 ;; filter this list to just those articles whose headers need to
1798 ;; be fetched.
1799 (let ((articles articles))
1800 ;; Remove known articles.
1801 (when (and (or gnus-agent-cache
1802 (not gnus-plugged))
1803 (gnus-agent-load-alist group))
1804 ;; Remove articles marked as downloaded.
1805 (if fetch-all
1806 ;; I want to fetch all headers in the active range.
1807 ;; Therefore, exclude only those headers that are in the
1808 ;; article alist.
1809 ;; NOTE: This is probably NOT what I want to do after
1810 ;; agent expiration in this group.
1811 (setq articles (gnus-agent-uncached-articles articles group))
1813 ;; I want to only fetch those headers that have never been
1814 ;; fetched. Therefore, exclude all headers that are, or
1815 ;; WERE, in the article alist.
1816 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1817 (high (cdr (gnus-active group))))
1818 ;; Low can be greater than High when the same group is
1819 ;; fetched twice in the same session {The first fetch will
1820 ;; fill the article alist such that (last
1821 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1822 ;; group))}. The addition of one(the 1+ above) then
1823 ;; forces Low to be greater than High. When this happens,
1824 ;; gnus-list-range-intersection returns nil which
1825 ;; indicates that no headers need to be fetched. -- Kevin
1826 (setq articles (gnus-list-range-intersection
1827 articles (list (cons low high)))))))
1829 (gnus-message
1830 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1831 (gnus-compress-sequence articles t))
1833 (save-excursion
1834 (set-buffer nntp-server-buffer)
1836 (if articles
1837 (progn
1838 (gnus-message 7 "Fetching headers for %s..." group)
1840 ;; Fetch them.
1841 (gnus-make-directory (nnheader-translate-file-chars
1842 (file-name-directory file) t))
1844 (unless (eq 'nov (gnus-retrieve-headers articles group))
1845 (nnvirtual-convert-headers))
1846 (gnus-agent-check-overview-buffer)
1847 ;; Move these headers to the overview buffer so that
1848 ;; gnus-agent-braid-nov can merge them with the contents
1849 ;; of FILE.
1850 (copy-to-buffer
1851 gnus-agent-overview-buffer (point-min) (point-max))
1852 ;; NOTE: Call g-a-brand-nov even when the file does not
1853 ;; exist. As a minimum, it will validate the article
1854 ;; numbers already in the buffer.
1855 (gnus-agent-braid-nov group articles file)
1856 (let ((coding-system-for-write
1857 gnus-agent-file-coding-system))
1858 (gnus-agent-check-overview-buffer)
1859 (write-region (point-min) (point-max) file nil 'silent))
1860 (gnus-agent-save-alist group articles nil)
1861 articles)
1862 (ignore-errors
1863 (erase-buffer)
1864 (nnheader-insert-file-contents file)))))
1865 articles))
1867 (defsubst gnus-agent-read-article-number ()
1868 "Reads the article number at point. Returns nil when a valid article number can not be read."
1870 ;; It is unfortunate but the read function quietly overflows
1871 ;; integer. As a result, I have to use string operations to test
1872 ;; for overflow BEFORE calling read.
1873 (when (looking-at "[0-9]+\t")
1874 (let ((len (- (match-end 0) (match-beginning 0))))
1875 (cond ((< len 9)
1876 (read (current-buffer)))
1877 ((= len 9)
1878 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1879 ;; Back convert from int to string to ensure that this is one of them.
1880 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1881 (num (read (current-buffer)))
1882 (str2 (int-to-string num)))
1883 (when (equal str1 str2)
1884 num)))))))
1886 (defsubst gnus-agent-copy-nov-line (article)
1887 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1888 (let (art b e)
1889 (set-buffer gnus-agent-overview-buffer)
1890 (while (and (not (eobp))
1891 (or (not (setq art (gnus-agent-read-article-number)))
1892 (< art article)))
1893 (forward-line 1))
1894 (beginning-of-line)
1895 (if (or (eobp)
1896 (not (eq article art)))
1897 (set-buffer nntp-server-buffer)
1898 (setq b (point))
1899 (setq e (progn (forward-line 1) (point)))
1900 (set-buffer nntp-server-buffer)
1901 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1903 (defun gnus-agent-braid-nov (group articles file)
1904 "Merge agent overview data with given file.
1905 Takes unvalidated headers for ARTICLES from
1906 `gnus-agent-overview-buffer' and validated headers from the given
1907 FILE and places the combined valid headers into
1908 `nntp-server-buffer'. This function can be used, when file
1909 doesn't exist, to valid the overview buffer."
1910 (let (start last)
1911 (set-buffer gnus-agent-overview-buffer)
1912 (goto-char (point-min))
1913 (set-buffer nntp-server-buffer)
1914 (erase-buffer)
1915 (when (file-exists-p file)
1916 (nnheader-insert-file-contents file))
1917 (goto-char (point-max))
1918 (forward-line -1)
1920 (unless (or (= (point-min) (point-max))
1921 (< (setq last (read (current-buffer))) (car articles)))
1922 ;; Old and new overlap -- We do it the hard way.
1923 (when (nnheader-find-nov-line (car articles))
1924 ;; Replacing existing NOV entry
1925 (delete-region (point) (progn (forward-line 1) (point))))
1926 (gnus-agent-copy-nov-line (pop articles))
1928 (ignore-errors
1929 (while articles
1930 (while (let ((art (read (current-buffer))))
1931 (cond ((< art (car articles))
1932 (forward-line 1)
1934 ((= art (car articles))
1935 (beginning-of-line)
1936 (delete-region
1937 (point) (progn (forward-line 1) (point)))
1938 nil)
1940 (beginning-of-line)
1941 nil))))
1943 (gnus-agent-copy-nov-line (pop articles)))))
1945 (goto-char (point-max))
1947 ;; Append the remaining lines
1948 (when articles
1949 (when last
1950 (set-buffer gnus-agent-overview-buffer)
1951 (setq start (point))
1952 (set-buffer nntp-server-buffer))
1954 (let ((p (point)))
1955 (insert-buffer-substring gnus-agent-overview-buffer start)
1956 (goto-char p))
1958 (setq last (or last -134217728))
1959 (while (catch 'problems
1960 (let (sort art)
1961 (while (not (eobp))
1962 (setq art (gnus-agent-read-article-number))
1963 (cond ((not art)
1964 ;; Bad art num - delete this line
1965 (beginning-of-line)
1966 (delete-region (point) (progn (forward-line 1) (point))))
1967 ((< art last)
1968 ;; Art num out of order - enable sort
1969 (setq sort t)
1970 (forward-line 1))
1971 ((= art last)
1972 ;; Bad repeat of art number - delete this line
1973 (beginning-of-line)
1974 (delete-region (point) (progn (forward-line 1) (point))))
1976 ;; Good art num
1977 (setq last art)
1978 (forward-line 1))))
1979 (when sort
1980 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
1981 ;; First, we'll fix the sort.
1982 (sort-numeric-fields 1 (point-min) (point-max))
1984 ;; but now we have to consider that we may have duplicate rows...
1985 ;; so reset to beginning of file
1986 (goto-char (point-min))
1987 (setq last -134217728)
1989 ;; and throw a code that restarts this scan
1990 (throw 'problems t))
1991 nil))))))
1993 ;; Keeps the compiler from warning about the free variable in
1994 ;; gnus-agent-read-agentview.
1995 (eval-when-compile
1996 (defvar gnus-agent-read-agentview))
1998 (defun gnus-agent-load-alist (group)
1999 "Load the article-state alist for GROUP."
2000 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
2001 (let ((gnus-agent-read-agentview group))
2002 (setq gnus-agent-article-alist
2003 (gnus-cache-file-contents
2004 (gnus-agent-article-name ".agentview" group)
2005 'gnus-agent-file-loading-cache
2006 'gnus-agent-read-agentview))))
2008 (defun gnus-agent-read-agentview (file)
2009 "Load FILE and do a `read' there."
2010 (with-temp-buffer
2011 (condition-case nil
2012 (progn
2013 (nnheader-insert-file-contents file)
2014 (goto-char (point-min))
2015 (let ((alist (read (current-buffer)))
2016 (version (condition-case nil (read (current-buffer))
2017 (end-of-file 0)))
2018 changed-version)
2020 (cond
2021 ((= version 0)
2022 (let ((inhibit-quit t)
2023 entry)
2024 (gnus-agent-open-history)
2025 (set-buffer (gnus-agent-history-buffer))
2026 (goto-char (point-min))
2027 (while (not (eobp))
2028 (if (and (looking-at
2029 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2030 (string= (match-string 2)
2031 gnus-agent-read-agentview)
2032 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2033 (setcdr entry (string-to-number (match-string 1))))
2034 (forward-line 1))
2035 (gnus-agent-close-history)
2036 (setq changed-version t)))
2037 ((= version 1)
2038 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2039 ((= version 2)
2040 (let (uncomp)
2041 (mapcar
2042 (lambda (comp-list)
2043 (let ((state (car comp-list))
2044 (sequence (inline
2045 (gnus-uncompress-range
2046 (cdr comp-list)))))
2047 (mapcar (lambda (article-id)
2048 (setq uncomp (cons (cons article-id state) uncomp)))
2049 sequence)))
2050 alist)
2051 (setq alist (sort uncomp 'car-less-than-car)))
2052 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2053 (when changed-version
2054 (let ((gnus-agent-article-alist alist))
2055 (gnus-agent-save-alist gnus-agent-read-agentview)))
2056 alist))
2057 (file-error nil))))
2059 (defun gnus-agent-save-alist (group &optional articles state)
2060 "Save the article-state alist for GROUP."
2061 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2062 (prev (cons nil gnus-agent-article-alist))
2063 (all prev)
2064 print-level print-length item article)
2065 (while (setq article (pop articles))
2066 (while (and (cdr prev)
2067 (< (caadr prev) article))
2068 (setq prev (cdr prev)))
2069 (cond
2070 ((not (cdr prev))
2071 (setcdr prev (list (cons article state))))
2072 ((> (caadr prev) article)
2073 (setcdr prev (cons (cons article state) (cdr prev))))
2074 ((= (caadr prev) article)
2075 (setcdr (cadr prev) state)))
2076 (setq prev (cdr prev)))
2077 (setq gnus-agent-article-alist (cdr all))
2079 (gnus-agent-set-local group
2080 (caar gnus-agent-article-alist)
2081 (caar (last gnus-agent-article-alist)))
2083 (gnus-make-directory (gnus-agent-article-name "" group))
2084 (with-temp-file (gnus-agent-article-name ".agentview" group)
2085 (cond ((eq gnus-agent-article-alist-save-format 1)
2086 (princ gnus-agent-article-alist (current-buffer)))
2087 ((eq gnus-agent-article-alist-save-format 2)
2088 (let ((compressed nil))
2089 (mapcar (lambda (pair)
2090 (let* ((article-id (car pair))
2091 (day-of-download (cdr pair))
2092 (comp-list (assq day-of-download compressed)))
2093 (if comp-list
2094 (setcdr comp-list
2095 (cons article-id (cdr comp-list)))
2096 (setq compressed
2097 (cons (list day-of-download article-id)
2098 compressed)))
2099 nil)) gnus-agent-article-alist)
2100 (mapcar (lambda (comp-list)
2101 (setcdr comp-list
2102 (gnus-compress-sequence
2103 (nreverse (cdr comp-list)))))
2104 compressed)
2105 (princ compressed (current-buffer)))))
2106 (insert "\n")
2107 (princ gnus-agent-article-alist-save-format (current-buffer))
2108 (insert "\n"))))
2110 (defvar gnus-agent-article-local nil)
2111 (defvar gnus-agent-file-loading-local nil)
2113 (defun gnus-agent-load-local (&optional method)
2114 "Load the METHOD'S local file. The local file contains min/max
2115 article counts for each of the method's subscribed groups."
2116 (let ((gnus-command-method (or method gnus-command-method)))
2117 (setq gnus-agent-article-local
2118 (gnus-cache-file-contents
2119 (gnus-agent-lib-file "local")
2120 'gnus-agent-file-loading-local
2121 'gnus-agent-read-and-cache-local))))
2123 (defun gnus-agent-read-and-cache-local (file)
2124 "Load and read FILE then bind its contents to
2125 gnus-agent-article-local. If that variable had `dirty' (also known as
2126 modified) original contents, they are first saved to their own file."
2128 (if (and gnus-agent-article-local
2129 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2130 (gnus-agent-save-local))
2131 (gnus-agent-read-local file))
2133 (defun gnus-agent-read-local (file)
2134 "Load FILE and do a `read' there."
2135 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2136 (point-max))))
2137 (line 1))
2138 (with-temp-buffer
2139 (condition-case nil
2140 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2141 (nnheader-insert-file-contents file))
2142 (file-error))
2144 (goto-char (point-min))
2145 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2146 (while (= (following-char) ?\;)
2147 (forward-line 1)
2148 (setq line (1+ line)))
2150 (while (not (eobp))
2151 (condition-case err
2152 (let (group
2155 (cur (current-buffer))
2156 (obarray my-obarray))
2157 (setq group (read cur)
2158 min (read cur)
2159 max (read cur))
2161 (when (stringp group)
2162 (setq group (intern group my-obarray)))
2164 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2165 (set group (cons (+ 0 min) (+ 0 max))))
2166 (error
2167 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2168 file line (error-message-string err))))
2169 (forward-line 1)
2170 (setq line (1+ line))))
2172 (set (intern "+dirty" my-obarray) nil)
2173 (set (intern "+method" my-obarray) gnus-command-method)
2174 my-obarray))
2176 (defun gnus-agent-save-local (&optional force)
2177 "Save gnus-agent-article-local under it method's agent.lib directory."
2178 (let ((my-obarray gnus-agent-article-local))
2179 (when (and my-obarray
2180 (or force (symbol-value (intern "+dirty" my-obarray))))
2181 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2182 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2183 (dest (gnus-agent-lib-file "local")))
2184 (gnus-make-directory (gnus-agent-lib-file ""))
2186 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2187 (with-temp-file dest
2188 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2189 (file-name-coding-system nnmail-pathname-coding-system)
2190 print-level print-length item article
2191 (standard-output (current-buffer)))
2192 (mapatoms (lambda (symbol)
2193 (cond ((not (boundp symbol))
2194 nil)
2195 ((member (symbol-name symbol) '("+dirty" "+method"))
2196 nil)
2198 (let ((range (symbol-value symbol)))
2199 (when range
2200 (prin1 symbol)
2201 (princ " ")
2202 (princ (car range))
2203 (princ " ")
2204 (princ (cdr range))
2205 (princ "\n"))))))
2206 my-obarray))))))))
2208 (defun gnus-agent-get-local (group &optional gmane method)
2209 (let* ((gmane (or gmane (gnus-group-real-name group)))
2210 (gnus-command-method (or method (gnus-find-method-for-group group)))
2211 (local (gnus-agent-load-local))
2212 (symb (intern gmane local))
2213 (minmax (and (boundp symb) (symbol-value symb))))
2214 (unless minmax
2215 ;; Bind these so that gnus-agent-load-alist doesn't change the
2216 ;; current alist (i.e. gnus-agent-article-alist)
2217 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2218 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2219 (alist (gnus-agent-load-alist group)))
2220 (when alist
2221 (setq minmax
2222 (cons (caar alist)
2223 (caar (last alist))))
2224 (gnus-agent-set-local group (car minmax) (cdr minmax)
2225 gmane gnus-command-method local))))
2226 minmax))
2228 (defun gnus-agent-set-local (group min max &optional gmane method local)
2229 (let* ((gmane (or gmane (gnus-group-real-name group)))
2230 (gnus-command-method (or method (gnus-find-method-for-group group)))
2231 (local (or local (gnus-agent-load-local)))
2232 (symb (intern gmane local))
2233 (minmax (and (boundp symb) (symbol-value symb))))
2235 (if (cond ((and minmax
2236 (or (not (eq min (car minmax)))
2237 (not (eq max (cdr minmax))))
2239 max)
2240 (setcar minmax min)
2241 (setcdr minmax max)
2243 (minmax
2244 nil)
2245 ((and min max)
2246 (set symb (cons min max))
2249 (unintern symb local)))
2250 (set (intern "+dirty" local) t))))
2252 (defun gnus-agent-article-name (article group)
2253 (expand-file-name article
2254 (file-name-as-directory
2255 (gnus-agent-group-pathname group))))
2257 (defun gnus-agent-batch-confirmation (msg)
2258 "Show error message and return t."
2259 (gnus-message 1 msg)
2262 ;;;###autoload
2263 (defun gnus-agent-batch-fetch ()
2264 "Start Gnus and fetch session."
2265 (interactive)
2266 (gnus)
2267 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2268 (gnus-agent-fetch-session))
2269 (gnus-group-exit))
2271 (defun gnus-agent-fetch-session ()
2272 "Fetch all articles and headers that are eligible for fetching."
2273 (interactive)
2274 (unless gnus-agent-covered-methods
2275 (error "No servers are covered by the Gnus agent"))
2276 (unless gnus-plugged
2277 (error "Can't fetch articles while Gnus is unplugged"))
2278 (let ((methods (gnus-agent-covered-methods))
2279 groups group gnus-command-method)
2280 (save-excursion
2281 (while methods
2282 (setq gnus-command-method (car methods))
2283 (when (and (or (gnus-server-opened gnus-command-method)
2284 (gnus-open-server gnus-command-method))
2285 (gnus-online gnus-command-method))
2286 (setq groups (gnus-groups-from-server (car methods)))
2287 (gnus-agent-with-fetch
2288 (while (setq group (pop groups))
2289 (when (<= (gnus-group-level group)
2290 gnus-agent-handle-level)
2291 (if (or debug-on-error debug-on-quit)
2292 (gnus-agent-fetch-group-1
2293 group gnus-command-method)
2294 (condition-case err
2295 (gnus-agent-fetch-group-1
2296 group gnus-command-method)
2297 (error
2298 (unless (funcall gnus-agent-confirmation-function
2299 (format "Error %s while fetching session. Should gnus continue? "
2300 (error-message-string err)))
2301 (error "Cannot fetch articles into the Gnus agent")))
2302 (quit
2303 (gnus-agent-regenerate-group group)
2304 (unless (funcall gnus-agent-confirmation-function
2305 (format
2306 "%s while fetching session. Should gnus continue? "
2307 (error-message-string err)))
2308 (signal 'quit
2309 "Cannot fetch articles into the Gnus agent")))))))))
2310 (setq methods (cdr methods)))
2311 (gnus-run-hooks 'gnus-agent-fetched-hook)
2312 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2314 (defun gnus-agent-fetch-group-1 (group method)
2315 "Fetch GROUP."
2316 (let ((gnus-command-method method)
2317 (gnus-newsgroup-name group)
2318 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2319 (gnus-newsgroup-headers gnus-newsgroup-headers)
2320 (gnus-newsgroup-scored gnus-newsgroup-scored)
2321 (gnus-use-cache gnus-use-cache)
2322 (gnus-summary-expunge-below gnus-summary-expunge-below)
2323 (gnus-summary-mark-below gnus-summary-mark-below)
2324 (gnus-orphan-score gnus-orphan-score)
2325 ;; Maybe some other gnus-summary local variables should also
2326 ;; be put here.
2328 gnus-headers
2329 gnus-score
2330 articles arts
2331 category predicate info marks score-param
2333 (unless (gnus-check-group group)
2334 (error "Can't open server for %s" group))
2336 ;; Fetch headers.
2337 (when (or gnus-newsgroup-active
2338 (gnus-active group)
2339 (gnus-activate-group group))
2340 (let ((marked-articles gnus-newsgroup-downloadable))
2341 ;; Identify the articles marked for download
2342 (unless gnus-newsgroup-active
2343 ;; The variable gnus-newsgroup-active was selected as I need
2344 ;; a gnus-summary local variable that is NOT bound to any
2345 ;; value (its global value should default to nil).
2346 (dolist (mark gnus-agent-download-marks)
2347 (let ((arts (cdr (assq mark (gnus-info-marks
2348 (setq info (gnus-get-info group)))))))
2349 (when arts
2350 (setq marked-articles (nconc (gnus-uncompress-range arts)
2351 marked-articles))
2352 ))))
2353 (setq marked-articles (sort marked-articles '<))
2355 ;; Fetch any new articles from the server
2356 (setq articles (gnus-agent-fetch-headers group))
2358 ;; Merge new articles with marked
2359 (setq articles (sort (append marked-articles articles) '<))
2361 (when articles
2362 ;; Parse them and see which articles we want to fetch.
2363 (setq gnus-newsgroup-dependencies
2364 (or gnus-newsgroup-dependencies
2365 (make-vector (length articles) 0)))
2366 (setq gnus-newsgroup-headers
2367 (or gnus-newsgroup-headers
2368 (gnus-get-newsgroup-headers-xover articles nil nil
2369 group)))
2370 ;; `gnus-agent-overview-buffer' may be killed for
2371 ;; timeout reason. If so, recreate it.
2372 (gnus-agent-create-buffer)
2374 ;; Figure out how to select articles in this group
2375 (setq category (gnus-group-category group))
2377 (setq predicate
2378 (gnus-get-predicate
2379 (gnus-agent-find-parameter group 'agent-predicate)))
2381 ;; If the selection predicate requires scoring, score each header
2382 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2383 (let ((score-param
2384 (gnus-agent-find-parameter group 'agent-score-file)))
2385 ;; Translate score-param into real one
2386 (cond
2387 ((not score-param))
2388 ((eq score-param 'file)
2389 (setq score-param (gnus-all-score-files group)))
2390 ((stringp (car score-param)))
2392 (setq score-param (list (list score-param)))))
2393 (when score-param
2394 (gnus-score-headers score-param))))
2396 (unless (and (eq predicate 'gnus-agent-false)
2397 (not marked-articles))
2398 (let ((arts (list nil)))
2399 (let ((arts-tail arts)
2400 (alist (gnus-agent-load-alist group))
2401 (marked-articles marked-articles)
2402 (gnus-newsgroup-headers gnus-newsgroup-headers))
2403 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2404 (let ((num (mail-header-number gnus-headers)))
2405 ;; Determine if this article is already in the cache
2406 (while (and alist
2407 (> num (caar alist)))
2408 (setq alist (cdr alist)))
2410 (unless (and (eq num (caar alist))
2411 (cdar alist))
2413 ;; Determine if this article was marked for download.
2414 (while (and marked-articles
2415 (> num (car marked-articles)))
2416 (setq marked-articles
2417 (cdr marked-articles)))
2419 ;; When this article is marked, or selected by the
2420 ;; predicate, add it to the download list
2421 (when (or (eq num (car marked-articles))
2422 (let ((gnus-score
2423 (or (cdr
2424 (assq num gnus-newsgroup-scored))
2425 gnus-summary-default-score))
2426 (gnus-agent-long-article
2427 (gnus-agent-find-parameter
2428 group 'agent-long-article))
2429 (gnus-agent-short-article
2430 (gnus-agent-find-parameter
2431 group 'agent-short-article))
2432 (gnus-agent-low-score
2433 (gnus-agent-find-parameter
2434 group 'agent-low-score))
2435 (gnus-agent-high-score
2436 (gnus-agent-find-parameter
2437 group 'agent-high-score))
2438 (gnus-agent-expire-days
2439 (gnus-agent-find-parameter
2440 group 'agent-days-until-old)))
2441 (funcall predicate)))
2442 (gnus-agent-append-to-list arts-tail num))))))
2444 (let (fetched-articles)
2445 ;; Fetch all selected articles
2446 (setq gnus-newsgroup-undownloaded
2447 (gnus-sorted-ndifference
2448 gnus-newsgroup-undownloaded
2449 (setq fetched-articles
2450 (if (cdr arts)
2451 (gnus-agent-fetch-articles group (cdr arts))
2452 nil))))
2454 (let ((unfetched-articles
2455 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2456 (if gnus-newsgroup-active
2457 ;; Update the summary buffer
2458 (progn
2459 (dolist (article marked-articles)
2460 (gnus-summary-set-agent-mark article t))
2461 (dolist (article fetched-articles)
2462 (when gnus-agent-mark-unread-after-downloaded
2463 (setq gnus-newsgroup-downloadable
2464 (delq article gnus-newsgroup-downloadable))
2465 (gnus-summary-mark-article
2466 article gnus-unread-mark))
2467 (when (gnus-summary-goto-subject article nil t)
2468 (gnus-summary-update-download-mark article)))
2469 (dolist (article unfetched-articles)
2470 (gnus-summary-mark-article
2471 article gnus-canceled-mark)))
2473 ;; Update the group buffer.
2475 ;; When some, or all, of the marked articles came
2476 ;; from the download mark. Remove that mark. I
2477 ;; didn't do this earlier as I only want to remove
2478 ;; the marks after the fetch is completed.
2480 (dolist (mark gnus-agent-download-marks)
2481 (when (eq mark 'download)
2482 (let ((marked-arts
2483 (assq mark (gnus-info-marks
2484 (setq info (gnus-get-info group))))))
2485 (when (cdr marked-arts)
2486 (setq marks
2487 (delq marked-arts (gnus-info-marks info)))
2488 (gnus-info-set-marks info marks)))))
2489 (let ((read (gnus-info-read
2490 (or info (setq info (gnus-get-info group))))))
2491 (gnus-info-set-read
2492 info (gnus-add-to-range read unfetched-articles)))
2494 (gnus-group-update-group group t)
2495 (sit-for 0)
2497 (gnus-dribble-enter
2498 (concat "(gnus-group-set-info '"
2499 (gnus-prin1-to-string info)
2500 ")"))))))))))))
2503 ;;; Agent Category Mode
2506 (defvar gnus-category-mode-hook nil
2507 "Hook run in `gnus-category-mode' buffers.")
2509 (defvar gnus-category-line-format " %(%20c%): %g\n"
2510 "Format of category lines.
2512 Valid specifiers include:
2513 %c Topic name (string)
2514 %g The number of groups in the topic (integer)
2516 General format specifiers can also be used. See Info node
2517 `(gnus)Formatting Variables'.")
2519 (defvar gnus-category-mode-line-format "Gnus: %%b"
2520 "The format specification for the category mode line.")
2522 (defvar gnus-agent-predicate 'false
2523 "The selection predicate used when no other source is available.")
2525 (defvar gnus-agent-short-article 100
2526 "Articles that have fewer lines than this are short.")
2528 (defvar gnus-agent-long-article 200
2529 "Articles that have more lines than this are long.")
2531 (defvar gnus-agent-low-score 0
2532 "Articles that have a score lower than this have a low score.")
2534 (defvar gnus-agent-high-score 0
2535 "Articles that have a score higher than this have a high score.")
2538 ;;; Internal variables.
2540 (defvar gnus-category-buffer "*Agent Category*")
2542 (defvar gnus-category-line-format-alist
2543 `((?c gnus-tmp-name ?s)
2544 (?g gnus-tmp-groups ?d)))
2546 (defvar gnus-category-mode-line-format-alist
2547 `((?u user-defined ?s)))
2549 (defvar gnus-category-line-format-spec nil)
2550 (defvar gnus-category-mode-line-format-spec nil)
2552 (defvar gnus-category-mode-map nil)
2553 (put 'gnus-category-mode 'mode-class 'special)
2555 (unless gnus-category-mode-map
2556 (setq gnus-category-mode-map (make-sparse-keymap))
2557 (suppress-keymap gnus-category-mode-map)
2559 (gnus-define-keys gnus-category-mode-map
2560 "q" gnus-category-exit
2561 "k" gnus-category-kill
2562 "c" gnus-category-copy
2563 "a" gnus-category-add
2564 "e" gnus-agent-customize-category
2565 "p" gnus-category-edit-predicate
2566 "g" gnus-category-edit-groups
2567 "s" gnus-category-edit-score
2568 "l" gnus-category-list
2570 "\C-c\C-i" gnus-info-find-node
2571 "\C-c\C-b" gnus-bug))
2573 (defvar gnus-category-menu-hook nil
2574 "*Hook run after the creation of the menu.")
2576 (defun gnus-category-make-menu-bar ()
2577 (gnus-turn-off-edit-menu 'category)
2578 (unless (boundp 'gnus-category-menu)
2579 (easy-menu-define
2580 gnus-category-menu gnus-category-mode-map ""
2581 '("Categories"
2582 ["Add" gnus-category-add t]
2583 ["Kill" gnus-category-kill t]
2584 ["Copy" gnus-category-copy t]
2585 ["Edit category" gnus-agent-customize-category t]
2586 ["Edit predicate" gnus-category-edit-predicate t]
2587 ["Edit score" gnus-category-edit-score t]
2588 ["Edit groups" gnus-category-edit-groups t]
2589 ["Exit" gnus-category-exit t]))
2591 (gnus-run-hooks 'gnus-category-menu-hook)))
2593 (defun gnus-category-mode ()
2594 "Major mode for listing and editing agent categories.
2596 All normal editing commands are switched off.
2597 \\<gnus-category-mode-map>
2598 For more in-depth information on this mode, read the manual
2599 \(`\\[gnus-info-find-node]').
2601 The following commands are available:
2603 \\{gnus-category-mode-map}"
2604 (interactive)
2605 (when (gnus-visual-p 'category-menu 'menu)
2606 (gnus-category-make-menu-bar))
2607 (kill-all-local-variables)
2608 (gnus-simplify-mode-line)
2609 (setq major-mode 'gnus-category-mode)
2610 (setq mode-name "Category")
2611 (gnus-set-default-directory)
2612 (setq mode-line-process nil)
2613 (use-local-map gnus-category-mode-map)
2614 (buffer-disable-undo)
2615 (setq truncate-lines t)
2616 (setq buffer-read-only t)
2617 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2619 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2621 (defun gnus-category-insert-line (category)
2622 (let* ((gnus-tmp-name (format "%s" (car category)))
2623 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2624 (beginning-of-line)
2625 (gnus-add-text-properties
2626 (point)
2627 (prog1 (1+ (point))
2628 ;; Insert the text.
2629 (eval gnus-category-line-format-spec))
2630 (list 'gnus-category gnus-tmp-name))))
2632 (defun gnus-enter-category-buffer ()
2633 "Go to the Category buffer."
2634 (interactive)
2635 (gnus-category-setup-buffer)
2636 (gnus-configure-windows 'category)
2637 (gnus-category-prepare))
2639 (defun gnus-category-setup-buffer ()
2640 (unless (get-buffer gnus-category-buffer)
2641 (save-excursion
2642 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2643 (gnus-category-mode))))
2645 (defun gnus-category-prepare ()
2646 (gnus-set-format 'category-mode)
2647 (gnus-set-format 'category t)
2648 (let ((alist gnus-category-alist)
2649 (buffer-read-only nil))
2650 (erase-buffer)
2651 (while alist
2652 (gnus-category-insert-line (pop alist)))
2653 (goto-char (point-min))
2654 (gnus-category-position-point)))
2656 (defun gnus-category-name ()
2657 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2658 (error "No category on the current line")))
2660 (defun gnus-category-read ()
2661 "Read the category alist."
2662 (setq gnus-category-alist
2664 (with-temp-buffer
2665 (ignore-errors
2666 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2667 (goto-char (point-min))
2668 ;; This code isn't temp, it will be needed so long as
2669 ;; anyone may be migrating from an older version.
2671 ;; Once we're certain that people will not revert to an
2672 ;; earlier version, we can take out the old-list code in
2673 ;; gnus-category-write.
2674 (let* ((old-list (read (current-buffer)))
2675 (new-list (ignore-errors (read (current-buffer)))))
2676 (if new-list
2677 new-list
2678 ;; Convert from a positional list to an alist.
2679 (mapcar
2680 (lambda (c)
2681 (setcdr c
2682 (delq nil
2683 (gnus-mapcar
2684 (lambda (valu symb)
2685 (if valu
2686 (cons symb valu)))
2687 (cdr c)
2688 '(agent-predicate agent-score-file agent-groups))))
2690 old-list)))))
2691 (list (gnus-agent-cat-make 'default 'short)))))
2693 (defun gnus-category-write ()
2694 "Write the category alist."
2695 (setq gnus-category-predicate-cache nil
2696 gnus-category-group-cache nil)
2697 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2698 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2699 ;; This prin1 is temporary. It exists so that people can revert
2700 ;; to an earlier version of gnus-agent.
2701 (prin1 (mapcar (lambda (c)
2702 (list (car c)
2703 (cdr (assoc 'agent-predicate c))
2704 (cdr (assoc 'agent-score-file c))
2705 (cdr (assoc 'agent-groups c))))
2706 gnus-category-alist)
2707 (current-buffer))
2708 (newline)
2709 (prin1 gnus-category-alist (current-buffer))))
2711 (defun gnus-category-edit-predicate (category)
2712 "Edit the predicate for CATEGORY."
2713 (interactive (list (gnus-category-name)))
2714 (let ((info (assq category gnus-category-alist)))
2715 (gnus-edit-form
2716 (gnus-agent-cat-predicate info)
2717 (format "Editing the select predicate for category %s" category)
2718 `(lambda (predicate)
2719 ;; Avoid run-time execution of setf form
2720 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2721 ;; predicate)
2722 ;; use its expansion instead:
2723 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2724 'agent-predicate predicate)
2726 (gnus-category-write)
2727 (gnus-category-list)))))
2729 (defun gnus-category-edit-score (category)
2730 "Edit the score expression for CATEGORY."
2731 (interactive (list (gnus-category-name)))
2732 (let ((info (assq category gnus-category-alist)))
2733 (gnus-edit-form
2734 (gnus-agent-cat-score-file info)
2735 (format "Editing the score expression for category %s" category)
2736 `(lambda (score-file)
2737 ;; Avoid run-time execution of setf form
2738 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2739 ;; score-file)
2740 ;; use its expansion instead:
2741 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2742 'agent-score-file score-file)
2744 (gnus-category-write)
2745 (gnus-category-list)))))
2747 (defun gnus-category-edit-groups (category)
2748 "Edit the group list for CATEGORY."
2749 (interactive (list (gnus-category-name)))
2750 (let ((info (assq category gnus-category-alist)))
2751 (gnus-edit-form
2752 (gnus-agent-cat-groups info)
2753 (format "Editing the group list for category %s" category)
2754 `(lambda (groups)
2755 ;; Avoid run-time execution of setf form
2756 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2757 ;; groups)
2758 ;; use its expansion instead:
2759 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2760 groups)
2762 (gnus-category-write)
2763 (gnus-category-list)))))
2765 (defun gnus-category-kill (category)
2766 "Kill the current category."
2767 (interactive (list (gnus-category-name)))
2768 (let ((info (assq category gnus-category-alist))
2769 (buffer-read-only nil))
2770 (gnus-delete-line)
2771 (setq gnus-category-alist (delq info gnus-category-alist))
2772 (gnus-category-write)))
2774 (defun gnus-category-copy (category to)
2775 "Copy the current category."
2776 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2777 (let ((info (assq category gnus-category-alist)))
2778 (push (let ((newcat (gnus-copy-sequence info)))
2779 (setf (gnus-agent-cat-name newcat) to)
2780 (setf (gnus-agent-cat-groups newcat) nil)
2781 newcat)
2782 gnus-category-alist)
2783 (gnus-category-write)
2784 (gnus-category-list)))
2786 (defun gnus-category-add (category)
2787 "Create a new category."
2788 (interactive "SCategory name: ")
2789 (when (assq category gnus-category-alist)
2790 (error "Category %s already exists" category))
2791 (push (gnus-agent-cat-make category)
2792 gnus-category-alist)
2793 (gnus-category-write)
2794 (gnus-category-list))
2796 (defun gnus-category-list ()
2797 "List all categories."
2798 (interactive)
2799 (gnus-category-prepare))
2801 (defun gnus-category-exit ()
2802 "Return to the group buffer."
2803 (interactive)
2804 (kill-buffer (current-buffer))
2805 (gnus-configure-windows 'group t))
2807 ;; To avoid having 8-bit characters in the source file.
2808 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2810 (defvar gnus-category-predicate-alist
2811 '((spam . gnus-agent-spam-p)
2812 (short . gnus-agent-short-p)
2813 (long . gnus-agent-long-p)
2814 (low . gnus-agent-low-scored-p)
2815 (high . gnus-agent-high-scored-p)
2816 (read . gnus-agent-read-p)
2817 (true . gnus-agent-true)
2818 (false . gnus-agent-false))
2819 "Mapping from short score predicate symbols to predicate functions.")
2821 (defun gnus-agent-spam-p ()
2822 "Say whether an article is spam or not."
2823 (unless gnus-agent-spam-hashtb
2824 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2825 (if (not (equal (mail-header-references gnus-headers) ""))
2827 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2828 (prog1
2829 (gnus-gethash string gnus-agent-spam-hashtb)
2830 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2832 (defun gnus-agent-short-p ()
2833 "Say whether an article is short or not."
2834 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2836 (defun gnus-agent-long-p ()
2837 "Say whether an article is long or not."
2838 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2840 (defun gnus-agent-low-scored-p ()
2841 "Say whether an article has a low score or not."
2842 (< gnus-score gnus-agent-low-score))
2844 (defun gnus-agent-high-scored-p ()
2845 "Say whether an article has a high score or not."
2846 (> gnus-score gnus-agent-high-score))
2848 (defun gnus-agent-read-p ()
2849 "Say whether an article is read or not."
2850 (gnus-member-of-range (mail-header-number gnus-headers)
2851 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2853 (defun gnus-category-make-function (predicate)
2854 "Make a function from PREDICATE."
2855 (let ((func (gnus-category-make-function-1 predicate)))
2856 (if (and (= (length func) 1)
2857 (symbolp (car func)))
2858 (car func)
2859 (gnus-byte-compile `(lambda () ,func)))))
2861 (defun gnus-agent-true ()
2862 "Return t."
2865 (defun gnus-agent-false ()
2866 "Return nil."
2867 nil)
2869 (defun gnus-category-make-function-1 (predicate)
2870 "Make a function from PREDICATE."
2871 (cond
2872 ;; Functions are just returned as is.
2873 ((or (symbolp predicate)
2874 (functionp predicate))
2875 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2876 predicate)))
2877 ;; More complex predicate.
2878 ((consp predicate)
2879 `(,(cond
2880 ((memq (car predicate) '(& and))
2881 'and)
2882 ((memq (car predicate) '(| or))
2883 'or)
2884 ((memq (car predicate) gnus-category-not)
2885 'not))
2886 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2888 (error "Unknown predicate type: %s" predicate))))
2890 (defun gnus-get-predicate (predicate)
2891 "Return the function implementing PREDICATE."
2892 (or (cdr (assoc predicate gnus-category-predicate-cache))
2893 (let ((func (gnus-category-make-function predicate)))
2894 (setq gnus-category-predicate-cache
2895 (nconc gnus-category-predicate-cache
2896 (list (cons predicate func))))
2897 func)))
2899 (defun gnus-predicate-implies-unread (predicate)
2900 "Say whether PREDICATE implies unread articles only.
2901 It is okay to miss some cases, but there must be no false positives.
2902 That is, if this predicate returns true, then indeed the predicate must
2903 return only unread articles."
2904 (eq t (gnus-function-implies-unread-1
2905 (gnus-category-make-function-1 predicate))))
2907 (defun gnus-function-implies-unread-1 (function)
2908 "Recursively evaluate a predicate function to determine whether it can select
2909 any read articles. Returns t if the function is known to never
2910 return read articles, nil when it is known to always return read
2911 articles, and t_nil when the function may return both read and unread
2912 articles."
2913 (let ((func (car function))
2914 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2915 (cond ((eq func 'and)
2916 (cond ((memq t args) ; if any argument returns only unread articles
2917 ;; then that argument constrains the result to only unread articles.
2919 ((memq 't_nil args) ; if any argument is indeterminate
2920 ;; then the result is indeterminate
2921 't_nil)))
2922 ((eq func 'or)
2923 (cond ((memq nil args) ; if any argument returns read articles
2924 ;; then that argument ensures that the results includes read articles.
2925 nil)
2926 ((memq 't_nil args) ; if any argument is indeterminate
2927 ;; then that argument ensures that the results are indeterminate
2928 't_nil)
2929 (t ; if all arguments return only unread articles
2930 ;; then the result returns only unread articles
2931 t)))
2932 ((eq func 'not)
2933 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2934 ; then the result is indeterminate
2935 (car args))
2936 (t ; otherwise
2937 ; toggle the result to be the opposite of the argument
2938 (not (car args)))))
2939 ((eq func 'gnus-agent-read-p)
2940 nil) ; The read predicate NEVER returns unread articles
2941 ((eq func 'gnus-agent-false)
2942 t) ; The false predicate returns t as the empty set excludes all read articles
2943 ((eq func 'gnus-agent-true)
2944 nil) ; The true predicate ALWAYS returns read articles
2945 ((catch 'found-match
2946 (let ((alist gnus-category-predicate-alist))
2947 (while alist
2948 (if (eq func (cdar alist))
2949 (throw 'found-match t)
2950 (setq alist (cdr alist))))))
2951 't_nil) ; All other predicates return read and unread articles
2953 (error "Unknown predicate function: %s" function)))))
2955 (defun gnus-group-category (group)
2956 "Return the category GROUP belongs to."
2957 (unless gnus-category-group-cache
2958 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2959 (let ((cs gnus-category-alist)
2960 groups cat)
2961 (while (setq cat (pop cs))
2962 (setq groups (gnus-agent-cat-groups cat))
2963 (while groups
2964 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2965 (or (gnus-gethash group gnus-category-group-cache)
2966 (assq 'default gnus-category-alist)))
2968 (defun gnus-agent-expire-group (group &optional articles force)
2969 "Expire all old articles in GROUP.
2970 If you want to force expiring of certain articles, this function can
2971 take ARTICLES, and FORCE parameters as well.
2973 The articles on which the expiration process runs are selected as follows:
2974 if ARTICLES is null, all read and unmarked articles.
2975 if ARTICLES is t, all articles.
2976 if ARTICLES is a list, just those articles.
2977 FORCE is equivalent to setting the expiration predicates to true."
2978 (interactive
2979 (list (let ((def (or (gnus-group-group-name)
2980 gnus-newsgroup-name)))
2981 (let ((select (read-string (if def
2982 (concat "Group Name ("
2983 def "): ")
2984 "Group Name: "))))
2985 (if (and (equal "" select)
2986 def)
2988 select)))))
2990 (if (not group)
2991 (gnus-agent-expire articles group force)
2992 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2993 ;; expiration statistics of this single group
2994 (gnus-agent-expire-stats (list 0 0 0.0)))
2995 (if (or (not (eq articles t))
2996 (yes-or-no-p
2997 (concat "Are you sure that you want to "
2998 "expire all articles in " group "? ")))
2999 (let ((gnus-command-method (gnus-find-method-for-group group))
3000 (overview (gnus-get-buffer-create " *expire overview*"))
3001 orig)
3002 (unwind-protect
3003 (let ((active-file (gnus-agent-lib-file "active")))
3004 (when (file-exists-p active-file)
3005 (with-temp-buffer
3006 (nnheader-insert-file-contents active-file)
3007 (gnus-active-to-gnus-format
3008 gnus-command-method
3009 (setq orig (gnus-make-hashtable
3010 (count-lines (point-min) (point-max))))))
3011 (save-excursion
3012 (gnus-agent-expire-group-1
3013 group overview (gnus-gethash-safe group orig)
3014 articles force))))
3015 (kill-buffer overview))))
3016 (gnus-message 4 (gnus-agent-expire-done-message)))))
3018 (defun gnus-agent-expire-group-1 (group overview active articles force)
3019 ;; Internal function - requires caller to have set
3020 ;; gnus-command-method, initialized overview buffer, and to have
3021 ;; provided a non-nil active
3023 (let ((dir (gnus-agent-group-pathname group)))
3024 (when (boundp 'gnus-agent-expire-current-dirs)
3025 (set 'gnus-agent-expire-current-dirs
3026 (cons dir
3027 (symbol-value 'gnus-agent-expire-current-dirs))))
3029 (if (and (not force)
3030 (eq 'DISABLE (gnus-agent-find-parameter group
3031 'agent-enable-expiration)))
3032 (gnus-message 5 "Expiry skipping over %s" group)
3033 (gnus-message 5 "Expiring articles in %s" group)
3034 (gnus-agent-load-alist group)
3035 (let* ((bytes-freed 0)
3036 (files-deleted 0)
3037 (nov-entries-deleted 0)
3038 (info (gnus-get-info group))
3039 (alist gnus-agent-article-alist)
3040 (day (- (time-to-days (current-time))
3041 (gnus-agent-find-parameter group 'agent-days-until-old)))
3042 (specials (if (and alist
3043 (not force))
3044 ;; This could be a bit of a problem. I need to
3045 ;; keep the last article to avoid refetching
3046 ;; headers when using nntp in the backend. At
3047 ;; the same time, if someone uses a backend
3048 ;; that supports article moving then I may have
3049 ;; to remove the last article to complete the
3050 ;; move. Right now, I'm going to assume that
3051 ;; FORCE overrides specials.
3052 (list (caar (last alist)))))
3053 (unreads ;; Articles that are excluded from the
3054 ;; expiration process
3055 (cond (gnus-agent-expire-all
3056 ;; All articles are marked read by global decree
3057 nil)
3058 ((eq articles t)
3059 ;; All articles are marked read by function
3060 ;; parameter
3061 nil)
3062 ((not articles)
3063 ;; Unread articles are marked protected from
3064 ;; expiration Don't call
3065 ;; gnus-list-of-unread-articles as it returns
3066 ;; articles that have not been fetched into the
3067 ;; agent.
3068 (ignore-errors
3069 (gnus-agent-unread-articles group)))
3071 ;; All articles EXCEPT those named by the caller
3072 ;; are protected from expiration
3073 (gnus-sorted-difference
3074 (gnus-uncompress-range
3075 (cons (caar alist)
3076 (caar (last alist))))
3077 (sort articles '<)))))
3078 (marked ;; More articles that are excluded from the
3079 ;; expiration process
3080 (cond (gnus-agent-expire-all
3081 ;; All articles are unmarked by global decree
3082 nil)
3083 ((eq articles t)
3084 ;; All articles are unmarked by function
3085 ;; parameter
3086 nil)
3087 (articles
3088 ;; All articles may as well be unmarked as the
3089 ;; unreads list already names the articles we are
3090 ;; going to keep
3091 nil)
3093 ;; Ticked and/or dormant articles are excluded
3094 ;; from expiration
3095 (nconc
3096 (gnus-uncompress-range
3097 (cdr (assq 'tick (gnus-info-marks info))))
3098 (gnus-uncompress-range
3099 (cdr (assq 'dormant
3100 (gnus-info-marks info))))))))
3101 (nov-file (concat dir ".overview"))
3102 (cnt 0)
3103 (completed -1)
3104 dlist
3105 type)
3107 ;; The normal article alist contains elements that look like
3108 ;; (article# . fetch_date) I need to combine other
3109 ;; information with this list. For example, a flag indicating
3110 ;; that a particular article MUST BE KEPT. To do this, I'm
3111 ;; going to transform the elements to look like (article#
3112 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3113 ;; the process to generate the expired article alist.
3115 ;; Convert the alist elements to (article# fetch_date nil
3116 ;; nil).
3117 (setq dlist (mapcar (lambda (e)
3118 (list (car e) (cdr e) nil nil)) alist))
3120 ;; Convert the keep lists to elements that look like (article#
3121 ;; nil keep_flag nil) then append it to the expanded dlist
3122 ;; These statements are sorted by ascending precidence of the
3123 ;; keep_flag.
3124 (setq dlist (nconc dlist
3125 (mapcar (lambda (e)
3126 (list e nil 'unread nil))
3127 unreads)))
3128 (setq dlist (nconc dlist
3129 (mapcar (lambda (e)
3130 (list e nil 'marked nil))
3131 marked)))
3132 (setq dlist (nconc dlist
3133 (mapcar (lambda (e)
3134 (list e nil 'special nil))
3135 specials)))
3137 (set-buffer overview)
3138 (erase-buffer)
3139 (buffer-disable-undo)
3140 (when (file-exists-p nov-file)
3141 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3142 (nnheader-insert-file-contents nov-file)
3143 (goto-char (point-min))
3145 (let (p)
3146 (while (< (setq p (point)) (point-max))
3147 (condition-case nil
3148 ;; If I successfully read an integer (the plus zero
3149 ;; ensures a numeric type), prepend a marker entry
3150 ;; to the list
3151 (push (list (+ 0 (read (current-buffer))) nil nil
3152 (set-marker (make-marker) p))
3153 dlist)
3154 (error
3155 (gnus-message 1 "gnus-agent-expire: read error \
3156 occurred when reading expression at %s in %s. Skipping to next \
3157 line." (point) nov-file)))
3158 ;; Whether I succeeded, or failed, it doesn't matter.
3159 ;; Move to the next line then try again.
3160 (forward-line 1)))
3162 (gnus-message
3163 7 "gnus-agent-expire: Loading overview... Done"))
3164 (set-buffer-modified-p nil)
3166 ;; At this point, all of the information is in dlist. The
3167 ;; only problem is that much of it is spread across multiple
3168 ;; entries. Sort then MERGE!!
3169 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3170 ;; If two entries have the same article-number then sort by
3171 ;; ascending keep_flag.
3172 (let ((special 0)
3173 (marked 1)
3174 (unread 2))
3175 (setq dlist
3176 (sort dlist
3177 (lambda (a b)
3178 (cond ((< (nth 0 a) (nth 0 b))
3180 ((> (nth 0 a) (nth 0 b))
3181 nil)
3183 (let ((a (or (symbol-value (nth 2 a))
3185 (b (or (symbol-value (nth 2 b))
3186 3)))
3187 (<= a b))))))))
3188 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3189 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3190 (let ((dlist dlist))
3191 (while (cdr dlist) ; I'm not at the end-of-list
3192 (if (eq (caar dlist) (caadr dlist))
3193 (let ((first (cdr (car dlist)))
3194 (secnd (cdr (cadr dlist))))
3195 (setcar first (or (car first)
3196 (car secnd))) ; fetch_date
3197 (setq first (cdr first)
3198 secnd (cdr secnd))
3199 (setcar first (or (car first)
3200 (car secnd))) ; Keep_flag
3201 (setq first (cdr first)
3202 secnd (cdr secnd))
3203 (setcar first (or (car first)
3204 (car secnd))) ; NOV_entry_marker
3206 (setcdr dlist (cddr dlist)))
3207 (setq dlist (cdr dlist)))))
3208 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3210 (let* ((len (float (length dlist)))
3211 (alist (list nil))
3212 (tail-alist alist))
3213 (while dlist
3214 (let ((new-completed (truncate (* 100.0
3215 (/ (setq cnt (1+ cnt))
3216 len))))
3217 message-log-max)
3218 (when (> new-completed completed)
3219 (setq completed new-completed)
3220 (gnus-message 7 "%3d%% completed..." completed)))
3221 (let* ((entry (car dlist))
3222 (article-number (nth 0 entry))
3223 (fetch-date (nth 1 entry))
3224 (keep (nth 2 entry))
3225 (marker (nth 3 entry)))
3227 (cond
3228 ;; Kept articles are unread, marked, or special.
3229 (keep
3230 (gnus-agent-message 10
3231 "gnus-agent-expire: %s:%d: Kept %s article%s."
3232 group article-number keep (if fetch-date " and file" ""))
3233 (when fetch-date
3234 (unless (file-exists-p
3235 (concat dir (number-to-string
3236 article-number)))
3237 (setf (nth 1 entry) nil)
3238 (gnus-agent-message 3 "gnus-agent-expire cleared \
3239 download flag on %s:%d as the cached article file is missing."
3240 group (caar dlist)))
3241 (unless marker
3242 (gnus-message 1 "gnus-agent-expire detected a \
3243 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3244 (gnus-agent-append-to-list
3245 tail-alist
3246 (cons article-number fetch-date)))
3248 ;; The following articles are READ, UNMARKED, and
3249 ;; ORDINARY. See if they can be EXPIRED!!!
3250 ((setq type
3251 (cond
3252 ((not (integerp fetch-date))
3253 'read) ;; never fetched article (may expire
3254 ;; right now)
3255 ((not (file-exists-p
3256 (concat dir (number-to-string
3257 article-number))))
3258 (setf (nth 1 entry) nil)
3259 'externally-expired) ;; Can't find the cached
3260 ;; article. Handle case
3261 ;; as though this article
3262 ;; was never fetched.
3264 ;; We now have the arrival day, so we see
3265 ;; whether it's old enough to be expired.
3266 ((< fetch-date day)
3267 'expired)
3268 (force
3269 'forced)))
3271 ;; I found some reason to expire this entry.
3273 (let ((actions nil))
3274 (when (memq type '(forced expired))
3275 (ignore-errors ; Just being paranoid.
3276 (let* ((file-name (nnheader-concat dir (number-to-string
3277 article-number)))
3278 (size (float (nth 7 (file-attributes file-name)))))
3279 (incf bytes-freed size)
3280 (incf files-deleted)
3281 (delete-file file-name))
3282 (push "expired cached article" actions))
3283 (setf (nth 1 entry) nil)
3286 (when marker
3287 (push "NOV entry removed" actions)
3288 (goto-char marker)
3290 (incf nov-entries-deleted)
3292 (let ((from (gnus-point-at-bol))
3293 (to (progn (forward-line 1) (point))))
3294 (incf bytes-freed (- to from))
3295 (delete-region from to)))
3297 ;; If considering all articles is set, I can only
3298 ;; expire article IDs that are no longer in the
3299 ;; active range (That is, articles that preceed the
3300 ;; first article in the new alist).
3301 (if (and gnus-agent-consider-all-articles
3302 (>= article-number (car active)))
3303 ;; I have to keep this ID in the alist
3304 (gnus-agent-append-to-list
3305 tail-alist (cons article-number fetch-date))
3306 (push (format "Removed %s article number from \
3307 article alist" type) actions))
3309 (when actions
3310 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3311 group article-number
3312 (mapconcat 'identity actions ", ")))))
3314 (gnus-agent-message
3315 10 "gnus-agent-expire: %s:%d: Article kept as \
3316 expiration tests failed." group article-number)
3317 (gnus-agent-append-to-list
3318 tail-alist (cons article-number fetch-date)))
3321 ;; Clean up markers as I want to recycle this buffer
3322 ;; over several groups.
3323 (when marker
3324 (set-marker marker nil))
3326 (setq dlist (cdr dlist))))
3328 (setq alist (cdr alist))
3330 (let ((inhibit-quit t))
3331 (unless (equal alist gnus-agent-article-alist)
3332 (setq gnus-agent-article-alist alist)
3333 (gnus-agent-save-alist group))
3335 (when (buffer-modified-p)
3336 (let ((coding-system-for-write
3337 gnus-agent-file-coding-system))
3338 (gnus-make-directory dir)
3339 (write-region (point-min) (point-max) nov-file nil
3340 'silent)
3341 ;; clear the modified flag as that I'm not confused by
3342 ;; its status on the next pass through this routine.
3343 (set-buffer-modified-p nil)))
3345 (when (eq articles t)
3346 (gnus-summary-update-info))))
3348 (when (boundp 'gnus-agent-expire-stats)
3349 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3350 (incf (nth 2 stats) bytes-freed)
3351 (incf (nth 1 stats) files-deleted)
3352 (incf (nth 0 stats) nov-entries-deleted)))
3353 ))))
3355 (defun gnus-agent-expire (&optional articles group force)
3356 "Expire all old articles.
3357 If you want to force expiring of certain articles, this function can
3358 take ARTICLES, GROUP and FORCE parameters as well.
3360 The articles on which the expiration process runs are selected as follows:
3361 if ARTICLES is null, all read and unmarked articles.
3362 if ARTICLES is t, all articles.
3363 if ARTICLES is a list, just those articles.
3364 Setting GROUP will limit expiration to that group.
3365 FORCE is equivalent to setting the expiration predicates to true."
3366 (interactive)
3368 (if group
3369 (gnus-agent-expire-group group articles force)
3370 (if (or (not (eq articles t))
3371 (yes-or-no-p "Are you sure that you want to expire all \
3372 articles in every agentized group? "))
3373 (let ((methods (gnus-agent-covered-methods))
3374 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3375 ;; of agent directories.
3376 (gnus-agent-expire-current-dirs nil)
3377 ;; Bind gnus-agent-expire-stats to enable tracking of
3378 ;; expiration statistics across all groups
3379 (gnus-agent-expire-stats (list 0 0 0.0))
3380 gnus-command-method overview orig)
3381 (setq overview (gnus-get-buffer-create " *expire overview*"))
3382 (unwind-protect
3383 (while (setq gnus-command-method (pop methods))
3384 (let ((active-file (gnus-agent-lib-file "active")))
3385 (when (file-exists-p active-file)
3386 (with-temp-buffer
3387 (nnheader-insert-file-contents active-file)
3388 (gnus-active-to-gnus-format
3389 gnus-command-method
3390 (setq orig (gnus-make-hashtable
3391 (count-lines (point-min) (point-max))))))
3392 (dolist (expiring-group (gnus-groups-from-server
3393 gnus-command-method))
3394 (let* ((active
3395 (gnus-gethash-safe expiring-group orig)))
3397 (when active
3398 (save-excursion
3399 (gnus-agent-expire-group-1
3400 expiring-group overview active articles force))))))))
3401 (kill-buffer overview))
3402 (gnus-agent-expire-unagentized-dirs)
3403 (gnus-message 4 (gnus-agent-expire-done-message))))))
3405 (defun gnus-agent-expire-done-message ()
3406 (if (and (> gnus-verbose 4)
3407 (boundp 'gnus-agent-expire-stats))
3408 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3409 (size (nth 2 stats))
3410 (units '(B KB MB GB)))
3411 (while (and (> size 1024.0)
3412 (cdr units))
3413 (setq size (/ size 1024.0)
3414 units (cdr units)))
3416 (format "Expiry recovered %d NOV entries, deleted %d files,\
3417 and freed %f %s."
3418 (nth 0 stats)
3419 (nth 1 stats)
3420 size (car units)))
3421 "Expiry...done"))
3423 (defun gnus-agent-expire-unagentized-dirs ()
3424 (when (and gnus-agent-expire-unagentized-dirs
3425 (boundp 'gnus-agent-expire-current-dirs))
3426 (let* ((keep (gnus-make-hashtable))
3427 ;; Formally bind gnus-agent-expire-current-dirs so that the
3428 ;; compiler will not complain about free references.
3429 (gnus-agent-expire-current-dirs
3430 (symbol-value 'gnus-agent-expire-current-dirs))
3431 dir)
3433 (gnus-sethash gnus-agent-directory t keep)
3434 (while gnus-agent-expire-current-dirs
3435 (setq dir (pop gnus-agent-expire-current-dirs))
3436 (when (and (stringp dir)
3437 (file-directory-p dir))
3438 (while (not (gnus-gethash dir keep))
3439 (gnus-sethash dir t keep)
3440 (setq dir (file-name-directory (directory-file-name dir))))))
3442 (let* (to-remove
3443 checker
3444 (checker
3445 (function
3446 (lambda (d)
3447 "Given a directory, check it and its subdirectories for
3448 membership in the keep hash. If it isn't found, add
3449 it to to-remove."
3450 (let ((files (directory-files d))
3451 file)
3452 (while (setq file (pop files))
3453 (cond ((equal file ".") ; Ignore self
3454 nil)
3455 ((equal file "..") ; Ignore parent
3456 nil)
3457 ((equal file ".overview")
3458 ;; Directory must contain .overview to be
3459 ;; agent's cache of a group.
3460 (let ((d (file-name-as-directory d))
3462 ;; Search ancestor's for last directory NOT
3463 ;; found in keep hash.
3464 (while (not (gnus-gethash
3465 (setq d (file-name-directory d)) keep))
3466 (setq r d
3467 d (directory-file-name d)))
3468 ;; if ANY ancestor was NOT in keep hash and
3469 ;; it it's already in to-remove, add it to
3470 ;; to-remove.
3471 (if (and r
3472 (not (member r to-remove)))
3473 (push r to-remove))))
3474 ((file-directory-p (setq file (nnheader-concat d file)))
3475 (funcall checker file)))))))))
3476 (funcall checker (expand-file-name gnus-agent-directory))
3478 (when (and to-remove
3479 (or gnus-expert-user
3480 (gnus-y-or-n-p
3481 "gnus-agent-expire has identified local directories that are\
3482 not currently required by any agentized group. Do you wish to consider\
3483 deleting them?")))
3484 (while to-remove
3485 (let ((dir (pop to-remove)))
3486 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3487 (let* (delete-recursive
3488 (delete-recursive
3489 (function
3490 (lambda (f-or-d)
3491 (ignore-errors
3492 (if (file-directory-p f-or-d)
3493 (condition-case nil
3494 (delete-directory f-or-d)
3495 (file-error
3496 (mapcar (lambda (f)
3497 (or (member f '("." ".."))
3498 (funcall delete-recursive
3499 (nnheader-concat
3500 f-or-d f))))
3501 (directory-files f-or-d))
3502 (delete-directory f-or-d)))
3503 (delete-file f-or-d)))))))
3504 (funcall delete-recursive dir))))))))))
3506 ;;;###autoload
3507 (defun gnus-agent-batch ()
3508 "Start Gnus, send queue and fetch session."
3509 (interactive)
3510 (let ((init-file-user "")
3511 (gnus-always-read-dribble-file t))
3512 (gnus))
3513 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3514 (gnus-group-send-queue)
3515 (gnus-agent-fetch-session)))
3517 (defun gnus-agent-unread-articles (group)
3518 (let* ((read (gnus-info-read (gnus-get-info group)))
3519 (known (gnus-agent-load-alist group))
3520 (unread (list nil))
3521 (tail-unread unread))
3522 (while (and known read)
3523 (let ((candidate (car (pop known))))
3524 (while (let* ((range (car read))
3525 (min (if (numberp range) range (car range)))
3526 (max (if (numberp range) range (cdr range))))
3527 (cond ((or (not min)
3528 (< candidate min))
3529 (gnus-agent-append-to-list tail-unread candidate)
3530 nil)
3531 ((> candidate max)
3532 (setq read (cdr read))
3533 ;; return t so that I always loop one more
3534 ;; time. If I just iterated off the end of
3535 ;; read, min will become nil and the current
3536 ;; candidate will be added to the unread list.
3537 t))))))
3538 (while known
3539 (gnus-agent-append-to-list tail-unread (car (pop known))))
3540 (cdr unread)))
3542 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3543 "Restrict ARTICLES to numbers already fetched.
3544 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3545 that have already been fetched.
3546 If CACHED-HEADER is nil, articles are only excluded if the article itself
3547 has been fetched."
3549 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3550 ;; 'car gnus-agent-article-alist))
3552 ;; Functionally, I don't need to construct a temp list using mapcar.
3554 (if (and (or gnus-agent-cache (not gnus-plugged))
3555 (gnus-agent-load-alist group))
3556 (let* ((ref gnus-agent-article-alist)
3557 (arts articles)
3558 (uncached (list nil))
3559 (tail-uncached uncached))
3560 (while (and ref arts)
3561 (let ((v1 (car arts))
3562 (v2 (caar ref)))
3563 (cond ((< v1 v2) ; v1 does not appear in the reference list
3564 (gnus-agent-append-to-list tail-uncached v1)
3565 (setq arts (cdr arts)))
3566 ((= v1 v2)
3567 (unless (or cached-header (cdar ref)) ; v1 is already cached
3568 (gnus-agent-append-to-list tail-uncached v1))
3569 (setq arts (cdr arts))
3570 (setq ref (cdr ref)))
3571 (t ; reference article (v2) preceeds the list being filtered
3572 (setq ref (cdr ref))))))
3573 (while arts
3574 (gnus-agent-append-to-list tail-uncached (pop arts)))
3575 (cdr uncached))
3576 ;; if gnus-agent-load-alist fails, no articles are cached.
3577 articles))
3579 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3580 (save-excursion
3581 (gnus-agent-create-buffer)
3582 (let ((gnus-decode-encoded-word-function 'identity)
3583 (gnus-decode-encoded-address-function 'identity)
3584 (file (gnus-agent-article-name ".overview" group))
3585 cached-articles uncached-articles)
3586 (gnus-make-directory (nnheader-translate-file-chars
3587 (file-name-directory file) t))
3589 ;; Populate temp buffer with known headers
3590 (when (file-exists-p file)
3591 (with-current-buffer gnus-agent-overview-buffer
3592 (erase-buffer)
3593 (let ((nnheader-file-coding-system
3594 gnus-agent-file-coding-system))
3595 (nnheader-insert-nov-file file (car articles)))))
3597 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3599 (progn
3600 ;; Populate nntp-server-buffer with uncached headers
3601 (set-buffer nntp-server-buffer)
3602 (erase-buffer)
3603 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3604 (gnus-retrieve-headers
3605 uncached-articles group fetch-old))))
3606 (nnvirtual-convert-headers))
3607 ((eq 'nntp (car gnus-current-select-method))
3608 ;; The author of gnus-get-newsgroup-headers-xover
3609 ;; reports that the XOVER command is commonly
3610 ;; unreliable. The problem is that recently
3611 ;; posted articles may not be entered into the
3612 ;; NOV database in time to respond to my XOVER
3613 ;; query.
3615 ;; I'm going to use his assumption that the NOV
3616 ;; database is updated in order of ascending
3617 ;; article ID. Therefore, a response containing
3618 ;; article ID N implies that all articles from 1
3619 ;; to N-1 are up-to-date. Therefore, missing
3620 ;; articles in that range have expired.
3622 (set-buffer nntp-server-buffer)
3623 (let* ((fetched-articles (list nil))
3624 (tail-fetched-articles fetched-articles)
3625 (min (cond ((numberp fetch-old)
3626 (max 1 (- (car articles) fetch-old)))
3627 (fetch-old
3630 (car articles))))
3631 (max (car (last articles))))
3633 ;; Get the list of articles that were fetched
3634 (goto-char (point-min))
3635 (let ((pm (point-max))
3636 art)
3637 (while (< (point) pm)
3638 (when (setq art (gnus-agent-read-article-number))
3639 (gnus-agent-append-to-list tail-fetched-articles art))
3640 (forward-line 1)))
3642 ;; Clip this list to the headers that will
3643 ;; actually be returned
3644 (setq fetched-articles (gnus-list-range-intersection
3645 (cdr fetched-articles)
3646 (cons min max)))
3648 ;; Clip the uncached articles list to exclude
3649 ;; IDs after the last FETCHED header. The
3650 ;; excluded IDs may be fetchable using HEAD.
3651 (if (car tail-fetched-articles)
3652 (setq uncached-articles
3653 (gnus-list-range-intersection
3654 uncached-articles
3655 (cons (car uncached-articles)
3656 (car tail-fetched-articles)))))
3658 ;; Create the list of articles that were
3659 ;; "successfully" fetched. Success, in this
3660 ;; case, means that the ID should not be
3661 ;; fetched again. In the case of an expired
3662 ;; article, the header will not be fetched.
3663 (setq uncached-articles
3664 (gnus-sorted-nunion fetched-articles
3665 uncached-articles))
3668 ;; Erase the temp buffer
3669 (set-buffer gnus-agent-overview-buffer)
3670 (erase-buffer)
3672 ;; Copy the nntp-server-buffer to the temp buffer
3673 (set-buffer nntp-server-buffer)
3674 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3676 ;; Merge the temp buffer with the known headers (found on
3677 ;; disk in FILE) into the nntp-server-buffer
3678 (when uncached-articles
3679 (gnus-agent-braid-nov group uncached-articles file))
3681 ;; Save the new set of known headers to FILE
3682 (set-buffer nntp-server-buffer)
3683 (let ((coding-system-for-write
3684 gnus-agent-file-coding-system))
3685 (gnus-agent-check-overview-buffer)
3686 (write-region (point-min) (point-max) file nil 'silent))
3688 ;; Update the group's article alist to include the newly
3689 ;; fetched articles.
3690 (gnus-agent-load-alist group)
3691 (gnus-agent-save-alist group uncached-articles nil)
3694 ;; Copy the temp buffer to the nntp-server-buffer
3695 (set-buffer nntp-server-buffer)
3696 (erase-buffer)
3697 (insert-buffer-substring gnus-agent-overview-buffer)))
3699 (if (and fetch-old
3700 (not (numberp fetch-old)))
3701 t ; Don't remove anything.
3702 (nnheader-nov-delete-outside-range
3703 (if fetch-old (max 1 (- (car articles) fetch-old))
3704 (car articles))
3705 (car (last articles)))
3708 'nov))
3710 (defun gnus-agent-request-article (article group)
3711 "Retrieve ARTICLE in GROUP from the agent cache."
3712 (when (and gnus-agent
3713 (or gnus-agent-cache
3714 (not gnus-plugged))
3715 (numberp article))
3716 (let* ((gnus-command-method (gnus-find-method-for-group group))
3717 (file (gnus-agent-article-name (number-to-string article) group))
3718 (buffer-read-only nil))
3719 (when (and (file-exists-p file)
3720 (> (nth 7 (file-attributes file)) 0))
3721 (erase-buffer)
3722 (gnus-kill-all-overlays)
3723 (let ((coding-system-for-read gnus-cache-coding-system))
3724 (insert-file-contents file))
3725 t))))
3727 (defun gnus-agent-regenerate-group (group &optional reread)
3728 "Regenerate GROUP.
3729 If REREAD is t, all articles in the .overview are marked as unread.
3730 If REREAD is a list, the specified articles will be marked as unread.
3731 In addition, their NOV entries in .overview will be refreshed using
3732 the articles' current headers.
3733 If REREAD is not nil, downloaded articles are marked as unread."
3734 (interactive
3735 (list (let ((def (or (gnus-group-group-name)
3736 gnus-newsgroup-name)))
3737 (let ((select (read-string (if def
3738 (concat "Group Name ("
3739 def "): ")
3740 "Group Name: "))))
3741 (if (and (equal "" select)
3742 def)
3744 select)))
3745 (catch 'mark
3746 (while (let (c
3747 (cursor-in-echo-area t)
3748 (echo-keystrokes 0))
3749 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3750 (setq c (read-char-exclusive))
3752 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3753 (throw 'mark nil))
3754 ((or (eq c ?a) (eq c ?A))
3755 (throw 'mark t))
3756 ((or (eq c ?d) (eq c ?D))
3757 (throw 'mark 'some)))
3758 (gnus-message 3 "Ignoring unexpected input")
3759 (sit-for 1)
3760 t)))))
3761 (when group
3762 (gnus-message 5 "Regenerating in %s" group)
3763 (let* ((gnus-command-method (or gnus-command-method
3764 (gnus-find-method-for-group group)))
3765 (file (gnus-agent-article-name ".overview" group))
3766 (dir (file-name-directory file))
3767 point
3768 (downloaded (if (file-exists-p dir)
3769 (sort (delq nil (mapcar (lambda (name)
3770 (and (not (file-directory-p (nnheader-concat dir name)))
3771 (string-to-number name)))
3772 (directory-files dir nil "^[0-9]+$" t)))
3774 (progn (gnus-make-directory dir) nil)))
3775 dl nov-arts
3776 alist header
3777 regenerated)
3779 (mm-with-unibyte-buffer
3780 (if (file-exists-p file)
3781 (let ((nnheader-file-coding-system
3782 gnus-agent-file-coding-system))
3783 (nnheader-insert-file-contents file)))
3784 (set-buffer-modified-p nil)
3786 ;; Load the article IDs found in the overview file. As a
3787 ;; side-effect, validate the file contents.
3788 (let ((load t))
3789 (while load
3790 (setq load nil)
3791 (goto-char (point-min))
3792 (while (< (point) (point-max))
3793 (cond ((and (looking-at "[0-9]+\t")
3794 (<= (- (match-end 0) (match-beginning 0)) 9))
3795 (push (read (current-buffer)) nov-arts)
3796 (forward-line 1)
3797 (let ((l1 (car nov-arts))
3798 (l2 (cadr nov-arts)))
3799 (cond ((and (listp reread) (memq l1 reread))
3800 (gnus-delete-line)
3801 (setq nov-arts (cdr nov-arts))
3802 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3803 entry of article %s deleted." l1))
3804 ((not l2)
3805 nil)
3806 ((< l1 l2)
3807 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3808 entries are NOT in ascending order.")
3809 ;; Don't sort now as I haven't verified
3810 ;; that every line begins with a number
3811 (setq load t))
3812 ((= l1 l2)
3813 (forward-line -1)
3814 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3815 entries contained duplicate of article %s. Duplicate deleted." l1)
3816 (gnus-delete-line)
3817 (setq nov-arts (cdr nov-arts))))))
3819 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3820 entries contained line that did not begin with an article number. Deleted\
3821 line.")
3822 (gnus-delete-line))))
3823 (when load
3824 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3825 entries into ascending order.")
3826 (sort-numeric-fields 1 (point-min) (point-max))
3827 (setq nov-arts nil))))
3828 (gnus-agent-check-overview-buffer)
3830 ;; Construct a new article alist whose nodes match every header
3831 ;; in the .overview file. As a side-effect, missing headers are
3832 ;; reconstructed from the downloaded article file.
3833 (while (or downloaded nov-arts)
3834 (cond ((and downloaded
3835 (or (not nov-arts)
3836 (> (car downloaded) (car nov-arts))))
3837 ;; This entry is missing from the overview file
3838 (gnus-message 3 "Regenerating NOV %s %d..." group
3839 (car downloaded))
3840 (let ((file (concat dir (number-to-string (car downloaded)))))
3841 (mm-with-unibyte-buffer
3842 (nnheader-insert-file-contents file)
3843 (nnheader-remove-body)
3844 (setq header (nnheader-parse-naked-head)))
3845 (mail-header-set-number header (car downloaded))
3846 (if nov-arts
3847 (let ((key (concat "^" (int-to-string (car nov-arts))
3848 "\t")))
3849 (or (re-search-backward key nil t)
3850 (re-search-forward key))
3851 (forward-line 1))
3852 (goto-char (point-min)))
3853 (nnheader-insert-nov header))
3854 (setq nov-arts (cons (car downloaded) nov-arts)))
3855 ((eq (car downloaded) (car nov-arts))
3856 ;; This entry in the overview has been downloaded
3857 (push (cons (car downloaded)
3858 (time-to-days
3859 (nth 5 (file-attributes
3860 (concat dir (number-to-string
3861 (car downloaded))))))) alist)
3862 (setq downloaded (cdr downloaded))
3863 (setq nov-arts (cdr nov-arts)))
3865 ;; This entry in the overview has not been downloaded
3866 (push (cons (car nov-arts) nil) alist)
3867 (setq nov-arts (cdr nov-arts)))))
3869 ;; When gnus-agent-consider-all-articles is set,
3870 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3871 ;; the alist. Those IDs serve as markers to indicate that an
3872 ;; attempt has been made to fetch that article's header.
3874 ;; When gnus-agent-consider-all-articles is NOT set,
3875 ;; gnus-agent-regenerate-group can remove the article ID of every
3876 ;; article (with the exception of the last ID in the list - it's
3877 ;; special) that no longer appears in the overview. In this
3878 ;; situtation, the last article ID in the list implies that it,
3879 ;; and every article ID preceeding it, have been fetched from the
3880 ;; server.
3882 (if gnus-agent-consider-all-articles
3883 ;; Restore all article IDs that were not found in the overview file.
3884 (let* ((n (cons nil alist))
3885 (merged n)
3886 (o (gnus-agent-load-alist group)))
3887 (while o
3888 (let ((nID (caadr n))
3889 (oID (caar o)))
3890 (cond ((not nID)
3891 (setq n (setcdr n (list (list oID))))
3892 (setq o (cdr o)))
3893 ((< oID nID)
3894 (setcdr n (cons (list oID) (cdr n)))
3895 (setq o (cdr o)))
3896 ((= oID nID)
3897 (setq o (cdr o))
3898 (setq n (cdr n)))
3900 (setq n (cdr n))))))
3901 (setq alist (cdr merged)))
3902 ;; Restore the last article ID if it is not already in the new alist
3903 (let ((n (last alist))
3904 (o (last (gnus-agent-load-alist group))))
3905 (cond ((not o)
3906 nil)
3907 ((not n)
3908 (push (cons (caar o) nil) alist))
3909 ((< (caar n) (caar o))
3910 (setcdr n (list (car o)))))))
3912 (let ((inhibit-quit t))
3913 (if (setq regenerated (buffer-modified-p))
3914 (let ((coding-system-for-write gnus-agent-file-coding-system))
3915 (write-region (point-min) (point-max) file nil 'silent)))
3917 (setq regenerated (or regenerated
3918 (and reread gnus-agent-article-alist)
3919 (not (equal alist gnus-agent-article-alist))))
3921 (setq gnus-agent-article-alist alist)
3923 (when regenerated
3924 (gnus-agent-save-alist group)
3926 ;; I have to alter the group's active range NOW as
3927 ;; gnus-make-ascending-articles-unread will use it to
3928 ;; recalculate the number of unread articles in the group
3930 (let ((group (gnus-group-real-name group))
3931 (group-active (or (gnus-active group)
3932 (gnus-activate-group group))))
3933 (gnus-agent-possibly-alter-active group group-active)))))
3935 (when (and reread gnus-agent-article-alist)
3936 (gnus-agent-synchronize-group-flags
3937 group
3938 (list (list
3939 (if (listp reread)
3940 reread
3941 (delq nil (mapcar (function (lambda (c)
3942 (cond ((eq reread t)
3943 (car c))
3944 ((cdr c)
3945 (car c)))))
3946 gnus-agent-article-alist)))
3947 'del '(read)))
3948 gnus-command-method)
3950 (when (gnus-buffer-live-p gnus-group-buffer)
3951 (gnus-group-update-group group t)))
3953 (gnus-message 5 "")
3954 regenerated)))
3956 ;;;###autoload
3957 (defun gnus-agent-regenerate (&optional clean reread)
3958 "Regenerate all agent covered files.
3959 If CLEAN, obsolete (ignore)."
3960 (interactive "P")
3961 (let (regenerated)
3962 (gnus-message 4 "Regenerating Gnus agent files...")
3963 (dolist (gnus-command-method (gnus-agent-covered-methods))
3964 (dolist (group (gnus-groups-from-server gnus-command-method))
3965 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3966 regenerated))))
3967 (gnus-message 4 "Regenerating Gnus agent files...done")
3969 regenerated))
3971 (defun gnus-agent-go-online (&optional force)
3972 "Switch servers into online status."
3973 (interactive (list t))
3974 (dolist (server gnus-opened-servers)
3975 (when (eq (nth 1 server) 'offline)
3976 (if (if (eq force 'ask)
3977 (gnus-y-or-n-p
3978 (format "Switch %s:%s into online status? "
3979 (caar server) (cadar server)))
3980 force)
3981 (setcar (nthcdr 1 server) 'close)))))
3983 (defun gnus-agent-toggle-group-plugged (group)
3984 "Toggle the status of the server of the current group."
3985 (interactive (list (gnus-group-group-name)))
3986 (let* ((method (gnus-find-method-for-group group))
3987 (status (cadr (assoc method gnus-opened-servers))))
3988 (if (eq status 'offline)
3989 (gnus-server-set-status method 'closed)
3990 (gnus-close-server method)
3991 (gnus-server-set-status method 'offline))
3992 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3993 (if (eq status 'offline) 'offline 'online)
3994 (if (eq status 'offline) 'online 'offline))))
3996 (defun gnus-agent-group-covered-p (group)
3997 (gnus-agent-method-p (gnus-group-method group)))
3999 (provide 'gnus-agent)
4001 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
4002 ;;; gnus-agent.el ends here