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