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