Merge from emacs-23
[emacs.git] / lisp / gnus / gnus-agent.el
blob2c215e85862b0626053b5c5580661fdd67f7bd4d
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, 2011 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 (unless gnus-agent-send-mail-function
689 (setq gnus-agent-send-mail-function
690 (or message-send-mail-real-function
691 (function (lambda () (funcall message-send-mail-function))))
692 message-send-mail-real-function 'gnus-agent-send-mail))
694 ;; If the servers file doesn't exist, auto-agentize some servers and
695 ;; save the servers file so this auto-agentizing isn't invoked
696 ;; again.
697 (when (and (not (file-exists-p (nnheader-concat
698 gnus-agent-directory "lib/servers")))
699 gnus-agent-auto-agentize-methods)
700 (gnus-message 3 "First time agent user, agentizing remote groups...")
701 (mapc
702 (lambda (server-or-method)
703 (let ((method (gnus-server-to-method server-or-method)))
704 (when (memq (car method)
705 gnus-agent-auto-agentize-methods)
706 (push (gnus-method-to-server method)
707 gnus-agent-covered-methods)
708 (setq gnus-agent-method-p-cache nil))))
709 (cons gnus-select-method gnus-secondary-select-methods))
710 (gnus-agent-write-servers)))
712 (defun gnus-agent-queue-setup (&optional group-name)
713 "Make sure the queue group exists.
714 Optional arg GROUP-NAME allows to specify another group."
715 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
716 gnus-newsrc-hashtb)
717 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
718 (let ((gnus-level-default-subscribed 1))
719 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
720 nil '(nndraft "")))
721 (gnus-group-set-parameter
722 (format "nndraft:%s" (or group-name "queue"))
723 'gnus-dummy '((gnus-draft-mode)))))
725 (defun gnus-agent-send-mail ()
726 (if (or (not gnus-agent-queue-mail)
727 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
728 (funcall gnus-agent-send-mail-function)
729 (goto-char (point-min))
730 (re-search-forward
731 (concat "^" (regexp-quote mail-header-separator) "\n"))
732 (replace-match "\n")
733 (gnus-agent-insert-meta-information 'mail)
734 (gnus-request-accept-article "nndraft:queue" nil t t)))
736 (defun gnus-agent-insert-meta-information (type &optional method)
737 "Insert meta-information into the message that says how it's to be posted.
738 TYPE can be either `mail' or `news'. If the latter, then METHOD can
739 be a select method."
740 (save-excursion
741 (message-remove-header gnus-agent-meta-information-header)
742 (goto-char (point-min))
743 (insert gnus-agent-meta-information-header ": "
744 (symbol-name type) " " (format "%S" method)
745 "\n")
746 (forward-char -1)
747 (while (search-backward "\n" nil t)
748 (replace-match "\\n" t t))))
750 (defun gnus-agent-restore-gcc ()
751 "Restore GCC field from saved header."
752 (save-excursion
753 (goto-char (point-min))
754 (while (re-search-forward
755 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
756 (replace-match "Gcc:" 'fixedcase))))
758 (defun gnus-agent-any-covered-gcc ()
759 (save-restriction
760 (message-narrow-to-headers)
761 (let* ((gcc (mail-fetch-field "gcc" nil t))
762 (methods (and gcc
763 (mapcar 'gnus-inews-group-method
764 (message-unquote-tokens
765 (message-tokenize-header
766 gcc " ,")))))
767 covered)
768 (while (and (not covered) methods)
769 (setq covered (gnus-agent-method-p (car methods))
770 methods (cdr methods)))
771 covered)))
773 ;;;###autoload
774 (defun gnus-agent-possibly-save-gcc ()
775 "Save GCC if Gnus is unplugged."
776 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
777 (save-excursion
778 (goto-char (point-min))
779 (let ((case-fold-search t))
780 (while (re-search-forward "^gcc:" nil t)
781 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
783 (defun gnus-agent-possibly-do-gcc ()
784 "Do GCC if Gnus is plugged."
785 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
786 (gnus-inews-do-gcc)))
789 ;;; Group mode commands
792 (defun gnus-agent-fetch-groups (n)
793 "Put all new articles in the current groups into the Agent."
794 (interactive "P")
795 (unless gnus-plugged
796 (error "Groups can't be fetched when Gnus is unplugged"))
797 (gnus-group-iterate n 'gnus-agent-fetch-group))
799 (defun gnus-agent-fetch-group (&optional group)
800 "Put all new articles in GROUP into the Agent."
801 (interactive (list (gnus-group-group-name)))
802 (setq group (or group gnus-newsgroup-name))
803 (unless group
804 (error "No group on the current line"))
805 (if (not (gnus-agent-group-covered-p group))
806 (message "%s isn't covered by the agent" group)
807 (gnus-agent-while-plugged
808 (let ((gnus-command-method (gnus-find-method-for-group group)))
809 (gnus-agent-with-fetch
810 (gnus-agent-fetch-group-1 group gnus-command-method)
811 (gnus-message 5 "Fetching %s...done" group))))))
813 (defun gnus-agent-add-group (category arg)
814 "Add the current group to an agent category."
815 (interactive
816 (list
817 (intern
818 (gnus-completing-read
819 "Add to category"
820 (mapcar (lambda (cat) (symbol-name (car cat)))
821 gnus-category-alist)
823 current-prefix-arg))
824 (let ((cat (assq category gnus-category-alist))
825 c groups)
826 (gnus-group-iterate arg
827 (lambda (group)
828 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
829 (setf (gnus-agent-cat-groups c)
830 (delete group (gnus-agent-cat-groups c))))
831 (push group groups)))
832 (setf (gnus-agent-cat-groups cat)
833 (nconc (gnus-agent-cat-groups cat) groups))
834 (gnus-category-write)))
836 (defun gnus-agent-remove-group (arg)
837 "Remove the current group from its agent category, if any."
838 (interactive "P")
839 (let (c)
840 (gnus-group-iterate arg
841 (lambda (group)
842 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
843 (setf (gnus-agent-cat-groups c)
844 (delete group (gnus-agent-cat-groups c))))))
845 (gnus-category-write)))
847 (defun gnus-agent-synchronize-flags ()
848 "Synchronize unplugged flags with servers."
849 (interactive)
850 (save-excursion
851 (dolist (gnus-command-method (gnus-agent-covered-methods))
852 (when (file-exists-p (gnus-agent-lib-file "flags"))
853 (gnus-agent-synchronize-flags-server gnus-command-method)))))
855 (defun gnus-agent-possibly-synchronize-flags ()
856 "Synchronize flags according to `gnus-agent-synchronize-flags'."
857 (interactive)
858 (save-excursion
859 (dolist (gnus-command-method (gnus-agent-covered-methods))
860 (when (eq (gnus-server-status gnus-command-method) 'ok)
861 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
863 (defun gnus-agent-synchronize-flags-server (method)
864 "Synchronize flags set when unplugged for server."
865 (let ((gnus-command-method method)
866 (gnus-agent nil))
867 (when (file-exists-p (gnus-agent-lib-file "flags"))
868 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
869 (erase-buffer)
870 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
871 (cond ((null gnus-plugged)
872 (gnus-message
873 1 "You must be plugged to synchronize flags with server %s"
874 (nth 1 gnus-command-method)))
875 ((null (gnus-check-server gnus-command-method))
876 (gnus-message
877 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
879 (condition-case err
880 (while t
881 (let ((bgn (point)))
882 (eval (read (current-buffer)))
883 (delete-region bgn (point))))
884 (end-of-file
885 (delete-file (gnus-agent-lib-file "flags")))
886 (error
887 (let ((file (gnus-agent-lib-file "flags")))
888 (write-region (point-min) (point-max)
889 (gnus-agent-lib-file "flags") nil 'silent)
890 (error "Couldn't set flags from file %s due to %s"
891 file (error-message-string err)))))))
892 (kill-buffer nil))))
894 (defun gnus-agent-possibly-synchronize-flags-server (method)
895 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
896 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
897 (or (and gnus-agent-synchronize-flags
898 (not (eq gnus-agent-synchronize-flags 'ask)))
899 (and (eq gnus-agent-synchronize-flags 'ask)
900 (gnus-y-or-n-p
901 (format "Synchronize flags on server `%s'? "
902 (cadr method))))))
903 (gnus-agent-synchronize-flags-server method)))
905 ;;;###autoload
906 (defun gnus-agent-rename-group (old-group new-group)
907 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
908 Always updates the agent, even when disabled, as the old agent
909 files would corrupt gnus when the agent was next enabled.
910 Depends upon the caller to determine whether group renaming is
911 supported."
912 (let* ((old-command-method (gnus-find-method-for-group old-group))
913 (old-path (directory-file-name
914 (let (gnus-command-method old-command-method)
915 (gnus-agent-group-pathname old-group))))
916 (new-command-method (gnus-find-method-for-group new-group))
917 (new-path (directory-file-name
918 (let (gnus-command-method new-command-method)
919 (gnus-agent-group-pathname new-group))))
920 (file-name-coding-system nnmail-pathname-coding-system))
921 (gnus-rename-file old-path new-path t)
923 (let* ((old-real-group (gnus-group-real-name old-group))
924 (new-real-group (gnus-group-real-name new-group))
925 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
926 (gnus-agent-save-group-info old-command-method old-real-group nil)
927 (gnus-agent-save-group-info new-command-method new-real-group old-active)
929 (let ((old-local (gnus-agent-get-local old-group
930 old-real-group old-command-method)))
931 (gnus-agent-set-local old-group
932 nil nil
933 old-real-group old-command-method)
934 (gnus-agent-set-local new-group
935 (car old-local) (cdr old-local)
936 new-real-group new-command-method)))))
938 ;;;###autoload
939 (defun gnus-agent-delete-group (group)
940 "Delete fully-qualified GROUP.
941 Always updates the agent, even when disabled, as the old agent
942 files would corrupt gnus when the agent was next enabled.
943 Depends upon the caller to determine whether group deletion is
944 supported."
945 (let* ((command-method (gnus-find-method-for-group group))
946 (path (directory-file-name
947 (let (gnus-command-method command-method)
948 (gnus-agent-group-pathname group))))
949 (file-name-coding-system nnmail-pathname-coding-system))
950 (gnus-delete-directory path)
952 (let* ((real-group (gnus-group-real-name group)))
953 (gnus-agent-save-group-info command-method real-group nil)
955 (let ((local (gnus-agent-get-local group
956 real-group command-method)))
957 (gnus-agent-set-local group
958 nil nil
959 real-group command-method)))))
962 ;;; Server mode commands
965 (defun gnus-agent-add-server ()
966 "Enroll SERVER in the agent program."
967 (interactive)
968 (let* ((server (gnus-server-server-name))
969 (named-server (gnus-server-named-server))
970 (method (and server
971 (gnus-server-get-method nil server))))
972 (unless server
973 (error "No server on the current line"))
975 (when (gnus-agent-method-p method)
976 (error "Server already in the agent program"))
978 (push named-server gnus-agent-covered-methods)
980 (setq gnus-agent-method-p-cache nil)
981 (gnus-server-update-server server)
982 (gnus-agent-write-servers)
983 (gnus-message 1 "Entered %s into the Agent" server)))
985 (defun gnus-agent-remove-server ()
986 "Remove SERVER from the agent program."
987 (interactive)
988 (let* ((server (gnus-server-server-name))
989 (named-server (gnus-server-named-server)))
990 (unless server
991 (error "No server on the current line"))
993 (unless (member named-server gnus-agent-covered-methods)
994 (error "Server not in the agent program"))
996 (setq gnus-agent-covered-methods
997 (delete named-server gnus-agent-covered-methods)
998 gnus-agent-method-p-cache nil)
1000 (gnus-server-update-server server)
1001 (gnus-agent-write-servers)
1002 (gnus-message 1 "Removed %s from the agent" server)))
1004 (defun gnus-agent-read-servers ()
1005 "Read the alist of covered servers."
1006 (setq gnus-agent-covered-methods
1007 (gnus-agent-read-file
1008 (nnheader-concat gnus-agent-directory "lib/servers"))
1009 gnus-agent-method-p-cache nil)
1011 ;; I am called so early in start-up that I can not validate server
1012 ;; names. When that is the case, I skip the validation. That is
1013 ;; alright as the gnus startup code calls the validate methods
1014 ;; directly.
1015 (if gnus-server-alist
1016 (gnus-agent-read-servers-validate)))
1018 (defun gnus-agent-read-servers-validate ()
1019 (mapcar (lambda (server-or-method)
1020 (let* ((server (if (stringp server-or-method)
1021 server-or-method
1022 (gnus-method-to-server server-or-method)))
1023 (method (gnus-server-to-method server)))
1024 (if method
1025 (unless (member server gnus-agent-covered-methods)
1026 (push server gnus-agent-covered-methods)
1027 (setq gnus-agent-method-p-cache nil))
1028 (gnus-message 8 "Ignoring disappeared server `%s'" server))))
1029 (prog1 gnus-agent-covered-methods
1030 (setq gnus-agent-covered-methods nil))))
1032 (defun gnus-agent-read-servers-validate-native (native-method)
1033 (setq gnus-agent-covered-methods
1034 (mapcar (lambda (method)
1035 (if (or (not method)
1036 (equal method native-method))
1037 "native"
1038 method)) gnus-agent-covered-methods)))
1040 (defun gnus-agent-write-servers ()
1041 "Write the alist of covered servers."
1042 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1043 (let ((coding-system-for-write nnheader-file-coding-system)
1044 (file-name-coding-system nnmail-pathname-coding-system))
1045 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1046 (prin1 gnus-agent-covered-methods
1047 (current-buffer)))))
1050 ;;; Summary commands
1053 (defun gnus-agent-mark-article (n &optional unmark)
1054 "Mark the next N articles as downloadable.
1055 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1056 the mark instead. The difference between N and the actual number of
1057 articles marked is returned."
1058 (interactive "p")
1059 (let ((backward (< n 0))
1060 (n (abs n)))
1061 (while (and
1062 (> n 0)
1063 (progn
1064 (gnus-summary-set-agent-mark
1065 (gnus-summary-article-number) unmark)
1066 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1067 (setq n (1- n)))
1068 (when (/= 0 n)
1069 (gnus-message 7 "No more articles"))
1070 (gnus-summary-recenter)
1071 (gnus-summary-position-point)
1074 (defun gnus-agent-unmark-article (n)
1075 "Remove the downloadable mark from the next N articles.
1076 If N is negative, unmark backward instead. The difference between N and
1077 the actual number of articles unmarked is returned."
1078 (interactive "p")
1079 (gnus-agent-mark-article n t))
1081 (defun gnus-agent-toggle-mark (n)
1082 "Toggle the downloadable mark from the next N articles.
1083 If N is negative, toggle backward instead. The difference between N and
1084 the actual number of articles toggled is returned."
1085 (interactive "p")
1086 (gnus-agent-mark-article n 'toggle))
1088 (defun gnus-summary-set-agent-mark (article &optional unmark)
1089 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1090 When UNMARK is t, the article is unmarked. For any other value, the
1091 article's mark is toggled."
1092 (let ((unmark (cond ((eq nil unmark)
1093 nil)
1094 ((eq t unmark)
1097 (memq article gnus-newsgroup-downloadable)))))
1098 (when (gnus-summary-goto-subject article nil t)
1099 (gnus-summary-update-mark
1100 (if unmark
1101 (progn
1102 (setq gnus-newsgroup-downloadable
1103 (delq article gnus-newsgroup-downloadable))
1104 (gnus-article-mark article))
1105 (setq gnus-newsgroup-downloadable
1106 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1107 gnus-downloadable-mark)
1108 'unread))))
1110 ;;;###autoload
1111 (defun gnus-agent-get-undownloaded-list ()
1112 "Construct list of articles that have not been downloaded."
1113 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1114 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1115 (gnus-agent-method-p gnus-command-method))
1116 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1117 (headers (sort (mapcar (lambda (h)
1118 (mail-header-number h))
1119 gnus-newsgroup-headers) '<))
1120 (cached (and gnus-use-cache gnus-newsgroup-cached))
1121 (undownloaded (list nil))
1122 (tail-undownloaded undownloaded)
1123 (unfetched (list nil))
1124 (tail-unfetched unfetched))
1125 (while (and alist headers)
1126 (let ((a (caar alist))
1127 (h (car headers)))
1128 (cond ((< a h)
1129 ;; Ignore IDs in the alist that are not being
1130 ;; displayed in the summary.
1131 (setq alist (cdr alist)))
1132 ((> a h)
1133 ;; Headers that are not in the alist should be
1134 ;; fictious (see nnagent-retrieve-headers); they
1135 ;; imply that this article isn't in the agent.
1136 (gnus-agent-append-to-list tail-undownloaded h)
1137 (gnus-agent-append-to-list tail-unfetched h)
1138 (setq headers (cdr headers)))
1139 ((cdar alist)
1140 (setq alist (cdr alist))
1141 (setq headers (cdr headers))
1142 nil ; ignore already downloaded
1145 (setq alist (cdr alist))
1146 (setq headers (cdr headers))
1148 ;; This article isn't in the agent. Check to see
1149 ;; if it is in the cache. If it is, it's been
1150 ;; downloaded.
1151 (while (and cached (< (car cached) a))
1152 (setq cached (cdr cached)))
1153 (unless (equal a (car cached))
1154 (gnus-agent-append-to-list tail-undownloaded a))))))
1156 (while headers
1157 (let ((num (pop headers)))
1158 (gnus-agent-append-to-list tail-undownloaded num)
1159 (gnus-agent-append-to-list tail-unfetched num)))
1161 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1162 gnus-newsgroup-unfetched (cdr unfetched))))))
1164 (defun gnus-agent-catchup ()
1165 "Mark as read all unhandled articles.
1166 An article is unhandled if it is neither cached, nor downloaded, nor
1167 downloadable."
1168 (interactive)
1169 (save-excursion
1170 (let ((articles gnus-newsgroup-undownloaded))
1171 (when (or gnus-newsgroup-downloadable
1172 gnus-newsgroup-cached)
1173 (setq articles (gnus-sorted-ndifference
1174 (gnus-sorted-ndifference
1175 (gnus-copy-sequence articles)
1176 gnus-newsgroup-downloadable)
1177 gnus-newsgroup-cached)))
1179 (while articles
1180 (gnus-summary-mark-article
1181 (pop articles) gnus-catchup-mark)))
1182 (gnus-summary-position-point)))
1184 (defun gnus-agent-summary-fetch-series ()
1185 (interactive)
1186 (when gnus-newsgroup-processable
1187 (setq gnus-newsgroup-downloadable
1188 (let* ((dl gnus-newsgroup-downloadable)
1189 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1190 (gnus-newsgroup-downloadable processable))
1191 (gnus-agent-summary-fetch-group)
1193 ;; For each article that I processed that is no longer
1194 ;; undownloaded, remove its processable mark.
1196 (mapc #'gnus-summary-remove-process-mark
1197 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1199 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1200 ;; updated the temporary gnus-newsgroup-downloadable to
1201 ;; remove each article successfully fetched. Now, I
1202 ;; update the real gnus-newsgroup-downloadable to only
1203 ;; include undownloaded articles.
1204 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1206 (defun gnus-agent-summary-fetch-group (&optional all)
1207 "Fetch the downloadable articles in the group.
1208 Optional arg ALL, if non-nil, means to fetch all articles."
1209 (interactive "P")
1210 (let ((articles
1211 (if all gnus-newsgroup-articles
1212 gnus-newsgroup-downloadable))
1213 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1214 fetched-articles)
1215 (gnus-agent-while-plugged
1216 (unless articles
1217 (error "No articles to download"))
1218 (gnus-agent-with-fetch
1219 (setq gnus-newsgroup-undownloaded
1220 (gnus-sorted-ndifference
1221 gnus-newsgroup-undownloaded
1222 (setq fetched-articles
1223 (gnus-agent-fetch-articles
1224 gnus-newsgroup-name articles)))))
1225 (save-excursion
1226 (dolist (article articles)
1227 (let ((was-marked-downloadable
1228 (memq article gnus-newsgroup-downloadable)))
1229 (cond (gnus-agent-mark-unread-after-downloaded
1230 (setq gnus-newsgroup-downloadable
1231 (delq article gnus-newsgroup-downloadable))
1233 (gnus-summary-mark-article article gnus-unread-mark))
1234 (was-marked-downloadable
1235 (gnus-summary-set-agent-mark article t)))
1236 (when (gnus-summary-goto-subject article nil t)
1237 (gnus-summary-update-download-mark article))))))
1238 fetched-articles))
1240 (defun gnus-agent-fetch-selected-article ()
1241 "Fetch the current article as it is selected.
1242 This can be added to `gnus-select-article-hook' or
1243 `gnus-mark-article-hook'."
1244 (let ((gnus-command-method gnus-current-select-method))
1245 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1246 (when (gnus-agent-fetch-articles
1247 gnus-newsgroup-name
1248 (list gnus-current-article))
1249 (setq gnus-newsgroup-undownloaded
1250 (delq gnus-current-article gnus-newsgroup-undownloaded))
1251 (gnus-summary-update-download-mark gnus-current-article)))))
1254 ;;; Internal functions
1257 (defun gnus-agent-synchronize-group-flags (group actions server)
1258 "Update a plugged group by performing the indicated actions."
1259 (let* ((gnus-command-method (gnus-server-to-method server))
1260 (info
1261 ;; This initializer is required as gnus-request-set-mark
1262 ;; calls gnus-group-real-name to strip off the host name
1263 ;; before calling the backend. Now that the backend is
1264 ;; trying to call gnus-request-set-mark, I have to
1265 ;; reconstruct the original group name.
1266 (or (gnus-get-info group)
1267 (gnus-get-info
1268 (setq group (gnus-group-full-name
1269 group gnus-command-method))))))
1270 (gnus-request-set-mark group actions)
1272 (when info
1273 (dolist (action actions)
1274 (let ((range (nth 0 action))
1275 (what (nth 1 action))
1276 (marks (nth 2 action)))
1277 (dolist (mark marks)
1278 (cond ((eq mark 'read)
1279 (gnus-info-set-read
1280 info
1281 (funcall (if (eq what 'add)
1282 'gnus-range-add
1283 'gnus-remove-from-range)
1284 (gnus-info-read info)
1285 range))
1286 (gnus-get-unread-articles-in-group
1287 info
1288 (gnus-active (gnus-info-group info))))
1289 ((memq mark '(tick))
1290 (let ((info-marks (assoc mark (gnus-info-marks info))))
1291 (unless info-marks
1292 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1293 (setcdr info-marks (funcall (if (eq what 'add)
1294 'gnus-range-add
1295 'gnus-remove-from-range)
1296 (cdr info-marks)
1297 range))))))))
1299 ;;Marks can be synchronized at any time by simply toggling from
1300 ;;unplugged to plugged. If that is what is happening right now, make
1301 ;;sure that the group buffer is up to date.
1302 (when (gnus-buffer-live-p gnus-group-buffer)
1303 (gnus-group-update-group group t)))
1304 nil))
1306 (defun gnus-agent-save-active (method)
1307 (when (gnus-agent-method-p method)
1308 (let* ((gnus-command-method method)
1309 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1310 (file (gnus-agent-lib-file "active")))
1311 (gnus-active-to-gnus-format nil new)
1312 (gnus-agent-write-active file new)
1313 (erase-buffer)
1314 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
1315 (nnheader-insert-file-contents file)))))
1317 (defun gnus-agent-write-active (file new)
1318 (gnus-make-directory (file-name-directory file))
1319 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1320 ;; The hashtable contains real names of groups. However, do NOT
1321 ;; add the foreign server prefix as gnus-active-to-gnus-format
1322 ;; will add it while reading the file.
1323 (gnus-write-active-file file new nil)))
1325 ;;;###autoload
1326 (defun gnus-agent-possibly-alter-active (group active &optional info)
1327 "Possibly expand a group's active range to include articles
1328 downloaded into the agent."
1329 (let* ((gnus-command-method (or gnus-command-method
1330 (gnus-find-method-for-group group))))
1331 (when (gnus-agent-method-p gnus-command-method)
1332 (let* ((local (gnus-agent-get-local group))
1333 (active-min (or (car active) 0))
1334 (active-max (or (cdr active) 0))
1335 (agent-min (or (car local) active-min))
1336 (agent-max (or (cdr local) active-max)))
1338 (when (< agent-min active-min)
1339 (setcar active agent-min))
1341 (when (> agent-max active-max)
1342 (setcdr active agent-max))
1344 (when (and info (< agent-max (- active-min 100)))
1345 ;; I'm expanding the active range by such a large amount
1346 ;; that there is a gap of more than 100 articles between the
1347 ;; last article known to the agent and the first article
1348 ;; currently available on the server. This gap contains
1349 ;; articles that have been lost, mark them as read so that
1350 ;; gnus doesn't waste resources trying to fetch them.
1352 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1353 ;; want to modify the local file everytime someone restarts
1354 ;; gnus. The small gap will cause a tiny performance hit
1355 ;; when gnus tries, and fails, to retrieve the articles.
1356 ;; Still that should be smaller than opening a buffer,
1357 ;; printing this list to the buffer, and then writing it to a
1358 ;; file.
1360 (let ((read (gnus-info-read info)))
1361 (gnus-info-set-read
1362 info
1363 (gnus-range-add
1364 read
1365 (list (cons (1+ agent-max)
1366 (1- active-min))))))
1368 ;; Lie about the agent's local range for this group to
1369 ;; disable the set read each time this server is opened.
1370 ;; NOTE: Opening this group will restore the valid local
1371 ;; range but it will also expand the local range to
1372 ;; incompass the new active range.
1373 (gnus-agent-set-local group agent-min (1- active-min)))))))
1375 (defun gnus-agent-save-group-info (method group active)
1376 "Update a single group's active range in the agent's copy of the server's active file."
1377 (when (gnus-agent-method-p method)
1378 (let* ((gnus-command-method (or method gnus-command-method))
1379 (coding-system-for-write nnheader-file-coding-system)
1380 (file-name-coding-system nnmail-pathname-coding-system)
1381 (file (gnus-agent-lib-file "active"))
1382 oactive-min oactive-max)
1383 (gnus-make-directory (file-name-directory file))
1384 (with-temp-file file
1385 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1386 (mm-disable-multibyte)
1387 (when (file-exists-p file)
1388 (nnheader-insert-file-contents file)
1390 (goto-char (point-min))
1391 (when (re-search-forward
1392 (concat "^" (regexp-quote group) " ") nil t)
1393 (save-excursion
1394 (setq oactive-max (read (current-buffer)) ;; max
1395 oactive-min (read (current-buffer)))) ;; min
1396 (gnus-delete-line)))
1397 (when active
1398 (insert (format "%S %d %d y\n" (intern group)
1399 (max (or oactive-max (cdr active)) (cdr active))
1400 (min (or oactive-min (car active)) (car active))))
1401 (goto-char (point-max))
1402 (while (search-backward "\\." nil t)
1403 (delete-char 1)))))))
1405 (defun gnus-agent-get-group-info (method group)
1406 "Get a single group's active range in the agent's copy of the server's active file."
1407 (when (gnus-agent-method-p method)
1408 (let* ((gnus-command-method (or method gnus-command-method))
1409 (coding-system-for-write nnheader-file-coding-system)
1410 (file-name-coding-system nnmail-pathname-coding-system)
1411 (file (gnus-agent-lib-file "active"))
1412 oactive-min oactive-max)
1413 (gnus-make-directory (file-name-directory file))
1414 (with-temp-buffer
1415 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1416 (mm-disable-multibyte)
1417 (when (file-exists-p file)
1418 (nnheader-insert-file-contents file)
1420 (goto-char (point-min))
1421 (when (re-search-forward
1422 (concat "^" (regexp-quote group) " ") nil t)
1423 (save-excursion
1424 (setq oactive-max (read (current-buffer)) ;; max
1425 oactive-min (read (current-buffer))) ;; min
1426 (cons oactive-min oactive-max))))))))
1428 (defvar gnus-agent-decoded-group-names nil
1429 "Alist of non-ASCII group names and decoded ones.")
1431 (defun gnus-agent-decoded-group-name (group)
1432 "Return a decoded group name of GROUP."
1433 (or (cdr (assoc group gnus-agent-decoded-group-names))
1434 (if (string-match "[^\000-\177]" group)
1435 (let ((decoded (gnus-group-decoded-name group)))
1436 (push (cons group decoded) gnus-agent-decoded-group-names)
1437 decoded)
1438 group)))
1440 (defun gnus-agent-group-path (group)
1441 "Translate GROUP into a file name."
1443 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1444 ;; The two methods must be kept synchronized, which is why
1445 ;; gnus-agent-group-pathname was added.
1447 (setq group
1448 (nnheader-translate-file-chars
1449 (nnheader-replace-duplicate-chars-in-string
1450 (nnheader-replace-chars-in-string
1451 (gnus-group-real-name (gnus-agent-decoded-group-name group))
1452 ?/ ?_)
1453 ?. ?_)))
1454 (if (or nnmail-use-long-file-names
1455 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1456 group
1457 (nnheader-replace-chars-in-string group ?. ?/)))
1459 (defun gnus-agent-group-pathname (group)
1460 "Translate GROUP into a file name."
1461 ;; nnagent uses nnmail-group-pathname to read articles while
1462 ;; unplugged. The agent must, therefore, use the same directory
1463 ;; while plugged.
1464 (nnmail-group-pathname
1465 (gnus-group-real-name (gnus-agent-decoded-group-name group))
1466 (if gnus-command-method
1467 (gnus-agent-directory)
1468 (let ((gnus-command-method (gnus-find-method-for-group group)))
1469 (gnus-agent-directory)))))
1471 (defun gnus-agent-get-function (method)
1472 (if (gnus-online method)
1473 (car method)
1474 (require 'nnagent)
1475 'nnagent))
1477 (defun gnus-agent-covered-methods ()
1478 "Return the subset of methods that are covered by the agent."
1479 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1481 ;;; History functions
1483 (defun gnus-agent-history-buffer ()
1484 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1486 (defun gnus-agent-open-history ()
1487 (save-excursion
1488 (push (cons (gnus-agent-method)
1489 (set-buffer (gnus-get-buffer-create
1490 (format " *Gnus agent %s history*"
1491 (gnus-agent-method)))))
1492 gnus-agent-history-buffers)
1493 (mm-disable-multibyte) ;; everything is binary
1494 (erase-buffer)
1495 (insert "\n")
1496 (let ((file (gnus-agent-lib-file "history")))
1497 (when (file-exists-p file)
1498 (nnheader-insert-file-contents file))
1499 (set (make-local-variable 'gnus-agent-file-name) file))))
1501 (defun gnus-agent-close-history ()
1502 (when (gnus-buffer-live-p gnus-agent-current-history)
1503 (kill-buffer gnus-agent-current-history)
1504 (setq gnus-agent-history-buffers
1505 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1506 gnus-agent-history-buffers))))
1509 ;;; Fetching
1512 (defun gnus-agent-fetch-articles (group articles)
1513 "Fetch ARTICLES from GROUP and put them into the Agent."
1514 (when articles
1515 (gnus-agent-load-alist group)
1516 (let* ((alist gnus-agent-article-alist)
1517 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1518 (selected-sets (list nil))
1519 (current-set-size 0)
1520 article
1521 header-number)
1522 ;; Check each article
1523 (while (setq article (pop articles))
1524 ;; Skip alist entries preceeding this article
1525 (while (> article (or (caar alist) (1+ article)))
1526 (setq alist (cdr alist)))
1528 ;; Prune off articles that we have already fetched.
1529 (unless (and (eq article (caar alist))
1530 (cdar alist))
1531 ;; Skip headers preceeding this article
1532 (while (> article
1533 (setq header-number
1534 (let* ((header (car headers)))
1535 (if header
1536 (mail-header-number header)
1537 (1+ article)))))
1538 (setq headers (cdr headers)))
1540 ;; Add this article to the current set
1541 (setcar selected-sets (cons article (car selected-sets)))
1543 ;; Update the set size, when the set is too large start a
1544 ;; new one. I do this after adding the article as I want at
1545 ;; least one article in each set.
1546 (when (< gnus-agent-max-fetch-size
1547 (setq current-set-size
1548 (+ current-set-size
1549 (if (= header-number article)
1550 (let ((char-size (mail-header-chars
1551 (car headers))))
1552 (if (<= char-size 0)
1553 ;; The char size was missing/invalid,
1554 ;; assume a worst-case situation of
1555 ;; 65 char/line. If the line count
1556 ;; is missing, arbitrarily assume a
1557 ;; size of 1000 characters.
1558 (max (* 65 (mail-header-lines
1559 (car headers)))
1560 1000)
1561 char-size))
1562 0))))
1563 (setcar selected-sets (nreverse (car selected-sets)))
1564 (setq selected-sets (cons nil selected-sets)
1565 current-set-size 0))))
1567 (when (or (cdr selected-sets) (car selected-sets))
1568 (let* ((fetched-articles (list nil))
1569 (tail-fetched-articles fetched-articles)
1570 (dir (gnus-agent-group-pathname group))
1571 (date (time-to-days (current-time)))
1572 (case-fold-search t)
1573 pos crosses id
1574 (file-name-coding-system nnmail-pathname-coding-system))
1576 (setcar selected-sets (nreverse (car selected-sets)))
1577 (setq selected-sets (nreverse selected-sets))
1579 (gnus-make-directory dir)
1580 (gnus-message 7 "Fetching articles for %s..."
1581 (gnus-agent-decoded-group-name group))
1583 (unwind-protect
1584 (while (setq articles (pop selected-sets))
1585 ;; Fetch the articles from the backend.
1586 (if (gnus-check-backend-function 'retrieve-articles group)
1587 (setq pos (gnus-retrieve-articles articles group))
1588 (with-temp-buffer
1589 (let (article)
1590 (while (setq article (pop articles))
1591 (gnus-message 10 "Fetching article %s for %s..."
1592 article
1593 (gnus-agent-decoded-group-name group))
1594 (when (or
1595 (gnus-backlog-request-article group article
1596 nntp-server-buffer)
1597 (gnus-request-article article group))
1598 (goto-char (point-max))
1599 (push (cons article (point)) pos)
1600 (insert-buffer-substring nntp-server-buffer)))
1601 (copy-to-buffer
1602 nntp-server-buffer (point-min) (point-max))
1603 (setq pos (nreverse pos)))))
1604 ;; Then save these articles into the Agent.
1605 (with-current-buffer nntp-server-buffer
1606 (while pos
1607 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1608 (goto-char (point-min))
1609 (unless (eobp) ;; Don't save empty articles.
1610 (when (search-forward "\n\n" nil t)
1611 (when (search-backward "\nXrefs: " nil t)
1612 ;; Handle cross posting.
1613 (goto-char (match-end 0)) ; move to end of header name
1614 (skip-chars-forward "^ ") ; skip server name
1615 (skip-chars-forward " ")
1616 (setq crosses nil)
1617 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1618 (push (cons (buffer-substring (match-beginning 1)
1619 (match-end 1))
1620 (string-to-number
1621 (buffer-substring (match-beginning 2)
1622 (match-end 2))))
1623 crosses)
1624 (goto-char (match-end 0)))
1625 (gnus-agent-crosspost crosses (caar pos) date)))
1626 (goto-char (point-min))
1627 (if (not (re-search-forward
1628 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1629 (setq id "No-Message-ID-in-article")
1630 (setq id (buffer-substring
1631 (match-beginning 1) (match-end 1))))
1632 (let ((coding-system-for-write
1633 gnus-agent-file-coding-system))
1634 (write-region (point-min) (point-max)
1635 (concat dir (number-to-string (caar pos)))
1636 nil 'silent))
1638 (gnus-agent-append-to-list
1639 tail-fetched-articles (caar pos)))
1640 (widen)
1641 (setq pos (cdr pos)))))
1643 (gnus-agent-save-alist group (cdr fetched-articles) date)
1644 (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1646 (gnus-message 7 ""))
1647 (cdr fetched-articles))))))
1649 (defun gnus-agent-unfetch-articles (group articles)
1650 "Delete ARTICLES that were fetched from GROUP into the agent."
1651 (when articles
1652 (gnus-agent-with-refreshed-group
1653 group
1654 (gnus-agent-load-alist group)
1655 (let* ((alist (cons nil gnus-agent-article-alist))
1656 (articles (sort articles #'<))
1657 (next-possibility alist)
1658 (delete-this (pop articles)))
1659 (while (and (cdr next-possibility) delete-this)
1660 (let ((have-this (caar (cdr next-possibility))))
1661 (cond
1662 ((< delete-this have-this)
1663 (setq delete-this (pop articles)))
1664 ((= delete-this have-this)
1665 (let ((timestamp (cdar (cdr next-possibility))))
1666 (when timestamp
1667 (let* ((file-name (concat (gnus-agent-group-pathname group)
1668 (number-to-string have-this)))
1669 (size-file
1670 (float (or (and gnus-agent-total-fetched-hashtb
1671 (nth 7 (file-attributes file-name)))
1672 0)))
1673 (file-name-coding-system
1674 nnmail-pathname-coding-system))
1675 (delete-file file-name)
1676 (gnus-agent-update-files-total-fetched-for
1677 group (- size-file)))))
1679 (setcdr next-possibility (cddr next-possibility)))
1681 (setq next-possibility (cdr next-possibility))))))
1682 (setq gnus-agent-article-alist (cdr alist))
1683 (gnus-agent-save-alist group)))))
1685 (defun gnus-agent-crosspost (crosses article &optional date)
1686 (setq date (or date t))
1688 (let (gnus-agent-article-alist group alist beg end)
1689 (with-current-buffer gnus-agent-overview-buffer
1690 (when (nnheader-find-nov-line article)
1691 (forward-word 1)
1692 (setq beg (point))
1693 (setq end (progn (forward-line 1) (point)))))
1694 (while crosses
1695 (setq group (caar crosses))
1696 (unless (setq alist (assoc group gnus-agent-group-alist))
1697 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1698 gnus-agent-group-alist))
1699 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1700 (with-current-buffer (gnus-get-buffer-create
1701 (format " *Gnus agent overview %s*"group))
1702 (when (= (point-max) (point-min))
1703 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1704 (ignore-errors
1705 (let ((file-name-coding-system nnmail-pathname-coding-system))
1706 (nnheader-insert-file-contents
1707 (gnus-agent-article-name ".overview" group)))))
1708 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1709 (insert (string-to-number (cdar crosses)))
1710 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1711 (gnus-agent-check-overview-buffer))
1712 (setq crosses (cdr crosses)))))
1714 (defun gnus-agent-backup-overview-buffer ()
1715 (when gnus-newsgroup-name
1716 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1717 (cnt 0)
1718 name
1719 (file-name-coding-system nnmail-pathname-coding-system))
1720 (while (file-exists-p
1721 (setq name (concat root "~"
1722 (int-to-string (setq cnt (1+ cnt))) "~"))))
1723 (write-region (point-min) (point-max) name nil 'no-msg)
1724 (gnus-message 1 "Created backup copy of overview in %s." name)))
1727 (defun gnus-agent-check-overview-buffer (&optional buffer)
1728 "Check the overview file given for sanity.
1729 In particular, checks that the file is sorted by article number
1730 and that there are no duplicates."
1731 (let ((prev-num -1)
1732 (backed-up nil))
1733 (save-excursion
1734 (when buffer
1735 (set-buffer buffer))
1736 (save-restriction
1737 (widen)
1738 (goto-char (point-min))
1740 (while (< (point) (point-max))
1741 (let ((p (point))
1742 (cur (condition-case nil
1743 (read (current-buffer))
1744 (error nil))))
1745 (cond
1746 ((or (not (integerp cur))
1747 (not (eq (char-after) ?\t)))
1748 (or backed-up
1749 (setq backed-up (gnus-agent-backup-overview-buffer)))
1750 (gnus-message 1
1751 "Overview buffer contains garbage '%s'."
1752 (buffer-substring
1753 p (point-at-eol))))
1754 ((= cur prev-num)
1755 (or backed-up
1756 (setq backed-up (gnus-agent-backup-overview-buffer)))
1757 (gnus-message 1
1758 "Duplicate overview line for %d" cur)
1759 (delete-region p (progn (forward-line 1) (point))))
1760 ((< cur prev-num)
1761 (or backed-up
1762 (setq backed-up (gnus-agent-backup-overview-buffer)))
1763 (gnus-message 1 "Overview buffer not sorted!")
1764 (sort-numeric-fields 1 (point-min) (point-max))
1765 (goto-char (point-min))
1766 (setq prev-num -1))
1768 (setq prev-num cur)))
1769 (forward-line 1)))))))
1771 (defun gnus-agent-flush-server (&optional server-or-method)
1772 "Flush all agent index files for every subscribed group within
1773 the given SERVER-OR-METHOD. When called with nil, the current
1774 value of gnus-command-method identifies the server."
1775 (let* ((gnus-command-method (if server-or-method
1776 (gnus-server-to-method server-or-method)
1777 gnus-command-method))
1778 (alist gnus-newsrc-alist))
1779 (while alist
1780 (let ((entry (pop alist)))
1781 (when (gnus-methods-equal-p gnus-command-method (gnus-info-method entry))
1782 (gnus-agent-flush-group (gnus-info-group entry)))))))
1784 (defun gnus-agent-flush-group (group)
1785 "Flush the agent's index files such that the GROUP no longer
1786 appears to have any local content. The actual content, the
1787 article files, may then be deleted using gnus-agent-expire-group.
1788 If flushing was a mistake, the gnus-agent-regenerate-group method
1789 provides an undo mechanism by reconstructing the index files from
1790 the article files."
1791 (interactive (list (gnus-agent-read-group)))
1793 (let* ((gnus-command-method (or gnus-command-method
1794 (gnus-find-method-for-group group)))
1795 (overview (gnus-agent-article-name ".overview" group))
1796 (agentview (gnus-agent-article-name ".agentview" group))
1797 (file-name-coding-system nnmail-pathname-coding-system))
1799 (if (file-exists-p overview)
1800 (delete-file overview))
1801 (if (file-exists-p agentview)
1802 (delete-file agentview))
1804 (gnus-agent-update-view-total-fetched-for group nil gnus-command-method)
1805 (gnus-agent-update-view-total-fetched-for group t gnus-command-method)
1807 ;(gnus-agent-set-local group nil nil)
1808 ;(gnus-agent-save-local t)
1809 (gnus-agent-save-group-info nil group nil)))
1811 (defun gnus-agent-flush-cache ()
1812 "Flush the agent's index files such that the group no longer
1813 appears to have any local content. The actual content, the
1814 article files, is then deleted using gnus-agent-expire-group. The
1815 gnus-agent-regenerate-group method provides an undo mechanism by
1816 reconstructing the index files from the article files."
1817 (interactive)
1818 (save-excursion
1819 (let ((file-name-coding-system nnmail-pathname-coding-system))
1820 (while gnus-agent-buffer-alist
1821 (set-buffer (cdar gnus-agent-buffer-alist))
1822 (let ((coding-system-for-write gnus-agent-file-coding-system))
1823 (write-region (point-min) (point-max)
1824 (gnus-agent-article-name ".overview"
1825 (caar gnus-agent-buffer-alist))
1826 nil 'silent))
1827 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1828 (while gnus-agent-group-alist
1829 (with-temp-file (gnus-agent-article-name
1830 ".agentview" (caar gnus-agent-group-alist))
1831 (princ (cdar gnus-agent-group-alist))
1832 (insert "\n")
1833 (princ 1 (current-buffer))
1834 (insert "\n"))
1835 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist))))))
1837 ;;;###autoload
1838 (defun gnus-agent-find-parameter (group symbol)
1839 "Search for GROUPs SYMBOL in the group's parameters, the group's
1840 topic parameters, the group's category, or the customizable
1841 variables. Returns the first non-nil value found."
1842 (or (gnus-group-find-parameter group symbol t)
1843 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1844 (symbol-value
1845 (cdr
1846 (assq symbol
1847 '((agent-short-article . gnus-agent-short-article)
1848 (agent-long-article . gnus-agent-long-article)
1849 (agent-low-score . gnus-agent-low-score)
1850 (agent-high-score . gnus-agent-high-score)
1851 (agent-days-until-old . gnus-agent-expire-days)
1852 (agent-enable-expiration
1853 . gnus-agent-enable-expiration)
1854 (agent-predicate . gnus-agent-predicate)))))))
1856 (defun gnus-agent-fetch-headers (group &optional force)
1857 "Fetch interesting headers into the agent. The group's overview
1858 file will be updated to include the headers while a list of available
1859 article numbers will be returned."
1860 (let* ((fetch-all (and gnus-agent-consider-all-articles
1861 ;; Do not fetch all headers if the predicate
1862 ;; implies that we only consider unread articles.
1863 (not (gnus-predicate-implies-unread
1864 (gnus-agent-find-parameter group
1865 'agent-predicate)))))
1866 (articles (if fetch-all
1867 (if gnus-newsgroup-maximum-articles
1868 (let ((active (gnus-active group)))
1869 (gnus-uncompress-range
1870 (cons (max (car active)
1871 (- (cdr active)
1872 gnus-newsgroup-maximum-articles
1873 -1))
1874 (cdr active))))
1875 (gnus-uncompress-range (gnus-active group)))
1876 (gnus-list-of-unread-articles group)))
1877 (gnus-decode-encoded-word-function 'identity)
1878 (gnus-decode-encoded-address-function 'identity)
1879 (file (gnus-agent-article-name ".overview" group))
1880 (file-name-coding-system nnmail-pathname-coding-system))
1882 (unless fetch-all
1883 ;; Add articles with marks to the list of article headers we want to
1884 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1885 ;; mark, but do fetch recent or seen articles if they have other, more
1886 ;; interesting marks. (We have to fetch articles with boring marks
1887 ;; because otherwise the agent will remove their marks.)
1888 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1889 (unless (memq (car arts) '(seen recent killed cache))
1890 (setq articles (gnus-range-add articles (cdr arts)))))
1891 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1893 ;; At this point, I have the list of articles to consider for
1894 ;; fetching. This is the list that I'll return to my caller. Some
1895 ;; of these articles may have already been fetched. That's OK as
1896 ;; the fetch article code will filter those out. Internally, I'll
1897 ;; filter this list to just those articles whose headers need to
1898 ;; be fetched.
1899 (let ((articles articles))
1900 ;; Remove known articles.
1901 (when (and (or gnus-agent-cache
1902 (not gnus-plugged))
1903 (gnus-agent-load-alist group))
1904 ;; Remove articles marked as downloaded.
1905 (if fetch-all
1906 ;; I want to fetch all headers in the active range.
1907 ;; Therefore, exclude only those headers that are in the
1908 ;; article alist.
1909 ;; NOTE: This is probably NOT what I want to do after
1910 ;; agent expiration in this group.
1911 (setq articles (gnus-agent-uncached-articles articles group))
1913 ;; I want to only fetch those headers that have never been
1914 ;; fetched. Therefore, exclude all headers that are, or
1915 ;; WERE, in the article alist.
1916 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1917 (high (cdr (gnus-active group))))
1918 ;; Low can be greater than High when the same group is
1919 ;; fetched twice in the same session {The first fetch will
1920 ;; fill the article alist such that (last
1921 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1922 ;; group))}. The addition of one(the 1+ above) then
1923 ;; forces Low to be greater than High. When this happens,
1924 ;; gnus-list-range-intersection returns nil which
1925 ;; indicates that no headers need to be fetched. -- Kevin
1926 (setq articles (gnus-list-range-intersection
1927 articles (list (cons low high)))))))
1929 (gnus-message
1930 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1931 (gnus-compress-sequence articles t))
1933 (with-current-buffer nntp-server-buffer
1934 (if articles
1935 (progn
1936 (gnus-message 7 "Fetching headers for %s..."
1937 (gnus-agent-decoded-group-name group))
1939 ;; Fetch them.
1940 (gnus-make-directory (nnheader-translate-file-chars
1941 (file-name-directory file) t))
1943 (unless (eq 'nov (gnus-retrieve-headers articles group))
1944 (nnvirtual-convert-headers))
1945 (gnus-agent-check-overview-buffer)
1946 ;; Move these headers to the overview buffer so that
1947 ;; gnus-agent-braid-nov can merge them with the contents
1948 ;; of FILE.
1949 (copy-to-buffer
1950 gnus-agent-overview-buffer (point-min) (point-max))
1951 ;; NOTE: Call g-a-brand-nov even when the file does not
1952 ;; exist. As a minimum, it will validate the article
1953 ;; numbers already in the buffer.
1954 (gnus-agent-braid-nov group articles file)
1955 (let ((coding-system-for-write
1956 gnus-agent-file-coding-system))
1957 (gnus-agent-check-overview-buffer)
1958 (write-region (point-min) (point-max) file nil 'silent))
1959 (gnus-agent-update-view-total-fetched-for group t)
1960 (gnus-agent-save-alist group articles nil)
1961 articles)
1962 (ignore-errors
1963 (erase-buffer)
1964 (nnheader-insert-file-contents file)))))
1965 articles))
1967 (defsubst gnus-agent-read-article-number ()
1968 "Reads the article number at point. Returns nil when a valid article number can not be read."
1970 ;; It is unfortunate but the read function quietly overflows
1971 ;; integer. As a result, I have to use string operations to test
1972 ;; for overflow BEFORE calling read.
1973 (when (looking-at "[0-9]+\t")
1974 (let ((len (- (match-end 0) (match-beginning 0))))
1975 (cond ((< len 9)
1976 (read (current-buffer)))
1977 ((= len 9)
1978 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1979 ;; Back convert from int to string to ensure that this is one of them.
1980 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1981 (num (read (current-buffer)))
1982 (str2 (int-to-string num)))
1983 (when (equal str1 str2)
1984 num)))))))
1986 (defsubst gnus-agent-copy-nov-line (article)
1987 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1988 (let (art b e)
1989 (set-buffer gnus-agent-overview-buffer)
1990 (while (and (not (eobp))
1991 (or (not (setq art (gnus-agent-read-article-number)))
1992 (< art article)))
1993 (forward-line 1))
1994 (beginning-of-line)
1995 (if (or (eobp)
1996 (not (eq article art)))
1997 (set-buffer nntp-server-buffer)
1998 (setq b (point))
1999 (setq e (progn (forward-line 1) (point)))
2000 (set-buffer nntp-server-buffer)
2001 (insert-buffer-substring gnus-agent-overview-buffer b e))))
2003 (defun gnus-agent-braid-nov (group articles file)
2004 "Merge agent overview data with given file.
2005 Takes unvalidated headers for ARTICLES from
2006 `gnus-agent-overview-buffer' and validated headers from the given
2007 FILE and places the combined valid headers into
2008 `nntp-server-buffer'. This function can be used, when file
2009 doesn't exist, to valid the overview buffer."
2010 (let (start last)
2011 (set-buffer gnus-agent-overview-buffer)
2012 (goto-char (point-min))
2013 (set-buffer nntp-server-buffer)
2014 (erase-buffer)
2015 (when (file-exists-p file)
2016 (nnheader-insert-file-contents file))
2017 (goto-char (point-max))
2018 (forward-line -1)
2020 (unless (or (= (point-min) (point-max))
2021 (< (setq last (read (current-buffer))) (car articles)))
2022 ;; Old and new overlap -- We do it the hard way.
2023 (when (nnheader-find-nov-line (car articles))
2024 ;; Replacing existing NOV entry
2025 (delete-region (point) (progn (forward-line 1) (point))))
2026 (gnus-agent-copy-nov-line (pop articles))
2028 (ignore-errors
2029 (while articles
2030 (while (let ((art (read (current-buffer))))
2031 (cond ((< art (car articles))
2032 (forward-line 1)
2034 ((= art (car articles))
2035 (beginning-of-line)
2036 (delete-region
2037 (point) (progn (forward-line 1) (point)))
2038 nil)
2040 (beginning-of-line)
2041 nil))))
2043 (gnus-agent-copy-nov-line (pop articles)))))
2045 (goto-char (point-max))
2047 ;; Append the remaining lines
2048 (when articles
2049 (when last
2050 (set-buffer gnus-agent-overview-buffer)
2051 (setq start (point))
2052 (set-buffer nntp-server-buffer))
2054 (let ((p (point)))
2055 (insert-buffer-substring gnus-agent-overview-buffer start)
2056 (goto-char p))
2058 (setq last (or last -134217728))
2059 (while (catch 'problems
2060 (let (sort art)
2061 (while (not (eobp))
2062 (setq art (gnus-agent-read-article-number))
2063 (cond ((not art)
2064 ;; Bad art num - delete this line
2065 (beginning-of-line)
2066 (delete-region (point) (progn (forward-line 1) (point))))
2067 ((< art last)
2068 ;; Art num out of order - enable sort
2069 (setq sort t)
2070 (forward-line 1))
2071 ((= art last)
2072 ;; Bad repeat of art number - delete this line
2073 (beginning-of-line)
2074 (delete-region (point) (progn (forward-line 1) (point))))
2076 ;; Good art num
2077 (setq last art)
2078 (forward-line 1))))
2079 (when sort
2080 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
2081 ;; First, we'll fix the sort.
2082 (sort-numeric-fields 1 (point-min) (point-max))
2084 ;; but now we have to consider that we may have duplicate rows...
2085 ;; so reset to beginning of file
2086 (goto-char (point-min))
2087 (setq last -134217728)
2089 ;; and throw a code that restarts this scan
2090 (throw 'problems t))
2091 nil))))))
2093 ;; Keeps the compiler from warning about the free variable in
2094 ;; gnus-agent-read-agentview.
2095 (defvar gnus-agent-read-agentview)
2097 (defun gnus-agent-load-alist (group)
2098 "Load the article-state alist for GROUP."
2099 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
2100 (let* ((gnus-agent-read-agentview group)
2101 (file-name-coding-system nnmail-pathname-coding-system)
2102 (agentview (gnus-agent-article-name ".agentview" group)))
2103 (setq gnus-agent-article-alist
2104 (and (file-exists-p agentview)
2105 (gnus-cache-file-contents
2106 agentview
2107 'gnus-agent-file-loading-cache
2108 'gnus-agent-read-agentview)))))
2110 (defun gnus-agent-read-agentview (file)
2111 "Load FILE and do a `read' there."
2112 (with-temp-buffer
2113 (condition-case nil
2114 (progn
2115 (nnheader-insert-file-contents file)
2116 (goto-char (point-min))
2117 (let ((alist (read (current-buffer)))
2118 (version (condition-case nil (read (current-buffer))
2119 (end-of-file 0)))
2120 changed-version)
2122 (cond
2123 ((= version 0)
2124 (let ((inhibit-quit t)
2125 entry)
2126 (gnus-agent-open-history)
2127 (set-buffer (gnus-agent-history-buffer))
2128 (goto-char (point-min))
2129 (while (not (eobp))
2130 (if (and (looking-at
2131 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2132 (string= (match-string 2)
2133 gnus-agent-read-agentview)
2134 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2135 (setcdr entry (string-to-number (match-string 1))))
2136 (forward-line 1))
2137 (gnus-agent-close-history)
2138 (setq changed-version t)))
2139 ((= version 1)
2140 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2141 ((= version 2)
2142 (let (state sequence uncomp)
2143 (while alist
2144 (setq state (caar alist)
2145 sequence (inline (gnus-uncompress-range (cdar alist)))
2146 alist (cdr alist))
2147 (while sequence
2148 (push (cons (pop sequence) state) uncomp)))
2149 (setq alist (sort uncomp 'car-less-than-car)))
2150 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2151 (when changed-version
2152 (let ((gnus-agent-article-alist alist))
2153 (gnus-agent-save-alist gnus-agent-read-agentview)))
2154 alist))
2155 ((end-of-file file-error)
2156 ;; The agentview file is missing.
2157 (condition-case nil
2158 ;; If the agent directory exists, attempt to perform a brute-force
2159 ;; reconstruction of its contents.
2160 (let* (alist
2161 (file-name-coding-system nnmail-pathname-coding-system)
2162 (file-attributes (directory-files-and-attributes
2163 (gnus-agent-article-name ""
2164 gnus-agent-read-agentview) nil "^[0-9]+$" t)))
2165 (while file-attributes
2166 (let ((fa (pop file-attributes)))
2167 (unless (nth 1 fa)
2168 (push (cons (string-to-number (nth 0 fa)) (time-to-days (nth 5 fa))) alist))))
2169 alist)
2170 (file-error nil))))))
2172 (defun gnus-agent-save-alist (group &optional articles state)
2173 "Save the article-state alist for GROUP."
2174 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2175 (prev (cons nil gnus-agent-article-alist))
2176 (all prev)
2177 print-level print-length item article)
2178 (while (setq article (pop articles))
2179 (while (and (cdr prev)
2180 (< (caadr prev) article))
2181 (setq prev (cdr prev)))
2182 (cond
2183 ((not (cdr prev))
2184 (setcdr prev (list (cons article state))))
2185 ((> (caadr prev) article)
2186 (setcdr prev (cons (cons article state) (cdr prev))))
2187 ((= (caadr prev) article)
2188 (setcdr (cadr prev) state)))
2189 (setq prev (cdr prev)))
2190 (setq gnus-agent-article-alist (cdr all))
2192 (gnus-agent-set-local group
2193 (caar gnus-agent-article-alist)
2194 (caar (last gnus-agent-article-alist)))
2196 (gnus-make-directory (gnus-agent-article-name "" group))
2197 (with-temp-file (gnus-agent-article-name ".agentview" group)
2198 (cond ((eq gnus-agent-article-alist-save-format 1)
2199 (princ gnus-agent-article-alist (current-buffer)))
2200 ((eq gnus-agent-article-alist-save-format 2)
2201 (let ((alist gnus-agent-article-alist)
2202 article-id day-of-download comp-list compressed)
2203 (while alist
2204 (setq article-id (caar alist)
2205 day-of-download (cdar alist)
2206 comp-list (assq day-of-download compressed)
2207 alist (cdr alist))
2208 (if comp-list
2209 (setcdr comp-list (cons article-id (cdr comp-list)))
2210 (push (list day-of-download article-id) compressed)))
2211 (setq alist compressed)
2212 (while alist
2213 (setq comp-list (pop alist))
2214 (setcdr comp-list
2215 (gnus-compress-sequence (nreverse (cdr comp-list)))))
2216 (princ compressed (current-buffer)))))
2217 (insert "\n")
2218 (princ gnus-agent-article-alist-save-format (current-buffer))
2219 (insert "\n"))
2221 (gnus-agent-update-view-total-fetched-for group nil)))
2223 (defvar gnus-agent-article-local nil)
2224 (defvar gnus-agent-article-local-times nil)
2225 (defvar gnus-agent-file-loading-local nil)
2227 (defun gnus-agent-load-local (&optional method)
2228 "Load the METHOD'S local file. The local file contains min/max
2229 article counts for each of the method's subscribed groups."
2230 (let ((gnus-command-method (or method gnus-command-method)))
2231 (when (or (null gnus-agent-article-local-times)
2232 (zerop gnus-agent-article-local-times))
2233 (setq gnus-agent-article-local
2234 (gnus-cache-file-contents
2235 (gnus-agent-lib-file "local")
2236 'gnus-agent-file-loading-local
2237 'gnus-agent-read-and-cache-local))
2238 (when gnus-agent-article-local-times
2239 (incf gnus-agent-article-local-times)))
2240 gnus-agent-article-local))
2242 (defun gnus-agent-read-and-cache-local (file)
2243 "Load and read FILE then bind its contents to
2244 gnus-agent-article-local. If that variable had `dirty' (also known as
2245 modified) original contents, they are first saved to their own file."
2246 (if (and gnus-agent-article-local
2247 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2248 (gnus-agent-save-local))
2249 (gnus-agent-read-local file))
2251 (defun gnus-agent-read-local (file)
2252 "Load FILE and do a `read' there."
2253 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2254 (point-max))))
2255 (line 1))
2256 (with-temp-buffer
2257 (condition-case nil
2258 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2259 (nnheader-insert-file-contents file))
2260 (file-error))
2262 (goto-char (point-min))
2263 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2264 (while (= (following-char) ?\;)
2265 (forward-line 1)
2266 (setq line (1+ line)))
2268 (while (not (eobp))
2269 (condition-case err
2270 (let (group
2273 (cur (current-buffer))
2274 (obarray my-obarray))
2275 (setq group (read cur)
2276 min (read cur)
2277 max (read cur))
2279 (when (stringp group)
2280 (setq group (intern group my-obarray)))
2282 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2283 (set group (cons (+ 0 min) (+ 0 max))))
2284 (error
2285 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2286 file line (error-message-string err))))
2287 (forward-line 1)
2288 (setq line (1+ line))))
2290 (set (intern "+dirty" my-obarray) nil)
2291 (set (intern "+method" my-obarray) gnus-command-method)
2292 my-obarray))
2294 (defun gnus-agent-save-local (&optional force)
2295 "Save gnus-agent-article-local under it method's agent.lib directory."
2296 (let ((my-obarray gnus-agent-article-local))
2297 (when (and my-obarray
2298 (or force (symbol-value (intern "+dirty" my-obarray))))
2299 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2300 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2301 (dest (gnus-agent-lib-file "local")))
2302 (gnus-make-directory (gnus-agent-lib-file ""))
2304 (let ((coding-system-for-write gnus-agent-file-coding-system)
2305 (file-name-coding-system nnmail-pathname-coding-system))
2306 (with-temp-file dest
2307 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2308 print-level print-length item article
2309 (standard-output (current-buffer)))
2310 (mapatoms (lambda (symbol)
2311 (cond ((not (boundp symbol))
2312 nil)
2313 ((member (symbol-name symbol) '("+dirty" "+method"))
2314 nil)
2316 (let ((range (symbol-value symbol)))
2317 (when range
2318 (prin1 symbol)
2319 (princ " ")
2320 (princ (car range))
2321 (princ " ")
2322 (princ (cdr range))
2323 (princ "\n"))))))
2324 my-obarray))))))))
2326 (defun gnus-agent-get-local (group &optional gmane method)
2327 (let* ((gmane (or gmane (gnus-group-real-name group)))
2328 (gnus-command-method (or method (gnus-find-method-for-group group)))
2329 (local (gnus-agent-load-local))
2330 (symb (intern gmane local))
2331 (minmax (and (boundp symb) (symbol-value symb))))
2332 (unless minmax
2333 ;; Bind these so that gnus-agent-load-alist doesn't change the
2334 ;; current alist (i.e. gnus-agent-article-alist)
2335 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2336 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2337 (alist (gnus-agent-load-alist group)))
2338 (when alist
2339 (setq minmax
2340 (cons (caar alist)
2341 (caar (last alist))))
2342 (gnus-agent-set-local group (car minmax) (cdr minmax)
2343 gmane gnus-command-method local))))
2344 minmax))
2346 (defun gnus-agent-set-local (group min max &optional gmane method local)
2347 (let* ((gmane (or gmane (gnus-group-real-name group)))
2348 (gnus-command-method (or method (gnus-find-method-for-group group)))
2349 (local (or local (gnus-agent-load-local)))
2350 (symb (intern gmane local))
2351 (minmax (and (boundp symb) (symbol-value symb))))
2352 (if (cond ((and minmax
2353 (or (not (eq min (car minmax)))
2354 (not (eq max (cdr minmax))))
2356 max)
2357 (setcar minmax min)
2358 (setcdr minmax max)
2360 (minmax
2361 nil)
2362 ((and min max)
2363 (set symb (cons min max))
2366 (unintern symb local)))
2367 (set (intern "+dirty" local) t))))
2369 (defun gnus-agent-article-name (article group)
2370 (expand-file-name article
2371 (file-name-as-directory
2372 (gnus-agent-group-pathname group))))
2374 (defun gnus-agent-batch-confirmation (msg)
2375 "Show error message and return t."
2376 (gnus-message 1 "%s" msg)
2379 ;;;###autoload
2380 (defun gnus-agent-batch-fetch ()
2381 "Start Gnus and fetch session."
2382 (interactive)
2383 (gnus)
2384 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2385 (gnus-agent-fetch-session))
2386 (gnus-group-exit))
2388 (defun gnus-agent-fetch-session ()
2389 "Fetch all articles and headers that are eligible for fetching."
2390 (interactive)
2391 (unless gnus-agent-covered-methods
2392 (error "No servers are covered by the Gnus agent"))
2393 (unless gnus-plugged
2394 (error "Can't fetch articles while Gnus is unplugged"))
2395 (let ((methods (gnus-agent-covered-methods))
2396 groups group gnus-command-method)
2397 (save-excursion
2398 (while methods
2399 (setq gnus-command-method (car methods))
2400 (when (and (or (gnus-server-opened gnus-command-method)
2401 (gnus-open-server gnus-command-method))
2402 (gnus-online gnus-command-method))
2403 (setq groups (gnus-groups-from-server (car methods)))
2404 (gnus-agent-with-fetch
2405 (while (setq group (pop groups))
2406 (when (<= (gnus-group-level group)
2407 gnus-agent-handle-level)
2408 (if (or debug-on-error debug-on-quit)
2409 (gnus-agent-fetch-group-1
2410 group gnus-command-method)
2411 (condition-case err
2412 (gnus-agent-fetch-group-1
2413 group gnus-command-method)
2414 (error
2415 (unless (funcall gnus-agent-confirmation-function
2416 (format "Error %s while fetching session. Should gnus continue? "
2417 (error-message-string err)))
2418 (error "Cannot fetch articles into the Gnus agent")))
2419 (quit
2420 (gnus-agent-regenerate-group group)
2421 (unless (funcall gnus-agent-confirmation-function
2422 (format
2423 "%s while fetching session. Should gnus continue? "
2424 (error-message-string err)))
2425 (signal 'quit
2426 "Cannot fetch articles into the Gnus agent")))))))))
2427 (setq methods (cdr methods)))
2428 (gnus-run-hooks 'gnus-agent-fetched-hook)
2429 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2431 (defun gnus-agent-fetch-group-1 (group method)
2432 "Fetch GROUP."
2433 (let ((gnus-command-method method)
2434 (gnus-newsgroup-name group)
2435 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2436 (gnus-newsgroup-headers gnus-newsgroup-headers)
2437 (gnus-newsgroup-scored gnus-newsgroup-scored)
2438 (gnus-use-cache gnus-use-cache)
2439 (gnus-summary-expunge-below gnus-summary-expunge-below)
2440 (gnus-summary-mark-below gnus-summary-mark-below)
2441 (gnus-orphan-score gnus-orphan-score)
2442 ;; Maybe some other gnus-summary local variables should also
2443 ;; be put here.
2445 gnus-headers
2446 gnus-score
2447 articles arts
2448 category predicate info marks score-param
2450 (unless (gnus-check-group group)
2451 (error "Can't open server for %s" group))
2453 ;; Fetch headers.
2454 (when (or gnus-newsgroup-active
2455 (gnus-active group)
2456 (gnus-activate-group group))
2457 (let ((marked-articles gnus-newsgroup-downloadable))
2458 ;; Identify the articles marked for download
2459 (unless gnus-newsgroup-active
2460 ;; The variable gnus-newsgroup-active was selected as I need
2461 ;; a gnus-summary local variable that is NOT bound to any
2462 ;; value (its global value should default to nil).
2463 (dolist (mark gnus-agent-download-marks)
2464 (let ((arts (cdr (assq mark (gnus-info-marks
2465 (setq info (gnus-get-info group)))))))
2466 (when arts
2467 (setq marked-articles (nconc (gnus-uncompress-range arts)
2468 marked-articles))
2469 ))))
2470 (setq marked-articles (sort marked-articles '<))
2472 ;; Fetch any new articles from the server
2473 (setq articles (gnus-agent-fetch-headers group))
2475 ;; Merge new articles with marked
2476 (setq articles (sort (append marked-articles articles) '<))
2478 (when articles
2479 ;; Parse them and see which articles we want to fetch.
2480 (setq gnus-newsgroup-dependencies
2481 (or gnus-newsgroup-dependencies
2482 (make-vector (length articles) 0)))
2483 (setq gnus-newsgroup-headers
2484 (or gnus-newsgroup-headers
2485 (gnus-get-newsgroup-headers-xover articles nil nil
2486 group)))
2487 ;; `gnus-agent-overview-buffer' may be killed for
2488 ;; timeout reason. If so, recreate it.
2489 (gnus-agent-create-buffer)
2491 ;; Figure out how to select articles in this group
2492 (setq category (gnus-group-category group))
2494 (setq predicate
2495 (gnus-get-predicate
2496 (gnus-agent-find-parameter group 'agent-predicate)))
2498 ;; If the selection predicate requires scoring, score each header
2499 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2500 (let ((score-param
2501 (gnus-agent-find-parameter group 'agent-score-file)))
2502 ;; Translate score-param into real one
2503 (cond
2504 ((not score-param))
2505 ((eq score-param 'file)
2506 (setq score-param (gnus-all-score-files group)))
2507 ((stringp (car score-param)))
2509 (setq score-param (list (list score-param)))))
2510 (when score-param
2511 (gnus-score-headers score-param))))
2513 (unless (and (eq predicate 'gnus-agent-false)
2514 (not marked-articles))
2515 (let ((arts (list nil)))
2516 (let ((arts-tail arts)
2517 (alist (gnus-agent-load-alist group))
2518 (marked-articles marked-articles)
2519 (gnus-newsgroup-headers gnus-newsgroup-headers))
2520 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2521 (let ((num (mail-header-number gnus-headers)))
2522 ;; Determine if this article is already in the cache
2523 (while (and alist
2524 (> num (caar alist)))
2525 (setq alist (cdr alist)))
2527 (unless (and (eq num (caar alist))
2528 (cdar alist))
2530 ;; Determine if this article was marked for download.
2531 (while (and marked-articles
2532 (> num (car marked-articles)))
2533 (setq marked-articles
2534 (cdr marked-articles)))
2536 ;; When this article is marked, or selected by the
2537 ;; predicate, add it to the download list
2538 (when (or (eq num (car marked-articles))
2539 (let ((gnus-score
2540 (or (cdr
2541 (assq num gnus-newsgroup-scored))
2542 gnus-summary-default-score))
2543 (gnus-agent-long-article
2544 (gnus-agent-find-parameter
2545 group 'agent-long-article))
2546 (gnus-agent-short-article
2547 (gnus-agent-find-parameter
2548 group 'agent-short-article))
2549 (gnus-agent-low-score
2550 (gnus-agent-find-parameter
2551 group 'agent-low-score))
2552 (gnus-agent-high-score
2553 (gnus-agent-find-parameter
2554 group 'agent-high-score))
2555 (gnus-agent-expire-days
2556 (gnus-agent-find-parameter
2557 group 'agent-days-until-old)))
2558 (funcall predicate)))
2559 (gnus-agent-append-to-list arts-tail num))))))
2561 (let (fetched-articles)
2562 ;; Fetch all selected articles
2563 (setq gnus-newsgroup-undownloaded
2564 (gnus-sorted-ndifference
2565 gnus-newsgroup-undownloaded
2566 (setq fetched-articles
2567 (if (cdr arts)
2568 (gnus-agent-fetch-articles group (cdr arts))
2569 nil))))
2571 (let ((unfetched-articles
2572 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2573 (if gnus-newsgroup-active
2574 ;; Update the summary buffer
2575 (progn
2576 (dolist (article marked-articles)
2577 (gnus-summary-set-agent-mark article t))
2578 (dolist (article fetched-articles)
2579 (when gnus-agent-mark-unread-after-downloaded
2580 (setq gnus-newsgroup-downloadable
2581 (delq article gnus-newsgroup-downloadable))
2582 (gnus-summary-mark-article
2583 article gnus-unread-mark))
2584 (when (gnus-summary-goto-subject article nil t)
2585 (gnus-summary-update-download-mark article)))
2586 (dolist (article unfetched-articles)
2587 (gnus-summary-mark-article
2588 article gnus-canceled-mark)))
2590 ;; Update the group buffer.
2592 ;; When some, or all, of the marked articles came
2593 ;; from the download mark. Remove that mark. I
2594 ;; didn't do this earlier as I only want to remove
2595 ;; the marks after the fetch is completed.
2597 (dolist (mark gnus-agent-download-marks)
2598 (when (eq mark 'download)
2599 (let ((marked-arts
2600 (assq mark (gnus-info-marks
2601 (setq info (gnus-get-info group))))))
2602 (when (cdr marked-arts)
2603 (setq marks
2604 (delq marked-arts (gnus-info-marks info)))
2605 (gnus-info-set-marks info marks)))))
2606 (let ((read (gnus-info-read
2607 (or info (setq info (gnus-get-info group))))))
2608 (gnus-info-set-read
2609 info (gnus-add-to-range read unfetched-articles)))
2611 (gnus-group-update-group group t)
2612 (sit-for 0)
2614 (gnus-dribble-enter
2615 (concat "(gnus-group-set-info '"
2616 (gnus-prin1-to-string info)
2617 ")"))))))))))))
2620 ;;; Agent Category Mode
2623 (defvar gnus-category-mode-hook nil
2624 "Hook run in `gnus-category-mode' buffers.")
2626 (defvar gnus-category-line-format " %(%20c%): %g\n"
2627 "Format of category lines.
2629 Valid specifiers include:
2630 %c Topic name (string)
2631 %g The number of groups in the topic (integer)
2633 General format specifiers can also be used. See Info node
2634 `(gnus)Formatting Variables'.")
2636 (defvar gnus-category-mode-line-format "Gnus: %%b"
2637 "The format specification for the category mode line.")
2639 (defvar gnus-agent-predicate 'false
2640 "The selection predicate used when no other source is available.")
2642 (defvar gnus-agent-short-article 500
2643 "Articles that have fewer lines than this are short.")
2645 (defvar gnus-agent-long-article 1000
2646 "Articles that have more lines than this are long.")
2648 (defvar gnus-agent-low-score 0
2649 "Articles that have a score lower than this have a low score.")
2651 (defvar gnus-agent-high-score 0
2652 "Articles that have a score higher than this have a high score.")
2655 ;;; Internal variables.
2657 (defvar gnus-category-buffer "*Agent Category*")
2659 (defvar gnus-category-line-format-alist
2660 `((?c gnus-tmp-name ?s)
2661 (?g gnus-tmp-groups ?d)))
2663 (defvar gnus-category-mode-line-format-alist
2664 `((?u user-defined ?s)))
2666 (defvar gnus-category-line-format-spec nil)
2667 (defvar gnus-category-mode-line-format-spec nil)
2669 (defvar gnus-category-mode-map nil)
2670 (put 'gnus-category-mode 'mode-class 'special)
2672 (unless gnus-category-mode-map
2673 (setq gnus-category-mode-map (make-sparse-keymap))
2674 (suppress-keymap gnus-category-mode-map)
2676 (gnus-define-keys gnus-category-mode-map
2677 "q" gnus-category-exit
2678 "k" gnus-category-kill
2679 "c" gnus-category-copy
2680 "a" gnus-category-add
2681 "e" gnus-agent-customize-category
2682 "p" gnus-category-edit-predicate
2683 "g" gnus-category-edit-groups
2684 "s" gnus-category-edit-score
2685 "l" gnus-category-list
2687 "\C-c\C-i" gnus-info-find-node
2688 "\C-c\C-b" gnus-bug))
2690 (defvar gnus-category-menu-hook nil
2691 "*Hook run after the creation of the menu.")
2693 (defun gnus-category-make-menu-bar ()
2694 (gnus-turn-off-edit-menu 'category)
2695 (unless (boundp 'gnus-category-menu)
2696 (easy-menu-define
2697 gnus-category-menu gnus-category-mode-map ""
2698 '("Categories"
2699 ["Add" gnus-category-add t]
2700 ["Kill" gnus-category-kill t]
2701 ["Copy" gnus-category-copy t]
2702 ["Edit category" gnus-agent-customize-category t]
2703 ["Edit predicate" gnus-category-edit-predicate t]
2704 ["Edit score" gnus-category-edit-score t]
2705 ["Edit groups" gnus-category-edit-groups t]
2706 ["Exit" gnus-category-exit t]))
2708 (gnus-run-hooks 'gnus-category-menu-hook)))
2710 (defun gnus-category-mode ()
2711 "Major mode for listing and editing agent categories.
2713 All normal editing commands are switched off.
2714 \\<gnus-category-mode-map>
2715 For more in-depth information on this mode, read the manual
2716 \(`\\[gnus-info-find-node]').
2718 The following commands are available:
2720 \\{gnus-category-mode-map}"
2721 (interactive)
2722 (when (gnus-visual-p 'category-menu 'menu)
2723 (gnus-category-make-menu-bar))
2724 (kill-all-local-variables)
2725 (gnus-simplify-mode-line)
2726 (setq major-mode 'gnus-category-mode)
2727 (setq mode-name "Category")
2728 (gnus-set-default-directory)
2729 (setq mode-line-process nil)
2730 (use-local-map gnus-category-mode-map)
2731 (buffer-disable-undo)
2732 (setq truncate-lines t)
2733 (setq buffer-read-only t)
2734 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2736 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2738 (defun gnus-category-insert-line (category)
2739 (let* ((gnus-tmp-name (format "%s" (car category)))
2740 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2741 (beginning-of-line)
2742 (gnus-add-text-properties
2743 (point)
2744 (prog1 (1+ (point))
2745 ;; Insert the text.
2746 (eval gnus-category-line-format-spec))
2747 (list 'gnus-category gnus-tmp-name))))
2749 (defun gnus-enter-category-buffer ()
2750 "Go to the Category buffer."
2751 (interactive)
2752 (gnus-category-setup-buffer)
2753 (gnus-configure-windows 'category)
2754 (gnus-category-prepare))
2756 (defun gnus-category-setup-buffer ()
2757 (unless (get-buffer gnus-category-buffer)
2758 (with-current-buffer (gnus-get-buffer-create gnus-category-buffer)
2759 (gnus-category-mode))))
2761 (defun gnus-category-prepare ()
2762 (gnus-set-format 'category-mode)
2763 (gnus-set-format 'category t)
2764 (let ((alist gnus-category-alist)
2765 (buffer-read-only nil))
2766 (erase-buffer)
2767 (while alist
2768 (gnus-category-insert-line (pop alist)))
2769 (goto-char (point-min))
2770 (gnus-category-position-point)))
2772 (defun gnus-category-name ()
2773 (or (intern (get-text-property (point-at-bol) 'gnus-category))
2774 (error "No category on the current line")))
2776 (defun gnus-category-read ()
2777 "Read the category alist."
2778 (setq gnus-category-alist
2780 (with-temp-buffer
2781 (ignore-errors
2782 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2783 (goto-char (point-min))
2784 ;; This code isn't temp, it will be needed so long as
2785 ;; anyone may be migrating from an older version.
2787 ;; Once we're certain that people will not revert to an
2788 ;; earlier version, we can take out the old-list code in
2789 ;; gnus-category-write.
2790 (let* ((old-list (read (current-buffer)))
2791 (new-list (ignore-errors (read (current-buffer)))))
2792 (if new-list
2793 new-list
2794 ;; Convert from a positional list to an alist.
2795 (mapcar
2796 (lambda (c)
2797 (setcdr c
2798 (delq nil
2799 (gnus-mapcar
2800 (lambda (valu symb)
2801 (if valu
2802 (cons symb valu)))
2803 (cdr c)
2804 '(agent-predicate agent-score-file agent-groups))))
2806 old-list)))))
2807 (list (gnus-agent-cat-make 'default 'short)))))
2809 (defun gnus-category-write ()
2810 "Write the category alist."
2811 (setq gnus-category-predicate-cache nil
2812 gnus-category-group-cache nil)
2813 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2814 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2815 ;; This prin1 is temporary. It exists so that people can revert
2816 ;; to an earlier version of gnus-agent.
2817 (prin1 (mapcar (lambda (c)
2818 (list (car c)
2819 (cdr (assoc 'agent-predicate c))
2820 (cdr (assoc 'agent-score-file c))
2821 (cdr (assoc 'agent-groups c))))
2822 gnus-category-alist)
2823 (current-buffer))
2824 (newline)
2825 (prin1 gnus-category-alist (current-buffer))))
2827 (defun gnus-category-edit-predicate (category)
2828 "Edit the predicate for CATEGORY."
2829 (interactive (list (gnus-category-name)))
2830 (let ((info (assq category gnus-category-alist)))
2831 (gnus-edit-form
2832 (gnus-agent-cat-predicate info)
2833 (format "Editing the select predicate for category %s" category)
2834 `(lambda (predicate)
2835 ;; Avoid run-time execution of setf form
2836 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2837 ;; predicate)
2838 ;; use its expansion instead:
2839 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2840 'agent-predicate predicate)
2842 (gnus-category-write)
2843 (gnus-category-list)))))
2845 (defun gnus-category-edit-score (category)
2846 "Edit the score expression for CATEGORY."
2847 (interactive (list (gnus-category-name)))
2848 (let ((info (assq category gnus-category-alist)))
2849 (gnus-edit-form
2850 (gnus-agent-cat-score-file info)
2851 (format "Editing the score expression for category %s" category)
2852 `(lambda (score-file)
2853 ;; Avoid run-time execution of setf form
2854 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2855 ;; score-file)
2856 ;; use its expansion instead:
2857 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2858 'agent-score-file score-file)
2860 (gnus-category-write)
2861 (gnus-category-list)))))
2863 (defun gnus-category-edit-groups (category)
2864 "Edit the group list for CATEGORY."
2865 (interactive (list (gnus-category-name)))
2866 (let ((info (assq category gnus-category-alist)))
2867 (gnus-edit-form
2868 (gnus-agent-cat-groups info)
2869 (format "Editing the group list for category %s" category)
2870 `(lambda (groups)
2871 ;; Avoid run-time execution of setf form
2872 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2873 ;; groups)
2874 ;; use its expansion instead:
2875 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2876 groups)
2878 (gnus-category-write)
2879 (gnus-category-list)))))
2881 (defun gnus-category-kill (category)
2882 "Kill the current category."
2883 (interactive (list (gnus-category-name)))
2884 (let ((info (assq category gnus-category-alist))
2885 (buffer-read-only nil))
2886 (gnus-delete-line)
2887 (setq gnus-category-alist (delq info gnus-category-alist))
2888 (gnus-category-write)))
2890 (defun gnus-category-copy (category to)
2891 "Copy the current category."
2892 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2893 (let ((info (assq category gnus-category-alist)))
2894 (push (let ((newcat (gnus-copy-sequence info)))
2895 (setf (gnus-agent-cat-name newcat) to)
2896 (setf (gnus-agent-cat-groups newcat) nil)
2897 newcat)
2898 gnus-category-alist)
2899 (gnus-category-write)
2900 (gnus-category-list)))
2902 (defun gnus-category-add (category)
2903 "Create a new category."
2904 (interactive "SCategory name: ")
2905 (when (assq category gnus-category-alist)
2906 (error "Category %s already exists" category))
2907 (push (gnus-agent-cat-make category)
2908 gnus-category-alist)
2909 (gnus-category-write)
2910 (gnus-category-list))
2912 (defun gnus-category-list ()
2913 "List all categories."
2914 (interactive)
2915 (gnus-category-prepare))
2917 (defun gnus-category-exit ()
2918 "Return to the group buffer."
2919 (interactive)
2920 (kill-buffer (current-buffer))
2921 (gnus-configure-windows 'group t))
2923 ;; To avoid having 8-bit characters in the source file.
2924 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2926 (defvar gnus-category-predicate-alist
2927 '((spam . gnus-agent-spam-p)
2928 (short . gnus-agent-short-p)
2929 (long . gnus-agent-long-p)
2930 (low . gnus-agent-low-scored-p)
2931 (high . gnus-agent-high-scored-p)
2932 (read . gnus-agent-read-p)
2933 (true . gnus-agent-true)
2934 (false . gnus-agent-false))
2935 "Mapping from short score predicate symbols to predicate functions.")
2937 (defun gnus-agent-spam-p ()
2938 "Say whether an article is spam or not."
2939 (unless gnus-agent-spam-hashtb
2940 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2941 (if (not (equal (mail-header-references gnus-headers) ""))
2943 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2944 (prog1
2945 (gnus-gethash string gnus-agent-spam-hashtb)
2946 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2948 (defun gnus-agent-short-p ()
2949 "Say whether an article is short or not."
2950 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2952 (defun gnus-agent-long-p ()
2953 "Say whether an article is long or not."
2954 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2956 (defun gnus-agent-low-scored-p ()
2957 "Say whether an article has a low score or not."
2958 (< gnus-score gnus-agent-low-score))
2960 (defun gnus-agent-high-scored-p ()
2961 "Say whether an article has a high score or not."
2962 (> gnus-score gnus-agent-high-score))
2964 (defun gnus-agent-read-p ()
2965 "Say whether an article is read or not."
2966 (gnus-member-of-range (mail-header-number gnus-headers)
2967 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2969 (defun gnus-category-make-function (predicate)
2970 "Make a function from PREDICATE."
2971 (let ((func (gnus-category-make-function-1 predicate)))
2972 (if (and (= (length func) 1)
2973 (symbolp (car func)))
2974 (car func)
2975 (gnus-byte-compile `(lambda () ,func)))))
2977 (defun gnus-agent-true ()
2978 "Return t."
2981 (defun gnus-agent-false ()
2982 "Return nil."
2983 nil)
2985 (defun gnus-category-make-function-1 (predicate)
2986 "Make a function from PREDICATE."
2987 (cond
2988 ;; Functions are just returned as is.
2989 ((or (symbolp predicate)
2990 (functionp predicate))
2991 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2992 predicate)))
2993 ;; More complex predicate.
2994 ((consp predicate)
2995 `(,(cond
2996 ((memq (car predicate) '(& and))
2997 'and)
2998 ((memq (car predicate) '(| or))
2999 'or)
3000 ((memq (car predicate) gnus-category-not)
3001 'not))
3002 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
3004 (error "Unknown predicate type: %s" predicate))))
3006 (defun gnus-get-predicate (predicate)
3007 "Return the function implementing PREDICATE."
3008 (or (cdr (assoc predicate gnus-category-predicate-cache))
3009 (let ((func (gnus-category-make-function predicate)))
3010 (setq gnus-category-predicate-cache
3011 (nconc gnus-category-predicate-cache
3012 (list (cons predicate func))))
3013 func)))
3015 (defun gnus-predicate-implies-unread (predicate)
3016 "Say whether PREDICATE implies unread articles only.
3017 It is okay to miss some cases, but there must be no false positives.
3018 That is, if this predicate returns true, then indeed the predicate must
3019 return only unread articles."
3020 (eq t (gnus-function-implies-unread-1
3021 (gnus-category-make-function-1 predicate))))
3023 (defun gnus-function-implies-unread-1 (function)
3024 "Recursively evaluate a predicate function to determine whether it can select
3025 any read articles. Returns t if the function is known to never
3026 return read articles, nil when it is known to always return read
3027 articles, and t_nil when the function may return both read and unread
3028 articles."
3029 (let ((func (car function))
3030 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
3031 (cond ((eq func 'and)
3032 (cond ((memq t args) ; if any argument returns only unread articles
3033 ;; then that argument constrains the result to only unread articles.
3035 ((memq 't_nil args) ; if any argument is indeterminate
3036 ;; then the result is indeterminate
3037 't_nil)))
3038 ((eq func 'or)
3039 (cond ((memq nil args) ; if any argument returns read articles
3040 ;; then that argument ensures that the results includes read articles.
3041 nil)
3042 ((memq 't_nil args) ; if any argument is indeterminate
3043 ;; then that argument ensures that the results are indeterminate
3044 't_nil)
3045 (t ; if all arguments return only unread articles
3046 ;; then the result returns only unread articles
3047 t)))
3048 ((eq func 'not)
3049 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
3050 ; then the result is indeterminate
3051 (car args))
3052 (t ; otherwise
3053 ; toggle the result to be the opposite of the argument
3054 (not (car args)))))
3055 ((eq func 'gnus-agent-read-p)
3056 nil) ; The read predicate NEVER returns unread articles
3057 ((eq func 'gnus-agent-false)
3058 t) ; The false predicate returns t as the empty set excludes all read articles
3059 ((eq func 'gnus-agent-true)
3060 nil) ; The true predicate ALWAYS returns read articles
3061 ((catch 'found-match
3062 (let ((alist gnus-category-predicate-alist))
3063 (while alist
3064 (if (eq func (cdar alist))
3065 (throw 'found-match t)
3066 (setq alist (cdr alist))))))
3067 't_nil) ; All other predicates return read and unread articles
3069 (error "Unknown predicate function: %s" function)))))
3071 (defun gnus-group-category (group)
3072 "Return the category GROUP belongs to."
3073 (unless gnus-category-group-cache
3074 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
3075 (let ((cs gnus-category-alist)
3076 groups cat)
3077 (while (setq cat (pop cs))
3078 (setq groups (gnus-agent-cat-groups cat))
3079 (while groups
3080 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
3081 (or (gnus-gethash group gnus-category-group-cache)
3082 (assq 'default gnus-category-alist)))
3084 (defun gnus-agent-expire-group (group &optional articles force)
3085 "Expire all old articles in GROUP.
3086 If you want to force expiring of certain articles, this function can
3087 take ARTICLES, and FORCE parameters as well.
3089 The articles on which the expiration process runs are selected as follows:
3090 if ARTICLES is null, all read and unmarked articles.
3091 if ARTICLES is t, all articles.
3092 if ARTICLES is a list, just those articles.
3093 FORCE is equivalent to setting the expiration predicates to true."
3094 (interactive (list (gnus-agent-read-group)))
3096 (if (not group)
3097 (gnus-agent-expire articles group force)
3098 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
3099 ;; expiration statistics of this single group
3100 (gnus-agent-expire-stats (list 0 0 0.0)))
3101 (if (or (not (eq articles t))
3102 (yes-or-no-p
3103 (concat "Are you sure that you want to "
3104 "expire all articles in " group "? ")))
3105 (let ((gnus-command-method (gnus-find-method-for-group group))
3106 (overview (gnus-get-buffer-create " *expire overview*"))
3107 orig)
3108 (unwind-protect
3109 (let ((active-file (gnus-agent-lib-file "active")))
3110 (when (file-exists-p active-file)
3111 (with-temp-buffer
3112 (nnheader-insert-file-contents active-file)
3113 (gnus-active-to-gnus-format
3114 gnus-command-method
3115 (setq orig (gnus-make-hashtable
3116 (count-lines (point-min) (point-max))))))
3117 (save-excursion
3118 (gnus-agent-expire-group-1
3119 group overview (gnus-gethash-safe group orig)
3120 articles force))))
3121 (kill-buffer overview))))
3122 (gnus-message 4 "%s" (gnus-agent-expire-done-message)))))
3124 (defun gnus-agent-expire-group-1 (group overview active articles force)
3125 ;; Internal function - requires caller to have set
3126 ;; gnus-command-method, initialized overview buffer, and to have
3127 ;; provided a non-nil active
3129 (let ((dir (gnus-agent-group-pathname group))
3130 (file-name-coding-system nnmail-pathname-coding-system)
3131 (decoded (gnus-agent-decoded-group-name group)))
3132 (gnus-agent-with-refreshed-group
3133 group
3134 (when (boundp 'gnus-agent-expire-current-dirs)
3135 (set 'gnus-agent-expire-current-dirs
3136 (cons dir
3137 (symbol-value 'gnus-agent-expire-current-dirs))))
3139 (if (and (not force)
3140 (eq 'DISABLE (gnus-agent-find-parameter group
3141 'agent-enable-expiration)))
3142 (gnus-message 5 "Expiry skipping over %s" decoded)
3143 (gnus-message 5 "Expiring articles in %s" decoded)
3144 (gnus-agent-load-alist group)
3145 (let* ((bytes-freed 0)
3146 (size-files-deleted 0.0)
3147 (files-deleted 0)
3148 (nov-entries-deleted 0)
3149 (info (gnus-get-info group))
3150 (alist gnus-agent-article-alist)
3151 (day (- (time-to-days (current-time))
3152 (gnus-agent-find-parameter group 'agent-days-until-old)))
3153 (specials (if (and alist
3154 (not force))
3155 ;; This could be a bit of a problem. I need to
3156 ;; keep the last article to avoid refetching
3157 ;; headers when using nntp in the backend. At
3158 ;; the same time, if someone uses a backend
3159 ;; that supports article moving then I may have
3160 ;; to remove the last article to complete the
3161 ;; move. Right now, I'm going to assume that
3162 ;; FORCE overrides specials.
3163 (list (caar (last alist)))))
3164 (unreads ;; Articles that are excluded from the
3165 ;; expiration process
3166 (cond (gnus-agent-expire-all
3167 ;; All articles are marked read by global decree
3168 nil)
3169 ((eq articles t)
3170 ;; All articles are marked read by function
3171 ;; parameter
3172 nil)
3173 ((not articles)
3174 ;; Unread articles are marked protected from
3175 ;; expiration Don't call
3176 ;; gnus-list-of-unread-articles as it returns
3177 ;; articles that have not been fetched into the
3178 ;; agent.
3179 (ignore-errors
3180 (gnus-agent-unread-articles group)))
3182 ;; All articles EXCEPT those named by the caller
3183 ;; are protected from expiration
3184 (gnus-sorted-difference
3185 (gnus-uncompress-range
3186 (cons (caar alist)
3187 (caar (last alist))))
3188 (sort articles '<)))))
3189 (marked ;; More articles that are excluded from the
3190 ;; expiration process
3191 (cond (gnus-agent-expire-all
3192 ;; All articles are unmarked by global decree
3193 nil)
3194 ((eq articles t)
3195 ;; All articles are unmarked by function
3196 ;; parameter
3197 nil)
3198 (articles
3199 ;; All articles may as well be unmarked as the
3200 ;; unreads list already names the articles we are
3201 ;; going to keep
3202 nil)
3204 ;; Ticked and/or dormant articles are excluded
3205 ;; from expiration
3206 (nconc
3207 (gnus-uncompress-range
3208 (cdr (assq 'tick (gnus-info-marks info))))
3209 (gnus-uncompress-range
3210 (cdr (assq 'dormant
3211 (gnus-info-marks info))))))))
3212 (nov-file (concat dir ".overview"))
3213 (cnt 0)
3214 (completed -1)
3215 dlist
3216 type)
3218 ;; The normal article alist contains elements that look like
3219 ;; (article# . fetch_date) I need to combine other
3220 ;; information with this list. For example, a flag indicating
3221 ;; that a particular article MUST BE KEPT. To do this, I'm
3222 ;; going to transform the elements to look like (article#
3223 ;; fetch_date keep_flag NOV_entry_position) Later, I'll reverse
3224 ;; the process to generate the expired article alist.
3226 ;; Convert the alist elements to (article# fetch_date nil
3227 ;; nil).
3228 (setq dlist (mapcar (lambda (e)
3229 (list (car e) (cdr e) nil nil)) alist))
3231 ;; Convert the keep lists to elements that look like (article#
3232 ;; nil keep_flag nil) then append it to the expanded dlist
3233 ;; These statements are sorted by ascending precidence of the
3234 ;; keep_flag.
3235 (setq dlist (nconc dlist
3236 (mapcar (lambda (e)
3237 (list e nil 'unread nil))
3238 unreads)))
3239 (setq dlist (nconc dlist
3240 (mapcar (lambda (e)
3241 (list e nil 'marked nil))
3242 marked)))
3243 (setq dlist (nconc dlist
3244 (mapcar (lambda (e)
3245 (list e nil 'special nil))
3246 specials)))
3248 (set-buffer overview)
3249 (erase-buffer)
3250 (buffer-disable-undo)
3251 (when (file-exists-p nov-file)
3252 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3253 (nnheader-insert-file-contents nov-file)
3254 (goto-char (point-min))
3256 (let (p)
3257 (while (< (setq p (point)) (point-max))
3258 (condition-case nil
3259 ;; If I successfully read an integer (the plus zero
3260 ;; ensures a numeric type), append the position
3261 ;; to the list
3262 (push (list (+ 0 (read (current-buffer))) nil nil
3264 dlist)
3265 (error
3266 (gnus-message 1 "gnus-agent-expire: read error \
3267 occurred when reading expression at %s in %s. Skipping to next \
3268 line." (point) nov-file)))
3269 ;; Whether I succeeded, or failed, it doesn't matter.
3270 ;; Move to the next line then try again.
3271 (forward-line 1)))
3273 (gnus-message
3274 7 "gnus-agent-expire: Loading overview... Done"))
3275 (set-buffer-modified-p nil)
3277 ;; At this point, all of the information is in dlist. The
3278 ;; only problem is that much of it is spread across multiple
3279 ;; entries. Sort then MERGE!!
3280 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3281 ;; If two entries have the same article-number then sort by
3282 ;; ascending keep_flag.
3283 (let ((special 0)
3284 (marked 1)
3285 (unread 2))
3286 (setq dlist
3287 (sort dlist
3288 (lambda (a b)
3289 (cond ((< (nth 0 a) (nth 0 b))
3291 ((> (nth 0 a) (nth 0 b))
3292 nil)
3294 (let ((a (or (symbol-value (nth 2 a))
3296 (b (or (symbol-value (nth 2 b))
3297 3)))
3298 (<= a b))))))))
3299 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3300 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3301 (let ((dlist dlist))
3302 (while (cdr dlist) ; I'm not at the end-of-list
3303 (if (eq (caar dlist) (caadr dlist))
3304 (let ((first (cdr (car dlist)))
3305 (secnd (cdr (cadr dlist))))
3306 (setcar first (or (car first)
3307 (car secnd))) ; fetch_date
3308 (setq first (cdr first)
3309 secnd (cdr secnd))
3310 (setcar first (or (car first)
3311 (car secnd))) ; Keep_flag
3312 (setq first (cdr first)
3313 secnd (cdr secnd))
3314 (setcar first (or (car first)
3315 (car secnd))) ; NOV_entry_position
3317 (setcdr dlist (cddr dlist)))
3318 (setq dlist (cdr dlist)))))
3320 ;; Check the order of the entry positions. They should be in
3321 ;; ascending order. If they aren't, the positions must be
3322 ;; converted to markers.
3323 (when (catch 'sort-results
3324 (let ((dlist dlist)
3325 (prev-pos -1)
3326 pos)
3327 (while dlist
3328 (if (setq pos (nth 3 (pop dlist)))
3329 (if (< pos prev-pos)
3330 (throw 'sort-results 'unsorted)
3331 (setq prev-pos pos))))))
3332 (gnus-message 7 "gnus-agent-expire: Unsorted overview; inserting markers to compensate.")
3333 (mapc (lambda (entry)
3334 (let ((pos (nth 3 entry)))
3335 (if pos
3336 (setf (nth 3 entry)
3337 (set-marker (make-marker)
3338 pos)))))
3339 dlist))
3341 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3343 (let* ((len (float (length dlist)))
3344 (alist (list nil))
3345 (tail-alist alist)
3346 (position-offset 0)
3349 (while dlist
3350 (let ((new-completed (truncate (* 100.0
3351 (/ (setq cnt (1+ cnt))
3352 len))))
3353 message-log-max)
3354 (when (> new-completed completed)
3355 (setq completed new-completed)
3356 (gnus-message 7 "%3d%% completed..." completed)))
3357 (let* ((entry (car dlist))
3358 (article-number (nth 0 entry))
3359 (fetch-date (nth 1 entry))
3360 (keep (nth 2 entry))
3361 (marker (nth 3 entry)))
3363 (cond
3364 ;; Kept articles are unread, marked, or special.
3365 (keep
3366 (gnus-agent-message 10
3367 "gnus-agent-expire: %s:%d: Kept %s article%s."
3368 decoded article-number keep (if fetch-date " and file" ""))
3369 (when fetch-date
3370 (unless (file-exists-p
3371 (concat dir (number-to-string
3372 article-number)))
3373 (setf (nth 1 entry) nil)
3374 (gnus-agent-message 3 "gnus-agent-expire cleared \
3375 download flag on %s:%d as the cached article file is missing."
3376 decoded (caar dlist)))
3377 (unless marker
3378 (gnus-message 1 "gnus-agent-expire detected a \
3379 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3380 (gnus-agent-append-to-list
3381 tail-alist
3382 (cons article-number fetch-date)))
3384 ;; The following articles are READ, UNMARKED, and
3385 ;; ORDINARY. See if they can be EXPIRED!!!
3386 ((setq type
3387 (cond
3388 ((not (integerp fetch-date))
3389 'read) ;; never fetched article (may expire
3390 ;; right now)
3391 ((not (file-exists-p
3392 (concat dir (number-to-string
3393 article-number))))
3394 (setf (nth 1 entry) nil)
3395 'externally-expired) ;; Can't find the cached
3396 ;; article. Handle case
3397 ;; as though this article
3398 ;; was never fetched.
3400 ;; We now have the arrival day, so we see
3401 ;; whether it's old enough to be expired.
3402 ((< fetch-date day)
3403 'expired)
3404 (force
3405 'forced)))
3407 ;; I found some reason to expire this entry.
3409 (let ((actions nil))
3410 (when (memq type '(forced expired))
3411 (ignore-errors ; Just being paranoid.
3412 (let* ((file-name (nnheader-concat dir (number-to-string
3413 article-number)))
3414 (size (float (nth 7 (file-attributes file-name)))))
3415 (incf bytes-freed size)
3416 (incf size-files-deleted size)
3417 (incf files-deleted)
3418 (delete-file file-name))
3419 (push "expired cached article" actions))
3420 (setf (nth 1 entry) nil)
3423 (when marker
3424 (push "NOV entry removed" actions)
3426 (goto-char (if (markerp marker)
3427 marker
3428 (- marker position-offset)))
3430 (incf nov-entries-deleted)
3432 (let* ((from (point-at-bol))
3433 (to (progn (forward-line 1) (point)))
3434 (freed (- to from)))
3435 (incf bytes-freed freed)
3436 (incf position-offset freed)
3437 (delete-region from to)))
3439 ;; If considering all articles is set, I can only
3440 ;; expire article IDs that are no longer in the
3441 ;; active range (That is, articles that preceed the
3442 ;; first article in the new alist).
3443 (if (and gnus-agent-consider-all-articles
3444 (>= article-number (car active)))
3445 ;; I have to keep this ID in the alist
3446 (gnus-agent-append-to-list
3447 tail-alist (cons article-number fetch-date))
3448 (push (format "Removed %s article number from \
3449 article alist" type) actions))
3451 (when actions
3452 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3453 decoded article-number
3454 (mapconcat 'identity actions ", ")))))
3456 (gnus-agent-message
3457 10 "gnus-agent-expire: %s:%d: Article kept as \
3458 expiration tests failed." decoded article-number)
3459 (gnus-agent-append-to-list
3460 tail-alist (cons article-number fetch-date)))
3463 ;; Remove markers as I intend to reuse this buffer again.
3464 (when (and marker
3465 (markerp marker))
3466 (set-marker marker nil))
3468 (setq dlist (cdr dlist))))
3470 (setq alist (cdr alist))
3472 (let ((inhibit-quit t))
3473 (unless (equal alist gnus-agent-article-alist)
3474 (setq gnus-agent-article-alist alist)
3475 (gnus-agent-save-alist group))
3477 (when (buffer-modified-p)
3478 (let ((coding-system-for-write
3479 gnus-agent-file-coding-system))
3480 (gnus-make-directory dir)
3481 (write-region (point-min) (point-max) nov-file nil
3482 'silent)
3483 ;; clear the modified flag as that I'm not confused by
3484 ;; its status on the next pass through this routine.
3485 (set-buffer-modified-p nil)
3486 (gnus-agent-update-view-total-fetched-for group t)))
3488 (when (eq articles t)
3489 (gnus-summary-update-info))))
3491 (when (boundp 'gnus-agent-expire-stats)
3492 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3493 (incf (nth 2 stats) bytes-freed)
3494 (incf (nth 1 stats) files-deleted)
3495 (incf (nth 0 stats) nov-entries-deleted)))
3497 (gnus-agent-update-files-total-fetched-for group (- size-files-deleted)))))))
3499 (defun gnus-agent-expire (&optional articles group force)
3500 "Expire all old articles.
3501 If you want to force expiring of certain articles, this function can
3502 take ARTICLES, GROUP and FORCE parameters as well.
3504 The articles on which the expiration process runs are selected as follows:
3505 if ARTICLES is null, all read and unmarked articles.
3506 if ARTICLES is t, all articles.
3507 if ARTICLES is a list, just those articles.
3508 Setting GROUP will limit expiration to that group.
3509 FORCE is equivalent to setting the expiration predicates to true."
3510 (interactive)
3512 (if group
3513 (gnus-agent-expire-group group articles force)
3514 (if (or (not (eq articles t))
3515 (yes-or-no-p "Are you sure that you want to expire all \
3516 articles in every agentized group? "))
3517 (let ((methods (gnus-agent-covered-methods))
3518 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3519 ;; of agent directories.
3520 (gnus-agent-expire-current-dirs nil)
3521 ;; Bind gnus-agent-expire-stats to enable tracking of
3522 ;; expiration statistics across all groups
3523 (gnus-agent-expire-stats (list 0 0 0.0))
3524 gnus-command-method overview orig)
3525 (setq overview (gnus-get-buffer-create " *expire overview*"))
3526 (unwind-protect
3527 (while (setq gnus-command-method (pop methods))
3528 (let ((active-file (gnus-agent-lib-file "active")))
3529 (when (file-exists-p active-file)
3530 (with-temp-buffer
3531 (nnheader-insert-file-contents active-file)
3532 (gnus-active-to-gnus-format
3533 gnus-command-method
3534 (setq orig (gnus-make-hashtable
3535 (count-lines (point-min) (point-max))))))
3536 (dolist (expiring-group (gnus-groups-from-server
3537 gnus-command-method))
3538 (let* ((active
3539 (gnus-gethash-safe expiring-group orig)))
3541 (when active
3542 (save-excursion
3543 (gnus-agent-expire-group-1
3544 expiring-group overview active articles force))))))))
3545 (kill-buffer overview))
3546 (gnus-agent-expire-unagentized-dirs)
3547 (gnus-message 4 "%s" (gnus-agent-expire-done-message))))))
3549 (defun gnus-agent-expire-done-message ()
3550 (if (and (> gnus-verbose 4)
3551 (boundp 'gnus-agent-expire-stats))
3552 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3553 (size (nth 2 stats))
3554 (units '(B KB MB GB)))
3555 (while (and (> size 1024.0)
3556 (cdr units))
3557 (setq size (/ size 1024.0)
3558 units (cdr units)))
3560 (format "Expiry recovered %d NOV entries, deleted %d files,\
3561 and freed %f %s."
3562 (nth 0 stats)
3563 (nth 1 stats)
3564 size (car units)))
3565 "Expiry...done"))
3567 (defun gnus-agent-expire-unagentized-dirs ()
3568 (when (and gnus-agent-expire-unagentized-dirs
3569 (boundp 'gnus-agent-expire-current-dirs))
3570 (let* ((keep (gnus-make-hashtable))
3571 ;; Formally bind gnus-agent-expire-current-dirs so that the
3572 ;; compiler will not complain about free references.
3573 (gnus-agent-expire-current-dirs
3574 (symbol-value 'gnus-agent-expire-current-dirs))
3576 (file-name-coding-system nnmail-pathname-coding-system))
3578 (gnus-sethash gnus-agent-directory t keep)
3579 (while gnus-agent-expire-current-dirs
3580 (setq dir (pop gnus-agent-expire-current-dirs))
3581 (when (and (stringp dir)
3582 (file-directory-p dir))
3583 (while (not (gnus-gethash dir keep))
3584 (gnus-sethash dir t keep)
3585 (setq dir (file-name-directory (directory-file-name dir))))))
3587 (let* (to-remove
3588 checker
3589 (checker
3590 (function
3591 (lambda (d)
3592 "Given a directory, check it and its subdirectories for
3593 membership in the keep hash. If it isn't found, add
3594 it to to-remove."
3595 (let ((files (directory-files d))
3596 file)
3597 (while (setq file (pop files))
3598 (cond ((equal file ".") ; Ignore self
3599 nil)
3600 ((equal file "..") ; Ignore parent
3601 nil)
3602 ((equal file ".overview")
3603 ;; Directory must contain .overview to be
3604 ;; agent's cache of a group.
3605 (let ((d (file-name-as-directory d))
3607 ;; Search ancestor's for last directory NOT
3608 ;; found in keep hash.
3609 (while (not (gnus-gethash
3610 (setq d (file-name-directory d)) keep))
3611 (setq r d
3612 d (directory-file-name d)))
3613 ;; if ANY ancestor was NOT in keep hash and
3614 ;; it it's already in to-remove, add it to
3615 ;; to-remove.
3616 (if (and r
3617 (not (member r to-remove)))
3618 (push r to-remove))))
3619 ((file-directory-p (setq file (nnheader-concat d file)))
3620 (funcall checker file)))))))))
3621 (funcall checker (expand-file-name gnus-agent-directory))
3623 (when (and to-remove
3624 (or gnus-expert-user
3625 (gnus-y-or-n-p
3626 "gnus-agent-expire has identified local directories that are\
3627 not currently required by any agentized group. Do you wish to consider\
3628 deleting them?")))
3629 (while to-remove
3630 (let ((dir (pop to-remove)))
3631 (if (or gnus-expert-user
3632 (gnus-y-or-n-p (format "Delete %s? " dir)))
3633 (let* (delete-recursive
3634 files f
3635 (delete-recursive
3636 (function
3637 (lambda (f-or-d)
3638 (ignore-errors
3639 (if (file-directory-p f-or-d)
3640 (condition-case nil
3641 (delete-directory f-or-d)
3642 (file-error
3643 (setq files (directory-files f-or-d))
3644 (while files
3645 (setq f (pop files))
3646 (or (member f '("." ".."))
3647 (funcall delete-recursive
3648 (nnheader-concat
3649 f-or-d f))))
3650 (delete-directory f-or-d)))
3651 (delete-file f-or-d)))))))
3652 (funcall delete-recursive dir))))))))))
3654 ;;;###autoload
3655 (defun gnus-agent-batch ()
3656 "Start Gnus, send queue and fetch session."
3657 (interactive)
3658 (let ((init-file-user "")
3659 (gnus-always-read-dribble-file t))
3660 (gnus))
3661 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3662 (gnus-group-send-queue)
3663 (gnus-agent-fetch-session)))
3665 (defun gnus-agent-unread-articles (group)
3666 (let* ((read (gnus-info-read (gnus-get-info group)))
3667 (known (gnus-agent-load-alist group))
3668 (unread (list nil))
3669 (tail-unread unread))
3670 (while (and known read)
3671 (let ((candidate (car (pop known))))
3672 (while (let* ((range (car read))
3673 (min (if (numberp range) range (car range)))
3674 (max (if (numberp range) range (cdr range))))
3675 (cond ((or (not min)
3676 (< candidate min))
3677 (gnus-agent-append-to-list tail-unread candidate)
3678 nil)
3679 ((> candidate max)
3680 (setq read (cdr read))
3681 ;; return t so that I always loop one more
3682 ;; time. If I just iterated off the end of
3683 ;; read, min will become nil and the current
3684 ;; candidate will be added to the unread list.
3685 t))))))
3686 (while known
3687 (gnus-agent-append-to-list tail-unread (car (pop known))))
3688 (cdr unread)))
3690 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3691 "Restrict ARTICLES to numbers already fetched.
3692 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3693 that have already been fetched.
3694 If CACHED-HEADER is nil, articles are only excluded if the article itself
3695 has been fetched."
3697 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3698 ;; 'car gnus-agent-article-alist))
3700 ;; Functionally, I don't need to construct a temp list using mapcar.
3702 (if (and (or gnus-agent-cache (not gnus-plugged))
3703 (gnus-agent-load-alist group))
3704 (let* ((ref gnus-agent-article-alist)
3705 (arts articles)
3706 (uncached (list nil))
3707 (tail-uncached uncached))
3708 (while (and ref arts)
3709 (let ((v1 (car arts))
3710 (v2 (caar ref)))
3711 (cond ((< v1 v2) ; v1 does not appear in the reference list
3712 (gnus-agent-append-to-list tail-uncached v1)
3713 (setq arts (cdr arts)))
3714 ((= v1 v2)
3715 (unless (or cached-header (cdar ref)) ; v1 is already cached
3716 (gnus-agent-append-to-list tail-uncached v1))
3717 (setq arts (cdr arts))
3718 (setq ref (cdr ref)))
3719 (t ; reference article (v2) preceeds the list being filtered
3720 (setq ref (cdr ref))))))
3721 (while arts
3722 (gnus-agent-append-to-list tail-uncached (pop arts)))
3723 (cdr uncached))
3724 ;; if gnus-agent-load-alist fails, no articles are cached.
3725 articles))
3727 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3728 (save-excursion
3729 (gnus-agent-create-buffer)
3730 (let ((gnus-decode-encoded-word-function 'identity)
3731 (gnus-decode-encoded-address-function 'identity)
3732 (file (gnus-agent-article-name ".overview" group))
3733 cached-articles uncached-articles
3734 (file-name-coding-system nnmail-pathname-coding-system))
3735 (gnus-make-directory (nnheader-translate-file-chars
3736 (file-name-directory file) t))
3738 ;; Populate temp buffer with known headers
3739 (when (file-exists-p file)
3740 (with-current-buffer gnus-agent-overview-buffer
3741 (erase-buffer)
3742 (let ((nnheader-file-coding-system
3743 gnus-agent-file-coding-system))
3744 (nnheader-insert-nov-file file (car articles)))))
3746 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3748 (progn
3749 ;; Populate nntp-server-buffer with uncached headers
3750 (set-buffer nntp-server-buffer)
3751 (erase-buffer)
3752 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3753 (gnus-retrieve-headers
3754 uncached-articles group))))
3755 (nnvirtual-convert-headers))
3756 ((eq 'nntp (car gnus-current-select-method))
3757 ;; The author of gnus-get-newsgroup-headers-xover
3758 ;; reports that the XOVER command is commonly
3759 ;; unreliable. The problem is that recently
3760 ;; posted articles may not be entered into the
3761 ;; NOV database in time to respond to my XOVER
3762 ;; query.
3764 ;; I'm going to use his assumption that the NOV
3765 ;; database is updated in order of ascending
3766 ;; article ID. Therefore, a response containing
3767 ;; article ID N implies that all articles from 1
3768 ;; to N-1 are up-to-date. Therefore, missing
3769 ;; articles in that range have expired.
3771 (set-buffer nntp-server-buffer)
3772 (let* ((fetched-articles (list nil))
3773 (tail-fetched-articles fetched-articles)
3774 (min (cond ((numberp fetch-old)
3775 (max 1 (- (car articles) fetch-old)))
3776 (fetch-old
3779 (car articles))))
3780 (max (car (last articles))))
3782 ;; Get the list of articles that were fetched
3783 (goto-char (point-min))
3784 (let ((pm (point-max))
3785 art)
3786 (while (< (point) pm)
3787 (when (setq art (gnus-agent-read-article-number))
3788 (gnus-agent-append-to-list tail-fetched-articles art))
3789 (forward-line 1)))
3791 ;; Clip this list to the headers that will
3792 ;; actually be returned
3793 (setq fetched-articles (gnus-list-range-intersection
3794 (cdr fetched-articles)
3795 (cons min max)))
3797 ;; Clip the uncached articles list to exclude
3798 ;; IDs after the last FETCHED header. The
3799 ;; excluded IDs may be fetchable using HEAD.
3800 (if (car tail-fetched-articles)
3801 (setq uncached-articles
3802 (gnus-list-range-intersection
3803 uncached-articles
3804 (cons (car uncached-articles)
3805 (car tail-fetched-articles)))))
3807 ;; Create the list of articles that were
3808 ;; "successfully" fetched. Success, in this
3809 ;; case, means that the ID should not be
3810 ;; fetched again. In the case of an expired
3811 ;; article, the header will not be fetched.
3812 (setq uncached-articles
3813 (gnus-sorted-nunion fetched-articles
3814 uncached-articles))
3817 ;; Erase the temp buffer
3818 (set-buffer gnus-agent-overview-buffer)
3819 (erase-buffer)
3821 ;; Copy the nntp-server-buffer to the temp buffer
3822 (set-buffer nntp-server-buffer)
3823 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3825 ;; Merge the temp buffer with the known headers (found on
3826 ;; disk in FILE) into the nntp-server-buffer
3827 (when uncached-articles
3828 (gnus-agent-braid-nov group uncached-articles file))
3830 ;; Save the new set of known headers to FILE
3831 (set-buffer nntp-server-buffer)
3832 (let ((coding-system-for-write
3833 gnus-agent-file-coding-system))
3834 (gnus-agent-check-overview-buffer)
3835 (write-region (point-min) (point-max) file nil 'silent))
3837 (gnus-agent-update-view-total-fetched-for group t)
3839 ;; Update the group's article alist to include the newly
3840 ;; fetched articles.
3841 (gnus-agent-load-alist group)
3842 (gnus-agent-save-alist group uncached-articles nil)
3845 ;; Copy the temp buffer to the nntp-server-buffer
3846 (set-buffer nntp-server-buffer)
3847 (erase-buffer)
3848 (insert-buffer-substring gnus-agent-overview-buffer)))
3850 (if (and fetch-old
3851 (not (numberp fetch-old)))
3852 t ; Don't remove anything.
3853 (nnheader-nov-delete-outside-range
3854 (if fetch-old (max 1 (- (car articles) fetch-old))
3855 (car articles))
3856 (car (last articles)))
3859 'nov))
3861 (defun gnus-agent-request-article (article group)
3862 "Retrieve ARTICLE in GROUP from the agent cache."
3863 (when (and gnus-agent
3864 (or gnus-agent-cache
3865 (not gnus-plugged))
3866 (numberp article))
3867 (let* ((gnus-command-method (gnus-find-method-for-group group))
3868 (file (gnus-agent-article-name (number-to-string article) group))
3869 (buffer-read-only nil)
3870 (file-name-coding-system nnmail-pathname-coding-system))
3871 (when (and (file-exists-p file)
3872 (> (nth 7 (file-attributes file)) 0))
3873 (erase-buffer)
3874 (gnus-kill-all-overlays)
3875 (let ((coding-system-for-read gnus-cache-coding-system))
3876 (insert-file-contents file))
3877 t))))
3879 (defun gnus-agent-regenerate-group (group &optional reread)
3880 "Regenerate GROUP.
3881 If REREAD is t, all articles in the .overview are marked as unread.
3882 If REREAD is a list, the specified articles will be marked as unread.
3883 In addition, their NOV entries in .overview will be refreshed using
3884 the articles' current headers.
3885 If REREAD is not nil, downloaded articles are marked as unread."
3886 (interactive
3887 (list (gnus-agent-read-group)
3888 (catch 'mark
3889 (while (let (c
3890 (cursor-in-echo-area t)
3891 (echo-keystrokes 0))
3892 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3893 (setq c (read-char-exclusive))
3895 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3896 (throw 'mark nil))
3897 ((or (eq c ?a) (eq c ?A))
3898 (throw 'mark t))
3899 ((or (eq c ?d) (eq c ?D))
3900 (throw 'mark 'some)))
3901 (gnus-message 3 "Ignoring unexpected input")
3902 (sit-for 1)
3903 t)))))
3904 (when group
3905 (gnus-message 5 "Regenerating in %s" (gnus-agent-decoded-group-name group))
3906 (let* ((gnus-command-method (or gnus-command-method
3907 (gnus-find-method-for-group group)))
3908 (file (gnus-agent-article-name ".overview" group))
3909 (dir (file-name-directory file))
3910 point
3911 (file-name-coding-system nnmail-pathname-coding-system)
3912 (downloaded (if (file-exists-p dir)
3913 (sort (delq nil (mapcar (lambda (name)
3914 (and (not (file-directory-p (nnheader-concat dir name)))
3915 (string-to-number name)))
3916 (directory-files dir nil "^[0-9]+$" t)))
3918 (progn (gnus-make-directory dir) nil)))
3919 dl nov-arts
3920 alist header
3921 regenerated)
3923 (mm-with-unibyte-buffer
3924 (if (file-exists-p file)
3925 (let ((nnheader-file-coding-system
3926 gnus-agent-file-coding-system))
3927 (nnheader-insert-file-contents file)))
3928 (set-buffer-modified-p nil)
3930 ;; Load the article IDs found in the overview file. As a
3931 ;; side-effect, validate the file contents.
3932 (let ((load t))
3933 (while load
3934 (setq load nil)
3935 (goto-char (point-min))
3936 (while (< (point) (point-max))
3937 (cond ((and (looking-at "[0-9]+\t")
3938 (<= (- (match-end 0) (match-beginning 0)) 9))
3939 (push (read (current-buffer)) nov-arts)
3940 (forward-line 1)
3941 (let ((l1 (car nov-arts))
3942 (l2 (cadr nov-arts)))
3943 (cond ((and (listp reread) (memq l1 reread))
3944 (gnus-delete-line)
3945 (setq nov-arts (cdr nov-arts))
3946 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3947 entry of article %s deleted." l1))
3948 ((not l2)
3949 nil)
3950 ((< l1 l2)
3951 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3952 entries are NOT in ascending order.")
3953 ;; Don't sort now as I haven't verified
3954 ;; that every line begins with a number
3955 (setq load t))
3956 ((= l1 l2)
3957 (forward-line -1)
3958 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3959 entries contained duplicate of article %s. Duplicate deleted." l1)
3960 (gnus-delete-line)
3961 (setq nov-arts (cdr nov-arts))))))
3963 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3964 entries contained line that did not begin with an article number. Deleted\
3965 line.")
3966 (gnus-delete-line))))
3967 (when load
3968 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3969 entries into ascending order.")
3970 (sort-numeric-fields 1 (point-min) (point-max))
3971 (setq nov-arts nil))))
3972 (gnus-agent-check-overview-buffer)
3974 ;; Construct a new article alist whose nodes match every header
3975 ;; in the .overview file. As a side-effect, missing headers are
3976 ;; reconstructed from the downloaded article file.
3977 (while (or downloaded nov-arts)
3978 (cond ((and downloaded
3979 (or (not nov-arts)
3980 (> (car downloaded) (car nov-arts))))
3981 ;; This entry is missing from the overview file
3982 (gnus-message 3 "Regenerating NOV %s %d..."
3983 (gnus-agent-decoded-group-name group)
3984 (car downloaded))
3985 (let ((file (concat dir (number-to-string (car downloaded)))))
3986 (mm-with-unibyte-buffer
3987 (nnheader-insert-file-contents file)
3988 (nnheader-remove-body)
3989 (setq header (nnheader-parse-naked-head)))
3990 (mail-header-set-number header (car downloaded))
3991 (if nov-arts
3992 (let ((key (concat "^" (int-to-string (car nov-arts))
3993 "\t")))
3994 (or (re-search-backward key nil t)
3995 (re-search-forward key))
3996 (forward-line 1))
3997 (goto-char (point-min)))
3998 (nnheader-insert-nov header))
3999 (setq nov-arts (cons (car downloaded) nov-arts)))
4000 ((eq (car downloaded) (car nov-arts))
4001 ;; This entry in the overview has been downloaded
4002 (push (cons (car downloaded)
4003 (time-to-days
4004 (nth 5 (file-attributes
4005 (concat dir (number-to-string
4006 (car downloaded))))))) alist)
4007 (setq downloaded (cdr downloaded))
4008 (setq nov-arts (cdr nov-arts)))
4010 ;; This entry in the overview has not been downloaded
4011 (push (cons (car nov-arts) nil) alist)
4012 (setq nov-arts (cdr nov-arts)))))
4014 ;; When gnus-agent-consider-all-articles is set,
4015 ;; gnus-agent-regenerate-group should NOT remove article IDs from
4016 ;; the alist. Those IDs serve as markers to indicate that an
4017 ;; attempt has been made to fetch that article's header.
4019 ;; When gnus-agent-consider-all-articles is NOT set,
4020 ;; gnus-agent-regenerate-group can remove the article ID of every
4021 ;; article (with the exception of the last ID in the list - it's
4022 ;; special) that no longer appears in the overview. In this
4023 ;; situtation, the last article ID in the list implies that it,
4024 ;; and every article ID preceeding it, have been fetched from the
4025 ;; server.
4027 (if gnus-agent-consider-all-articles
4028 ;; Restore all article IDs that were not found in the overview file.
4029 (let* ((n (cons nil alist))
4030 (merged n)
4031 (o (gnus-agent-load-alist group)))
4032 (while o
4033 (let ((nID (caadr n))
4034 (oID (caar o)))
4035 (cond ((not nID)
4036 (setq n (setcdr n (list (list oID))))
4037 (setq o (cdr o)))
4038 ((< oID nID)
4039 (setcdr n (cons (list oID) (cdr n)))
4040 (setq o (cdr o)))
4041 ((= oID nID)
4042 (setq o (cdr o))
4043 (setq n (cdr n)))
4045 (setq n (cdr n))))))
4046 (setq alist (cdr merged)))
4047 ;; Restore the last article ID if it is not already in the new alist
4048 (let ((n (last alist))
4049 (o (last (gnus-agent-load-alist group))))
4050 (cond ((not o)
4051 nil)
4052 ((not n)
4053 (push (cons (caar o) nil) alist))
4054 ((< (caar n) (caar o))
4055 (setcdr n (list (car o)))))))
4057 (let ((inhibit-quit t))
4058 (if (setq regenerated (buffer-modified-p))
4059 (let ((coding-system-for-write gnus-agent-file-coding-system))
4060 (write-region (point-min) (point-max) file nil 'silent)))
4062 (setq regenerated (or regenerated
4063 (and reread gnus-agent-article-alist)
4064 (not (equal alist gnus-agent-article-alist))))
4066 (setq gnus-agent-article-alist alist)
4068 (when regenerated
4069 (gnus-agent-save-alist group)
4071 ;; I have to alter the group's active range NOW as
4072 ;; gnus-make-ascending-articles-unread will use it to
4073 ;; recalculate the number of unread articles in the group
4075 (let ((group (gnus-group-real-name group))
4076 (group-active (or (gnus-active group)
4077 (gnus-activate-group group))))
4078 (gnus-agent-possibly-alter-active group group-active)))))
4080 (when (and reread gnus-agent-article-alist)
4081 (gnus-agent-synchronize-group-flags
4082 group
4083 (list (list
4084 (if (listp reread)
4085 reread
4086 (delq nil (mapcar (function (lambda (c)
4087 (cond ((eq reread t)
4088 (car c))
4089 ((cdr c)
4090 (car c)))))
4091 gnus-agent-article-alist)))
4092 'del '(read)))
4093 gnus-command-method)
4095 (when regenerated
4096 (gnus-agent-update-files-total-fetched-for group nil)))
4098 (gnus-message 5 "")
4099 regenerated)))
4101 ;;;###autoload
4102 (defun gnus-agent-regenerate (&optional clean reread)
4103 "Regenerate all agent covered files.
4104 If CLEAN, obsolete (ignore)."
4105 (interactive "P")
4106 (let (regenerated)
4107 (gnus-message 4 "Regenerating Gnus agent files...")
4108 (dolist (gnus-command-method (gnus-agent-covered-methods))
4109 (dolist (group (gnus-groups-from-server gnus-command-method))
4110 (setq regenerated (or (gnus-agent-regenerate-group group reread)
4111 regenerated))))
4112 (gnus-message 4 "Regenerating Gnus agent files...done")
4114 regenerated))
4116 (defun gnus-agent-go-online (&optional force)
4117 "Switch servers into online status."
4118 (interactive (list t))
4119 (dolist (server gnus-opened-servers)
4120 (when (eq (nth 1 server) 'offline)
4121 (if (if (eq force 'ask)
4122 (gnus-y-or-n-p
4123 (format "Switch %s:%s into online status? "
4124 (caar server) (cadar server)))
4125 force)
4126 (setcar (nthcdr 1 server) 'close)))))
4128 (defun gnus-agent-toggle-group-plugged (group)
4129 "Toggle the status of the server of the current group."
4130 (interactive (list (gnus-group-group-name)))
4131 (let* ((method (gnus-find-method-for-group group))
4132 (status (cadr (assoc method gnus-opened-servers))))
4133 (if (eq status 'offline)
4134 (gnus-server-set-status method 'closed)
4135 (gnus-close-server method)
4136 (gnus-server-set-status method 'offline))
4137 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
4138 (if (eq status 'offline) 'offline 'online)
4139 (if (eq status 'offline) 'online 'offline))))
4141 (defun gnus-agent-group-covered-p (group)
4142 (gnus-agent-method-p (gnus-group-method group)))
4144 (defun gnus-agent-update-files-total-fetched-for
4145 (group delta &optional method path)
4146 "Update, or set, the total disk space used by the articles that the
4147 agent has fetched."
4148 (when gnus-agent-total-fetched-hashtb
4149 (gnus-agent-with-refreshed-group
4150 group
4151 ;; if null, gnus-agent-group-pathname will calc method.
4152 (let* ((gnus-command-method method)
4153 (path (or path (gnus-agent-group-pathname group)))
4154 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4155 (gnus-sethash path (make-list 3 0)
4156 gnus-agent-total-fetched-hashtb)))
4157 (file-name-coding-system nnmail-pathname-coding-system))
4158 (when (listp delta)
4159 (if delta
4160 (let ((sum 0.0)
4161 file)
4162 (while (setq file (pop delta))
4163 (incf sum (float (or (nth 7 (file-attributes
4164 (nnheader-concat
4165 path
4166 (if (numberp file)
4167 (number-to-string file)
4168 file)))) 0))))
4169 (setq delta sum))
4170 (let ((sum (- (nth 2 entry)))
4171 (info (directory-files-and-attributes path nil "^-?[0-9]+$" t))
4172 file)
4173 (while (setq file (pop info))
4174 (incf sum (float (or (nth 8 file) 0))))
4175 (setq delta sum))))
4177 (setq gnus-agent-need-update-total-fetched-for t)
4178 (incf (nth 2 entry) delta)))))
4180 (defun gnus-agent-update-view-total-fetched-for
4181 (group agent-over &optional method path)
4182 "Update, or set, the total disk space used by the .agentview and
4183 .overview files. These files are calculated separately as they can be
4184 modified."
4185 (when gnus-agent-total-fetched-hashtb
4186 (gnus-agent-with-refreshed-group
4187 group
4188 ;; if null, gnus-agent-group-pathname will calc method.
4189 (let* ((gnus-command-method method)
4190 (path (or path (gnus-agent-group-pathname group)))
4191 (entry (or (gnus-gethash path gnus-agent-total-fetched-hashtb)
4192 (gnus-sethash path (make-list 3 0)
4193 gnus-agent-total-fetched-hashtb)))
4194 (file-name-coding-system nnmail-pathname-coding-system)
4195 (size (or (nth 7 (file-attributes
4196 (nnheader-concat
4197 path (if agent-over
4198 ".overview"
4199 ".agentview"))))
4200 0)))
4201 (setq gnus-agent-need-update-total-fetched-for t)
4202 (setf (nth (if agent-over 1 0) entry) size)))))
4204 (defun gnus-agent-total-fetched-for (group &optional method no-inhibit)
4205 "Get the total disk space used by the specified GROUP."
4206 (unless (equal group "dummy.group")
4207 (unless gnus-agent-total-fetched-hashtb
4208 (setq gnus-agent-total-fetched-hashtb (gnus-make-hashtable 1024)))
4210 ;; if null, gnus-agent-group-pathname will calc method.
4211 (let* ((gnus-command-method method)
4212 (path (gnus-agent-group-pathname group))
4213 (entry (gnus-gethash path gnus-agent-total-fetched-hashtb)))
4214 (if entry
4215 (apply '+ entry)
4216 (let ((gnus-agent-inhibit-update-total-fetched-for (not no-inhibit)))
4218 (gnus-agent-update-view-total-fetched-for group nil method path)
4219 (gnus-agent-update-view-total-fetched-for group t method path)
4220 (gnus-agent-update-files-total-fetched-for group nil method path)))))))
4222 (provide 'gnus-agent)
4224 ;;; gnus-agent.el ends here