1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
33 (require 'gnus-score))
35 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
36 "Where the Gnus agent will store its files."
40 (defcustom gnus-agent-plugged-hook nil
41 "Hook run when plugging into the network."
45 (defcustom gnus-agent-unplugged-hook nil
46 "Hook run when unplugging from the network."
50 (defcustom gnus-agent-handle-level gnus-level-subscribed
51 "Groups on levels higher than this variable will be ignored by the Agent."
55 (defcustom gnus-agent-expire-days 7
56 "Read articles older than this will be expired."
60 (defcustom gnus-agent-expire-all nil
61 "If non-nil, also expire unread, ticked and dormant articles.
62 If nil, only read articles will be expired."
66 (defcustom gnus-agent-group-mode-hook nil
67 "Hook run in Agent group minor modes."
71 (defcustom gnus-agent-summary-mode-hook nil
72 "Hook run in Agent summary minor modes."
76 (defcustom gnus-agent-server-mode-hook nil
77 "Hook run in Agent summary minor modes."
81 (defcustom gnus-agent-confirmation-function 'y-or-n-p
82 "Function to confirm when error happens."
86 ;;; Internal variables
88 (defvar gnus-agent-history-buffers nil)
89 (defvar gnus-agent-buffer-alist nil)
90 (defvar gnus-agent-article-alist nil)
91 (defvar gnus-agent-group-alist nil)
92 (defvar gnus-agent-covered-methods nil)
93 (defvar gnus-category-alist nil)
94 (defvar gnus-agent-current-history nil)
95 (defvar gnus-agent-overview-buffer nil)
96 (defvar gnus-category-predicate-cache nil)
97 (defvar gnus-category-group-cache nil)
98 (defvar gnus-agent-spam-hashtb nil)
99 (defvar gnus-agent-file-name nil)
100 (defvar gnus-agent-send-mail-function nil)
101 (defvar gnus-agent-file-coding-system 'raw-text)
103 (defconst gnus-agent-scoreable-headers
104 '("subject" "from" "date" "message-id" "references" "chars" "lines" "xref")
105 "Headers that are considered when scoring articles for download via the Agent.")
108 (defvar gnus-headers)
115 (defun gnus-open-agent ()
117 (gnus-agent-read-servers)
119 (gnus-agent-create-buffer)
120 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
121 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
122 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
124 (defun gnus-agent-create-buffer ()
125 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
127 (setq gnus-agent-overview-buffer
128 (gnus-get-buffer-create " *Gnus agent overview*"))
129 (with-current-buffer gnus-agent-overview-buffer
130 (mm-enable-multibyte))
133 (gnus-add-shutdown 'gnus-close-agent 'gnus)
135 (defun gnus-close-agent ()
136 (setq gnus-agent-covered-methods nil
137 gnus-category-predicate-cache nil
138 gnus-category-group-cache nil
139 gnus-agent-spam-hashtb nil)
140 (gnus-kill-buffer gnus-agent-overview-buffer))
143 ;;; Utility functions
146 (defun gnus-agent-read-file (file)
147 "Load FILE and do a `read' there."
150 (nnheader-insert-file-contents file)
151 (goto-char (point-min))
152 (read (current-buffer)))))
154 (defsubst gnus-agent-method ()
155 (concat (symbol-name (car gnus-command-method)) "/"
156 (if (equal (cadr gnus-command-method) "")
158 (cadr gnus-command-method))))
160 (defsubst gnus-agent-directory ()
161 "Path of the Gnus agent directory."
162 (nnheader-concat gnus-agent-directory
163 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
165 (defun gnus-agent-lib-file (file)
166 "The full path of the Gnus agent library FILE."
167 (concat (gnus-agent-directory) "agent.lib/" file))
169 ;;; Fetching setup functions.
171 (defun gnus-agent-start-fetch ()
172 "Initialize data structures for efficient fetching."
173 (gnus-agent-open-history)
174 (setq gnus-agent-current-history (gnus-agent-history-buffer))
175 (gnus-agent-create-buffer))
177 (defun gnus-agent-stop-fetch ()
178 "Save all data structures and clean up."
179 (gnus-agent-save-history)
180 (gnus-agent-close-history)
181 (setq gnus-agent-spam-hashtb nil)
183 (set-buffer nntp-server-buffer)
186 (defmacro gnus-agent-with-fetch (&rest forms)
190 (gnus-agent-start-fetch)
192 (gnus-agent-stop-fetch)))
194 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
195 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
201 (defvar gnus-agent-mode-hook nil
202 "Hook run when installing agent mode.")
204 (defvar gnus-agent-mode nil)
205 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
207 (defun gnus-agent-mode ()
208 "Minor mode for providing a agent support in Gnus buffers."
209 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
210 (symbol-name major-mode))
211 (match-string 1 (symbol-name major-mode))))
212 (mode (intern (format "gnus-agent-%s-mode" buffer))))
213 (set (make-local-variable 'gnus-agent-mode) t)
215 (set (make-local-variable mode) t)
217 (when (gnus-visual-p 'agent-menu 'menu)
218 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
219 (unless (assq 'gnus-agent-mode minor-mode-alist)
220 (push gnus-agent-mode-status minor-mode-alist))
221 (unless (assq mode minor-mode-map-alist)
222 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
224 minor-mode-map-alist))
225 (when (eq major-mode 'gnus-group-mode)
226 (gnus-agent-toggle-plugged gnus-plugged))
227 (gnus-run-hooks 'gnus-agent-mode-hook
228 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
230 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
231 (gnus-define-keys gnus-agent-group-mode-map
232 "Ju" gnus-agent-fetch-groups
233 "Jc" gnus-enter-category-buffer
234 "Jj" gnus-agent-toggle-plugged
235 "Js" gnus-agent-fetch-session
236 "JY" gnus-agent-synchronize
237 "JS" gnus-group-send-drafts
238 "Ja" gnus-agent-add-group
239 "Jr" gnus-agent-remove-group)
241 (defun gnus-agent-group-make-menu-bar ()
242 (unless (boundp 'gnus-agent-group-menu)
244 gnus-agent-group-menu gnus-agent-group-mode-map ""
246 ["Toggle plugged" gnus-agent-toggle-plugged t]
247 ["List categories" gnus-enter-category-buffer t]
248 ["Send drafts" gnus-group-send-drafts gnus-plugged]
250 ["All" gnus-agent-fetch-session gnus-plugged]
251 ["Group" gnus-agent-fetch-group gnus-plugged])))))
253 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
254 (gnus-define-keys gnus-agent-summary-mode-map
255 "Jj" gnus-agent-toggle-plugged
256 "J#" gnus-agent-mark-article
257 "J\M-#" gnus-agent-unmark-article
258 "@" gnus-agent-toggle-mark
259 "Jc" gnus-agent-catchup)
261 (defun gnus-agent-summary-make-menu-bar ()
262 (unless (boundp 'gnus-agent-summary-menu)
264 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
266 ["Toggle plugged" gnus-agent-toggle-plugged t]
267 ["Mark as downloadable" gnus-agent-mark-article t]
268 ["Unmark as downloadable" gnus-agent-unmark-article t]
269 ["Toggle mark" gnus-agent-toggle-mark t]
270 ["Catchup undownloaded" gnus-agent-catchup t]))))
272 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
273 (gnus-define-keys gnus-agent-server-mode-map
274 "Jj" gnus-agent-toggle-plugged
275 "Ja" gnus-agent-add-server
276 "Jr" gnus-agent-remove-server)
278 (defun gnus-agent-server-make-menu-bar ()
279 (unless (boundp 'gnus-agent-server-menu)
281 gnus-agent-server-menu gnus-agent-server-mode-map ""
283 ["Toggle plugged" gnus-agent-toggle-plugged t]
284 ["Add" gnus-agent-add-server t]
285 ["Remove" gnus-agent-remove-server t]))))
287 (defun gnus-agent-toggle-plugged (plugged)
288 "Toggle whether Gnus is unplugged or not."
289 (interactive (list (not gnus-plugged)))
292 (setq gnus-plugged plugged)
293 (gnus-run-hooks 'gnus-agent-plugged-hook)
294 (setcar (cdr gnus-agent-mode-status) " Plugged"))
295 (gnus-agent-close-connections)
296 (setq gnus-plugged plugged)
297 (gnus-run-hooks 'gnus-agent-unplugged-hook)
298 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
299 (set-buffer-modified-p t))
301 (defun gnus-agent-close-connections ()
302 "Close all methods covered by the Gnus agent."
303 (let ((methods gnus-agent-covered-methods))
305 (gnus-close-server (pop methods)))))
308 (defun gnus-unplugged ()
309 "Start Gnus unplugged."
311 (setq gnus-plugged nil)
315 (defun gnus-plugged ()
316 "Start Gnus plugged."
318 (setq gnus-plugged t)
322 (defun gnus-agentize ()
323 "Allow Gnus to be an offline newsreader.
324 The normal usage of this command is to put the following as the
325 last form in your `.gnus.el' file:
329 This will modify the `gnus-before-startup-hook', `gnus-post-method',
330 and `message-send-mail-function' variables, and install the Gnus
331 agent minor mode in all Gnus buffers."
334 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
335 (unless gnus-agent-send-mail-function
336 (setq gnus-agent-send-mail-function message-send-mail-function
337 message-send-mail-function 'gnus-agent-send-mail))
338 (unless gnus-agent-covered-methods
339 (setq gnus-agent-covered-methods (list gnus-select-method))))
341 (defun gnus-agent-queue-setup ()
342 "Make sure the queue group exists."
343 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
344 (gnus-request-create-group "queue" '(nndraft ""))
345 (let ((gnus-level-default-subscribed 1))
346 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
347 (gnus-group-set-parameter
348 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
350 (defun gnus-agent-send-mail ()
352 (funcall gnus-agent-send-mail-function)
353 (goto-char (point-min))
355 (concat "^" (regexp-quote mail-header-separator) "\n"))
357 (gnus-agent-insert-meta-information 'mail)
358 (gnus-request-accept-article "nndraft:queue" nil t t)))
360 (defun gnus-agent-insert-meta-information (type &optional method)
361 "Insert meta-information into the message that says how it's to be posted.
362 TYPE can be either `mail' or `news'. If the latter METHOD can
365 (message-remove-header gnus-agent-meta-information-header)
366 (goto-char (point-min))
367 (insert gnus-agent-meta-information-header ": "
368 (symbol-name type) " " (format "%S" method)
371 (while (search-backward "\n" nil t)
372 (replace-match "\\n" t t))))
375 ;;; Group mode commands
378 (defun gnus-agent-fetch-groups (n)
379 "Put all new articles in the current groups into the Agent."
382 (error "Groups can't be fetched when Gnus is unplugged"))
383 (gnus-group-iterate n 'gnus-agent-fetch-group))
385 (defun gnus-agent-fetch-group (group)
386 "Put all new articles in GROUP into the Agent."
387 (interactive (list (gnus-group-group-name)))
389 (error "Groups can't be fetched when Gnus is unplugged"))
391 (error "No group on the current line"))
392 (let ((gnus-command-method (gnus-find-method-for-group group)))
393 (gnus-agent-with-fetch
394 (gnus-agent-fetch-group-1 group gnus-command-method)
395 (gnus-message 5 "Fetching %s...done" group))))
397 (defun gnus-agent-add-group (category arg)
398 "Add the current group to an agent category."
404 (mapcar (lambda (cat) (list (symbol-name (car cat))))
408 (let ((cat (assq category gnus-category-alist))
410 (gnus-group-iterate arg
412 (when (cadddr (setq c (gnus-group-category group)))
413 (setf (cadddr c) (delete group (cadddr c))))
414 (push group groups)))
415 (setf (cadddr cat) (nconc (cadddr cat) groups))
416 (gnus-category-write)))
418 (defun gnus-agent-remove-group (arg)
419 "Remove the current group from its agent category, if any."
422 (gnus-group-iterate arg
424 (when (cadddr (setq c (gnus-group-category group)))
425 (setf (cadddr c) (delete group (cadddr c))))))
426 (gnus-category-write)))
428 (defun gnus-agent-synchronize ()
429 "Synchronize local, unplugged, data with backend.
430 Currently sends flag setting requests, if any."
433 (dolist (gnus-command-method gnus-agent-covered-methods)
434 (when (file-exists-p (gnus-agent-lib-file "flags"))
435 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
437 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
438 (if (null (gnus-check-server gnus-command-method))
439 (message "Couldn't open server %s" (nth 1 gnus-command-method))
441 (if (null (eval (read (current-buffer))))
442 (progn (forward-line)
444 (write-file (gnus-agent-lib-file "flags"))
445 (error "Couldn't set flags from file %s"
446 (gnus-agent-lib-file "flags"))))
447 (write-file (gnus-agent-lib-file "flags")))
448 (kill-buffer nil)))))
451 ;;; Server mode commands
454 (defun gnus-agent-add-server (server)
455 "Enroll SERVER in the agent program."
456 (interactive (list (gnus-server-server-name)))
458 (error "No server on the current line"))
459 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
460 (when (member method gnus-agent-covered-methods)
461 (error "Server already in the agent program"))
462 (push method gnus-agent-covered-methods)
463 (gnus-agent-write-servers)
464 (message "Entered %s into the Agent" server)))
466 (defun gnus-agent-remove-server (server)
467 "Remove SERVER from the agent program."
468 (interactive (list (gnus-server-server-name)))
470 (error "No server on the current line"))
471 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
472 (unless (member method gnus-agent-covered-methods)
473 (error "Server not in the agent program"))
474 (setq gnus-agent-covered-methods
475 (delete method gnus-agent-covered-methods))
476 (gnus-agent-write-servers)
477 (message "Removed %s from the agent" server)))
479 (defun gnus-agent-read-servers ()
480 "Read the alist of covered servers."
481 (setq gnus-agent-covered-methods
482 (gnus-agent-read-file
483 (nnheader-concat gnus-agent-directory "lib/servers"))))
485 (defun gnus-agent-write-servers ()
486 "Write the alist of covered servers."
487 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
488 (let ((coding-system-for-write nnheader-file-coding-system)
489 (file-name-coding-system nnmail-pathname-coding-system))
490 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
491 (prin1 gnus-agent-covered-methods (current-buffer)))))
497 (defun gnus-agent-mark-article (n &optional unmark)
498 "Mark the next N articles as downloadable.
499 If N is negative, mark backward instead. If UNMARK is non-nil, remove
500 the mark instead. The difference between N and the actual number of
501 articles marked is returned."
503 (let ((backward (< n 0))
508 (gnus-summary-set-agent-mark
509 (gnus-summary-article-number) unmark)
510 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
513 (gnus-message 7 "No more articles"))
514 (gnus-summary-recenter)
515 (gnus-summary-position-point)
518 (defun gnus-agent-unmark-article (n)
519 "Remove the downloadable mark from the next N articles.
520 If N is negative, unmark backward instead. The difference between N and
521 the actual number of articles unmarked is returned."
523 (gnus-agent-mark-article n t))
525 (defun gnus-agent-toggle-mark (n)
526 "Toggle the downloadable mark from the next N articles.
527 If N is negative, toggle backward instead. The difference between N and
528 the actual number of articles toggled is returned."
530 (gnus-agent-mark-article n 'toggle))
532 (defun gnus-summary-set-agent-mark (article &optional unmark)
533 "Mark ARTICLE as downloadable."
534 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
535 (memq article gnus-newsgroup-downloadable)
539 (setq gnus-newsgroup-downloadable
540 (delq article gnus-newsgroup-downloadable))
541 (push article gnus-newsgroup-undownloaded))
542 (setq gnus-newsgroup-undownloaded
543 (delq article gnus-newsgroup-undownloaded))
544 (push article gnus-newsgroup-downloadable))
545 (gnus-summary-update-mark
546 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
549 (defun gnus-agent-get-undownloaded-list ()
550 "Mark all unfetched articles as read."
551 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
552 (when (and (not gnus-plugged)
553 (gnus-agent-method-p gnus-command-method))
554 (gnus-agent-load-alist gnus-newsgroup-name)
555 ;; First mark all undownloaded articles as undownloaded.
556 (let ((articles (append gnus-newsgroup-unreads
557 gnus-newsgroup-marked
558 gnus-newsgroup-dormant))
560 (while (setq article (pop articles))
561 (unless (or (cdr (assq article gnus-agent-article-alist))
562 (memq article gnus-newsgroup-downloadable)
563 (memq article gnus-newsgroup-cached))
564 (push article gnus-newsgroup-undownloaded))))
565 ;; Then mark downloaded downloadable as not-downloadable,
566 ;; if you get my drift.
567 (let ((articles gnus-newsgroup-downloadable)
569 (while (setq article (pop articles))
570 (when (cdr (assq article gnus-agent-article-alist))
571 (setq gnus-newsgroup-downloadable
572 (delq article gnus-newsgroup-downloadable))))))))
574 (defun gnus-agent-catchup ()
575 "Mark all undownloaded articles as read."
578 (while gnus-newsgroup-undownloaded
579 (gnus-summary-mark-article
580 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
581 (gnus-summary-position-point))
584 ;;; Internal functions
587 (defun gnus-agent-save-active (method)
588 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
590 (defun gnus-agent-save-active-1 (method function)
591 (when (gnus-agent-method-p method)
592 (let* ((gnus-command-method method)
593 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
594 (file (gnus-agent-lib-file "active")))
595 (funcall function nil new)
596 (gnus-agent-write-active file new)
598 (nnheader-insert-file-contents file))))
600 (defun gnus-agent-write-active (file new)
601 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
602 (file (gnus-agent-lib-file "active"))
604 (when (file-exists-p file)
606 (nnheader-insert-file-contents file)
607 (gnus-active-to-gnus-format nil orig))
610 (when (and sym (boundp sym))
611 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
612 (setq elem (symbol-value osym)))
613 (setcdr elem (cdr (symbol-value sym)))
614 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
616 (gnus-make-directory (file-name-directory file))
617 (let ((coding-system-for-write gnus-agent-file-coding-system))
618 ;; The hashtable contains real names of groups, no more prefix
619 ;; removing, so set `full' to `t'.
620 (gnus-write-active-file file orig t))))
622 (defun gnus-agent-save-groups (method)
623 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
625 (defun gnus-agent-save-group-info (method group active)
626 (when (gnus-agent-method-p method)
627 (let* ((gnus-command-method method)
628 (coding-system-for-write nnheader-file-coding-system)
629 (file-name-coding-system nnmail-pathname-coding-system)
630 (file (gnus-agent-lib-file "active"))
632 (gnus-make-directory (file-name-directory file))
634 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
635 (mm-disable-multibyte)
636 (when (file-exists-p file)
637 (nnheader-insert-file-contents file))
638 (goto-char (point-min))
639 (when (re-search-forward
640 (concat "^" (regexp-quote group) " ") nil t)
643 (narrow-to-region (match-beginning 0)
647 (setq oactive (car (nnmail-parse-active)))))
649 (insert (format "%S %d %d y\n" (intern group)
651 (or (car oactive) (car active))))
652 (goto-char (point-max))
653 (while (search-backward "\\." nil t)
656 (defun gnus-agent-group-path (group)
657 "Translate GROUP into a path."
658 (if nnmail-use-long-file-names
659 (gnus-group-real-name group)
660 (nnheader-translate-file-chars
661 (nnheader-replace-chars-in-string
662 (nnheader-replace-duplicate-chars-in-string
663 (nnheader-replace-chars-in-string
664 (gnus-group-real-name group)
671 (defun gnus-agent-method-p (method)
672 "Say whether METHOD is covered by the agent."
673 (member method gnus-agent-covered-methods))
675 (defun gnus-agent-get-function (method)
676 (if (and (not gnus-plugged)
677 (gnus-agent-method-p method))
683 ;;; History functions
685 (defun gnus-agent-history-buffer ()
686 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
688 (defun gnus-agent-open-history ()
690 (push (cons (gnus-agent-method)
691 (set-buffer (gnus-get-buffer-create
692 (format " *Gnus agent %s history*"
693 (gnus-agent-method)))))
694 gnus-agent-history-buffers)
695 (mm-disable-multibyte) ;; everything is binary
698 (let ((file (gnus-agent-lib-file "history")))
699 (when (file-exists-p file)
700 (nnheader-insert-file-contents file))
701 (set (make-local-variable 'gnus-agent-file-name) file))))
703 (defun gnus-agent-save-history ()
705 (set-buffer gnus-agent-current-history)
706 (gnus-make-directory (file-name-directory gnus-agent-file-name))
707 (let ((coding-system-for-write gnus-agent-file-coding-system))
708 (write-region (1+ (point-min)) (point-max)
709 gnus-agent-file-name nil 'silent))))
711 (defun gnus-agent-close-history ()
712 (when (gnus-buffer-live-p gnus-agent-current-history)
713 (kill-buffer gnus-agent-current-history)
714 (setq gnus-agent-history-buffers
715 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
716 gnus-agent-history-buffers))))
718 (defun gnus-agent-enter-history (id group-arts date)
720 (set-buffer gnus-agent-current-history)
721 (goto-char (point-max))
723 (insert id "\t" (number-to-string date) "\t")
725 (insert (format "%S" (intern (caar group-arts)))
726 " " (number-to-string (cdr (pop group-arts)))
729 (while (search-backward "\\." p t)
732 (defun gnus-agent-article-in-history-p (id)
734 (set-buffer (gnus-agent-history-buffer))
735 (goto-char (point-min))
736 (search-forward (concat "\n" id "\t") nil t)))
738 (defun gnus-agent-history-path (id)
740 (set-buffer (gnus-agent-history-buffer))
741 (goto-char (point-min))
742 (when (search-forward (concat "\n" id "\t") nil t)
743 (let ((method (gnus-agent-method)))
745 (while (not (numberp (setq group (read (current-buffer)))))
746 (push (concat method "/" group) paths))
747 (nreverse paths))))))
753 (defun gnus-agent-fetch-articles (group articles)
754 "Fetch ARTICLES from GROUP and put them into the Agent."
756 ;; Prune off articles that we have already fetched.
758 (cdr (assq (car articles) gnus-agent-article-alist)))
760 (let ((arts articles))
762 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
763 (setcdr arts (cddr arts))
764 (setq arts (cdr arts)))))
767 (gnus-agent-directory)
768 (gnus-agent-group-path group) "/"))
769 (date (time-to-days (current-time)))
772 (gnus-make-directory dir)
773 (gnus-message 7 "Fetching articles for %s..." group)
774 ;; Fetch the articles from the backend.
775 (if (gnus-check-backend-function 'retrieve-articles group)
776 (setq pos (gnus-retrieve-articles articles group))
779 (while (setq article (pop articles))
781 (gnus-backlog-request-article group article
783 (gnus-request-article article group))
784 (goto-char (point-max))
785 (push (cons article (point)) pos)
786 (insert-buffer-substring nntp-server-buffer)))
787 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
788 (setq pos (nreverse pos)))))
789 ;; Then save these articles into the Agent.
791 (set-buffer nntp-server-buffer)
793 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
794 (goto-char (point-min))
795 (when (search-forward "\n\n" nil t)
796 (when (search-backward "\nXrefs: " nil t)
797 ;; Handle crossposting.
798 (skip-chars-forward "^ ")
799 (skip-chars-forward " ")
801 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
802 (push (cons (buffer-substring (match-beginning 1)
804 (buffer-substring (match-beginning 2)
807 (goto-char (match-end 0)))
808 (gnus-agent-crosspost crosses (caar pos))))
809 (goto-char (point-min))
810 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
811 (setq id "No-Message-ID-in-article")
812 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
813 (let ((coding-system-for-write
814 gnus-agent-file-coding-system))
815 (write-region (point-min) (point-max)
816 (concat dir (number-to-string (caar pos)))
818 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
820 (gnus-agent-enter-history
821 id (or crosses (list (cons group (caar pos)))) date)
824 (gnus-agent-save-alist group)))))
826 (defun gnus-agent-crosspost (crosses article)
827 (let (gnus-agent-article-alist group alist beg end)
829 (set-buffer gnus-agent-overview-buffer)
830 (when (nnheader-find-nov-line article)
833 (setq end (progn (forward-line 1) (point)))))
835 (setq group (caar crosses))
836 (unless (setq alist (assoc group gnus-agent-group-alist))
837 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
838 gnus-agent-group-alist))
839 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
841 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
843 (when (= (point-max) (point-min))
844 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
846 (nnheader-insert-file-contents
847 (gnus-agent-article-name ".overview" group))))
848 (nnheader-find-nov-line (string-to-number (cdar crosses)))
849 (insert (string-to-number (cdar crosses)))
850 (insert-buffer-substring gnus-agent-overview-buffer beg end))
853 (defun gnus-agent-flush-cache ()
855 (while gnus-agent-buffer-alist
856 (set-buffer (cdar gnus-agent-buffer-alist))
857 (let ((coding-system-for-write
858 gnus-agent-file-coding-system))
859 (write-region (point-min) (point-max)
860 (gnus-agent-article-name ".overview"
861 (caar gnus-agent-buffer-alist))
863 (pop gnus-agent-buffer-alist))
864 (while gnus-agent-group-alist
865 (with-temp-file (caar gnus-agent-group-alist)
866 (princ (cdar gnus-agent-group-alist))
868 (pop gnus-agent-group-alist))))
870 (defun gnus-agent-fetch-headers (group &optional force)
871 (let ((articles (gnus-list-of-unread-articles group))
872 (gnus-decode-encoded-word-function 'identity)
873 (file (gnus-agent-article-name ".overview" group)))
874 ;; Add article with marks to list of article headers we want to fetch.
875 (dolist (arts (gnus-info-marks (gnus-get-info group)))
876 (setq articles (union (gnus-uncompress-sequence (cdr arts))
878 (setq articles (sort articles '<))
879 ;; Remove known articles.
880 (when (gnus-agent-load-alist group)
881 (setq articles (gnus-sorted-intersection
883 (gnus-uncompress-range
884 (cons (1+ (caar (last gnus-agent-article-alist)))
885 (cdr (gnus-active group)))))))
887 (gnus-make-directory (nnheader-translate-file-chars
888 (file-name-directory file) t))
890 (gnus-message 7 "Fetching headers for %s..." group)
892 (set-buffer nntp-server-buffer)
893 (unless (eq 'nov (gnus-retrieve-headers articles group))
894 (nnvirtual-convert-headers))
895 ;; Save these headers for later processing.
896 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
897 (when (file-exists-p file)
898 (gnus-agent-braid-nov group articles file))
899 (let ((coding-system-for-write
900 gnus-agent-file-coding-system))
901 (write-region (point-min) (point-max) file nil 'silent))
902 (gnus-agent-save-alist group articles nil)
903 (gnus-agent-enter-history
904 "last-header-fetched-for-session"
905 (list (cons group (nth (- (length articles) 1) articles)))
906 (time-to-days (current-time)))
909 (defsubst gnus-agent-copy-nov-line (article)
911 (set-buffer gnus-agent-overview-buffer)
913 (if (eq article (read (current-buffer)))
914 (setq e (progn (forward-line 1) (point)))
918 (set-buffer nntp-server-buffer)
919 (insert-buffer-substring gnus-agent-overview-buffer b e)))
921 (defun gnus-agent-braid-nov (group articles file)
922 (set-buffer gnus-agent-overview-buffer)
923 (goto-char (point-min))
924 (set-buffer nntp-server-buffer)
926 (nnheader-insert-file-contents file)
927 (goto-char (point-max))
928 (if (or (= (point-min) (point-max))
931 (< (read (current-buffer)) (car articles))))
932 ;; We have only headers that are after the older headers,
933 ;; so we just append them.
935 (goto-char (point-max))
936 (insert-buffer-substring gnus-agent-overview-buffer))
937 ;; We do it the hard way.
938 (nnheader-find-nov-line (car articles))
939 (gnus-agent-copy-nov-line (car articles))
943 (while (and (not (eobp))
944 (< (read (current-buffer)) (car articles)))
948 (gnus-agent-copy-nov-line (car articles))
949 (setq articles (cdr articles))))
952 (set-buffer gnus-agent-overview-buffer)
955 (set-buffer nntp-server-buffer)
956 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
958 (defun gnus-agent-load-alist (group &optional dir)
959 "Load the article-state alist for GROUP."
960 (setq gnus-agent-article-alist
961 (gnus-agent-read-file
963 (concat dir ".agentview")
964 (gnus-agent-article-name ".agentview" group)))))
966 (defun gnus-agent-save-alist (group &optional articles state dir)
967 "Save the article-state alist for GROUP."
968 (let ((file-name-coding-system nnmail-pathname-coding-system))
969 (with-temp-file (if dir
970 (concat dir ".agentview")
971 (gnus-agent-article-name ".agentview" group))
972 (princ (setq gnus-agent-article-alist
973 (nconc gnus-agent-article-alist
974 (mapcar (lambda (article) (cons article state))
979 (defun gnus-agent-article-name (article group)
980 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
981 (if (stringp article) article (string-to-number article))))
983 (defun gnus-agent-batch-confirmation (msg)
984 "Show error message and return t."
989 (defun gnus-agent-batch-fetch ()
990 "Start Gnus and fetch session."
993 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
994 (gnus-agent-fetch-session))
997 (defun gnus-agent-fetch-session ()
998 "Fetch all articles and headers that are eligible for fetching."
1000 (unless gnus-agent-covered-methods
1001 (error "No servers are covered by the Gnus agent"))
1002 (unless gnus-plugged
1003 (error "Can't fetch articles while Gnus is unplugged"))
1004 (let ((methods gnus-agent-covered-methods)
1005 groups group gnus-command-method)
1010 (setq gnus-command-method (car methods))
1011 (when (or (gnus-server-opened gnus-command-method)
1012 (gnus-open-server gnus-command-method))
1013 (setq groups (gnus-groups-from-server (car methods)))
1014 (gnus-agent-with-fetch
1015 (while (setq group (pop groups))
1016 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1017 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1019 (unless (funcall gnus-agent-confirmation-function
1020 (format "Error (%s). Continue? " err))
1021 (error "Cannot fetch articles into the Gnus agent."))))
1023 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1025 (defun gnus-agent-fetch-group-1 (group method)
1027 (let ((gnus-command-method method)
1028 (gnus-newsgroup-name group)
1029 gnus-newsgroup-dependencies gnus-newsgroup-headers
1030 gnus-newsgroup-scored gnus-headers gnus-score
1031 gnus-use-cache articles arts
1032 category predicate info marks score-param)
1033 (unless (gnus-check-group group)
1034 (error "Can't open server for %s" group))
1036 (when (and (or (gnus-active group) (gnus-activate-group group))
1037 (setq articles (gnus-agent-fetch-headers group))
1039 ;; Parse them and see which articles we want to fetch.
1040 (setq gnus-newsgroup-dependencies
1041 (make-vector (length articles) 0))
1042 ;; No need to call `gnus-get-newsgroup-headers-xover' with
1043 ;; the entire .overview for group as we still have the just
1044 ;; downloaded headers in `gnus-agent-overview-buffer'.
1045 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1046 (setq gnus-newsgroup-headers
1047 (gnus-get-newsgroup-headers-xover articles nil nil
1049 ;; `gnus-agent-overview-buffer' may be killed for
1050 ;; timeout reason. If so, recreate it.
1051 (gnus-agent-create-buffer)))
1052 (setq category (gnus-group-category group))
1055 (or (gnus-group-find-parameter group 'agent-predicate t)
1057 ;; Do we want to download everything, or nothing?
1058 (if (or (eq (caaddr predicate) 'gnus-agent-true)
1059 (eq (caaddr predicate) 'gnus-agent-false))
1061 (setq arts (symbol-value
1062 (cadr (assoc (caaddr predicate)
1063 '((gnus-agent-true articles)
1064 (gnus-agent-false nil))))))
1065 ;; No, we need to decide what we want.
1069 (gnus-group-find-parameter group 'agent-score t)
1072 (require 'gnus-score)
1073 (if (eq score-method 'file)
1075 (gnus-score-load-files
1076 (gnus-all-score-files group)))
1078 (while (setq list (car entries))
1079 (push (car list) score-file)
1080 (setq list (cdr list))
1082 (when (member (caar list)
1083 gnus-agent-scoreable-headers)
1084 (push (car list) score-file))
1085 (setq list (cdr list)))
1087 (append score-param (list (nreverse score-file)))
1088 score-file nil entries (cdr entries)))
1090 (if (stringp (car score-method))
1092 (list (list score-method)))))))
1094 (gnus-score-headers score-param))
1096 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1098 (or (cdr (assq (mail-header-number gnus-headers)
1099 gnus-newsgroup-scored))
1100 gnus-summary-default-score))
1101 (when (funcall predicate)
1102 (push (mail-header-number gnus-headers)
1104 ;; Fetch the articles.
1106 (gnus-agent-fetch-articles group arts)))
1107 ;; Perhaps we have some additional articles to fetch.
1108 (setq arts (assq 'download (gnus-info-marks
1109 (setq info (gnus-get-info group)))))
1111 (gnus-agent-fetch-articles
1112 group (gnus-uncompress-range (cdr arts)))
1113 (setq marks (delq arts (gnus-info-marks info)))
1114 (gnus-info-set-marks info marks)
1116 (concat "(gnus-group-set-info '"
1117 (gnus-prin1-to-string info)
1121 ;;; Agent Category Mode
1124 (defvar gnus-category-mode-hook nil
1125 "Hook run in `gnus-category-mode' buffers.")
1127 (defvar gnus-category-line-format " %(%20c%): %g\n"
1128 "Format of category lines.")
1130 (defvar gnus-category-mode-line-format "Gnus: %%b"
1131 "The format specification for the category mode line.")
1133 (defvar gnus-agent-short-article 100
1134 "Articles that have fewer lines than this are short.")
1136 (defvar gnus-agent-long-article 200
1137 "Articles that have more lines than this are long.")
1139 (defvar gnus-agent-low-score 0
1140 "Articles that have a score lower than this have a low score.")
1142 (defvar gnus-agent-high-score 0
1143 "Articles that have a score higher than this have a high score.")
1146 ;;; Internal variables.
1148 (defvar gnus-category-buffer "*Agent Category*")
1150 (defvar gnus-category-line-format-alist
1151 `((?c gnus-tmp-name ?s)
1152 (?g gnus-tmp-groups ?d)))
1154 (defvar gnus-category-mode-line-format-alist
1155 `((?u user-defined ?s)))
1157 (defvar gnus-category-line-format-spec nil)
1158 (defvar gnus-category-mode-line-format-spec nil)
1160 (defvar gnus-category-mode-map nil)
1161 (put 'gnus-category-mode 'mode-class 'special)
1163 (unless gnus-category-mode-map
1164 (setq gnus-category-mode-map (make-sparse-keymap))
1165 (suppress-keymap gnus-category-mode-map)
1167 (gnus-define-keys gnus-category-mode-map
1168 "q" gnus-category-exit
1169 "k" gnus-category-kill
1170 "c" gnus-category-copy
1171 "a" gnus-category-add
1172 "p" gnus-category-edit-predicate
1173 "g" gnus-category-edit-groups
1174 "s" gnus-category-edit-score
1175 "l" gnus-category-list
1177 "\C-c\C-i" gnus-info-find-node
1178 "\C-c\C-b" gnus-bug))
1180 (defvar gnus-category-menu-hook nil
1181 "*Hook run after the creation of the menu.")
1183 (defun gnus-category-make-menu-bar ()
1184 (gnus-turn-off-edit-menu 'category)
1185 (unless (boundp 'gnus-category-menu)
1187 gnus-category-menu gnus-category-mode-map ""
1189 ["Add" gnus-category-add t]
1190 ["Kill" gnus-category-kill t]
1191 ["Copy" gnus-category-copy t]
1192 ["Edit predicate" gnus-category-edit-predicate t]
1193 ["Edit score" gnus-category-edit-score t]
1194 ["Edit groups" gnus-category-edit-groups t]
1195 ["Exit" gnus-category-exit t]))
1197 (gnus-run-hooks 'gnus-category-menu-hook)))
1199 (defun gnus-category-mode ()
1200 "Major mode for listing and editing agent categories.
1202 All normal editing commands are switched off.
1203 \\<gnus-category-mode-map>
1204 For more in-depth information on this mode, read the manual
1205 (`\\[gnus-info-find-node]').
1207 The following commands are available:
1209 \\{gnus-category-mode-map}"
1211 (when (gnus-visual-p 'category-menu 'menu)
1212 (gnus-category-make-menu-bar))
1213 (kill-all-local-variables)
1214 (gnus-simplify-mode-line)
1215 (setq major-mode 'gnus-category-mode)
1216 (setq mode-name "Category")
1217 (gnus-set-default-directory)
1218 (setq mode-line-process nil)
1219 (use-local-map gnus-category-mode-map)
1220 (buffer-disable-undo)
1221 (setq truncate-lines t)
1222 (setq buffer-read-only t)
1223 (gnus-run-hooks 'gnus-category-mode-hook))
1225 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1227 (defun gnus-category-insert-line (category)
1228 (let* ((gnus-tmp-name (car category))
1229 (gnus-tmp-groups (length (cadddr category))))
1231 (gnus-add-text-properties
1235 (eval gnus-category-line-format-spec))
1236 (list 'gnus-category gnus-tmp-name))))
1238 (defun gnus-enter-category-buffer ()
1239 "Go to the Category buffer."
1241 (gnus-category-setup-buffer)
1242 (gnus-configure-windows 'category)
1243 (gnus-category-prepare))
1245 (defun gnus-category-setup-buffer ()
1246 (unless (get-buffer gnus-category-buffer)
1248 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1249 (gnus-category-mode))))
1251 (defun gnus-category-prepare ()
1252 (gnus-set-format 'category-mode)
1253 (gnus-set-format 'category t)
1254 (let ((alist gnus-category-alist)
1255 (buffer-read-only nil))
1258 (gnus-category-insert-line (pop alist)))
1259 (goto-char (point-min))
1260 (gnus-category-position-point)))
1262 (defun gnus-category-name ()
1263 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1264 (error "No category on the current line")))
1266 (defun gnus-category-read ()
1267 "Read the category alist."
1268 (setq gnus-category-alist
1269 (or (gnus-agent-read-file
1270 (nnheader-concat gnus-agent-directory "lib/categories"))
1271 (list (list 'default 'short nil nil)))))
1273 (defun gnus-category-write ()
1274 "Write the category alist."
1275 (setq gnus-category-predicate-cache nil
1276 gnus-category-group-cache nil)
1277 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1278 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1279 (prin1 gnus-category-alist (current-buffer))))
1281 (defun gnus-category-edit-predicate (category)
1282 "Edit the predicate for CATEGORY."
1283 (interactive (list (gnus-category-name)))
1284 (let ((info (assq category gnus-category-alist)))
1286 (cadr info) (format "Editing the predicate for category %s" category)
1287 `(lambda (predicate)
1288 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1289 (gnus-category-write)
1290 (gnus-category-list)))))
1292 (defun gnus-category-edit-score (category)
1293 "Edit the score expression for CATEGORY."
1294 (interactive (list (gnus-category-name)))
1295 (let ((info (assq category gnus-category-alist)))
1298 (format "Editing the score expression for category %s" category)
1300 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1301 (gnus-category-write)
1302 (gnus-category-list)))))
1304 (defun gnus-category-edit-groups (category)
1305 "Edit the group list for CATEGORY."
1306 (interactive (list (gnus-category-name)))
1307 (let ((info (assq category gnus-category-alist)))
1309 (cadddr info) (format "Editing the group list for category %s" category)
1311 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1312 (gnus-category-write)
1313 (gnus-category-list)))))
1315 (defun gnus-category-kill (category)
1316 "Kill the current category."
1317 (interactive (list (gnus-category-name)))
1318 (let ((info (assq category gnus-category-alist))
1319 (buffer-read-only nil))
1321 (setq gnus-category-alist (delq info gnus-category-alist))
1322 (gnus-category-write)))
1324 (defun gnus-category-copy (category to)
1325 "Copy the current category."
1326 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1327 (let ((info (assq category gnus-category-alist)))
1328 (push (list to (gnus-copy-sequence (cadr info))
1329 (gnus-copy-sequence (caddr info)) nil)
1330 gnus-category-alist)
1331 (gnus-category-write)
1332 (gnus-category-list)))
1334 (defun gnus-category-add (category)
1335 "Create a new category."
1336 (interactive "SCategory name: ")
1337 (when (assq category gnus-category-alist)
1338 (error "Category %s already exists" category))
1339 (push (list category 'false nil nil)
1340 gnus-category-alist)
1341 (gnus-category-write)
1342 (gnus-category-list))
1344 (defun gnus-category-list ()
1345 "List all categories."
1347 (gnus-category-prepare))
1349 (defun gnus-category-exit ()
1350 "Return to the group buffer."
1352 (kill-buffer (current-buffer))
1353 (gnus-configure-windows 'group t))
1355 ;; To avoid having 8-bit characters in the source file.
1356 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1358 (defvar gnus-category-predicate-alist
1359 '((spam . gnus-agent-spam-p)
1360 (short . gnus-agent-short-p)
1361 (long . gnus-agent-long-p)
1362 (low . gnus-agent-low-scored-p)
1363 (high . gnus-agent-high-scored-p)
1364 (true . gnus-agent-true)
1365 (false . gnus-agent-false))
1366 "Mapping from short score predicate symbols to predicate functions.")
1368 (defun gnus-agent-spam-p ()
1369 "Say whether an article is spam or not."
1370 (unless gnus-agent-spam-hashtb
1371 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1372 (if (not (equal (mail-header-references gnus-headers) ""))
1374 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1376 (gnus-gethash string gnus-agent-spam-hashtb)
1377 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1379 (defun gnus-agent-short-p ()
1380 "Say whether an article is short or not."
1381 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1383 (defun gnus-agent-long-p ()
1384 "Say whether an article is long or not."
1385 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1387 (defun gnus-agent-low-scored-p ()
1388 "Say whether an article has a low score or not."
1389 (< gnus-score gnus-agent-low-score))
1391 (defun gnus-agent-high-scored-p ()
1392 "Say whether an article has a high score or not."
1393 (> gnus-score gnus-agent-high-score))
1395 (defun gnus-category-make-function (cat)
1396 "Make a function from category CAT."
1397 `(lambda () ,(gnus-category-make-function-1 cat)))
1399 (defun gnus-agent-true ()
1403 (defun gnus-agent-false ()
1407 (defun gnus-category-make-function-1 (cat)
1408 "Make a function from category CAT."
1410 ;; Functions are just returned as is.
1412 (gnus-functionp cat))
1413 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1415 ;; More complex category.
1418 ((memq (car cat) '(& and))
1420 ((memq (car cat) '(| or))
1422 ((memq (car cat) gnus-category-not)
1424 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1426 (error "Unknown category type: %s" cat))))
1428 (defun gnus-get-predicate (predicate)
1429 "Return the predicate for CATEGORY."
1430 (or (cdr (assoc predicate gnus-category-predicate-cache))
1431 (cdar (push (cons predicate
1432 (gnus-category-make-function predicate))
1433 gnus-category-predicate-cache))))
1435 (defun gnus-group-category (group)
1436 "Return the category GROUP belongs to."
1437 (unless gnus-category-group-cache
1438 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1439 (let ((cs gnus-category-alist)
1441 (while (setq cat (pop cs))
1442 (setq groups (cadddr cat))
1444 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1445 (or (gnus-gethash group gnus-category-group-cache)
1446 (assq 'default gnus-category-alist)))
1448 (defun gnus-agent-expire ()
1449 "Expire all old articles."
1451 (let ((methods gnus-agent-covered-methods)
1452 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1453 gnus-command-method sym group articles
1454 history overview file histories elem art nov-file low info
1455 unreads marked article orig lowest highest)
1457 (setq overview (gnus-get-buffer-create " *expire overview*"))
1458 (while (setq gnus-command-method (pop methods))
1459 (when (file-exists-p (gnus-agent-lib-file "active"))
1461 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1462 (gnus-active-to-gnus-format
1464 (setq orig (gnus-make-hashtable
1465 (count-lines (point-min) (point-max))))))
1466 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1467 (gnus-agent-open-history)
1469 (setq gnus-agent-current-history
1470 (setq history (gnus-agent-history-buffer))))
1471 (goto-char (point-min))
1472 (when (> (buffer-size) 1)
1473 (goto-char (point-min))
1475 (skip-chars-forward "^\t")
1476 (if (> (read (current-buffer)) day)
1477 ;; New article; we don't expire it.
1479 ;; Old article. Schedule it for possible nuking.
1481 (setq sym (let ((obarray expiry-hashtb) s)
1482 (setq s (read (current-buffer)))
1483 (if (stringp s) (intern s) s)))
1485 (set sym (cons (cons (read (current-buffer)) (point))
1486 (symbol-value sym)))
1487 (set sym (list (cons (read (current-buffer)) (point)))))
1488 (skip-chars-forward " "))
1490 ;; We now have all articles that can possibly be expired.
1493 (setq group (symbol-name sym)
1494 articles (sort (symbol-value sym) 'car-less-than-car)
1495 low (car (gnus-active group))
1496 info (gnus-get-info group)
1497 unreads (ignore-errors
1498 (gnus-list-of-unread-articles group))
1500 (gnus-uncompress-range
1501 (cdr (assq 'tick (gnus-info-marks info))))
1502 (gnus-uncompress-range
1504 (gnus-info-marks info)))))
1505 nov-file (gnus-agent-article-name ".overview" group)
1508 (gnus-agent-load-alist group)
1509 (gnus-message 5 "Expiring articles in %s" group)
1510 (set-buffer overview)
1512 (when (file-exists-p nov-file)
1513 (nnheader-insert-file-contents nov-file))
1514 (goto-char (point-min))
1516 (while (setq elem (pop articles))
1517 (setq article (car elem))
1518 (when (or (null low)
1520 gnus-agent-expire-all
1521 (and (not (memq article unreads))
1522 (not (memq article marked))))
1523 ;; Find and nuke the NOV line.
1524 (while (and (not (eobp))
1526 (setq art (read (current-buffer)))))
1528 (if (and (numberp art)
1530 (gnus-agent-article-name
1531 (number-to-string art) group)))
1537 ;; Remove old NOV lines that have no articles.
1538 (gnus-delete-line)))
1543 ;; Nuke the article.
1544 (when (file-exists-p
1545 (setq file (gnus-agent-article-name
1546 (number-to-string article)
1549 ;; Schedule the history line for nuking.
1550 (push (cdr elem) histories)))
1551 (gnus-make-directory (file-name-directory nov-file))
1552 (let ((coding-system-for-write
1553 gnus-agent-file-coding-system))
1554 (write-region (point-min) (point-max) nov-file nil 'silent))
1555 ;; Delete the unwanted entries in the alist.
1556 (setq gnus-agent-article-alist
1557 (sort gnus-agent-article-alist 'car-less-than-car))
1558 (let* ((alist gnus-agent-article-alist)
1559 (prev (cons nil alist))
1563 (<= (caar alist) article))
1564 (if (or (not (cdar alist))
1566 (gnus-agent-article-name
1571 (push (caar alist) expired)
1572 (setcdr prev (setq alist (cdr alist))))
1574 alist (cdr alist))))
1575 (setq gnus-agent-article-alist (cdr first))
1576 (gnus-agent-save-alist group)
1577 ;; Mark all articles up to the first article
1578 ;; in `gnus-article-alist' as read.
1579 (when (and info (caar gnus-agent-article-alist))
1580 (setcar (nthcdr 2 info)
1583 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1584 ;; Maybe everything has been expired from `gnus-article-alist'
1585 ;; and so the above marking as read could not be conducted,
1586 ;; or there are expired article within the range of the alist.
1589 (or (not (caar gnus-agent-article-alist))
1591 (caar gnus-agent-article-alist))))
1592 (setcar (nthcdr 2 info)
1595 (nreverse expired))))
1597 (concat "(gnus-group-set-info '"
1598 (gnus-prin1-to-string info)
1601 (if (gnus-gethash group orig)
1602 (setcar (gnus-gethash group orig) lowest)
1603 (gnus-sethash group (cons lowest highest) orig))))
1605 (set-buffer history)
1606 (setq histories (nreverse (sort histories '<)))
1608 (goto-char (pop histories))
1610 (gnus-agent-save-history)
1611 (gnus-agent-close-history)
1612 (gnus-write-active-file
1613 (gnus-agent-lib-file "active") orig))
1614 (gnus-message 4 "Expiry...done")))))))
1617 (defun gnus-agent-batch ()
1619 (let ((init-file-user "")
1620 (gnus-always-read-dribble-file t))
1622 (gnus-group-send-drafts)
1623 (gnus-agent-fetch-session))
1625 (provide 'gnus-agent)
1627 ;;; gnus-agent.el ends here