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