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