1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
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.
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)
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.
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.
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
37 (if (featurep 'xemacs)
40 (require 'gnus-group))
43 (autoload 'gnus-server-update-server "gnus-srvr"))
45 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
46 "Where the Gnus agent will store its files."
50 (defcustom gnus-agent-plugged-hook nil
51 "Hook run when plugging into the network."
55 (defcustom gnus-agent-unplugged-hook nil
56 "Hook run when unplugging from the network."
60 (defcustom gnus-agent-handle-level gnus-level-subscribed
61 "Groups on levels higher than this variable will be ignored by the Agent."
65 (defcustom gnus-agent-expire-days 7
66 "Read articles older than this will be expired."
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."
76 (defcustom gnus-agent-group-mode-hook nil
77 "Hook run in Agent group minor modes."
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))
85 (defcustom gnus-agent-summary-mode-hook nil
86 "Hook run in Agent summary minor modes."
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))
94 (defcustom gnus-agent-server-mode-hook nil
95 "Hook run in Agent summary minor modes."
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))
103 (defcustom gnus-agent-confirmation-function 'y-or-n-p
104 "Function to confirm when error happens."
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."
114 :type '(choice (const nil)
115 (integer :tag "Number")))
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."
121 :type '(choice (const :tag "Always" t)
122 (const :tag "Never" nil)
123 (const :tag "Ask" ask))
126 ;;; Internal variables
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)
144 (defvar gnus-headers)
151 (defun gnus-open-agent ()
153 (gnus-agent-read-servers)
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))
160 (defun gnus-agent-create-buffer ()
161 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
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))
169 (gnus-add-shutdown 'gnus-close-agent 'gnus)
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))
179 ;;; Utility functions
182 (defun gnus-agent-read-file (file)
183 "Load FILE and do a `read' there."
186 (nnheader-insert-file-contents file)
187 (goto-char (point-min))
188 (read (current-buffer)))))
190 (defsubst gnus-agent-method ()
191 (concat (symbol-name (car gnus-command-method)) "/"
192 (if (equal (cadr gnus-command-method) "")
194 (cadr gnus-command-method))))
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)) "/"))
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)))))
207 ;;; Fetching setup functions.
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))
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)
221 (set-buffer nntp-server-buffer)
224 (defmacro gnus-agent-with-fetch (&rest forms)
227 (let ((gnus-agent-fetching t))
228 (gnus-agent-start-fetch)
230 (gnus-agent-stop-fetch)))
232 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
233 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
239 (defvar gnus-agent-mode-hook nil
240 "Hook run when installing agent mode.")
242 (defvar gnus-agent-mode nil)
243 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
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)
253 (set (make-local-variable mode) t)
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"
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)))))
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)
279 (defun gnus-agent-group-make-menu-bar ()
280 (unless (boundp 'gnus-agent-group-menu)
282 gnus-agent-group-menu gnus-agent-group-mode-map ""
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]
288 ["All" gnus-agent-fetch-session gnus-plugged]
289 ["Group" gnus-agent-fetch-group gnus-plugged])))))
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)
300 (defun gnus-agent-summary-make-menu-bar ()
301 (unless (boundp 'gnus-agent-summary-menu)
303 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
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]))))
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)
318 (defun gnus-agent-server-make-menu-bar ()
319 (unless (boundp 'gnus-agent-server-menu)
321 gnus-agent-server-menu gnus-agent-server-mode-map ""
323 ["Toggle plugged" gnus-agent-toggle-plugged t]
324 ["Add" gnus-agent-add-server t]
325 ["Remove" gnus-agent-remove-server t]))))
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))
334 (defun gnus-agent-toggle-plugged (plugged)
335 "Toggle whether Gnus is unplugged or not."
336 (interactive (list (not gnus-plugged)))
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"
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"
352 'gnus-agent-toggle-plugged)))
353 (force-mode-line-update))
355 (defun gnus-agent-close-connections ()
356 "Close all methods covered by the Gnus agent."
357 (let ((methods gnus-agent-covered-methods))
359 (gnus-close-server (pop methods)))))
362 (defun gnus-unplugged ()
363 "Start Gnus unplugged."
365 (setq gnus-plugged nil)
369 (defun gnus-plugged ()
370 "Start Gnus plugged."
372 (setq gnus-plugged t)
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:
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."
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))))
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)))))
406 (defun gnus-agent-send-mail ()
408 (funcall gnus-agent-send-mail-function)
409 (goto-char (point-min))
411 (concat "^" (regexp-quote mail-header-separator) "\n"))
413 (gnus-agent-insert-meta-information 'mail)
414 (gnus-request-accept-article "nndraft:queue" nil t t)))
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
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)
427 (while (search-backward "\n" nil t)
428 (replace-match "\\n" t t))))
430 (defun gnus-agent-restore-gcc ()
431 "Restore GCC field from saved header."
433 (goto-char (point-min))
434 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
435 (replace-match "Gcc:" 'fixedcase))))
437 (defun gnus-agent-any-covered-gcc ()
439 (message-narrow-to-headers)
440 (let* ((gcc (mail-fetch-field "gcc" nil t))
442 (mapcar 'gnus-inews-group-method
443 (message-unquote-tokens
444 (message-tokenize-header
447 (while (and (not covered) methods)
448 (setq covered (gnus-agent-method-p (car methods))
449 methods (cdr methods)))
452 (defun gnus-agent-possibly-save-gcc ()
453 "Save GCC if Gnus is unplugged."
454 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
456 (goto-char (point-min))
457 (let ((case-fold-search t))
458 (while (re-search-forward "^gcc:" nil t)
459 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
461 (defun gnus-agent-possibly-do-gcc ()
462 "Do GCC if Gnus is plugged."
463 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
464 (gnus-inews-do-gcc)))
467 ;;; Group mode commands
470 (defun gnus-agent-fetch-groups (n)
471 "Put all new articles in the current groups into the Agent."
474 (error "Groups can't be fetched when Gnus is unplugged"))
475 (gnus-group-iterate n 'gnus-agent-fetch-group))
477 (defun gnus-agent-fetch-group (group)
478 "Put all new articles in GROUP into the Agent."
479 (interactive (list (gnus-group-group-name)))
480 (let ((state gnus-plugged))
484 (error "No group on the current line"))
486 (gnus-agent-toggle-plugged gnus-plugged))
487 (let ((gnus-command-method (gnus-find-method-for-group group)))
488 (gnus-agent-with-fetch
489 (gnus-agent-fetch-group-1 group gnus-command-method)
490 (gnus-message 5 "Fetching %s...done" group))))
491 (when (and (not state)
493 (gnus-agent-toggle-plugged gnus-plugged)))))
495 (defun gnus-agent-add-group (category arg)
496 "Add the current group to an agent category."
502 (mapcar (lambda (cat) (list (symbol-name (car cat))))
506 (let ((cat (assq category gnus-category-alist))
508 (gnus-group-iterate arg
510 (when (cadddr (setq c (gnus-group-category group)))
511 (setf (cadddr c) (delete group (cadddr c))))
512 (push group groups)))
513 (setf (cadddr cat) (nconc (cadddr cat) groups))
514 (gnus-category-write)))
516 (defun gnus-agent-remove-group (arg)
517 "Remove the current group from its agent category, if any."
520 (gnus-group-iterate arg
522 (when (cadddr (setq c (gnus-group-category group)))
523 (setf (cadddr c) (delete group (cadddr c))))))
524 (gnus-category-write)))
526 (defun gnus-agent-synchronize-flags ()
527 "Synchronize unplugged flags with servers."
530 (dolist (gnus-command-method gnus-agent-covered-methods)
531 (when (file-exists-p (gnus-agent-lib-file "flags"))
532 (gnus-agent-synchronize-flags-server gnus-command-method)))))
534 (defun gnus-agent-possibly-synchronize-flags ()
535 "Synchronize flags according to `gnus-agent-synchronize-flags'."
538 (dolist (gnus-command-method gnus-agent-covered-methods)
539 (when (file-exists-p (gnus-agent-lib-file "flags"))
540 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
542 (defun gnus-agent-synchronize-flags-server (method)
543 "Synchronize flags set when unplugged for server."
544 (let ((gnus-command-method method))
545 (when (file-exists-p (gnus-agent-lib-file "flags"))
546 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
548 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
549 (if (null (gnus-check-server gnus-command-method))
550 (message "Couldn't open server %s" (nth 1 gnus-command-method))
552 (if (null (eval (read (current-buffer))))
553 (progn (forward-line)
555 (write-file (gnus-agent-lib-file "flags"))
556 (error "Couldn't set flags from file %s"
557 (gnus-agent-lib-file "flags"))))
558 (delete-file (gnus-agent-lib-file "flags")))
561 (defun gnus-agent-possibly-synchronize-flags-server (method)
562 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
563 (when (or (and gnus-agent-synchronize-flags
564 (not (eq gnus-agent-synchronize-flags 'ask)))
565 (and (eq gnus-agent-synchronize-flags 'ask)
566 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
568 (gnus-agent-synchronize-flags-server method)))
571 ;;; Server mode commands
574 (defun gnus-agent-add-server (server)
575 "Enroll SERVER in the agent program."
576 (interactive (list (gnus-server-server-name)))
578 (error "No server on the current line"))
579 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
580 (when (gnus-agent-method-p method)
581 (error "Server already in the agent program"))
582 (push method gnus-agent-covered-methods)
583 (gnus-server-update-server server)
584 (gnus-agent-write-servers)
585 (message "Entered %s into the Agent" server)))
587 (defun gnus-agent-remove-server (server)
588 "Remove SERVER from the agent program."
589 (interactive (list (gnus-server-server-name)))
591 (error "No server on the current line"))
592 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
593 (unless (gnus-agent-method-p method)
594 (error "Server not in the agent program"))
595 (setq gnus-agent-covered-methods
596 (delete method gnus-agent-covered-methods))
597 (gnus-server-update-server server)
598 (gnus-agent-write-servers)
599 (message "Removed %s from the agent" server)))
601 (defun gnus-agent-read-servers ()
602 "Read the alist of covered servers."
603 (setq gnus-agent-covered-methods
604 (gnus-agent-read-file
605 (nnheader-concat gnus-agent-directory "lib/servers"))))
607 (defun gnus-agent-write-servers ()
608 "Write the alist of covered servers."
609 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
610 (let ((coding-system-for-write nnheader-file-coding-system)
611 (output-coding-system nnheader-file-coding-system)
612 (file-name-coding-system nnmail-pathname-coding-system)
613 (pathname-coding-system nnmail-pathname-coding-system))
614 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
615 (prin1 gnus-agent-covered-methods (current-buffer)))))
621 (defun gnus-agent-mark-article (n &optional unmark)
622 "Mark the next N articles as downloadable.
623 If N is negative, mark backward instead. If UNMARK is non-nil, remove
624 the mark instead. The difference between N and the actual number of
625 articles marked is returned."
627 (let ((backward (< n 0))
632 (gnus-summary-set-agent-mark
633 (gnus-summary-article-number) unmark)
634 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
637 (gnus-message 7 "No more articles"))
638 (gnus-summary-recenter)
639 (gnus-summary-position-point)
642 (defun gnus-agent-unmark-article (n)
643 "Remove the downloadable mark from the next N articles.
644 If N is negative, unmark backward instead. The difference between N and
645 the actual number of articles unmarked is returned."
647 (gnus-agent-mark-article n t))
649 (defun gnus-agent-toggle-mark (n)
650 "Toggle the downloadable mark from the next N articles.
651 If N is negative, toggle backward instead. The difference between N and
652 the actual number of articles toggled is returned."
654 (gnus-agent-mark-article n 'toggle))
656 (defun gnus-summary-set-agent-mark (article &optional unmark)
657 "Mark ARTICLE as downloadable."
658 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
659 (memq article gnus-newsgroup-downloadable)
663 (setq gnus-newsgroup-downloadable
664 (delq article gnus-newsgroup-downloadable))
665 (push article gnus-newsgroup-undownloaded))
666 (setq gnus-newsgroup-undownloaded
667 (delq article gnus-newsgroup-undownloaded))
668 (push article gnus-newsgroup-downloadable))
669 (gnus-summary-update-mark
670 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
673 (defun gnus-agent-get-undownloaded-list ()
674 "Mark all unfetched articles as read."
675 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
676 (when (and (not gnus-plugged)
677 (gnus-agent-method-p gnus-command-method))
678 (gnus-agent-load-alist gnus-newsgroup-name)
679 ;; First mark all undownloaded articles as undownloaded.
680 (dolist (article (mapcar (lambda (header) (mail-header-number header))
681 gnus-newsgroup-headers))
682 (unless (or (cdr (assq article gnus-agent-article-alist))
683 (memq article gnus-newsgroup-downloadable)
684 (memq article gnus-newsgroup-cached))
685 (push article gnus-newsgroup-undownloaded)))
686 ;; Then mark downloaded downloadable as not-downloadable,
687 ;; if you get my drift.
688 (dolist (article gnus-newsgroup-downloadable)
689 (when (cdr (assq article gnus-agent-article-alist))
690 (setq gnus-newsgroup-downloadable
691 (delq article gnus-newsgroup-downloadable)))))))
693 (defun gnus-agent-catchup ()
694 "Mark all undownloaded articles as read."
697 (while gnus-newsgroup-undownloaded
698 (gnus-summary-mark-article
699 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
700 (gnus-summary-position-point))
702 (defun gnus-agent-summary-fetch-group ()
703 "Fetch the downloadable articles in the group."
705 (let ((articles gnus-newsgroup-downloadable)
706 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
707 (state gnus-plugged))
711 (gnus-agent-toggle-plugged t))
713 (error "No articles to download"))
714 (gnus-agent-with-fetch
715 (gnus-agent-fetch-articles gnus-newsgroup-name articles))
717 (dolist (article articles)
718 (setq gnus-newsgroup-downloadable
719 (delq article gnus-newsgroup-downloadable))
720 (gnus-summary-mark-article article gnus-unread-mark))))
721 (when (and (not state)
723 (gnus-agent-toggle-plugged nil)))))
726 ;;; Internal functions
729 (defun gnus-agent-save-active (method)
730 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
732 (defun gnus-agent-save-active-1 (method function)
733 (when (gnus-agent-method-p method)
734 (let* ((gnus-command-method method)
735 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
736 (file (gnus-agent-lib-file "active")))
737 (funcall function nil new)
738 (gnus-agent-write-active file new)
740 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
743 (defun gnus-agent-write-active (file new)
744 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
745 (file (gnus-agent-lib-file "active"))
747 (when (file-exists-p file)
749 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
751 (gnus-active-to-gnus-format nil orig))
754 (when (and sym (boundp sym))
755 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
756 (setq elem (symbol-value osym)))
757 (if (> (car elem) (car (symbol-value sym)))
758 (setcar elem (car (symbol-value sym)))
759 (setcdr elem (cdr (symbol-value sym))))
760 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
762 (gnus-make-directory (file-name-directory file))
763 ;; The hashtable contains real names of groups, no more prefix
764 ;; removing, so set `full' to `t'.
765 (gnus-write-active-file file orig t)))
767 (defun gnus-agent-save-groups (method)
768 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
770 (defun gnus-agent-save-group-info (method group active)
771 (when (gnus-agent-method-p method)
772 (let* ((gnus-command-method method)
773 (coding-system-for-write nnheader-file-coding-system)
774 (output-coding-system nnheader-file-coding-system)
775 (file-name-coding-system nnmail-pathname-coding-system)
776 (pathname-coding-system nnmail-pathname-coding-system)
777 (file (gnus-agent-lib-file "active"))
779 (gnus-make-directory (file-name-directory file))
781 (when (file-exists-p file)
782 (nnheader-insert-file-contents file))
783 (goto-char (point-min))
784 (when (re-search-forward
785 (concat "^" (regexp-quote group) " ") nil t)
787 (read (current-buffer)) ;; max
788 (setq oactive-min (read (current-buffer)))) ;; min
790 (insert (format "%S %d %d y\n" (intern group)
792 (or oactive-min (car active))))
793 (goto-char (point-max))
794 (while (search-backward "\\." nil t)
797 (defun gnus-agent-group-path (group)
798 "Translate GROUP into a path."
799 (if nnmail-use-long-file-names
800 (gnus-group-real-name group)
801 (nnheader-translate-file-chars
802 (nnheader-replace-chars-in-string
803 (nnheader-replace-duplicate-chars-in-string
804 (nnheader-replace-chars-in-string
805 (gnus-group-real-name group)
812 (defun gnus-agent-method-p (method)
813 "Say whether METHOD is covered by the agent."
814 (member method gnus-agent-covered-methods))
816 (defun gnus-agent-get-function (method)
817 (if (and (not gnus-plugged)
818 (gnus-agent-method-p method))
824 ;;; History functions
826 (defun gnus-agent-history-buffer ()
827 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
829 (defun gnus-agent-open-history ()
831 (push (cons (gnus-agent-method)
832 (set-buffer (gnus-get-buffer-create
833 (format " *Gnus agent %s history*"
834 (gnus-agent-method)))))
835 gnus-agent-history-buffers)
838 (let ((file (gnus-agent-lib-file "history")))
839 (when (file-exists-p file)
840 (nnheader-insert-file-contents file))
841 (set (make-local-variable 'gnus-agent-file-name) file))))
843 (defun gnus-agent-save-history ()
845 (set-buffer gnus-agent-current-history)
846 (gnus-make-directory (file-name-directory gnus-agent-file-name))
847 (write-region-as-coding-system
848 gnus-agent-file-coding-system
849 (1+ (point-min)) (point-max) gnus-agent-file-name nil 'silent)))
851 (defun gnus-agent-close-history ()
852 (when (gnus-buffer-live-p gnus-agent-current-history)
853 (kill-buffer gnus-agent-current-history)
854 (setq gnus-agent-history-buffers
855 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
856 gnus-agent-history-buffers))))
858 (defun gnus-agent-enter-history (id group-arts date)
860 (set-buffer gnus-agent-current-history)
861 (goto-char (point-max))
863 (insert id "\t" (number-to-string date) "\t")
865 (insert (format "%S" (intern (caar group-arts)))
866 " " (number-to-string (cdr (pop group-arts)))
869 (while (search-backward "\\." p t)
872 (defun gnus-agent-article-in-history-p (id)
874 (set-buffer (gnus-agent-history-buffer))
875 (goto-char (point-min))
876 (search-forward (concat "\n" id "\t") nil t)))
878 (defun gnus-agent-history-path (id)
880 (set-buffer (gnus-agent-history-buffer))
881 (goto-char (point-min))
882 (when (search-forward (concat "\n" id "\t") nil t)
883 (let ((method (gnus-agent-method)))
885 (while (not (numberp (setq group (read (current-buffer)))))
886 (push (concat method "/" group) paths))
887 (nreverse paths))))))
893 (defun gnus-agent-fetch-articles (group articles)
894 "Fetch ARTICLES from GROUP and put them into the Agent."
896 ;; Prune off articles that we have already fetched.
898 (cdr (assq (car articles) gnus-agent-article-alist)))
900 (let ((arts articles))
902 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
903 (setcdr arts (cddr arts))
904 (setq arts (cdr arts)))))
907 (gnus-agent-directory)
908 (gnus-agent-group-path group) "/"))
909 (date (time-to-days (current-time)))
912 (gnus-make-directory dir)
913 (gnus-message 7 "Fetching articles for %s..." group)
914 ;; Fetch the articles from the backend.
915 (if (gnus-check-backend-function 'retrieve-articles group)
916 (setq pos (gnus-retrieve-articles articles group))
919 (while (setq article (pop articles))
921 (gnus-backlog-request-article group article
923 (gnus-request-article article group))
924 (goto-char (point-max))
925 (push (cons article (point)) pos)
926 (insert-buffer-substring nntp-server-buffer)))
927 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
928 (setq pos (nreverse pos)))))
929 ;; Then save these articles into the Agent.
931 (set-buffer nntp-server-buffer)
933 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
934 (goto-char (point-min))
935 (when (search-forward "\n\n" nil t)
936 (when (search-backward "\nXrefs: " nil t)
937 ;; Handle crossposting.
938 (skip-chars-forward "^ ")
939 (skip-chars-forward " ")
941 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
942 (push (cons (buffer-substring (match-beginning 1)
944 (buffer-substring (match-beginning 2)
947 (goto-char (match-end 0)))
948 (gnus-agent-crosspost crosses (caar pos))))
949 (goto-char (point-min))
950 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
951 (setq id "No-Message-ID-in-article")
952 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
953 (write-region-as-coding-system
954 gnus-agent-file-coding-system
955 (point-min) (point-max)
956 (concat dir (number-to-string (caar pos))) nil 'silent)
957 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
959 (gnus-agent-enter-history
960 id (or crosses (list (cons group (caar pos)))) date)
963 (gnus-agent-save-alist group)))))
965 (defun gnus-agent-crosspost (crosses article)
966 (let (gnus-agent-article-alist group alist beg end)
968 (set-buffer gnus-agent-overview-buffer)
969 (when (nnheader-find-nov-line article)
972 (setq end (progn (forward-line 1) (point)))))
974 (setq group (caar crosses))
975 (unless (setq alist (assoc group gnus-agent-group-alist))
976 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
977 gnus-agent-group-alist))
978 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
980 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
982 (when (= (point-max) (point-min))
983 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
985 (nnheader-insert-file-contents
986 (gnus-agent-article-name ".overview" group))))
987 (nnheader-find-nov-line (string-to-number (cdar crosses)))
988 (insert (string-to-number (cdar crosses)))
989 (insert-buffer-substring gnus-agent-overview-buffer beg end))
992 (defun gnus-agent-flush-cache ()
994 (while gnus-agent-buffer-alist
995 (set-buffer (cdar gnus-agent-buffer-alist))
996 (write-region-as-coding-system
997 gnus-agent-file-coding-system
998 (point-min) (point-max)
999 (gnus-agent-article-name ".overview"
1000 (caar gnus-agent-buffer-alist))
1002 (pop gnus-agent-buffer-alist))
1003 (while gnus-agent-group-alist
1004 (with-temp-file (caar gnus-agent-group-alist)
1005 (princ (cdar gnus-agent-group-alist))
1007 (pop gnus-agent-group-alist))))
1009 (defun gnus-agent-fetch-headers (group &optional force)
1010 (let* ((articles (gnus-list-of-unread-articles group))
1011 (len (length articles))
1012 (gnus-decode-encoded-word-function 'identity)
1013 (file (gnus-agent-article-name ".overview" group))
1015 ;; Check the number of articles is not too large.
1016 (when (and (integerp gnus-agent-large-newsgroup)
1017 (< 0 gnus-agent-large-newsgroup))
1018 (and (< 0 (setq i (- len gnus-agent-large-newsgroup)))
1019 (setq articles (nthcdr i articles))))
1020 ;; add article with marks to list of article headers we want to fetch.
1021 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1022 (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
1024 (setq articles (sort articles '<))
1025 ;; Remove known articles.
1026 (when (gnus-agent-load-alist group)
1027 (setq articles (gnus-sorted-intersection
1029 (gnus-uncompress-range
1030 (cons (1+ (caar (last gnus-agent-article-alist)))
1031 (cdr (gnus-active group)))))))
1033 (gnus-make-directory (nnheader-translate-file-chars
1034 (file-name-directory file) t))
1036 (gnus-message 7 "Fetching headers for %s..." group)
1038 (set-buffer nntp-server-buffer)
1039 (unless (eq 'nov (gnus-retrieve-headers articles group))
1040 (nnvirtual-convert-headers))
1041 ;; Save these headers for later processing.
1042 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1043 (when (file-exists-p file)
1044 (gnus-agent-braid-nov group articles file))
1045 (write-region-as-coding-system
1046 gnus-agent-file-coding-system
1047 (point-min) (point-max) file nil 'silent)
1048 (gnus-agent-save-alist group articles nil)
1049 (gnus-agent-enter-history
1050 "last-header-fetched-for-session"
1051 (list (cons group (nth (- (length articles) 1) articles)))
1052 (time-to-days (current-time)))
1055 (defsubst gnus-agent-copy-nov-line (article)
1057 (set-buffer gnus-agent-overview-buffer)
1059 (if (eq article (read (current-buffer)))
1060 (setq e (progn (forward-line 1) (point)))
1064 (set-buffer nntp-server-buffer)
1065 (insert-buffer-substring gnus-agent-overview-buffer b e)))
1067 (defun gnus-agent-braid-nov (group articles file)
1068 (set-buffer gnus-agent-overview-buffer)
1069 (goto-char (point-min))
1070 (set-buffer nntp-server-buffer)
1072 (nnheader-insert-file-contents file)
1073 (goto-char (point-max))
1074 (if (or (= (point-min) (point-max))
1077 (< (read (current-buffer)) (car articles))))
1078 ;; We have only headers that are after the older headers,
1079 ;; so we just append them.
1081 (goto-char (point-max))
1082 (insert-buffer-substring gnus-agent-overview-buffer))
1083 ;; We do it the hard way.
1084 (nnheader-find-nov-line (car articles))
1085 (gnus-agent-copy-nov-line (car articles))
1087 (while (and articles
1089 (while (and (not (eobp))
1090 (< (read (current-buffer)) (car articles)))
1094 (gnus-agent-copy-nov-line (car articles))
1095 (setq articles (cdr articles))))
1098 (set-buffer gnus-agent-overview-buffer)
1101 (set-buffer nntp-server-buffer)
1102 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1104 (defun gnus-agent-load-alist (group &optional dir)
1105 "Load the article-state alist for GROUP."
1106 (setq gnus-agent-article-alist
1107 (gnus-agent-read-file
1109 (expand-file-name ".agentview" dir)
1110 (gnus-agent-article-name ".agentview" group)))))
1112 (defun gnus-agent-save-alist (group &optional articles state dir)
1113 "Save the article-state alist for GROUP."
1114 (let ((file-name-coding-system nnmail-pathname-coding-system)
1115 (pathname-coding-system nnmail-pathname-coding-system)
1116 print-level print-length)
1117 (with-temp-file (if dir
1118 (expand-file-name ".agentview" dir)
1119 (gnus-agent-article-name ".agentview" group))
1120 (princ (setq gnus-agent-article-alist
1121 (nconc gnus-agent-article-alist
1122 (mapcar (lambda (article) (cons article state))
1127 (defun gnus-agent-article-name (article group)
1128 (expand-file-name (if (stringp article) article (string-to-number article))
1129 (file-name-as-directory
1130 (expand-file-name (gnus-agent-group-path group)
1131 (gnus-agent-directory)))))
1134 (defun gnus-agent-batch-fetch ()
1135 "Start Gnus and fetch session."
1138 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1139 (gnus-agent-fetch-session))
1142 (defun gnus-agent-fetch-session ()
1143 "Fetch all articles and headers that are eligible for fetching."
1145 (unless gnus-agent-covered-methods
1146 (error "No servers are covered by the Gnus agent"))
1147 (unless gnus-plugged
1148 (error "Can't fetch articles while Gnus is unplugged"))
1149 (let ((methods gnus-agent-covered-methods)
1150 groups group gnus-command-method)
1155 (setq gnus-command-method (car methods))
1156 (when (or (gnus-server-opened gnus-command-method)
1157 (gnus-open-server gnus-command-method))
1158 (setq groups (gnus-groups-from-server (car methods)))
1159 (gnus-agent-with-fetch
1160 (while (setq group (pop groups))
1161 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1162 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1164 (unless (funcall gnus-agent-confirmation-function
1165 (format "Error (%s). Continue? " err))
1166 (error "Cannot fetch articles into the Gnus agent")))
1168 (unless (funcall gnus-agent-confirmation-function
1169 (format "Quit fetching session (%s). Continue? "
1171 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1173 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1175 (defun gnus-agent-fetch-group-1 (group method)
1177 (let ((gnus-command-method method)
1178 (gnus-newsgroup-name group)
1179 gnus-newsgroup-dependencies gnus-newsgroup-headers
1180 gnus-newsgroup-scored gnus-headers gnus-score
1181 gnus-use-cache articles arts
1182 category predicate info marks score-param
1183 (gnus-summary-expunge-below gnus-summary-expunge-below)
1184 (gnus-summary-mark-below gnus-summary-mark-below)
1185 (gnus-orphan-score gnus-orphan-score)
1186 ;; Maybe some other gnus-summary local variables should also
1189 (unless (gnus-check-group group)
1190 (error "Can't open server for %s" group))
1192 (when (and (or (gnus-active group)
1193 (gnus-activate-group group))
1194 (setq articles (gnus-agent-fetch-headers group))
1195 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1196 ;; Parse them and see which articles we want to fetch.
1197 (setq gnus-newsgroup-dependencies
1198 (make-vector (length articles) 0))
1199 (setq gnus-newsgroup-headers
1200 (gnus-get-newsgroup-headers-xover articles nil nil
1202 ;; `gnus-agent-overview-buffer' may be killed for
1203 ;; timeout reason. If so, recreate it.
1204 (gnus-agent-create-buffer)))
1205 (setq category (gnus-group-category group))
1208 (or (gnus-group-find-parameter group 'agent-predicate t)
1210 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1211 ;; Simple implementation
1212 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1215 (or (gnus-group-get-parameter group 'agent-score t)
1217 ;; Translate score-param into real one
1220 ((eq score-param 'file)
1221 (setq score-param (gnus-all-score-files group)))
1222 ((stringp (car score-param)))
1224 (setq score-param (list (list score-param)))))
1226 (gnus-score-headers score-param))
1227 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1229 (or (cdr (assq (mail-header-number gnus-headers)
1230 gnus-newsgroup-scored))
1231 gnus-summary-default-score))
1232 (when (funcall predicate)
1233 (push (mail-header-number gnus-headers)
1235 ;; Fetch the articles.
1237 (gnus-agent-fetch-articles group arts)))
1238 ;; Perhaps we have some additional articles to fetch.
1239 (setq arts (assq 'download (gnus-info-marks
1240 (setq info (gnus-get-info group)))))
1242 (gnus-agent-fetch-articles
1243 group (gnus-uncompress-range (cdr arts)))
1244 (setq marks (delq arts (gnus-info-marks info)))
1245 (gnus-info-set-marks info marks)
1247 (concat "(gnus-group-set-info '"
1248 (gnus-prin1-to-string info)
1252 ;;; Agent Category Mode
1255 (defvar gnus-category-mode-hook nil
1256 "Hook run in `gnus-category-mode' buffers.")
1258 (defvar gnus-category-line-format " %(%20c%): %g\n"
1259 "Format of category lines.")
1261 (defvar gnus-category-mode-line-format "Gnus: %%b"
1262 "The format specification for the category mode line.")
1264 (defvar gnus-agent-short-article 100
1265 "Articles that have fewer lines than this are short.")
1267 (defvar gnus-agent-long-article 200
1268 "Articles that have more lines than this are long.")
1270 (defvar gnus-agent-low-score 0
1271 "Articles that have a score lower than this have a low score.")
1273 (defvar gnus-agent-high-score 0
1274 "Articles that have a score higher than this have a high score.")
1277 ;;; Internal variables.
1279 (defvar gnus-category-buffer "*Agent Category*")
1281 (defvar gnus-category-line-format-alist
1282 `((?c gnus-tmp-name ?s)
1283 (?g gnus-tmp-groups ?d)))
1285 (defvar gnus-category-mode-line-format-alist
1286 `((?u user-defined ?s)))
1288 (defvar gnus-category-line-format-spec nil)
1289 (defvar gnus-category-mode-line-format-spec nil)
1291 (defvar gnus-category-mode-map nil)
1292 (put 'gnus-category-mode 'mode-class 'special)
1294 (unless gnus-category-mode-map
1295 (setq gnus-category-mode-map (make-sparse-keymap))
1296 (suppress-keymap gnus-category-mode-map)
1298 (gnus-define-keys gnus-category-mode-map
1299 "q" gnus-category-exit
1300 "k" gnus-category-kill
1301 "c" gnus-category-copy
1302 "a" gnus-category-add
1303 "p" gnus-category-edit-predicate
1304 "g" gnus-category-edit-groups
1305 "s" gnus-category-edit-score
1306 "l" gnus-category-list
1308 "\C-c\C-i" gnus-info-find-node
1309 "\C-c\C-b" gnus-bug))
1311 (defvar gnus-category-menu-hook nil
1312 "*Hook run after the creation of the menu.")
1314 (defun gnus-category-make-menu-bar ()
1315 (gnus-turn-off-edit-menu 'category)
1316 (unless (boundp 'gnus-category-menu)
1318 gnus-category-menu gnus-category-mode-map ""
1320 ["Add" gnus-category-add t]
1321 ["Kill" gnus-category-kill t]
1322 ["Copy" gnus-category-copy t]
1323 ["Edit predicate" gnus-category-edit-predicate t]
1324 ["Edit score" gnus-category-edit-score t]
1325 ["Edit groups" gnus-category-edit-groups t]
1326 ["Exit" gnus-category-exit t]))
1328 (gnus-run-hooks 'gnus-category-menu-hook)))
1330 (defun gnus-category-mode ()
1331 "Major mode for listing and editing agent categories.
1333 All normal editing commands are switched off.
1334 \\<gnus-category-mode-map>
1335 For more in-depth information on this mode, read the manual
1336 (`\\[gnus-info-find-node]').
1338 The following commands are available:
1340 \\{gnus-category-mode-map}"
1342 (when (gnus-visual-p 'category-menu 'menu)
1343 (gnus-category-make-menu-bar))
1344 (kill-all-local-variables)
1345 (gnus-simplify-mode-line)
1346 (setq major-mode 'gnus-category-mode)
1347 (setq mode-name "Category")
1348 (gnus-set-default-directory)
1349 (setq mode-line-process nil)
1350 (use-local-map gnus-category-mode-map)
1351 (buffer-disable-undo)
1352 (setq truncate-lines t)
1353 (setq buffer-read-only t)
1354 (gnus-run-hooks 'gnus-category-mode-hook))
1356 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1358 (defun gnus-category-insert-line (category)
1359 (let* ((gnus-tmp-name (car category))
1360 (gnus-tmp-groups (length (cadddr category))))
1362 (gnus-add-text-properties
1366 (eval gnus-category-line-format-spec))
1367 (list 'gnus-category gnus-tmp-name))))
1369 (defun gnus-enter-category-buffer ()
1370 "Go to the Category buffer."
1372 (gnus-category-setup-buffer)
1373 (gnus-configure-windows 'category)
1374 (gnus-category-prepare))
1376 (defun gnus-category-setup-buffer ()
1377 (unless (get-buffer gnus-category-buffer)
1379 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1380 (gnus-category-mode))))
1382 (defun gnus-category-prepare ()
1383 (gnus-set-format 'category-mode)
1384 (gnus-set-format 'category t)
1385 (let ((alist gnus-category-alist)
1386 (buffer-read-only nil))
1389 (gnus-category-insert-line (pop alist)))
1390 (goto-char (point-min))
1391 (gnus-category-position-point)))
1393 (defun gnus-category-name ()
1394 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1395 (error "No category on the current line")))
1397 (defun gnus-category-read ()
1398 "Read the category alist."
1399 (setq gnus-category-alist
1400 (or (gnus-agent-read-file
1401 (nnheader-concat gnus-agent-directory "lib/categories"))
1402 (list (list 'default 'short nil nil)))))
1404 (defun gnus-category-write ()
1405 "Write the category alist."
1406 (setq gnus-category-predicate-cache nil
1407 gnus-category-group-cache nil)
1408 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1409 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1410 (prin1 gnus-category-alist (current-buffer))))
1412 (defun gnus-category-edit-predicate (category)
1413 "Edit the predicate for CATEGORY."
1414 (interactive (list (gnus-category-name)))
1415 (let ((info (assq category gnus-category-alist)))
1417 (cadr info) (format "Editing the predicate for category %s" category)
1418 `(lambda (predicate)
1419 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1420 (gnus-category-write)
1421 (gnus-category-list)))))
1423 (defun gnus-category-edit-score (category)
1424 "Edit the score expression for CATEGORY."
1425 (interactive (list (gnus-category-name)))
1426 (let ((info (assq category gnus-category-alist)))
1429 (format "Editing the score expression for category %s" category)
1431 (setcar (nthcdr 2 (assq ',category gnus-category-alist)) groups)
1432 (gnus-category-write)
1433 (gnus-category-list)))))
1435 (defun gnus-category-edit-groups (category)
1436 "Edit the group list for CATEGORY."
1437 (interactive (list (gnus-category-name)))
1438 (let ((info (assq category gnus-category-alist)))
1440 (cadddr info) (format "Editing the group list for category %s" category)
1442 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1443 (gnus-category-write)
1444 (gnus-category-list)))))
1446 (defun gnus-category-kill (category)
1447 "Kill the current category."
1448 (interactive (list (gnus-category-name)))
1449 (let ((info (assq category gnus-category-alist))
1450 (buffer-read-only nil))
1452 (setq gnus-category-alist (delq info gnus-category-alist))
1453 (gnus-category-write)))
1455 (defun gnus-category-copy (category to)
1456 "Copy the current category."
1457 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1458 (let ((info (assq category gnus-category-alist)))
1459 (push (list to (gnus-copy-sequence (cadr info))
1460 (gnus-copy-sequence (caddr info)) nil)
1461 gnus-category-alist)
1462 (gnus-category-write)
1463 (gnus-category-list)))
1465 (defun gnus-category-add (category)
1466 "Create a new category."
1467 (interactive "SCategory name: ")
1468 (when (assq category gnus-category-alist)
1469 (error "Category %s already exists" category))
1470 (push (list category 'false nil nil)
1471 gnus-category-alist)
1472 (gnus-category-write)
1473 (gnus-category-list))
1475 (defun gnus-category-list ()
1476 "List all categories."
1478 (gnus-category-prepare))
1480 (defun gnus-category-exit ()
1481 "Return to the group buffer."
1483 (kill-buffer (current-buffer))
1484 (gnus-configure-windows 'group t))
1486 ;; To avoid having 8-bit characters in the source file.
1487 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1489 (defvar gnus-category-predicate-alist
1490 '((spam . gnus-agent-spam-p)
1491 (short . gnus-agent-short-p)
1492 (long . gnus-agent-long-p)
1493 (low . gnus-agent-low-scored-p)
1494 (high . gnus-agent-high-scored-p)
1495 (true . gnus-agent-true)
1496 (false . gnus-agent-false))
1497 "Mapping from short score predicate symbols to predicate functions.")
1499 (defun gnus-agent-spam-p ()
1500 "Say whether an article is spam or not."
1501 (unless gnus-agent-spam-hashtb
1502 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1503 (if (not (equal (mail-header-references gnus-headers) ""))
1505 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1507 (gnus-gethash string gnus-agent-spam-hashtb)
1508 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1510 (defun gnus-agent-short-p ()
1511 "Say whether an article is short or not."
1512 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1514 (defun gnus-agent-long-p ()
1515 "Say whether an article is long or not."
1516 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1518 (defun gnus-agent-low-scored-p ()
1519 "Say whether an article has a low score or not."
1520 (< gnus-score gnus-agent-low-score))
1522 (defun gnus-agent-high-scored-p ()
1523 "Say whether an article has a high score or not."
1524 (> gnus-score gnus-agent-high-score))
1526 (defun gnus-category-make-function (cat)
1527 "Make a function from category CAT."
1528 (let ((func (gnus-category-make-function-1 cat)))
1529 (if (and (= (length func) 1)
1530 (symbolp (car func)))
1532 (gnus-byte-compile `(lambda () ,func)))))
1534 (defun gnus-agent-true ()
1538 (defun gnus-agent-false ()
1542 (defun gnus-category-make-function-1 (cat)
1543 "Make a function from category CAT."
1545 ;; Functions are just returned as is.
1547 (gnus-functionp cat))
1548 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1550 ;; More complex category.
1553 ((memq (car cat) '(& and))
1555 ((memq (car cat) '(| or))
1557 ((memq (car cat) gnus-category-not)
1559 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1561 (error "Unknown category type: %s" cat))))
1563 (defun gnus-get-predicate (predicate)
1564 "Return the predicate for CATEGORY."
1565 (or (cdr (assoc predicate gnus-category-predicate-cache))
1566 (cdar (push (cons predicate
1567 (gnus-category-make-function predicate))
1568 gnus-category-predicate-cache))))
1570 (defun gnus-group-category (group)
1571 "Return the category GROUP belongs to."
1572 (unless gnus-category-group-cache
1573 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1574 (let ((cs gnus-category-alist)
1576 (while (setq cat (pop cs))
1577 (setq groups (cadddr cat))
1579 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1580 (or (gnus-gethash group gnus-category-group-cache)
1581 (assq 'default gnus-category-alist)))
1583 (defun gnus-agent-expire ()
1584 "Expire all old articles."
1586 (let ((methods gnus-agent-covered-methods)
1587 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1588 gnus-command-method sym group articles
1589 history overview file histories elem art nov-file low info
1590 unreads marked article orig lowest highest)
1592 (setq overview (gnus-get-buffer-create " *expire overview*"))
1593 (while (setq gnus-command-method (pop methods))
1594 (when (file-exists-p (gnus-agent-lib-file "active"))
1596 (insert-file-contents-as-coding-system
1597 gnus-agent-file-coding-system (gnus-agent-lib-file "active"))
1598 (gnus-active-to-gnus-format
1600 (setq orig (gnus-make-hashtable
1601 (count-lines (point-min) (point-max))))))
1602 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1603 (gnus-agent-open-history)
1605 (setq gnus-agent-current-history
1606 (setq history (gnus-agent-history-buffer))))
1607 (goto-char (point-min))
1608 (when (> (buffer-size) 1)
1609 (goto-char (point-min))
1611 (skip-chars-forward "^\t")
1612 (if (let ((fetch-date (read (current-buffer))))
1613 (if (numberp fetch-date)
1615 ;; History file is corrupted.
1618 (format "File %s is corrupted!"
1619 (gnus-agent-lib-file "history")))
1623 ;; New article; we don't expire it.
1625 ;; Old article. Schedule it for possible nuking.
1627 (setq sym (let ((obarray expiry-hashtb) s)
1628 (setq s (read (current-buffer)))
1629 (if (stringp s) (intern s) s)))
1631 (set sym (cons (cons (read (current-buffer)) (point))
1632 (symbol-value sym)))
1633 (set sym (list (cons (read (current-buffer)) (point)))))
1634 (skip-chars-forward " "))
1636 ;; We now have all articles that can possibly be expired.
1639 (setq group (symbol-name sym)
1640 articles (sort (symbol-value sym) 'car-less-than-car)
1641 low (car (gnus-active group))
1642 info (gnus-get-info group)
1643 unreads (ignore-errors
1644 (gnus-list-of-unread-articles group))
1646 (gnus-uncompress-range
1647 (cdr (assq 'tick (gnus-info-marks info))))
1648 (gnus-uncompress-range
1649 (cdr (assq 'dormant (gnus-info-marks info))))
1650 (gnus-uncompress-range
1651 (cdr (assq 'save (gnus-info-marks info))))
1652 (gnus-uncompress-range
1653 (cdr (assq 'reply (gnus-info-marks info)))))
1654 nov-file (gnus-agent-article-name ".overview" group)
1657 (gnus-agent-load-alist group)
1658 (gnus-message 5 "Expiring articles in %s" group)
1659 (set-buffer overview)
1661 (when (file-exists-p nov-file)
1662 (nnheader-insert-file-contents nov-file))
1663 (goto-char (point-min))
1665 (while (setq elem (pop articles))
1666 (setq article (car elem))
1667 (when (or (null low)
1669 gnus-agent-expire-all
1670 (and (not (memq article unreads))
1671 (not (memq article marked))))
1672 ;; Find and nuke the NOV line.
1673 (while (and (not (eobp))
1675 (setq art (read (current-buffer)))))
1677 (if (and (numberp art)
1679 (gnus-agent-article-name
1680 (number-to-string art) group)))
1686 ;; Remove old NOV lines that have no articles.
1687 (gnus-delete-line)))
1692 ;; Nuke the article.
1693 (when (file-exists-p
1694 (setq file (gnus-agent-article-name
1695 (number-to-string article)
1698 ;; Schedule the history line for nuking.
1699 (push (cdr elem) histories)))
1700 (gnus-make-directory (file-name-directory nov-file))
1701 (write-region-as-coding-system
1702 gnus-agent-file-coding-system
1703 (point-min) (point-max) nov-file nil 'silent)
1704 ;; Delete the unwanted entries in the alist.
1705 (setq gnus-agent-article-alist
1706 (sort gnus-agent-article-alist 'car-less-than-car))
1707 (let* ((alist gnus-agent-article-alist)
1708 (prev (cons nil alist))
1712 (<= (caar alist) article))
1713 (if (or (not (cdar alist))
1715 (gnus-agent-article-name
1720 (push (caar alist) expired)
1721 (setcdr prev (setq alist (cdr alist))))
1723 alist (cdr alist))))
1724 (setq gnus-agent-article-alist (cdr first))
1725 (gnus-agent-save-alist group)
1726 ;; Mark all articles up to the first article
1727 ;; in `gnus-article-alist' as read.
1728 (when (and info (caar gnus-agent-article-alist))
1729 (setcar (nthcdr 2 info)
1732 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1733 ;; Maybe everything has been expired from `gnus-article-alist'
1734 ;; and so the above marking as read could not be conducted,
1735 ;; or there are expired article within the range of the alist.
1738 (or (not (caar gnus-agent-article-alist))
1740 (caar gnus-agent-article-alist))))
1741 (setcar (nthcdr 2 info)
1744 (nreverse expired))))
1746 (concat "(gnus-group-set-info '"
1747 (gnus-prin1-to-string info)
1750 (if (gnus-gethash group orig)
1751 (setcar (gnus-gethash group orig) lowest)
1752 (gnus-sethash group (cons lowest highest) orig))))
1754 (set-buffer history)
1755 (setq histories (nreverse (sort histories '<)))
1757 (goto-char (pop histories))
1759 (gnus-agent-save-history)
1760 (gnus-agent-close-history)
1761 (gnus-write-active-file (gnus-agent-lib-file "active") orig))
1762 (gnus-message 4 "Expiry...done")))))))
1765 (defun gnus-agent-batch ()
1767 (let ((init-file-user "")
1768 (gnus-always-read-dribble-file t))
1770 (gnus-group-send-drafts)
1771 (gnus-agent-fetch-session))
1773 (provide 'gnus-agent)
1775 ;;; gnus-agent.el ends here