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