Importing qgnus-0.18
[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   (let ((backward (< n 0))
382         (n (abs n)))
383     (while (and
384             (> n 0)
385             (progn
386               (gnus-summary-set-agent-mark
387                (gnus-summary-article-number) unmark)
388               (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
389       (setq n (1- n)))
390     (when (/= 0 n)
391       (gnus-message 7 "No more articles"))
392     (gnus-summary-recenter)
393     (gnus-summary-position-point)
394     n))
395
396 (defun gnus-agent-unmark-article (n)
397   "Remove the downloadable mark from the next N articles.
398 If N is negative, unmark backward instead.  The difference between N and
399 the actual number of articles unmarked is returned."
400   (interactive "p")
401   (gnus-agent-mark-article n t))
402
403 (defun gnus-agent-toggle-mark (n)
404   "Toggle the downloadable mark from the next N articles.
405 If N is negative, toggle backward instead.  The difference between N and
406 the actual number of articles toggled is returned."
407   (interactive "p")
408   (gnus-agent-mark-article n 'toggle))
409
410 (defun gnus-summary-set-agent-mark (article &optional unmark)
411   "Mark ARTICLE as downloadable."
412   (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
413                     (memq article gnus-newsgroup-downloadable)
414                   unmark)))
415     (if unmark
416         (progn
417           (setq gnus-newsgroup-downloadable
418                 (delq article gnus-newsgroup-downloadable))
419           (push article gnus-newsgroup-undownloaded))
420       (setq gnus-newsgroup-undownloaded
421             (delq article gnus-newsgroup-undownloaded))
422       (push article gnus-newsgroup-downloadable))
423     (gnus-summary-update-mark
424      (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
425      'unread)))
426
427 (defun gnus-agent-get-undownloaded-list ()
428   "Mark all unfetched articles as read."
429   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
430     (when (and (not gnus-plugged)
431                (gnus-agent-method-p gnus-command-method))
432       (gnus-agent-load-alist gnus-newsgroup-name)
433       (let ((articles gnus-newsgroup-unreads)
434             article)
435         (while (setq article (pop articles))
436           (unless (or (cdr (assq article gnus-agent-article-alist))
437                   (memq article gnus-newsgroup-downloadable))
438             (push article gnus-newsgroup-undownloaded)))))))
439
440 (defun gnus-agent-catchup ()
441   "Mark all undownloaded articles as read."
442   (interactive)
443   (save-excursion
444     (while gnus-newsgroup-undownloaded
445       (gnus-summary-mark-article
446        (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
447   (gnus-summary-position-point))
448
449 ;;;
450 ;;; Internal functions
451 ;;;
452
453 (defun gnus-agent-save-active (method)
454   (when (gnus-agent-method-p method)
455     (let* ((gnus-command-method method)
456            (file (gnus-agent-lib-file "active")))
457       (gnus-make-directory (file-name-directory file))
458       (let ((coding-system-for-write gnus-agent-article-file-coding-system))
459         (write-region (point-min) (point-max) file nil 'silent))
460       (when (file-exists-p (gnus-agent-lib-file "groups"))
461         (delete-file (gnus-agent-lib-file "groups"))))))
462
463 (defun gnus-agent-save-groups (method)
464   (let* ((gnus-command-method method)
465          (file (gnus-agent-lib-file "groups")))
466     (gnus-make-directory (file-name-directory file))
467     (write-region (point-min) (point-max) file nil 'silent))
468     (when (file-exists-p (gnus-agent-lib-file "active"))
469       (delete-file (gnus-agent-lib-file "active"))))
470
471 (defun gnus-agent-group-path (group)
472   "Translate GROUP into a path."
473   (nnheader-translate-file-chars
474    (nnheader-replace-chars-in-string group ?. ?/)))
475
476 \f
477
478 (defun gnus-agent-method-p (method)
479   "Say whether METHOD is covered by the agent."
480   (member method gnus-agent-covered-methods))
481
482 (defun gnus-agent-get-function (method)
483   (if (and (not gnus-plugged)
484            (gnus-agent-method-p method))
485       (progn
486         (require 'nnagent)
487         'nnagent)
488     (car method)))
489
490 ;;; History functions
491
492 (defun gnus-agent-history-buffer ()
493   (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
494
495 (defun gnus-agent-open-history ()
496   (save-excursion
497     (push (cons (gnus-agent-method)
498                 (set-buffer (get-buffer-create
499                              (format " *Gnus agent %s history*"
500                                      (gnus-agent-method)))))
501           gnus-agent-history-buffers)
502     (erase-buffer)
503     (insert "\n")
504     (let ((file (gnus-agent-lib-file "history")))
505       (when (file-exists-p file)
506         (insert-file file))
507       (set (make-local-variable 'gnus-agent-file-name) file))))
508
509 (defun gnus-agent-save-history ()
510   (save-excursion
511     (set-buffer gnus-agent-current-history)
512     (gnus-make-directory (file-name-directory gnus-agent-file-name))
513     (write-region (1+ (point-min)) (point-max)
514                   gnus-agent-file-name nil 'silent)))
515
516 (defun gnus-agent-close-history ()
517   (when (gnus-buffer-live-p gnus-agent-current-history)
518     (kill-buffer gnus-agent-current-history)
519     (setq gnus-agent-history-buffers
520           (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
521                 gnus-agent-history-buffers))))
522
523 (defun gnus-agent-enter-history (id group-arts date)
524   (save-excursion
525     (set-buffer gnus-agent-current-history)
526     (goto-char (point-max))
527     (insert id "\t" (number-to-string date) "\t")
528     (while group-arts
529       (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
530               " "))
531     (insert "\n")))
532
533 (defun gnus-agent-article-in-history-p (id)
534   (save-excursion
535     (set-buffer (gnus-agent-history-buffer))
536     (goto-char (point-min))
537     (search-forward (concat "\n" id "\t") nil t)))
538
539 (defun gnus-agent-history-path (id)
540   (save-excursion
541     (set-buffer (gnus-agent-history-buffer))
542     (goto-char (point-min))
543     (when (search-forward (concat "\n" id "\t") nil t)
544       (let ((method (gnus-agent-method)))
545         (let (paths group)
546           (while (not (numberp (setq group (read (current-buffer)))))
547             (push (concat method "/" group) paths))
548           (nreverse paths))))))
549
550 ;;;
551 ;;; Fetching
552 ;;;
553
554 (defun gnus-agent-fetch-articles (group articles)
555   "Fetch ARTICLES from GROUP and put them into the agent."
556   (when articles
557     ;; Prune off articles that we have already fetched.
558     (while (and articles
559                 (cdr (assq (car articles) gnus-agent-article-alist)))
560       (pop articles))
561     (let ((arts articles))
562       (while (cdr arts)
563         (if (cdr (assq (cadr arts) gnus-agent-article-alist))
564             (setcdr arts (cddr arts))
565           (setq arts (cdr arts)))))
566     (when articles
567       (let ((dir (concat
568                   (gnus-agent-directory)
569                   (gnus-agent-group-path group) "/"))
570             (date (gnus-time-to-day (current-time)))
571             (case-fold-search t)
572             pos alists crosses id elem)
573         (gnus-make-directory dir)
574         (gnus-message 7 "Fetching articles for %s..." group)
575         ;; Fetch the articles from the backend.
576         (if (gnus-check-backend-function 'retrieve-articles group)
577             (setq pos (gnus-retrieve-articles articles group))
578           (nnheader-temp-write nil
579             (let ((buf (current-buffer))
580                   article)
581               (while (setq article (pop articles))
582                 (when (gnus-request-article article group)
583                   (goto-char (point-max))
584                   (push (cons article (point)) pos)
585                   (insert-buffer-substring nntp-server-buffer)))
586               (copy-to-buffer nntp-server-buffer (point-min) (point-max))
587               (setq pos (nreverse pos)))))
588         ;; Then save these articles into the agent.
589         (save-excursion
590           (set-buffer nntp-server-buffer)
591           (while pos
592             (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
593             (goto-char (point-min))
594             (when (search-forward "\n\n" nil t)
595               (when (search-backward "\nXrefs: " nil t)
596                 ;; Handle crossposting.
597                 (skip-chars-forward "^ ")
598                 (skip-chars-forward " ")
599                 (setq crosses nil)
600                 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
601                   (push (cons (buffer-substring (match-beginning 1)
602                                                 (match-end 1))
603                               (buffer-substring (match-beginning 2)
604                                                 (match-end 2)))
605                         crosses)
606                   (goto-char (match-end 0)))
607                 (gnus-agent-crosspost crosses (caar pos))))
608             (goto-char (point-min))
609             (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
610                 (setq id "No-Message-ID-in-article")
611               (setq id (buffer-substring (match-beginning 1) (match-end 1))))
612             (let ((coding-system-for-write gnus-agent-article-file-coding-system))
613               (write-region (point-min) (point-max)
614                             (concat dir (number-to-string (caar pos)))
615                             nil 'silent))
616             (when (setq elem (assq (caar pos) gnus-agent-article-alist))
617               (setcdr elem t))
618             (gnus-agent-enter-history
619              id (or crosses (list (cons group (caar pos)))) date)
620             (widen)
621             (pop pos)))
622         (gnus-agent-save-alist group)))))
623
624 (defun gnus-agent-crosspost (crosses article)
625   (let (gnus-agent-article-alist group alist beg end)
626     (save-excursion
627       (set-buffer gnus-agent-overview-buffer)
628       (when (nnheader-find-nov-line article)
629         (forward-word 1)
630         (setq beg (point))
631         (setq end (progn (forward-line 1) (point)))))
632     (while crosses
633       (setq group (caar crosses))
634       (unless (setq alist (assoc group gnus-agent-group-alist))
635         (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
636               gnus-agent-group-alist))
637       (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
638       (save-excursion
639         (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
640                                                group)))
641         (when (= (point-max) (point-min))
642           (push (cons group (current-buffer)) gnus-agent-buffer-alist)
643           (ignore-errors
644             (insert-file-contents
645              (gnus-agent-article-name ".overview" group))))
646         (nnheader-find-nov-line (string-to-number (cdar crosses)))
647         (insert (string-to-number (cdar crosses)))
648         (insert-buffer-substring gnus-agent-overview-buffer beg end))
649       (pop crosses))))
650
651 (defun gnus-agent-flush-cache ()
652   (save-excursion
653     (while gnus-agent-buffer-alist
654       (set-buffer (cdar gnus-agent-buffer-alist))
655       (write-region (point-min) (point-max)
656                     (gnus-agent-article-name ".overview"
657                                              (caar gnus-agent-buffer-alist))
658                      nil 'silent)
659       (pop gnus-agent-buffer-alist))
660     (while gnus-agent-group-alist
661       (nnheader-temp-write (caar gnus-agent-group-alist)
662         (princ (cdar gnus-agent-group-alist))
663         (insert "\n"))
664       (pop gnus-agent-group-alist))))
665
666 (defun gnus-agent-fetch-headers (group articles &optional force)
667   (gnus-agent-load-alist group)
668   ;; Find out what headers we need to retrieve.
669   (when articles
670     (while (and articles
671                 (assq (car articles) gnus-agent-article-alist))
672       (pop articles))
673     (let ((arts articles))
674       (while (cdr arts)
675         (if (assq (cadr arts) gnus-agent-article-alist)
676             (setcdr arts (cddr arts))
677           (setq arts (cdr arts)))))
678     ;; Fetch them.
679     (when articles
680       (gnus-message 7 "Fetching headers for %s..." group)
681       (save-excursion
682         (set-buffer nntp-server-buffer)
683         (unless (eq 'nov (gnus-retrieve-headers articles group))
684           (nnvirtual-convert-headers))
685         ;; Save these headers for later processing.
686         (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
687         (let (file)
688           (when (file-exists-p
689                  (setq file (gnus-agent-article-name ".overview" group)))
690             (gnus-agent-braid-nov group articles file))
691           (gnus-make-directory (nnheader-translate-file-chars
692                                 (file-name-directory file)))
693           (write-region (point-min) (point-max) file nil 'silent)
694           (gnus-agent-save-alist group articles nil))
695         t))))
696
697 (defsubst gnus-agent-copy-nov-line (article)
698   (let (b e)
699     (set-buffer gnus-agent-overview-buffer)
700     (setq b (point))
701     (if (eq article (read (current-buffer)))
702         (setq e (progn (forward-line 1) (point)))
703       (setq e b))
704     (set-buffer nntp-server-buffer)
705     (insert-buffer-substring gnus-agent-overview-buffer b e)))
706
707 (defun gnus-agent-braid-nov (group articles file)
708   (let (beg end)
709     (set-buffer gnus-agent-overview-buffer)
710     (goto-char (point-min))
711     (set-buffer nntp-server-buffer)
712     (erase-buffer)
713     (insert-file-contents file)
714     (goto-char (point-min))
715     (if (or (= (point-min) (point-max))
716             (progn
717               (forward-line -1)
718               (< (read (current-buffer)) (car articles))))
719         ;; We have only headers that are after the older headers,
720         ;; so we just append them.
721         (progn
722           (goto-char (point-max))
723           (insert-buffer-substring gnus-agent-overview-buffer))
724       ;; We do it the hard way.
725       (nnheader-find-nov-line (car articles))
726       (gnus-agent-copy-nov-line (car articles))
727       (pop articles)
728       (while (and articles
729                   (not (eobp)))
730         (while (and (not (eobp))
731                     (< (read (current-buffer)) (car articles)))
732           (forward-line 1))
733         (beginning-of-line)
734         (unless (eobp)
735           (gnus-agent-copy-nov-line (car articles))
736           (setq articles (cdr articles))))
737       (when articles
738         (let (b e)
739           (set-buffer gnus-agent-overview-buffer)
740           (setq b (point)
741                 e (point-max))
742           (set-buffer nntp-server-buffer)
743           (insert-buffer-substring gnus-agent-overview-buffer b e))))))
744
745 (defun gnus-agent-load-alist (group &optional dir)
746   "Load the article-state alist for GROUP."
747   (setq gnus-agent-article-alist
748         (gnus-agent-read-file
749          (if dir
750              (concat dir ".agentview")
751            (gnus-agent-article-name ".agentview" group)))))
752
753 (defun gnus-agent-save-alist (group &optional articles state dir)
754   "Load the article-state alist for GROUP."
755   (nnheader-temp-write (if dir
756                            (concat dir ".agentview")
757                          (gnus-agent-article-name ".agentview" group))
758     (princ (setq gnus-agent-article-alist
759                  (nconc gnus-agent-article-alist
760                         (mapcar (lambda (article) (cons article state))
761                                 articles)))
762            (current-buffer))
763     (insert "\n")))
764
765 (defun gnus-agent-article-name (article group)
766   (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
767           (if (stringp article) article (string-to-number article))))
768
769 ;;;###autoload
770 (defun gnus-agent-batch-fetch ()
771   "Start Gnus and fetch session."
772   (interactive)
773   (gnus)
774   (gnus-agent-fetch-session)
775   (gnus-group-exit))
776
777 (defun gnus-agent-fetch-session ()
778   "Fetch all articles and headers that are eligible for fetching."
779   (interactive)
780   (unless gnus-agent-covered-methods
781     (error "No servers are covered by the Gnus agent"))
782   (unless gnus-plugged
783     (error "Can't fetch articles while Gnus is unplugged"))
784   (let ((methods gnus-agent-covered-methods)
785         groups group gnus-command-method)
786     (save-excursion
787       (while methods
788         (setq gnus-command-method (car methods)
789               groups (gnus-groups-from-server (pop methods)))
790         (gnus-agent-with-fetch
791           (while (setq group (pop groups))
792             (gnus-agent-fetch-group-1 group gnus-command-method))))
793       (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
794
795 (defun gnus-agent-fetch-group-1 (group method)
796   "Fetch GROUP."
797   (let ((gnus-command-method method)
798         gnus-newsgroup-dependencies gnus-newsgroup-headers
799         gnus-newsgroup-scored gnus-headers gnus-score
800         gnus-use-cache articles score arts
801         category predicate info marks score-param)
802     ;; Fetch headers.
803     (when (and (setq articles (gnus-list-of-unread-articles group))
804                (gnus-agent-fetch-headers group articles))
805       ;; Parse them and see which articles we want to fetch.
806       (setq gnus-newsgroup-dependencies
807             (make-vector (length articles) 0))
808       (setq gnus-newsgroup-headers
809             (gnus-get-newsgroup-headers-xover articles nil nil group))
810       (setq category (gnus-group-category group))
811       (setq predicate
812             (gnus-get-predicate 
813              (or (gnus-group-get-parameter group 'agent-predicate)
814                  (cadr category))))
815       (setq score-param
816             (or (gnus-group-get-parameter group 'agent-score)
817                 (caddr category)))
818       (when score-param
819         (gnus-score-headers (list (list score-param))))
820       (setq arts nil)
821       (while (setq gnus-headers (pop gnus-newsgroup-headers))
822         (setq gnus-score
823               (or (cdr (assq (mail-header-number gnus-headers)
824                              gnus-newsgroup-scored))
825                   gnus-summary-default-score))
826         (when (funcall predicate)
827           (push (mail-header-number gnus-headers)
828                 arts)))
829       ;; Fetch the articles.
830       (when arts
831         (gnus-agent-fetch-articles group arts)))
832     ;; Perhaps we have some additional articles to fetch.
833     (setq arts (assq 'download (gnus-info-marks
834                                 (setq info (gnus-get-info group)))))
835     (when (cdr arts)
836       (gnus-agent-fetch-articles
837        group (gnus-uncompress-range (cdr arts)))
838       (setq marks (delq arts (gnus-info-marks info)))
839       (gnus-info-set-marks info marks))))
840
841 ;;;
842 ;;; Agent Category Mode
843 ;;;
844
845 (defvar gnus-category-mode-hook nil
846   "Hook run in `gnus-category-mode' buffers.")
847
848 (defvar gnus-category-line-format "     %(%20c%): %g\n"
849   "Format of category lines.")
850
851 (defvar gnus-category-mode-line-format "Gnus: %%b"
852   "The format specification for the category mode line.")
853
854 (defvar gnus-agent-short-article 100
855   "Articles that have fewer lines than this are short.")
856
857 (defvar gnus-agent-long-article 200
858   "Articles that have more lines than this are long.")
859
860 (defvar gnus-agent-low-score 0
861   "Articles that have a score lower than this have a low score.")
862
863 (defvar gnus-agent-high-score 0
864   "Articles that have a score higher than this have a high score.")
865
866
867 ;;; Internal variables.
868
869 (defvar gnus-category-buffer "*Agent Category*")
870
871 (defvar gnus-category-line-format-alist
872   `((?c name ?s)
873     (?g groups ?d)))
874
875 (defvar gnus-category-mode-line-format-alist
876   `((?u user-defined ?s)))
877
878 (defvar gnus-category-line-format-spec nil)
879 (defvar gnus-category-mode-line-format-spec nil)
880
881 (defvar gnus-category-mode-map nil)
882 (put 'gnus-category-mode 'mode-class 'special)
883
884 (unless gnus-category-mode-map
885   (setq gnus-category-mode-map (make-sparse-keymap))
886   (suppress-keymap gnus-category-mode-map)
887
888   (gnus-define-keys gnus-category-mode-map
889     "q" gnus-category-exit
890     "k" gnus-category-kill
891     "c" gnus-category-copy
892     "a" gnus-category-add
893     "p" gnus-category-edit-predicate
894     "g" gnus-category-edit-groups
895     "s" gnus-category-edit-score
896     "l" gnus-category-list
897
898     "\C-c\C-i" gnus-info-find-node
899     "\C-c\C-b" gnus-bug))
900
901 (defvar gnus-category-menu-hook nil
902   "*Hook run after the creation of the menu.")
903
904 (defun gnus-category-make-menu-bar ()
905   (gnus-turn-off-edit-menu 'category)
906   (unless (boundp 'gnus-category-menu)
907     (easy-menu-define
908      gnus-category-menu gnus-category-mode-map ""
909      '("Categories"
910        ["Add" gnus-category-add t]
911        ["Kill" gnus-category-kill t]
912        ["Copy" gnus-category-copy t]
913        ["Edit predicate" gnus-category-edit-predicate t]
914        ["Edit score" gnus-category-edit-score t]
915        ["Edit groups" gnus-category-edit-groups t]
916        ["Exit" gnus-category-exit t]))
917
918     (run-hooks 'gnus-category-menu-hook)))
919
920 (defun gnus-category-mode ()
921   "Major mode for listing and editing agent categories.
922
923 All normal editing commands are switched off.
924 \\<gnus-category-mode-map>
925 For more in-depth information on this mode, read the manual
926 (`\\[gnus-info-find-node]').
927
928 The following commands are available:
929
930 \\{gnus-category-mode-map}"
931   (interactive)
932   (when (gnus-visual-p 'category-menu 'menu)
933     (gnus-category-make-menu-bar))
934   (kill-all-local-variables)
935   (gnus-simplify-mode-line)
936   (setq major-mode 'gnus-category-mode)
937   (setq mode-name "Category")
938   (gnus-set-default-directory)
939   (setq mode-line-process nil)
940   (use-local-map gnus-category-mode-map)
941   (buffer-disable-undo (current-buffer))
942   (setq truncate-lines t)
943   (setq buffer-read-only t)
944   (run-hooks 'gnus-category-mode-hook))
945
946 (defalias 'gnus-category-position-point 'gnus-goto-colon)
947
948 (defun gnus-category-insert-line (category)
949   (let* ((name (car category))
950          (groups (length (cadddr category))))
951     (beginning-of-line)
952     (gnus-add-text-properties
953      (point)
954      (prog1 (1+ (point))
955        ;; Insert the text.
956        (eval gnus-category-line-format-spec))
957      (list 'gnus-category name))))
958
959 (defun gnus-enter-category-buffer ()
960   "Go to the Category buffer."
961   (interactive)
962   (gnus-category-setup-buffer)
963   (gnus-configure-windows 'category)
964   (gnus-category-prepare))
965
966 (defun gnus-category-setup-buffer ()
967   (unless (get-buffer gnus-category-buffer)
968     (save-excursion
969       (set-buffer (get-buffer-create gnus-category-buffer))
970       (gnus-add-current-to-buffer-list)
971       (gnus-category-mode))))
972
973 (defun gnus-category-prepare ()
974   (gnus-set-format 'category-mode)
975   (gnus-set-format 'category t)
976   (let ((alist gnus-category-alist)
977         (buffer-read-only nil))
978     (erase-buffer)
979     (while alist
980       (gnus-category-insert-line (pop alist)))
981     (goto-char (point-min))
982     (gnus-category-position-point)))
983
984 (defun gnus-category-name ()
985   (or (get-text-property (gnus-point-at-bol) 'gnus-category)
986       (error "No category on the current line")))
987
988 (defun gnus-category-read ()
989   "Read the category alist."
990   (setq gnus-category-alist
991         (or (gnus-agent-read-file
992              (nnheader-concat gnus-agent-directory "lib/categories"))
993             (list (list 'default 'true nil nil)))))
994     
995 (defun gnus-category-write ()
996   "Write the category alist."
997   (setq gnus-category-predicate-cache nil
998         gnus-category-group-cache nil)
999   (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1000     (prin1 gnus-category-alist (current-buffer))))
1001
1002 (defun gnus-category-edit-predicate (category)
1003   "Edit the predicate for CATEGORY."
1004   (interactive (list (gnus-category-name)))
1005   (let ((info (assq category gnus-category-alist)))
1006     (gnus-edit-form
1007      (cadr info) (format "Editing the predicate for category %s" category)
1008      `(lambda (predicate)
1009         (setf (cadr (assq ',category gnus-category-alist)) predicate)
1010         (gnus-category-write)
1011         (gnus-category-list)))))
1012   
1013 (defun gnus-category-edit-score (category)
1014   "Edit the score expression for CATEGORY."
1015   (interactive (list (gnus-category-name)))
1016   (let ((info (assq category gnus-category-alist)))
1017     (gnus-edit-form
1018      (caddr info)
1019      (format "Editing the score expression for category %s" category)
1020      `(lambda (groups)
1021         (setf (caddr (assq ',category gnus-category-alist)) groups)
1022         (gnus-category-write)
1023         (gnus-category-list)))))
1024
1025 (defun gnus-category-edit-groups (category)
1026   "Edit the group list for CATEGORY."
1027   (interactive (list (gnus-category-name)))
1028   (let ((info (assq category gnus-category-alist)))
1029     (gnus-edit-form
1030      (cadddr info) (format "Editing the group list for category %s" category)
1031      `(lambda (groups)
1032         (setf (cadddr (assq ',category gnus-category-alist)) groups)
1033         (gnus-category-write)
1034         (gnus-category-list)))))
1035
1036 (defun gnus-category-kill (category)
1037   "Kill the current category."
1038   (interactive (list (gnus-category-name)))
1039   (let ((info (assq category gnus-category-alist))
1040         (buffer-read-only nil))
1041     (gnus-delete-line)
1042     (gnus-category-write)
1043     (setq gnus-category-alist (delq info gnus-category-alist))))
1044
1045 (defun gnus-category-copy (category to)
1046   "Copy the current category."
1047   (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1048   (let ((info (assq category gnus-category-alist)))
1049     (push (list to (gnus-copy-sequence (cadr info))
1050                 (gnus-copy-sequence (caddr info)) nil)
1051           gnus-category-alist)
1052     (gnus-category-write)
1053     (gnus-category-list)))
1054
1055 (defun gnus-category-add (category)
1056   "Create a new category."
1057   (interactive "SCategory name: ")
1058   (when (assq category gnus-category-alist)
1059     (error "Category %s already exists" category))
1060   (push (list category 'true nil nil)
1061         gnus-category-alist)
1062   (gnus-category-write)
1063   (gnus-category-list))
1064
1065 (defun gnus-category-list ()
1066   "List all categories."
1067   (interactive)
1068   (gnus-category-prepare))
1069
1070 (defun gnus-category-exit ()
1071   "Return to the group buffer."
1072   (interactive)
1073   (kill-buffer (current-buffer))
1074   (gnus-configure-windows 'group t))
1075
1076 ;; To avoid having 8-bit characters in the source file.
1077 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1078
1079 (defvar gnus-category-predicate-alist
1080   '((spam . gnus-agent-spam-p)
1081     (short . gnus-agent-short-p)
1082     (long . gnus-agent-long-p)
1083     (low . gnus-agent-low-scored-p)
1084     (high . gnus-agent-high-scored-p)
1085     (true . gnus-agent-true)
1086     (false . gnus-agent-false))
1087   "Mapping from short score predicate symbols to predicate functions.")
1088
1089 (defun gnus-agent-spam-p ()
1090   "Say whether an article is spam or not."
1091   (unless gnus-agent-spam-hashtb
1092     (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1093   (if (not (equal (mail-header-references gnus-headers) ""))
1094       nil
1095     (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1096       (prog1
1097           (gnus-gethash string gnus-agent-spam-hashtb)
1098         (gnus-sethash string t gnus-agent-spam-hashtb)))))
1099
1100 (defun gnus-agent-short-p ()
1101   "Say whether an article is short or not."
1102   (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1103
1104 (defun gnus-agent-long-p ()
1105   "Say whether an article is long or not."
1106   (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1107
1108 (defun gnus-agent-low-scored-p ()
1109   "Say whether an article has a low score or not."
1110   (< gnus-score gnus-agent-low-score))
1111
1112 (defun gnus-agent-high-scored-p ()
1113   "Say whether an article has a high score or not."
1114   (> gnus-score gnus-agent-low-score))
1115
1116 (defun gnus-category-make-function (cat)
1117   "Make a function from category CAT."
1118   `(lambda () ,(gnus-category-make-function-1 cat)))
1119
1120 (defun gnus-agent-true ()
1121   "Return t."
1122   t)
1123
1124 (defun gnus-agent-false ()
1125   "Return nil."
1126   nil)
1127   
1128 (defun gnus-category-make-function-1 (cat)
1129   "Make a function from category CAT."
1130   (cond
1131    ;; Functions are just returned as is.
1132    ((or (symbolp cat)
1133         (gnus-functionp cat))
1134     `(,(or (cdr (assq cat gnus-category-predicate-alist))
1135            cat)))
1136    ;; More complex category.
1137    ((consp cat)
1138     `(,(cond
1139         ((memq (car cat) '(& and))
1140          'and)
1141         ((memq (car cat) '(| or))
1142          'or)
1143         ((memq (car cat) gnus-category-not)
1144          'not))
1145       ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1146    (t
1147     (error "Unknown category type: %s" cat))))
1148
1149 (defun gnus-get-predicate (predicate)
1150   "Return the predicate for CATEGORY."
1151   (or (cdr (assoc predicate gnus-category-predicate-cache))
1152       (cdar (push (cons predicate
1153                         (gnus-category-make-function predicate))
1154                   gnus-category-predicate-cache))))
1155
1156 (defun gnus-group-category (group)
1157   "Return the category GROUP belongs to."
1158   (unless gnus-category-group-cache
1159     (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1160     (let ((cs gnus-category-alist)
1161           groups cat)
1162       (while (setq cat (pop cs))
1163         (setq groups (cadddr cat))
1164         (while groups
1165           (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1166   (or (gnus-gethash group gnus-category-group-cache)
1167       (assq 'default gnus-category-alist)))
1168
1169 (defun gnus-agent-expire ()
1170   "Expire all old articles."
1171   (interactive)
1172   (let ((methods gnus-agent-covered-methods)
1173         (alist (cdr gnus-newsrc-alist))
1174         gnus-command-method ofiles info method file group)
1175     (while (setq gnus-command-method (pop methods))
1176       (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1177                                   (gnus-agent-directory)))))
1178     (while (setq info (pop alist))
1179       (when (and (gnus-agent-method-p
1180                   (setq gnus-command-method
1181                         (gnus-find-method-for-group
1182                          (setq group (gnus-info-group info)))))
1183                  (member
1184                   (setq file
1185                         (concat
1186                          (gnus-agent-directory)
1187                          (gnus-agent-group-path group) "/.overview"))
1188                   ofiles))
1189         (setq ofiles (delete file ofiles))
1190         (gnus-agent-expire-group file group)))
1191     (while ofiles
1192       (gnus-agent-expire-group (pop ofiles)))))
1193
1194 (defun gnus-agent-expire-directory (dir)
1195   "Expire all groups in DIR recursively."
1196   (when (file-directory-p dir)
1197     (let ((files (directory-files dir t))
1198           file ofiles)
1199       (while (setq file (pop files))
1200         (cond
1201          ((member (file-name-nondirectory file) '("." ".."))
1202           ;; Do nothing.
1203           )
1204          ((file-directory-p file)
1205           ;; Recurse.
1206           (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1207          ((string-match "\\.overview$" file)
1208           ;; Expire group.
1209           (push file ofiles))))
1210       ofiles)))
1211
1212 (defun gnus-agent-expire-group (overview &optional group)
1213   "Expire articles in OVERVIEW."
1214   (gnus-message 5 "Expiring %s..." overview)
1215   (let ((odate (- (gnus-time-to-day (current-time)) 4))
1216         (dir (file-name-directory overview))
1217         (info (when group (gnus-get-info group)))
1218         headers article file point unreads)
1219     (gnus-agent-load-alist nil dir)
1220     (when info
1221       (setq unreads
1222             (nconc
1223              (gnus-list-of-unread-articles group)
1224              (gnus-uncompress-range
1225               (cdr (assq 'tick (gnus-info-marks info))))
1226              (gnus-uncompress-range
1227               (cdr (assq 'dormant (gnus-info-marks info)))))))
1228     (nnheader-temp-write overview
1229       (insert-file-contents overview)
1230       (goto-char (point-min))
1231       (while (not (eobp))
1232         (setq point (point))
1233         (condition-case ()
1234             (setq headers (inline (nnheader-parse-nov)))
1235           (error
1236            (goto-char point)
1237            (gnus-delete-line)
1238            (setq headers nil)))
1239         (when headers
1240           (unless (memq (setq article (mail-header-number headers)) unreads)
1241             (if (not (< (inline
1242                           (gnus-time-to-day
1243                            (inline (nnmail-date-to-time
1244                                     (mail-header-date headers)))))
1245                         odate))
1246                 (forward-line 1)              
1247               (gnus-delete-line)
1248               (setq gnus-agent-article-alist
1249                     (delq (assq article gnus-agent-article-alist)
1250                           gnus-agent-article-alist))
1251               (when (file-exists-p
1252                      (setq file (concat dir (number-to-string article))))
1253                 (delete-file file))))))
1254       (gnus-agent-save-alist nil nil nil dir))))
1255
1256 (provide 'gnus-agent)
1257
1258 ;;; gnus-agent.el ends here