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