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