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