1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
27 (eval-when-compile (require 'cl))
28 (eval-when-compile (require 'gnus-clfns))
34 (eval-when-compile (require 'gnus-score) (require 'gnus-group))
36 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
37 "Where the Gnus agent will store its files."
41 (defcustom gnus-agent-plugged-hook nil
42 "Hook run when plugging into the network."
46 (defcustom gnus-agent-unplugged-hook nil
47 "Hook run when unplugging from the network."
51 (defcustom gnus-agent-handle-level gnus-level-subscribed
52 "Groups on levels higher than this variable will be ignored by the Agent."
56 (defcustom gnus-agent-expire-days 7
57 "Read articles older than this will be expired."
61 (defcustom gnus-agent-expire-all nil
62 "If non-nil, also expire unread, ticked and dormant articles.
63 If nil, only read articles will be expired."
67 (defcustom gnus-agent-group-mode-hook nil
68 "Hook run in Agent group minor modes."
72 (defcustom gnus-agent-summary-mode-hook nil
73 "Hook run in Agent summary minor modes."
77 (defcustom gnus-agent-server-mode-hook nil
78 "Hook run in Agent summary minor modes."
82 (defcustom gnus-agent-confirmation-function 'y-or-n-p
83 "Function to confirm when error happens."
87 (defcustom gnus-agent-large-newsgroup nil
88 "*The number of articles which indicates a large newsgroup.
89 If the number of unread articles exceeds it, The number of articles to be
90 fetched will be limited to it. If not a positive integer, never consider it."
92 :type '(choice (const nil)
93 (integer :tag "Number")))
95 ;;; Internal variables
97 (defvar gnus-agent-history-buffers nil)
98 (defvar gnus-agent-buffer-alist nil)
99 (defvar gnus-agent-article-alist nil)
100 (defvar gnus-agent-group-alist nil)
101 (defvar gnus-agent-covered-methods nil)
102 (defvar gnus-category-alist nil)
103 (defvar gnus-agent-current-history nil)
104 (defvar gnus-agent-overview-buffer nil)
105 (defvar gnus-category-predicate-cache nil)
106 (defvar gnus-category-group-cache nil)
107 (defvar gnus-agent-spam-hashtb nil)
108 (defvar gnus-agent-file-name nil)
109 (defvar gnus-agent-send-mail-function nil)
110 (defvar gnus-agent-file-coding-system 'raw-text)
112 (defconst gnus-agent-scoreable-headers
113 '("subject" "from" "date" "message-id" "references" "chars" "lines" "xref")
114 "Headers that are considered when scoring articles for download via the Agent.")
117 (defvar gnus-headers)
124 (defun gnus-open-agent ()
126 (gnus-agent-read-servers)
128 (gnus-agent-create-buffer)
129 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
130 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
131 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
133 (defun gnus-agent-create-buffer ()
134 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
136 (setq gnus-agent-overview-buffer
137 (gnus-get-buffer-create " *Gnus agent overview*"))
138 (with-current-buffer gnus-agent-overview-buffer
139 (set-buffer-multibyte t))
142 (gnus-add-shutdown 'gnus-close-agent 'gnus)
144 (defun gnus-close-agent ()
145 (setq gnus-agent-covered-methods nil
146 gnus-category-predicate-cache nil
147 gnus-category-group-cache nil
148 gnus-agent-spam-hashtb nil)
149 (gnus-kill-buffer gnus-agent-overview-buffer))
152 ;;; Utility functions
155 (defun gnus-agent-read-file (file)
156 "Load FILE and do a `read' there."
159 (nnheader-insert-file-contents file)
160 (goto-char (point-min))
161 (read (current-buffer)))))
163 (defsubst gnus-agent-method ()
164 (concat (symbol-name (car gnus-command-method)) "/"
165 (if (equal (cadr gnus-command-method) "")
167 (cadr gnus-command-method))))
169 (defsubst gnus-agent-directory ()
170 "Path of the Gnus agent directory."
171 (nnheader-concat gnus-agent-directory
172 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
174 (defun gnus-agent-lib-file (file)
175 "The full path of the Gnus agent library FILE."
176 (concat (gnus-agent-directory) "agent.lib/" file))
178 ;;; Fetching setup functions.
180 (defun gnus-agent-start-fetch ()
181 "Initialize data structures for efficient fetching."
182 (gnus-agent-open-history)
183 (setq gnus-agent-current-history (gnus-agent-history-buffer))
184 (gnus-agent-create-buffer))
186 (defun gnus-agent-stop-fetch ()
187 "Save all data structures and clean up."
188 (gnus-agent-save-history)
189 (gnus-agent-close-history)
190 (setq gnus-agent-spam-hashtb nil)
192 (set-buffer nntp-server-buffer)
195 (defmacro gnus-agent-with-fetch (&rest forms)
199 (gnus-agent-start-fetch)
201 (gnus-agent-stop-fetch)))
203 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
204 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
210 (defvar gnus-agent-mode-hook nil
211 "Hook run when installing agent mode.")
213 (defvar gnus-agent-mode nil)
214 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
216 (defun gnus-agent-mode ()
217 "Minor mode for providing a agent support in Gnus buffers."
218 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
219 (symbol-name major-mode))
220 (match-string 1 (symbol-name major-mode))))
221 (mode (intern (format "gnus-agent-%s-mode" buffer))))
222 (set (make-local-variable 'gnus-agent-mode) t)
224 (set (make-local-variable mode) t)
226 (when (gnus-visual-p 'agent-menu 'menu)
227 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
228 (unless (assq 'gnus-agent-mode minor-mode-alist)
229 (push gnus-agent-mode-status minor-mode-alist))
230 (unless (assq mode minor-mode-map-alist)
231 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
233 minor-mode-map-alist))
234 (when (eq major-mode 'gnus-group-mode)
235 (gnus-agent-toggle-plugged gnus-plugged))
236 (gnus-run-hooks 'gnus-agent-mode-hook
237 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
239 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
240 (gnus-define-keys gnus-agent-group-mode-map
241 "Ju" gnus-agent-fetch-groups
242 "Jc" gnus-enter-category-buffer
243 "Jj" gnus-agent-toggle-plugged
244 "Js" gnus-agent-fetch-session
245 "JY" gnus-agent-synchronize
246 "JS" gnus-group-send-drafts
247 "Ja" gnus-agent-add-group
248 "Jr" gnus-agent-remove-group)
250 (defun gnus-agent-group-make-menu-bar ()
251 (unless (boundp 'gnus-agent-group-menu)
253 gnus-agent-group-menu gnus-agent-group-mode-map ""
255 ["Toggle plugged" gnus-agent-toggle-plugged t]
256 ["List categories" gnus-enter-category-buffer t]
257 ["Send drafts" gnus-group-send-drafts gnus-plugged]
259 ["All" gnus-agent-fetch-session gnus-plugged]
260 ["Group" gnus-agent-fetch-group gnus-plugged])))))
262 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
263 (gnus-define-keys gnus-agent-summary-mode-map
264 "Jj" gnus-agent-toggle-plugged
265 "J#" gnus-agent-mark-article
266 "J\M-#" gnus-agent-unmark-article
267 "@" gnus-agent-toggle-mark
268 "Jc" gnus-agent-catchup)
270 (defun gnus-agent-summary-make-menu-bar ()
271 (unless (boundp 'gnus-agent-summary-menu)
273 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
275 ["Toggle plugged" gnus-agent-toggle-plugged t]
276 ["Mark as downloadable" gnus-agent-mark-article t]
277 ["Unmark as downloadable" gnus-agent-unmark-article t]
278 ["Toggle mark" gnus-agent-toggle-mark t]
279 ["Catchup undownloaded" gnus-agent-catchup t]))))
281 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
282 (gnus-define-keys gnus-agent-server-mode-map
283 "Jj" gnus-agent-toggle-plugged
284 "Ja" gnus-agent-add-server
285 "Jr" gnus-agent-remove-server)
287 (defun gnus-agent-server-make-menu-bar ()
288 (unless (boundp 'gnus-agent-server-menu)
290 gnus-agent-server-menu gnus-agent-server-mode-map ""
292 ["Toggle plugged" gnus-agent-toggle-plugged t]
293 ["Add" gnus-agent-add-server t]
294 ["Remove" gnus-agent-remove-server t]))))
296 (defun gnus-agent-toggle-plugged (plugged)
297 "Toggle whether Gnus is unplugged or not."
298 (interactive (list (not gnus-plugged)))
301 (setq gnus-plugged plugged)
302 (gnus-run-hooks 'gnus-agent-plugged-hook)
303 (setcar (cdr gnus-agent-mode-status) " Plugged"))
304 (gnus-agent-close-connections)
305 (setq gnus-plugged plugged)
306 (gnus-run-hooks 'gnus-agent-unplugged-hook)
307 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
308 (force-mode-line-update))
310 (defun gnus-agent-close-connections ()
311 "Close all methods covered by the Gnus agent."
312 (let ((methods gnus-agent-covered-methods))
314 (gnus-close-server (pop methods)))))
317 (defun gnus-unplugged ()
318 "Start Gnus unplugged."
320 (setq gnus-plugged nil)
324 (defun gnus-plugged ()
325 "Start Gnus plugged."
327 (setq gnus-plugged t)
331 (defun gnus-agentize ()
332 "Allow Gnus to be an offline newsreader.
333 The normal usage of this command is to put the following as the
334 last form in your `.gnus.el' file:
338 This will modify the `gnus-before-startup-hook', `gnus-post-method',
339 and `message-send-mail-function' variables, and install the Gnus
340 agent minor mode in all Gnus buffers."
343 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
344 (unless gnus-agent-send-mail-function
345 (setq gnus-agent-send-mail-function message-send-mail-function
346 message-send-mail-function 'gnus-agent-send-mail))
347 (unless gnus-agent-covered-methods
348 (setq gnus-agent-covered-methods (list gnus-select-method))))
350 (defun gnus-agent-queue-setup ()
351 "Make sure the queue group exists."
352 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
353 (gnus-request-create-group "queue" '(nndraft ""))
354 (let ((gnus-level-default-subscribed 1))
355 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
356 (gnus-group-set-parameter
357 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
359 (defun gnus-agent-send-mail ()
361 (funcall gnus-agent-send-mail-function)
362 (goto-char (point-min))
364 (concat "^" (regexp-quote mail-header-separator) "\n"))
366 (gnus-agent-insert-meta-information 'mail)
367 (gnus-request-accept-article "nndraft:queue" nil t t)))
369 (defun gnus-agent-insert-meta-information (type &optional method)
370 "Insert meta-information into the message that says how it's to be posted.
371 TYPE can be either `mail' or `news'. If the latter METHOD can
374 (message-remove-header gnus-agent-meta-information-header)
375 (goto-char (point-min))
376 (insert gnus-agent-meta-information-header ": "
377 (symbol-name type) " " (format "%S" method)
380 (while (search-backward "\n" nil t)
381 (replace-match "\\n" t t))))
384 ;;; Group mode commands
387 (defun gnus-agent-fetch-groups (n)
388 "Put all new articles in the current groups into the Agent."
391 (error "Groups can't be fetched when Gnus is unplugged"))
392 (gnus-group-iterate n 'gnus-agent-fetch-group))
394 (defun gnus-agent-fetch-group (group)
395 "Put all new articles in GROUP into the Agent."
396 (interactive (list (gnus-group-group-name)))
398 (error "Groups can't be fetched when Gnus is unplugged"))
400 (error "No group on the current line"))
401 (let ((gnus-command-method (gnus-find-method-for-group group)))
402 (gnus-agent-with-fetch
403 (gnus-agent-fetch-group-1 group gnus-command-method)
404 (gnus-message 5 "Fetching %s...done" group))))
406 (defun gnus-agent-add-group (category arg)
407 "Add the current group to an agent category."
413 (mapcar (lambda (cat) (list (symbol-name (car cat))))
417 (let ((cat (assq category gnus-category-alist))
419 (gnus-group-iterate arg
421 (when (cadddr (setq c (gnus-group-category group)))
422 (setf (cadddr c) (delete group (cadddr c))))
423 (push group groups)))
424 (setf (cadddr cat) (nconc (cadddr cat) groups))
425 (gnus-category-write)))
427 (defun gnus-agent-remove-group (arg)
428 "Remove the current group from its agent category, if any."
431 (gnus-group-iterate arg
433 (when (cadddr (setq c (gnus-group-category group)))
434 (setf (cadddr c) (delete group (cadddr c))))))
435 (gnus-category-write)))
437 (defun gnus-agent-synchronize ()
438 "Synchronize local, unplugged, data with backend.
439 Currently sends flag setting requests, if any."
442 (dolist (gnus-command-method gnus-agent-covered-methods)
443 (when (file-exists-p (gnus-agent-lib-file "flags"))
444 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
446 (insert-file-contents (gnus-agent-lib-file "flags"))
447 (if (null (gnus-check-server gnus-command-method))
448 (message "Couldn't open server %s" (nth 1 gnus-command-method))
450 (if (null (eval (read (current-buffer))))
451 (progn (forward-line)
453 (write-file (gnus-agent-lib-file "flags"))
454 (error "Couldn't set flags from file %s"
455 (gnus-agent-lib-file "flags"))))
456 (write-file (gnus-agent-lib-file "flags")))))))
459 ;;; Server mode commands
462 (defun gnus-agent-add-server (server)
463 "Enroll SERVER in the agent program."
464 (interactive (list (gnus-server-server-name)))
466 (error "No server on the current line"))
467 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
468 (when (member method gnus-agent-covered-methods)
469 (error "Server already in the agent program"))
470 (push method gnus-agent-covered-methods)
471 (gnus-agent-write-servers)
472 (message "Entered %s into the Agent" server)))
474 (defun gnus-agent-remove-server (server)
475 "Remove SERVER from the agent program."
476 (interactive (list (gnus-server-server-name)))
478 (error "No server on the current line"))
479 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
480 (unless (member method gnus-agent-covered-methods)
481 (error "Server not in the agent program"))
482 (setq gnus-agent-covered-methods
483 (delete method gnus-agent-covered-methods))
484 (gnus-agent-write-servers)
485 (message "Removed %s from the agent" server)))
487 (defun gnus-agent-read-servers ()
488 "Read the alist of covered servers."
489 (setq gnus-agent-covered-methods
490 (gnus-agent-read-file
491 (nnheader-concat gnus-agent-directory "lib/servers"))))
493 (defun gnus-agent-write-servers ()
494 "Write the alist of covered servers."
495 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
496 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
497 (prin1 gnus-agent-covered-methods (current-buffer))))
503 (defun gnus-agent-mark-article (n &optional unmark)
504 "Mark the next N articles as downloadable.
505 If N is negative, mark backward instead. If UNMARK is non-nil, remove
506 the mark instead. The difference between N and the actual number of
507 articles marked is returned."
509 (let ((backward (< n 0))
514 (gnus-summary-set-agent-mark
515 (gnus-summary-article-number) unmark)
516 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
519 (gnus-message 7 "No more articles"))
520 (gnus-summary-recenter)
521 (gnus-summary-position-point)
524 (defun gnus-agent-unmark-article (n)
525 "Remove the downloadable mark from the next N articles.
526 If N is negative, unmark backward instead. The difference between N and
527 the actual number of articles unmarked is returned."
529 (gnus-agent-mark-article n t))
531 (defun gnus-agent-toggle-mark (n)
532 "Toggle the downloadable mark from the next N articles.
533 If N is negative, toggle backward instead. The difference between N and
534 the actual number of articles toggled is returned."
536 (gnus-agent-mark-article n 'toggle))
538 (defun gnus-summary-set-agent-mark (article &optional unmark)
539 "Mark ARTICLE as downloadable."
540 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
541 (memq article gnus-newsgroup-downloadable)
545 (setq gnus-newsgroup-downloadable
546 (delq article gnus-newsgroup-downloadable))
547 (push article gnus-newsgroup-undownloaded))
548 (setq gnus-newsgroup-undownloaded
549 (delq article gnus-newsgroup-undownloaded))
550 (push article gnus-newsgroup-downloadable))
551 (gnus-summary-update-mark
552 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
555 (defun gnus-agent-get-undownloaded-list ()
556 "Mark all unfetched articles as read."
557 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
558 (when (and (not gnus-plugged)
559 (gnus-agent-method-p gnus-command-method))
560 (gnus-agent-load-alist gnus-newsgroup-name)
561 ;; First mark all undownloaded articles as undownloaded.
562 (let ((articles (append gnus-newsgroup-unreads
563 gnus-newsgroup-marked
564 gnus-newsgroup-dormant))
566 (while (setq article (pop articles))
567 (unless (or (cdr (assq article gnus-agent-article-alist))
568 (memq article gnus-newsgroup-downloadable)
569 (memq article gnus-newsgroup-cached))
570 (push article gnus-newsgroup-undownloaded))))
571 ;; Then mark downloaded downloadable as not-downloadable,
572 ;; if you get my drift.
573 (let ((articles gnus-newsgroup-downloadable)
575 (while (setq article (pop articles))
576 (when (cdr (assq article gnus-agent-article-alist))
577 (setq gnus-newsgroup-downloadable
578 (delq article gnus-newsgroup-downloadable))))))))
580 (defun gnus-agent-catchup ()
581 "Mark all undownloaded articles as read."
584 (while gnus-newsgroup-undownloaded
585 (gnus-summary-mark-article
586 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
587 (gnus-summary-position-point))
590 ;;; Internal functions
593 (defun gnus-agent-save-active (method)
594 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
596 (defun gnus-agent-save-active-1 (method function)
597 (when (gnus-agent-method-p method)
598 (let* ((gnus-command-method method)
599 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
600 (file (gnus-agent-lib-file "active")))
601 (funcall function nil new)
602 (gnus-agent-write-active file new)
604 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
607 (defun gnus-agent-write-active (file new)
608 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
609 (file (gnus-agent-lib-file "active"))
611 (when (file-exists-p file)
613 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
615 (gnus-active-to-gnus-format nil orig))
618 (when (and sym (boundp sym))
619 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
620 (setq elem (symbol-value osym)))
621 (setcdr elem (cdr (symbol-value sym)))
622 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
624 (gnus-make-directory (file-name-directory file))
625 ;; The hashtable contains real names of groups, no more prefix
626 ;; removing, so set `full' to `t'.
627 (gnus-write-active-file-as-coding-system gnus-agent-file-coding-system
630 (defun gnus-agent-save-groups (method)
631 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
633 (defun gnus-agent-save-group-info (method group active)
634 (when (gnus-agent-method-p method)
635 (let* ((gnus-command-method method)
636 (file (gnus-agent-lib-file "active"))
638 (gnus-make-directory (file-name-directory file))
640 (when (file-exists-p file)
641 (nnheader-insert-file-contents file))
642 (goto-char (point-min))
643 (when (re-search-forward
644 (concat "^" (regexp-quote group) " ") nil t)
647 (narrow-to-region (match-beginning 0)
651 (setq oactive (car (nnmail-parse-active)))))
653 (insert (format "%S %d %d y\n" (intern group)
655 (or (car oactive) (car active))))
656 (goto-char (point-max))
657 (while (search-backward "\\." nil t)
660 (defun gnus-agent-group-path (group)
661 "Translate GROUP into a path."
662 (if nnmail-use-long-file-names
663 (gnus-group-real-name group)
664 (nnheader-translate-file-chars
665 (nnheader-replace-chars-in-string
666 (nnheader-replace-duplicate-chars-in-string
667 (nnheader-replace-chars-in-string
668 (gnus-group-real-name group)
675 (defun gnus-agent-method-p (method)
676 "Say whether METHOD is covered by the agent."
677 (member method gnus-agent-covered-methods))
679 (defun gnus-agent-get-function (method)
680 (if (and (not gnus-plugged)
681 (gnus-agent-method-p method))
687 ;;; History functions
689 (defun gnus-agent-history-buffer ()
690 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
692 (defun gnus-agent-open-history ()
694 (push (cons (gnus-agent-method)
695 (set-buffer (gnus-get-buffer-create
696 (format " *Gnus agent %s history*"
697 (gnus-agent-method)))))
698 gnus-agent-history-buffers)
701 (let ((file (gnus-agent-lib-file "history")))
702 (when (file-exists-p file)
704 (set (make-local-variable 'gnus-agent-file-name) file))))
706 (defun gnus-agent-save-history ()
708 (set-buffer gnus-agent-current-history)
709 (gnus-make-directory (file-name-directory gnus-agent-file-name))
710 (write-region-as-coding-system
711 gnus-agent-file-coding-system
712 (1+ (point-min)) (point-max) gnus-agent-file-name nil 'silent)))
714 (defun gnus-agent-close-history ()
715 (when (gnus-buffer-live-p gnus-agent-current-history)
716 (kill-buffer gnus-agent-current-history)
717 (setq gnus-agent-history-buffers
718 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
719 gnus-agent-history-buffers))))
721 (defun gnus-agent-enter-history (id group-arts date)
723 (set-buffer gnus-agent-current-history)
724 (goto-char (point-max))
726 (insert id "\t" (number-to-string date) "\t")
728 (insert (format "%S" (intern (caar group-arts)))
729 " " (number-to-string (cdr (pop group-arts)))
732 (while (search-backward "\\." p t)
735 (defun gnus-agent-article-in-history-p (id)
737 (set-buffer (gnus-agent-history-buffer))
738 (goto-char (point-min))
739 (search-forward (concat "\n" id "\t") nil t)))
741 (defun gnus-agent-history-path (id)
743 (set-buffer (gnus-agent-history-buffer))
744 (goto-char (point-min))
745 (when (search-forward (concat "\n" id "\t") nil t)
746 (let ((method (gnus-agent-method)))
748 (while (not (numberp (setq group (read (current-buffer)))))
749 (push (concat method "/" group) paths))
750 (nreverse paths))))))
756 (defun gnus-agent-fetch-articles (group articles)
757 "Fetch ARTICLES from GROUP and put them into the Agent."
759 ;; Prune off articles that we have already fetched.
761 (cdr (assq (car articles) gnus-agent-article-alist)))
763 (let ((arts articles))
765 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
766 (setcdr arts (cddr arts))
767 (setq arts (cdr arts)))))
770 (gnus-agent-directory)
771 (gnus-agent-group-path group) "/"))
772 (date (time-to-days (current-time)))
775 (gnus-make-directory dir)
776 (gnus-message 7 "Fetching articles for %s..." group)
777 ;; Fetch the articles from the backend.
778 (if (gnus-check-backend-function 'retrieve-articles group)
779 (setq pos (gnus-retrieve-articles articles group))
782 (while (setq article (pop articles))
784 (gnus-backlog-request-article group article
786 (gnus-request-article article group))
787 (goto-char (point-max))
788 (push (cons article (point)) pos)
789 (insert-buffer-substring nntp-server-buffer)))
790 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
791 (setq pos (nreverse pos)))))
792 ;; Then save these articles into the Agent.
794 (set-buffer nntp-server-buffer)
796 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
797 (goto-char (point-min))
798 (when (search-forward "\n\n" nil t)
799 (when (search-backward "\nXrefs: " nil t)
800 ;; Handle crossposting.
801 (skip-chars-forward "^ ")
802 (skip-chars-forward " ")
804 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
805 (push (cons (buffer-substring (match-beginning 1)
807 (buffer-substring (match-beginning 2)
810 (goto-char (match-end 0)))
811 (gnus-agent-crosspost crosses (caar pos))))
812 (goto-char (point-min))
813 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
814 (setq id "No-Message-ID-in-article")
815 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
816 (write-region-as-coding-system
817 gnus-agent-file-coding-system
818 (point-min) (point-max)
819 (concat dir (number-to-string (caar pos))) nil 'silent)
820 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
822 (gnus-agent-enter-history
823 id (or crosses (list (cons group (caar pos)))) date)
826 (gnus-agent-save-alist group)))))
828 (defun gnus-agent-crosspost (crosses article)
829 (let (gnus-agent-article-alist group alist beg end)
831 (set-buffer gnus-agent-overview-buffer)
832 (when (nnheader-find-nov-line article)
835 (setq end (progn (forward-line 1) (point)))))
837 (setq group (caar crosses))
838 (unless (setq alist (assoc group gnus-agent-group-alist))
839 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
840 gnus-agent-group-alist))
841 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
843 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
845 (when (= (point-max) (point-min))
846 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
848 (nnheader-insert-file-contents
849 (gnus-agent-article-name ".overview" group))))
850 (nnheader-find-nov-line (string-to-number (cdar crosses)))
851 (insert (string-to-number (cdar crosses)))
852 (insert-buffer-substring gnus-agent-overview-buffer beg end))
855 (defun gnus-agent-flush-cache ()
857 (while gnus-agent-buffer-alist
858 (set-buffer (cdar gnus-agent-buffer-alist))
859 (write-region-as-coding-system
860 gnus-agent-file-coding-system
861 (point-min) (point-max)
862 (gnus-agent-article-name ".overview"
863 (caar gnus-agent-buffer-alist))
865 (pop gnus-agent-buffer-alist))
866 (while gnus-agent-group-alist
867 (with-temp-file (caar gnus-agent-group-alist)
868 (princ (cdar gnus-agent-group-alist))
870 (pop gnus-agent-group-alist))))
872 (defun gnus-agent-fetch-headers (group &optional force)
873 (let* ((articles (gnus-list-of-unread-articles group))
874 (len (length articles))
875 (gnus-decode-encoded-word-function 'identity)
876 (file (gnus-agent-article-name ".overview" group))
878 ;; Check the number of articles is not too large.
879 (when (and (integerp gnus-agent-large-newsgroup)
880 (< 0 gnus-agent-large-newsgroup))
881 (and (< 0 (setq i (- len gnus-agent-large-newsgroup)))
882 (setq articles (nthcdr i articles))))
883 ;; add article with marks to list of article headers we want to fetch
884 (dolist (arts (gnus-info-marks (gnus-get-info group)))
885 (setq articles (union (gnus-uncompress-sequence (cdr arts))
887 (setq articles (sort articles '<))
888 ;; remove known articles
889 (when (gnus-agent-load-alist group)
890 (setq articles (gnus-sorted-intersection
892 (gnus-uncompress-range
893 (cons (1+ (caar (last gnus-agent-article-alist)))
894 (cdr (gnus-active group)))))))
896 (gnus-make-directory (nnheader-translate-file-chars
897 (file-name-directory file) t))
899 (gnus-message 7 "Fetching headers for %s..." group)
901 (set-buffer nntp-server-buffer)
902 (unless (eq 'nov (gnus-retrieve-headers articles group))
903 (nnvirtual-convert-headers))
904 ;; Save these headers for later processing.
905 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
906 (when (file-exists-p file)
907 (gnus-agent-braid-nov group articles file))
908 (write-region-as-coding-system
909 gnus-agent-file-coding-system
910 (point-min) (point-max) file nil 'silent)
911 (gnus-agent-save-alist group articles nil)
912 (gnus-agent-enter-history
913 "last-header-fetched-for-session"
914 (list (cons group (nth (- (length articles) 1) articles)))
915 (time-to-days (current-time)))
918 (defsubst gnus-agent-copy-nov-line (article)
920 (set-buffer gnus-agent-overview-buffer)
922 (if (eq article (read (current-buffer)))
923 (setq e (progn (forward-line 1) (point)))
927 (set-buffer nntp-server-buffer)
928 (insert-buffer-substring gnus-agent-overview-buffer b e)))
930 (defun gnus-agent-braid-nov (group articles file)
931 (set-buffer gnus-agent-overview-buffer)
932 (goto-char (point-min))
933 (set-buffer nntp-server-buffer)
935 (nnheader-insert-file-contents file)
936 (goto-char (point-max))
937 (if (or (= (point-min) (point-max))
940 (< (read (current-buffer)) (car articles))))
941 ;; We have only headers that are after the older headers,
942 ;; so we just append them.
944 (goto-char (point-max))
945 (insert-buffer-substring gnus-agent-overview-buffer))
946 ;; We do it the hard way.
947 (nnheader-find-nov-line (car articles))
948 (gnus-agent-copy-nov-line (car articles))
952 (while (and (not (eobp))
953 (< (read (current-buffer)) (car articles)))
957 (gnus-agent-copy-nov-line (car articles))
958 (setq articles (cdr articles))))
961 (set-buffer gnus-agent-overview-buffer)
964 (set-buffer nntp-server-buffer)
965 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
967 (defun gnus-agent-load-alist (group &optional dir)
968 "Load the article-state alist for GROUP."
969 (setq gnus-agent-article-alist
970 (gnus-agent-read-file
972 (concat dir ".agentview")
973 (gnus-agent-article-name ".agentview" group)))))
975 (defun gnus-agent-save-alist (group &optional articles state dir)
976 "Save the article-state alist for GROUP."
977 (with-temp-file (if dir
978 (concat dir ".agentview")
979 (gnus-agent-article-name ".agentview" group))
980 (princ (setq gnus-agent-article-alist
981 (nconc gnus-agent-article-alist
982 (mapcar (lambda (article) (cons article state))
987 (defun gnus-agent-article-name (article group)
988 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
989 (if (stringp article) article (string-to-number article))))
992 (defun gnus-agent-batch-fetch ()
993 "Start Gnus and fetch session."
996 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
997 (gnus-agent-fetch-session))
1000 (defun gnus-agent-fetch-session ()
1001 "Fetch all articles and headers that are eligible for fetching."
1003 (unless gnus-agent-covered-methods
1004 (error "No servers are covered by the Gnus agent"))
1005 (unless gnus-plugged
1006 (error "Can't fetch articles while Gnus is unplugged"))
1007 (let ((methods gnus-agent-covered-methods)
1008 groups group gnus-command-method)
1013 (setq gnus-command-method (car methods))
1014 (when (or (gnus-server-opened gnus-command-method)
1015 (gnus-open-server gnus-command-method))
1016 (setq groups (gnus-groups-from-server (car methods)))
1017 (gnus-agent-with-fetch
1018 (while (setq group (pop groups))
1019 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1020 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1022 (unless (funcall gnus-agent-confirmation-function
1023 (format "Error (%s). Continue? " err))
1024 (error "Cannot fetch articles into the Gnus agent."))))
1026 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1028 (defun gnus-agent-fetch-group-1 (group method)
1030 (let ((gnus-command-method method)
1031 (gnus-newsgroup-name group)
1032 gnus-newsgroup-dependencies gnus-newsgroup-headers
1033 gnus-newsgroup-scored gnus-headers gnus-score
1034 gnus-use-cache articles arts
1035 category predicate info marks score-param)
1036 (unless (gnus-check-group group)
1037 (error "Can't open server for %s" group))
1039 (when (and (or (gnus-active group) (gnus-activate-group group))
1040 (setq articles (gnus-agent-fetch-headers group))
1042 ;; Parse them and see which articles we want to fetch.
1043 (setq gnus-newsgroup-dependencies
1044 (make-vector (length articles) 0))
1045 ;; No need to call `gnus-get-newsgroup-headers-xover' with
1046 ;; the entire .overview for group as we still have the just
1047 ;; downloaded headers in `gnus-agent-overview-buffer'.
1048 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1049 (setq gnus-newsgroup-headers
1050 (gnus-get-newsgroup-headers-xover articles nil nil
1052 ;; `gnus-agent-overview-buffer' may be killed for
1053 ;; timeout reason. If so, recreate it.
1054 (gnus-agent-create-buffer)))
1055 (setq category (gnus-group-category group))
1058 (or (gnus-group-find-parameter group 'agent-predicate t)
1060 ;; Do we want to download everything, or nothing?
1061 (if (or (eq (caaddr predicate) 'gnus-agent-true)
1062 (eq (caaddr predicate) 'gnus-agent-false))
1064 (setq arts (symbol-value
1065 (cadr (assoc (caaddr predicate)
1066 '((gnus-agent-true articles)
1067 (gnus-agent-false nil))))))
1068 ;; No, we need to decide what we want.
1072 (gnus-group-find-parameter group 'agent-score t)
1075 (require 'gnus-score)
1076 (if (eq score-method 'file)
1078 (gnus-score-load-files
1079 (gnus-all-score-files group)))
1081 (while (setq list (car entries))
1082 (push (car list) score-file)
1083 (setq list (cdr list))
1085 (when (member (caar list)
1086 gnus-agent-scoreable-headers)
1087 (push (car list) score-file))
1088 (setq list (cdr list)))
1090 (append score-param (list (nreverse score-file)))
1091 score-file nil entries (cdr entries)))
1093 (if (stringp (car score-method))
1095 (list (list score-method)))))))
1097 (gnus-score-headers score-param))
1099 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1101 (or (cdr (assq (mail-header-number gnus-headers)
1102 gnus-newsgroup-scored))
1103 gnus-summary-default-score))
1104 (when (funcall predicate)
1105 (push (mail-header-number gnus-headers)
1107 ;; Fetch the articles.
1109 (gnus-agent-fetch-articles group arts)))
1110 ;; Perhaps we have some additional articles to fetch.
1111 (setq arts (assq 'download (gnus-info-marks
1112 (setq info (gnus-get-info group)))))
1114 (gnus-agent-fetch-articles
1115 group (gnus-uncompress-range (cdr arts)))
1116 (setq marks (delq arts (gnus-info-marks info)))
1117 (gnus-info-set-marks info marks)
1119 (concat "(gnus-group-set-info '"
1120 (gnus-prin1-to-string info)
1124 ;;; Agent Category Mode
1127 (defvar gnus-category-mode-hook nil
1128 "Hook run in `gnus-category-mode' buffers.")
1130 (defvar gnus-category-line-format " %(%20c%): %g\n"
1131 "Format of category lines.")
1133 (defvar gnus-category-mode-line-format "Gnus: %%b"
1134 "The format specification for the category mode line.")
1136 (defvar gnus-agent-short-article 100
1137 "Articles that have fewer lines than this are short.")
1139 (defvar gnus-agent-long-article 200
1140 "Articles that have more lines than this are long.")
1142 (defvar gnus-agent-low-score 0
1143 "Articles that have a score lower than this have a low score.")
1145 (defvar gnus-agent-high-score 0
1146 "Articles that have a score higher than this have a high score.")
1149 ;;; Internal variables.
1151 (defvar gnus-category-buffer "*Agent Category*")
1153 (defvar gnus-category-line-format-alist
1154 `((?c gnus-tmp-name ?s)
1155 (?g gnus-tmp-groups ?d)))
1157 (defvar gnus-category-mode-line-format-alist
1158 `((?u user-defined ?s)))
1160 (defvar gnus-category-line-format-spec nil)
1161 (defvar gnus-category-mode-line-format-spec nil)
1163 (defvar gnus-category-mode-map nil)
1164 (put 'gnus-category-mode 'mode-class 'special)
1166 (unless gnus-category-mode-map
1167 (setq gnus-category-mode-map (make-sparse-keymap))
1168 (suppress-keymap gnus-category-mode-map)
1170 (gnus-define-keys gnus-category-mode-map
1171 "q" gnus-category-exit
1172 "k" gnus-category-kill
1173 "c" gnus-category-copy
1174 "a" gnus-category-add
1175 "p" gnus-category-edit-predicate
1176 "g" gnus-category-edit-groups
1177 "s" gnus-category-edit-score
1178 "l" gnus-category-list
1180 "\C-c\C-i" gnus-info-find-node
1181 "\C-c\C-b" gnus-bug))
1183 (defvar gnus-category-menu-hook nil
1184 "*Hook run after the creation of the menu.")
1186 (defun gnus-category-make-menu-bar ()
1187 (gnus-turn-off-edit-menu 'category)
1188 (unless (boundp 'gnus-category-menu)
1190 gnus-category-menu gnus-category-mode-map ""
1192 ["Add" gnus-category-add t]
1193 ["Kill" gnus-category-kill t]
1194 ["Copy" gnus-category-copy t]
1195 ["Edit predicate" gnus-category-edit-predicate t]
1196 ["Edit score" gnus-category-edit-score t]
1197 ["Edit groups" gnus-category-edit-groups t]
1198 ["Exit" gnus-category-exit t]))
1200 (gnus-run-hooks 'gnus-category-menu-hook)))
1202 (defun gnus-category-mode ()
1203 "Major mode for listing and editing agent categories.
1205 All normal editing commands are switched off.
1206 \\<gnus-category-mode-map>
1207 For more in-depth information on this mode, read the manual
1208 (`\\[gnus-info-find-node]').
1210 The following commands are available:
1212 \\{gnus-category-mode-map}"
1214 (when (gnus-visual-p 'category-menu 'menu)
1215 (gnus-category-make-menu-bar))
1216 (kill-all-local-variables)
1217 (gnus-simplify-mode-line)
1218 (setq major-mode 'gnus-category-mode)
1219 (setq mode-name "Category")
1220 (gnus-set-default-directory)
1221 (setq mode-line-process nil)
1222 (use-local-map gnus-category-mode-map)
1223 (buffer-disable-undo)
1224 (setq truncate-lines t)
1225 (setq buffer-read-only t)
1226 (gnus-run-hooks 'gnus-category-mode-hook))
1228 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1230 (defun gnus-category-insert-line (category)
1231 (let* ((gnus-tmp-name (car category))
1232 (gnus-tmp-groups (length (cadddr category))))
1234 (gnus-add-text-properties
1238 (eval gnus-category-line-format-spec))
1239 (list 'gnus-category gnus-tmp-name))))
1241 (defun gnus-enter-category-buffer ()
1242 "Go to the Category buffer."
1244 (gnus-category-setup-buffer)
1245 (gnus-configure-windows 'category)
1246 (gnus-category-prepare))
1248 (defun gnus-category-setup-buffer ()
1249 (unless (get-buffer gnus-category-buffer)
1251 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1252 (gnus-category-mode))))
1254 (defun gnus-category-prepare ()
1255 (gnus-set-format 'category-mode)
1256 (gnus-set-format 'category t)
1257 (let ((alist gnus-category-alist)
1258 (buffer-read-only nil))
1261 (gnus-category-insert-line (pop alist)))
1262 (goto-char (point-min))
1263 (gnus-category-position-point)))
1265 (defun gnus-category-name ()
1266 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1267 (error "No category on the current line")))
1269 (defun gnus-category-read ()
1270 "Read the category alist."
1271 (setq gnus-category-alist
1272 (or (gnus-agent-read-file
1273 (nnheader-concat gnus-agent-directory "lib/categories"))
1274 (list (list 'default 'short nil nil)))))
1276 (defun gnus-category-write ()
1277 "Write the category alist."
1278 (setq gnus-category-predicate-cache nil
1279 gnus-category-group-cache nil)
1280 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1281 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1282 (prin1 gnus-category-alist (current-buffer))))
1284 (defun gnus-category-edit-predicate (category)
1285 "Edit the predicate for CATEGORY."
1286 (interactive (list (gnus-category-name)))
1287 (let ((info (assq category gnus-category-alist)))
1289 (cadr info) (format "Editing the predicate for category %s" category)
1290 `(lambda (predicate)
1291 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1292 (gnus-category-write)
1293 (gnus-category-list)))))
1295 (defun gnus-category-edit-score (category)
1296 "Edit the score expression for CATEGORY."
1297 (interactive (list (gnus-category-name)))
1298 (let ((info (assq category gnus-category-alist)))
1301 (format "Editing the score expression for category %s" category)
1303 (setcar (nthcdr 2 (assq ',category gnus-category-alist)) groups)
1304 (gnus-category-write)
1305 (gnus-category-list)))))
1307 (defun gnus-category-edit-groups (category)
1308 "Edit the group list for CATEGORY."
1309 (interactive (list (gnus-category-name)))
1310 (let ((info (assq category gnus-category-alist)))
1312 (cadddr info) (format "Editing the group list for category %s" category)
1314 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1315 (gnus-category-write)
1316 (gnus-category-list)))))
1318 (defun gnus-category-kill (category)
1319 "Kill the current category."
1320 (interactive (list (gnus-category-name)))
1321 (let ((info (assq category gnus-category-alist))
1322 (buffer-read-only nil))
1324 (gnus-category-write)
1325 (setq gnus-category-alist (delq info gnus-category-alist))))
1327 (defun gnus-category-copy (category to)
1328 "Copy the current category."
1329 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1330 (let ((info (assq category gnus-category-alist)))
1331 (push (list to (gnus-copy-sequence (cadr info))
1332 (gnus-copy-sequence (caddr info)) nil)
1333 gnus-category-alist)
1334 (gnus-category-write)
1335 (gnus-category-list)))
1337 (defun gnus-category-add (category)
1338 "Create a new category."
1339 (interactive "SCategory name: ")
1340 (when (assq category gnus-category-alist)
1341 (error "Category %s already exists" category))
1342 (push (list category 'false nil nil)
1343 gnus-category-alist)
1344 (gnus-category-write)
1345 (gnus-category-list))
1347 (defun gnus-category-list ()
1348 "List all categories."
1350 (gnus-category-prepare))
1352 (defun gnus-category-exit ()
1353 "Return to the group buffer."
1355 (kill-buffer (current-buffer))
1356 (gnus-configure-windows 'group t))
1358 ;; To avoid having 8-bit characters in the source file.
1359 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1361 (defvar gnus-category-predicate-alist
1362 '((spam . gnus-agent-spam-p)
1363 (short . gnus-agent-short-p)
1364 (long . gnus-agent-long-p)
1365 (low . gnus-agent-low-scored-p)
1366 (high . gnus-agent-high-scored-p)
1367 (true . gnus-agent-true)
1368 (false . gnus-agent-false))
1369 "Mapping from short score predicate symbols to predicate functions.")
1371 (defun gnus-agent-spam-p ()
1372 "Say whether an article is spam or not."
1373 (unless gnus-agent-spam-hashtb
1374 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1375 (if (not (equal (mail-header-references gnus-headers) ""))
1377 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1379 (gnus-gethash string gnus-agent-spam-hashtb)
1380 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1382 (defun gnus-agent-short-p ()
1383 "Say whether an article is short or not."
1384 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1386 (defun gnus-agent-long-p ()
1387 "Say whether an article is long or not."
1388 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1390 (defun gnus-agent-low-scored-p ()
1391 "Say whether an article has a low score or not."
1392 (< gnus-score gnus-agent-low-score))
1394 (defun gnus-agent-high-scored-p ()
1395 "Say whether an article has a high score or not."
1396 (> gnus-score gnus-agent-high-score))
1398 (defun gnus-category-make-function (cat)
1399 "Make a function from category CAT."
1400 `(lambda () ,(gnus-category-make-function-1 cat)))
1402 (defun gnus-agent-true ()
1406 (defun gnus-agent-false ()
1410 (defun gnus-category-make-function-1 (cat)
1411 "Make a function from category CAT."
1413 ;; Functions are just returned as is.
1415 (gnus-functionp cat))
1416 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1418 ;; More complex category.
1421 ((memq (car cat) '(& and))
1423 ((memq (car cat) '(| or))
1425 ((memq (car cat) gnus-category-not)
1427 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1429 (error "Unknown category type: %s" cat))))
1431 (defun gnus-get-predicate (predicate)
1432 "Return the predicate for CATEGORY."
1433 (or (cdr (assoc predicate gnus-category-predicate-cache))
1434 (cdar (push (cons predicate
1435 (gnus-category-make-function predicate))
1436 gnus-category-predicate-cache))))
1438 (defun gnus-group-category (group)
1439 "Return the category GROUP belongs to."
1440 (unless gnus-category-group-cache
1441 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1442 (let ((cs gnus-category-alist)
1444 (while (setq cat (pop cs))
1445 (setq groups (cadddr cat))
1447 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1448 (or (gnus-gethash group gnus-category-group-cache)
1449 (assq 'default gnus-category-alist)))
1451 (defun gnus-agent-expire ()
1452 "Expire all old articles."
1454 (let ((methods gnus-agent-covered-methods)
1455 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1456 gnus-command-method sym group articles
1457 history overview file histories elem art nov-file low info
1458 unreads marked article orig lowest highest)
1460 (setq overview (gnus-get-buffer-create " *expire overview*"))
1461 (while (setq gnus-command-method (pop methods))
1462 (when (file-exists-p (gnus-agent-lib-file "active"))
1464 (insert-file-contents-as-coding-system
1465 gnus-agent-file-coding-system (gnus-agent-lib-file "active"))
1466 (gnus-active-to-gnus-format
1468 (setq orig (gnus-make-hashtable
1469 (count-lines (point-min) (point-max))))))
1470 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1471 (gnus-agent-open-history)
1473 (setq gnus-agent-current-history
1474 (setq history (gnus-agent-history-buffer))))
1475 (goto-char (point-min))
1476 (when (> (buffer-size) 1)
1477 (goto-char (point-min))
1479 (skip-chars-forward "^\t")
1480 (if (> (read (current-buffer)) day)
1481 ;; New article; we don't expire it.
1483 ;; Old article. Schedule it for possible nuking.
1485 (setq sym (let ((obarray expiry-hashtb) s)
1486 (setq s (read (current-buffer)))
1487 (if (stringp s) (intern s) s)))
1489 (set sym (cons (cons (read (current-buffer)) (point))
1490 (symbol-value sym)))
1491 (set sym (list (cons (read (current-buffer)) (point)))))
1492 (skip-chars-forward " "))
1494 ;; We now have all articles that can possibly be expired.
1497 (setq group (symbol-name sym)
1498 articles (sort (symbol-value sym) 'car-less-than-car)
1499 low (car (gnus-active group))
1500 info (gnus-get-info group)
1501 unreads (ignore-errors
1502 (gnus-list-of-unread-articles group))
1504 (gnus-uncompress-range
1505 (cdr (assq 'tick (gnus-info-marks info))))
1506 (gnus-uncompress-range
1507 (cdr (assq 'dormant (gnus-info-marks info))))
1508 (gnus-uncompress-range
1509 (cdr (assq 'save (gnus-info-marks info))))
1510 (gnus-uncompress-range
1511 (cdr (assq 'reply (gnus-info-marks info)))))
1512 nov-file (gnus-agent-article-name ".overview" group)
1515 (gnus-agent-load-alist group)
1516 (gnus-message 5 "Expiring articles in %s" group)
1517 (set-buffer overview)
1519 (when (file-exists-p nov-file)
1520 (nnheader-insert-file-contents nov-file))
1521 (goto-char (point-min))
1523 (while (setq elem (pop articles))
1524 (setq article (car elem))
1525 (when (or (null low)
1527 gnus-agent-expire-all
1528 (and (not (memq article unreads))
1529 (not (memq article marked))))
1530 ;; Find and nuke the NOV line.
1531 (while (and (not (eobp))
1533 (setq art (read (current-buffer)))))
1535 (if (and (numberp art)
1537 (gnus-agent-article-name
1538 (number-to-string art) group)))
1544 ;; Remove old NOV lines that have no articles.
1545 (gnus-delete-line)))
1550 ;; Nuke the article.
1551 (when (file-exists-p
1552 (setq file (gnus-agent-article-name
1553 (number-to-string article)
1556 ;; Schedule the history line for nuking.
1557 (push (cdr elem) histories)))
1558 (gnus-make-directory (file-name-directory nov-file))
1559 (write-region-as-coding-system
1560 gnus-agent-file-coding-system
1561 (point-min) (point-max) nov-file nil 'silent)
1562 ;; Delete the unwanted entries in the alist.
1563 (setq gnus-agent-article-alist
1564 (sort gnus-agent-article-alist 'car-less-than-car))
1565 (let* ((alist gnus-agent-article-alist)
1566 (prev (cons nil alist))
1570 (<= (caar alist) article))
1571 (if (or (not (cdar alist))
1573 (gnus-agent-article-name
1578 (push (caar alist) expired)
1579 (setcdr prev (setq alist (cdr alist))))
1581 alist (cdr alist))))
1582 (setq gnus-agent-article-alist (cdr first))
1583 (gnus-agent-save-alist group)
1584 ;; Mark all articles up to the first article
1585 ;; in `gnus-article-alist' as read.
1586 (when (and info (caar gnus-agent-article-alist))
1587 (setcar (nthcdr 2 info)
1590 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1591 ;; Maybe everything has been expired from `gnus-article-alist'
1592 ;; and so the above marking as read could not be conducted,
1593 ;; or there are expired article within the range of the alist.
1596 (or (not (caar gnus-agent-article-alist))
1598 (caar gnus-agent-article-alist))))
1599 (setcar (nthcdr 2 info)
1602 (nreverse expired))))
1604 (concat "(gnus-group-set-info '"
1605 (gnus-prin1-to-string info)
1608 (if (gnus-gethash group orig)
1609 (setcar (gnus-gethash group orig) lowest)
1610 (gnus-sethash group (cons lowest highest) orig))))
1612 (set-buffer history)
1613 (setq histories (nreverse (sort histories '<)))
1615 (goto-char (pop histories))
1617 (gnus-agent-save-history)
1618 (gnus-agent-close-history)
1619 (gnus-write-active-file-as-coding-system
1620 gnus-agent-file-coding-system
1621 (gnus-agent-lib-file "active") orig))
1622 (gnus-message 4 "Expiry...done")))))))
1625 (defun gnus-agent-batch ()
1627 (let ((init-file-user "")
1628 (gnus-always-read-dribble-file t))
1630 (gnus-group-send-drafts)
1631 (gnus-agent-fetch-session))
1637 (defadvice gnus-group-get-new-news (after gnus-agent-advice
1638 activate preactivate)
1640 (unless (interactive-p)
1641 (gnus-agent-toggle-plugged gnus-plugged)))
1643 (provide 'gnus-agent)
1645 ;;; gnus-agent.el ends here