Quassia Gnus v0.13.
[elisp/gnus.git-] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
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., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-cache)
30 (require 'nnvirtual)
31 (require 'gnus-sum)
32 (eval-when-compile (require 'cl))
33
34 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
35   "Where the Gnus agent will store its files."
36   :group 'gnus-agent
37   :type 'directory)
38
39 (defcustom gnus-agent-plugged-hook nil
40   "Hook run when plugging into the network."
41   :group 'gnus-agent
42   :type 'hook)
43
44 (defcustom gnus-agent-unplugged-hook nil
45   "Hook run when unplugging from the network."
46   :group 'gnus-agent
47   :type 'hook)
48
49 ;;; Internal variables
50
51 (defvar gnus-agent-history-buffers nil)
52 (defvar gnus-agent-buffer-alist nil)
53 (defvar gnus-agent-article-alist nil)
54 (defvar gnus-agent-group-alist nil)
55 (defvar gnus-agent-covered-methods nil)
56 (defvar gnus-category-alist nil)
57 (defvar gnus-agent-current-history nil)
58 (defvar gnus-agent-overview-buffer nil)
59 (defvar gnus-category-predicate-cache nil)
60 (defvar gnus-category-group-cache nil)
61 (defvar gnus-agent-spam-hashtb nil)
62 (defvar gnus-agent-file-name nil)
63 (defvar gnus-agent-send-mail-function nil)
64 (defvar gnus-agent-article-file-coding-system 'no-conversion)
65
66 ;; Dynamic variables
67 (defvar gnus-headers)
68 (defvar gnus-score)
69
70 ;;;
71 ;;; Setup
72 ;;;
73
74 (defun gnus-open-agent ()
75   (setq gnus-agent t)
76   (gnus-agent-read-servers)
77   (gnus-category-read)
78   (setq gnus-agent-overview-buffer
79         (get-buffer-create " *Gnus agent overview*"))
80   (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
81   (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
82   (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
83
84 (gnus-add-shutdown 'gnus-close-agent 'gnus)
85
86 (defun gnus-close-agent ()
87   (setq gnus-agent-covered-methods nil
88         gnus-category-predicate-cache nil
89         gnus-category-group-cache nil
90         gnus-agent-spam-hashtb nil)
91   (gnus-kill-buffer gnus-agent-overview-buffer))
92
93 ;;;
94 ;;; Utility functions
95 ;;;
96
97 (defun gnus-agent-read-file (file)
98   "Load FILE and do a `read' there."
99   (nnheader-temp-write nil
100     (ignore-errors
101       (insert-file-contents file)
102       (goto-char (point-min))
103       (read (current-buffer)))))
104
105 (defsubst gnus-agent-method ()
106   (concat (symbol-name (car gnus-command-method)) "/"
107           (if (equal (cadr gnus-command-method) "")
108               "unnamed"
109             (cadr gnus-command-method))))
110
111 (defsubst gnus-agent-directory ()
112   "Path of the Gnus agent directory."
113   (nnheader-concat gnus-agent-directory (gnus-agent-method) "/"))
114
115 (defun gnus-agent-lib-file (file)
116   "The full path of the Gnus agent library FILE."
117   (concat (gnus-agent-directory) "agent.lib/" file))
118
119 ;;; Fetching setup functions.
120
121 (defun gnus-agent-start-fetch ()
122   "Initialize data structures for efficient fetching."
123   (gnus-agent-open-history)
124   (setq gnus-agent-current-history (gnus-agent-history-buffer)))
125
126 (defun gnus-agent-stop-fetch ()
127   "Save all data structures and clean up."
128   (gnus-agent-save-history)
129   (gnus-agent-close-history)
130   (setq gnus-agent-spam-hashtb nil)
131   (save-excursion
132     (set-buffer nntp-server-buffer)
133     (widen)))
134
135 (defmacro gnus-agent-with-fetch (&rest forms)
136   "Do FORMS safely."
137   `(unwind-protect
138        (progn
139          (gnus-agent-start-fetch)
140          ,@forms)
141      (gnus-agent-stop-fetch)))
142
143 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
144 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
145
146 ;;;
147 ;;; Mode infestation
148 ;;;
149
150 (defvar gnus-agent-mode-hook nil
151   "Hook run when installing agent mode.")
152
153 (defvar gnus-agent-mode nil)
154 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
155
156 (defun gnus-agent-mode ()
157   "Minor mode for providing a agent support in Gnus buffers."
158   (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
159                                       (symbol-name major-mode))
160                         (match-string 1 (symbol-name major-mode))))
161          (mode (intern (format "gnus-agent-%s-mode" buffer))))
162     (set (make-local-variable 'gnus-agent-mode) t)
163     (set mode nil)
164     (set (make-local-variable mode) t)
165     ;; Set up the menu.
166     (when (gnus-visual-p 'agent-menu 'menu)
167       (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
168     (unless (assq 'gnus-agent-mode minor-mode-alist)
169       (push gnus-agent-mode-status minor-mode-alist))
170     (unless (assq mode minor-mode-map-alist)
171       (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
172                                                      buffer))))
173             minor-mode-map-alist))
174     (gnus-agent-toggle-plugged gnus-plugged)
175     (run-hooks 'gnus-agent-mode-hook)))
176
177 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
178 (gnus-define-keys gnus-agent-group-mode-map
179   "Ju" gnus-agent-fetch-group
180   "Jc" gnus-enter-category-buffer
181   "Jj" gnus-agent-toggle-plugged
182   "Js" gnus-agent-fetch-session
183   "JS" gnus-group-send-drafts
184   "Ja" gnus-agent-add-group)
185
186 (defun gnus-agent-group-make-menu-bar ()
187   (unless (boundp 'gnus-agent-group-menu)
188     (easy-menu-define
189      gnus-agent-group-menu gnus-agent-group-mode-map ""
190      '("Agent"
191        ["Toggle plugged" gnus-agent-toggle-plugged t]
192        ["List categories" gnus-enter-category-buffer t]
193        ["Send drafts" gnus-group-send-drafts gnus-plugged]
194        ("Fetch"
195         ["All" gnus-agent-fetch-session gnus-plugged]
196         ["Group" gnus-agent-fetch-group gnus-plugged])))))
197
198 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
199 (gnus-define-keys gnus-agent-summary-mode-map
200   "Jj" gnus-agent-toggle-plugged
201   "J#" gnus-agent-mark-article
202   "J\M-#" gnus-agent-unmark-article
203   "@" gnus-agent-toggle-mark
204   "Jc" gnus-agent-catchup)
205
206 (defun gnus-agent-summary-make-menu-bar ()
207   (unless (boundp 'gnus-agent-summary-menu)
208     (easy-menu-define
209      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
210      '("Agent"
211        ["Toggle plugged" gnus-agent-toggle-plugged t]
212        ["Mark as downloadable" gnus-agent-mark-article t]
213        ["Unmark as downloadable" gnus-agent-unmark-article t]
214        ["Toggle mark" gnus-agent-toggle-mark t]
215        ["Catchup undownloaded" gnus-agent-catchup t]))))
216
217 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
218 (gnus-define-keys gnus-agent-server-mode-map
219   "Jj" gnus-agent-toggle-plugged
220   "Ja" gnus-agent-add-server
221   "Jr" gnus-agent-remove-server)
222
223 (defun gnus-agent-server-make-menu-bar ()
224   (unless (boundp 'gnus-agent-server-menu)
225     (easy-menu-define
226      gnus-agent-server-menu gnus-agent-server-mode-map ""
227      '("Agent"
228        ["Toggle plugged" gnus-agent-toggle-plugged t]
229        ["Add" gnus-agent-add-server t]
230        ["Remove" gnus-agent-remove-server t]))))
231
232 (defun gnus-agent-toggle-plugged (plugged)
233   "Toggle whether Gnus is unplugged or not."
234   (interactive (list (not gnus-plugged)))
235   (if plugged
236       (progn
237         (run-hooks 'gnus-agent-plugged-hook)
238         (setcar (cdr gnus-agent-mode-status) " Plugged"))
239     (gnus-agent-close-connections)
240     (run-hooks 'gnus-agent-unplugged-hook)
241     (setcar (cdr gnus-agent-mode-status) " Unplugged"))
242   (setq gnus-plugged plugged)
243   (set-buffer-modified-p t))
244
245 (defun gnus-agent-close-connections ()
246   "Close all methods covered by the Gnus agent."
247   (let ((methods gnus-agent-covered-methods))
248     (while methods
249       (gnus-close-server (pop methods)))))
250
251 ;;;###autoload
252 (defun gnus-unplugged ()
253   "Start Gnus unplugged."
254   (interactive)
255   (setq gnus-plugged nil)
256   (gnus))
257
258 ;;;###autoload
259 (defun gnus-agentize ()
260   "Allow Gnus to be an offline newsreader.
261 The normal usage of this command is to put the following as the
262 last form in your `.gnus.el' file:
263
264 \(gnus-agentize)
265
266 This will modify the `gnus-before-startup-hook', `gnus-post-method',
267 and `message-send-mail-function' variables, and install the Gnus
268 agent minor mode in all Gnus buffers."
269   (interactive)
270   (gnus-open-agent)
271   (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
272   (unless gnus-agent-send-mail-function 
273     (setq gnus-agent-send-mail-function message-send-mail-function
274           message-send-mail-function 'gnus-agent-send-mail))
275   (unless gnus-agent-covered-methods
276     (setq gnus-agent-covered-methods (list gnus-select-method))))
277
278 (defun gnus-agent-queue-setup ()
279   "Make sure the queue group exists."
280   (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
281     (gnus-request-create-group "queue" '(nndraft ""))
282     (let ((gnus-level-default-subscribed 1))
283       (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
284     (gnus-group-set-parameter
285      "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
286
287 (defun gnus-agent-send-mail ()
288   (if gnus-plugged
289       (funcall gnus-agent-send-mail-function)
290     (goto-char (point-min))
291     (re-search-forward
292      (concat "^" (regexp-quote mail-header-separator) "\n"))
293     (replace-match "\n")
294     (gnus-request-accept-article "nndraft:queue")))
295
296 ;;;
297 ;;; Group mode commands
298 ;;;
299
300 (defun gnus-agent-fetch-group (group)
301   "Put all new articles in GROUP into the agent."
302   (interactive (list (gnus-group-group-name)))
303   (unless group
304     (error "No group on the current line"))
305   (let ((gnus-command-method (gnus-find-method-for-group group)))
306     (gnus-agent-with-fetch
307       (gnus-agent-fetch-group-1 group gnus-command-method)
308       (gnus-message 5 "Fetching %s...done" group))))
309
310 (defun gnus-agent-add-group (category arg)
311   "Add the current group to an agent category."
312   (interactive
313    (list
314     (intern
315      (completing-read
316       "Add to category: "
317       (mapcar (lambda (cat) (list (symbol-name (car cat))))
318               gnus-category-alist)
319       nil t))
320     current-prefix-arg))
321   (let ((cat (assq category gnus-category-alist))
322         c groups)
323     (gnus-group-iterate arg
324       (lambda (group)
325         (when (cadddr (setq c (gnus-group-category group)))
326           (setf (cadddr c) (delete group (cadddr c))))
327         (push group groups)))
328     (setf (cadddr cat) (nconc (cadddr cat) groups))
329     (gnus-category-write)))
330
331 ;;;
332 ;;; Server mode commands
333 ;;;
334
335 (defun gnus-agent-add-server (server)
336   "Enroll SERVER in the agent program."
337   (interactive (list (gnus-server-server-name)))
338   (unless server
339     (error "No server on the current line"))
340   (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
341     (when (member method gnus-agent-covered-methods)
342       (error "Server already in the agent program"))
343     (push method gnus-agent-covered-methods)
344     (gnus-agent-write-servers)
345     (message "Entered %s into the agent" server)))
346
347 (defun gnus-agent-remove-server (server)
348   "Remove SERVER from the agent program."
349   (interactive (list (gnus-server-server-name)))
350   (unless server
351     (error "No server on the current line"))
352   (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
353     (unless (member method gnus-agent-covered-methods)
354       (error "Server not in the agent program"))
355     (setq gnus-agent-covered-methods
356           (delete method gnus-agent-covered-methods))
357     (gnus-agent-write-servers)
358     (message "Removed %s from the agent" server)))
359
360 (defun gnus-agent-read-servers ()
361   "Read the alist of covered servers."
362   (setq gnus-agent-covered-methods
363         (gnus-agent-read-file
364          (nnheader-concat gnus-agent-directory "lib/servers"))))
365
366 (defun gnus-agent-write-servers ()
367   "Write the alist of covered servers."
368   (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
369     (prin1 gnus-agent-covered-methods (current-buffer))))
370
371 ;;;
372 ;;; Summary commands
373 ;;;
374
375 (defun gnus-agent-mark-article (n &optional unmark)
376   "Mark the next N articles as downloadable.
377 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
378 the mark instead.  The difference between N and the actual number of
379 articles marked is returned."
380   (interactive "p")
381   (gnus-set-global-variables)
382   (let ((backward (< n 0))
383         (n (abs n)))
384     (while (and
385             (> n 0)
386             (progn
387               (gnus-summary-set-agent-mark
388                (gnus-summary-article-number) unmark)
389               (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
390       (setq n (1- n)))
391     (when (/= 0 n)
392       (gnus-message 7 "No more articles"))
393     (gnus-summary-recenter)
394     (gnus-summary-position-point)
395     n))
396
397 (defun gnus-agent-unmark-article (n)
398   "Remove the downloadable mark from the next N articles.
399 If N is negative, unmark backward instead.  The difference between N and
400 the actual number of articles unmarked is returned."
401   (interactive "p")
402   (gnus-set-global-variables)
403   (gnus-agent-mark-article n t))
404
405 (defun gnus-agent-toggle-mark (n)
406   "Toggle the downloadable mark from the next N articles.
407 If N is negative, toggle backward instead.  The difference between N and
408 the actual number of articles toggled is returned."
409   (interactive "p")
410   (gnus-set-global-variables)
411   (gnus-agent-mark-article n 'toggle))
412
413 (defun gnus-summary-set-agent-mark (article &optional unmark)
414   "Mark ARTICLE as downloadable."
415   (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
416                     (memq article gnus-newsgroup-downloadable)
417                   unmark)))
418     (setq gnus-newsgroup-downloadable
419           (delq article gnus-newsgroup-downloadable))
420     (unless unmark
421       (push article gnus-newsgroup-downloadable))
422     (gnus-summary-update-mark
423      (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
424      'unread)))
425
426 (defun gnus-agent-get-undownloaded-list ()
427   "Mark all unfetched articles as read."
428   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
429     (when (and (not gnus-plugged)
430                (gnus-agent-method-p gnus-command-method))
431       (gnus-agent-load-alist gnus-newsgroup-name)
432       (let ((articles gnus-newsgroup-unreads)
433             article)
434         (while (setq article (pop articles))
435           (unless (or (cdr (assq article gnus-agent-article-alist))
436                   (memq article gnus-newsgroup-downloadable))
437             (push article gnus-newsgroup-undownloaded)))))))
438
439 (defun gnus-agent-catchup ()
440   "Mark all undownloaded articles as read."
441   (interactive)
442   (save-excursion
443     (while gnus-newsgroup-undownloaded
444       (gnus-summary-mark-article
445        (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
446   (gnus-summary-position-point))
447
448 ;;;
449 ;;; Internal functions
450 ;;;
451
452 (defun gnus-agent-save-active (method)
453   (when (gnus-agent-method-p method)
454     (let* ((gnus-command-method method)
455            (file (gnus-agent-lib-file "active")))
456       (gnus-make-directory (file-name-directory file))
457       (let ((coding-system-for-write gnus-agent-article-file-coding-system))
458         (write-region (point-min) (point-max) file nil 'silent))
459       (when (file-exists-p (gnus-agent-lib-file "groups"))
460         (delete-file (gnus-agent-lib-file "groups"))))))
461
462 (defun gnus-agent-save-groups (method)
463   (let* ((gnus-command-method method)
464          (file (gnus-agent-lib-file "groups")))
465     (gnus-make-directory (file-name-directory file))
466     (write-region (point-min) (point-max) file nil 'silent))
467     (when (file-exists-p (gnus-agent-lib-file "active"))
468       (delete-file (gnus-agent-lib-file "active"))))
469
470 (defun gnus-agent-group-path (group)
471   "Translate GROUP into a path."
472   (nnheader-translate-file-chars
473    (nnheader-replace-chars-in-string group ?. ?/)))
474
475 \f
476
477 (defun gnus-agent-method-p (method)
478   "Say whether METHOD is covered by the agent."
479   (member method gnus-agent-covered-methods))
480
481 (defun gnus-agent-get-function (method)
482   (if (and (not gnus-plugged)
483            (gnus-agent-method-p method))
484       (progn
485         (require 'nnagent)
486         'nnagent)
487     (car method)))
488
489 ;;; History functions
490
491 (defun gnus-agent-history-buffer ()
492   (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
493
494 (defun gnus-agent-open-history ()
495   (save-excursion
496     (push (cons (gnus-agent-method)
497                 (set-buffer (get-buffer-create
498                              (format " *Gnus agent %s history*"
499                                      (gnus-agent-method)))))
500           gnus-agent-history-buffers)
501     (erase-buffer)
502     (insert "\n")
503     (let ((file (gnus-agent-lib-file "history")))
504       (when (file-exists-p file)
505         (insert-file file))
506       (set (make-local-variable 'gnus-agent-file-name) file))))
507
508 (defun gnus-agent-save-history ()
509   (save-excursion
510     (set-buffer gnus-agent-current-history)
511     (gnus-make-directory (file-name-directory gnus-agent-file-name))
512     (write-region (1+ (point-min)) (point-max)
513                   gnus-agent-file-name nil 'silent)))
514
515 (defun gnus-agent-close-history ()
516   (when (gnus-buffer-live-p gnus-agent-current-history)
517     (kill-buffer gnus-agent-current-history)
518     (setq gnus-agent-history-buffers
519           (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
520                 gnus-agent-history-buffers))))
521
522 (defun gnus-agent-enter-history (id group-arts date)
523   (save-excursion
524     (set-buffer gnus-agent-current-history)
525     (goto-char (point-max))
526     (insert id "\t" (number-to-string date) "\t")
527     (while group-arts
528       (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
529               " "))
530     (insert "\n")))
531
532 (defun gnus-agent-article-in-history-p (id)
533   (save-excursion
534     (set-buffer (gnus-agent-history-buffer))
535     (goto-char (point-min))
536     (search-forward (concat "\n" id "\t") nil t)))
537
538 (defun gnus-agent-history-path (id)
539   (save-excursion
540     (set-buffer (gnus-agent-history-buffer))
541     (goto-char (point-min))
542     (when (search-forward (concat "\n" id "\t") nil t)
543       (let ((method (gnus-agent-method)))
544         (let (paths group)
545           (while (not (numberp (setq group (read (current-buffer)))))
546             (push (concat method "/" group) paths))
547           (nreverse paths))))))
548
549 ;;;
550 ;;; Fetching
551 ;;;
552
553 (defun gnus-agent-fetch-articles (group articles)
554   "Fetch ARTICLES from GROUP and put them into the agent."
555   (when articles
556     ;; Prune off articles that we have already fetched.
557     (while (and articles
558                 (cdr (assq (car articles) gnus-agent-article-alist)))
559       (pop articles))
560     (let ((arts articles))
561       (while (cdr arts)
562         (if (cdr (assq (cadr arts) gnus-agent-article-alist))
563             (setcdr arts (cddr arts))
564           (setq arts (cdr arts)))))
565     (when articles
566       (let ((dir (concat
567                   (gnus-agent-directory)
568                   (gnus-agent-group-path group) "/"))
569             (date (gnus-time-to-day (current-time)))
570             (case-fold-search t)
571             pos alists crosses id elem)
572         (gnus-make-directory dir)
573         (gnus-message 7 "Fetching articles for %s..." group)
574         ;; Fetch the articles from the backend.
575         (if (gnus-check-backend-function 'retrieve-articles group)
576             (setq pos (gnus-retrieve-articles articles group))
577           (nnheader-temp-write nil
578             (let ((buf (current-buffer))
579                   article)
580               (while (setq article (pop articles))
581                 (when (gnus-request-article article group)
582                   (goto-char (point-max))
583                   (push (cons article (point)) pos)
584                   (insert-buffer-substring nntp-server-buffer)))
585               (copy-to-buffer nntp-server-buffer (point-min) (point-max))
586               (setq pos (nreverse pos)))))
587         ;; Then save these articles into the agent.
588         (save-excursion
589           (set-buffer nntp-server-buffer)
590           (while pos
591             (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
592             (goto-char (point-min))
593             (when (search-forward "\n\n" nil t)
594               (when (search-backward "\nXrefs: " nil t)
595                 ;; Handle crossposting.
596                 (skip-chars-forward "^ ")
597                 (skip-chars-forward " ")
598                 (setq crosses nil)
599                 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
600                   (push (cons (buffer-substring (match-beginning 1)
601                                                 (match-end 1))
602                               (buffer-substring (match-beginning 2)
603                                                 (match-end 2)))
604                         crosses)
605                   (goto-char (match-end 0)))
606                 (gnus-agent-crosspost crosses (caar pos))))
607             (goto-char (point-min))
608             (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
609                 (setq id "No-Message-ID-in-article")
610               (setq id (buffer-substring (match-beginning 1) (match-end 1))))
611             (write-region (point-min) (point-max)
612                           (concat dir (number-to-string (caar pos)))
613                           nil 'silent)
614             (when (setq elem (assq (caar pos) gnus-agent-article-alist))
615               (setcdr elem t))
616             (gnus-agent-enter-history
617              id (or crosses (list (cons group (caar pos)))) date)
618             (widen)
619             (pop pos)))
620         (gnus-agent-save-alist group)))))
621
622 (defun gnus-agent-crosspost (crosses article)
623   (let (gnus-agent-article-alist group alist beg end)
624     (save-excursion
625       (set-buffer gnus-agent-overview-buffer)
626       (when (nnheader-find-nov-line article)
627         (forward-word 1)
628         (setq beg (point))
629         (setq end (progn (forward-line 1) (point)))))
630     (while crosses
631       (setq group (caar crosses))
632       (unless (setq alist (assoc group gnus-agent-group-alist))
633         (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
634               gnus-agent-group-alist))
635       (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
636       (save-excursion
637         (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
638                                                group)))
639         (when (= (point-max) (point-min))
640           (push (cons group (current-buffer)) gnus-agent-buffer-alist)
641           (ignore-errors
642             (insert-file-contents
643              (gnus-agent-article-name ".overview" group))))
644         (nnheader-find-nov-line (string-to-number (cdar crosses)))
645         (insert (string-to-number (cdar crosses)))
646         (insert-buffer-substring gnus-agent-overview-buffer beg end))
647       (pop crosses))))
648
649 (defun gnus-agent-flush-cache ()
650   (save-excursion
651     (while gnus-agent-buffer-alist
652       (set-buffer (cdar gnus-agent-buffer-alist))
653       (write-region (point-min) (point-max)
654                     (gnus-agent-article-name ".overview"
655                                              (caar gnus-agent-buffer-alist))
656                      nil 'silent)
657       (pop gnus-agent-buffer-alist))
658     (while gnus-agent-group-alist
659       (nnheader-temp-write (caar gnus-agent-group-alist)
660         (princ (cdar gnus-agent-group-alist))
661         (insert "\n"))
662       (pop gnus-agent-group-alist))))
663
664 (defun gnus-agent-fetch-headers (group articles &optional force)
665   (gnus-agent-load-alist group)
666   ;; Find out what headers we need to retrieve.
667   (when articles
668     (while (and articles
669                 (assq (car articles) gnus-agent-article-alist))
670       (pop articles))
671     (let ((arts articles))
672       (while (cdr arts)
673         (if (assq (cadr arts) gnus-agent-article-alist)
674             (setcdr arts (cddr arts))
675           (setq arts (cdr arts)))))
676     ;; Fetch them.
677     (when articles
678       (gnus-message 7 "Fetching headers for %s..." group)
679       (save-excursion
680         (set-buffer nntp-server-buffer)
681         (unless (eq 'nov (gnus-retrieve-headers articles group))
682           (nnvirtual-convert-headers))
683         ;; Save these headers for later processing.
684         (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
685         (let (file)
686           (when (file-exists-p
687                  (setq file (gnus-agent-article-name ".overview" group)))
688             (gnus-agent-braid-nov group articles file))
689           (gnus-make-directory (file-name-directory file))
690           (write-region (point-min) (point-max) file nil 'silent)
691           (gnus-agent-save-alist group articles nil))
692         t))))
693
694 (defsubst gnus-agent-copy-nov-line (article)
695   (let (b e)
696     (set-buffer gnus-agent-overview-buffer)
697     (setq b (point))
698     (if (eq article (read (current-buffer)))
699         (setq e (progn (forward-line 1) (point)))
700       (setq e b))
701     (set-buffer nntp-server-buffer)
702     (insert-buffer-substring gnus-agent-overview-buffer b e)))
703
704 (defun gnus-agent-braid-nov (group articles file)
705   (let (beg end)
706     (set-buffer gnus-agent-overview-buffer)
707     (goto-char (point-min))
708     (set-buffer nntp-server-buffer)
709     (erase-buffer)
710     (insert-file-contents file)
711     (goto-char (point-min))
712     (if (or (= (point-min) (point-max))
713             (progn
714               (forward-line -1)
715               (< (read (current-buffer)) (car articles))))
716         ;; We have only headers that are after the older headers,
717         ;; so we just append them.
718         (progn
719           (goto-char (point-max))
720           (insert-buffer-substring gnus-agent-overview-buffer))
721       ;; We do it the hard way.
722       (nnheader-find-nov-line (car articles))
723       (gnus-agent-copy-nov-line (car articles))
724       (pop articles)
725       (while (and articles
726                   (not (eobp)))
727         (while (and (not (eobp))
728                     (< (read (current-buffer)) (car articles)))
729           (forward-line 1))
730         (beginning-of-line)
731         (unless (eobp)
732           (gnus-agent-copy-nov-line (car articles))
733           (setq articles (cdr articles))))
734       (when articles
735         (let (b e)
736           (set-buffer gnus-agent-overview-buffer)
737           (setq b (point)
738                 e (point-max))
739           (set-buffer nntp-server-buffer)
740           (insert-buffer-substring gnus-agent-overview-buffer b e))))))
741
742 (defun gnus-agent-load-alist (group &optional dir)
743   "Load the article-state alist for GROUP."
744   (setq gnus-agent-article-alist
745         (gnus-agent-read-file
746          (if dir
747              (concat dir ".agentview")
748            (gnus-agent-article-name ".agentview" group)))))
749
750 (defun gnus-agent-save-alist (group &optional articles state dir)
751   "Load the article-state alist for GROUP."
752   (nnheader-temp-write (if dir
753                            (concat dir ".agentview")
754                          (gnus-agent-article-name ".agentview" group))
755     (princ (setq gnus-agent-article-alist
756                  (nconc gnus-agent-article-alist
757                         (mapcar (lambda (article) (cons article state))
758                                 articles)))
759            (current-buffer))
760     (insert "\n")))
761
762 (defun gnus-agent-article-name (article group)
763   (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
764           (if (stringp article) article (string-to-number article))))
765
766 ;;;###autoload
767 (defun gnus-agent-batch-fetch ()
768   "Start Gnus and fetch session."
769   (interactive)
770   (gnus)
771   (gnus-agent-fetch-session)
772   (gnus-group-exit))
773
774 (defun gnus-agent-fetch-session ()
775   "Fetch all articles and headers that are eligible for fetching."
776   (interactive)
777   (unless gnus-agent-covered-methods
778     (error "No servers are covered by the Gnus agent"))
779   (unless gnus-plugged
780     (error "Can't fetch articles while Gnus is unplugged"))
781   (let ((methods gnus-agent-covered-methods)
782         groups group gnus-command-method)
783     (save-excursion
784       (while methods
785         (setq gnus-command-method (car methods)
786               groups (gnus-groups-from-server (pop methods)))
787         (gnus-agent-with-fetch
788           (while (setq group (pop groups))
789             (gnus-agent-fetch-group-1 group gnus-command-method))))
790       (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
791
792 (defun gnus-agent-fetch-group-1 (group method)
793   "Fetch GROUP."
794   (let ((gnus-command-method method)
795         gnus-newsgroup-dependencies gnus-newsgroup-headers
796         gnus-newsgroup-scored gnus-headers gnus-score
797         gnus-use-cache articles score arts
798         category predicate info marks score-param)
799     ;; Fetch headers.
800     (when (and (setq articles (gnus-list-of-unread-articles group))
801                (gnus-agent-fetch-headers group articles))
802       ;; Parse them and see which articles we want to fetch.
803       (setq gnus-newsgroup-dependencies
804             (make-vector (length articles) 0))
805       (setq gnus-newsgroup-headers
806             (gnus-get-newsgroup-headers-xover articles nil nil group))
807       (setq category (gnus-group-category group))
808       (setq predicate
809             (gnus-get-predicate 
810              (or (gnus-group-get-parameter group 'agent-predicate)
811                  (cadr category))))
812       (setq score-param
813             (or (gnus-group-get-parameter group 'agent-score)
814                 (caddr category)))
815       (when score-param
816         (gnus-score-headers (list (list score-param))))
817       (setq arts nil)
818       (while (setq gnus-headers (pop gnus-newsgroup-headers))
819         (setq gnus-score
820               (or (cdr (assq (mail-header-number gnus-headers)
821                              gnus-newsgroup-scored))
822                   gnus-summary-default-score))
823         (when (funcall predicate)
824           (push (mail-header-number gnus-headers)
825                 arts)))
826       ;; Fetch the articles.
827       (when arts
828         (gnus-agent-fetch-articles group arts)))
829     ;; Perhaps we have some additional articles to fetch.
830     (setq arts (assq 'download (gnus-info-marks
831                                 (setq info (gnus-get-info group)))))
832     (when (cdr arts)
833       (gnus-agent-fetch-articles
834        group (gnus-uncompress-range (cdr arts)))
835       (setq marks (delq arts (gnus-info-marks info)))
836       (gnus-info-set-marks info marks))))
837
838 ;;;
839 ;;; Agent Category Mode
840 ;;;
841
842 (defvar gnus-category-mode-hook nil
843   "Hook run in `gnus-category-mode' buffers.")
844
845 (defvar gnus-category-line-format "     %(%20c%): %g\n"
846   "Format of category lines.")
847
848 (defvar gnus-category-mode-line-format "Gnus: %%b"
849   "The format specification for the category mode line.")
850
851 (defvar gnus-agent-short-article 100
852   "Articles that have fewer lines than this are short.")
853
854 (defvar gnus-agent-long-article 200
855   "Articles that have more lines than this are long.")
856
857 (defvar gnus-agent-low-score 0
858   "Articles that have a score lower than this have a low score.")
859
860 (defvar gnus-agent-high-score 0
861   "Articles that have a score higher than this have a high score.")
862
863
864 ;;; Internal variables.
865
866 (defvar gnus-category-buffer "*Agent Category*")
867
868 (defvar gnus-category-line-format-alist
869   `((?c name ?s)
870     (?g groups ?d)))
871
872 (defvar gnus-category-mode-line-format-alist
873   `((?u user-defined ?s)))
874
875 (defvar gnus-category-line-format-spec nil)
876 (defvar gnus-category-mode-line-format-spec nil)
877
878 (defvar gnus-category-mode-map nil)
879 (put 'gnus-category-mode 'mode-class 'special)
880
881 (unless gnus-category-mode-map
882   (setq gnus-category-mode-map (make-sparse-keymap))
883   (suppress-keymap gnus-category-mode-map)
884
885   (gnus-define-keys gnus-category-mode-map
886     "q" gnus-category-exit
887     "k" gnus-category-kill
888     "c" gnus-category-copy
889     "a" gnus-category-add
890     "p" gnus-category-edit-predicate
891     "g" gnus-category-edit-groups
892     "s" gnus-category-edit-score
893     "l" gnus-category-list
894
895     "\C-c\C-i" gnus-info-find-node
896     "\C-c\C-b" gnus-bug))
897
898 (defvar gnus-category-menu-hook nil
899   "*Hook run after the creation of the menu.")
900
901 (defun gnus-category-make-menu-bar ()
902   (gnus-turn-off-edit-menu 'category)
903   (unless (boundp 'gnus-category-menu)
904     (easy-menu-define
905      gnus-category-menu gnus-category-mode-map ""
906      '("Categories"
907        ["Add" gnus-category-add t]
908        ["Kill" gnus-category-kill t]
909        ["Copy" gnus-category-copy t]
910        ["Edit predicate" gnus-category-edit-predicate t]
911        ["Edit score" gnus-category-edit-score t]
912        ["Edit groups" gnus-category-edit-groups t]
913        ["Exit" gnus-category-exit t]))
914
915     (run-hooks 'gnus-category-menu-hook)))
916
917 (defun gnus-category-mode ()
918   "Major mode for listing and editing agent categories.
919
920 All normal editing commands are switched off.
921 \\<gnus-category-mode-map>
922 For more in-depth information on this mode, read the manual
923 (`\\[gnus-info-find-node]').
924
925 The following commands are available:
926
927 \\{gnus-category-mode-map}"
928   (interactive)
929   (when (gnus-visual-p 'category-menu 'menu)
930     (gnus-category-make-menu-bar))
931   (kill-all-local-variables)
932   (gnus-simplify-mode-line)
933   (setq major-mode 'gnus-category-mode)
934   (setq mode-name "Category")
935   (gnus-set-default-directory)
936   (setq mode-line-process nil)
937   (use-local-map gnus-category-mode-map)
938   (buffer-disable-undo (current-buffer))
939   (setq truncate-lines t)
940   (setq buffer-read-only t)
941   (run-hooks 'gnus-category-mode-hook))
942
943 (defalias 'gnus-category-position-point 'gnus-goto-colon)
944
945 (defun gnus-category-insert-line (category)
946   (let* ((name (car category))
947          (groups (length (cadddr category))))
948     (beginning-of-line)
949     (gnus-add-text-properties
950      (point)
951      (prog1 (1+ (point))
952        ;; Insert the text.
953        (eval gnus-category-line-format-spec))
954      (list 'gnus-category name))))
955
956 (defun gnus-enter-category-buffer ()
957   "Go to the Category buffer."
958   (interactive)
959   (gnus-category-setup-buffer)
960   (gnus-configure-windows 'category)
961   (gnus-category-prepare))
962
963 (defun gnus-category-setup-buffer ()
964   (unless (get-buffer gnus-category-buffer)
965     (save-excursion
966       (set-buffer (get-buffer-create gnus-category-buffer))
967       (gnus-add-current-to-buffer-list)
968       (gnus-category-mode))))
969
970 (defun gnus-category-prepare ()
971   (gnus-set-format 'category-mode)
972   (gnus-set-format 'category t)
973   (let ((alist gnus-category-alist)
974         (buffer-read-only nil))
975     (erase-buffer)
976     (while alist
977       (gnus-category-insert-line (pop alist)))
978     (goto-char (point-min))
979     (gnus-category-position-point)))
980
981 (defun gnus-category-name ()
982   (or (get-text-property (gnus-point-at-bol) 'gnus-category)
983       (error "No category on the current line")))
984
985 (defun gnus-category-read ()
986   "Read the category alist."
987   (setq gnus-category-alist
988         (or (gnus-agent-read-file
989              (nnheader-concat gnus-agent-directory "lib/categories"))
990             (list (list 'default 'true nil nil)))))
991     
992 (defun gnus-category-write ()
993   "Write the category alist."
994   (setq gnus-category-predicate-cache nil
995         gnus-category-group-cache nil)
996   (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
997     (prin1 gnus-category-alist (current-buffer))))
998
999 (defun gnus-category-edit-predicate (category)
1000   "Edit the predicate for CATEGORY."
1001   (interactive (list (gnus-category-name)))
1002   (let ((info (assq category gnus-category-alist)))
1003     (gnus-edit-form
1004      (cadr info) (format "Editing the predicate for category %s" category)
1005      `(lambda (predicate)
1006         (setf (cadr (assq ',category gnus-category-alist)) predicate)
1007         (gnus-category-write)
1008         (gnus-category-list)))))
1009   
1010 (defun gnus-category-edit-score (category)
1011   "Edit the score expression for CATEGORY."
1012   (interactive (list (gnus-category-name)))
1013   (let ((info (assq category gnus-category-alist)))
1014     (gnus-edit-form
1015      (caddr info)
1016      (format "Editing the score expression for category %s" category)
1017      `(lambda (groups)
1018         (setf (caddr (assq ',category gnus-category-alist)) groups)
1019         (gnus-category-write)
1020         (gnus-category-list)))))
1021
1022 (defun gnus-category-edit-groups (category)
1023   "Edit the group list for CATEGORY."
1024   (interactive (list (gnus-category-name)))
1025   (let ((info (assq category gnus-category-alist)))
1026     (gnus-edit-form
1027      (cadddr info) (format "Editing the group list for category %s" category)
1028      `(lambda (groups)
1029         (setf (cadddr (assq ',category gnus-category-alist)) groups)
1030         (gnus-category-write)
1031         (gnus-category-list)))))
1032
1033 (defun gnus-category-kill (category)
1034   "Kill the current category."
1035   (interactive (list (gnus-category-name)))
1036   (let ((info (assq category gnus-category-alist))
1037         (buffer-read-only nil))
1038     (gnus-delete-line)
1039     (gnus-category-write)
1040     (setq gnus-category-alist (delq info gnus-category-alist))))
1041
1042 (defun gnus-category-copy (category to)
1043   "Copy the current category."
1044   (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1045   (let ((info (assq category gnus-category-alist)))
1046     (push (list to (gnus-copy-sequence (cadr info))
1047                 (gnus-copy-sequence (caddr info)) nil)
1048           gnus-category-alist)
1049     (gnus-category-write)
1050     (gnus-category-list)))
1051
1052 (defun gnus-category-add (category)
1053   "Create a new category."
1054   (interactive "SCategory name: ")
1055   (when (assq category gnus-category-alist)
1056     (error "Category %s already exists" category))
1057   (push (list category 'true nil nil)
1058         gnus-category-alist)
1059   (gnus-category-write)
1060   (gnus-category-list))
1061
1062 (defun gnus-category-list ()
1063   "List all categories."
1064   (interactive)
1065   (gnus-category-prepare))
1066
1067 (defun gnus-category-exit ()
1068   "Return to the group buffer."
1069   (interactive)
1070   (kill-buffer (current-buffer))
1071   (gnus-configure-windows 'group t))
1072
1073 ;; To avoid having 8-bit characters in the source file.
1074 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1075
1076 (defvar gnus-category-predicate-alist
1077   '((spam . gnus-agent-spam-p)
1078     (short . gnus-agent-short-p)
1079     (long . gnus-agent-long-p)
1080     (low . gnus-agent-low-scored-p)
1081     (high . gnus-agent-high-scored-p)
1082     (true . gnus-agent-true)
1083     (false . gnus-agent-false))
1084   "Mapping from short score predicate symbols to predicate functions.")
1085
1086 (defun gnus-agent-spam-p ()
1087   "Say whether an article is spam or not."
1088   (unless gnus-agent-spam-hashtb
1089     (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1090   (if (not (equal (mail-header-references gnus-headers) ""))
1091       nil
1092     (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1093       (prog1
1094           (gnus-gethash string gnus-agent-spam-hashtb)
1095         (gnus-sethash string t gnus-agent-spam-hashtb)))))
1096
1097 (defun gnus-agent-short-p ()
1098   "Say whether an article is short or not."
1099   (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1100
1101 (defun gnus-agent-long-p ()
1102   "Say whether an article is long or not."
1103   (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1104
1105 (defun gnus-agent-low-scored-p ()
1106   "Say whether an article has a low score or not."
1107   (< gnus-score gnus-agent-low-score))
1108
1109 (defun gnus-agent-high-scored-p ()
1110   "Say whether an article has a high score or not."
1111   (> gnus-score gnus-agent-low-score))
1112
1113 (defun gnus-category-make-function (cat)
1114   "Make a function from category CAT."
1115   `(lambda () ,(gnus-category-make-function-1 cat)))
1116
1117 (defun gnus-agent-true ()
1118   "Return t."
1119   t)
1120
1121 (defun gnus-agent-false ()
1122   "Return nil."
1123   nil)
1124   
1125 (defun gnus-category-make-function-1 (cat)
1126   "Make a function from category CAT."
1127   (cond
1128    ;; Functions are just returned as is.
1129    ((or (symbolp cat)
1130         (gnus-functionp cat))
1131     `(,(or (cdr (assq cat gnus-category-predicate-alist))
1132            cat)))
1133    ;; More complex category.
1134    ((consp cat)
1135     `(,(cond
1136         ((memq (car cat) '(& and))
1137          'and)
1138         ((memq (car cat) '(| or))
1139          'or)
1140         ((memq (car cat) gnus-category-not)
1141          'not))
1142       ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1143    (t
1144     (error "Unknown category type: %s" cat))))
1145
1146 (defun gnus-get-predicate (predicate)
1147   "Return the predicate for CATEGORY."
1148   (or (cdr (assoc predicate gnus-category-predicate-cache))
1149       (cdar (push (cons predicate
1150                         (gnus-category-make-function predicate))
1151                   gnus-category-predicate-cache))))
1152
1153 (defun gnus-group-category (group)
1154   "Return the category GROUP belongs to."
1155   (unless gnus-category-group-cache
1156     (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1157     (let ((cs gnus-category-alist)
1158           groups cat)
1159       (while (setq cat (pop cs))
1160         (setq groups (cadddr cat))
1161         (while groups
1162           (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1163   (or (gnus-gethash group gnus-category-group-cache)
1164       (assq 'default gnus-category-alist)))
1165
1166 (defun gnus-agent-expire ()
1167   "Expire all old articles."
1168   (interactive)
1169   (let ((methods gnus-agent-covered-methods)
1170         (alist (cdr gnus-newsrc-alist))
1171         gnus-command-method ofiles info method file group)
1172     (while (setq gnus-command-method (pop methods))
1173       (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1174                                   (gnus-agent-directory)))))
1175     (while (setq info (pop alist))
1176       (when (and (gnus-agent-method-p
1177                   (setq gnus-command-method
1178                         (gnus-find-method-for-group
1179                          (setq group (gnus-info-group info)))))
1180                  (member
1181                   (setq file
1182                         (concat
1183                          (gnus-agent-directory)
1184                          (gnus-agent-group-path group) "/.overview"))
1185                   ofiles))
1186         (setq ofiles (delete file ofiles))
1187         (gnus-agent-expire-group file group)))
1188     (while ofiles
1189       (gnus-agent-expire-group (pop ofiles)))))
1190
1191 (defun gnus-agent-expire-directory (dir)
1192   "Expire all groups in DIR recursively."
1193   (when (file-directory-p dir)
1194     (let ((files (directory-files dir t))
1195           file ofiles)
1196       (while (setq file (pop files))
1197         (cond
1198          ((member (file-name-nondirectory file) '("." ".."))
1199           ;; Do nothing.
1200           )
1201          ((file-directory-p file)
1202           ;; Recurse.
1203           (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1204          ((string-match "\\.overview$" file)
1205           ;; Expire group.
1206           (push file ofiles))))
1207       ofiles)))
1208
1209 (defun gnus-agent-expire-group (overview &optional group)
1210   "Expire articles in OVERVIEW."
1211   (gnus-message 5 "Expiring %s..." overview)
1212   (let ((odate (- (gnus-time-to-day (current-time)) 4))
1213         (dir (file-name-directory overview))
1214         (info (when group (gnus-get-info group)))
1215         headers article file point unreads)
1216     (gnus-agent-load-alist nil dir)
1217     (when info
1218       (setq unreads
1219             (nconc
1220              (gnus-list-of-unread-articles group)
1221              (gnus-uncompress-range
1222               (cdr (assq 'tick (gnus-info-marks info))))
1223              (gnus-uncompress-range
1224               (cdr (assq 'dormant (gnus-info-marks info)))))))
1225     (nnheader-temp-write overview
1226       (insert-file-contents overview)
1227       (goto-char (point-min))
1228       (while (not (eobp))
1229         (setq point (point))
1230         (condition-case ()
1231             (setq headers (inline (nnheader-parse-nov)))
1232           (error
1233            (goto-char point)
1234            (gnus-delete-line)
1235            (setq headers nil)))
1236         (when headers
1237           (unless (memq (setq article (mail-header-number headers)) unreads)
1238             (if (not (< (inline
1239                           (gnus-time-to-day
1240                            (inline (nnmail-date-to-time
1241                                     (mail-header-date headers)))))
1242                         odate))
1243                 (forward-line 1)              
1244               (gnus-delete-line)
1245               (setq gnus-agent-article-alist
1246                     (delq (assq article gnus-agent-article-alist)
1247                           gnus-agent-article-alist))
1248               (when (file-exists-p
1249                      (setq file (concat dir (number-to-string article))))
1250                 (delete-file file))))))
1251       (gnus-agent-save-alist nil nil nil dir))))
1252
1253 (provide 'gnus-agent)
1254
1255 ;;; gnus-agent.el ends here