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