1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
33 (if (featurep 'xemacs)
39 (autoload 'gnus-server-update-server "gnus-srvr"))
41 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
42 "Where the Gnus agent will store its files."
46 (defcustom gnus-agent-plugged-hook nil
47 "Hook run when plugging into the network."
51 (defcustom gnus-agent-unplugged-hook nil
52 "Hook run when unplugging from the network."
56 (defcustom gnus-agent-handle-level gnus-level-subscribed
57 "Groups on levels higher than this variable will be ignored by the Agent."
61 (defcustom gnus-agent-expire-days 7
62 "Read articles older than this will be expired."
66 (defcustom gnus-agent-expire-all nil
67 "If non-nil, also expire unread, ticked and dormant articles.
68 If nil, only read articles will be expired."
72 (defcustom gnus-agent-group-mode-hook nil
73 "Hook run in Agent group minor modes."
77 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
78 (when (featurep 'xemacs)
79 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
81 (defcustom gnus-agent-summary-mode-hook nil
82 "Hook run in Agent summary minor modes."
86 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
87 (when (featurep 'xemacs)
88 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
90 (defcustom gnus-agent-server-mode-hook nil
91 "Hook run in Agent summary minor modes."
95 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
96 (when (featurep 'xemacs)
97 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
99 (defcustom gnus-agent-confirmation-function 'y-or-n-p
100 "Function to confirm when error happens."
105 (defcustom gnus-agent-synchronize-flags 'ask
106 "Indicate if flags are synchronized when you plug in.
107 If this is `ask' the hook will query the user."
109 :type '(choice (const :tag "Always" t)
110 (const :tag "Never" nil)
111 (const :tag "Ask" ask))
114 ;;; Internal variables
116 (defvar gnus-agent-history-buffers nil)
117 (defvar gnus-agent-buffer-alist nil)
118 (defvar gnus-agent-article-alist nil)
119 (defvar gnus-agent-group-alist nil)
120 (defvar gnus-agent-covered-methods nil)
121 (defvar gnus-category-alist nil)
122 (defvar gnus-agent-current-history nil)
123 (defvar gnus-agent-overview-buffer nil)
124 (defvar gnus-category-predicate-cache nil)
125 (defvar gnus-category-group-cache nil)
126 (defvar gnus-agent-spam-hashtb nil)
127 (defvar gnus-agent-file-name nil)
128 (defvar gnus-agent-send-mail-function nil)
129 (defvar gnus-agent-file-coding-system 'raw-text)
132 (defvar gnus-headers)
139 (defun gnus-open-agent ()
141 (gnus-agent-read-servers)
143 (gnus-agent-create-buffer)
144 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
145 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
146 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
148 (defun gnus-agent-create-buffer ()
149 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
151 (setq gnus-agent-overview-buffer
152 (gnus-get-buffer-create " *Gnus agent overview*"))
153 (with-current-buffer gnus-agent-overview-buffer
154 (mm-enable-multibyte))
157 (gnus-add-shutdown 'gnus-close-agent 'gnus)
159 (defun gnus-close-agent ()
160 (setq gnus-agent-covered-methods nil
161 gnus-category-predicate-cache nil
162 gnus-category-group-cache nil
163 gnus-agent-spam-hashtb nil)
164 (gnus-kill-buffer gnus-agent-overview-buffer))
167 ;;; Utility functions
170 (defun gnus-agent-read-file (file)
171 "Load FILE and do a `read' there."
174 (nnheader-insert-file-contents file)
175 (goto-char (point-min))
176 (read (current-buffer)))))
178 (defsubst gnus-agent-method ()
179 (concat (symbol-name (car gnus-command-method)) "/"
180 (if (equal (cadr gnus-command-method) "")
182 (cadr gnus-command-method))))
184 (defsubst gnus-agent-directory ()
185 "Path of the Gnus agent directory."
186 (nnheader-concat gnus-agent-directory
187 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
189 (defun gnus-agent-lib-file (file)
190 "The full path of the Gnus agent library FILE."
191 (expand-file-name file
192 (file-name-as-directory
193 (expand-file-name "agent.lib" (gnus-agent-directory)))))
195 ;;; Fetching setup functions.
197 (defun gnus-agent-start-fetch ()
198 "Initialize data structures for efficient fetching."
199 (gnus-agent-open-history)
200 (setq gnus-agent-current-history (gnus-agent-history-buffer))
201 (gnus-agent-create-buffer))
203 (defun gnus-agent-stop-fetch ()
204 "Save all data structures and clean up."
205 (gnus-agent-save-history)
206 (gnus-agent-close-history)
207 (setq gnus-agent-spam-hashtb nil)
209 (set-buffer nntp-server-buffer)
212 (defmacro gnus-agent-with-fetch (&rest forms)
215 (let ((gnus-agent-fetching t))
216 (gnus-agent-start-fetch)
218 (gnus-agent-stop-fetch)))
220 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
221 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
227 (defvar gnus-agent-mode-hook nil
228 "Hook run when installing agent mode.")
230 (defvar gnus-agent-mode nil)
231 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
233 (defun gnus-agent-mode ()
234 "Minor mode for providing a agent support in Gnus buffers."
235 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
236 (symbol-name major-mode))
237 (match-string 1 (symbol-name major-mode))))
238 (mode (intern (format "gnus-agent-%s-mode" buffer))))
239 (set (make-local-variable 'gnus-agent-mode) t)
241 (set (make-local-variable mode) t)
243 (when (gnus-visual-p 'agent-menu 'menu)
244 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
245 (unless (assq 'gnus-agent-mode minor-mode-alist)
246 (push gnus-agent-mode-status minor-mode-alist))
247 (unless (assq mode minor-mode-map-alist)
248 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
250 minor-mode-map-alist))
251 (when (eq major-mode 'gnus-group-mode)
252 (gnus-agent-toggle-plugged gnus-plugged))
253 (gnus-run-hooks 'gnus-agent-mode-hook
254 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
256 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
257 (gnus-define-keys gnus-agent-group-mode-map
258 "Ju" gnus-agent-fetch-groups
259 "Jc" gnus-enter-category-buffer
260 "Jj" gnus-agent-toggle-plugged
261 "Js" gnus-agent-fetch-session
262 "JY" gnus-agent-synchronize-flags
263 "JS" gnus-group-send-drafts
264 "Ja" gnus-agent-add-group
265 "Jr" gnus-agent-remove-group)
267 (defun gnus-agent-group-make-menu-bar ()
268 (unless (boundp 'gnus-agent-group-menu)
270 gnus-agent-group-menu gnus-agent-group-mode-map ""
272 ["Toggle plugged" gnus-agent-toggle-plugged t]
273 ["List categories" gnus-enter-category-buffer t]
274 ["Send drafts" gnus-group-send-drafts gnus-plugged]
276 ["All" gnus-agent-fetch-session gnus-plugged]
277 ["Group" gnus-agent-fetch-group gnus-plugged])))))
279 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
280 (gnus-define-keys gnus-agent-summary-mode-map
281 "Jj" gnus-agent-toggle-plugged
282 "J#" gnus-agent-mark-article
283 "J\M-#" gnus-agent-unmark-article
284 "@" gnus-agent-toggle-mark
285 "Jc" gnus-agent-catchup)
287 (defun gnus-agent-summary-make-menu-bar ()
288 (unless (boundp 'gnus-agent-summary-menu)
290 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
292 ["Toggle plugged" gnus-agent-toggle-plugged t]
293 ["Mark as downloadable" gnus-agent-mark-article t]
294 ["Unmark as downloadable" gnus-agent-unmark-article t]
295 ["Toggle mark" gnus-agent-toggle-mark t]
296 ["Catchup undownloaded" gnus-agent-catchup t]))))
298 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
299 (gnus-define-keys gnus-agent-server-mode-map
300 "Jj" gnus-agent-toggle-plugged
301 "Ja" gnus-agent-add-server
302 "Jr" gnus-agent-remove-server)
304 (defun gnus-agent-server-make-menu-bar ()
305 (unless (boundp 'gnus-agent-server-menu)
307 gnus-agent-server-menu gnus-agent-server-mode-map ""
309 ["Toggle plugged" gnus-agent-toggle-plugged t]
310 ["Add" gnus-agent-add-server t]
311 ["Remove" gnus-agent-remove-server t]))))
313 (defun gnus-agent-toggle-plugged (plugged)
314 "Toggle whether Gnus is unplugged or not."
315 (interactive (list (not gnus-plugged)))
318 (setq gnus-plugged plugged)
319 (gnus-agent-possibly-synchronize-flags)
320 (gnus-run-hooks 'gnus-agent-plugged-hook)
321 (setcar (cdr gnus-agent-mode-status) " Plugged"))
322 (gnus-agent-close-connections)
323 (setq gnus-plugged plugged)
324 (gnus-run-hooks 'gnus-agent-unplugged-hook)
325 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
326 (set-buffer-modified-p t))
328 (defun gnus-agent-close-connections ()
329 "Close all methods covered by the Gnus agent."
330 (let ((methods gnus-agent-covered-methods))
332 (gnus-close-server (pop methods)))))
335 (defun gnus-unplugged ()
336 "Start Gnus unplugged."
338 (setq gnus-plugged nil)
342 (defun gnus-plugged ()
343 "Start Gnus plugged."
345 (setq gnus-plugged t)
349 (defun gnus-agentize ()
350 "Allow Gnus to be an offline newsreader.
351 The normal usage of this command is to put the following as the
352 last form in your `.gnus.el' file:
356 This will modify the `gnus-setup-news-hook', and
357 `message-send-mail-real-function' variables, and install the Gnus agent
358 minor mode in all Gnus buffers."
361 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
362 (unless gnus-agent-send-mail-function
363 (setq gnus-agent-send-mail-function (or
364 message-send-mail-real-function
365 message-send-mail-function)
366 message-send-mail-real-function 'gnus-agent-send-mail))
367 (unless gnus-agent-covered-methods
368 (setq gnus-agent-covered-methods (list gnus-select-method))))
370 (defun gnus-agent-queue-setup ()
371 "Make sure the queue group exists."
372 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
373 (gnus-request-create-group "queue" '(nndraft ""))
374 (let ((gnus-level-default-subscribed 1))
375 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
376 (gnus-group-set-parameter
377 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
379 (defun gnus-agent-send-mail ()
381 (funcall gnus-agent-send-mail-function)
382 (goto-char (point-min))
384 (concat "^" (regexp-quote mail-header-separator) "\n"))
386 (gnus-agent-insert-meta-information 'mail)
387 (gnus-request-accept-article "nndraft:queue" nil t t)))
389 (defun gnus-agent-insert-meta-information (type &optional method)
390 "Insert meta-information into the message that says how it's to be posted.
391 TYPE can be either `mail' or `news'. If the latter, then METHOD can
394 (message-remove-header gnus-agent-meta-information-header)
395 (goto-char (point-min))
396 (insert gnus-agent-meta-information-header ": "
397 (symbol-name type) " " (format "%S" method)
400 (while (search-backward "\n" nil t)
401 (replace-match "\\n" t t))))
403 (defun gnus-agent-restore-gcc ()
404 "Restore GCC field from saved header."
406 (goto-char (point-min))
407 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
408 (replace-match "Gcc:" 'fixedcase))))
410 (defun gnus-agent-any-covered-gcc ()
412 (message-narrow-to-headers)
413 (let* ((gcc (mail-fetch-field "gcc" nil t))
415 (mapcar 'gnus-inews-group-method
416 (message-unquote-tokens
417 (message-tokenize-header
420 (while (and (not covered) methods)
422 (member (car methods) gnus-agent-covered-methods)
423 methods (cdr methods)))
426 (defun gnus-agent-possibly-save-gcc ()
427 "Save GCC if Gnus is unplugged."
428 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
430 (goto-char (point-min))
431 (let ((case-fold-search t))
432 (while (re-search-forward "^gcc:" nil t)
433 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
435 (defun gnus-agent-possibly-do-gcc ()
436 "Do GCC if Gnus is plugged."
437 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
438 (gnus-inews-do-gcc)))
441 ;;; Group mode commands
444 (defun gnus-agent-fetch-groups (n)
445 "Put all new articles in the current groups into the Agent."
448 (error "Groups can't be fetched when Gnus is unplugged"))
449 (gnus-group-iterate n 'gnus-agent-fetch-group))
451 (defun gnus-agent-fetch-group (group)
452 "Put all new articles in GROUP into the Agent."
453 (interactive (list (gnus-group-group-name)))
455 (error "Groups can't be fetched when Gnus is unplugged"))
457 (error "No group on the current line"))
458 (let ((gnus-command-method (gnus-find-method-for-group group)))
459 (gnus-agent-with-fetch
460 (gnus-agent-fetch-group-1 group gnus-command-method)
461 (gnus-message 5 "Fetching %s...done" group))))
463 (defun gnus-agent-add-group (category arg)
464 "Add the current group to an agent category."
470 (mapcar (lambda (cat) (list (symbol-name (car cat))))
474 (let ((cat (assq category gnus-category-alist))
476 (gnus-group-iterate arg
478 (when (cadddr (setq c (gnus-group-category group)))
479 (setf (cadddr c) (delete group (cadddr c))))
480 (push group groups)))
481 (setf (cadddr cat) (nconc (cadddr cat) groups))
482 (gnus-category-write)))
484 (defun gnus-agent-remove-group (arg)
485 "Remove the current group from its agent category, if any."
488 (gnus-group-iterate arg
490 (when (cadddr (setq c (gnus-group-category group)))
491 (setf (cadddr c) (delete group (cadddr c))))))
492 (gnus-category-write)))
494 (defun gnus-agent-synchronize-flags ()
495 "Synchronize unplugged flags with servers."
498 (dolist (gnus-command-method gnus-agent-covered-methods)
499 (when (file-exists-p (gnus-agent-lib-file "flags"))
500 (gnus-agent-synchronize-flags-server gnus-command-method)))))
502 (defun gnus-agent-possibly-synchronize-flags ()
503 "Synchronize flags according to `gnus-agent-synchronize-flags'."
506 (dolist (gnus-command-method gnus-agent-covered-methods)
507 (when (file-exists-p (gnus-agent-lib-file "flags"))
508 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
510 (defun gnus-agent-synchronize-flags-server (method)
511 "Synchronize flags set when unplugged for server."
512 (let ((gnus-command-method method))
513 (when (file-exists-p (gnus-agent-lib-file "flags"))
514 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
516 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
517 (if (null (gnus-check-server gnus-command-method))
518 (message "Couldn't open server %s" (nth 1 gnus-command-method))
520 (if (null (eval (read (current-buffer))))
521 (progn (forward-line)
523 (write-file (gnus-agent-lib-file "flags"))
524 (error "Couldn't set flags from file %s"
525 (gnus-agent-lib-file "flags"))))
526 (delete-file (gnus-agent-lib-file "flags")))
529 (defun gnus-agent-possibly-synchronize-flags-server (method)
530 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
531 (when (or (and gnus-agent-synchronize-flags
532 (not (eq gnus-agent-synchronize-flags 'ask)))
533 (and (eq gnus-agent-synchronize-flags 'ask)
534 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
536 (gnus-agent-synchronize-flags-server method)))
539 ;;; Server mode commands
542 (defun gnus-agent-add-server (server)
543 "Enroll SERVER in the agent program."
544 (interactive (list (gnus-server-server-name)))
546 (error "No server on the current line"))
547 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
548 (when (member method gnus-agent-covered-methods)
549 (error "Server already in the agent program"))
550 (push method gnus-agent-covered-methods)
551 (gnus-server-update-server server)
552 (gnus-agent-write-servers)
553 (message "Entered %s into the Agent" server)))
555 (defun gnus-agent-remove-server (server)
556 "Remove SERVER from the agent program."
557 (interactive (list (gnus-server-server-name)))
559 (error "No server on the current line"))
560 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
561 (unless (member method gnus-agent-covered-methods)
562 (error "Server not in the agent program"))
563 (setq gnus-agent-covered-methods
564 (delete method gnus-agent-covered-methods))
565 (gnus-server-update-server server)
566 (gnus-agent-write-servers)
567 (message "Removed %s from the agent" server)))
569 (defun gnus-agent-read-servers ()
570 "Read the alist of covered servers."
571 (setq gnus-agent-covered-methods
572 (gnus-agent-read-file
573 (nnheader-concat gnus-agent-directory "lib/servers"))))
575 (defun gnus-agent-write-servers ()
576 "Write the alist of covered servers."
577 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
578 (let ((coding-system-for-write nnheader-file-coding-system)
579 (file-name-coding-system nnmail-pathname-coding-system))
580 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
581 (prin1 gnus-agent-covered-methods (current-buffer)))))
587 (defun gnus-agent-mark-article (n &optional unmark)
588 "Mark the next N articles as downloadable.
589 If N is negative, mark backward instead. If UNMARK is non-nil, remove
590 the mark instead. The difference between N and the actual number of
591 articles marked is returned."
593 (let ((backward (< n 0))
598 (gnus-summary-set-agent-mark
599 (gnus-summary-article-number) unmark)
600 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
603 (gnus-message 7 "No more articles"))
604 (gnus-summary-recenter)
605 (gnus-summary-position-point)
608 (defun gnus-agent-unmark-article (n)
609 "Remove the downloadable mark from the next N articles.
610 If N is negative, unmark backward instead. The difference between N and
611 the actual number of articles unmarked is returned."
613 (gnus-agent-mark-article n t))
615 (defun gnus-agent-toggle-mark (n)
616 "Toggle the downloadable mark from the next N articles.
617 If N is negative, toggle backward instead. The difference between N and
618 the actual number of articles toggled is returned."
620 (gnus-agent-mark-article n 'toggle))
622 (defun gnus-summary-set-agent-mark (article &optional unmark)
623 "Mark ARTICLE as downloadable."
624 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
625 (memq article gnus-newsgroup-downloadable)
629 (setq gnus-newsgroup-downloadable
630 (delq article gnus-newsgroup-downloadable))
631 (push article gnus-newsgroup-undownloaded))
632 (setq gnus-newsgroup-undownloaded
633 (delq article gnus-newsgroup-undownloaded))
634 (push article gnus-newsgroup-downloadable))
635 (gnus-summary-update-mark
636 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
639 (defun gnus-agent-get-undownloaded-list ()
640 "Mark all unfetched articles as read."
641 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
642 (when (and (not gnus-plugged)
643 (gnus-agent-method-p gnus-command-method))
644 (gnus-agent-load-alist gnus-newsgroup-name)
645 ;; First mark all undownloaded articles as undownloaded.
646 (dolist (article (mapcar (lambda (header) (mail-header-number header))
647 gnus-newsgroup-headers))
648 (unless (or (cdr (assq article gnus-agent-article-alist))
649 (memq article gnus-newsgroup-downloadable)
650 (memq article gnus-newsgroup-cached))
651 (push article gnus-newsgroup-undownloaded)))
652 ;; Then mark downloaded downloadable as not-downloadable,
653 ;; if you get my drift.
654 (dolist (article gnus-newsgroup-downloadable)
655 (when (cdr (assq article gnus-agent-article-alist))
656 (setq gnus-newsgroup-downloadable
657 (delq article gnus-newsgroup-downloadable)))))))
659 (defun gnus-agent-catchup ()
660 "Mark all undownloaded articles as read."
663 (while gnus-newsgroup-undownloaded
664 (gnus-summary-mark-article
665 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
666 (gnus-summary-position-point))
669 ;;; Internal functions
672 (defun gnus-agent-save-active (method)
673 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
675 (defun gnus-agent-save-active-1 (method function)
676 (when (gnus-agent-method-p method)
677 (let* ((gnus-command-method method)
678 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
679 (file (gnus-agent-lib-file "active")))
680 (funcall function nil new)
681 (gnus-agent-write-active file new)
683 (nnheader-insert-file-contents file))))
685 (defun gnus-agent-write-active (file new)
686 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
687 (file (gnus-agent-lib-file "active"))
689 (when (file-exists-p file)
691 (nnheader-insert-file-contents file)
692 (gnus-active-to-gnus-format nil orig))
695 (when (and sym (boundp sym))
696 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
697 (setq elem (symbol-value osym)))
698 (setcdr elem (cdr (symbol-value sym)))
699 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
701 (gnus-make-directory (file-name-directory file))
702 (let ((coding-system-for-write gnus-agent-file-coding-system))
703 ;; The hashtable contains real names of groups, no more prefix
704 ;; removing, so set `full' to `t'.
705 (gnus-write-active-file file orig t))))
707 (defun gnus-agent-save-groups (method)
708 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
710 (defun gnus-agent-save-group-info (method group active)
711 (when (gnus-agent-method-p method)
712 (let* ((gnus-command-method method)
713 (coding-system-for-write nnheader-file-coding-system)
714 (file-name-coding-system nnmail-pathname-coding-system)
715 (file (gnus-agent-lib-file "active"))
717 (gnus-make-directory (file-name-directory file))
719 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
720 (mm-disable-multibyte)
721 (when (file-exists-p file)
722 (nnheader-insert-file-contents file))
723 (goto-char (point-min))
724 (when (re-search-forward
725 (concat "^" (regexp-quote group) " ") nil t)
727 (read (current-buffer)) ;; max
728 (setq oactive-min (read (current-buffer)))) ;; min
730 (insert (format "%S %d %d y\n" (intern group)
732 (or oactive-min (car active))))
733 (goto-char (point-max))
734 (while (search-backward "\\." nil t)
737 (defun gnus-agent-group-path (group)
738 "Translate GROUP into a path."
739 (if nnmail-use-long-file-names
740 (gnus-group-real-name group)
741 (nnheader-translate-file-chars
742 (nnheader-replace-chars-in-string
743 (nnheader-replace-duplicate-chars-in-string
744 (nnheader-replace-chars-in-string
745 (gnus-group-real-name group)
752 (defun gnus-agent-method-p (method)
753 "Say whether METHOD is covered by the agent."
754 (member method gnus-agent-covered-methods))
756 (defun gnus-agent-get-function (method)
757 (if (and (not gnus-plugged)
758 (gnus-agent-method-p method))
764 ;;; History functions
766 (defun gnus-agent-history-buffer ()
767 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
769 (defun gnus-agent-open-history ()
771 (push (cons (gnus-agent-method)
772 (set-buffer (gnus-get-buffer-create
773 (format " *Gnus agent %s history*"
774 (gnus-agent-method)))))
775 gnus-agent-history-buffers)
776 (mm-disable-multibyte) ;; everything is binary
779 (let ((file (gnus-agent-lib-file "history")))
780 (when (file-exists-p file)
781 (nnheader-insert-file-contents file))
782 (set (make-local-variable 'gnus-agent-file-name) file))))
784 (defun gnus-agent-save-history ()
786 (set-buffer gnus-agent-current-history)
787 (gnus-make-directory (file-name-directory gnus-agent-file-name))
788 (let ((coding-system-for-write gnus-agent-file-coding-system))
789 (write-region (1+ (point-min)) (point-max)
790 gnus-agent-file-name nil 'silent))))
792 (defun gnus-agent-close-history ()
793 (when (gnus-buffer-live-p gnus-agent-current-history)
794 (kill-buffer gnus-agent-current-history)
795 (setq gnus-agent-history-buffers
796 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
797 gnus-agent-history-buffers))))
799 (defun gnus-agent-enter-history (id group-arts date)
801 (set-buffer gnus-agent-current-history)
802 (goto-char (point-max))
804 (insert id "\t" (number-to-string date) "\t")
806 (insert (format "%S" (intern (caar group-arts)))
807 " " (number-to-string (cdr (pop group-arts)))
810 (while (search-backward "\\." p t)
813 (defun gnus-agent-article-in-history-p (id)
815 (set-buffer (gnus-agent-history-buffer))
816 (goto-char (point-min))
817 (search-forward (concat "\n" id "\t") nil t)))
819 (defun gnus-agent-history-path (id)
821 (set-buffer (gnus-agent-history-buffer))
822 (goto-char (point-min))
823 (when (search-forward (concat "\n" id "\t") nil t)
824 (let ((method (gnus-agent-method)))
826 (while (not (numberp (setq group (read (current-buffer)))))
827 (push (concat method "/" group) paths))
828 (nreverse paths))))))
834 (defun gnus-agent-fetch-articles (group articles)
835 "Fetch ARTICLES from GROUP and put them into the Agent."
837 ;; Prune off articles that we have already fetched.
839 (cdr (assq (car articles) gnus-agent-article-alist)))
841 (let ((arts articles))
843 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
844 (setcdr arts (cddr arts))
845 (setq arts (cdr arts)))))
848 (gnus-agent-directory)
849 (gnus-agent-group-path group) "/"))
850 (date (time-to-days (current-time)))
853 (gnus-make-directory dir)
854 (gnus-message 7 "Fetching articles for %s..." group)
855 ;; Fetch the articles from the backend.
856 (if (gnus-check-backend-function 'retrieve-articles group)
857 (setq pos (gnus-retrieve-articles articles group))
860 (while (setq article (pop articles))
862 (gnus-backlog-request-article group article
864 (gnus-request-article article group))
865 (goto-char (point-max))
866 (push (cons article (point)) pos)
867 (insert-buffer-substring nntp-server-buffer)))
868 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
869 (setq pos (nreverse pos)))))
870 ;; Then save these articles into the Agent.
872 (set-buffer nntp-server-buffer)
874 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
875 (goto-char (point-min))
876 (when (search-forward "\n\n" nil t)
877 (when (search-backward "\nXrefs: " nil t)
878 ;; Handle crossposting.
879 (skip-chars-forward "^ ")
880 (skip-chars-forward " ")
882 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
883 (push (cons (buffer-substring (match-beginning 1)
885 (buffer-substring (match-beginning 2)
888 (goto-char (match-end 0)))
889 (gnus-agent-crosspost crosses (caar pos))))
890 (goto-char (point-min))
891 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
892 (setq id "No-Message-ID-in-article")
893 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
894 (let ((coding-system-for-write
895 gnus-agent-file-coding-system))
896 (write-region (point-min) (point-max)
897 (concat dir (number-to-string (caar pos)))
899 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
901 (gnus-agent-enter-history
902 id (or crosses (list (cons group (caar pos)))) date)
905 (gnus-agent-save-alist group)))))
907 (defun gnus-agent-crosspost (crosses article)
908 (let (gnus-agent-article-alist group alist beg end)
910 (set-buffer gnus-agent-overview-buffer)
911 (when (nnheader-find-nov-line article)
914 (setq end (progn (forward-line 1) (point)))))
916 (setq group (caar crosses))
917 (unless (setq alist (assoc group gnus-agent-group-alist))
918 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
919 gnus-agent-group-alist))
920 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
922 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
924 (when (= (point-max) (point-min))
925 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
927 (nnheader-insert-file-contents
928 (gnus-agent-article-name ".overview" group))))
929 (nnheader-find-nov-line (string-to-number (cdar crosses)))
930 (insert (string-to-number (cdar crosses)))
931 (insert-buffer-substring gnus-agent-overview-buffer beg end))
934 (defun gnus-agent-flush-cache ()
936 (while gnus-agent-buffer-alist
937 (set-buffer (cdar gnus-agent-buffer-alist))
938 (let ((coding-system-for-write
939 gnus-agent-file-coding-system))
940 (write-region (point-min) (point-max)
941 (gnus-agent-article-name ".overview"
942 (caar gnus-agent-buffer-alist))
944 (pop gnus-agent-buffer-alist))
945 (while gnus-agent-group-alist
946 (with-temp-file (caar gnus-agent-group-alist)
947 (princ (cdar gnus-agent-group-alist))
949 (pop gnus-agent-group-alist))))
951 (defun gnus-agent-fetch-headers (group &optional force)
952 (let ((articles (gnus-list-of-unread-articles group))
953 (gnus-decode-encoded-word-function 'identity)
954 (file (gnus-agent-article-name ".overview" group)))
955 ;; Add article with marks to list of article headers we want to fetch.
956 (dolist (arts (gnus-info-marks (gnus-get-info group)))
957 (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
959 (setq articles (sort articles '<))
960 ;; Remove known articles.
961 (when (gnus-agent-load-alist group)
962 (setq articles (gnus-sorted-intersection
964 (gnus-uncompress-range
965 (cons (1+ (caar (last gnus-agent-article-alist)))
966 (cdr (gnus-active group)))))))
968 (gnus-make-directory (nnheader-translate-file-chars
969 (file-name-directory file) t))
971 (gnus-message 7 "Fetching headers for %s..." group)
973 (set-buffer nntp-server-buffer)
974 (unless (eq 'nov (gnus-retrieve-headers articles group))
975 (nnvirtual-convert-headers))
976 ;; Save these headers for later processing.
977 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
978 (when (file-exists-p file)
979 (gnus-agent-braid-nov group articles file))
980 (let ((coding-system-for-write
981 gnus-agent-file-coding-system))
982 (write-region (point-min) (point-max) file nil 'silent))
983 (gnus-agent-save-alist group articles nil)
984 (gnus-agent-enter-history
985 "last-header-fetched-for-session"
986 (list (cons group (nth (- (length articles) 1) articles)))
987 (time-to-days (current-time)))
990 (defsubst gnus-agent-copy-nov-line (article)
992 (set-buffer gnus-agent-overview-buffer)
994 (if (eq article (read (current-buffer)))
995 (setq e (progn (forward-line 1) (point)))
999 (set-buffer nntp-server-buffer)
1000 (insert-buffer-substring gnus-agent-overview-buffer b e)))
1002 (defun gnus-agent-braid-nov (group articles file)
1003 (set-buffer gnus-agent-overview-buffer)
1004 (goto-char (point-min))
1005 (set-buffer nntp-server-buffer)
1007 (nnheader-insert-file-contents file)
1008 (goto-char (point-max))
1009 (if (or (= (point-min) (point-max))
1012 (< (read (current-buffer)) (car articles))))
1013 ;; We have only headers that are after the older headers,
1014 ;; so we just append them.
1016 (goto-char (point-max))
1017 (insert-buffer-substring gnus-agent-overview-buffer))
1018 ;; We do it the hard way.
1019 (nnheader-find-nov-line (car articles))
1020 (gnus-agent-copy-nov-line (car articles))
1022 (while (and articles
1024 (while (and (not (eobp))
1025 (< (read (current-buffer)) (car articles)))
1029 (gnus-agent-copy-nov-line (car articles))
1030 (setq articles (cdr articles))))
1033 (set-buffer gnus-agent-overview-buffer)
1036 (set-buffer nntp-server-buffer)
1037 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1039 (defun gnus-agent-load-alist (group &optional dir)
1040 "Load the article-state alist for GROUP."
1041 (setq gnus-agent-article-alist
1042 (gnus-agent-read-file
1044 (expand-file-name ".agentview" dir)
1045 (gnus-agent-article-name ".agentview" group)))))
1047 (defun gnus-agent-save-alist (group &optional articles state dir)
1048 "Save the article-state alist for GROUP."
1049 (let ((file-name-coding-system nnmail-pathname-coding-system)
1050 print-level print-length)
1051 (with-temp-file (if dir
1052 (expand-file-name ".agentview" dir)
1053 (gnus-agent-article-name ".agentview" group))
1054 (princ (setq gnus-agent-article-alist
1055 (nconc gnus-agent-article-alist
1056 (mapcar (lambda (article) (cons article state))
1061 (defun gnus-agent-article-name (article group)
1062 (expand-file-name (if (stringp article) article (string-to-number article))
1063 (file-name-as-directory
1064 (expand-file-name (gnus-agent-group-path group)
1065 (gnus-agent-directory)))))
1067 (defun gnus-agent-batch-confirmation (msg)
1068 "Show error message and return t."
1069 (gnus-message 1 msg)
1073 (defun gnus-agent-batch-fetch ()
1074 "Start Gnus and fetch session."
1077 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1078 (gnus-agent-fetch-session))
1081 (defun gnus-agent-fetch-session ()
1082 "Fetch all articles and headers that are eligible for fetching."
1084 (unless gnus-agent-covered-methods
1085 (error "No servers are covered by the Gnus agent"))
1086 (unless gnus-plugged
1087 (error "Can't fetch articles while Gnus is unplugged"))
1088 (let ((methods gnus-agent-covered-methods)
1089 groups group gnus-command-method)
1094 (setq gnus-command-method (car methods))
1095 (when (or (gnus-server-opened gnus-command-method)
1096 (gnus-open-server gnus-command-method))
1097 (setq groups (gnus-groups-from-server (car methods)))
1098 (gnus-agent-with-fetch
1099 (while (setq group (pop groups))
1100 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1101 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1103 (unless (funcall gnus-agent-confirmation-function
1104 (format "Error (%s). Continue? " err))
1105 (error "Cannot fetch articles into the Gnus agent")))
1107 (unless (funcall gnus-agent-confirmation-function
1108 (format "Quit fetching session (%s). Continue? "
1110 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1112 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1114 (defun gnus-agent-fetch-group-1 (group method)
1116 (let ((gnus-command-method method)
1117 (gnus-newsgroup-name group)
1118 gnus-newsgroup-dependencies gnus-newsgroup-headers
1119 gnus-newsgroup-scored gnus-headers gnus-score
1120 gnus-use-cache articles arts
1121 category predicate info marks score-param
1122 (gnus-summary-expunge-below gnus-summary-expunge-below)
1123 (gnus-summary-mark-below gnus-summary-mark-below)
1124 (gnus-orphan-score gnus-orphan-score)
1125 ;; Maybe some other gnus-summary local variables should also
1128 (unless (gnus-check-group group)
1129 (error "Can't open server for %s" group))
1131 (when (and (or (gnus-active group) (gnus-activate-group group))
1132 (setq articles (gnus-agent-fetch-headers group))
1133 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1134 ;; Parse them and see which articles we want to fetch.
1135 (setq gnus-newsgroup-dependencies
1136 (make-vector (length articles) 0))
1137 (setq gnus-newsgroup-headers
1138 (gnus-get-newsgroup-headers-xover articles nil nil
1140 ;; `gnus-agent-overview-buffer' may be killed for
1141 ;; timeout reason. If so, recreate it.
1142 (gnus-agent-create-buffer)))
1143 (setq category (gnus-group-category group))
1146 (or (gnus-group-find-parameter group 'agent-predicate t)
1148 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1149 ;; Simple implementation
1151 (and (eq predicate 'gnus-agent-true) articles))
1154 (or (gnus-group-get-parameter group 'agent-score t)
1156 ;; Translate score-param into real one
1159 ((eq score-param 'file)
1160 (setq score-param (gnus-all-score-files group)))
1161 ((stringp (car score-param)))
1163 (setq score-param (list (list score-param)))))
1165 (gnus-score-headers score-param))
1166 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1168 (or (cdr (assq (mail-header-number gnus-headers)
1169 gnus-newsgroup-scored))
1170 gnus-summary-default-score))
1171 (when (funcall predicate)
1172 (push (mail-header-number gnus-headers)
1174 ;; Fetch the articles.
1176 (gnus-agent-fetch-articles group arts)))
1177 ;; Perhaps we have some additional articles to fetch.
1178 (setq arts (assq 'download (gnus-info-marks
1179 (setq info (gnus-get-info group)))))
1181 (gnus-agent-fetch-articles
1182 group (gnus-uncompress-range (cdr arts)))
1183 (setq marks (delq arts (gnus-info-marks info)))
1184 (gnus-info-set-marks info marks)
1186 (concat "(gnus-group-set-info '"
1187 (gnus-prin1-to-string info)
1191 ;;; Agent Category Mode
1194 (defvar gnus-category-mode-hook nil
1195 "Hook run in `gnus-category-mode' buffers.")
1197 (defvar gnus-category-line-format " %(%20c%): %g\n"
1198 "Format of category lines.")
1200 (defvar gnus-category-mode-line-format "Gnus: %%b"
1201 "The format specification for the category mode line.")
1203 (defvar gnus-agent-short-article 100
1204 "Articles that have fewer lines than this are short.")
1206 (defvar gnus-agent-long-article 200
1207 "Articles that have more lines than this are long.")
1209 (defvar gnus-agent-low-score 0
1210 "Articles that have a score lower than this have a low score.")
1212 (defvar gnus-agent-high-score 0
1213 "Articles that have a score higher than this have a high score.")
1216 ;;; Internal variables.
1218 (defvar gnus-category-buffer "*Agent Category*")
1220 (defvar gnus-category-line-format-alist
1221 `((?c gnus-tmp-name ?s)
1222 (?g gnus-tmp-groups ?d)))
1224 (defvar gnus-category-mode-line-format-alist
1225 `((?u user-defined ?s)))
1227 (defvar gnus-category-line-format-spec nil)
1228 (defvar gnus-category-mode-line-format-spec nil)
1230 (defvar gnus-category-mode-map nil)
1231 (put 'gnus-category-mode 'mode-class 'special)
1233 (unless gnus-category-mode-map
1234 (setq gnus-category-mode-map (make-sparse-keymap))
1235 (suppress-keymap gnus-category-mode-map)
1237 (gnus-define-keys gnus-category-mode-map
1238 "q" gnus-category-exit
1239 "k" gnus-category-kill
1240 "c" gnus-category-copy
1241 "a" gnus-category-add
1242 "p" gnus-category-edit-predicate
1243 "g" gnus-category-edit-groups
1244 "s" gnus-category-edit-score
1245 "l" gnus-category-list
1247 "\C-c\C-i" gnus-info-find-node
1248 "\C-c\C-b" gnus-bug))
1250 (defvar gnus-category-menu-hook nil
1251 "*Hook run after the creation of the menu.")
1253 (defun gnus-category-make-menu-bar ()
1254 (gnus-turn-off-edit-menu 'category)
1255 (unless (boundp 'gnus-category-menu)
1257 gnus-category-menu gnus-category-mode-map ""
1259 ["Add" gnus-category-add t]
1260 ["Kill" gnus-category-kill t]
1261 ["Copy" gnus-category-copy t]
1262 ["Edit predicate" gnus-category-edit-predicate t]
1263 ["Edit score" gnus-category-edit-score t]
1264 ["Edit groups" gnus-category-edit-groups t]
1265 ["Exit" gnus-category-exit t]))
1267 (gnus-run-hooks 'gnus-category-menu-hook)))
1269 (defun gnus-category-mode ()
1270 "Major mode for listing and editing agent categories.
1272 All normal editing commands are switched off.
1273 \\<gnus-category-mode-map>
1274 For more in-depth information on this mode, read the manual
1275 (`\\[gnus-info-find-node]').
1277 The following commands are available:
1279 \\{gnus-category-mode-map}"
1281 (when (gnus-visual-p 'category-menu 'menu)
1282 (gnus-category-make-menu-bar))
1283 (kill-all-local-variables)
1284 (gnus-simplify-mode-line)
1285 (setq major-mode 'gnus-category-mode)
1286 (setq mode-name "Category")
1287 (gnus-set-default-directory)
1288 (setq mode-line-process nil)
1289 (use-local-map gnus-category-mode-map)
1290 (buffer-disable-undo)
1291 (setq truncate-lines t)
1292 (setq buffer-read-only t)
1293 (gnus-run-hooks 'gnus-category-mode-hook))
1295 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1297 (defun gnus-category-insert-line (category)
1298 (let* ((gnus-tmp-name (car category))
1299 (gnus-tmp-groups (length (cadddr category))))
1301 (gnus-add-text-properties
1305 (eval gnus-category-line-format-spec))
1306 (list 'gnus-category gnus-tmp-name))))
1308 (defun gnus-enter-category-buffer ()
1309 "Go to the Category buffer."
1311 (gnus-category-setup-buffer)
1312 (gnus-configure-windows 'category)
1313 (gnus-category-prepare))
1315 (defun gnus-category-setup-buffer ()
1316 (unless (get-buffer gnus-category-buffer)
1318 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1319 (gnus-category-mode))))
1321 (defun gnus-category-prepare ()
1322 (gnus-set-format 'category-mode)
1323 (gnus-set-format 'category t)
1324 (let ((alist gnus-category-alist)
1325 (buffer-read-only nil))
1328 (gnus-category-insert-line (pop alist)))
1329 (goto-char (point-min))
1330 (gnus-category-position-point)))
1332 (defun gnus-category-name ()
1333 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1334 (error "No category on the current line")))
1336 (defun gnus-category-read ()
1337 "Read the category alist."
1338 (setq gnus-category-alist
1339 (or (gnus-agent-read-file
1340 (nnheader-concat gnus-agent-directory "lib/categories"))
1341 (list (list 'default 'short nil nil)))))
1343 (defun gnus-category-write ()
1344 "Write the category alist."
1345 (setq gnus-category-predicate-cache nil
1346 gnus-category-group-cache nil)
1347 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1348 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1349 (prin1 gnus-category-alist (current-buffer))))
1351 (defun gnus-category-edit-predicate (category)
1352 "Edit the predicate for CATEGORY."
1353 (interactive (list (gnus-category-name)))
1354 (let ((info (assq category gnus-category-alist)))
1356 (cadr info) (format "Editing the predicate for category %s" category)
1357 `(lambda (predicate)
1358 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1359 (gnus-category-write)
1360 (gnus-category-list)))))
1362 (defun gnus-category-edit-score (category)
1363 "Edit the score expression for CATEGORY."
1364 (interactive (list (gnus-category-name)))
1365 (let ((info (assq category gnus-category-alist)))
1368 (format "Editing the score expression for category %s" category)
1370 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1371 (gnus-category-write)
1372 (gnus-category-list)))))
1374 (defun gnus-category-edit-groups (category)
1375 "Edit the group list for CATEGORY."
1376 (interactive (list (gnus-category-name)))
1377 (let ((info (assq category gnus-category-alist)))
1379 (cadddr info) (format "Editing the group list for category %s" category)
1381 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1382 (gnus-category-write)
1383 (gnus-category-list)))))
1385 (defun gnus-category-kill (category)
1386 "Kill the current category."
1387 (interactive (list (gnus-category-name)))
1388 (let ((info (assq category gnus-category-alist))
1389 (buffer-read-only nil))
1391 (setq gnus-category-alist (delq info gnus-category-alist))
1392 (gnus-category-write)))
1394 (defun gnus-category-copy (category to)
1395 "Copy the current category."
1396 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1397 (let ((info (assq category gnus-category-alist)))
1398 (push (list to (gnus-copy-sequence (cadr info))
1399 (gnus-copy-sequence (caddr info)) nil)
1400 gnus-category-alist)
1401 (gnus-category-write)
1402 (gnus-category-list)))
1404 (defun gnus-category-add (category)
1405 "Create a new category."
1406 (interactive "SCategory name: ")
1407 (when (assq category gnus-category-alist)
1408 (error "Category %s already exists" category))
1409 (push (list category 'false nil nil)
1410 gnus-category-alist)
1411 (gnus-category-write)
1412 (gnus-category-list))
1414 (defun gnus-category-list ()
1415 "List all categories."
1417 (gnus-category-prepare))
1419 (defun gnus-category-exit ()
1420 "Return to the group buffer."
1422 (kill-buffer (current-buffer))
1423 (gnus-configure-windows 'group t))
1425 ;; To avoid having 8-bit characters in the source file.
1426 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1428 (defvar gnus-category-predicate-alist
1429 '((spam . gnus-agent-spam-p)
1430 (short . gnus-agent-short-p)
1431 (long . gnus-agent-long-p)
1432 (low . gnus-agent-low-scored-p)
1433 (high . gnus-agent-high-scored-p)
1434 (true . gnus-agent-true)
1435 (false . gnus-agent-false))
1436 "Mapping from short score predicate symbols to predicate functions.")
1438 (defun gnus-agent-spam-p ()
1439 "Say whether an article is spam or not."
1440 (unless gnus-agent-spam-hashtb
1441 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1442 (if (not (equal (mail-header-references gnus-headers) ""))
1444 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1446 (gnus-gethash string gnus-agent-spam-hashtb)
1447 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1449 (defun gnus-agent-short-p ()
1450 "Say whether an article is short or not."
1451 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1453 (defun gnus-agent-long-p ()
1454 "Say whether an article is long or not."
1455 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1457 (defun gnus-agent-low-scored-p ()
1458 "Say whether an article has a low score or not."
1459 (< gnus-score gnus-agent-low-score))
1461 (defun gnus-agent-high-scored-p ()
1462 "Say whether an article has a high score or not."
1463 (> gnus-score gnus-agent-high-score))
1465 (defun gnus-category-make-function (cat)
1466 "Make a function from category CAT."
1467 (let ((func (gnus-category-make-function-1 cat)))
1468 (if (and (= (length func) 1)
1469 (symbolp (car func)))
1471 (gnus-byte-compile `(lambda () ,func)))))
1473 (defun gnus-agent-true ()
1477 (defun gnus-agent-false ()
1481 (defun gnus-category-make-function-1 (cat)
1482 "Make a function from category CAT."
1484 ;; Functions are just returned as is.
1486 (gnus-functionp cat))
1487 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1489 ;; More complex category.
1492 ((memq (car cat) '(& and))
1494 ((memq (car cat) '(| or))
1496 ((memq (car cat) gnus-category-not)
1498 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1500 (error "Unknown category type: %s" cat))))
1502 (defun gnus-get-predicate (predicate)
1503 "Return the predicate for CATEGORY."
1504 (or (cdr (assoc predicate gnus-category-predicate-cache))
1505 (cdar (push (cons predicate
1506 (gnus-category-make-function predicate))
1507 gnus-category-predicate-cache))))
1509 (defun gnus-group-category (group)
1510 "Return the category GROUP belongs to."
1511 (unless gnus-category-group-cache
1512 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1513 (let ((cs gnus-category-alist)
1515 (while (setq cat (pop cs))
1516 (setq groups (cadddr cat))
1518 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1519 (or (gnus-gethash group gnus-category-group-cache)
1520 (assq 'default gnus-category-alist)))
1522 (defun gnus-agent-expire ()
1523 "Expire all old articles."
1525 (let ((methods gnus-agent-covered-methods)
1526 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1527 gnus-command-method sym group articles
1528 history overview file histories elem art nov-file low info
1529 unreads marked article orig lowest highest)
1531 (setq overview (gnus-get-buffer-create " *expire overview*"))
1532 (while (setq gnus-command-method (pop methods))
1533 (when (file-exists-p (gnus-agent-lib-file "active"))
1535 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1536 (gnus-active-to-gnus-format
1538 (setq orig (gnus-make-hashtable
1539 (count-lines (point-min) (point-max))))))
1540 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1541 (gnus-agent-open-history)
1543 (setq gnus-agent-current-history
1544 (setq history (gnus-agent-history-buffer))))
1545 (goto-char (point-min))
1546 (when (> (buffer-size) 1)
1547 (goto-char (point-min))
1549 (skip-chars-forward "^\t")
1550 (if (let ((fetch-date (read (current-buffer))))
1551 (if (numberp fetch-date)
1553 ;; History file is corrupted.
1556 (format "File %s is corrupted!"
1557 (gnus-agent-lib-file "history")))
1561 ;; New article; we don't expire it.
1563 ;; Old article. Schedule it for possible nuking.
1565 (setq sym (let ((obarray expiry-hashtb) s)
1566 (setq s (read (current-buffer)))
1567 (if (stringp s) (intern s) s)))
1569 (set sym (cons (cons (read (current-buffer)) (point))
1570 (symbol-value sym)))
1571 (set sym (list (cons (read (current-buffer)) (point)))))
1572 (skip-chars-forward " "))
1574 ;; We now have all articles that can possibly be expired.
1577 (setq group (symbol-name sym)
1578 articles (sort (symbol-value sym) 'car-less-than-car)
1579 low (car (gnus-active group))
1580 info (gnus-get-info group)
1581 unreads (ignore-errors
1582 (gnus-list-of-unread-articles group))
1584 (gnus-uncompress-range
1585 (cdr (assq 'tick (gnus-info-marks info))))
1586 (gnus-uncompress-range
1588 (gnus-info-marks info)))))
1589 nov-file (gnus-agent-article-name ".overview" group)
1592 (gnus-agent-load-alist group)
1593 (gnus-message 5 "Expiring articles in %s" group)
1594 (set-buffer overview)
1596 (when (file-exists-p nov-file)
1597 (nnheader-insert-file-contents nov-file))
1598 (goto-char (point-min))
1600 (while (setq elem (pop articles))
1601 (setq article (car elem))
1602 (when (or (null low)
1604 gnus-agent-expire-all
1605 (and (not (memq article unreads))
1606 (not (memq article marked))))
1607 ;; Find and nuke the NOV line.
1608 (while (and (not (eobp))
1610 (setq art (read (current-buffer)))))
1612 (if (and (numberp art)
1614 (gnus-agent-article-name
1615 (number-to-string art) group)))
1621 ;; Remove old NOV lines that have no articles.
1622 (gnus-delete-line)))
1627 ;; Nuke the article.
1628 (when (file-exists-p
1629 (setq file (gnus-agent-article-name
1630 (number-to-string article)
1633 ;; Schedule the history line for nuking.
1634 (push (cdr elem) histories)))
1635 (gnus-make-directory (file-name-directory nov-file))
1636 (let ((coding-system-for-write
1637 gnus-agent-file-coding-system))
1638 (write-region (point-min) (point-max) nov-file nil 'silent))
1639 ;; Delete the unwanted entries in the alist.
1640 (setq gnus-agent-article-alist
1641 (sort gnus-agent-article-alist 'car-less-than-car))
1642 (let* ((alist gnus-agent-article-alist)
1643 (prev (cons nil alist))
1647 (<= (caar alist) article))
1648 (if (or (not (cdar alist))
1650 (gnus-agent-article-name
1655 (push (caar alist) expired)
1656 (setcdr prev (setq alist (cdr alist))))
1658 alist (cdr alist))))
1659 (setq gnus-agent-article-alist (cdr first))
1660 (gnus-agent-save-alist group)
1661 ;; Mark all articles up to the first article
1662 ;; in `gnus-article-alist' as read.
1663 (when (and info (caar gnus-agent-article-alist))
1664 (setcar (nthcdr 2 info)
1667 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1668 ;; Maybe everything has been expired from `gnus-article-alist'
1669 ;; and so the above marking as read could not be conducted,
1670 ;; or there are expired article within the range of the alist.
1673 (or (not (caar gnus-agent-article-alist))
1675 (caar gnus-agent-article-alist))))
1676 (setcar (nthcdr 2 info)
1679 (nreverse expired))))
1681 (concat "(gnus-group-set-info '"
1682 (gnus-prin1-to-string info)
1685 (if (gnus-gethash group orig)
1686 (setcar (gnus-gethash group orig) lowest)
1687 (gnus-sethash group (cons lowest highest) orig))))
1689 (set-buffer history)
1690 (setq histories (nreverse (sort histories '<)))
1692 (goto-char (pop histories))
1694 (gnus-agent-save-history)
1695 (gnus-agent-close-history)
1696 (gnus-write-active-file
1697 (gnus-agent-lib-file "active") orig))
1698 (gnus-message 4 "Expiry...done")))))))
1701 (defun gnus-agent-batch ()
1703 (let ((init-file-user "")
1704 (gnus-always-read-dribble-file t))
1706 (gnus-group-send-drafts)
1707 (gnus-agent-fetch-session))
1709 (provide 'gnus-agent)
1711 ;;; gnus-agent.el ends here