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