Sync up with qgnus-0.33.
[elisp/gnus.git-] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Semi-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         ;;
707         ;; To gnus-agent-expire work fine with no Xref field in .overview 
708         ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
709         (goto-char (point-min))
710         (while (not (eobp))
711           (goto-char (point-at-eol))
712           (insert "\t")
713           (forward-line 1))
714         ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
715         ;; To gnus-agent-expire work fine with no Xref field in .overview 
716         ;;
717         ;; Save these headers for later processing.
718         (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
719         (let (file)
720           (when (file-exists-p
721                  (setq file (gnus-agent-article-name ".overview" group)))
722             (gnus-agent-braid-nov group articles file))
723           (gnus-make-directory (nnheader-translate-file-chars
724                                 (file-name-directory file)))
725           (write-region (point-min) (point-max) file nil 'silent)
726           (gnus-agent-save-alist group articles nil))
727         t))))
728
729 (defsubst gnus-agent-copy-nov-line (article)
730   (let (b e)
731     (set-buffer gnus-agent-overview-buffer)
732     (setq b (point))
733     (if (eq article (read (current-buffer)))
734         (setq e (progn (forward-line 1) (point)))
735       (setq e b))
736     (set-buffer nntp-server-buffer)
737     (insert-buffer-substring gnus-agent-overview-buffer b e)))
738
739 (defun gnus-agent-braid-nov (group articles file)
740   (let (beg end)
741     (set-buffer gnus-agent-overview-buffer)
742     (goto-char (point-min))
743     (set-buffer nntp-server-buffer)
744     (erase-buffer)
745     (insert-file-contents file)
746     (goto-char (point-min))
747     (if (or (= (point-min) (point-max))
748             (progn
749               (forward-line -1)
750               (< (read (current-buffer)) (car articles))))
751         ;; We have only headers that are after the older headers,
752         ;; so we just append them.
753         (progn
754           (goto-char (point-max))
755           (insert-buffer-substring gnus-agent-overview-buffer))
756       ;; We do it the hard way.
757       (nnheader-find-nov-line (car articles))
758       (gnus-agent-copy-nov-line (car articles))
759       (pop articles)
760       (while (and articles
761                   (not (eobp)))
762         (while (and (not (eobp))
763                     (< (read (current-buffer)) (car articles)))
764           (forward-line 1))
765         (beginning-of-line)
766         (unless (eobp)
767           (gnus-agent-copy-nov-line (car articles))
768           (setq articles (cdr articles))))
769       (when articles
770         (let (b e)
771           (set-buffer gnus-agent-overview-buffer)
772           (setq b (point)
773                 e (point-max))
774           (set-buffer nntp-server-buffer)
775           (insert-buffer-substring gnus-agent-overview-buffer b e))))))
776
777 (defun gnus-agent-load-alist (group &optional dir)
778   "Load the article-state alist for GROUP."
779   (setq gnus-agent-article-alist
780         (gnus-agent-read-file
781          (if dir
782              (concat dir ".agentview")
783            (gnus-agent-article-name ".agentview" group)))))
784
785 (defun gnus-agent-save-alist (group &optional articles state dir)
786   "Load the article-state alist for GROUP."
787   (nnheader-temp-write (if dir
788                            (concat dir ".agentview")
789                          (gnus-agent-article-name ".agentview" group))
790     (princ (setq gnus-agent-article-alist
791                  (nconc gnus-agent-article-alist
792                         (mapcar (lambda (article) (cons article state))
793                                 articles)))
794            (current-buffer))
795     (insert "\n")))
796
797 (defun gnus-agent-article-name (article group)
798   (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
799           (if (stringp article) article (string-to-number article))))
800
801 ;;;###autoload
802 (defun gnus-agent-batch-fetch ()
803   "Start Gnus and fetch session."
804   (interactive)
805   (gnus)
806   (gnus-agent-fetch-session)
807   (gnus-group-exit))
808
809 (defun gnus-agent-fetch-session ()
810   "Fetch all articles and headers that are eligible for fetching."
811   (interactive)
812   (unless gnus-agent-covered-methods
813     (error "No servers are covered by the Gnus agent"))
814   (unless gnus-plugged
815     (error "Can't fetch articles while Gnus is unplugged"))
816   (let ((methods gnus-agent-covered-methods)
817         groups group gnus-command-method)
818     (save-excursion
819       (while methods
820         (setq gnus-command-method (car methods)
821               groups (gnus-groups-from-server (pop methods)))
822         (gnus-agent-with-fetch
823           (while (setq group (pop groups))
824             (when (<= (gnus-group-level group) gnus-agent-handle-level)
825               (gnus-agent-fetch-group-1 group gnus-command-method)))))
826       (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
827
828 (defun gnus-agent-fetch-group-1 (group method)
829   "Fetch GROUP."
830   (let ((gnus-command-method method)
831         gnus-newsgroup-dependencies gnus-newsgroup-headers
832         gnus-newsgroup-scored gnus-headers gnus-score
833         gnus-use-cache articles score arts
834         category predicate info marks score-param)
835     ;; Fetch headers.
836     (when (and (setq articles (gnus-list-of-unread-articles group))
837                (gnus-agent-fetch-headers group articles))
838       ;; Parse them and see which articles we want to fetch.
839       (setq gnus-newsgroup-dependencies
840             (make-vector (length articles) 0))
841       (setq gnus-newsgroup-headers
842             (gnus-get-newsgroup-headers-xover articles nil nil group))
843       (setq category (gnus-group-category group))
844       (setq predicate
845             (gnus-get-predicate 
846              (or (gnus-group-get-parameter group 'agent-predicate)
847                  (cadr category))))
848       (setq score-param
849             (or (gnus-group-get-parameter group 'agent-score)
850                 (caddr category)))
851       (when score-param
852         (gnus-score-headers (list (list score-param))))
853       (setq arts nil)
854       (while (setq gnus-headers (pop gnus-newsgroup-headers))
855         (setq gnus-score
856               (or (cdr (assq (mail-header-number gnus-headers)
857                              gnus-newsgroup-scored))
858                   gnus-summary-default-score))
859         (when (funcall predicate)
860           (push (mail-header-number gnus-headers)
861                 arts)))
862       ;; Fetch the articles.
863       (when arts
864         (gnus-agent-fetch-articles group arts)))
865     ;; Perhaps we have some additional articles to fetch.
866     (setq arts (assq 'download (gnus-info-marks
867                                 (setq info (gnus-get-info group)))))
868     (when (cdr arts)
869       (gnus-agent-fetch-articles
870        group (gnus-uncompress-range (cdr arts)))
871       (setq marks (delq arts (gnus-info-marks info)))
872       (gnus-info-set-marks info marks))))
873
874 ;;;
875 ;;; Agent Category Mode
876 ;;;
877
878 (defvar gnus-category-mode-hook nil
879   "Hook run in `gnus-category-mode' buffers.")
880
881 (defvar gnus-category-line-format "     %(%20c%): %g\n"
882   "Format of category lines.")
883
884 (defvar gnus-category-mode-line-format "Gnus: %%b"
885   "The format specification for the category mode line.")
886
887 (defvar gnus-agent-short-article 100
888   "Articles that have fewer lines than this are short.")
889
890 (defvar gnus-agent-long-article 200
891   "Articles that have more lines than this are long.")
892
893 (defvar gnus-agent-low-score 0
894   "Articles that have a score lower than this have a low score.")
895
896 (defvar gnus-agent-high-score 0
897   "Articles that have a score higher than this have a high score.")
898
899
900 ;;; Internal variables.
901
902 (defvar gnus-category-buffer "*Agent Category*")
903
904 (defvar gnus-category-line-format-alist
905   `((?c name ?s)
906     (?g groups ?d)))
907
908 (defvar gnus-category-mode-line-format-alist
909   `((?u user-defined ?s)))
910
911 (defvar gnus-category-line-format-spec nil)
912 (defvar gnus-category-mode-line-format-spec nil)
913
914 (defvar gnus-category-mode-map nil)
915 (put 'gnus-category-mode 'mode-class 'special)
916
917 (unless gnus-category-mode-map
918   (setq gnus-category-mode-map (make-sparse-keymap))
919   (suppress-keymap gnus-category-mode-map)
920
921   (gnus-define-keys gnus-category-mode-map
922     "q" gnus-category-exit
923     "k" gnus-category-kill
924     "c" gnus-category-copy
925     "a" gnus-category-add
926     "p" gnus-category-edit-predicate
927     "g" gnus-category-edit-groups
928     "s" gnus-category-edit-score
929     "l" gnus-category-list
930
931     "\C-c\C-i" gnus-info-find-node
932     "\C-c\C-b" gnus-bug))
933
934 (defvar gnus-category-menu-hook nil
935   "*Hook run after the creation of the menu.")
936
937 (defun gnus-category-make-menu-bar ()
938   (gnus-turn-off-edit-menu 'category)
939   (unless (boundp 'gnus-category-menu)
940     (easy-menu-define
941      gnus-category-menu gnus-category-mode-map ""
942      '("Categories"
943        ["Add" gnus-category-add t]
944        ["Kill" gnus-category-kill t]
945        ["Copy" gnus-category-copy t]
946        ["Edit predicate" gnus-category-edit-predicate t]
947        ["Edit score" gnus-category-edit-score t]
948        ["Edit groups" gnus-category-edit-groups t]
949        ["Exit" gnus-category-exit t]))
950
951     (gnus-run-hooks 'gnus-category-menu-hook)))
952
953 (defun gnus-category-mode ()
954   "Major mode for listing and editing agent categories.
955
956 All normal editing commands are switched off.
957 \\<gnus-category-mode-map>
958 For more in-depth information on this mode, read the manual
959 (`\\[gnus-info-find-node]').
960
961 The following commands are available:
962
963 \\{gnus-category-mode-map}"
964   (interactive)
965   (when (gnus-visual-p 'category-menu 'menu)
966     (gnus-category-make-menu-bar))
967   (kill-all-local-variables)
968   (gnus-simplify-mode-line)
969   (setq major-mode 'gnus-category-mode)
970   (setq mode-name "Category")
971   (gnus-set-default-directory)
972   (setq mode-line-process nil)
973   (use-local-map gnus-category-mode-map)
974   (buffer-disable-undo (current-buffer))
975   (setq truncate-lines t)
976   (setq buffer-read-only t)
977   (gnus-run-hooks 'gnus-category-mode-hook))
978
979 (defalias 'gnus-category-position-point 'gnus-goto-colon)
980
981 (defun gnus-category-insert-line (category)
982   (let* ((name (car category))
983          (groups (length (cadddr category))))
984     (beginning-of-line)
985     (gnus-add-text-properties
986      (point)
987      (prog1 (1+ (point))
988        ;; Insert the text.
989        (eval gnus-category-line-format-spec))
990      (list 'gnus-category name))))
991
992 (defun gnus-enter-category-buffer ()
993   "Go to the Category buffer."
994   (interactive)
995   (gnus-category-setup-buffer)
996   (gnus-configure-windows 'category)
997   (gnus-category-prepare))
998
999 (defun gnus-category-setup-buffer ()
1000   (unless (get-buffer gnus-category-buffer)
1001     (save-excursion
1002       (set-buffer (get-buffer-create gnus-category-buffer))
1003       (gnus-add-current-to-buffer-list)
1004       (gnus-category-mode))))
1005
1006 (defun gnus-category-prepare ()
1007   (gnus-set-format 'category-mode)
1008   (gnus-set-format 'category t)
1009   (let ((alist gnus-category-alist)
1010         (buffer-read-only nil))
1011     (erase-buffer)
1012     (while alist
1013       (gnus-category-insert-line (pop alist)))
1014     (goto-char (point-min))
1015     (gnus-category-position-point)))
1016
1017 (defun gnus-category-name ()
1018   (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1019       (error "No category on the current line")))
1020
1021 (defun gnus-category-read ()
1022   "Read the category alist."
1023   (setq gnus-category-alist
1024         (or (gnus-agent-read-file
1025              (nnheader-concat gnus-agent-directory "lib/categories"))
1026             (list (list 'default 'short nil nil)))))
1027     
1028 (defun gnus-category-write ()
1029   "Write the category alist."
1030   (setq gnus-category-predicate-cache nil
1031         gnus-category-group-cache nil)
1032   (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1033     (prin1 gnus-category-alist (current-buffer))))
1034
1035 (defun gnus-category-edit-predicate (category)
1036   "Edit the predicate for CATEGORY."
1037   (interactive (list (gnus-category-name)))
1038   (let ((info (assq category gnus-category-alist)))
1039     (gnus-edit-form
1040      (cadr info) (format "Editing the predicate for category %s" category)
1041      `(lambda (predicate)
1042         (setf (cadr (assq ',category gnus-category-alist)) predicate)
1043         (gnus-category-write)
1044         (gnus-category-list)))))
1045   
1046 (defun gnus-category-edit-score (category)
1047   "Edit the score expression for CATEGORY."
1048   (interactive (list (gnus-category-name)))
1049   (let ((info (assq category gnus-category-alist)))
1050     (gnus-edit-form
1051      (caddr info)
1052      (format "Editing the score expression for category %s" category)
1053      `(lambda (groups)
1054         (setf (caddr (assq ',category gnus-category-alist)) groups)
1055         (gnus-category-write)
1056         (gnus-category-list)))))
1057
1058 (defun gnus-category-edit-groups (category)
1059   "Edit the group list for CATEGORY."
1060   (interactive (list (gnus-category-name)))
1061   (let ((info (assq category gnus-category-alist)))
1062     (gnus-edit-form
1063      (cadddr info) (format "Editing the group list for category %s" category)
1064      `(lambda (groups)
1065         (setf (cadddr (assq ',category gnus-category-alist)) groups)
1066         (gnus-category-write)
1067         (gnus-category-list)))))
1068
1069 (defun gnus-category-kill (category)
1070   "Kill the current category."
1071   (interactive (list (gnus-category-name)))
1072   (let ((info (assq category gnus-category-alist))
1073         (buffer-read-only nil))
1074     (gnus-delete-line)
1075     (gnus-category-write)
1076     (setq gnus-category-alist (delq info gnus-category-alist))))
1077
1078 (defun gnus-category-copy (category to)
1079   "Copy the current category."
1080   (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1081   (let ((info (assq category gnus-category-alist)))
1082     (push (list to (gnus-copy-sequence (cadr info))
1083                 (gnus-copy-sequence (caddr info)) nil)
1084           gnus-category-alist)
1085     (gnus-category-write)
1086     (gnus-category-list)))
1087
1088 (defun gnus-category-add (category)
1089   "Create a new category."
1090   (interactive "SCategory name: ")
1091   (when (assq category gnus-category-alist)
1092     (error "Category %s already exists" category))
1093   (push (list category 'true nil nil)
1094         gnus-category-alist)
1095   (gnus-category-write)
1096   (gnus-category-list))
1097
1098 (defun gnus-category-list ()
1099   "List all categories."
1100   (interactive)
1101   (gnus-category-prepare))
1102
1103 (defun gnus-category-exit ()
1104   "Return to the group buffer."
1105   (interactive)
1106   (kill-buffer (current-buffer))
1107   (gnus-configure-windows 'group t))
1108
1109 ;; To avoid having 8-bit characters in the source file.
1110 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1111
1112 (defvar gnus-category-predicate-alist
1113   '((spam . gnus-agent-spam-p)
1114     (short . gnus-agent-short-p)
1115     (long . gnus-agent-long-p)
1116     (low . gnus-agent-low-scored-p)
1117     (high . gnus-agent-high-scored-p)
1118     (true . gnus-agent-true)
1119     (false . gnus-agent-false))
1120   "Mapping from short score predicate symbols to predicate functions.")
1121
1122 (defun gnus-agent-spam-p ()
1123   "Say whether an article is spam or not."
1124   (unless gnus-agent-spam-hashtb
1125     (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1126   (if (not (equal (mail-header-references gnus-headers) ""))
1127       nil
1128     (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1129       (prog1
1130           (gnus-gethash string gnus-agent-spam-hashtb)
1131         (gnus-sethash string t gnus-agent-spam-hashtb)))))
1132
1133 (defun gnus-agent-short-p ()
1134   "Say whether an article is short or not."
1135   (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1136
1137 (defun gnus-agent-long-p ()
1138   "Say whether an article is long or not."
1139   (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1140
1141 (defun gnus-agent-low-scored-p ()
1142   "Say whether an article has a low score or not."
1143   (< gnus-score gnus-agent-low-score))
1144
1145 (defun gnus-agent-high-scored-p ()
1146   "Say whether an article has a high score or not."
1147   (> gnus-score gnus-agent-low-score))
1148
1149 (defun gnus-category-make-function (cat)
1150   "Make a function from category CAT."
1151   `(lambda () ,(gnus-category-make-function-1 cat)))
1152
1153 (defun gnus-agent-true ()
1154   "Return t."
1155   t)
1156
1157 (defun gnus-agent-false ()
1158   "Return nil."
1159   nil)
1160   
1161 (defun gnus-category-make-function-1 (cat)
1162   "Make a function from category CAT."
1163   (cond
1164    ;; Functions are just returned as is.
1165    ((or (symbolp cat)
1166         (gnus-functionp cat))
1167     `(,(or (cdr (assq cat gnus-category-predicate-alist))
1168            cat)))
1169    ;; More complex category.
1170    ((consp cat)
1171     `(,(cond
1172         ((memq (car cat) '(& and))
1173          'and)
1174         ((memq (car cat) '(| or))
1175          'or)
1176         ((memq (car cat) gnus-category-not)
1177          'not))
1178       ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1179    (t
1180     (error "Unknown category type: %s" cat))))
1181
1182 (defun gnus-get-predicate (predicate)
1183   "Return the predicate for CATEGORY."
1184   (or (cdr (assoc predicate gnus-category-predicate-cache))
1185       (cdar (push (cons predicate
1186                         (gnus-category-make-function predicate))
1187                   gnus-category-predicate-cache))))
1188
1189 (defun gnus-group-category (group)
1190   "Return the category GROUP belongs to."
1191   (unless gnus-category-group-cache
1192     (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1193     (let ((cs gnus-category-alist)
1194           groups cat)
1195       (while (setq cat (pop cs))
1196         (setq groups (cadddr cat))
1197         (while groups
1198           (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1199   (or (gnus-gethash group gnus-category-group-cache)
1200       (assq 'default gnus-category-alist)))
1201
1202 (defun gnus-agent-expire ()
1203   "Expire all old articles."
1204   (interactive)
1205   (let ((methods gnus-agent-covered-methods)
1206         (alist (cdr gnus-newsrc-alist))
1207         gnus-command-method ofiles info method file group)
1208     (while (setq gnus-command-method (pop methods))
1209       (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1210                                   (gnus-agent-directory)))))
1211     (while (setq info (pop alist))
1212       (when (and (gnus-agent-method-p
1213                   (setq gnus-command-method
1214                         (gnus-find-method-for-group
1215                          (setq group (gnus-info-group info)))))
1216                  (member
1217                   (setq file
1218                         (concat
1219                          (gnus-agent-directory)
1220                          (gnus-agent-group-path group) "/.overview"))
1221                   ofiles))
1222         (setq ofiles (delete file ofiles))
1223         (gnus-agent-expire-group file group)))
1224     (while ofiles
1225       (gnus-agent-expire-group (pop ofiles)))))
1226
1227 (defun gnus-agent-expire-directory (dir)
1228   "Expire all groups in DIR recursively."
1229   (when (file-directory-p dir)
1230     (let ((files (directory-files dir t))
1231           file ofiles)
1232       (while (setq file (pop files))
1233         (cond
1234          ((member (file-name-nondirectory file) '("." ".."))
1235           ;; Do nothing.
1236           )
1237          ((file-directory-p file)
1238           ;; Recurse.
1239           (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1240          ((string-match "\\.overview$" file)
1241           ;; Expire group.
1242           (push file ofiles))))
1243       ofiles)))
1244
1245 (defun gnus-agent-expire-group (overview &optional group)
1246   "Expire articles in OVERVIEW."
1247   (gnus-message 5 "Expiring %s..." overview)
1248   (let ((odate (- (gnus-time-to-day (current-time)) 4))
1249         (dir (file-name-directory overview))
1250         (info (when group (gnus-get-info group)))
1251         headers article file point unreads)
1252     (gnus-agent-load-alist nil dir)
1253     (when info
1254       (setq unreads
1255             (nconc
1256              (gnus-list-of-unread-articles group)
1257              (gnus-uncompress-range
1258               (cdr (assq 'tick (gnus-info-marks info))))
1259              (gnus-uncompress-range
1260               (cdr (assq 'dormant (gnus-info-marks info)))))))
1261     (nnheader-temp-write overview
1262       (insert-file-contents overview)
1263       (goto-char (point-min))
1264       (while (not (eobp))
1265         (setq point (point))
1266         (condition-case ()
1267             (setq headers (inline (nnheader-parse-nov)))
1268           (error
1269            (goto-char point)
1270            (gnus-delete-line)
1271            (setq headers nil)))
1272         (when headers
1273           (if (memq (setq article (mail-header-number headers)) unreads)
1274               (forward-line 1)
1275             (if (not (< (inline
1276                           (gnus-time-to-day
1277                            (inline (nnmail-date-to-time
1278                                     (mail-header-date headers)))))
1279                         odate))
1280                 (forward-line 1)              
1281               (gnus-delete-line)
1282               (setq gnus-agent-article-alist
1283                     (delq (assq article gnus-agent-article-alist)
1284                           gnus-agent-article-alist))
1285               (when (file-exists-p
1286                      (setq file (concat dir (number-to-string article))))
1287                 (delete-file file))))))
1288       (gnus-agent-save-alist nil nil nil dir))))
1289
1290 ;;;###autoload
1291 (defun gnus-agent-batch ()
1292   (interactive)
1293   (let ((init-file-user "")
1294         (gnus-always-read-dribble-file t))
1295     (gnus))
1296   (gnus-group-send-drafts)
1297   (gnus-agent-fetch-session))
1298
1299 (provide 'gnus-agent)
1300
1301 ;;; gnus-agent.el ends here