* mm-uu.el (mm-uu-pgp-signed-test): Erase prompt.
[gnus] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Gnus
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-cache)
30 (require 'nnmail)
31 (require 'nnvirtual)
32 (require 'gnus-sum)
33 (require 'gnus-score)
34 (require 'gnus-srvr)
35 (require 'gnus-util)
36 (eval-when-compile
37   (if (featurep 'xemacs)
38       (require 'itimer)
39     (require 'timer))
40   (require 'cl))
41
42 (eval-and-compile
43   (autoload 'gnus-server-update-server "gnus-srvr")
44   (autoload 'gnus-agent-customize-category "gnus-cus")
45 )
46
47 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48   "Where the Gnus agent will store its files."
49   :group 'gnus-agent
50   :type 'directory)
51
52 (defcustom gnus-agent-plugged-hook nil
53   "Hook run when plugging into the network."
54   :group 'gnus-agent
55   :type 'hook)
56
57 (defcustom gnus-agent-unplugged-hook nil
58   "Hook run when unplugging from the network."
59   :group 'gnus-agent
60   :type 'hook)
61
62 (defcustom gnus-agent-fetched-hook nil
63   "Hook run when finished fetching articles."
64   :version "22.1"
65   :group 'gnus-agent
66   :type 'hook)
67
68 (defcustom gnus-agent-handle-level gnus-level-subscribed
69   "Groups on levels higher than this variable will be ignored by the Agent."
70   :group 'gnus-agent
71   :type 'integer)
72
73 (defcustom gnus-agent-expire-days 7
74   "Read articles older than this will be expired.
75 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
76   :group 'gnus-agent
77   :type '(number :tag "days"))
78
79 (defcustom gnus-agent-expire-all nil
80   "If non-nil, also expire unread, ticked and dormant articles.
81 If nil, only read articles will be expired."
82   :group 'gnus-agent
83   :type 'boolean)
84
85 (defcustom gnus-agent-group-mode-hook nil
86   "Hook run in Agent group minor modes."
87   :group 'gnus-agent
88   :type 'hook)
89
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92   (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
93
94 (defcustom gnus-agent-summary-mode-hook nil
95   "Hook run in Agent summary minor modes."
96   :group 'gnus-agent
97   :type 'hook)
98
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101   (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
102
103 (defcustom gnus-agent-server-mode-hook nil
104   "Hook run in Agent summary minor modes."
105   :group 'gnus-agent
106   :type 'hook)
107
108 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
109 (when (featurep 'xemacs)
110   (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
111
112 (defcustom gnus-agent-confirmation-function 'y-or-n-p
113   "Function to confirm when error happens."
114   :version "21.1"
115   :group 'gnus-agent
116   :type 'function)
117
118 (defcustom gnus-agent-synchronize-flags nil
119   "Indicate if flags are synchronized when you plug in.
120 If this is `ask' the hook will query the user."
121   ;; If the default switches to something else than nil, then the function
122   ;; should be fixed not be exceedingly slow.  See 2005-09-20 ChangeLog entry.
123   :version "21.1"
124   :type '(choice (const :tag "Always" t)
125                  (const :tag "Never" nil)
126                  (const :tag "Ask" ask))
127   :group 'gnus-agent)
128
129 (defcustom gnus-agent-go-online 'ask
130   "Indicate if offline servers go online when you plug in.
131 If this is `ask' the hook will query the user."
132   :version "21.3"
133   :type '(choice (const :tag "Always" t)
134                  (const :tag "Never" nil)
135                  (const :tag "Ask" ask))
136   :group 'gnus-agent)
137
138 (defcustom gnus-agent-mark-unread-after-downloaded t
139   "Indicate whether to mark articles unread after downloaded."
140   :version "21.1"
141   :type 'boolean
142   :group 'gnus-agent)
143
144 (defcustom gnus-agent-download-marks '(download)
145   "Marks for downloading."
146   :version "21.1"
147   :type '(repeat (symbol :tag "Mark"))
148   :group 'gnus-agent)
149
150 (defcustom gnus-agent-consider-all-articles nil
151   "When non-nil, the agent will let the agent predicate decide
152 whether articles need to be downloaded or not, for all articles.  When
153 nil, the default, the agent will only let the predicate decide
154 whether unread articles are downloaded or not.  If you enable this,
155 groups with large active ranges may open slower and you may also want
156 to look into the agent expiry settings to block the expiration of
157 read articles as they would just be downloaded again."
158   :version "22.1"
159   :type 'boolean
160   :group 'gnus-agent)
161
162 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
163   "Chunk size for `gnus-agent-fetch-session'.
164 The function will split its article fetches into chunks smaller than
165 this limit."
166   :version "22.1"
167   :group 'gnus-agent
168   :type 'integer)
169
170 (defcustom gnus-agent-enable-expiration 'ENABLE
171   "The default expiration state for each group.
172 When set to ENABLE, the default, `gnus-agent-expire' will expire old
173 contents from a group's local storage.  This value may be overridden
174 to disable expiration in specific categories, topics, and groups.  Of
175 course, you could change gnus-agent-enable-expiration to DISABLE then
176 enable expiration per categories, topics, and groups."
177   :version "22.1"
178   :group 'gnus-agent
179   :type '(radio (const :format "Enable " ENABLE)
180                 (const :format "Disable " DISABLE)))
181
182 (defcustom gnus-agent-expire-unagentized-dirs t
183   "*Whether expiration should expire in unagentized directories.
184 Have gnus-agent-expire scan the directories under
185 \(gnus-agent-directory) for groups that are no longer agentized.
186 When found, offer to remove them."
187   :version "22.1"
188   :type 'boolean
189   :group 'gnus-agent)
190
191 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
192   "Initially, all servers from these methods are agentized.
193 The user may remove or add servers using the Server buffer.
194 See Info node `(gnus)Server Buffer'."
195   :version "22.1"
196   :type '(repeat symbol)
197   :group 'gnus-agent)
198
199 (defcustom gnus-agent-queue-mail t
200   "Whether and when outgoing mail should be queued by the agent.
201 When `always', always queue outgoing mail.  When nil, never
202 queue.  Otherwise, queue if and only if unplugged."
203   :version "22.1"
204   :group 'gnus-agent
205   :type '(radio (const :format "Always" always)
206                 (const :format "Never" nil)
207                 (const :format "When unplugged" t)))
208
209 (defcustom gnus-agent-prompt-send-queue nil
210   "If non-nil, `gnus-group-send-queue' will prompt if called when
211 unplugged."
212   :version "22.1"
213   :group 'gnus-agent
214   :type 'boolean)
215
216 (defcustom gnus-agent-article-alist-save-format 1
217   "Indicates whether to use compression(2), versus no
218 compression(1), when writing agentview files.  The compressed
219 files do save space but load times are 6-7 times higher.  A group
220 must be opened then closed for the agentview to be updated using
221 the new format."
222   ;; Wouldn't symbols instead numbers be nicer?  --rsteib
223   :version "22.1"
224   :group 'gnus-agent
225   :type '(radio (const :format "Compressed" 2)
226                 (const :format "Uncompressed" 1)))
227
228 ;;; Internal variables
229
230 (defvar gnus-agent-history-buffers nil)
231 (defvar gnus-agent-buffer-alist nil)
232 (defvar gnus-agent-article-alist nil
233   "An assoc list identifying the articles whose headers have been fetched.
234 If successfully fetched, these headers will be stored in the group's overview
235 file.  The key of each assoc pair is the article ID, the value of each assoc
236 pair is a flag indicating whether the identified article has been downloaded
237 \(gnus-agent-fetch-articles sets the value to the day of the download).
238 NOTES:
239 1) The last element of this list can not be expired as some
240    routines (for example, get-agent-fetch-headers) use the last
241    value to track which articles have had their headers retrieved.
242 2) The function `gnus-agent-regenerate' may destructively modify the value.")
243 (defvar gnus-agent-group-alist nil)
244 (defvar gnus-category-alist nil)
245 (defvar gnus-agent-current-history nil)
246 (defvar gnus-agent-overview-buffer nil)
247 (defvar gnus-category-predicate-cache nil)
248 (defvar gnus-category-group-cache nil)
249 (defvar gnus-agent-spam-hashtb nil)
250 (defvar gnus-agent-file-name nil)
251 (defvar gnus-agent-send-mail-function nil)
252 (defvar gnus-agent-file-coding-system 'raw-text)
253 (defvar gnus-agent-file-loading-cache nil)
254 (defvar gnus-agent-total-fetched-hashtb nil)
255 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
256 (defvar gnus-agent-need-update-total-fetched-for nil)
257
258 ;; Dynamic variables
259 (defvar gnus-headers)
260 (defvar gnus-score)
261
262 ;;;
263 ;;; Setup
264 ;;;
265
266 (defun gnus-open-agent ()
267   (setq gnus-agent t)
268   (gnus-agent-read-servers)
269   (gnus-category-read)
270   (gnus-agent-create-buffer)
271   (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
272   (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
273   (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
274
275 (defun gnus-agent-create-buffer ()
276   (if (gnus-buffer-live-p gnus-agent-overview-buffer)
277       t
278     (setq gnus-agent-overview-buffer
279           (gnus-get-buffer-create " *Gnus agent overview*"))
280     (with-current-buffer gnus-agent-overview-buffer
281       (mm-enable-multibyte))
282     nil))
283
284 (gnus-add-shutdown 'gnus-close-agent 'gnus)
285
286 (defun gnus-close-agent ()
287   (setq gnus-category-predicate-cache nil
288         gnus-category-group-cache nil
289         gnus-agent-spam-hashtb nil)
290   (gnus-kill-buffer gnus-agent-overview-buffer))
291
292 ;;;
293 ;;; Utility functions
294 ;;;
295
296 (defmacro gnus-agent-with-refreshed-group (group &rest body)
297   "Performs the body then updates the group's line in the group
298 buffer.  Automatically blocks multiple updates due to recursion."
299 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
300      (when (and gnus-agent-need-update-total-fetched-for
301                 (not gnus-agent-inhibit-update-total-fetched-for))
302         (save-excursion
303           (set-buffer gnus-group-buffer)
304           (setq gnus-agent-need-update-total-fetched-for nil)
305           (gnus-group-update-group ,group t)))))
306
307 (defun gnus-agent-read-file (file)
308   "Load FILE and do a `read' there."
309   (with-temp-buffer
310     (ignore-errors
311       (nnheader-insert-file-contents file)
312       (goto-char (point-min))
313       (read (current-buffer)))))
314
315 (defsubst gnus-agent-method ()
316   (concat (symbol-name (car gnus-command-method)) "/"
317           (if (equal (cadr gnus-command-method) "")
318               "unnamed"
319             (cadr gnus-command-method))))
320
321 (defsubst gnus-agent-directory ()
322   "The name of the Gnus agent directory."
323   (nnheader-concat gnus-agent-directory
324                    (nnheader-translate-file-chars (gnus-agent-method)) "/"))
325
326 (defun gnus-agent-lib-file (file)
327   "The full name of the Gnus agent library FILE."
328   (expand-file-name file
329                     (file-name-as-directory
330                      (expand-file-name "agent.lib" (gnus-agent-directory)))))
331
332 (defun gnus-agent-cat-set-property (category property value)
333   (if value
334       (setcdr (or (assq property category)
335               (let ((cell (cons property nil)))
336                     (setcdr category (cons cell (cdr category)))
337                     cell)) value)
338     (let ((category category))
339       (while (cond ((eq property (caadr category))
340                     (setcdr category (cddr category))
341                     nil)
342                    (t
343                     (setq category (cdr category)))))))
344   category)
345
346 (eval-when-compile
347   (defmacro gnus-agent-cat-defaccessor (name prop-name)
348     "Define accessor and setter methods for manipulating a list of the form
349 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
350 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
351 manipulated as follows:
352   (func LIST): Returns VALUE1
353   (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
354     `(progn (defmacro ,name (category)
355               (list (quote cdr) (list (quote assq)
356                                       (quote (quote ,prop-name)) category)))
357
358             (define-setf-method ,name (category)
359               (let* ((--category--temp-- (make-symbol "--category--"))
360                      (--value--temp-- (make-symbol "--value--")))
361                 (list (list --category--temp--) ; temporary-variables
362                       (list category)           ; value-forms
363                       (list --value--temp--)    ; store-variables
364                       (let* ((category --category--temp--) ; store-form
365                              (value --value--temp--))
366                         (list (quote gnus-agent-cat-set-property)
367                               category
368                               (quote (quote ,prop-name))
369                               value))
370                       (list (quote ,name) --category--temp--) ; access-form
371                       )))))
372   )
373
374 (defmacro gnus-agent-cat-name (category)
375   `(car ,category))
376
377 (gnus-agent-cat-defaccessor
378  gnus-agent-cat-days-until-old             agent-days-until-old)
379 (gnus-agent-cat-defaccessor
380  gnus-agent-cat-enable-expiration          agent-enable-expiration)
381 (gnus-agent-cat-defaccessor
382  gnus-agent-cat-groups                     agent-groups)
383 (gnus-agent-cat-defaccessor
384  gnus-agent-cat-high-score                 agent-high-score)
385 (gnus-agent-cat-defaccessor
386  gnus-agent-cat-length-when-long           agent-long-article)
387 (gnus-agent-cat-defaccessor
388  gnus-agent-cat-length-when-short          agent-short-article)
389 (gnus-agent-cat-defaccessor
390  gnus-agent-cat-low-score                  agent-low-score)
391 (gnus-agent-cat-defaccessor
392  gnus-agent-cat-predicate                  agent-predicate)
393 (gnus-agent-cat-defaccessor
394  gnus-agent-cat-score-file                 agent-score)
395 (gnus-agent-cat-defaccessor
396  gnus-agent-cat-enable-undownloaded-faces  agent-enable-undownloaded-faces)
397
398
399 ;; This form is equivalent to defsetf except that it calls make-symbol
400 ;; whereas defsetf calls gensym (Using gensym creates a run-time
401 ;; dependency on the CL library).
402
403 (eval-and-compile
404   (define-setf-method gnus-agent-cat-groups (category)
405     (let* ((--category--temp-- (make-symbol "--category--"))
406            (--groups--temp-- (make-symbol "--groups--")))
407       (list (list --category--temp--)
408             (list category)
409             (list --groups--temp--)
410             (let* ((category --category--temp--)
411                    (groups --groups--temp--))
412               (list (quote gnus-agent-set-cat-groups) category groups))
413             (list (quote gnus-agent-cat-groups) --category--temp--))))
414   )
415
416 (defun gnus-agent-set-cat-groups (category groups)
417   (unless (eq groups 'ignore)
418     (let ((new-g groups)
419           (old-g (gnus-agent-cat-groups category)))
420       (cond ((eq new-g old-g)
421              ;; gnus-agent-add-group is fiddling with the group
422              ;; list. Still, Im done.
423              nil
424              )
425             ((eq new-g (cdr old-g))
426              ;; gnus-agent-add-group is fiddling with the group list
427              (setcdr (or (assq 'agent-groups category)
428                          (let ((cell (cons 'agent-groups nil)))
429                            (setcdr category (cons cell (cdr category)))
430                            cell)) new-g))
431             (t
432              (let ((groups groups))
433                (while groups
434                  (let* ((group        (pop groups))
435                         (old-category (gnus-group-category group)))
436                    (if (eq category old-category)
437                        nil
438                      (setf (gnus-agent-cat-groups old-category)
439                            (delete group (gnus-agent-cat-groups
440                                           old-category))))))
441                ;; Purge cache as preceeding loop invalidated it.
442                (setq gnus-category-group-cache nil))
443
444              (setcdr (or (assq 'agent-groups category)
445                          (let ((cell (cons 'agent-groups nil)))
446                            (setcdr category (cons cell (cdr category)))
447                            cell)) groups))))))
448
449 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
450   (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
451
452 ;;; Fetching setup functions.
453
454 (defun gnus-agent-start-fetch ()
455   "Initialize data structures for efficient fetching."
456   (gnus-agent-create-buffer))
457
458 (defun gnus-agent-stop-fetch ()
459   "Save all data structures and clean up."
460   (setq gnus-agent-spam-hashtb nil)
461   (save-excursion
462     (set-buffer nntp-server-buffer)
463     (widen)))
464
465 (defmacro gnus-agent-with-fetch (&rest forms)
466   "Do FORMS safely."
467   `(unwind-protect
468        (let ((gnus-agent-fetching t))
469          (gnus-agent-start-fetch)
470          ,@forms)
471      (gnus-agent-stop-fetch)))
472
473 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
474 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
475
476 (defmacro gnus-agent-append-to-list (tail value)
477   `(setq ,tail (setcdr ,tail (cons ,value nil))))
478
479 (defmacro gnus-agent-message (level &rest args)
480   `(if (<= ,level gnus-verbose)
481        (message ,@args)))
482
483 ;;;
484 ;;; Mode infestation
485 ;;;
486
487 (defvar gnus-agent-mode-hook nil
488   "Hook run when installing agent mode.")
489
490 (defvar gnus-agent-mode nil)
491 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
492
493 (defun gnus-agent-mode ()
494   "Minor mode for providing a agent support in Gnus buffers."
495   (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
496                                       (symbol-name major-mode))
497                         (match-string 1 (symbol-name major-mode))))
498          (mode (intern (format "gnus-agent-%s-mode" buffer))))
499     (set (make-local-variable 'gnus-agent-mode) t)
500     (set mode nil)
501     (set (make-local-variable mode) t)
502     ;; Set up the menu.
503     (when (gnus-visual-p 'agent-menu 'menu)
504       (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
505     (unless (assq 'gnus-agent-mode minor-mode-alist)
506       (push gnus-agent-mode-status minor-mode-alist))
507     (unless (assq mode minor-mode-map-alist)
508       (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
509                                                      buffer))))
510             minor-mode-map-alist))
511     (when (eq major-mode 'gnus-group-mode)
512       (let ((init-plugged gnus-plugged)
513             (gnus-agent-go-online nil))
514         ;; g-a-t-p does nothing when gnus-plugged isn't changed.
515         ;; Therefore, make certain that the current value does not
516         ;; match the desired initial value.
517         (setq gnus-plugged :unknown)
518         (gnus-agent-toggle-plugged init-plugged)))
519     (gnus-run-hooks 'gnus-agent-mode-hook
520                     (intern (format "gnus-agent-%s-mode-hook" buffer)))))
521
522 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
523 (gnus-define-keys gnus-agent-group-mode-map
524   "Ju" gnus-agent-fetch-groups
525   "Jc" gnus-enter-category-buffer
526   "Jj" gnus-agent-toggle-plugged
527   "Js" gnus-agent-fetch-session
528   "JY" gnus-agent-synchronize-flags
529   "JS" gnus-group-send-queue
530   "Ja" gnus-agent-add-group
531   "Jr" gnus-agent-remove-group
532   "Jo" gnus-agent-toggle-group-plugged)
533
534 (defun gnus-agent-group-make-menu-bar ()
535   (unless (boundp 'gnus-agent-group-menu)
536     (easy-menu-define
537      gnus-agent-group-menu gnus-agent-group-mode-map ""
538      '("Agent"
539        ["Toggle plugged" gnus-agent-toggle-plugged t]
540        ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
541        ["List categories" gnus-enter-category-buffer t]
542        ["Add (current) group to category" gnus-agent-add-group t]
543        ["Remove (current) group from category" gnus-agent-remove-group t]
544        ["Send queue" gnus-group-send-queue gnus-plugged]
545        ("Fetch"
546         ["All" gnus-agent-fetch-session gnus-plugged]
547         ["Group" gnus-agent-fetch-group gnus-plugged])
548        ["Synchronize flags" gnus-agent-synchronize-flags t]
549        ))))
550
551 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
552 (gnus-define-keys gnus-agent-summary-mode-map
553   "Jj" gnus-agent-toggle-plugged
554   "Ju" gnus-agent-summary-fetch-group
555   "JS" gnus-agent-fetch-group
556   "Js" gnus-agent-summary-fetch-series
557   "J#" gnus-agent-mark-article
558   "J\M-#" gnus-agent-unmark-article
559   "@" gnus-agent-toggle-mark
560   "Jc" gnus-agent-catchup)
561
562 (defun gnus-agent-summary-make-menu-bar ()
563   (unless (boundp 'gnus-agent-summary-menu)
564     (easy-menu-define
565      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
566      '("Agent"
567        ["Toggle plugged" gnus-agent-toggle-plugged t]
568        ["Mark as downloadable" gnus-agent-mark-article t]
569        ["Unmark as downloadable" gnus-agent-unmark-article t]
570        ["Toggle mark" gnus-agent-toggle-mark t]
571        ["Fetch downloadable" gnus-agent-summary-fetch-group t]
572        ["Catchup undownloaded" gnus-agent-catchup t]))))
573
574 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
575 (gnus-define-keys gnus-agent-server-mode-map
576   "Jj" gnus-agent-toggle-plugged
577   "Ja" gnus-agent-add-server
578   "Jr" gnus-agent-remove-server)
579
580 (defun gnus-agent-server-make-menu-bar ()
581   (unless (boundp 'gnus-agent-server-menu)
582     (easy-menu-define
583      gnus-agent-server-menu gnus-agent-server-mode-map ""
584      '("Agent"
585        ["Toggle plugged" gnus-agent-toggle-plugged t]
586        ["Add" gnus-agent-add-server t]
587        ["Remove" gnus-agent-remove-server t]))))
588
589 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
590   (if (and (fboundp 'propertize)
591            (fboundp 'make-mode-line-mouse-map))
592       (propertize string 'local-map
593                   (make-mode-line-mouse-map mouse-button mouse-func)
594                   'mouse-face 'mode-line-highlight)
595     string))
596
597 (defun gnus-agent-toggle-plugged (set-to)
598   "Toggle whether Gnus is unplugged or not."
599   (interactive (list (not gnus-plugged)))
600   (cond ((eq set-to gnus-plugged)
601          nil)
602         (set-to
603          (setq gnus-plugged set-to)
604          (gnus-run-hooks 'gnus-agent-plugged-hook)
605          (setcar (cdr gnus-agent-mode-status)
606                  (gnus-agent-make-mode-line-string " Plugged"
607                                                    'mouse-2
608                                                    'gnus-agent-toggle-plugged))
609          (gnus-agent-go-online gnus-agent-go-online)
610          (gnus-agent-possibly-synchronize-flags))
611         (t
612          (gnus-agent-close-connections)
613          (setq gnus-plugged set-to)
614          (gnus-run-hooks 'gnus-agent-unplugged-hook)
615          (setcar (cdr gnus-agent-mode-status)
616                  (gnus-agent-make-mode-line-string " Unplugged"
617                                                    'mouse-2
618                                                    'gnus-agent-toggle-plugged))))
619   (set-buffer-modified-p t))
620
621 (defmacro gnus-agent-while-plugged (&rest body)
622   `(let ((original-gnus-plugged gnus-plugged))
623     (unwind-protect
624         (progn (gnus-agent-toggle-plugged t)
625                ,@body)
626       (gnus-agent-toggle-plugged original-gnus-plugged))))
627
628 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
629 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
630
631 (defun gnus-agent-close-connections ()
632   "Close all methods covered by the Gnus agent."
633   (let ((methods (gnus-agent-covered-methods)))
634     (while methods
635       (gnus-close-server (pop methods)))))
636
637 ;;;###autoload
638 (defun gnus-unplugged ()
639   "Start Gnus unplugged."
640   (interactive)
641   (setq gnus-plugged nil)
642   (gnus))
643
644 ;;;###autoload
645 (defun gnus-plugged ()
646   "Start Gnus plugged."
647   (interactive)
648   (setq gnus-plugged t)
649   (gnus))
650
651 ;;;###autoload
652 (defun gnus-slave-unplugged (&optional arg)
653   "Read news as a slave unplugged."
654   (interactive "P")
655   (setq gnus-plugged nil)
656   (gnus arg nil 'slave))
657
658 ;;;###autoload
659 (defun gnus-agentize ()
660   "Allow Gnus to be an offline newsreader.
661
662 The gnus-agentize function is now called internally by gnus when
663 gnus-agent is set.  If you wish to avoid calling gnus-agentize,
664 customize gnus-agent to nil.
665
666 This will modify the `gnus-setup-news-hook', and
667 `message-send-mail-real-function' variables, and install the Gnus agent
668 minor mode in all Gnus buffers."
669   (interactive)
670   (gnus-open-agent)
671   (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
672   (unless gnus-agent-send-mail-function
673     (setq gnus-agent-send-mail-function
674           (or message-send-mail-real-function
675               (function (lambda () (funcall message-send-mail-function))))
676           message-send-mail-real-function 'gnus-agent-send-mail))
677
678   ;; If the servers file doesn't exist, auto-agentize some servers and
679   ;; save the servers file so this auto-agentizing isn't invoked
680   ;; again.
681   (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
682     (gnus-message 3 "First time agent user, agentizing remote groups...")
683     (mapc
684      (lambda (server-or-method)
685        (let ((method (gnus-server-to-method server-or-method)))
686          (when (memq (car method)
687                      gnus-agent-auto-agentize-methods)
688            (push (gnus-method-to-server method)
689                  gnus-agent-covered-methods)
690            (setq gnus-agent-method-p-cache nil))))
691      (cons gnus-select-method gnus-secondary-select-methods))
692     (gnus-agent-write-servers)))
693
694 (defun gnus-agent-queue-setup (&optional group-name)
695   "Make sure the queue group exists.
696 Optional arg GROUP-NAME allows to specify another group."
697   (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
698                         gnus-newsrc-hashtb)
699     (gnus-request-create-group (or group-name "queue") '(nndraft ""))
700     (let ((gnus-level-default-subscribed 1))
701       (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
702                             nil '(nndraft "")))
703     (gnus-group-set-parameter
704      (format "nndraft:%s" (or group-name "queue"))
705      'gnus-dummy '((gnus-draft-mode)))))
706
707 (defun gnus-agent-send-mail ()
708   (if (or (not gnus-agent-queue-mail)
709           (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
710       (funcall gnus-agent-send-mail-function)
711     (goto-char (point-min))
712     (re-search-forward
713      (concat "^" (regexp-quote mail-header-separator) "\n"))
714     (replace-match "\n")
715     (gnus-agent-insert-meta-information 'mail)
716     (gnus-request-accept-article "nndraft:queue" nil t t)))
717
718 (defun gnus-agent-insert-meta-information (type &optional method)
719   "Insert meta-information into the message that says how it's to be posted.
720 TYPE can be either `mail' or `news'.  If the latter, then METHOD can
721 be a select method."
722   (save-excursion
723     (message-remove-header gnus-agent-meta-information-header)
724     (goto-char (point-min))
725     (insert gnus-agent-meta-information-header ": "
726             (symbol-name type) " " (format "%S" method)
727             "\n")
728     (forward-char -1)
729     (while (search-backward "\n" nil t)
730       (replace-match "\\n" t t))))
731
732 (defun gnus-agent-restore-gcc ()
733   "Restore GCC field from saved header."
734   (save-excursion
735     (goto-char (point-min))
736     (while (re-search-forward
737             (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
738       (replace-match "Gcc:" 'fixedcase))))
739
740 (defun gnus-agent-any-covered-gcc ()
741   (save-restriction
742     (message-narrow-to-headers)
743     (let* ((gcc (mail-fetch-field "gcc" nil t))
744            (methods (and gcc
745                          (mapcar 'gnus-inews-group-method
746                                  (message-unquote-tokens
747                                   (message-tokenize-header
748                                    gcc " ,")))))
749            covered)
750       (while (and (not covered) methods)
751         (setq covered (gnus-agent-method-p (car methods))
752               methods (cdr methods)))
753       covered)))
754
755 ;;;###autoload
756 (defun gnus-agent-possibly-save-gcc ()
757   "Save GCC if Gnus is unplugged."
758   (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
759     (save-excursion
760       (goto-char (point-min))
761       (let ((case-fold-search t))
762         (while (re-search-forward "^gcc:" nil t)
763           (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
764
765 (defun gnus-agent-possibly-do-gcc ()
766   "Do GCC if Gnus is plugged."
767   (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
768     (gnus-inews-do-gcc)))
769
770 ;;;
771 ;;; Group mode commands
772 ;;;
773
774 (defun gnus-agent-fetch-groups (n)
775   "Put all new articles in the current groups into the Agent."
776   (interactive "P")
777   (unless gnus-plugged
778     (error "Groups can't be fetched when Gnus is unplugged"))
779   (gnus-group-iterate n 'gnus-agent-fetch-group))
780
781 (defun gnus-agent-fetch-group (&optional group)
782   "Put all new articles in GROUP into the Agent."
783   (interactive (list (gnus-group-group-name)))
784   (setq group (or group gnus-newsgroup-name))
785   (unless group
786     (error "No group on the current line"))
787
788   (gnus-agent-while-plugged
789     (let ((gnus-command-method (gnus-find-method-for-group group)))
790       (gnus-agent-with-fetch
791         (gnus-agent-fetch-group-1 group gnus-command-method)
792         (gnus-message 5 "Fetching %s...done" group)))))
793
794 (defun gnus-agent-add-group (category arg)
795   "Add the current group to an agent category."
796   (interactive
797    (list
798     (intern
799      (completing-read
800       "Add to category: "
801       (mapcar (lambda (cat) (list (symbol-name (car cat))))
802               gnus-category-alist)
803       nil t))
804     current-prefix-arg))
805   (let ((cat (assq category gnus-category-alist))
806         c groups)
807     (gnus-group-iterate arg
808       (lambda (group)
809         (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
810           (setf (gnus-agent-cat-groups c)
811                 (delete group (gnus-agent-cat-groups c))))
812         (push group groups)))
813     (setf (gnus-agent-cat-groups cat)
814           (nconc (gnus-agent-cat-groups cat) groups))
815     (gnus-category-write)))
816
817 (defun gnus-agent-remove-group (arg)
818   "Remove the current group from its agent category, if any."
819   (interactive "P")
820   (let (c)
821     (gnus-group-iterate arg
822       (lambda (group)
823         (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
824           (setf (gnus-agent-cat-groups c)
825                 (delete group (gnus-agent-cat-groups c))))))
826     (gnus-category-write)))
827
828 (defun gnus-agent-synchronize-flags ()
829   "Synchronize unplugged flags with servers."
830   (interactive)
831   (save-excursion
832     (dolist (gnus-command-method (gnus-agent-covered-methods))
833       (when (file-exists-p (gnus-agent-lib-file "flags"))
834         (gnus-agent-synchronize-flags-server gnus-command-method)))))
835
836 (defun gnus-agent-possibly-synchronize-flags ()
837   "Synchronize flags according to `gnus-agent-synchronize-flags'."
838   (interactive)
839   (save-excursion
840     (dolist (gnus-command-method (gnus-agent-covered-methods))
841       (when (and (file-exists-p (gnus-agent-lib-file "flags"))
842                  (eq (gnus-server-status gnus-command-method) 'ok))
843         (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
844
845 (defun gnus-agent-synchronize-flags-server (method)
846   "Synchronize flags set when unplugged for server."
847   (let ((gnus-command-method method)
848         (gnus-agent nil))
849     (when (file-exists-p (gnus-agent-lib-file "flags"))
850       (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
851       (erase-buffer)
852       (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
853       (cond ((null gnus-plugged)
854              (gnus-message
855               1 "You must be plugged to synchronize flags with server %s"
856               (nth 1 gnus-command-method)))
857             ((null (gnus-check-server gnus-command-method))
858              (gnus-message
859               1 "Couldn't open server %s" (nth 1 gnus-command-method)))
860             (t
861              (condition-case err
862                  (while t
863                    (let ((bgn (point)))
864                      (eval (read (current-buffer)))
865                      (delete-region bgn (point))))
866                (end-of-file
867                 (delete-file (gnus-agent-lib-file "flags")))
868                (error
869                 (let ((file (gnus-agent-lib-file "flags")))
870                   (write-region (point-min) (point-max)
871                                 (gnus-agent-lib-file "flags") nil 'silent)
872                   (error "Couldn't set flags from file %s due to %s"
873                          file (error-message-string err)))))))
874       (kill-buffer nil))))
875
876 (defun gnus-agent-possibly-synchronize-flags-server (method)
877   "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
878   (when (or (and gnus-agent-synchronize-flags
879                  (not (eq gnus-agent-synchronize-flags 'ask)))
880             (and (eq gnus-agent-synchronize-flags 'ask)
881                  (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
882                                         (cadr method)))))
883     (gnus-agent-synchronize-flags-server method)))
884
885 ;;;###autoload
886 (defun gnus-agent-rename-group (old-group new-group)
887   "Rename fully-qualified OLD-GROUP as NEW-GROUP.
888 Always updates the agent, even when disabled, as the old agent
889 files would corrupt gnus when the agent was next enabled.
890 Depends upon the caller to determine whether group renaming is
891 supported."
892   (let* ((old-command-method (gnus-find-method-for-group old-group))
893          (old-path           (directory-file-name
894                               (let (gnus-command-method old-command-method)
895                                 (gnus-agent-group-pathname old-group))))
896          (new-command-method (gnus-find-method-for-group new-group))
897          (new-path           (directory-file-name
898                               (let (gnus-command-method new-command-method)
899                                 (gnus-agent-group-pathname new-group)))))
900     (gnus-rename-file old-path new-path t)
901
902     (let* ((old-real-group (gnus-group-real-name old-group))
903            (new-real-group (gnus-group-real-name new-group))
904            (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
905       (gnus-agent-save-group-info old-command-method old-real-group nil)
906       (gnus-agent-save-group-info new-command-method new-real-group old-active)
907
908       (let ((old-local (gnus-agent-get-local old-group
909                                              old-real-group old-command-method)))
910         (gnus-agent-set-local old-group
911                               nil nil
912                               old-real-group old-command-method)
913         (gnus-agent-set-local new-group
914                               (car old-local) (cdr old-local)
915                               new-real-group new-command-method)))))
916
917 ;;;###autoload
918 (defun gnus-agent-delete-group (group)
919   "Delete fully-qualified GROUP.
920 Always updates the agent, even when disabled, as the old agent
921 files would corrupt gnus when the agent was next enabled.
922 Depends upon the caller to determine whether group deletion is
923 supported."
924   (let* ((command-method (gnus-find-method-for-group group))
925          (path           (directory-file-name
926                           (let (gnus-command-method command-method)
927                             (gnus-agent-group-pathname group)))))
928     (gnus-delete-directory path)
929
930     (let* ((real-group (gnus-group-real-name group)))
931       (gnus-agent-save-group-info command-method real-group nil)
932
933       (let ((local (gnus-agent-get-local group
934                                          real-group command-method)))
935         (gnus-agent-set-local group
936                               nil nil
937                               real-group command-method)))))
938
939 ;;;
940 ;;; Server mode commands
941 ;;;
942
943 (defun gnus-agent-add-server ()
944   "Enroll SERVER in the agent program."
945   (interactive)
946   (let* ((server       (gnus-server-server-name))
947          (named-server (gnus-server-named-server))
948          (method       (and server
949                             (gnus-server-get-method nil server))))
950     (unless server
951       (error "No server on the current line"))
952
953     (when (gnus-agent-method-p method)
954       (error "Server already in the agent program"))
955
956     (push named-server gnus-agent-covered-methods)
957
958     (setq gnus-agent-method-p-cache nil)
959     (gnus-server-update-server server)
960     (gnus-agent-write-servers)
961     (gnus-message 1 "Entered %s into the Agent" server)))
962
963 (defun gnus-agent-remove-server ()
964   "Remove SERVER from the agent program."
965   (interactive)
966   (let* ((server       (gnus-server-server-name))
967          (named-server (gnus-server-named-server)))
968     (unless server
969       (error "No server on the current line"))
970
971     (unless (member named-server gnus-agent-covered-methods)
972       (error "Server not in the agent program"))
973
974     (setq gnus-agent-covered-methods
975           (delete named-server gnus-agent-covered-methods)
976           gnus-agent-method-p-cache nil)
977
978     (gnus-server-update-server server)
979     (gnus-agent-write-servers)
980     (gnus-message 1 "Removed %s from the agent" server)))
981
982 (defun gnus-agent-read-servers ()
983   "Read the alist of covered servers."
984   (setq gnus-agent-covered-methods
985         (gnus-agent-read-file
986          (nnheader-concat gnus-agent-directory "lib/servers"))
987         gnus-agent-method-p-cache nil)
988
989   ;; I am called so early in start-up that I can not validate server
990   ;; names.  When that is the case, I skip the validation.  That is
991   ;; alright as the gnus startup code calls the validate methods
992   ;; directly.
993   (if gnus-server-alist
994       (gnus-agent-read-servers-validate)))
995
996 (defun gnus-agent-read-servers-validate ()
997   (mapcar (lambda (server-or-method)
998             (let* ((server (if (stringp server-or-method)
999                                server-or-method
1000                              (gnus-method-to-server server-or-method)))
1001                    (method (gnus-server-to-method server)))
1002               (if method
1003                   (unless (member server gnus-agent-covered-methods)
1004                     (push server gnus-agent-covered-methods)
1005                     (setq gnus-agent-method-p-cache nil))
1006                 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1007           (prog1 gnus-agent-covered-methods
1008             (setq gnus-agent-covered-methods nil))))
1009
1010 (defun gnus-agent-read-servers-validate-native (native-method)
1011   (setq gnus-agent-covered-methods
1012         (mapcar (lambda (method)
1013                   (if (or (not method)
1014                           (equal method native-method))
1015                       "native"
1016                     method)) gnus-agent-covered-methods)))
1017
1018 (defun gnus-agent-write-servers ()
1019   "Write the alist of covered servers."
1020   (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1021   (let ((coding-system-for-write nnheader-file-coding-system)
1022         (file-name-coding-system nnmail-pathname-coding-system))
1023     (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1024       (prin1 gnus-agent-covered-methods
1025              (current-buffer)))))
1026
1027 ;;;
1028 ;;; Summary commands
1029 ;;;
1030
1031 (defun gnus-agent-mark-article (n &optional unmark)
1032   "Mark the next N articles as downloadable.
1033 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
1034 the mark instead.  The difference between N and the actual number of
1035 articles marked is returned."
1036   (interactive "p")
1037   (let ((backward (< n 0))
1038         (n (abs n)))
1039     (while (and
1040             (> n 0)
1041             (progn
1042               (gnus-summary-set-agent-mark
1043                (gnus-summary-article-number) unmark)
1044               (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1045       (setq n (1- n)))
1046     (when (/= 0 n)
1047       (gnus-message 7 "No more articles"))
1048     (gnus-summary-recenter)
1049     (gnus-summary-position-point)
1050     n))
1051
1052 (defun gnus-agent-unmark-article (n)
1053   "Remove the downloadable mark from the next N articles.
1054 If N is negative, unmark backward instead.  The difference between N and
1055 the actual number of articles unmarked is returned."
1056   (interactive "p")
1057   (gnus-agent-mark-article n t))
1058
1059 (defun gnus-agent-toggle-mark (n)
1060   "Toggle the downloadable mark from the next N articles.
1061 If N is negative, toggle backward instead.  The difference between N and
1062 the actual number of articles toggled is returned."
1063   (interactive "p")
1064   (gnus-agent-mark-article n 'toggle))
1065
1066 (defun gnus-summary-set-agent-mark (article &optional unmark)
1067   "Mark ARTICLE as downloadable.  If UNMARK is nil, article is marked.
1068 When UNMARK is t, the article is unmarked.  For any other value, the
1069 article's mark is toggled."
1070   (let ((unmark (cond ((eq nil unmark)
1071                        nil)
1072                       ((eq t unmark)
1073                        t)
1074                       (t
1075                        (memq article gnus-newsgroup-downloadable)))))
1076     (when (gnus-summary-goto-subject article nil t)
1077       (gnus-summary-update-mark
1078        (if unmark
1079            (progn
1080              (setq gnus-newsgroup-downloadable
1081                    (delq article gnus-newsgroup-downloadable))
1082              (gnus-article-mark article))
1083          (setq gnus-newsgroup-downloadable
1084                (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1085          gnus-downloadable-mark)
1086        'unread))))
1087
1088 ;;;###autoload
1089 (defun gnus-agent-get-undownloaded-list ()
1090   "Construct list of articles that have not been downloaded."
1091   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1092     (when (set (make-local-variable 'gnus-newsgroup-agentized)
1093                (gnus-agent-method-p gnus-command-method))
1094       (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1095              (headers (sort (mapcar (lambda (h)
1096                                       (mail-header-number h))
1097                                     gnus-newsgroup-headers) '<))
1098              (cached (and gnus-use-cache gnus-newsgroup-cached))
1099              (undownloaded (list nil))
1100              (tail-undownloaded undownloaded)
1101              (unfetched (list nil))
1102              (tail-unfetched unfetched))
1103         (while (and alist headers)
1104           (let ((a (caar alist))
1105                 (h (car headers)))
1106             (cond ((< a h)
1107                    ;; Ignore IDs in the alist that are not being
1108                    ;; displayed in the summary.
1109                    (setq alist (cdr alist)))
1110                   ((> a h)
1111                    ;; Headers that are not in the alist should be
1112                    ;; fictious (see nnagent-retrieve-headers); they
1113                    ;; imply that this article isn't in the agent.
1114                    (gnus-agent-append-to-list tail-undownloaded h)
1115                    (gnus-agent-append-to-list tail-unfetched    h)
1116                    (setq headers (cdr headers)))
1117                   ((cdar alist)
1118                    (setq alist (cdr alist))
1119                    (setq headers (cdr headers))
1120                    nil                  ; ignore already downloaded
1121                    )
1122                   (t
1123                    (setq alist (cdr alist))
1124                    (setq headers (cdr headers))
1125
1126                    ;; This article isn't in the agent.  Check to see
1127                    ;; if it is in the cache.  If it is, it's been
1128                    ;; downloaded.
1129                    (while (and cached (< (car cached) a))
1130                      (setq cached (cdr cached)))
1131                    (unless (equal a (car cached))
1132                      (gnus-agent-append-to-list tail-undownloaded a))))))
1133
1134         (while headers
1135           (let ((num (pop headers)))
1136             (gnus-agent-append-to-list tail-undownloaded num)
1137             (gnus-agent-append-to-list tail-unfetched    num)))
1138
1139         (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1140               gnus-newsgroup-unfetched    (cdr unfetched))))))
1141
1142 (defun gnus-agent-catchup ()
1143   "Mark as read all unhandled articles.
1144 An article is unhandled if it is neither cached, nor downloaded, nor
1145 downloadable."
1146   (interactive)
1147   (save-excursion
1148     (let ((articles gnus-newsgroup-undownloaded))
1149       (when (or gnus-newsgroup-downloadable
1150                 gnus-newsgroup-cached)
1151         (setq articles (gnus-sorted-ndifference
1152                         (gnus-sorted-ndifference
1153                          (gnus-copy-sequence articles)
1154                          gnus-newsgroup-downloadable)
1155                         gnus-newsgroup-cached)))
1156
1157       (while articles
1158         (gnus-summary-mark-article
1159          (pop articles) gnus-catchup-mark)))
1160     (gnus-summary-position-point)))
1161
1162 (defun gnus-agent-summary-fetch-series ()
1163   (interactive)
1164   (when gnus-newsgroup-processable
1165     (setq gnus-newsgroup-downloadable
1166           (let* ((dl gnus-newsgroup-downloadable)
1167                  (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1168                  (gnus-newsgroup-downloadable processable))
1169             (gnus-agent-summary-fetch-group)
1170
1171             ;; For each article that I processed that is no longer
1172             ;; undownloaded, remove its processable mark.
1173
1174             (mapc #'gnus-summary-remove-process-mark 
1175                   (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1176
1177             ;; The preceeding call to (gnus-agent-summary-fetch-group)
1178             ;; updated the temporary gnus-newsgroup-downloadable to
1179             ;; remove each article successfully fetched.  Now, I
1180             ;; update the real gnus-newsgroup-downloadable to only
1181             ;; include undownloaded articles.
1182             (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1183
1184 (defun gnus-agent-summary-fetch-group (&optional all)
1185   "Fetch the downloadable articles in the group.
1186 Optional arg ALL, if non-nil, means to fetch all articles."
1187   (interactive "P")
1188   (let ((articles
1189          (if all gnus-newsgroup-articles
1190            gnus-newsgroup-downloadable))
1191         (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1192         fetched-articles)
1193     (gnus-agent-while-plugged
1194       (unless articles
1195         (error "No articles to download"))
1196       (gnus-agent-with-fetch
1197         (setq gnus-newsgroup-undownloaded
1198               (gnus-sorted-ndifference
1199                gnus-newsgroup-undownloaded
1200                (setq fetched-articles
1201                      (gnus-agent-fetch-articles
1202                       gnus-newsgroup-name articles)))))
1203       (save-excursion
1204         (dolist (article articles)
1205           (let ((was-marked-downloadable
1206                  (memq article gnus-newsgroup-downloadable)))
1207             (cond (gnus-agent-mark-unread-after-downloaded
1208                    (setq gnus-newsgroup-downloadable
1209                          (delq article gnus-newsgroup-downloadable))
1210
1211                    (gnus-summary-mark-article article gnus-unread-mark))
1212                   (was-marked-downloadable
1213                    (gnus-summary-set-agent-mark article t)))
1214             (when (gnus-summary-goto-subject article nil t)
1215               (gnus-summary-update-download-mark article))))))
1216     fetched-articles))
1217
1218 (defun gnus-agent-fetch-selected-article ()
1219   "Fetch the current article as it is selected.
1220 This can be added to `gnus-select-article-hook' or
1221 `gnus-mark-article-hook'."
1222   (let ((gnus-command-method gnus-current-select-method))
1223     (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1224       (when (gnus-agent-fetch-articles
1225              gnus-newsgroup-name
1226              (list gnus-current-article))
1227         (setq gnus-newsgroup-undownloaded
1228               (delq gnus-current-article gnus-newsgroup-undownloaded))
1229         (gnus-summary-update-download-mark gnus-current-article)))))
1230
1231 ;;;
1232 ;;; Internal functions
1233 ;;;
1234
1235 (defun gnus-agent-synchronize-group-flags (group actions server)
1236 "Update a plugged group by performing the indicated actions."
1237   (let* ((gnus-command-method (gnus-server-to-method server))
1238          (info
1239           ;; This initializer is required as gnus-request-set-mark
1240           ;; calls gnus-group-real-name to strip off the host name
1241           ;; before calling the backend.  Now that the backend is
1242           ;; trying to call gnus-request-set-mark, I have to
1243           ;; reconstruct the original group name.
1244           (or (gnus-get-info group)
1245               (gnus-get-info
1246                (setq group (gnus-group-full-name
1247                             group gnus-command-method))))))
1248     (gnus-request-set-mark group actions)
1249
1250     (when info
1251       (dolist (action actions)
1252         (let ((range (nth 0 action))
1253               (what  (nth 1 action))
1254               (marks (nth 2 action)))
1255           (dolist (mark marks)
1256             (cond ((eq mark 'read)
1257                    (gnus-info-set-read
1258                     info
1259                     (funcall (if (eq what 'add)
1260                                  'gnus-range-add
1261                                'gnus-remove-from-range)
1262                              (gnus-info-read info)
1263                              range))
1264                    (gnus-get-unread-articles-in-group
1265                     info
1266                     (gnus-active (gnus-info-group info))))
1267                   ((memq mark '(tick))
1268                    (let ((info-marks (assoc mark (gnus-info-marks info))))
1269                      (unless info-marks
1270                        (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1271                      (setcdr info-marks (funcall (if (eq what 'add)
1272                                   'gnus-range-add
1273                                 'gnus-remove-from-range)
1274                               (cdr info-marks)
1275                               range))))))))
1276
1277       ;;Marks can be synchronized at any time by simply toggling from
1278       ;;unplugged to plugged.  If that is what is happening right now, make
1279       ;;sure that the group buffer is up to date.
1280           (when (gnus-buffer-live-p gnus-group-buffer)
1281             (gnus-group-update-group group t)))
1282     nil))
1283
1284 (defun gnus-agent-save-active (method)
1285   (when (gnus-agent-method-p method)
1286     (let* ((gnus-command-method method)
1287            (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1288            (file (gnus-agent-lib-file "active")))
1289       (gnus-active-to-gnus-format nil new)
1290       (gnus-agent-write-active file new)
1291       (erase-buffer)
1292       (nnheader-insert-file-contents file))))
1293
1294 (defun gnus-agent-write-active (file new)
1295     (gnus-make-directory (file-name-directory file))
1296     (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1297       ;; The hashtable contains real names of groups.  However, do NOT
1298       ;; add the foreign server prefix as gnus-active-to-gnus-format
1299       ;; will add it while reading the file.
1300       (gnus-write-active-file file new nil)))
1301
1302 ;;;###autoload
1303 (defun gnus-agent-possibly-alter-active (group active &optional info)
1304   "Possibly expand a group's active range to include articles
1305 downloaded into the agent."
1306   (let* ((gnus-command-method (or gnus-command-method
1307                                   (gnus-find-method-for-group group))))
1308     (when (gnus-agent-method-p gnus-command-method)
1309       (let* ((local (gnus-agent-get-local group))
1310              (active-min (or (car active) 0))
1311              (active-max (or (cdr active) 0))
1312              (agent-min (or (car local) active-min))
1313              (agent-max (or (cdr local) active-max)))
1314
1315         (when (< agent-min active-min)
1316           (setcar active agent-min))
1317
1318         (when (> agent-max active-max)
1319           (setcdr active agent-max))
1320
1321         (when (and info (< agent-max (- active-min 100)))
1322           ;; I'm expanding the active range by such a large amount
1323           ;; that there is a gap of more than 100 articles between the
1324           ;; last article known to the agent and the first article
1325           ;; currently available on the server.  This gap contains
1326           ;; articles that have been lost, mark them as read so that
1327           ;; gnus doesn't waste resources trying to fetch them.
1328
1329           ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1330           ;; want to modify the local file everytime someone restarts
1331           ;; gnus.  The small gap will cause a tiny performance hit
1332           ;; when gnus tries, and fails, to retrieve the articles.
1333           ;; Still that should be smaller than opening a buffer,
1334           ;; printing this list to the buffer, and then writing it to a
1335           ;; file.
1336
1337           (let ((read (gnus-info-read info)))
1338             (gnus-info-set-read
1339              info
1340              (gnus-range-add
1341               read
1342               (list (cons (1+ agent-max)
1343                           (1- active-min))))))
1344
1345           ;; Lie about the agent's local range for this group to
1346           ;; disable the set read each time this server is opened.
1347           ;; NOTE: Opening this group will restore the valid local
1348           ;; range but it will also expand the local range to
1349           ;; incompass the new active range.
1350           (gnus-agent-set-local group agent-min (1- active-min)))))))
1351
1352 (defun gnus-agent-save-group-info (method group active)
1353   "Update a single group's active range in the agent's copy of the server's active file."
1354   (when (gnus-agent-method-p method)
1355     (let* ((gnus-command-method (or method gnus-command-method))
1356            (coding-system-for-write nnheader-file-coding-system)
1357            (file-name-coding-system nnmail-pathname-coding-system)
1358            (file (gnus-agent-lib-file "active"))
1359            oactive-min oactive-max)
1360       (gnus-make-directory (file-name-directory file))
1361       (with-temp-file file
1362         ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1363         (mm-disable-multibyte)
1364         (when (file-exists-p file)
1365           (nnheader-insert-file-contents file)
1366
1367           (goto-char (point-min))
1368           (when (re-search-forward
1369                  (concat "^" (regexp-quote group) " ") nil t)
1370             (save-excursion
1371               (setq oactive-max (read (current-buffer)) ;; max
1372                     oactive-min (read (current-buffer)))) ;; min
1373             (gnus-delete-line)))
1374         (when active
1375           (insert (format "%S %d %d y\n" (intern group)
1376                           (max (or oactive-max (cdr active)) (cdr active))
1377                           (min (or oactive-min (car active)) (car active))))
1378           (goto-char (point-max))
1379           (while (search-backward "\\." nil t)
1380             (delete-char 1)))))))
1381
1382 (defun gnus-agent-get-group-info (method group)
1383   "Get a single group's active range in the agent's copy of the server's active file."
1384   (when (gnus-agent-method-p method)
1385     (let* ((gnus-command-method (or method gnus-command-method))
1386            (coding-system-for-write nnheader-file-coding-system)
1387            (file-name-coding-system nnmail-pathname-coding-system)
1388            (file (gnus-agent-lib-file "active"))
1389            oactive-min oactive-max)
1390       (gnus-make-directory (file-name-directory file))
1391       (with-temp-buffer
1392         ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1393         (mm-disable-multibyte)
1394         (when (file-exists-p file)
1395           (nnheader-insert-file-contents file)
1396
1397           (goto-char (point-min))
1398           (when (re-search-forward
1399                  (concat "^" (regexp-quote group) " ") nil t)
1400             (save-excursion
1401               (setq oactive-max (read (current-buffer)) ;; max
1402                     oactive-min (read (current-buffer))) ;; min
1403               (cons oactive-min oactive-max))))))))
1404
1405 (defun gnus-agent-group-path (group)
1406   "Translate GROUP into a file name."
1407
1408   ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1409   ;; The two methods must be kept synchronized, which is why
1410   ;; gnus-agent-group-pathname was added.
1411
1412   (setq group
1413         (nnheader-translate-file-chars
1414          (nnheader-replace-duplicate-chars-in-string
1415           (nnheader-replace-chars-in-string
1416            (gnus-group-real-name (gnus-group-decoded-name group))
1417            ?/ ?_)
1418           ?. ?_)))
1419   (if (or nnmail-use-long-file-names
1420           (file-directory-p (expand-file-name group (gnus-agent-directory))))
1421       group
1422     (mm-encode-coding-string
1423      (nnheader-replace-chars-in-string group ?. ?/)
1424      nnmail-pathname-coding-system)))
1425
1426 (defun gnus-agent-group-pathname (group)
1427   "Translate GROUP into a file name."
1428   ;; nnagent uses nnmail-group-pathname to read articles while
1429   ;; unplugged.  The agent must, therefore, use the same directory
1430   ;; while plugged.
1431   (let ((gnus-command-method (or gnus-command-method
1432                                  (gnus-find-method-for-group group))))
1433     (nnmail-group-pathname (gnus-group-real-name
1434                             (gnus-group-decoded-name group))
1435                            (gnus-agent-directory))))
1436
1437 (defun gnus-agent-get-function (method)
1438   (if (gnus-online method)
1439       (car method)
1440     (require 'nnagent)
1441     'nnagent))
1442
1443 (defun gnus-agent-covered-methods ()
1444   "Return the subset of methods that are covered by the agent."
1445   (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1446
1447 ;;; History functions
1448
1449 (defun gnus-agent-history-buffer ()
1450   (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1451
1452 (defun gnus-agent-open-history ()
1453   (save-excursion
1454     (push (cons (gnus-agent-method)
1455                 (set-buffer (gnus-get-buffer-create
1456                              (format " *Gnus agent %s history*"
1457                                      (gnus-agent-method)))))
1458           gnus-agent-history-buffers)
1459     (mm-disable-multibyte) ;; everything is binary
1460     (erase-buffer)
1461     (insert "\n")
1462     (let ((file (gnus-agent-lib-file "history")))
1463       (when (file-exists-p file)
1464         (nnheader-insert-file-contents file))
1465       (set (make-local-variable 'gnus-agent-file-name) file))))
1466
1467 (defun gnus-agent-close-history ()
1468   (when (gnus-buffer-live-p gnus-agent-current-history)
1469     (kill-buffer gnus-agent-current-history)
1470     (setq gnus-agent-history-buffers
1471           (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1472                 gnus-agent-history-buffers))))
1473
1474 ;;;
1475 ;;; Fetching
1476 ;;;
1477
1478 (defun gnus-agent-fetch-articles (group articles)
1479   "Fetch ARTICLES from GROUP and put them into the Agent."
1480   (when articles
1481     (gnus-agent-load-alist group)
1482     (let* ((alist   gnus-agent-article-alist)
1483            (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1484            (selected-sets (list nil))
1485            (current-set-size 0)
1486            article
1487            header-number)
1488       ;; Check each article
1489       (while (setq article (pop articles))
1490         ;; Skip alist entries preceeding this article
1491         (while (> article (or (caar alist) (1+ article)))
1492           (setq alist (cdr alist)))
1493
1494         ;; Prune off articles that we have already fetched.
1495         (unless (and (eq article (caar alist))
1496                      (cdar alist))
1497           ;; Skip headers preceeding this article
1498           (while (> article
1499                     (setq header-number
1500                           (let* ((header (car headers)))
1501                             (if header
1502                                 (mail-header-number header)
1503                               (1+ article)))))
1504             (setq headers (cdr headers)))
1505
1506           ;; Add this article to the current set
1507           (setcar selected-sets (cons article (car selected-sets)))
1508
1509           ;; Update the set size, when the set is too large start a
1510           ;; new one.  I do this after adding the article as I want at
1511           ;; least one article in each set.
1512           (when (< gnus-agent-max-fetch-size
1513                    (setq current-set-size
1514                          (+ current-set-size
1515                             (if (= header-number article)
1516                                 (let ((char-size (mail-header-chars
1517                                                   (car headers))))
1518                                   (if (<= char-size 0)
1519                                       ;; The char size was missing/invalid,
1520                                       ;; assume a worst-case situation of
1521                                       ;; 65 char/line.  If the line count
1522                                       ;; is missing, arbitrarily assume a
1523                                       ;; size of 1000 characters.
1524                                     (max (* 65 (mail-header-lines
1525                                                 (car headers)))
1526                                          1000)
1527                                     char-size))
1528                               0))))
1529             (setcar selected-sets (nreverse (car selected-sets)))
1530             (setq selected-sets (cons nil selected-sets)
1531                   current-set-size 0))))
1532
1533       (when (or (cdr selected-sets) (car selected-sets))
1534         (let* ((fetched-articles (list nil))
1535                (tail-fetched-articles fetched-articles)
1536                (dir (gnus-agent-group-pathname group))
1537                (date (time-to-days (current-time)))
1538                (case-fold-search t)
1539                pos crosses id)
1540
1541           (setcar selected-sets (nreverse (car selected-sets)))
1542           (setq selected-sets (nreverse selected-sets))
1543
1544           (gnus-make-directory dir)
1545           (gnus-message 7 "Fetching articles for %s..." group)
1546
1547           (unwind-protect
1548               (while (setq articles (pop selected-sets))
1549                 ;; Fetch the articles from the backend.
1550                 (if (gnus-check-backend-function 'retrieve-articles group)
1551                     (setq pos (gnus-retrieve-articles articles group))
1552                   (with-temp-buffer
1553                     (let (article)
1554                       (while (setq article (pop articles))
1555                         (gnus-message 10 "Fetching article %s for %s..."
1556                                       article group)
1557                         (when (or
1558                                (gnus-backlog-request-article group article
1559                                                              nntp-server-buffer)
1560                                (gnus-request-article article group))
1561                           (goto-char (point-max))
1562                           (push (cons article (point)) pos)
1563                           (insert-buffer-substring nntp-server-buffer)))
1564                       (copy-to-buffer
1565                        nntp-server-buffer (point-min) (point-max))
1566                       (setq pos (nreverse pos)))))
1567                 ;; Then save these articles into the Agent.
1568                 (save-excursion
1569                   (set-buffer nntp-server-buffer)
1570                   (while pos
1571                     (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1572                     (goto-char (point-min))
1573                     (unless (eobp) ;; Don't save empty articles.
1574                       (when (search-forward "\n\n" nil t)
1575                         (when (search-backward "\nXrefs: " nil t)
1576                           ;; Handle cross posting.
1577                           (goto-char (match-end 0)) ; move to end of header name
1578                           (skip-chars-forward "^ ") ; skip server name
1579                           (skip-chars-forward " ")
1580                           (setq crosses nil)
1581                           (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1582                             (push (cons (buffer-substring (match-beginning 1)
1583                                                           (match-end 1))
1584                                         (string-to-number
1585                                          (buffer-substring (match-beginning 2)
1586                                                            (match-end 2))))
1587                                   crosses)
1588                             (goto-char (match-end 0)))
1589                           (gnus-agent-crosspost crosses (caar pos) date)))
1590                       (goto-char (point-min))
1591                       (if (not (re-search-forward
1592                                 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1593                           (setq id "No-Message-ID-in-article")
1594                         (setq id (buffer-substring
1595                                   (match-beginning 1) (match-end 1))))
1596                       (let ((coding-system-for-write
1597                              gnus-agent-file-coding-system))
1598                         (write-region (point-min) (point-max)
1599                                       (concat dir (number-to-string (caar pos)))
1600                                       nil 'silent))
1601
1602                       (gnus-agent-append-to-list
1603                        tail-fetched-articles (caar pos)))
1604                     (widen)
1605                     (setq pos (cdr pos)))))
1606
1607             (gnus-agent-save-alist group (cdr fetched-articles) date)
1608             (gnus-agent-update-files-total-fetched-for group (cdr fetched-articles))
1609
1610             (gnus-message 7 ""))
1611           (cdr fetched-articles))))))
1612
1613 (defun gnus-agent-unfetch-articles (group articles)
1614   "Delete ARTICLES that were fetched from GROUP into the agent."
1615   (when articles
1616     (gnus-agent-with-refreshed-group
1617      group
1618      (gnus-agent-load-alist group)
1619      (let* ((alist (cons nil gnus-agent-article-alist))
1620             (articles (sort articles #'<))
1621             (next-possibility alist)
1622             (delete-this (pop articles)))
1623        (while (and (cdr next-possibility) delete-this)
1624          (let ((have-this (caar (cdr next-possibility))))
1625            (cond ((< delete-this have-this)
1626                   (setq delete-this (pop articles)))
1627                  ((= delete-this have-this)
1628                   (let ((timestamp (cdar (cdr next-possibility))))
1629                     (when timestamp
1630                       (let* ((file-name (concat (gnus-agent-group-pathname group)
1631                                                 (number-to-string have-this)))
1632                              (size-file (float (or (and gnus-agent-total-fetched-hashtb
1633                                                         (nth 7 (file-attributes file-name)))
1634                                                    0))))
1635                         (delete-file file-name)
1636                         (gnus-agent-update-files-total-fetched-for group (- size-file)))))
1637
1638                   (setcdr next-possibility (cddr next-possibility)))
1639                  (t
1640                   (setq next-possibility (cdr next-possibility))))))
1641        (setq gnus-agent-article-alist (cdr alist))
1642        (gnus-agent-save-alist group)))))
1643
1644 (defun gnus-agent-crosspost (crosses article &optional date)
1645   (setq date (or date t))
1646
1647   (let (gnus-agent-article-alist group alist beg end)
1648     (save-excursion
1649       (set-buffer gnus-agent-overview-buffer)
1650       (when (nnheader-find-nov-line article)
1651         (forward-word 1)
1652         (setq beg (point))
1653         (setq end (progn (forward-line 1) (point)))))
1654     (while crosses
1655       (setq group (caar crosses))
1656       (unless (setq alist (assoc group gnus-agent-group-alist))
1657         (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1658               gnus-agent-group-alist))
1659       (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1660       (save-excursion
1661         (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1662                                                     group)))
1663         (when (= (point-max) (point-min))
1664           (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1665           (ignore-errors
1666             (nnheader-insert-file-contents
1667              (gnus-agent-article-name ".overview" group))))
1668         (nnheader-find-nov-line (string-to-number (cdar crosses)))
1669         (insert (string-to-number (cdar crosses)))
1670         (insert-buffer-substring gnus-agent-overview-buffer beg end)
1671         (gnus-agent-check-overview-buffer))
1672       (setq crosses (cdr crosses)))))
1673
1674 (defun gnus-agent-backup-overview-buffer ()
1675   (when gnus-newsgroup-name
1676     (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1677           (cnt 0)
1678           name)
1679       (while (file-exists-p
1680               (setq name (concat root "~"
1681                                  (int-to-string (setq cnt (1+ cnt))) "~"))))
1682       (write-region (point-min) (point-max) name nil 'no-msg)
1683       (gnus-message 1 "Created backup copy of overview in %s." name)))
1684   t)
1685
1686 (defun gnus-agent-check-overview-buffer (&optional buffer)
1687   "Check the overview file given for sanity.
1688 In particular, checks that the file is sorted by article number
1689 and that there are no duplicates."
1690   (let ((prev-num -1)
1691         (backed-up nil))
1692     (save-excursion
1693       (when buffer
1694         (set-buffer buffer))
1695       (save-restriction
1696         (widen)
1697         (goto-char (point-min))
1698
1699         (while (< (point) (point-max))
1700           (let ((p (point))
1701                 (cur (condition-case nil
1702                          (read (current-buffer))
1703                        (error nil))))
1704             (cond
1705              ((or (not (integerp cur))
1706                   (not (eq (char-after) ?\t)))
1707               (or backed-up
1708                   (setq backed-up (gnus-agent-backup-overview-buffer)))
1709               (gnus-message 1
1710                             "Overview buffer contains garbage '%s'."
1711                             (buffer-substring
1712                              p (point-at-eol))))
1713              ((= cur prev-num)
1714               (or backed-up
1715                   (setq backed-up (gnus-agent-backup-overview-buffer)))
1716               (gnus-message 1
1717                             "Duplicate overview line for %d" cur)
1718               (delete-region p (progn (forward-line 1) (point))))
1719              ((< cur prev-num)
1720               (or backed-up
1721                   (setq backed-up (gnus-agent-backup-overview-buffer)))
1722               (gnus-message 1 "Overview buffer not sorted!")
1723               (sort-numeric-fields 1 (point-min) (point-max))
1724               (goto-char (point-min))
1725               (setq prev-num -1))
1726              (t
1727               (setq prev-num cur)))
1728             (forward-line 1)))))))
1729
1730 (defun gnus-agent-flush-cache ()
1731   (save-excursion
1732     (while gnus-agent-buffer-alist
1733       (set-buffer (cdar gnus-agent-buffer-alist))
1734       (let ((coding-system-for-write
1735              gnus-agent-file-coding-system))
1736         (write-region (point-min) (point-max)
1737                       (gnus-agent-article-name ".overview"
1738                                                (caar gnus-agent-buffer-alist))
1739                       nil 'silent))
1740       (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1741     (while gnus-agent-group-alist
1742       (with-temp-file (gnus-agent-article-name
1743                        ".agentview" (caar gnus-agent-group-alist))
1744         (princ (cdar gnus-agent-group-alist))
1745         (insert "\n")
1746         (princ 1 (current-buffer))
1747         (insert "\n"))
1748       (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1749
1750 ;;;###autoload
1751 (defun gnus-agent-find-parameter (group symbol)
1752   "Search for GROUPs SYMBOL in the group's parameters, the group's
1753 topic parameters, the group's category, or the customizable
1754 variables.  Returns the first non-nil value found."
1755   (or (gnus-group-find-parameter group symbol t)
1756       (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1757       (symbol-value
1758        (cdr
1759         (assq symbol
1760               '((agent-short-article . gnus-agent-short-article)
1761                 (agent-long-article . gnus-agent-long-article)
1762                 (agent-low-score . gnus-agent-low-score)
1763                 (agent-high-score . gnus-agent-high-score)
1764                 (agent-days-until-old . gnus-agent-expire-days)
1765                 (agent-enable-expiration
1766             &nb