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)
38 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
39 "Where the Gnus agent will store its files."
43 (defcustom gnus-agent-plugged-hook nil
44 "Hook run when plugging into the network."
48 (defcustom gnus-agent-unplugged-hook nil
49 "Hook run when unplugging from the network."
53 (defcustom gnus-agent-handle-level gnus-level-subscribed
54 "Groups on levels higher than this variable will be ignored by the Agent."
58 (defcustom gnus-agent-expire-days 7
59 "Read articles older than this will be expired."
63 (defcustom gnus-agent-expire-all nil
64 "If non-nil, also expire unread, ticked and dormant articles.
65 If nil, only read articles will be expired."
69 (defcustom gnus-agent-group-mode-hook nil
70 "Hook run in Agent group minor modes."
74 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
75 (when (featurep 'xemacs)
76 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
78 (defcustom gnus-agent-summary-mode-hook nil
79 "Hook run in Agent summary minor modes."
83 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
84 (when (featurep 'xemacs)
85 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
87 (defcustom gnus-agent-server-mode-hook nil
88 "Hook run in Agent summary minor modes."
92 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
93 (when (featurep 'xemacs)
94 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
96 (defcustom gnus-agent-confirmation-function 'y-or-n-p
97 "Function to confirm when error happens."
102 (defcustom gnus-agent-synchronize-flags 'ask
103 "Indicate if flags are synchronized when you plug in.
104 If this is `ask' the hook will query the user."
106 :type '(choice (const :tag "Always" t)
107 (const :tag "Never" nil)
108 (const :tag "Ask" ask))
111 ;;; Internal variables
113 (defvar gnus-agent-history-buffers nil)
114 (defvar gnus-agent-buffer-alist nil)
115 (defvar gnus-agent-article-alist nil)
116 (defvar gnus-agent-group-alist nil)
117 (defvar gnus-agent-covered-methods nil)
118 (defvar gnus-category-alist nil)
119 (defvar gnus-agent-current-history nil)
120 (defvar gnus-agent-overview-buffer nil)
121 (defvar gnus-category-predicate-cache nil)
122 (defvar gnus-category-group-cache nil)
123 (defvar gnus-agent-spam-hashtb nil)
124 (defvar gnus-agent-file-name nil)
125 (defvar gnus-agent-send-mail-function nil)
126 (defvar gnus-agent-file-coding-system 'raw-text)
129 (defvar gnus-headers)
136 (defun gnus-open-agent ()
138 (gnus-agent-read-servers)
140 (gnus-agent-create-buffer)
141 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
142 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
143 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
145 (defun gnus-agent-create-buffer ()
146 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
148 (setq gnus-agent-overview-buffer
149 (gnus-get-buffer-create " *Gnus agent overview*"))
150 (with-current-buffer gnus-agent-overview-buffer
151 (mm-enable-multibyte))
154 (gnus-add-shutdown 'gnus-close-agent 'gnus)
156 (defun gnus-close-agent ()
157 (setq gnus-agent-covered-methods nil
158 gnus-category-predicate-cache nil
159 gnus-category-group-cache nil
160 gnus-agent-spam-hashtb nil)
161 (gnus-kill-buffer gnus-agent-overview-buffer))
164 ;;; Utility functions
167 (defun gnus-agent-read-file (file)
168 "Load FILE and do a `read' there."
171 (nnheader-insert-file-contents file)
172 (goto-char (point-min))
173 (read (current-buffer)))))
175 (defsubst gnus-agent-method ()
176 (concat (symbol-name (car gnus-command-method)) "/"
177 (if (equal (cadr gnus-command-method) "")
179 (cadr gnus-command-method))))
181 (defsubst gnus-agent-directory ()
182 "Path of the Gnus agent directory."
183 (nnheader-concat gnus-agent-directory
184 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
186 (defun gnus-agent-lib-file (file)
187 "The full path of the Gnus agent library FILE."
188 (expand-file-name file
189 (file-name-as-directory
190 (expand-file-name "agent.lib" (gnus-agent-directory)))))
192 ;;; Fetching setup functions.
194 (defun gnus-agent-start-fetch ()
195 "Initialize data structures for efficient fetching."
196 (gnus-agent-open-history)
197 (setq gnus-agent-current-history (gnus-agent-history-buffer))
198 (gnus-agent-create-buffer))
200 (defun gnus-agent-stop-fetch ()
201 "Save all data structures and clean up."
202 (gnus-agent-save-history)
203 (gnus-agent-close-history)
204 (setq gnus-agent-spam-hashtb nil)
206 (set-buffer nntp-server-buffer)
209 (defmacro gnus-agent-with-fetch (&rest forms)
212 (let ((gnus-agent-fetching t))
213 (gnus-agent-start-fetch)
215 (gnus-agent-stop-fetch)))
217 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
218 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
224 (defvar gnus-agent-mode-hook nil
225 "Hook run when installing agent mode.")
227 (defvar gnus-agent-mode nil)
228 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
230 (defun gnus-agent-mode ()
231 "Minor mode for providing a agent support in Gnus buffers."
232 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
233 (symbol-name major-mode))
234 (match-string 1 (symbol-name major-mode))))
235 (mode (intern (format "gnus-agent-%s-mode" buffer))))
236 (set (make-local-variable 'gnus-agent-mode) t)
238 (set (make-local-variable mode) t)
240 (when (gnus-visual-p 'agent-menu 'menu)
241 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
242 (unless (assq 'gnus-agent-mode minor-mode-alist)
243 (push gnus-agent-mode-status minor-mode-alist))
244 (unless (assq mode minor-mode-map-alist)
245 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
247 minor-mode-map-alist))
248 (when (eq major-mode 'gnus-group-mode)
249 (gnus-agent-toggle-plugged gnus-plugged))
250 (gnus-run-hooks 'gnus-agent-mode-hook
251 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
253 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
254 (gnus-define-keys gnus-agent-group-mode-map
255 "Ju" gnus-agent-fetch-groups
256 "Jc" gnus-enter-category-buffer
257 "Jj" gnus-agent-toggle-plugged
258 "Js" gnus-agent-fetch-session
259 "JY" gnus-agent-synchronize-flags
260 "JS" gnus-group-send-drafts
261 "Ja" gnus-agent-add-group
262 "Jr" gnus-agent-remove-group)
264 (defun gnus-agent-group-make-menu-bar ()
265 (unless (boundp 'gnus-agent-group-menu)
267 gnus-agent-group-menu gnus-agent-group-mode-map ""
269 ["Toggle plugged" gnus-agent-toggle-plugged t]
270 ["List categories" gnus-enter-category-buffer t]
271 ["Send drafts" gnus-group-send-drafts gnus-plugged]
273 ["All" gnus-agent-fetch-session gnus-plugged]
274 ["Group" gnus-agent-fetch-group gnus-plugged])))))
276 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
277 (gnus-define-keys gnus-agent-summary-mode-map
278 "Jj" gnus-agent-toggle-plugged
279 "J#" gnus-agent-mark-article
280 "J\M-#" gnus-agent-unmark-article
281 "@" gnus-agent-toggle-mark
282 "Jc" gnus-agent-catchup)
284 (defun gnus-agent-summary-make-menu-bar ()
285 (unless (boundp 'gnus-agent-summary-menu)
287 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
289 ["Toggle plugged" gnus-agent-toggle-plugged t]
290 ["Mark as downloadable" gnus-agent-mark-article t]
291 ["Unmark as downloadable" gnus-agent-unmark-article t]
292 ["Toggle mark" gnus-agent-toggle-mark t]
293 ["Catchup undownloaded" gnus-agent-catchup t]))))
295 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
296 (gnus-define-keys gnus-agent-server-mode-map
297 "Jj" gnus-agent-toggle-plugged
298 "Ja" gnus-agent-add-server
299 "Jr" gnus-agent-remove-server)
301 (defun gnus-agent-server-make-menu-bar ()
302 (unless (boundp 'gnus-agent-server-menu)
304 gnus-agent-server-menu gnus-agent-server-mode-map ""
306 ["Toggle plugged" gnus-agent-toggle-plugged t]
307 ["Add" gnus-agent-add-server t]
308 ["Remove" gnus-agent-remove-server t]))))
310 (defun gnus-agent-toggle-plugged (plugged)
311 "Toggle whether Gnus is unplugged or not."
312 (interactive (list (not gnus-plugged)))
315 (setq gnus-plugged plugged)
316 (gnus-agent-possibly-synchronize-flags)
317 (gnus-run-hooks 'gnus-agent-plugged-hook)
318 (setcar (cdr gnus-agent-mode-status) " Plugged"))
319 (gnus-agent-close-connections)
320 (setq gnus-plugged plugged)
321 (gnus-run-hooks 'gnus-agent-unplugged-hook)
322 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
323 (set-buffer-modified-p t))
325 (defun gnus-agent-close-connections ()
326 "Close all methods covered by the Gnus agent."
327 (let ((methods gnus-agent-covered-methods))
329 (gnus-close-server (pop methods)))))
332 (defun gnus-unplugged ()
333 "Start Gnus unplugged."
335 (setq gnus-plugged nil)
339 (defun gnus-plugged ()
340 "Start Gnus plugged."
342 (setq gnus-plugged t)
346 (defun gnus-agentize ()
347 "Allow Gnus to be an offline newsreader.
348 The normal usage of this command is to put the following as the
349 last form in your `.gnus.el' file:
353 This will modify the `gnus-before-startup-hook', `gnus-post-method',
354 and `message-send-mail-function' variables, and install the Gnus
355 agent minor mode in all Gnus buffers."
358 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
359 (unless gnus-agent-send-mail-function
360 (setq gnus-agent-send-mail-function message-send-mail-function
361 message-send-mail-function 'gnus-agent-send-mail))
362 (unless gnus-agent-covered-methods
363 (setq gnus-agent-covered-methods (list gnus-select-method))))
365 (defun gnus-agent-queue-setup ()
366 "Make sure the queue group exists."
367 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
368 (gnus-request-create-group "queue" '(nndraft ""))
369 (let ((gnus-level-default-subscribed 1))
370 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
371 (gnus-group-set-parameter
372 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
374 (defun gnus-agent-send-mail ()
376 (funcall gnus-agent-send-mail-function)
377 (goto-char (point-min))
379 (concat "^" (regexp-quote mail-header-separator) "\n"))
381 (gnus-agent-insert-meta-information 'mail)
382 (gnus-request-accept-article "nndraft:queue" nil t t)))
384 (defun gnus-agent-insert-meta-information (type &optional method)
385 "Insert meta-information into the message that says how it's to be posted.
386 TYPE can be either `mail' or `news'. If the latter, then METHOD can
389 (message-remove-header gnus-agent-meta-information-header)
390 (goto-char (point-min))
391 (insert gnus-agent-meta-information-header ": "
392 (symbol-name type) " " (format "%S" method)
395 (while (search-backward "\n" nil t)
396 (replace-match "\\n" t t))))
398 (defun gnus-agent-restore-gcc ()
399 "Restore GCC field from saved header."
401 (goto-char (point-min))
402 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
403 (replace-match "Gcc:" 'fixedcase))))
405 (defun gnus-agent-any-covered-gcc ()
407 (message-narrow-to-headers)
408 (let* ((gcc (mail-fetch-field "gcc" nil t))
410 (mapcar 'gnus-inews-group-method
411 (message-unquote-tokens
412 (message-tokenize-header
415 (while (and (not covered) methods)
417 (member (car methods) gnus-agent-covered-methods)
418 methods (cdr methods)))
421 (defun gnus-agent-possibly-save-gcc ()
422 "Save GCC if Gnus is unplugged."
423 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
425 (goto-char (point-min))
426 (let ((case-fold-search t))
427 (while (re-search-forward "^gcc:" nil t)
428 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
430 (defun gnus-agent-possibly-do-gcc ()
431 "Do GCC if Gnus is plugged."
432 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
433 (gnus-inews-do-gcc)))
436 ;;; Group mode commands
439 (defun gnus-agent-fetch-groups (n)
440 "Put all new articles in the current groups into the Agent."
443 (error "Groups can't be fetched when Gnus is unplugged"))
444 (gnus-group-iterate n 'gnus-agent-fetch-group))
446 (defun gnus-agent-fetch-group (group)
447 "Put all new articles in GROUP into the Agent."
448 (interactive (list (gnus-group-group-name)))
450 (error "Groups can't be fetched when Gnus is unplugged"))
452 (error "No group on the current line"))
453 (let ((gnus-command-method (gnus-find-method-for-group group)))
454 (gnus-agent-with-fetch
455 (gnus-agent-fetch-group-1 group gnus-command-method)
456 (gnus-message 5 "Fetching %s...done" group))))
458 (defun gnus-agent-add-group (category arg)
459 "Add the current group to an agent category."
465 (mapcar (lambda (cat) (list (symbol-name (car cat))))
469 (let ((cat (assq category gnus-category-alist))
471 (gnus-group-iterate arg
473 (when (cadddr (setq c (gnus-group-category group)))
474 (setf (cadddr c) (delete group (cadddr c))))
475 (push group groups)))
476 (setf (cadddr cat) (nconc (cadddr cat) groups))
477 (gnus-category-write)))
479 (defun gnus-agent-remove-group (arg)
480 "Remove the current group from its agent category, if any."
483 (gnus-group-iterate arg
485 (when (cadddr (setq c (gnus-group-category group)))
486 (setf (cadddr c) (delete group (cadddr c))))))
487 (gnus-category-write)))
489 (defun gnus-agent-synchronize-flags ()
490 "Synchronize unplugged flags with servers."
493 (dolist (gnus-command-method gnus-agent-covered-methods)
494 (when (file-exists-p (gnus-agent-lib-file "flags"))
495 (gnus-agent-synchronize-flags-server gnus-command-method)))))
497 (defun gnus-agent-possibly-synchronize-flags ()
498 "Synchronize flags according to `gnus-agent-synchronize-flags'."
501 (dolist (gnus-command-method gnus-agent-covered-methods)
502 (when (file-exists-p (gnus-agent-lib-file "flags"))
503 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
505 (defun gnus-agent-synchronize-flags-server (method)
506 "Synchronize flags set when unplugged for server."
507 (let ((gnus-command-method method))
508 (when (file-exists-p (gnus-agent-lib-file "flags"))
509 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
511 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
512 (if (null (gnus-check-server gnus-command-method))
513 (message "Couldn't open server %s" (nth 1 gnus-command-method))
515 (if (null (eval (read (current-buffer))))
516 (progn (forward-line)
518 (write-file (gnus-agent-lib-file "flags"))
519 (error "Couldn't set flags from file %s"
520 (gnus-agent-lib-file "flags"))))
521 (delete-file (gnus-agent-lib-file "flags")))
524 (defun gnus-agent-possibly-synchronize-flags-server (method)
525 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
526 (when (or (and gnus-agent-synchronize-flags
527 (not (eq gnus-agent-synchronize-flags 'ask)))
528 (and (eq gnus-agent-synchronize-flags 'ask)
529 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
531 (gnus-agent-synchronize-flags-server method)))
534 ;;; Server mode commands
537 (defun gnus-agent-add-server (server)
538 "Enroll SERVER in the agent program."
539 (interactive (list (gnus-server-server-name)))
541 (error "No server on the current line"))
542 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
543 (when (member method gnus-agent-covered-methods)
544 (error "Server already in the agent program"))
545 (push method gnus-agent-covered-methods)
546 (gnus-agent-write-servers)
547 (message "Entered %s into the Agent" server)))
549 (defun gnus-agent-remove-server (server)
550 "Remove SERVER from the agent program."
551 (interactive (list (gnus-server-server-name)))
553 (error "No server on the current line"))
554 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
555 (unless (member method gnus-agent-covered-methods)
556 (error "Server not in the agent program"))
557 (setq gnus-agent-covered-methods
558 (delete method gnus-agent-covered-methods))
559 (gnus-agent-write-servers)
560 (message "Removed %s from the agent" server)))
562 (defun gnus-agent-read-servers ()
563 "Read the alist of covered servers."
564 (setq gnus-agent-covered-methods
565 (gnus-agent-read-file
566 (nnheader-concat gnus-agent-directory "lib/servers"))))
568 (defun gnus-agent-write-servers ()
569 "Write the alist of covered servers."
570 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
571 (let ((coding-system-for-write nnheader-file-coding-system)
572 (file-name-coding-system nnmail-pathname-coding-system))
573 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
574 (prin1 gnus-agent-covered-methods (current-buffer)))))
580 (defun gnus-agent-mark-article (n &optional unmark)
581 "Mark the next N articles as downloadable.
582 If N is negative, mark backward instead. If UNMARK is non-nil, remove
583 the mark instead. The difference between N and the actual number of
584 articles marked is returned."
586 (let ((backward (< n 0))
591 (gnus-summary-set-agent-mark
592 (gnus-summary-article-number) unmark)
593 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
596 (gnus-message 7 "No more articles"))
597 (gnus-summary-recenter)
598 (gnus-summary-position-point)
601 (defun gnus-agent-unmark-article (n)
602 "Remove the downloadable mark from the next N articles.
603 If N is negative, unmark backward instead. The difference between N and
604 the actual number of articles unmarked is returned."
606 (gnus-agent-mark-article n t))
608 (defun gnus-agent-toggle-mark (n)
609 "Toggle the downloadable mark from the next N articles.
610 If N is negative, toggle backward instead. The difference between N and
611 the actual number of articles toggled is returned."
613 (gnus-agent-mark-article n 'toggle))
615 (defun gnus-summary-set-agent-mark (article &optional unmark)
616 "Mark ARTICLE as downloadable."
617 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
618 (memq article gnus-newsgroup-downloadable)
622 (setq gnus-newsgroup-downloadable
623 (delq article gnus-newsgroup-downloadable))
624 (push article gnus-newsgroup-undownloaded))
625 (setq gnus-newsgroup-undownloaded
626 (delq article gnus-newsgroup-undownloaded))
627 (push article gnus-newsgroup-downloadable))
628 (gnus-summary-update-mark
629 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
632 (defun gnus-agent-get-undownloaded-list ()
633 "Mark all unfetched articles as read."
634 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
635 (when (and (not gnus-plugged)
636 (gnus-agent-method-p gnus-command-method))
637 (gnus-agent-load-alist gnus-newsgroup-name)
638 ;; First mark all undownloaded articles as undownloaded.
639 (let ((articles (append gnus-newsgroup-unreads
640 gnus-newsgroup-marked
641 gnus-newsgroup-dormant))
643 (while (setq article (pop articles))
644 (unless (or (cdr (assq article gnus-agent-article-alist))
645 (memq article gnus-newsgroup-downloadable)
646 (memq article gnus-newsgroup-cached))
647 (push article gnus-newsgroup-undownloaded))))
648 ;; Then mark downloaded downloadable as not-downloadable,
649 ;; if you get my drift.
650 (let ((articles gnus-newsgroup-downloadable)
652 (while (setq article (pop articles))
653 (when (cdr (assq article gnus-agent-article-alist))
654 (setq gnus-newsgroup-downloadable
655 (delq article gnus-newsgroup-downloadable))))))))
657 (defun gnus-agent-catchup ()
658 "Mark all undownloaded articles as read."
661 (while gnus-newsgroup-undownloaded
662 (gnus-summary-mark-article
663 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
664 (gnus-summary-position-point))
667 ;;; Internal functions
670 (defun gnus-agent-save-active (method)
671 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
673 (defun gnus-agent-save-active-1 (method function)
674 (when (gnus-agent-method-p method)
675 (let* ((gnus-command-method method)
676 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
677 (file (gnus-agent-lib-file "active")))
678 (funcall function nil new)
679 (gnus-agent-write-active file new)
681 (nnheader-insert-file-contents file))))
683 (defun gnus-agent-write-active (file new)
684 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
685 (file (gnus-agent-lib-file "active"))
687 (when (file-exists-p file)
689 (nnheader-insert-file-contents file)
690 (gnus-active-to-gnus-format nil orig))
693 (when (and sym (boundp sym))
694 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
695 (setq elem (symbol-value osym)))
696 (setcdr elem (cdr (symbol-value sym)))
697 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
699 (gnus-make-directory (file-name-directory file))
700 (let ((coding-system-for-write gnus-agent-file-coding-system))
701 ;; The hashtable contains real names of groups, no more prefix
702 ;; removing, so set `full' to `t'.
703 (gnus-write-active-file file orig t))))
705 (defun gnus-agent-save-groups (method)
706 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
708 (defun gnus-agent-save-group-info (method group active)
709 (when (gnus-agent-method-p method)
710 (let* ((gnus-command-method method)
711 (coding-system-for-write nnheader-file-coding-system)
712 (file-name-coding-system nnmail-pathname-coding-system)
713 (file (gnus-agent-lib-file "active"))
715 (gnus-make-directory (file-name-directory file))
717 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
718 (mm-disable-multibyte)
719 (when (file-exists-p file)
720 (nnheader-insert-file-contents file))
721 (goto-char (point-min))
722 (when (re-search-forward
723 (concat "^" (regexp-quote group) " ") nil t)
726 (narrow-to-region (match-beginning 0)
730 (setq oactive (car (nnmail-parse-active)))))
732 (insert (format "%S %d %d y\n" (intern group)
734 (or (car oactive) (car active))))
735 (goto-char (point-max))
736 (while (search-backward "\\." nil t)
739 (defun gnus-agent-group-path (group)
740 "Translate GROUP into a path."
741 (if nnmail-use-long-file-names
742 (gnus-group-real-name group)
743 (nnheader-translate-file-chars
744 (nnheader-replace-chars-in-string
745 (nnheader-replace-duplicate-chars-in-string
746 (nnheader-replace-chars-in-string
747 (gnus-group-real-name group)
754 (defun gnus-agent-method-p (method)
755 "Say whether METHOD is covered by the agent."
756 (member method gnus-agent-covered-methods))
758 (defun gnus-agent-get-function (method)
759 (if (and (not gnus-plugged)
760 (gnus-agent-method-p method))
766 ;;; History functions
768 (defun gnus-agent-history-buffer ()
769 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
771 (defun gnus-agent-open-history ()
773 (push (cons (gnus-agent-method)
774 (set-buffer (gnus-get-buffer-create
775 (format " *Gnus agent %s history*"
776 (gnus-agent-method)))))
777 gnus-agent-history-buffers)
778 (mm-disable-multibyte) ;; everything is binary
781 (let ((file (gnus-agent-lib-file "history")))
782 (when (file-exists-p file)
783 (nnheader-insert-file-contents file))
784 (set (make-local-variable 'gnus-agent-file-name) file))))
786 (defun gnus-agent-save-history ()
788 (set-buffer gnus-agent-current-history)
789 (gnus-make-directory (file-name-directory gnus-agent-file-name))
790 (let ((coding-system-for-write gnus-agent-file-coding-system))
791 (write-region (1+ (point-min)) (point-max)
792 gnus-agent-file-name nil 'silent))))
794 (defun gnus-agent-close-history ()
795 (when (gnus-buffer-live-p gnus-agent-current-history)
796 (kill-buffer gnus-agent-current-history)
797 (setq gnus-agent-history-buffers
798 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
799 gnus-agent-history-buffers))))
801 (defun gnus-agent-enter-history (id group-arts date)
803 (set-buffer gnus-agent-current-history)
804 (goto-char (point-max))
806 (insert id "\t" (number-to-string date) "\t")
808 (insert (format "%S" (intern (caar group-arts)))
809 " " (number-to-string (cdr (pop group-arts)))
812 (while (search-backward "\\." p t)
815 (defun gnus-agent-article-in-history-p (id)
817 (set-buffer (gnus-agent-history-buffer))
818 (goto-char (point-min))
819 (search-forward (concat "\n" id "\t") nil t)))
821 (defun gnus-agent-history-path (id)
823 (set-buffer (gnus-agent-history-buffer))
824 (goto-char (point-min))
825 (when (search-forward (concat "\n" id "\t") nil t)
826 (let ((method (gnus-agent-method)))
828 (while (not (numberp (setq group (read (current-buffer)))))
829 (push (concat method "/" group) paths))
830 (nreverse paths))))))
836 (defun gnus-agent-fetch-articles (group articles)
837 "Fetch ARTICLES from GROUP and put them into the Agent."
839 ;; Prune off articles that we have already fetched.
841 (cdr (assq (car articles) gnus-agent-article-alist)))
843 (let ((arts articles))
845 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
846 (setcdr arts (cddr arts))
847 (setq arts (cdr arts)))))
850 (gnus-agent-directory)
851 (gnus-agent-group-path group) "/"))
852 (date (time-to-days (current-time)))
855 (gnus-make-directory dir)
856 (gnus-message 7 "Fetching articles for %s..." group)
857 ;; Fetch the articles from the backend.
858 (if (gnus-check-backend-function 'retrieve-articles group)
859 (setq pos (gnus-retrieve-articles articles group))
862 (while (setq article (pop articles))
864 (gnus-backlog-request-article group article
866 (gnus-request-article article group))
867 (goto-char (point-max))
868 (push (cons article (point)) pos)
869 (insert-buffer-substring nntp-server-buffer)))
870 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
871 (setq pos (nreverse pos)))))
872 ;; Then save these articles into the Agent.
874 (set-buffer nntp-server-buffer)
876 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
877 (goto-char (point-min))
878 (when (search-forward "\n\n" nil t)
879 (when (search-backward "\nXrefs: " nil t)
880 ;; Handle crossposting.
881 (skip-chars-forward "^ ")
882 (skip-chars-forward " ")
884 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
885 (push (cons (buffer-substring (match-beginning 1)
887 (buffer-substring (match-beginning 2)
890 (goto-char (match-end 0)))
891 (gnus-agent-crosspost crosses (caar pos))))
892 (goto-char (point-min))
893 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
894 (setq id "No-Message-ID-in-article")
895 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
896 (let ((coding-system-for-write
897 gnus-agent-file-coding-system))
898 (write-region (point-min) (point-max)
899 (concat dir (number-to-string (caar pos)))
901 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
903 (gnus-agent-enter-history
904 id (or crosses (list (cons group (caar pos)))) date)
907 (gnus-agent-save-alist group)))))
909 (defun gnus-agent-crosspost (crosses article)
910 (let (gnus-agent-article-alist group alist beg end)
912 (set-buffer gnus-agent-overview-buffer)
913 (when (nnheader-find-nov-line article)
916 (setq end (progn (forward-line 1) (point)))))
918 (setq group (caar crosses))
919 (unless (setq alist (assoc group gnus-agent-group-alist))
920 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
921 gnus-agent-group-alist))
922 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
924 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
926 (when (= (point-max) (point-min))
927 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
929 (nnheader-insert-file-contents
930 (gnus-agent-article-name ".overview" group))))
931 (nnheader-find-nov-line (string-to-number (cdar crosses)))
932 (insert (string-to-number (cdar crosses)))
933 (insert-buffer-substring gnus-agent-overview-buffer beg end))
936 (defun gnus-agent-flush-cache ()
938 (while gnus-agent-buffer-alist
939 (set-buffer (cdar gnus-agent-buffer-alist))
940 (let ((coding-system-for-write
941 gnus-agent-file-coding-system))
942 (write-region (point-min) (point-max)
943 (gnus-agent-article-name ".overview"
944 (caar gnus-agent-buffer-alist))
946 (pop gnus-agent-buffer-alist))
947 (while gnus-agent-group-alist
948 (with-temp-file (caar gnus-agent-group-alist)
949 (princ (cdar gnus-agent-group-alist))
951 (pop gnus-agent-group-alist))))
953 (defun gnus-agent-fetch-headers (group &optional force)
954 (let ((articles (gnus-list-of-unread-articles group))
955 (gnus-decode-encoded-word-function 'identity)
956 (file (gnus-agent-article-name ".overview" group)))
957 ;; Add article with marks to list of article headers we want to fetch.
958 (dolist (arts (gnus-info-marks (gnus-get-info group)))
959 (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
961 (setq articles (sort articles '<))
962 ;; Remove known articles.
963 (when (gnus-agent-load-alist group)
964 (setq articles (gnus-sorted-intersection
966 (gnus-uncompress-range
967 (cons (1+ (caar (last gnus-agent-article-alist)))
968 (cdr (gnus-active group)))))))
970 (gnus-make-directory (nnheader-translate-file-chars
971 (file-name-directory file) t))
973 (gnus-message 7 "Fetching headers for %s..." group)
975 (set-buffer nntp-server-buffer)
976 (unless (eq 'nov (gnus-retrieve-headers articles group))
977 (nnvirtual-convert-headers))
978 ;; Save these headers for later processing.
979 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
980 (when (file-exists-p file)
981 (gnus-agent-braid-nov group articles file))
982 (let ((coding-system-for-write
983 gnus-agent-file-coding-system))
984 (write-region (point-min) (point-max) file nil 'silent))
985 (gnus-agent-save-alist group articles nil)
986 (gnus-agent-enter-history
987 "last-header-fetched-for-session"
988 (list (cons group (nth (- (length articles) 1) articles)))
989 (time-to-days (current-time)))
992 (defsubst gnus-agent-copy-nov-line (article)
994 (set-buffer gnus-agent-overview-buffer)
996 (if (eq article (read (current-buffer)))
997 (setq e (progn (forward-line 1) (point)))
1001 (set-buffer nntp-server-buffer)
1002 (insert-buffer-substring gnus-agent-overview-buffer b e)))
1004 (defun gnus-agent-braid-nov (group articles file)
1005 (set-buffer gnus-agent-overview-buffer)
1006 (goto-char (point-min))
1007 (set-buffer nntp-server-buffer)
1009 (nnheader-insert-file-contents file)
1010 (goto-char (point-max))
1011 (if (or (= (point-min) (point-max))
1014 (< (read (current-buffer)) (car articles))))
1015 ;; We have only headers that are after the older headers,
1016 ;; so we just append them.
1018 (goto-char (point-max))
1019 (insert-buffer-substring gnus-agent-overview-buffer))
1020 ;; We do it the hard way.
1021 (nnheader-find-nov-line (car articles))
1022 (gnus-agent-copy-nov-line (car articles))
1024 (while (and articles
1026 (while (and (not (eobp))
1027 (< (read (current-buffer)) (car articles)))
1031 (gnus-agent-copy-nov-line (car articles))
1032 (setq articles (cdr articles))))
1035 (set-buffer gnus-agent-overview-buffer)
1038 (set-buffer nntp-server-buffer)
1039 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1041 (defun gnus-agent-load-alist (group &optional dir)
1042 "Load the article-state alist for GROUP."
1043 (setq gnus-agent-article-alist
1044 (gnus-agent-read-file
1046 (expand-file-name ".agentview" dir)
1047 (gnus-agent-article-name ".agentview" group)))))
1049 (defun gnus-agent-save-alist (group &optional articles state dir)
1050 "Save the article-state alist for GROUP."
1051 (let ((file-name-coding-system nnmail-pathname-coding-system))
1052 (with-temp-file (if dir
1053 (expand-file-name ".agentview" dir)
1054 (gnus-agent-article-name ".agentview" group))
1055 (princ (setq gnus-agent-article-alist
1056 (nconc gnus-agent-article-alist
1057 (mapcar (lambda (article) (cons article state))
1062 (defun gnus-agent-article-name (article group)
1063 (expand-file-name (if (stringp article) article (string-to-number article))
1064 (file-name-as-directory
1065 (expand-file-name (gnus-agent-group-path group)
1066 (gnus-agent-directory)))))
1068 (defun gnus-agent-batch-confirmation (msg)
1069 "Show error message and return t."
1070 (gnus-message 1 msg)
1074 (defun gnus-agent-batch-fetch ()
1075 "Start Gnus and fetch session."
1078 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1079 (gnus-agent-fetch-session))
1082 (defun gnus-agent-fetch-session ()
1083 "Fetch all articles and headers that are eligible for fetching."
1085 (unless gnus-agent-covered-methods
1086 (error "No servers are covered by the Gnus agent"))
1087 (unless gnus-plugged
1088 (error "Can't fetch articles while Gnus is unplugged"))
1089 (let ((methods gnus-agent-covered-methods)
1090 groups group gnus-command-method)
1095 (setq gnus-command-method (car methods))
1096 (when (or (gnus-server-opened gnus-command-method)
1097 (gnus-open-server gnus-command-method))
1098 (setq groups (gnus-groups-from-server (car methods)))
1099 (gnus-agent-with-fetch
1100 (while (setq group (pop groups))
1101 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1102 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1104 (unless (funcall gnus-agent-confirmation-function
1105 (format "Error (%s). Continue? " err))
1106 (error "Cannot fetch articles into the Gnus agent.")))
1108 (unless (funcall gnus-agent-confirmation-function
1109 (format "Quit fetching session (%s). Continue? "
1111 (signal 'quit "Cannot fetch articles into the Gnus agent."))))
1113 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1115 (defun gnus-agent-fetch-group-1 (group method)
1117 (let ((gnus-command-method method)
1118 (gnus-newsgroup-name group)
1119 gnus-newsgroup-dependencies gnus-newsgroup-headers
1120 gnus-newsgroup-scored gnus-headers gnus-score
1121 gnus-use-cache articles arts
1122 category predicate info marks score-param
1123 (gnus-summary-expunge-below gnus-summary-expunge-below)
1124 (gnus-summary-mark-below gnus-summary-mark-below)
1125 (gnus-orphan-score gnus-orphan-score)
1126 ;; Maybe some other gnus-summary local variables should also
1129 (unless (gnus-check-group group)
1130 (error "Can't open server for %s" group))
1132 (when (and (or (gnus-active group) (gnus-activate-group group))
1133 (setq articles (gnus-agent-fetch-headers group))
1134 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1135 ;; Parse them and see which articles we want to fetch.
1136 (setq gnus-newsgroup-dependencies
1137 (make-vector (length articles) 0))
1138 (setq gnus-newsgroup-headers
1139 (gnus-get-newsgroup-headers-xover articles nil nil
1141 ;; `gnus-agent-overview-buffer' may be killed for
1142 ;; timeout reason. If so, recreate it.
1143 (gnus-agent-create-buffer)))
1144 (setq category (gnus-group-category group))
1147 (or (gnus-group-find-parameter group 'agent-predicate t)
1149 (if (memq (caaddr predicate) '(gnus-agent-true gnus-agent-false))
1150 ;; Simple implementation
1152 (and (eq (caaddr predicate) 'gnus-agent-true) articles))
1155 (or (gnus-group-get-parameter group 'agent-score t)
1157 ;; Translate score-param into real one
1160 ((eq score-param 'file)
1161 (setq score-param (gnus-all-score-files group)))
1162 ((stringp (car score-param)))
1164 (setq score-param (list (list score-param)))))
1166 (gnus-score-headers score-param))
1167 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1169 (or (cdr (assq (mail-header-number gnus-headers)
1170 gnus-newsgroup-scored))
1171 gnus-summary-default-score))
1172 (when (funcall predicate)
1173 (push (mail-header-number gnus-headers)
1175 ;; Fetch the articles.
1177 (gnus-agent-fetch-articles group arts)))
1178 ;; Perhaps we have some additional articles to fetch.
1179 (setq arts (assq 'download (gnus-info-marks
1180 (setq info (gnus-get-info group)))))
1182 (gnus-agent-fetch-articles
1183 group (gnus-uncompress-range (cdr arts)))
1184 (setq marks (delq arts (gnus-info-marks info)))
1185 (gnus-info-set-marks info marks)
1187 (concat "(gnus-group-set-info '"
1188 (gnus-prin1-to-string info)
1192 ;;; Agent Category Mode
1195 (defvar gnus-category-mode-hook nil
1196 "Hook run in `gnus-category-mode' buffers.")
1198 (defvar gnus-category-line-format " %(%20c%): %g\n"
1199 "Format of category lines.")
1201 (defvar gnus-category-mode-line-format "Gnus: %%b"
1202 "The format specification for the category mode line.")
1204 (defvar gnus-agent-short-article 100
1205 "Articles that have fewer lines than this are short.")
1207 (defvar gnus-agent-long-article 200
1208 "Articles that have more lines than this are long.")
1210 (defvar gnus-agent-low-score 0
1211 "Articles that have a score lower than this have a low score.")
1213 (defvar gnus-agent-high-score 0
1214 "Articles that have a score higher than this have a high score.")
1217 ;;; Internal variables.
1219 (defvar gnus-category-buffer "*Agent Category*")
1221 (defvar gnus-category-line-format-alist
1222 `((?c gnus-tmp-name ?s)
1223 (?g gnus-tmp-groups ?d)))
1225 (defvar gnus-category-mode-line-format-alist
1226 `((?u user-defined ?s)))
1228 (defvar gnus-category-line-format-spec nil)
1229 (defvar gnus-category-mode-line-format-spec nil)
1231 (defvar gnus-category-mode-map nil)
1232 (put 'gnus-category-mode 'mode-class 'special)
1234 (unless gnus-category-mode-map
1235 (setq gnus-category-mode-map (make-sparse-keymap))
1236 (suppress-keymap gnus-category-mode-map)
1238 (gnus-define-keys gnus-category-mode-map
1239 "q" gnus-category-exit
1240 "k" gnus-category-kill
1241 "c" gnus-category-copy
1242 "a" gnus-category-add
1243 "p" gnus-category-edit-predicate
1244 "g" gnus-category-edit-groups
1245 "s" gnus-category-edit-score
1246 "l" gnus-category-list
1248 "\C-c\C-i" gnus-info-find-node
1249 "\C-c\C-b" gnus-bug))
1251 (defvar gnus-category-menu-hook nil
1252 "*Hook run after the creation of the menu.")
1254 (defun gnus-category-make-menu-bar ()
1255 (gnus-turn-off-edit-menu 'category)
1256 (unless (boundp 'gnus-category-menu)
1258 gnus-category-menu gnus-category-mode-map ""
1260 ["Add" gnus-category-add t]
1261 ["Kill" gnus-category-kill t]
1262 ["Copy" gnus-category-copy t]
1263 ["Edit predicate" gnus-category-edit-predicate t]
1264 ["Edit score" gnus-category-edit-score t]
1265 ["Edit groups" gnus-category-edit-groups t]
1266 ["Exit" gnus-category-exit t]))
1268 (gnus-run-hooks 'gnus-category-menu-hook)))
1270 (defun gnus-category-mode ()
1271 "Major mode for listing and editing agent categories.
1273 All normal editing commands are switched off.
1274 \\<gnus-category-mode-map>
1275 For more in-depth information on this mode, read the manual
1276 (`\\[gnus-info-find-node]').
1278 The following commands are available:
1280 \\{gnus-category-mode-map}"
1282 (when (gnus-visual-p 'category-menu 'menu)
1283 (gnus-category-make-menu-bar))
1284 (kill-all-local-variables)
1285 (gnus-simplify-mode-line)
1286 (setq major-mode 'gnus-category-mode)
1287 (setq mode-name "Category")
1288 (gnus-set-default-directory)
1289 (setq mode-line-process nil)
1290 (use-local-map gnus-category-mode-map)
1291 (buffer-disable-undo)
1292 (setq truncate-lines t)
1293 (setq buffer-read-only t)
1294 (gnus-run-hooks 'gnus-category-mode-hook))
1296 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1298 (defun gnus-category-insert-line (category)
1299 (let* ((gnus-tmp-name (car category))
1300 (gnus-tmp-groups (length (cadddr category))))
1302 (gnus-add-text-properties
1306 (eval gnus-category-line-format-spec))
1307 (list 'gnus-category gnus-tmp-name))))
1309 (defun gnus-enter-category-buffer ()
1310 "Go to the Category buffer."
1312 (gnus-category-setup-buffer)
1313 (gnus-configure-windows 'category)
1314 (gnus-category-prepare))
1316 (defun gnus-category-setup-buffer ()
1317 (unless (get-buffer gnus-category-buffer)
1319 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1320 (gnus-category-mode))))
1322 (defun gnus-category-prepare ()
1323 (gnus-set-format 'category-mode)
1324 (gnus-set-format 'category t)
1325 (let ((alist gnus-category-alist)
1326 (buffer-read-only nil))
1329 (gnus-category-insert-line (pop alist)))
1330 (goto-char (point-min))
1331 (gnus-category-position-point)))
1333 (defun gnus-category-name ()
1334 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1335 (error "No category on the current line")))
1337 (defun gnus-category-read ()
1338 "Read the category alist."
1339 (setq gnus-category-alist
1340 (or (gnus-agent-read-file
1341 (nnheader-concat gnus-agent-directory "lib/categories"))
1342 (list (list 'default 'short nil nil)))))
1344 (defun gnus-category-write ()
1345 "Write the category alist."
1346 (setq gnus-category-predicate-cache nil
1347 gnus-category-group-cache nil)
1348 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1349 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1350 (prin1 gnus-category-alist (current-buffer))))
1352 (defun gnus-category-edit-predicate (category)
1353 "Edit the predicate for CATEGORY."
1354 (interactive (list (gnus-category-name)))
1355 (let ((info (assq category gnus-category-alist)))
1357 (cadr info) (format "Editing the predicate for category %s" category)
1358 `(lambda (predicate)
1359 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1360 (gnus-category-write)
1361 (gnus-category-list)))))
1363 (defun gnus-category-edit-score (category)
1364 "Edit the score expression for CATEGORY."
1365 (interactive (list (gnus-category-name)))
1366 (let ((info (assq category gnus-category-alist)))
1369 (format "Editing the score expression for category %s" category)
1371 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1372 (gnus-category-write)
1373 (gnus-category-list)))))
1375 (defun gnus-category-edit-groups (category)
1376 "Edit the group list for CATEGORY."
1377 (interactive (list (gnus-category-name)))
1378 (let ((info (assq category gnus-category-alist)))
1380 (cadddr info) (format "Editing the group list for category %s" category)
1382 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1383 (gnus-category-write)
1384 (gnus-category-list)))))
1386 (defun gnus-category-kill (category)
1387 "Kill the current category."
1388 (interactive (list (gnus-category-name)))
1389 (let ((info (assq category gnus-category-alist))
1390 (buffer-read-only nil))
1392 (setq gnus-category-alist (delq info gnus-category-alist))
1393 (gnus-category-write)))
1395 (defun gnus-category-copy (category to)
1396 "Copy the current category."
1397 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1398 (let ((info (assq category gnus-category-alist)))
1399 (push (list to (gnus-copy-sequence (cadr info))
1400 (gnus-copy-sequence (caddr info)) nil)
1401 gnus-category-alist)
1402 (gnus-category-write)
1403 (gnus-category-list)))
1405 (defun gnus-category-add (category)
1406 "Create a new category."
1407 (interactive "SCategory name: ")
1408 (when (assq category gnus-category-alist)
1409 (error "Category %s already exists" category))
1410 (push (list category 'false nil nil)
1411 gnus-category-alist)
1412 (gnus-category-write)
1413 (gnus-category-list))
1415 (defun gnus-category-list ()
1416 "List all categories."
1418 (gnus-category-prepare))
1420 (defun gnus-category-exit ()
1421 "Return to the group buffer."
1423 (kill-buffer (current-buffer))
1424 (gnus-configure-windows 'group t))
1426 ;; To avoid having 8-bit characters in the source file.
1427 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1429 (defvar gnus-category-predicate-alist
1430 '((spam . gnus-agent-spam-p)
1431 (short . gnus-agent-short-p)
1432 (long . gnus-agent-long-p)
1433 (low . gnus-agent-low-scored-p)
1434 (high . gnus-agent-high-scored-p)
1435 (true . gnus-agent-true)
1436 (false . gnus-agent-false))
1437 "Mapping from short score predicate symbols to predicate functions.")
1439 (defun gnus-agent-spam-p ()
1440 "Say whether an article is spam or not."
1441 (unless gnus-agent-spam-hashtb
1442 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1443 (if (not (equal (mail-header-references gnus-headers) ""))
1445 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1447 (gnus-gethash string gnus-agent-spam-hashtb)
1448 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1450 (defun gnus-agent-short-p ()
1451 "Say whether an article is short or not."
1452 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1454 (defun gnus-agent-long-p ()
1455 "Say whether an article is long or not."
1456 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1458 (defun gnus-agent-low-scored-p ()
1459 "Say whether an article has a low score or not."
1460 (< gnus-score gnus-agent-low-score))
1462 (defun gnus-agent-high-scored-p ()
1463 "Say whether an article has a high score or not."
1464 (> gnus-score gnus-agent-high-score))
1466 (defun gnus-category-make-function (cat)
1467 "Make a function from category CAT."
1468 `(lambda () ,(gnus-category-make-function-1 cat)))
1470 (defun gnus-agent-true ()
1474 (defun gnus-agent-false ()
1478 (defun gnus-category-make-function-1 (cat)
1479 "Make a function from category CAT."
1481 ;; Functions are just returned as is.
1483 (gnus-functionp cat))
1484 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1486 ;; More complex category.
1489 ((memq (car cat) '(& and))
1491 ((memq (car cat) '(| or))
1493 ((memq (car cat) gnus-category-not)
1495 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1497 (error "Unknown category type: %s" cat))))
1499 (defun gnus-get-predicate (predicate)
1500 "Return the predicate for CATEGORY."
1501 (or (cdr (assoc predicate gnus-category-predicate-cache))
1502 (cdar (push (cons predicate
1503 (gnus-category-make-function predicate))
1504 gnus-category-predicate-cache))))
1506 (defun gnus-group-category (group)
1507 "Return the category GROUP belongs to."
1508 (unless gnus-category-group-cache
1509 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1510 (let ((cs gnus-category-alist)
1512 (while (setq cat (pop cs))
1513 (setq groups (cadddr cat))
1515 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1516 (or (gnus-gethash group gnus-category-group-cache)
1517 (assq 'default gnus-category-alist)))
1519 (defun gnus-agent-expire ()
1520 "Expire all old articles."
1522 (let ((methods gnus-agent-covered-methods)
1523 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1524 gnus-command-method sym group articles
1525 history overview file histories elem art nov-file low info
1526 unreads marked article orig lowest highest)
1528 (setq overview (gnus-get-buffer-create " *expire overview*"))
1529 (while (setq gnus-command-method (pop methods))
1530 (when (file-exists-p (gnus-agent-lib-file "active"))
1532 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1533 (gnus-active-to-gnus-format
1535 (setq orig (gnus-make-hashtable
1536 (count-lines (point-min) (point-max))))))
1537 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1538 (gnus-agent-open-history)
1540 (setq gnus-agent-current-history
1541 (setq history (gnus-agent-history-buffer))))
1542 (goto-char (point-min))
1543 (when (> (buffer-size) 1)
1544 (goto-char (point-min))
1546 (skip-chars-forward "^\t")
1547 (if (let ((fetch-date (read (current-buffer))))
1548 (if (numberp fetch-date)
1550 ;; History file is corrupted.
1553 (format "File %s is corrupted!"
1554 (gnus-agent-lib-file "history")))
1558 ;; New article; we don't expire it.
1560 ;; Old article. Schedule it for possible nuking.
1562 (setq sym (let ((obarray expiry-hashtb) s)
1563 (setq s (read (current-buffer)))
1564 (if (stringp s) (intern s) s)))
1566 (set sym (cons (cons (read (current-buffer)) (point))
1567 (symbol-value sym)))
1568 (set sym (list (cons (read (current-buffer)) (point)))))
1569 (skip-chars-forward " "))
1571 ;; We now have all articles that can possibly be expired.
1574 (setq group (symbol-name sym)
1575 articles (sort (symbol-value sym) 'car-less-than-car)
1576 low (car (gnus-active group))
1577 info (gnus-get-info group)
1578 unreads (ignore-errors
1579 (gnus-list-of-unread-articles group))
1581 (gnus-uncompress-range
1582 (cdr (assq 'tick (gnus-info-marks info))))
1583 (gnus-uncompress-range
1585 (gnus-info-marks info)))))
1586 nov-file (gnus-agent-article-name ".overview" group)
1589 (gnus-agent-load-alist group)
1590 (gnus-message 5 "Expiring articles in %s" group)
1591 (set-buffer overview)
1593 (when (file-exists-p nov-file)
1594 (nnheader-insert-file-contents nov-file))
1595 (goto-char (point-min))
1597 (while (setq elem (pop articles))
1598 (setq article (car elem))
1599 (when (or (null low)
1601 gnus-agent-expire-all
1602 (and (not (memq article unreads))
1603 (not (memq article marked))))
1604 ;; Find and nuke the NOV line.
1605 (while (and (not (eobp))
1607 (setq art (read (current-buffer)))))
1609 (if (and (numberp art)
1611 (gnus-agent-article-name
1612 (number-to-string art) group)))
1618 ;; Remove old NOV lines that have no articles.
1619 (gnus-delete-line)))
1624 ;; Nuke the article.
1625 (when (file-exists-p
1626 (setq file (gnus-agent-article-name
1627 (number-to-string article)
1630 ;; Schedule the history line for nuking.
1631 (push (cdr elem) histories)))
1632 (gnus-make-directory (file-name-directory nov-file))
1633 (let ((coding-system-for-write
1634 gnus-agent-file-coding-system))
1635 (write-region (point-min) (point-max) nov-file nil 'silent))
1636 ;; Delete the unwanted entries in the alist.
1637 (setq gnus-agent-article-alist
1638 (sort gnus-agent-article-alist 'car-less-than-car))
1639 (let* ((alist gnus-agent-article-alist)
1640 (prev (cons nil alist))
1644 (<= (caar alist) article))
1645 (if (or (not (cdar alist))
1647 (gnus-agent-article-name
1652 (push (caar alist) expired)
1653 (setcdr prev (setq alist (cdr alist))))
1655 alist (cdr alist))))
1656 (setq gnus-agent-article-alist (cdr first))
1657 (gnus-agent-save-alist group)
1658 ;; Mark all articles up to the first article
1659 ;; in `gnus-article-alist' as read.
1660 (when (and info (caar gnus-agent-article-alist))
1661 (setcar (nthcdr 2 info)
1664 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1665 ;; Maybe everything has been expired from `gnus-article-alist'
1666 ;; and so the above marking as read could not be conducted,
1667 ;; or there are expired article within the range of the alist.
1670 (or (not (caar gnus-agent-article-alist))
1672 (caar gnus-agent-article-alist))))
1673 (setcar (nthcdr 2 info)
1676 (nreverse expired))))
1678 (concat "(gnus-group-set-info '"
1679 (gnus-prin1-to-string info)
1682 (if (gnus-gethash group orig)
1683 (setcar (gnus-gethash group orig) lowest)
1684 (gnus-sethash group (cons lowest highest) orig))))
1686 (set-buffer history)
1687 (setq histories (nreverse (sort histories '<)))
1689 (goto-char (pop histories))
1691 (gnus-agent-save-history)
1692 (gnus-agent-close-history)
1693 (gnus-write-active-file
1694 (gnus-agent-lib-file "active") orig))
1695 (gnus-message 4 "Expiry...done")))))))
1698 (defun gnus-agent-batch ()
1700 (let ((init-file-user "")
1701 (gnus-always-read-dribble-file t))
1703 (gnus-group-send-drafts)
1704 (gnus-agent-fetch-session))
1706 (provide 'gnus-agent)
1708 ;;; gnus-agent.el ends here