* gnus-ja.texi (Positioning Point): Translated.
[elisp/gnus.git-] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
30
31 (require 'gnus)
32 (require 'gnus-cache)
33 (require 'nnvirtual)
34 (require 'gnus-sum)
35 (require 'gnus-score)
36 (eval-when-compile
37   (if (featurep 'xemacs)
38       (require 'itimer)
39     (require 'timer))
40   (require 'gnus-group))
41
42 (eval-and-compile
43   (autoload 'gnus-server-update-server "gnus-srvr"))
44
45 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
46   "Where the Gnus agent will store its files."
47   :group 'gnus-agent
48   :type 'directory)
49
50 (defcustom gnus-agent-plugged-hook nil
51   "Hook run when plugging into the network."
52   :group 'gnus-agent
53   :type 'hook)
54
55 (defcustom gnus-agent-unplugged-hook nil
56   "Hook run when unplugging from the network."
57   :group 'gnus-agent
58   :type 'hook)
59
60 (defcustom gnus-agent-handle-level gnus-level-subscribed
61   "Groups on levels higher than this variable will be ignored by the Agent."
62   :group 'gnus-agent
63   :type 'integer)
64
65 (defcustom gnus-agent-expire-days 7
66   "Read articles older than this will be expired."
67   :group 'gnus-agent
68   :type 'integer)
69
70 (defcustom gnus-agent-expire-all nil
71   "If non-nil, also expire unread, ticked and dormant articles.
72 If nil, only read articles will be expired."
73   :group 'gnus-agent
74   :type 'boolean)
75
76 (defcustom gnus-agent-group-mode-hook nil
77   "Hook run in Agent group minor modes."
78   :group 'gnus-agent
79   :type 'hook)
80
81 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
82 (when (featurep 'xemacs)
83   (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
84
85 (defcustom gnus-agent-summary-mode-hook nil
86   "Hook run in Agent summary minor modes."
87   :group 'gnus-agent
88   :type 'hook)
89
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92   (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
93
94 (defcustom gnus-agent-server-mode-hook nil
95   "Hook run in Agent summary minor modes."
96   :group 'gnus-agent
97   :type 'hook)
98
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101   (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
102
103 (defcustom gnus-agent-confirmation-function 'y-or-n-p
104   "Function to confirm when error happens."
105   :version "21.1"
106   :group 'gnus-agent
107   :type 'function)
108
109 (defcustom gnus-agent-large-newsgroup nil
110   "*The number of articles which indicates a large newsgroup.
111 If the number of unread articles exceeds it, The number of articles to be
112 fetched will be limited to it. If not a positive integer, never consider it."
113   :group 'gnus-agent
114   :type '(choice (const nil)
115                  (integer :tag "Number")))
116
117 (defcustom gnus-agent-synchronize-flags 'ask
118   "Indicate if flags are synchronized when you plug in.
119 If this is `ask' the hook will query the user."
120   :version "21.1"
121   :type '(choice (const :tag "Always" t)
122                  (const :tag "Never" nil)
123                  (const :tag "Ask" ask))
124   :group 'gnus-agent)
125
126 ;;; Internal variables
127
128 (defvar gnus-agent-history-buffers nil)
129 (defvar gnus-agent-buffer-alist nil)
130 (defvar gnus-agent-article-alist nil)
131 (defvar gnus-agent-group-alist nil)
132 (defvar gnus-agent-covered-methods nil)
133 (defvar gnus-category-alist nil)
134 (defvar gnus-agent-current-history nil)
135 (defvar gnus-agent-overview-buffer nil)
136 (defvar gnus-category-predicate-cache nil)
137 (defvar gnus-category-group-cache nil)
138 (defvar gnus-agent-spam-hashtb nil)
139 (defvar gnus-agent-file-name nil)
140 (defvar gnus-agent-send-mail-function nil)
141 (defvar gnus-agent-file-coding-system 'raw-text)
142
143 ;; Dynamic variables
144 (defvar gnus-headers)
145 (defvar gnus-score)
146
147 ;;;
148 ;;; Setup
149 ;;;
150
151 (defun gnus-open-agent ()
152   (setq gnus-agent t)
153   (gnus-agent-read-servers)
154   (gnus-category-read)
155   (gnus-agent-create-buffer)
156   (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
157   (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
158   (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
159
160 (defun gnus-agent-create-buffer ()
161   (if (gnus-buffer-live-p gnus-agent-overview-buffer)
162       t
163     (setq gnus-agent-overview-buffer
164           (gnus-get-buffer-create " *Gnus agent overview*"))
165     (with-current-buffer gnus-agent-overview-buffer
166       (set-buffer-multibyte t))
167     nil))
168
169 (gnus-add-shutdown 'gnus-close-agent 'gnus)
170
171 (defun gnus-close-agent ()
172   (setq gnus-agent-covered-methods nil
173         gnus-category-predicate-cache nil
174         gnus-category-group-cache nil
175         gnus-agent-spam-hashtb nil)
176   (gnus-kill-buffer gnus-agent-overview-buffer))
177
178 ;;;
179 ;;; Utility functions
180 ;;;
181
182 (defun gnus-agent-read-file (file)
183   "Load FILE and do a `read' there."
184   (with-temp-buffer
185     (ignore-errors
186       (nnheader-insert-file-contents file)
187       (goto-char (point-min))
188       (read (current-buffer)))))
189
190 (defsubst gnus-agent-method ()
191   (concat (symbol-name (car gnus-command-method)) "/"
192           (if (equal (cadr gnus-command-method) "")
193               "unnamed"
194             (cadr gnus-command-method))))
195
196 (defsubst gnus-agent-directory ()
197   "Path of the Gnus agent directory."
198   (nnheader-concat gnus-agent-directory
199                    (nnheader-translate-file-chars (gnus-agent-method)) "/"))
200
201 (defun gnus-agent-lib-file (file)
202   "The full path of the Gnus agent library FILE."
203   (expand-file-name file
204                     (file-name-as-directory
205                      (expand-file-name "agent.lib" (gnus-agent-directory)))))
206
207 ;;; Fetching setup functions.
208
209 (defun gnus-agent-start-fetch ()
210   "Initialize data structures for efficient fetching."
211   (gnus-agent-open-history)
212   (setq gnus-agent-current-history (gnus-agent-history-buffer))
213   (gnus-agent-create-buffer))
214
215 (defun gnus-agent-stop-fetch ()
216   "Save all data structures and clean up."
217   (gnus-agent-save-history)
218   (gnus-agent-close-history)
219   (setq gnus-agent-spam-hashtb nil)
220   (save-excursion
221     (set-buffer nntp-server-buffer)
222     (widen)))
223
224 (defmacro gnus-agent-with-fetch (&rest forms)
225   "Do FORMS safely."
226   `(unwind-protect
227        (let ((gnus-agent-fetching t))
228          (gnus-agent-start-fetch)
229          ,@forms)
230      (gnus-agent-stop-fetch)))
231
232 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
233 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
234
235 ;;;
236 ;;; Mode infestation
237 ;;;
238
239 (defvar gnus-agent-mode-hook nil
240   "Hook run when installing agent mode.")
241
242 (defvar gnus-agent-mode nil)
243 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
244
245 (defun gnus-agent-mode ()
246   "Minor mode for providing a agent support in Gnus buffers."
247   (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
248                                       (symbol-name major-mode))
249                         (match-string 1 (symbol-name major-mode))))
250          (mode (intern (format "gnus-agent-%s-mode" buffer))))
251     (set (make-local-variable 'gnus-agent-mode) t)
252     (set mode nil)
253     (set (make-local-variable mode) t)
254     ;; Set up the menu.
255     (when (gnus-visual-p 'agent-menu 'menu)
256       (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
257     (unless (assq 'gnus-agent-mode minor-mode-alist)
258       (push gnus-agent-mode-status minor-mode-alist))
259     (unless (assq mode minor-mode-map-alist)
260       (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
261                                                      buffer))))
262             minor-mode-map-alist))
263     (when (eq major-mode 'gnus-group-mode)
264       (gnus-agent-toggle-plugged gnus-plugged))
265     (gnus-run-hooks 'gnus-agent-mode-hook
266                     (intern (format "gnus-agent-%s-mode-hook" buffer)))))
267
268 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
269 (gnus-define-keys gnus-agent-group-mode-map
270   "Ju" gnus-agent-fetch-groups
271   "Jc" gnus-enter-category-buffer
272   "Jj" gnus-agent-toggle-plugged
273   "Js" gnus-agent-fetch-session
274   "JY" gnus-agent-synchronize-flags
275   "JS" gnus-group-send-drafts
276   "Ja" gnus-agent-add-group
277   "Jr" gnus-agent-remove-group)
278
279 (defun gnus-agent-group-make-menu-bar ()
280   (unless (boundp 'gnus-agent-group-menu)
281     (easy-menu-define
282      gnus-agent-group-menu gnus-agent-group-mode-map ""
283      '("Agent"
284        ["Toggle plugged" gnus-agent-toggle-plugged t]
285        ["List categories" gnus-enter-category-buffer t]
286        ["Send drafts" gnus-group-send-drafts gnus-plugged]
287        ("Fetch"
288         ["All" gnus-agent-fetch-session gnus-plugged]
289         ["Group" gnus-agent-fetch-group gnus-plugged])))))
290
291 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
292 (gnus-define-keys gnus-agent-summary-mode-map
293   "Jj" gnus-agent-toggle-plugged
294   "Ju" gnus-agent-summary-fetch-group
295   "J#" gnus-agent-mark-article
296   "J\M-#" gnus-agent-unmark-article
297   "@" gnus-agent-toggle-mark
298   "Jc" gnus-agent-catchup)
299
300 (defun gnus-agent-summary-make-menu-bar ()
301   (unless (boundp 'gnus-agent-summary-menu)
302     (easy-menu-define
303      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
304      '("Agent"
305        ["Toggle plugged" gnus-agent-toggle-plugged t]
306        ["Mark as downloadable" gnus-agent-mark-article t]
307        ["Unmark as downloadable" gnus-agent-unmark-article t]
308        ["Toggle mark" gnus-agent-toggle-mark t]
309        ["Fetch downloadable" gnus-agent-summary-fetch-group t]
310        ["Catchup undownloaded" gnus-agent-catchup t]))))
311
312 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
313 (gnus-define-keys gnus-agent-server-mode-map
314   "Jj" gnus-agent-toggle-plugged
315   "Ja" gnus-agent-add-server
316   "Jr" gnus-agent-remove-server)
317
318 (defun gnus-agent-server-make-menu-bar ()
319   (unless (boundp 'gnus-agent-server-menu)
320     (easy-menu-define
321      gnus-agent-server-menu gnus-agent-server-mode-map ""
322      '("Agent"
323        ["Toggle plugged" gnus-agent-toggle-plugged t]
324        ["Add" gnus-agent-add-server t]
325        ["Remove" gnus-agent-remove-server t]))))
326
327 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
328   (if (and (fboundp 'propertize)
329            (fboundp 'make-mode-line-mouse-map))
330       (propertize string 'local-map
331                   (make-mode-line-mouse-map mouse-button mouse-func))
332     string))
333
334 (defun gnus-agent-toggle-plugged (plugged)
335   "Toggle whether Gnus is unplugged or not."
336   (interactive (list (not gnus-plugged)))
337   (if plugged
338       (progn
339         (setq gnus-plugged plugged)
340         (gnus-agent-possibly-synchronize-flags)
341         (gnus-run-hooks 'gnus-agent-plugged-hook)
342         (setcar (cdr gnus-agent-mode-status)
343                 (gnus-agent-make-mode-line-string " Plugged"
344                                                   'mouse-2
345                                                   'gnus-agent-toggle-plugged)))
346     (gnus-agent-close-connections)
347     (setq gnus-plugged plugged)
348     (gnus-run-hooks 'gnus-agent-unplugged-hook)
349     (setcar (cdr gnus-agent-mode-status)
350             (gnus-agent-make-mode-line-string " Unplugged"
351                                               'mouse-2
352                                               'gnus-agent-toggle-plugged)))
353   (force-mode-line-update))
354
355 (defun gnus-agent-close-connections ()
356   "Close all methods covered by the Gnus agent."
357   (let ((methods gnus-agent-covered-methods))
358     (while methods
359       (gnus-close-server (pop methods)))))
360
361 ;;;###autoload
362 (defun gnus-unplugged ()
363   "Start Gnus unplugged."
364   (interactive)
365   (setq gnus-plugged nil)
366   (gnus))
367
368 ;;;###autoload
369 (defun gnus-plugged ()
370   "Start Gnus plugged."
371   (interactive)
372   (setq gnus-plugged t)
373   (gnus))
374
375 ;;;###autoload
376 (defun gnus-agentize ()
377   "Allow Gnus to be an offline newsreader.
378 The normal usage of this command is to put the following as the
379 last form in your `.gnus.el' file:
380
381 \(gnus-agentize)
382
383 This will modify the `gnus-setup-news-hook', and
384 `message-send-mail-real-function' variables, and install the Gnus agent
385 minor mode in all Gnus buffers."
386   (interactive)
387   (gnus-open-agent)
388   (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
389   (unless gnus-agent-send-mail-function
390     (setq gnus-agent-send-mail-function (or
391                                          message-send-mail-real-function
392                                          message-send-mail-function)
393           message-send-mail-real-function 'gnus-agent-send-mail))
394   (unless gnus-agent-covered-methods
395     (setq gnus-agent-covered-methods (list gnus-select-method))))
396
397 (defun gnus-agent-queue-setup ()
398   "Make sure the queue group exists."
399   (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
400     (gnus-request-create-group "queue" '(nndraft ""))
401     (let ((gnus-level-default-subscribed 1))
402       (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
403     (gnus-group-set-parameter
404      "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
405
406 (defun gnus-agent-send-mail ()
407   (if gnus-plugged
408       (funcall gnus-agent-send-mail-function)
409     (goto-char (point-min))
410     (re-search-forward
411      (concat "^" (regexp-quote mail-header-separator) "\n"))
412     (replace-match "\n")
413     (gnus-agent-insert-meta-information 'mail)
414     (gnus-request-accept-article "nndraft:queue" nil t t)))
415
416 (defun gnus-agent-insert-meta-information (type &optional method)
417   "Insert meta-information into the message that says how it's to be posted.
418 TYPE can be either `mail' or `news'.  If the latter, then METHOD can
419 be a select method."
420   (save-excursion
421     (message-remove-header gnus-agent-meta-information-header)
422     (goto-char (point-min))
423     (insert gnus-agent-meta-information-header ": "
424             (symbol-name type) " " (format "%S" method)
425             "\n")
426     (forward-char -1)
427     (while (search-backward "\n" nil t)
428       (replace-match "\\n" t t))))
429
430 (defun gnus-agent-restore-gcc ()
431   "Restore GCC field from saved header."
432   (save-excursion
433     (goto-char (point-min))
434     (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
435       (replace-match "Gcc:" 'fixedcase))))
436
437 (defun gnus-agent-any-covered-gcc ()
438   (save-restriction
439     (message-narrow-to-headers)
440     (let* ((gcc (mail-fetch-field "gcc" nil t))
441            (methods (and gcc
442                          (mapcar 'gnus-inews-group-method
443                                  (message-unquote-tokens
444                                   (message-tokenize-header
445                                    gcc " ,")))))
446            covered)
447       (while (and (not covered) methods)
448         (setq covered
449               (member (car methods) gnus-agent-covered-methods)
450               methods (cdr methods)))
451       covered)))
452
453 (defun gnus-agent-possibly-save-gcc ()
454   "Save GCC if Gnus is unplugged."
455   (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
456     (save-excursion
457       (goto-char (point-min))
458       (let ((case-fold-search t))
459         (while (re-search-forward "^gcc:" nil t)
460           (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
461
462 (defun gnus-agent-possibly-do-gcc ()
463   "Do GCC if Gnus is plugged."
464   (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
465     (gnus-inews-do-gcc)))
466
467 ;;;
468 ;;; Group mode commands
469 ;;;
470
471 (defun gnus-agent-fetch-groups (n)
472   "Put all new articles in the current groups into the Agent."
473   (interactive "P")
474   (unless gnus-plugged
475     (error "Groups can't be fetched when Gnus is unplugged"))
476   (gnus-group-iterate n 'gnus-agent-fetch-group))
477
478 (defun gnus-agent-fetch-group (group)
479   "Put all new articles in GROUP into the Agent."
480   (interactive (list (gnus-group-group-name)))
481   (let ((state gnus-plugged))
482     (unwind-protect
483         (progn
484           (unless state
485             (gnus-agent-toggle-plugged gnus-plugged)
486             (unless group
487               (error "No group on the current line"))
488             (let ((gnus-command-method (gnus-find-method-for-group group)))
489               (gnus-agent-with-fetch
490                 (gnus-agent-fetch-group-1 group gnus-command-method)
491                 (gnus-message 5 "Fetching %s...done" group)))))
492       (when (and (not state)
493                  gnus-plugged)
494         (gnus-agent-toggle-plugged gnus-plugged)))))
495
496 (defun gnus-agent-add-group (category arg)
497   "Add the current group to an agent category."
498   (interactive
499    (list
500     (intern
501      (completing-read
502       "Add to category: "
503       (mapcar (lambda (cat) (list (symbol-name (car cat))))
504               gnus-category-alist)
505       nil t))
506     current-prefix-arg))
507   (let ((cat (assq category gnus-category-alist))
508         c groups)
509     (gnus-group-iterate arg
510       (lambda (group)
511         (when (cadddr (setq c (gnus-group-category group)))
512           (setf (cadddr c) (delete group (cadddr c))))
513         (push group groups)))
514     (setf (cadddr cat) (nconc (cadddr cat) groups))
515     (gnus-category-write)))
516
517 (defun gnus-agent-remove-group (arg)
518   "Remove the current group from its agent category, if any."
519   (interactive "P")
520   (let (c)
521     (gnus-group-iterate arg
522       (lambda (group)
523         (when (cadddr (setq c (gnus-group-category group)))
524           (setf (cadddr c) (delete group (cadddr c))))))
525     (gnus-category-write)))
526
527 (defun gnus-agent-synchronize-flags ()
528   "Synchronize unplugged flags with servers."
529   (interactive)
530   (save-excursion
531     (dolist (gnus-command-method gnus-agent-covered-methods)
532       (when (file-exists-p (gnus-agent-lib-file "flags"))
533         (gnus-agent-synchronize-flags-server gnus-command-method)))))
534
535 (defun gnus-agent-possibly-synchronize-flags ()
536   "Synchronize flags according to `gnus-agent-synchronize-flags'."
537   (interactive)
538   (save-excursion
539     (dolist (gnus-command-method gnus-agent-covered-methods)
540       (when (file-exists-p (gnus-agent-lib-file "flags"))
541         (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
542
543 (defun gnus-agent-synchronize-flags-server (method)
544   "Synchronize flags set when unplugged for server."
545   (let ((gnus-command-method method))
546     (when (file-exists-p (gnus-agent-lib-file "flags"))
547       (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
548       (erase-buffer)
549       (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
550       (if (null (gnus-check-server gnus-command-method))
551           (message "Couldn't open server %s" (nth 1 gnus-command-method))
552         (while (not (eobp))
553           (if (null (eval (read (current-buffer))))
554               (progn (forward-line)
555                      (kill-line -1))
556             (write-file (gnus-agent-lib-file "flags"))
557             (error "Couldn't set flags from file %s"
558                    (gnus-agent-lib-file "flags"))))
559         (delete-file (gnus-agent-lib-file "flags")))
560       (kill-buffer nil))))
561
562 (defun gnus-agent-possibly-synchronize-flags-server (method)
563   "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
564   (when (or (and gnus-agent-synchronize-flags
565                  (not (eq gnus-agent-synchronize-flags 'ask)))
566             (and (eq gnus-agent-synchronize-flags 'ask)
567                  (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
568                                         (cadr method)))))
569     (gnus-agent-synchronize-flags-server method)))
570
571 ;;;
572 ;;; Server mode commands
573 ;;;
574
575 (defun gnus-agent-add-server (server)
576   "Enroll SERVER in the agent program."
577   (interactive (list (gnus-server-server-name)))
578   (unless server
579     (error "No server on the current line"))
580   (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
581     (when (member method gnus-agent-covered-methods)
582       (error "Server already in the agent program"))
583     (push method gnus-agent-covered-methods)
584     (gnus-server-update-server server)
585     (gnus-agent-write-servers)
586     (message "Entered %s into the Agent" server)))
587
588 (defun gnus-agent-remove-server (server)
589   "Remove SERVER from the agent program."
590   (interactive (list (gnus-server-server-name)))
591   (unless server
592     (error "No server on the current line"))
593   (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
594     (unless (member method gnus-agent-covered-methods)
595       (error "Server not in the agent program"))
596     (setq gnus-agent-covered-methods
597           (delete method gnus-agent-covered-methods))
598     (gnus-server-update-server server)
599     (gnus-agent-write-servers)
600     (message "Removed %s from the agent" server)))
601
602 (defun gnus-agent-read-servers ()
603   "Read the alist of covered servers."
604   (setq gnus-agent-covered-methods
605         (gnus-agent-read-file
606          (nnheader-concat gnus-agent-directory "lib/servers"))))
607
608 (defun gnus-agent-write-servers ()
609   "Write the alist of covered servers."
610   (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
611   (let ((coding-system-for-write nnheader-file-coding-system)
612         (output-coding-system nnheader-file-coding-system)
613         (file-name-coding-system nnmail-pathname-coding-system)
614         (pathname-coding-system nnmail-pathname-coding-system))
615     (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
616       (prin1 gnus-agent-covered-methods (current-buffer)))))
617
618 ;;;
619 ;;; Summary commands
620 ;;;
621
622 (defun gnus-agent-mark-article (n &optional unmark)
623   "Mark the next N articles as downloadable.
624 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
625 the mark instead.  The difference between N and the actual number of
626 articles marked is returned."
627   (interactive "p")
628   (let ((backward (< n 0))
629         (n (abs n)))
630     (while (and
631             (> n 0)
632             (progn
633               (gnus-summary-set-agent-mark
634                (gnus-summary-article-number) unmark)
635               (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
636       (setq n (1- n)))
637     (when (/= 0 n)
638       (gnus-message 7 "No more articles"))
639     (gnus-summary-recenter)
640     (gnus-summary-position-point)
641     n))
642
643 (defun gnus-agent-unmark-article (n)
644   "Remove the downloadable mark from the next N articles.
645 If N is negative, unmark backward instead.  The difference between N and
646 the actual number of articles unmarked is returned."
647   (interactive "p")
648   (gnus-agent-mark-article n t))
649
650 (defun gnus-agent-toggle-mark (n)
651   "Toggle the downloadable mark from the next N articles.
652 If N is negative, toggle backward instead.  The difference between N and
653 the actual number of articles toggled is returned."
654   (interactive "p")
655   (gnus-agent-mark-article n 'toggle))
656
657 (defun gnus-summary-set-agent-mark (article &optional unmark)
658   "Mark ARTICLE as downloadable."
659   (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
660                     (memq article gnus-newsgroup-downloadable)
661                   unmark)))
662     (if unmark
663         (progn
664           (setq gnus-newsgroup-downloadable
665                 (delq article gnus-newsgroup-downloadable))
666           (push article gnus-newsgroup-undownloaded))
667       (setq gnus-newsgroup-undownloaded
668             (delq article gnus-newsgroup-undownloaded))
669       (push article gnus-newsgroup-downloadable))
670     (gnus-summary-update-mark
671      (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
672      'unread)))
673
674 (defun gnus-agent-get-undownloaded-list ()
675   "Mark all unfetched articles as read."
676   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
677     (when (and (not gnus-plugged)
678                (gnus-agent-method-p gnus-command-method))
679       (gnus-agent-load-alist gnus-newsgroup-name)
680       ;; First mark all undownloaded articles as undownloaded.
681       (dolist (article (mapcar (lambda (header) (mail-header-number header))
682                                gnus-newsgroup-headers))
683         (unless (or (cdr (assq article gnus-agent-article-alist))
684                     (memq article gnus-newsgroup-downloadable)
685                     (memq article gnus-newsgroup-cached))
686           (push article gnus-newsgroup-undownloaded)))
687       ;; Then mark downloaded downloadable as not-downloadable,
688       ;; if you get my drift.
689       (dolist (article gnus-newsgroup-downloadable)
690         (when (cdr (assq article gnus-agent-article-alist))
691           (setq gnus-newsgroup-downloadable
692                 (delq article gnus-newsgroup-downloadable)))))))
693
694 (defun gnus-agent-catchup ()
695   "Mark all undownloaded articles as read."
696   (interactive)
697   (save-excursion
698     (while gnus-newsgroup-undownloaded
699       (gnus-summary-mark-article
700        (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
701   (gnus-summary-position-point))
702
703 (defun gnus-agent-summary-fetch-group ()
704   "Fetch the downloadable articles in the group."
705   (interactive)
706   (let ((articles gnus-newsgroup-downloadable)
707         (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
708         (state gnus-plugged))
709     (unwind-protect
710         (progn
711           (unless state
712             (gnus-agent-toggle-plugged t))
713           (unless articles
714             (error "No articles to download"))
715           (gnus-agent-with-fetch
716             (gnus-agent-fetch-articles gnus-newsgroup-name articles))
717           (save-excursion
718             (dolist (article articles)
719               (setq gnus-newsgroup-downloadable
720                     (delq article gnus-newsgroup-downloadable))
721               (gnus-summary-mark-article article gnus-unread-mark))))
722       (when (and (not state)
723                  gnus-plugged)
724         (gnus-agent-toggle-plugged nil)))))
725
726 ;;;
727 ;;; Internal functions
728 ;;;
729
730 (defun gnus-agent-save-active (method)
731   (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
732
733 (defun gnus-agent-save-active-1 (method function)
734   (when (gnus-agent-method-p method)
735     (let* ((gnus-command-method method)
736            (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
737            (file (gnus-agent-lib-file "active")))
738       (funcall function nil new)
739       (gnus-agent-write-active file new)
740       (erase-buffer)
741       (insert-file-contents-as-coding-system gnus-agent-file-coding-system
742                                              file))))
743
744 (defun gnus-agent-write-active (file new)
745   (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
746         (file (gnus-agent-lib-file "active"))
747         elem osym)
748     (when (file-exists-p file)
749       (with-temp-buffer
750         (insert-file-contents-as-coding-system gnus-agent-file-coding-system
751                                                file)
752         (gnus-active-to-gnus-format nil orig))
753       (mapatoms
754        (lambda (sym)
755          (when (and sym (boundp sym))
756            (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
757                     (setq elem (symbol-value osym)))
758                (setcdr elem (cdr (symbol-value sym)))
759              (set (intern (symbol-name sym) orig) (symbol-value sym)))))
760        new))
761     (gnus-make-directory (file-name-directory file))
762     ;; The hashtable contains real names of groups,  no more prefix
763     ;; removing, so set `full' to `t'.
764     (gnus-write-active-file file orig t)))
765
766 (defun gnus-agent-save-groups (method)
767   (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
768
769 (defun gnus-agent-save-group-info (method group active)
770   (when (gnus-agent-method-p method)
771     (let* ((gnus-command-method method)
772            (coding-system-for-write nnheader-file-coding-system)
773            (output-coding-system nnheader-file-coding-system)
774            (file-name-coding-system nnmail-pathname-coding-system)
775            (pathname-coding-system nnmail-pathname-coding-system)
776            (file (gnus-agent-lib-file "active"))
777            oactive-min)
778       (gnus-make-directory (file-name-directory file))
779       (with-temp-file file
780         (when (file-exists-p file)
781           (nnheader-insert-file-contents file))
782         (goto-char (point-min))
783         (when (re-search-forward
784                (concat "^" (regexp-quote group) " ") nil t)
785           (save-excursion
786             (read (current-buffer))                      ;; max
787             (setq oactive-min (read (current-buffer))))  ;; min
788           (gnus-delete-line))
789         (insert (format "%S %d %d y\n" (intern group)
790                         (cdr active)
791                         (or oactive-min (car active))))
792         (goto-char (point-max))
793         (while (search-backward "\\." nil t)
794           (delete-char 1))))))
795
796 (defun gnus-agent-group-path (group)
797   "Translate GROUP into a path."
798   (if nnmail-use-long-file-names
799       (gnus-group-real-name group)
800     (nnheader-translate-file-chars
801      (nnheader-replace-chars-in-string
802       (nnheader-replace-duplicate-chars-in-string
803        (nnheader-replace-chars-in-string
804         (gnus-group-real-name group)
805         ?/ ?_)
806        ?. ?_)
807       ?. ?/))))
808
809 \f
810
811 (defun gnus-agent-method-p (method)
812   "Say whether METHOD is covered by the agent."
813   (member method gnus-agent-covered-methods))
814
815 (defun gnus-agent-get-function (method)
816   (if (and (not gnus-plugged)
817            (gnus-agent-method-p method))
818       (progn
819         (require 'nnagent)
820         'nnagent)
821     (car method)))
822
823 ;;; History functions
824
825 (defun gnus-agent-history-buffer ()
826   (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
827
828 (defun gnus-agent-open-history ()
829   (save-excursion
830     (push (cons (gnus-agent-method)
831                 (set-buffer (gnus-get-buffer-create
832                              (format " *Gnus agent %s history*"
833                                      (gnus-agent-method)))))
834           gnus-agent-history-buffers)
835     (erase-buffer)
836     (insert "\n")
837     (let ((file (gnus-agent-lib-file "history")))
838       (when (file-exists-p file)
839         (nnheader-insert-file-contents file))
840       (set (make-local-variable 'gnus-agent-file-name) file))))
841
842 (defun gnus-agent-save-history ()
843   (save-excursion
844     (set-buffer gnus-agent-current-history)
845     (gnus-make-directory (file-name-directory gnus-agent-file-name))
846     (write-region-as-coding-system
847      gnus-agent-file-coding-system
848      (1+ (point-min)) (point-max) gnus-agent-file-name nil 'silent)))
849
850 (defun gnus-agent-close-history ()
851   (when (gnus-buffer-live-p gnus-agent-current-history)
852     (kill-buffer gnus-agent-current-history)
853     (setq gnus-agent-history-buffers
854           (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
855                 gnus-agent-history-buffers))))
856
857 (defun gnus-agent-enter-history (id group-arts date)
858   (save-excursion
859     (set-buffer gnus-agent-current-history)
860     (goto-char (point-max))
861     (let ((p (point)))
862       (insert id "\t" (number-to-string date) "\t")
863       (while group-arts
864         (insert (format "%S" (intern (caar group-arts)))
865                 " " (number-to-string (cdr (pop group-arts)))
866                 " "))
867       (insert "\n")
868       (while (search-backward "\\." p t)
869         (delete-char 1)))))
870
871 (defun gnus-agent-article-in-history-p (id)
872   (save-excursion
873     (set-buffer (gnus-agent-history-buffer))
874     (goto-char (point-min))
875     (search-forward (concat "\n" id "\t") nil t)))
876
877 (defun gnus-agent-history-path (id)
878   (save-excursion
879     (set-buffer (gnus-agent-history-buffer))
880     (goto-char (point-min))
881     (when (search-forward (concat "\n" id "\t") nil t)
882       (let ((method (gnus-agent-method)))
883         (let (paths group)
884           (while (not (numberp (setq group (read (current-buffer)))))
885             (push (concat method "/" group) paths))
886           (nreverse paths))))))
887
888 ;;;
889 ;;; Fetching
890 ;;;
891
892 (defun gnus-agent-fetch-articles (group articles)
893   "Fetch ARTICLES from GROUP and put them into the Agent."
894   (when articles
895     ;; Prune off articles that we have already fetched.
896     (while (and articles
897                 (cdr (assq (car articles) gnus-agent-article-alist)))
898       (pop articles))
899     (let ((arts articles))
900       (while (cdr arts)
901         (if (cdr (assq (cadr arts) gnus-agent-article-alist))
902             (setcdr arts (cddr arts))
903           (setq arts (cdr arts)))))
904     (when articles
905       (let ((dir (concat
906                   (gnus-agent-directory)
907                   (gnus-agent-group-path group) "/"))
908             (date (time-to-days (current-time)))
909             (case-fold-search t)
910             pos crosses id elem)
911         (gnus-make-directory dir)
912         (gnus-message 7 "Fetching articles for %s..." group)
913         ;; Fetch the articles from the backend.
914         (if (gnus-check-backend-function 'retrieve-articles group)
915             (setq pos (gnus-retrieve-articles articles group))
916           (with-temp-buffer
917             (let (article)
918               (while (setq article (pop articles))
919                 (when (or
920                        (gnus-backlog-request-article group article
921                                                      nntp-server-buffer)
922                        (gnus-request-article article group))
923                   (goto-char (point-max))
924                   (push (cons article (point)) pos)
925                   (insert-buffer-substring nntp-server-buffer)))
926               (copy-to-buffer nntp-server-buffer (point-min) (point-max))
927               (setq pos (nreverse pos)))))
928         ;; Then save these articles into the Agent.
929         (save-excursion
930           (set-buffer nntp-server-buffer)
931           (while pos
932             (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
933             (goto-char (point-min))
934             (when (search-forward "\n\n" nil t)
935               (when (search-backward "\nXrefs: " nil t)
936                 ;; Handle crossposting.
937                 (skip-chars-forward "^ ")
938                 (skip-chars-forward " ")
939                 (setq crosses nil)
940                 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
941                   (push (cons (buffer-substring (match-beginning 1)
942                                                 (match-end 1))
943                               (buffer-substring (match-beginning 2)
944                                                 (match-end 2)))
945                         crosses)
946                   (goto-char (match-end 0)))
947                 (gnus-agent-crosspost crosses (caar pos))))
948             (goto-char (point-min))
949             (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
950                 (setq id "No-Message-ID-in-article")
951               (setq id (buffer-substring (match-beginning 1) (match-end 1))))
952             (write-region-as-coding-system
953              gnus-agent-file-coding-system
954              (point-min) (point-max)
955              (concat dir (number-to-string (caar pos))) nil 'silent)
956             (when (setq elem (assq (caar pos) gnus-agent-article-alist))
957               (setcdr elem t))
958             (gnus-agent-enter-history
959              id (or crosses (list (cons group (caar pos)))) date)
960             (widen)
961             (pop pos)))
962         (gnus-agent-save-alist group)))))
963
964 (defun gnus-agent-crosspost (crosses article)
965   (let (gnus-agent-article-alist group alist beg end)
966     (save-excursion
967       (set-buffer gnus-agent-overview-buffer)
968       (when (nnheader-find-nov-line article)
969         (forward-word 1)
970         (setq beg (point))
971         (setq end (progn (forward-line 1) (point)))))
972     (while crosses
973       (setq group (caar crosses))
974       (unless (setq alist (assoc group gnus-agent-group-alist))
975         (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
976               gnus-agent-group-alist))
977       (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
978       (save-excursion
979         (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
980                                                     group)))
981         (when (= (point-max) (point-min))
982           (push (cons group (current-buffer)) gnus-agent-buffer-alist)
983           (ignore-errors
984             (nnheader-insert-file-contents
985              (gnus-agent-article-name ".overview" group))))
986         (nnheader-find-nov-line (string-to-number (cdar crosses)))
987         (insert (string-to-number (cdar crosses)))
988         (insert-buffer-substring gnus-agent-overview-buffer beg end))
989       (pop crosses))))
990
991 (defun gnus-agent-flush-cache ()
992   (save-excursion
993     (while gnus-agent-buffer-alist
994       (set-buffer (cdar gnus-agent-buffer-alist))
995       (write-region-as-coding-system
996        gnus-agent-file-coding-system
997        (point-min) (point-max)
998        (gnus-agent-article-name ".overview"
999                                 (caar gnus-agent-buffer-alist))
1000        nil 'silent)
1001       (pop gnus-agent-buffer-alist))
1002     (while gnus-agent-group-alist
1003       (with-temp-file (caar gnus-agent-group-alist)
1004         (princ (cdar gnus-agent-group-alist))
1005         (insert "\n"))
1006       (pop gnus-agent-group-alist))))
1007
1008 (defun gnus-agent-fetch-headers (group &optional force)
1009   (let* ((articles (gnus-list-of-unread-articles group))
1010          (len (length articles))
1011          (gnus-decode-encoded-word-function 'identity)
1012          (file (gnus-agent-article-name ".overview" group))
1013          i)
1014     ;; Check the number of articles is not too large.
1015     (when (and (integerp gnus-agent-large-newsgroup)
1016                (< 0 gnus-agent-large-newsgroup))
1017       (and (< 0 (setq i (- len gnus-agent-large-newsgroup)))
1018            (setq articles (nthcdr i articles))))
1019     ;; add article with marks to list of article headers we want to fetch.
1020     (dolist (arts (gnus-info-marks (gnus-get-info group)))
1021       (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
1022                                  articles)))
1023     (setq articles (sort articles '<))
1024     ;; Remove known articles.
1025     (when (gnus-agent-load-alist group)
1026       (setq articles (gnus-sorted-intersection
1027                       articles
1028                       (gnus-uncompress-range
1029                        (cons (1+ (caar (last gnus-agent-article-alist)))
1030                              (cdr (gnus-active group)))))))
1031     ;; Fetch them.
1032     (gnus-make-directory (nnheader-translate-file-chars
1033                           (file-name-directory file) t))
1034     (when articles
1035       (gnus-message 7 "Fetching headers for %s..." group)
1036       (save-excursion
1037         (set-buffer nntp-server-buffer)
1038         (unless (eq 'nov (gnus-retrieve-headers articles group))
1039           (nnvirtual-convert-headers))
1040         ;; Save these headers for later processing.
1041         (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1042         (when (file-exists-p file)
1043           (gnus-agent-braid-nov group articles file))
1044         (write-region-as-coding-system
1045          gnus-agent-file-coding-system
1046          (point-min) (point-max) file nil 'silent)
1047         (gnus-agent-save-alist group articles nil)
1048         (gnus-agent-enter-history
1049          "last-header-fetched-for-session"
1050          (list (cons group (nth (- (length  articles) 1) articles)))
1051          (time-to-days (current-time)))
1052         articles))))
1053
1054 (defsubst gnus-agent-copy-nov-line (article)
1055   (let (b e)
1056     (set-buffer gnus-agent-overview-buffer)
1057     (setq b (point))
1058     (if (eq article (read (current-buffer)))
1059         (setq e (progn (forward-line 1) (point)))
1060       (progn
1061         (beginning-of-line)
1062         (setq e b)))
1063     (set-buffer nntp-server-buffer)
1064     (insert-buffer-substring gnus-agent-overview-buffer b e)))
1065
1066 (defun gnus-agent-braid-nov (group articles file)
1067   (set-buffer gnus-agent-overview-buffer)
1068   (goto-char (point-min))
1069   (set-buffer nntp-server-buffer)
1070   (erase-buffer)
1071   (nnheader-insert-file-contents file)
1072   (goto-char (point-max))
1073   (if (or (= (point-min) (point-max))
1074           (progn
1075             (forward-line -1)
1076             (< (read (current-buffer)) (car articles))))
1077       ;; We have only headers that are after the older headers,
1078       ;; so we just append them.
1079       (progn
1080         (goto-char (point-max))
1081         (insert-buffer-substring gnus-agent-overview-buffer))
1082     ;; We do it the hard way.
1083     (nnheader-find-nov-line (car articles))
1084     (gnus-agent-copy-nov-line (car articles))
1085     (pop articles)
1086     (while (and articles
1087                 (not (eobp)))
1088       (while (and (not (eobp))
1089                   (< (read (current-buffer)) (car articles)))
1090         (forward-line 1))
1091       (beginning-of-line)
1092       (unless (eobp)
1093         (gnus-agent-copy-nov-line (car articles))
1094         (setq articles (cdr articles))))
1095     (when articles
1096       (let (b e)
1097         (set-buffer gnus-agent-overview-buffer)
1098         (setq b (point)
1099               e (point-max))
1100         (set-buffer nntp-server-buffer)
1101         (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1102
1103 (defun gnus-agent-load-alist (group &optional dir)
1104   "Load the article-state alist for GROUP."
1105   (setq gnus-agent-article-alist
1106         (gnus-agent-read-file
1107          (if dir
1108              (expand-file-name ".agentview" dir)
1109            (gnus-agent-article-name ".agentview" group)))))
1110
1111 (defun gnus-agent-save-alist (group &optional articles state dir)
1112   "Save the article-state alist for GROUP."
1113   (let ((file-name-coding-system nnmail-pathname-coding-system)
1114         (pathname-coding-system nnmail-pathname-coding-system)
1115         print-level print-length)
1116     (with-temp-file (if dir
1117                         (expand-file-name ".agentview" dir)
1118                       (gnus-agent-article-name ".agentview" group))
1119       (princ (setq gnus-agent-article-alist
1120                    (nconc gnus-agent-article-alist
1121                           (mapcar (lambda (article) (cons article state))
1122                                   articles)))
1123              (current-buffer))
1124       (insert "\n"))))
1125
1126 (defun gnus-agent-article-name (article group)
1127   (expand-file-name (if (stringp article) article (string-to-number article))
1128                     (file-name-as-directory
1129                      (expand-file-name (gnus-agent-group-path group)
1130                                        (gnus-agent-directory)))))
1131
1132 ;;;###autoload
1133 (defun gnus-agent-batch-fetch ()
1134   "Start Gnus and fetch session."
1135   (interactive)
1136   (gnus)
1137   (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1138     (gnus-agent-fetch-session))
1139   (gnus-group-exit))
1140
1141 (defun gnus-agent-fetch-session ()
1142   "Fetch all articles and headers that are eligible for fetching."
1143   (interactive)
1144   (unless gnus-agent-covered-methods
1145     (error "No servers are covered by the Gnus agent"))
1146   (unless gnus-plugged
1147     (error "Can't fetch articles while Gnus is unplugged"))
1148   (let ((methods gnus-agent-covered-methods)
1149         groups group gnus-command-method)
1150     (save-excursion
1151       (while methods
1152         (condition-case err
1153             (progn
1154               (setq gnus-command-method (car methods))
1155               (when (or (gnus-server-opened gnus-command-method)
1156                         (gnus-open-server gnus-command-method))
1157                 (setq groups (gnus-groups-from-server (car methods)))
1158                 (gnus-agent-with-fetch
1159                   (while (setq group (pop groups))
1160                     (when (<= (gnus-group-level group) gnus-agent-handle-level)
1161                       (gnus-agent-fetch-group-1 group gnus-command-method))))))
1162           (error
1163            (unless (funcall gnus-agent-confirmation-function
1164                             (format "Error (%s).  Continue? " err))
1165              (error "Cannot fetch articles into the Gnus agent")))
1166           (quit
1167            (unless (funcall gnus-agent-confirmation-function
1168                             (format "Quit fetching session (%s).  Continue? "
1169                                     err))
1170              (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1171         (pop methods))
1172       (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1173
1174 (defun gnus-agent-fetch-group-1 (group method)
1175   "Fetch GROUP."
1176   (let ((gnus-command-method method)
1177         (gnus-newsgroup-name group)
1178         gnus-newsgroup-dependencies gnus-newsgroup-headers
1179         gnus-newsgroup-scored gnus-headers gnus-score
1180         gnus-use-cache articles arts
1181         category predicate info marks score-param
1182         (gnus-summary-expunge-below gnus-summary-expunge-below)
1183         (gnus-summary-mark-below gnus-summary-mark-below)
1184         (gnus-orphan-score gnus-orphan-score)
1185         ;; Maybe some other gnus-summary local variables should also
1186         ;; be put here.
1187         )
1188     (unless (gnus-check-group group)
1189       (error "Can't open server for %s" group))
1190     ;; Fetch headers.
1191     (when (and (or (gnus-active group)
1192                    (gnus-activate-group group))
1193                (setq articles (gnus-agent-fetch-headers group))
1194                (let ((nntp-server-buffer gnus-agent-overview-buffer))
1195                  ;; Parse them and see which articles we want to fetch.
1196                  (setq gnus-newsgroup-dependencies
1197                        (make-vector (length articles) 0))
1198                  (setq gnus-newsgroup-headers
1199                        (gnus-get-newsgroup-headers-xover articles nil nil
1200                                                          group))
1201                  ;; `gnus-agent-overview-buffer' may be killed for
1202                  ;; timeout reason.  If so, recreate it.
1203                  (gnus-agent-create-buffer)))
1204       (setq category (gnus-group-category group))
1205       (setq predicate
1206             (gnus-get-predicate
1207              (or (gnus-group-find-parameter group 'agent-predicate t)
1208                  (cadr category))))
1209       (if (memq predicate '(gnus-agent-true gnus-agent-false))
1210           ;; Simple implementation
1211           (setq arts (and (eq predicate 'gnus-agent-true) articles))
1212         (setq arts nil)
1213         (setq score-param
1214               (or (gnus-group-get-parameter group 'agent-score t)
1215                   (caddr category)))
1216         ;; Translate score-param into real one
1217         (cond
1218          ((not score-param))
1219          ((eq score-param 'file)
1220           (setq score-param (gnus-all-score-files group)))
1221          ((stringp (car score-param)))
1222          (t
1223           (setq score-param (list (list score-param)))))
1224         (when score-param
1225           (gnus-score-headers score-param))
1226         (while (setq gnus-headers (pop gnus-newsgroup-headers))
1227           (setq gnus-score
1228                 (or (cdr (assq (mail-header-number gnus-headers)
1229                                gnus-newsgroup-scored))
1230                     gnus-summary-default-score))
1231           (when (funcall predicate)
1232             (push (mail-header-number gnus-headers)
1233                   arts))))
1234       ;; Fetch the articles.
1235       (when arts
1236         (gnus-agent-fetch-articles group arts)))
1237     ;; Perhaps we have some additional articles to fetch.
1238     (setq arts (assq 'download (gnus-info-marks
1239                                 (setq info (gnus-get-info group)))))
1240     (when (cdr arts)
1241       (gnus-agent-fetch-articles
1242        group (gnus-uncompress-range (cdr arts)))
1243       (setq marks (delq arts (gnus-info-marks info)))
1244       (gnus-info-set-marks info marks)
1245       (gnus-dribble-enter
1246        (concat "(gnus-group-set-info '"
1247                (gnus-prin1-to-string info)
1248                ")")))))
1249
1250 ;;;
1251 ;;; Agent Category Mode
1252 ;;;
1253
1254 (defvar gnus-category-mode-hook nil
1255   "Hook run in `gnus-category-mode' buffers.")
1256
1257 (defvar gnus-category-line-format "     %(%20c%): %g\n"
1258   "Format of category lines.")
1259
1260 (defvar gnus-category-mode-line-format "Gnus: %%b"
1261   "The format specification for the category mode line.")
1262
1263 (defvar gnus-agent-short-article 100
1264   "Articles that have fewer lines than this are short.")
1265
1266 (defvar gnus-agent-long-article 200
1267   "Articles that have more lines than this are long.")
1268
1269 (defvar gnus-agent-low-score 0
1270   "Articles that have a score lower than this have a low score.")
1271
1272 (defvar gnus-agent-high-score 0
1273   "Articles that have a score higher than this have a high score.")
1274
1275
1276 ;;; Internal variables.
1277
1278 (defvar gnus-category-buffer "*Agent Category*")
1279
1280 (defvar gnus-category-line-format-alist
1281   `((?c gnus-tmp-name ?s)
1282     (?g gnus-tmp-groups ?d)))
1283
1284 (defvar gnus-category-mode-line-format-alist
1285   `((?u user-defined ?s)))
1286
1287 (defvar gnus-category-line-format-spec nil)
1288 (defvar gnus-category-mode-line-format-spec nil)
1289
1290 (defvar gnus-category-mode-map nil)
1291 (put 'gnus-category-mode 'mode-class 'special)
1292
1293 (unless gnus-category-mode-map
1294   (setq gnus-category-mode-map (make-sparse-keymap))
1295   (suppress-keymap gnus-category-mode-map)
1296
1297   (gnus-define-keys gnus-category-mode-map
1298     "q" gnus-category-exit
1299     "k" gnus-category-kill
1300     "c" gnus-category-copy
1301     "a" gnus-category-add
1302     "p" gnus-category-edit-predicate
1303     "g" gnus-category-edit-groups
1304     "s" gnus-category-edit-score
1305     "l" gnus-category-list
1306
1307     "\C-c\C-i" gnus-info-find-node
1308     "\C-c\C-b" gnus-bug))
1309
1310 (defvar gnus-category-menu-hook nil
1311   "*Hook run after the creation of the menu.")
1312
1313 (defun gnus-category-make-menu-bar ()
1314   (gnus-turn-off-edit-menu 'category)
1315   (unless (boundp 'gnus-category-menu)
1316     (easy-menu-define
1317      gnus-category-menu gnus-category-mode-map ""
1318      '("Categories"
1319        ["Add" gnus-category-add t]
1320        ["Kill" gnus-category-kill t]
1321        ["Copy" gnus-category-copy t]
1322        ["Edit predicate" gnus-category-edit-predicate t]
1323        ["Edit score" gnus-category-edit-score t]
1324        ["Edit groups" gnus-category-edit-groups t]
1325        ["Exit" gnus-category-exit t]))
1326
1327     (gnus-run-hooks 'gnus-category-menu-hook)))
1328
1329 (defun gnus-category-mode ()
1330   "Major mode for listing and editing agent categories.
1331
1332 All normal editing commands are switched off.
1333 \\<gnus-category-mode-map>
1334 For more in-depth information on this mode, read the manual
1335 (`\\[gnus-info-find-node]').
1336
1337 The following commands are available:
1338
1339 \\{gnus-category-mode-map}"
1340   (interactive)
1341   (when (gnus-visual-p 'category-menu 'menu)
1342     (gnus-category-make-menu-bar))
1343   (kill-all-local-variables)
1344   (gnus-simplify-mode-line)
1345   (setq major-mode 'gnus-category-mode)
1346   (setq mode-name "Category")
1347   (gnus-set-default-directory)
1348   (setq mode-line-process nil)
1349   (use-local-map gnus-category-mode-map)
1350   (buffer-disable-undo)
1351   (setq truncate-lines t)
1352   (setq buffer-read-only t)
1353   (gnus-run-hooks 'gnus-category-mode-hook))
1354
1355 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1356
1357 (defun gnus-category-insert-line (category)
1358   (let* ((gnus-tmp-name (car category))
1359          (gnus-tmp-groups (length (cadddr category))))
1360     (beginning-of-line)
1361     (gnus-add-text-properties
1362      (point)
1363      (prog1 (1+ (point))
1364        ;; Insert the text.
1365        (eval gnus-category-line-format-spec))
1366      (list 'gnus-category gnus-tmp-name))))
1367
1368 (defun gnus-enter-category-buffer ()
1369   "Go to the Category buffer."
1370   (interactive)
1371   (gnus-category-setup-buffer)
1372   (gnus-configure-windows 'category)
1373   (gnus-category-prepare))
1374
1375 (defun gnus-category-setup-buffer ()
1376   (unless (get-buffer gnus-category-buffer)
1377     (save-excursion
1378       (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1379       (gnus-category-mode))))
1380
1381 (defun gnus-category-prepare ()
1382   (gnus-set-format 'category-mode)
1383   (gnus-set-format 'category t)
1384   (let ((alist gnus-category-alist)
1385         (buffer-read-only nil))
1386     (erase-buffer)
1387     (while alist
1388       (gnus-category-insert-line (pop alist)))
1389     (goto-char (point-min))
1390     (gnus-category-position-point)))
1391
1392 (defun gnus-category-name ()
1393   (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1394       (error "No category on the current line")))
1395
1396 (defun gnus-category-read ()
1397   "Read the category alist."
1398   (setq gnus-category-alist
1399         (or (gnus-agent-read-file
1400              (nnheader-concat gnus-agent-directory "lib/categories"))
1401             (list (list 'default 'short nil nil)))))
1402
1403 (defun gnus-category-write ()
1404   "Write the category alist."
1405   (setq gnus-category-predicate-cache nil
1406         gnus-category-group-cache nil)
1407   (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1408   (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1409     (prin1 gnus-category-alist (current-buffer))))
1410
1411 (defun gnus-category-edit-predicate (category)
1412   "Edit the predicate for CATEGORY."
1413   (interactive (list (gnus-category-name)))
1414   (let ((info (assq category gnus-category-alist)))
1415     (gnus-edit-form
1416      (cadr info) (format "Editing the predicate for category %s" category)
1417      `(lambda (predicate)
1418         (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1419         (gnus-category-write)
1420         (gnus-category-list)))))
1421
1422 (defun gnus-category-edit-score (category)
1423   "Edit the score expression for CATEGORY."
1424   (interactive (list (gnus-category-name)))
1425   (let ((info (assq category gnus-category-alist)))
1426     (gnus-edit-form
1427      (caddr info)
1428      (format "Editing the score expression for category %s" category)
1429      `(lambda (groups)
1430         (setcar (nthcdr 2 (assq ',category gnus-category-alist)) groups)
1431         (gnus-category-write)
1432         (gnus-category-list)))))
1433
1434 (defun gnus-category-edit-groups (category)
1435   "Edit the group list for CATEGORY."
1436   (interactive (list (gnus-category-name)))
1437   (let ((info (assq category gnus-category-alist)))
1438     (gnus-edit-form
1439      (cadddr info) (format "Editing the group list for category %s" category)
1440      `(lambda (groups)
1441         (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1442         (gnus-category-write)
1443         (gnus-category-list)))))
1444
1445 (defun gnus-category-kill (category)
1446   "Kill the current category."
1447   (interactive (list (gnus-category-name)))
1448   (let ((info (assq category gnus-category-alist))
1449         (buffer-read-only nil))
1450     (gnus-delete-line)
1451     (setq gnus-category-alist (delq info gnus-category-alist))
1452     (gnus-category-write)))
1453
1454 (defun gnus-category-copy (category to)
1455   "Copy the current category."
1456   (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1457   (let ((info (assq category gnus-category-alist)))
1458     (push (list to (gnus-copy-sequence (cadr info))
1459                 (gnus-copy-sequence (caddr info)) nil)
1460           gnus-category-alist)
1461     (gnus-category-write)
1462     (gnus-category-list)))
1463
1464 (defun gnus-category-add (category)
1465   "Create a new category."
1466   (interactive "SCategory name: ")
1467   (when (assq category gnus-category-alist)
1468     (error "Category %s already exists" category))
1469   (push (list category 'false nil nil)
1470         gnus-category-alist)
1471   (gnus-category-write)
1472   (gnus-category-list))
1473
1474 (defun gnus-category-list ()
1475   "List all categories."
1476   (interactive)
1477   (gnus-category-prepare))
1478
1479 (defun gnus-category-exit ()
1480   "Return to the group buffer."
1481   (interactive)
1482   (kill-buffer (current-buffer))
1483   (gnus-configure-windows 'group t))
1484
1485 ;; To avoid having 8-bit characters in the source file.
1486 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1487
1488 (defvar gnus-category-predicate-alist
1489   '((spam . gnus-agent-spam-p)
1490     (short . gnus-agent-short-p)
1491     (long . gnus-agent-long-p)
1492     (low . gnus-agent-low-scored-p)
1493     (high . gnus-agent-high-scored-p)
1494     (true . gnus-agent-true)
1495     (false . gnus-agent-false))
1496   "Mapping from short score predicate symbols to predicate functions.")
1497
1498 (defun gnus-agent-spam-p ()
1499   "Say whether an article is spam or not."
1500   (unless gnus-agent-spam-hashtb
1501     (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1502   (if (not (equal (mail-header-references gnus-headers) ""))
1503       nil
1504     (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1505       (prog1
1506           (gnus-gethash string gnus-agent-spam-hashtb)
1507         (gnus-sethash string t gnus-agent-spam-hashtb)))))
1508
1509 (defun gnus-agent-short-p ()
1510   "Say whether an article is short or not."
1511   (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1512
1513 (defun gnus-agent-long-p ()
1514   "Say whether an article is long or not."
1515   (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1516
1517 (defun gnus-agent-low-scored-p ()
1518   "Say whether an article has a low score or not."
1519   (< gnus-score gnus-agent-low-score))
1520
1521 (defun gnus-agent-high-scored-p ()
1522   "Say whether an article has a high score or not."
1523   (> gnus-score gnus-agent-high-score))
1524
1525 (defun gnus-category-make-function (cat)
1526   "Make a function from category CAT."
1527   (let ((func (gnus-category-make-function-1 cat)))
1528     (if (and (= (length func) 1)
1529              (symbolp (car func)))
1530         (car func)
1531       (gnus-byte-compile `(lambda () ,func)))))
1532
1533 (defun gnus-agent-true ()
1534   "Return t."
1535   t)
1536
1537 (defun gnus-agent-false ()
1538   "Return nil."
1539   nil)
1540
1541 (defun gnus-category-make-function-1 (cat)
1542   "Make a function from category CAT."
1543   (cond
1544    ;; Functions are just returned as is.
1545    ((or (symbolp cat)
1546         (gnus-functionp cat))
1547     `(,(or (cdr (assq cat gnus-category-predicate-alist))
1548            cat)))
1549    ;; More complex category.
1550    ((consp cat)
1551     `(,(cond
1552         ((memq (car cat) '(& and))
1553          'and)
1554         ((memq (car cat) '(| or))
1555          'or)
1556         ((memq (car cat) gnus-category-not)
1557          'not))
1558       ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1559    (t
1560     (error "Unknown category type: %s" cat))))
1561
1562 (defun gnus-get-predicate (predicate)
1563   "Return the predicate for CATEGORY."
1564   (or (cdr (assoc predicate gnus-category-predicate-cache))
1565       (cdar (push (cons predicate
1566                         (gnus-category-make-function predicate))
1567                   gnus-category-predicate-cache))))
1568
1569 (defun gnus-group-category (group)
1570   "Return the category GROUP belongs to."
1571   (unless gnus-category-group-cache
1572     (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1573     (let ((cs gnus-category-alist)
1574           groups cat)
1575       (while (setq cat (pop cs))
1576         (setq groups (cadddr cat))
1577         (while groups
1578           (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1579   (or (gnus-gethash group gnus-category-group-cache)
1580       (assq 'default gnus-category-alist)))
1581
1582 (defun gnus-agent-expire ()
1583   "Expire all old articles."
1584   (interactive)
1585   (let ((methods gnus-agent-covered-methods)
1586         (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1587         gnus-command-method sym group articles
1588         history overview file histories elem art nov-file low info
1589         unreads marked article orig lowest highest)
1590     (save-excursion
1591       (setq overview (gnus-get-buffer-create " *expire overview*"))
1592       (while (setq gnus-command-method (pop methods))
1593         (when (file-exists-p (gnus-agent-lib-file "active"))
1594           (with-temp-buffer
1595             (insert-file-contents-as-coding-system
1596              gnus-agent-file-coding-system (gnus-agent-lib-file "active"))
1597             (gnus-active-to-gnus-format
1598              gnus-command-method
1599              (setq orig (gnus-make-hashtable
1600                          (count-lines (point-min) (point-max))))))
1601           (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1602             (gnus-agent-open-history)
1603             (set-buffer
1604              (setq gnus-agent-current-history
1605                    (setq history (gnus-agent-history-buffer))))
1606             (goto-char (point-min))
1607             (when (> (buffer-size) 1)
1608               (goto-char (point-min))
1609               (while (not (eobp))
1610                 (skip-chars-forward "^\t")
1611                 (if (let ((fetch-date (read (current-buffer))))
1612                       (if (numberp fetch-date)
1613                           (>  fetch-date day)
1614                         ;; History file is corrupted.
1615                         (gnus-message
1616                          5
1617                          (format "File %s is corrupted!"
1618                                  (gnus-agent-lib-file "history")))
1619                         (sit-for 1)
1620                         ;; Ignore it
1621                         t))
1622                     ;; New article; we don't expire it.
1623                     (forward-line 1)
1624                   ;; Old article.  Schedule it for possible nuking.
1625                   (while (not (eolp))
1626                     (setq sym (let ((obarray expiry-hashtb) s)
1627                                 (setq s (read (current-buffer)))
1628                                 (if (stringp s) (intern s) s)))
1629                     (if (boundp sym)
1630                         (set sym (cons (cons (read (current-buffer)) (point))
1631                                        (symbol-value sym)))
1632                       (set sym (list (cons (read (current-buffer)) (point)))))
1633                     (skip-chars-forward " "))
1634                   (forward-line 1)))
1635               ;; We now have all articles that can possibly be expired.
1636               (mapatoms
1637                (lambda (sym)
1638                  (setq group (symbol-name sym)
1639                        articles (sort (symbol-value sym) 'car-less-than-car)
1640                        low (car (gnus-active group))
1641                        info (gnus-get-info group)
1642                        unreads (ignore-errors
1643                                  (gnus-list-of-unread-articles group))
1644                        marked (nconc
1645                                (gnus-uncompress-range
1646                                 (cdr (assq 'tick (gnus-info-marks info))))
1647                                (gnus-uncompress-range
1648                                 (cdr (assq 'dormant (gnus-info-marks info))))
1649                                (gnus-uncompress-range
1650                                 (cdr (assq 'save (gnus-info-marks info))))
1651                                (gnus-uncompress-range
1652                                 (cdr (assq 'reply (gnus-info-marks info)))))
1653                        nov-file (gnus-agent-article-name ".overview" group)
1654                        lowest nil
1655                        highest nil)
1656                  (gnus-agent-load-alist group)
1657                  (gnus-message 5 "Expiring articles in %s" group)
1658                  (set-buffer overview)
1659                  (erase-buffer)
1660                  (when (file-exists-p nov-file)
1661                    (nnheader-insert-file-contents nov-file))
1662                  (goto-char (point-min))
1663                  (setq article 0)
1664                  (while (setq elem (pop articles))
1665                    (setq article (car elem))
1666                    (when (or (null low)
1667                              (< article low)
1668                              gnus-agent-expire-all
1669                              (and (not (memq article unreads))
1670                                   (not (memq article marked))))
1671                      ;; Find and nuke the NOV line.
1672                      (while (and (not (eobp))
1673                                  (or (not (numberp
1674                                            (setq art (read (current-buffer)))))
1675                                      (< art article)))
1676                        (if (and (numberp art)
1677                                 (file-exists-p
1678                                  (gnus-agent-article-name
1679                                   (number-to-string art) group)))
1680                            (progn
1681                              (unless lowest
1682                                (setq lowest art))
1683                              (setq highest art)
1684                              (forward-line 1))
1685                          ;; Remove old NOV lines that have no articles.
1686                          (gnus-delete-line)))
1687                      (if (or (eobp)
1688                              (/= art article))
1689                          (beginning-of-line)
1690                        (gnus-delete-line))
1691                      ;; Nuke the article.
1692                      (when (file-exists-p
1693                             (setq file (gnus-agent-article-name
1694                                         (number-to-string article)
1695                                         group)))
1696                        (delete-file file))
1697                      ;; Schedule the history line for nuking.
1698                      (push (cdr elem) histories)))
1699                  (gnus-make-directory (file-name-directory nov-file))
1700                  (write-region-as-coding-system
1701                   gnus-agent-file-coding-system
1702                   (point-min) (point-max) nov-file nil 'silent)
1703                  ;; Delete the unwanted entries in the alist.
1704                  (setq gnus-agent-article-alist
1705                        (sort gnus-agent-article-alist 'car-less-than-car))
1706                  (let* ((alist gnus-agent-article-alist)
1707                         (prev (cons nil alist))
1708                         (first prev)
1709                         expired)
1710                    (while (and alist
1711                                (<= (caar alist) article))
1712                      (if (or (not (cdar alist))
1713                              (not (file-exists-p
1714                                    (gnus-agent-article-name
1715                                     (number-to-string
1716                                      (caar alist))
1717                                     group))))
1718                          (progn
1719                            (push (caar alist) expired)
1720                            (setcdr prev (setq alist (cdr alist))))
1721                        (setq prev alist
1722                              alist (cdr alist))))
1723                    (setq gnus-agent-article-alist (cdr first))
1724                    (gnus-agent-save-alist group)
1725                    ;; Mark all articles up to the first article
1726                    ;; in `gnus-article-alist' as read.
1727                    (when (and info (caar gnus-agent-article-alist))
1728                      (setcar (nthcdr 2 info)
1729                              (gnus-range-add
1730                               (nth 2 info)
1731                               (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1732                    ;; Maybe everything has been expired from `gnus-article-alist'
1733                    ;; and so the above marking as read could not be conducted,
1734                    ;; or there are expired article within the range of the alist.
1735                    (when (and info
1736                               expired
1737                               (or (not (caar gnus-agent-article-alist))
1738                                   (> (car expired)
1739                                      (caar gnus-agent-article-alist))))
1740                      (setcar (nthcdr 2 info)
1741                              (gnus-add-to-range
1742                               (nth 2 info)
1743                               (nreverse expired))))
1744                    (gnus-dribble-enter
1745                     (concat "(gnus-group-set-info '"
1746                             (gnus-prin1-to-string info)
1747                             ")")))
1748                  (when lowest
1749                    (if (gnus-gethash group orig)
1750                        (setcar (gnus-gethash group orig) lowest)
1751                      (gnus-sethash group (cons lowest highest) orig))))
1752                expiry-hashtb)
1753               (set-buffer history)
1754               (setq histories (nreverse (sort histories '<)))
1755               (while histories
1756                 (goto-char (pop histories))
1757                 (gnus-delete-line))
1758               (gnus-agent-save-history)
1759               (gnus-agent-close-history)
1760               (gnus-write-active-file (gnus-agent-lib-file "active") orig))
1761             (gnus-message 4 "Expiry...done")))))))
1762
1763 ;;;###autoload
1764 (defun gnus-agent-batch ()
1765   (interactive)
1766   (let ((init-file-user "")
1767         (gnus-always-read-dribble-file t))
1768     (gnus))
1769   (gnus-group-send-drafts)
1770   (gnus-agent-fetch-session))
1771
1772 (provide 'gnus-agent)
1773
1774 ;;; gnus-agent.el ends here