1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
37 (if (featurep 'xemacs)
40 (require 'gnus-group))
43 (autoload 'gnus-server-update-server "gnus-srvr"))
45 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
46 "Where the Gnus agent will store its files."
50 (defcustom gnus-agent-plugged-hook nil
51 "Hook run when plugging into the network."
55 (defcustom gnus-agent-unplugged-hook nil
56 "Hook run when unplugging from the network."
60 (defcustom gnus-agent-handle-level gnus-level-subscribed
61 "Groups on levels higher than this variable will be ignored by the Agent."
65 (defcustom gnus-agent-expire-days 7
66 "Read articles older than this will be expired."
70 (defcustom gnus-agent-expire-all nil
71 "If non-nil, also expire unread, ticked and dormant articles.
72 If nil, only read articles will be expired."
76 (defcustom gnus-agent-group-mode-hook nil
77 "Hook run in Agent group minor modes."
81 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
82 (when (featurep 'xemacs)
83 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
85 (defcustom gnus-agent-summary-mode-hook nil
86 "Hook run in Agent summary minor modes."
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
94 (defcustom gnus-agent-server-mode-hook nil
95 "Hook run in Agent summary minor modes."
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
103 (defcustom gnus-agent-confirmation-function 'y-or-n-p
104 "Function to confirm when error happens."
109 (defcustom gnus-agent-large-newsgroup nil
110 "*The number of articles which indicates a large newsgroup.
111 If the number of unread articles exceeds it, The number of articles to be
112 fetched will be limited to it. If not a positive integer, never consider it."
114 :type '(choice (const nil)
115 (integer :tag "Number")))
117 (defcustom gnus-agent-synchronize-flags 'ask
118 "Indicate if flags are synchronized when you plug in.
119 If this is `ask' the hook will query the user."
121 :type '(choice (const :tag "Always" t)
122 (const :tag "Never" nil)
123 (const :tag "Ask" ask))
126 ;;; Internal variables
128 (defvar gnus-agent-history-buffers nil)
129 (defvar gnus-agent-buffer-alist nil)
130 (defvar gnus-agent-article-alist nil)
131 (defvar gnus-agent-group-alist nil)
132 (defvar gnus-agent-covered-methods nil)
133 (defvar gnus-category-alist nil)
134 (defvar gnus-agent-current-history nil)
135 (defvar gnus-agent-overview-buffer nil)
136 (defvar gnus-category-predicate-cache nil)
137 (defvar gnus-category-group-cache nil)
138 (defvar gnus-agent-spam-hashtb nil)
139 (defvar gnus-agent-file-name nil)
140 (defvar gnus-agent-send-mail-function nil)
141 (defvar gnus-agent-file-coding-system 'raw-text)
144 (defvar gnus-headers)
151 (defun gnus-open-agent ()
153 (gnus-agent-read-servers)
155 (gnus-agent-create-buffer)
156 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
157 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
158 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
160 (defun gnus-agent-create-buffer ()
161 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
163 (setq gnus-agent-overview-buffer
164 (gnus-get-buffer-create " *Gnus agent overview*"))
165 (with-current-buffer gnus-agent-overview-buffer
166 (set-buffer-multibyte t))
169 (gnus-add-shutdown 'gnus-close-agent 'gnus)
171 (defun gnus-close-agent ()
172 (setq gnus-agent-covered-methods nil
173 gnus-category-predicate-cache nil
174 gnus-category-group-cache nil
175 gnus-agent-spam-hashtb nil)
176 (gnus-kill-buffer gnus-agent-overview-buffer))
179 ;;; Utility functions
182 (defun gnus-agent-read-file (file)
183 "Load FILE and do a `read' there."
186 (nnheader-insert-file-contents file)
187 (goto-char (point-min))
188 (read (current-buffer)))))
190 (defsubst gnus-agent-method ()
191 (concat (symbol-name (car gnus-command-method)) "/"
192 (if (equal (cadr gnus-command-method) "")
194 (cadr gnus-command-method))))
196 (defsubst gnus-agent-directory ()
197 "Path of the Gnus agent directory."
198 (nnheader-concat gnus-agent-directory
199 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
201 (defun gnus-agent-lib-file (file)
202 "The full path of the Gnus agent library FILE."
203 (expand-file-name file
204 (file-name-as-directory
205 (expand-file-name "agent.lib" (gnus-agent-directory)))))
207 ;;; Fetching setup functions.
209 (defun gnus-agent-start-fetch ()
210 "Initialize data structures for efficient fetching."
211 (gnus-agent-open-history)
212 (setq gnus-agent-current-history (gnus-agent-history-buffer))
213 (gnus-agent-create-buffer))
215 (defun gnus-agent-stop-fetch ()
216 "Save all data structures and clean up."
217 (gnus-agent-save-history)
218 (gnus-agent-close-history)
219 (setq gnus-agent-spam-hashtb nil)
221 (set-buffer nntp-server-buffer)
224 (defmacro gnus-agent-with-fetch (&rest forms)
227 (let ((gnus-agent-fetching t))
228 (gnus-agent-start-fetch)
230 (gnus-agent-stop-fetch)))
232 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
233 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
239 (defvar gnus-agent-mode-hook nil
240 "Hook run when installing agent mode.")
242 (defvar gnus-agent-mode nil)
243 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
245 (defun gnus-agent-mode ()
246 "Minor mode for providing a agent support in Gnus buffers."
247 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
248 (symbol-name major-mode))
249 (match-string 1 (symbol-name major-mode))))
250 (mode (intern (format "gnus-agent-%s-mode" buffer))))
251 (set (make-local-variable 'gnus-agent-mode) t)
253 (set (make-local-variable mode) t)
255 (when (gnus-visual-p 'agent-menu 'menu)
256 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
257 (unless (assq 'gnus-agent-mode minor-mode-alist)
258 (push gnus-agent-mode-status minor-mode-alist))
259 (unless (assq mode minor-mode-map-alist)
260 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
262 minor-mode-map-alist))
263 (when (eq major-mode 'gnus-group-mode)
264 (gnus-agent-toggle-plugged gnus-plugged))
265 (gnus-run-hooks 'gnus-agent-mode-hook
266 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
268 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
269 (gnus-define-keys gnus-agent-group-mode-map
270 "Ju" gnus-agent-fetch-groups
271 "Jc" gnus-enter-category-buffer
272 "Jj" gnus-agent-toggle-plugged
273 "Js" gnus-agent-fetch-session
274 "JY" gnus-agent-synchronize-flags
275 "JS" gnus-group-send-drafts
276 "Ja" gnus-agent-add-group
277 "Jr" gnus-agent-remove-group)
279 (defun gnus-agent-group-make-menu-bar ()
280 (unless (boundp 'gnus-agent-group-menu)
282 gnus-agent-group-menu gnus-agent-group-mode-map ""
284 ["Toggle plugged" gnus-agent-toggle-plugged t]
285 ["List categories" gnus-enter-category-buffer t]
286 ["Send drafts" gnus-group-send-drafts gnus-plugged]
288 ["All" gnus-agent-fetch-session gnus-plugged]
289 ["Group" gnus-agent-fetch-group gnus-plugged])))))
291 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
292 (gnus-define-keys gnus-agent-summary-mode-map
293 "Jj" gnus-agent-toggle-plugged
294 "Ju" gnus-agent-summary-fetch-group
295 "J#" gnus-agent-mark-article
296 "J\M-#" gnus-agent-unmark-article
297 "@" gnus-agent-toggle-mark
298 "Jc" gnus-agent-catchup)
300 (defun gnus-agent-summary-make-menu-bar ()
301 (unless (boundp 'gnus-agent-summary-menu)
303 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
305 ["Toggle plugged" gnus-agent-toggle-plugged t]
306 ["Mark as downloadable" gnus-agent-mark-article t]
307 ["Unmark as downloadable" gnus-agent-unmark-article t]
308 ["Toggle mark" gnus-agent-toggle-mark t]
309 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
310 ["Catchup undownloaded" gnus-agent-catchup t]))))
312 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
313 (gnus-define-keys gnus-agent-server-mode-map
314 "Jj" gnus-agent-toggle-plugged
315 "Ja" gnus-agent-add-server
316 "Jr" gnus-agent-remove-server)
318 (defun gnus-agent-server-make-menu-bar ()
319 (unless (boundp 'gnus-agent-server-menu)
321 gnus-agent-server-menu gnus-agent-server-mode-map ""
323 ["Toggle plugged" gnus-agent-toggle-plugged t]
324 ["Add" gnus-agent-add-server t]
325 ["Remove" gnus-agent-remove-server t]))))
327 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
328 (if (and (fboundp 'propertize)
329 (fboundp 'make-mode-line-mouse-map))
330 (propertize string 'local-map
331 (make-mode-line-mouse-map mouse-button mouse-func))
334 (defun gnus-agent-toggle-plugged (plugged)
335 "Toggle whether Gnus is unplugged or not."
336 (interactive (list (not gnus-plugged)))
339 (setq gnus-plugged plugged)
340 (gnus-agent-possibly-synchronize-flags)
341 (gnus-run-hooks 'gnus-agent-plugged-hook)
342 (setcar (cdr gnus-agent-mode-status)
343 (gnus-agent-make-mode-line-string " Plugged"
345 'gnus-agent-toggle-plugged)))
346 (gnus-agent-close-connections)
347 (setq gnus-plugged plugged)
348 (gnus-run-hooks 'gnus-agent-unplugged-hook)
349 (setcar (cdr gnus-agent-mode-status)
350 (gnus-agent-make-mode-line-string " Unplugged"
352 'gnus-agent-toggle-plugged)))
353 (force-mode-line-update))
355 (defun gnus-agent-close-connections ()
356 "Close all methods covered by the Gnus agent."
357 (let ((methods gnus-agent-covered-methods))
359 (gnus-close-server (pop methods)))))
362 (defun gnus-unplugged ()
363 "Start Gnus unplugged."
365 (setq gnus-plugged nil)
369 (defun gnus-plugged ()
370 "Start Gnus plugged."
372 (setq gnus-plugged t)
376 (defun gnus-agentize ()
377 "Allow Gnus to be an offline newsreader.
378 The normal usage of this command is to put the following as the
379 last form in your `.gnus.el' file:
383 This will modify the `gnus-setup-news-hook', and
384 `message-send-mail-real-function' variables, and install the Gnus agent
385 minor mode in all Gnus buffers."
388 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
389 (unless gnus-agent-send-mail-function
390 (setq gnus-agent-send-mail-function (or
391 message-send-mail-real-function
392 message-send-mail-function)
393 message-send-mail-real-function 'gnus-agent-send-mail))
394 (unless gnus-agent-covered-methods
395 (setq gnus-agent-covered-methods (list gnus-select-method))))
397 (defun gnus-agent-queue-setup ()
398 "Make sure the queue group exists."
399 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
400 (gnus-request-create-group "queue" '(nndraft ""))
401 (let ((gnus-level-default-subscribed 1))
402 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
403 (gnus-group-set-parameter
404 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
406 (defun gnus-agent-send-mail ()
408 (funcall gnus-agent-send-mail-function)
409 (goto-char (point-min))
411 (concat "^" (regexp-quote mail-header-separator) "\n"))
413 (gnus-agent-insert-meta-information 'mail)
414 (gnus-request-accept-article "nndraft:queue" nil t t)))
416 (defun gnus-agent-insert-meta-information (type &optional method)
417 "Insert meta-information into the message that says how it's to be posted.
418 TYPE can be either `mail' or `news'. If the latter, then METHOD can
421 (message-remove-header gnus-agent-meta-information-header)
422 (goto-char (point-min))
423 (insert gnus-agent-meta-information-header ": "
424 (symbol-name type) " " (format "%S" method)
427 (while (search-backward "\n" nil t)
428 (replace-match "\\n" t t))))
430 (defun gnus-agent-restore-gcc ()
431 "Restore GCC field from saved header."
433 (goto-char (point-min))
434 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
435 (replace-match "Gcc:" 'fixedcase))))
437 (defun gnus-agent-any-covered-gcc ()
439 (message-narrow-to-headers)
440 (let* ((gcc (mail-fetch-field "gcc" nil t))
442 (mapcar 'gnus-inews-group-method
443 (message-unquote-tokens
444 (message-tokenize-header
447 (while (and (not covered) methods)
449 (member (car methods) gnus-agent-covered-methods)
450 methods (cdr methods)))
453 (defun gnus-agent-possibly-save-gcc ()
454 "Save GCC if Gnus is unplugged."
455 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
457 (goto-char (point-min))
458 (let ((case-fold-search t))
459 (while (re-search-forward "^gcc:" nil t)
460 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
462 (defun gnus-agent-possibly-do-gcc ()
463 "Do GCC if Gnus is plugged."
464 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
465 (gnus-inews-do-gcc)))
468 ;;; Group mode commands
471 (defun gnus-agent-fetch-groups (n)
472 "Put all new articles in the current groups into the Agent."
475 (error "Groups can't be fetched when Gnus is unplugged"))
476 (gnus-group-iterate n 'gnus-agent-fetch-group))
478 (defun gnus-agent-fetch-group (group)
479 "Put all new articles in GROUP into the Agent."
480 (interactive (list (gnus-group-group-name)))
481 (let ((state gnus-plugged))
485 (gnus-agent-toggle-plugged gnus-plugged)
487 (error "No group on the current line"))
488 (let ((gnus-command-method (gnus-find-method-for-group group)))
489 (gnus-agent-with-fetch
490 (gnus-agent-fetch-group-1 group gnus-command-method)
491 (gnus-message 5 "Fetching %s...done" group)))))
492 (when (and (not state)
494 (gnus-agent-toggle-plugged gnus-plugged)))))
496 (defun gnus-agent-add-group (category arg)
497 "Add the current group to an agent category."
503 (mapcar (lambda (cat) (list (symbol-name (car cat))))
507 (let ((cat (assq category gnus-category-alist))
509 (gnus-group-iterate arg
511 (when (cadddr (setq c (gnus-group-category group)))
512 (setf (cadddr c) (delete group (cadddr c))))
513 (push group groups)))
514 (setf (cadddr cat) (nconc (cadddr cat) groups))
515 (gnus-category-write)))
517 (defun gnus-agent-remove-group (arg)
518 "Remove the current group from its agent category, if any."
521 (gnus-group-iterate arg
523 (when (cadddr (setq c (gnus-group-category group)))
524 (setf (cadddr c) (delete group (cadddr c))))))
525 (gnus-category-write)))
527 (defun gnus-agent-synchronize-flags ()
528 "Synchronize unplugged flags with servers."
531 (dolist (gnus-command-method gnus-agent-covered-methods)
532 (when (file-exists-p (gnus-agent-lib-file "flags"))
533 (gnus-agent-synchronize-flags-server gnus-command-method)))))
535 (defun gnus-agent-possibly-synchronize-flags ()
536 "Synchronize flags according to `gnus-agent-synchronize-flags'."
539 (dolist (gnus-command-method gnus-agent-covered-methods)
540 (when (file-exists-p (gnus-agent-lib-file "flags"))
541 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
543 (defun gnus-agent-synchronize-flags-server (method)
544 "Synchronize flags set when unplugged for server."
545 (let ((gnus-command-method method))
546 (when (file-exists-p (gnus-agent-lib-file "flags"))
547 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
549 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
550 (if (null (gnus-check-server gnus-command-method))
551 (message "Couldn't open server %s" (nth 1 gnus-command-method))
553 (if (null (eval (read (current-buffer))))
554 (progn (forward-line)
556 (write-file (gnus-agent-lib-file "flags"))
557 (error "Couldn't set flags from file %s"
558 (gnus-agent-lib-file "flags"))))
559 (delete-file (gnus-agent-lib-file "flags")))
562 (defun gnus-agent-possibly-synchronize-flags-server (method)
563 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
564 (when (or (and gnus-agent-synchronize-flags
565 (not (eq gnus-agent-synchronize-flags 'ask)))
566 (and (eq gnus-agent-synchronize-flags 'ask)
567 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
569 (gnus-agent-synchronize-flags-server method)))
572 ;;; Server mode commands
575 (defun gnus-agent-add-server (server)
576 "Enroll SERVER in the agent program."
577 (interactive (list (gnus-server-server-name)))
579 (error "No server on the current line"))
580 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
581 (when (member method gnus-agent-covered-methods)
582 (error "Server already in the agent program"))
583 (push method gnus-agent-covered-methods)
584 (gnus-server-update-server server)
585 (gnus-agent-write-servers)
586 (message "Entered %s into the Agent" server)))
588 (defun gnus-agent-remove-server (server)
589 "Remove SERVER from the agent program."
590 (interactive (list (gnus-server-server-name)))
592 (error "No server on the current line"))
593 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
594 (unless (member method gnus-agent-covered-methods)
595 (error "Server not in the agent program"))
596 (setq gnus-agent-covered-methods
597 (delete method gnus-agent-covered-methods))
598 (gnus-server-update-server server)
599 (gnus-agent-write-servers)
600 (message "Removed %s from the agent" server)))
602 (defun gnus-agent-read-servers ()
603 "Read the alist of covered servers."
604 (setq gnus-agent-covered-methods
605 (gnus-agent-read-file
606 (nnheader-concat gnus-agent-directory "lib/servers"))))
608 (defun gnus-agent-write-servers ()
609 "Write the alist of covered servers."
610 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
611 (let ((coding-system-for-write nnheader-file-coding-system)
612 (output-coding-system nnheader-file-coding-system)
613 (file-name-coding-system nnmail-pathname-coding-system)
614 (pathname-coding-system nnmail-pathname-coding-system))
615 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
616 (prin1 gnus-agent-covered-methods (current-buffer)))))
622 (defun gnus-agent-mark-article (n &optional unmark)
623 "Mark the next N articles as downloadable.
624 If N is negative, mark backward instead. If UNMARK is non-nil, remove
625 the mark instead. The difference between N and the actual number of
626 articles marked is returned."
628 (let ((backward (< n 0))
633 (gnus-summary-set-agent-mark
634 (gnus-summary-article-number) unmark)
635 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
638 (gnus-message 7 "No more articles"))
639 (gnus-summary-recenter)
640 (gnus-summary-position-point)
643 (defun gnus-agent-unmark-article (n)
644 "Remove the downloadable mark from the next N articles.
645 If N is negative, unmark backward instead. The difference between N and
646 the actual number of articles unmarked is returned."
648 (gnus-agent-mark-article n t))
650 (defun gnus-agent-toggle-mark (n)
651 "Toggle the downloadable mark from the next N articles.
652 If N is negative, toggle backward instead. The difference between N and
653 the actual number of articles toggled is returned."
655 (gnus-agent-mark-article n 'toggle))
657 (defun gnus-summary-set-agent-mark (article &optional unmark)
658 "Mark ARTICLE as downloadable."
659 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
660 (memq article gnus-newsgroup-downloadable)
664 (setq gnus-newsgroup-downloadable
665 (delq article gnus-newsgroup-downloadable))
666 (push article gnus-newsgroup-undownloaded))
667 (setq gnus-newsgroup-undownloaded
668 (delq article gnus-newsgroup-undownloaded))
669 (push article gnus-newsgroup-downloadable))
670 (gnus-summary-update-mark
671 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
674 (defun gnus-agent-get-undownloaded-list ()
675 "Mark all unfetched articles as read."
676 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
677 (when (and (not gnus-plugged)
678 (gnus-agent-method-p gnus-command-method))
679 (gnus-agent-load-alist gnus-newsgroup-name)
680 ;; First mark all undownloaded articles as undownloaded.
681 (dolist (article (mapcar (lambda (header) (mail-header-number header))
682 gnus-newsgroup-headers))
683 (unless (or (cdr (assq article gnus-agent-article-alist))
684 (memq article gnus-newsgroup-downloadable)
685 (memq article gnus-newsgroup-cached))
686 (push article gnus-newsgroup-undownloaded)))
687 ;; Then mark downloaded downloadable as not-downloadable,
688 ;; if you get my drift.
689 (dolist (article gnus-newsgroup-downloadable)
690 (when (cdr (assq article gnus-agent-article-alist))
691 (setq gnus-newsgroup-downloadable
692 (delq article gnus-newsgroup-downloadable)))))))
694 (defun gnus-agent-catchup ()
695 "Mark all undownloaded articles as read."
698 (while gnus-newsgroup-undownloaded
699 (gnus-summary-mark-article
700 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
701 (gnus-summary-position-point))
703 (defun gnus-agent-summary-fetch-group ()
704 "Fetch the downloadable articles in the group."
706 (let ((articles gnus-newsgroup-downloadable)
707 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
708 (state gnus-plugged))
712 (gnus-agent-toggle-plugged t))
714 (error "No articles to download"))
715 (gnus-agent-with-fetch
716 (gnus-agent-fetch-articles gnus-newsgroup-name articles))
718 (dolist (article articles)
719 (setq gnus-newsgroup-downloadable
720 (delq article gnus-newsgroup-downloadable))
721 (gnus-summary-mark-article article gnus-unread-mark))))
722 (when (and (not state)
724 (gnus-agent-toggle-plugged nil)))))
727 ;;; Internal functions
730 (defun gnus-agent-save-active (method)
731 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
733 (defun gnus-agent-save-active-1 (method function)
734 (when (gnus-agent-method-p method)
735 (let* ((gnus-command-method method)
736 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
737 (file (gnus-agent-lib-file "active")))
738 (funcall function nil new)
739 (gnus-agent-write-active file new)
741 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
744 (defun gnus-agent-write-active (file new)
745 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
746 (file (gnus-agent-lib-file "active"))
748 (when (file-exists-p file)
750 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
752 (gnus-active-to-gnus-format nil orig))
755 (when (and sym (boundp sym))
756 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
757 (setq elem (symbol-value osym)))
758 (setcdr elem (cdr (symbol-value sym)))
759 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
761 (gnus-make-directory (file-name-directory file))
762 ;; The hashtable contains real names of groups, no more prefix
763 ;; removing, so set `full' to `t'.
764 (gnus-write-active-file file orig t)))
766 (defun gnus-agent-save-groups (method)
767 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
769 (defun gnus-agent-save-group-info (method group active)
770 (when (gnus-agent-method-p method)
771 (let* ((gnus-command-method method)
772 (coding-system-for-write nnheader-file-coding-system)
773 (output-coding-system nnheader-file-coding-system)
774 (file-name-coding-system nnmail-pathname-coding-system)
775 (pathname-coding-system nnmail-pathname-coding-system)
776 (file (gnus-agent-lib-file "active"))
778 (gnus-make-directory (file-name-directory file))
780 (when (file-exists-p file)
781 (nnheader-insert-file-contents file))
782 (goto-char (point-min))
783 (when (re-search-forward
784 (concat "^" (regexp-quote group) " ") nil t)
786 (read (current-buffer)) ;; max
787 (setq oactive-min (read (current-buffer)))) ;; min
789 (insert (format "%S %d %d y\n" (intern group)
791 (or oactive-min (car active))))
792 (goto-char (point-max))
793 (while (search-backward "\\." nil t)
796 (defun gnus-agent-group-path (group)
797 "Translate GROUP into a path."
798 (if nnmail-use-long-file-names
799 (gnus-group-real-name group)
800 (nnheader-translate-file-chars
801 (nnheader-replace-chars-in-string
802 (nnheader-replace-duplicate-chars-in-string
803 (nnheader-replace-chars-in-string
804 (gnus-group-real-name group)
811 (defun gnus-agent-method-p (method)
812 "Say whether METHOD is covered by the agent."
813 (member method gnus-agent-covered-methods))
815 (defun gnus-agent-get-function (method)
816 (if (and (not gnus-plugged)
817 (gnus-agent-method-p method))
823 ;;; History functions
825 (defun gnus-agent-history-buffer ()
826 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
828 (defun gnus-agent-open-history ()
830 (push (cons (gnus-agent-method)
831 (set-buffer (gnus-get-buffer-create
832 (format " *Gnus agent %s history*"
833 (gnus-agent-method)))))
834 gnus-agent-history-buffers)
837 (let ((file (gnus-agent-lib-file "history")))
838 (when (file-exists-p file)
839 (nnheader-insert-file-contents file))
840 (set (make-local-variable 'gnus-agent-file-name) file))))
842 (defun gnus-agent-save-history ()
844 (set-buffer gnus-agent-current-history)
845 (gnus-make-directory (file-name-directory gnus-agent-file-name))
846 (write-region-as-coding-system
847 gnus-agent-file-coding-system
848 (1+ (point-min)) (point-max) gnus-agent-file-name nil 'silent)))
850 (defun gnus-agent-close-history ()
851 (when (gnus-buffer-live-p gnus-agent-current-history)
852 (kill-buffer gnus-agent-current-history)
853 (setq gnus-agent-history-buffers
854 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
855 gnus-agent-history-buffers))))
857 (defun gnus-agent-enter-history (id group-arts date)
859 (set-buffer gnus-agent-current-history)
860 (goto-char (point-max))
862 (insert id "\t" (number-to-string date) "\t")
864 (insert (format "%S" (intern (caar group-arts)))
865 " " (number-to-string (cdr (pop group-arts)))
868 (while (search-backward "\\." p t)
871 (defun gnus-agent-article-in-history-p (id)
873 (set-buffer (gnus-agent-history-buffer))
874 (goto-char (point-min))
875 (search-forward (concat "\n" id "\t") nil t)))
877 (defun gnus-agent-history-path (id)
879 (set-buffer (gnus-agent-history-buffer))
880 (goto-char (point-min))
881 (when (search-forward (concat "\n" id "\t") nil t)
882 (let ((method (gnus-agent-method)))
884 (while (not (numberp (setq group (read (current-buffer)))))
885 (push (concat method "/" group) paths))
886 (nreverse paths))))))
892 (defun gnus-agent-fetch-articles (group articles)
893 "Fetch ARTICLES from GROUP and put them into the Agent."
895 ;; Prune off articles that we have already fetched.
897 (cdr (assq (car articles) gnus-agent-article-alist)))
899 (let ((arts articles))
901 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
902 (setcdr arts (cddr arts))
903 (setq arts (cdr arts)))))
906 (gnus-agent-directory)
907 (gnus-agent-group-path group) "/"))
908 (date (time-to-days (current-time)))
911 (gnus-make-directory dir)
912 (gnus-message 7 "Fetching articles for %s..." group)
913 ;; Fetch the articles from the backend.
914 (if (gnus-check-backend-function 'retrieve-articles group)
915 (setq pos (gnus-retrieve-articles articles group))
918 (while (setq article (pop articles))
920 (gnus-backlog-request-article group article
922 (gnus-request-article article group))
923 (goto-char (point-max))
924 (push (cons article (point)) pos)
925 (insert-buffer-substring nntp-server-buffer)))
926 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
927 (setq pos (nreverse pos)))))
928 ;; Then save these articles into the Agent.
930 (set-buffer nntp-server-buffer)
932 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
933 (goto-char (point-min))
934 (when (search-forward "\n\n" nil t)
935 (when (search-backward "\nXrefs: " nil t)
936 ;; Handle crossposting.
937 (skip-chars-forward "^ ")
938 (skip-chars-forward " ")
940 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
941 (push (cons (buffer-substring (match-beginning 1)
943 (buffer-substring (match-beginning 2)
946 (goto-char (match-end 0)))
947 (gnus-agent-crosspost crosses (caar pos))))
948 (goto-char (point-min))
949 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
950 (setq id "No-Message-ID-in-article")
951 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
952 (write-region-as-coding-system
953 gnus-agent-file-coding-system
954 (point-min) (point-max)
955 (concat dir (number-to-string (caar pos))) nil 'silent)
956 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
958 (gnus-agent-enter-history
959 id (or crosses (list (cons group (caar pos)))) date)
962 (gnus-agent-save-alist group)))))
964 (defun gnus-agent-crosspost (crosses article)
965 (let (gnus-agent-article-alist group alist beg end)
967 (set-buffer gnus-agent-overview-buffer)
968 (when (nnheader-find-nov-line article)
971 (setq end (progn (forward-line 1) (point)))))
973 (setq group (caar crosses))
974 (unless (setq alist (assoc group gnus-agent-group-alist))
975 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
976 gnus-agent-group-alist))
977 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
979 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
981 (when (= (point-max) (point-min))
982 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
984 (nnheader-insert-file-contents
985 (gnus-agent-article-name ".overview" group))))
986 (nnheader-find-nov-line (string-to-number (cdar crosses)))
987 (insert (string-to-number (cdar crosses)))
988 (insert-buffer-substring gnus-agent-overview-buffer beg end))
991 (defun gnus-agent-flush-cache ()
993 (while gnus-agent-buffer-alist
994 (set-buffer (cdar gnus-agent-buffer-alist))
995 (write-region-as-coding-system
996 gnus-agent-file-coding-system
997 (point-min) (point-max)
998 (gnus-agent-article-name ".overview"
999 (caar gnus-agent-buffer-alist))
1001 (pop gnus-agent-buffer-alist))
1002 (while gnus-agent-group-alist
1003 (with-temp-file (caar gnus-agent-group-alist)
1004 (princ (cdar gnus-agent-group-alist))
1006 (pop gnus-agent-group-alist))))
1008 (defun gnus-agent-fetch-headers (group &optional force)
1009 (let* ((articles (gnus-list-of-unread-articles group))
1010 (len (length articles))
1011 (gnus-decode-encoded-word-function 'identity)
1012 (file (gnus-agent-article-name ".overview" group))
1014 ;; Check the number of articles is not too large.
1015 (when (and (integerp gnus-agent-large-newsgroup)
1016 (< 0 gnus-agent-large-newsgroup))
1017 (and (< 0 (setq i (- len gnus-agent-large-newsgroup)))
1018 (setq articles (nthcdr i articles))))
1019 ;; add article with marks to list of article headers we want to fetch.
1020 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1021 (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
1023 (setq articles (sort articles '<))
1024 ;; Remove known articles.
1025 (when (gnus-agent-load-alist group)
1026 (setq articles (gnus-sorted-intersection
1028 (gnus-uncompress-range
1029 (cons (1+ (caar (last gnus-agent-article-alist)))
1030 (cdr (gnus-active group)))))))
1032 (gnus-make-directory (nnheader-translate-file-chars
1033 (file-name-directory file) t))
1035 (gnus-message 7 "Fetching headers for %s..." group)
1037 (set-buffer nntp-server-buffer)
1038 (unless (eq 'nov (gnus-retrieve-headers articles group))
1039 (nnvirtual-convert-headers))
1040 ;; Save these headers for later processing.
1041 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1042 (when (file-exists-p file)
1043 (gnus-agent-braid-nov group articles file))
1044 (write-region-as-coding-system
1045 gnus-agent-file-coding-system
1046 (point-min) (point-max) file nil 'silent)
1047 (gnus-agent-save-alist group articles nil)
1048 (gnus-agent-enter-history
1049 "last-header-fetched-for-session"
1050 (list (cons group (nth (- (length articles) 1) articles)))
1051 (time-to-days (current-time)))
1054 (defsubst gnus-agent-copy-nov-line (article)
1056 (set-buffer gnus-agent-overview-buffer)
1058 (if (eq article (read (current-buffer)))
1059 (setq e (progn (forward-line 1) (point)))
1063 (set-buffer nntp-server-buffer)
1064 (insert-buffer-substring gnus-agent-overview-buffer b e)))
1066 (defun gnus-agent-braid-nov (group articles file)
1067 (set-buffer gnus-agent-overview-buffer)
1068 (goto-char (point-min))
1069 (set-buffer nntp-server-buffer)
1071 (nnheader-insert-file-contents file)
1072 (goto-char (point-max))
1073 (if (or (= (point-min) (point-max))
1076 (< (read (current-buffer)) (car articles))))
1077 ;; We have only headers that are after the older headers,
1078 ;; so we just append them.
1080 (goto-char (point-max))
1081 (insert-buffer-substring gnus-agent-overview-buffer))
1082 ;; We do it the hard way.
1083 (nnheader-find-nov-line (car articles))
1084 (gnus-agent-copy-nov-line (car articles))
1086 (while (and articles
1088 (while (and (not (eobp))
1089 (< (read (current-buffer)) (car articles)))
1093 (gnus-agent-copy-nov-line (car articles))
1094 (setq articles (cdr articles))))
1097 (set-buffer gnus-agent-overview-buffer)
1100 (set-buffer nntp-server-buffer)
1101 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1103 (defun gnus-agent-load-alist (group &optional dir)
1104 "Load the article-state alist for GROUP."
1105 (setq gnus-agent-article-alist
1106 (gnus-agent-read-file
1108 (expand-file-name ".agentview" dir)
1109 (gnus-agent-article-name ".agentview" group)))))
1111 (defun gnus-agent-save-alist (group &optional articles state dir)
1112 "Save the article-state alist for GROUP."
1113 (let ((file-name-coding-system nnmail-pathname-coding-system)
1114 (pathname-coding-system nnmail-pathname-coding-system)
1115 print-level print-length)
1116 (with-temp-file (if dir
1117 (expand-file-name ".agentview" dir)
1118 (gnus-agent-article-name ".agentview" group))
1119 (princ (setq gnus-agent-article-alist
1120 (nconc gnus-agent-article-alist
1121 (mapcar (lambda (article) (cons article state))
1126 (defun gnus-agent-article-name (article group)
1127 (expand-file-name (if (stringp article) article (string-to-number article))
1128 (file-name-as-directory
1129 (expand-file-name (gnus-agent-group-path group)
1130 (gnus-agent-directory)))))
1133 (defun gnus-agent-batch-fetch ()
1134 "Start Gnus and fetch session."
1137 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1138 (gnus-agent-fetch-session))
1141 (defun gnus-agent-fetch-session ()
1142 "Fetch all articles and headers that are eligible for fetching."
1144 (unless gnus-agent-covered-methods
1145 (error "No servers are covered by the Gnus agent"))
1146 (unless gnus-plugged
1147 (error "Can't fetch articles while Gnus is unplugged"))
1148 (let ((methods gnus-agent-covered-methods)
1149 groups group gnus-command-method)
1154 (setq gnus-command-method (car methods))
1155 (when (or (gnus-server-opened gnus-command-method)
1156 (gnus-open-server gnus-command-method))
1157 (setq groups (gnus-groups-from-server (car methods)))
1158 (gnus-agent-with-fetch
1159 (while (setq group (pop groups))
1160 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1161 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1163 (unless (funcall gnus-agent-confirmation-function
1164 (format "Error (%s). Continue? " err))
1165 (error "Cannot fetch articles into the Gnus agent")))
1167 (unless (funcall gnus-agent-confirmation-function
1168 (format "Quit fetching session (%s). Continue? "
1170 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1172 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1174 (defun gnus-agent-fetch-group-1 (group method)
1176 (let ((gnus-command-method method)
1177 (gnus-newsgroup-name group)
1178 gnus-newsgroup-dependencies gnus-newsgroup-headers
1179 gnus-newsgroup-scored gnus-headers gnus-score
1180 gnus-use-cache articles arts
1181 category predicate info marks score-param
1182 (gnus-summary-expunge-below gnus-summary-expunge-below)
1183 (gnus-summary-mark-below gnus-summary-mark-below)
1184 (gnus-orphan-score gnus-orphan-score)
1185 ;; Maybe some other gnus-summary local variables should also
1188 (unless (gnus-check-group group)
1189 (error "Can't open server for %s" group))
1191 (when (and (or (gnus-active group)
1192 (gnus-activate-group group))
1193 (setq articles (gnus-agent-fetch-headers group))
1194 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1195 ;; Parse them and see which articles we want to fetch.
1196 (setq gnus-newsgroup-dependencies
1197 (make-vector (length articles) 0))
1198 (setq gnus-newsgroup-headers
1199 (gnus-get-newsgroup-headers-xover articles nil nil
1201 ;; `gnus-agent-overview-buffer' may be killed for
1202 ;; timeout reason. If so, recreate it.
1203 (gnus-agent-create-buffer)))
1204 (setq category (gnus-group-category group))
1207 (or (gnus-group-find-parameter group 'agent-predicate t)
1209 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1210 ;; Simple implementation
1211 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1214 (or (gnus-group-get-parameter group 'agent-score t)
1216 ;; Translate score-param into real one
1219 ((eq score-param 'file)
1220 (setq score-param (gnus-all-score-files group)))
1221 ((stringp (car score-param)))
1223 (setq score-param (list (list score-param)))))
1225 (gnus-score-headers score-param))
1226 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1228 (or (cdr (assq (mail-header-number gnus-headers)
1229 gnus-newsgroup-scored))
1230 gnus-summary-default-score))
1231 (when (funcall predicate)
1232 (push (mail-header-number gnus-headers)
1234 ;; Fetch the articles.
1236 (gnus-agent-fetch-articles group arts)))
1237 ;; Perhaps we have some additional articles to fetch.
1238 (setq arts (assq 'download (gnus-info-marks
1239 (setq info (gnus-get-info group)))))
1241 (gnus-agent-fetch-articles
1242 group (gnus-uncompress-range (cdr arts)))
1243 (setq marks (delq arts (gnus-info-marks info)))
1244 (gnus-info-set-marks info marks)
1246 (concat "(gnus-group-set-info '"
1247 (gnus-prin1-to-string info)
1251 ;;; Agent Category Mode
1254 (defvar gnus-category-mode-hook nil
1255 "Hook run in `gnus-category-mode' buffers.")
1257 (defvar gnus-category-line-format " %(%20c%): %g\n"
1258 "Format of category lines.")
1260 (defvar gnus-category-mode-line-format "Gnus: %%b"
1261 "The format specification for the category mode line.")
1263 (defvar gnus-agent-short-article 100
1264 "Articles that have fewer lines than this are short.")
1266 (defvar gnus-agent-long-article 200
1267 "Articles that have more lines than this are long.")
1269 (defvar gnus-agent-low-score 0
1270 "Articles that have a score lower than this have a low score.")
1272 (defvar gnus-agent-high-score 0
1273 "Articles that have a score higher than this have a high score.")
1276 ;;; Internal variables.
1278 (defvar gnus-category-buffer "*Agent Category*")
1280 (defvar gnus-category-line-format-alist
1281 `((?c gnus-tmp-name ?s)
1282 (?g gnus-tmp-groups ?d)))
1284 (defvar gnus-category-mode-line-format-alist
1285 `((?u user-defined ?s)))
1287 (defvar gnus-category-line-format-spec nil)
1288 (defvar gnus-category-mode-line-format-spec nil)
1290 (defvar gnus-category-mode-map nil)
1291 (put 'gnus-category-mode 'mode-class 'special)
1293 (unless gnus-category-mode-map
1294 (setq gnus-category-mode-map (make-sparse-keymap))
1295 (suppress-keymap gnus-category-mode-map)
1297 (gnus-define-keys gnus-category-mode-map
1298 "q" gnus-category-exit
1299 "k" gnus-category-kill
1300 "c" gnus-category-copy
1301 "a" gnus-category-add
1302 "p" gnus-category-edit-predicate
1303 "g" gnus-category-edit-groups
1304 "s" gnus-category-edit-score
1305 "l" gnus-category-list
1307 "\C-c\C-i" gnus-info-find-node
1308 "\C-c\C-b" gnus-bug))
1310 (defvar gnus-category-menu-hook nil
1311 "*Hook run after the creation of the menu.")
1313 (defun gnus-category-make-menu-bar ()
1314 (gnus-turn-off-edit-menu 'category)
1315 (unless (boundp 'gnus-category-menu)
1317 gnus-category-menu gnus-category-mode-map ""
1319 ["Add" gnus-category-add t]
1320 ["Kill" gnus-category-kill t]
1321 ["Copy" gnus-category-copy t]
1322 ["Edit predicate" gnus-category-edit-predicate t]
1323 ["Edit score" gnus-category-edit-score t]
1324 ["Edit groups" gnus-category-edit-groups t]
1325 ["Exit" gnus-category-exit t]))
1327 (gnus-run-hooks 'gnus-category-menu-hook)))
1329 (defun gnus-category-mode ()
1330 "Major mode for listing and editing agent categories.
1332 All normal editing commands are switched off.
1333 \\<gnus-category-mode-map>
1334 For more in-depth information on this mode, read the manual
1335 (`\\[gnus-info-find-node]').
1337 The following commands are available:
1339 \\{gnus-category-mode-map}"
1341 (when (gnus-visual-p 'category-menu 'menu)
1342 (gnus-category-make-menu-bar))
1343 (kill-all-local-variables)
1344 (gnus-simplify-mode-line)
1345 (setq major-mode 'gnus-category-mode)
1346 (setq mode-name "Category")
1347 (gnus-set-default-directory)
1348 (setq mode-line-process nil)
1349 (use-local-map gnus-category-mode-map)
1350 (buffer-disable-undo)
1351 (setq truncate-lines t)
1352 (setq buffer-read-only t)
1353 (gnus-run-hooks 'gnus-category-mode-hook))
1355 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1357 (defun gnus-category-insert-line (category)
1358 (let* ((gnus-tmp-name (car category))
1359 (gnus-tmp-groups (length (cadddr category))))
1361 (gnus-add-text-properties
1365 (eval gnus-category-line-format-spec))
1366 (list 'gnus-category gnus-tmp-name))))
1368 (defun gnus-enter-category-buffer ()
1369 "Go to the Category buffer."
1371 (gnus-category-setup-buffer)
1372 (gnus-configure-windows 'category)
1373 (gnus-category-prepare))
1375 (defun gnus-category-setup-buffer ()
1376 (unless (get-buffer gnus-category-buffer)
1378 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1379 (gnus-category-mode))))
1381 (defun gnus-category-prepare ()
1382 (gnus-set-format 'category-mode)
1383 (gnus-set-format 'category t)
1384 (let ((alist gnus-category-alist)
1385 (buffer-read-only nil))
1388 (gnus-category-insert-line (pop alist)))
1389 (goto-char (point-min))
1390 (gnus-category-position-point)))
1392 (defun gnus-category-name ()
1393 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1394 (error "No category on the current line")))
1396 (defun gnus-category-read ()
1397 "Read the category alist."
1398 (setq gnus-category-alist
1399 (or (gnus-agent-read-file
1400 (nnheader-concat gnus-agent-directory "lib/categories"))
1401 (list (list 'default 'short nil nil)))))
1403 (defun gnus-category-write ()
1404 "Write the category alist."
1405 (setq gnus-category-predicate-cache nil
1406 gnus-category-group-cache nil)
1407 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1408 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1409 (prin1 gnus-category-alist (current-buffer))))
1411 (defun gnus-category-edit-predicate (category)
1412 "Edit the predicate for CATEGORY."
1413 (interactive (list (gnus-category-name)))
1414 (let ((info (assq category gnus-category-alist)))
1416 (cadr info) (format "Editing the predicate for category %s" category)
1417 `(lambda (predicate)
1418 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1419 (gnus-category-write)
1420 (gnus-category-list)))))
1422 (defun gnus-category-edit-score (category)
1423 "Edit the score expression for CATEGORY."
1424 (interactive (list (gnus-category-name)))
1425 (let ((info (assq category gnus-category-alist)))
1428 (format "Editing the score expression for category %s" category)
1430 (setcar (nthcdr 2 (assq ',category gnus-category-alist)) groups)
1431 (gnus-category-write)
1432 (gnus-category-list)))))
1434 (defun gnus-category-edit-groups (category)
1435 "Edit the group list for CATEGORY."
1436 (interactive (list (gnus-category-name)))
1437 (let ((info (assq category gnus-category-alist)))
1439 (cadddr info) (format "Editing the group list for category %s" category)
1441 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1442 (gnus-category-write)
1443 (gnus-category-list)))))
1445 (defun gnus-category-kill (category)
1446 "Kill the current category."
1447 (interactive (list (gnus-category-name)))
1448 (let ((info (assq category gnus-category-alist))
1449 (buffer-read-only nil))
1451 (setq gnus-category-alist (delq info gnus-category-alist))
1452 (gnus-category-write)))
1454 (defun gnus-category-copy (category to)
1455 "Copy the current category."
1456 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1457 (let ((info (assq category gnus-category-alist)))
1458 (push (list to (gnus-copy-sequence (cadr info))
1459 (gnus-copy-sequence (caddr info)) nil)
1460 gnus-category-alist)
1461 (gnus-category-write)
1462 (gnus-category-list)))
1464 (defun gnus-category-add (category)
1465 "Create a new category."
1466 (interactive "SCategory name: ")
1467 (when (assq category gnus-category-alist)
1468 (error "Category %s already exists" category))
1469 (push (list category 'false nil nil)
1470 gnus-category-alist)
1471 (gnus-category-write)
1472 (gnus-category-list))
1474 (defun gnus-category-list ()
1475 "List all categories."
1477 (gnus-category-prepare))
1479 (defun gnus-category-exit ()
1480 "Return to the group buffer."
1482 (kill-buffer (current-buffer))
1483 (gnus-configure-windows 'group t))
1485 ;; To avoid having 8-bit characters in the source file.
1486 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1488 (defvar gnus-category-predicate-alist
1489 '((spam . gnus-agent-spam-p)
1490 (short . gnus-agent-short-p)
1491 (long . gnus-agent-long-p)
1492 (low . gnus-agent-low-scored-p)
1493 (high . gnus-agent-high-scored-p)
1494 (true . gnus-agent-true)
1495 (false . gnus-agent-false))
1496 "Mapping from short score predicate symbols to predicate functions.")
1498 (defun gnus-agent-spam-p ()
1499 "Say whether an article is spam or not."
1500 (unless gnus-agent-spam-hashtb
1501 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1502 (if (not (equal (mail-header-references gnus-headers) ""))
1504 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1506 (gnus-gethash string gnus-agent-spam-hashtb)
1507 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1509 (defun gnus-agent-short-p ()
1510 "Say whether an article is short or not."
1511 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1513 (defun gnus-agent-long-p ()
1514 "Say whether an article is long or not."
1515 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1517 (defun gnus-agent-low-scored-p ()
1518 "Say whether an article has a low score or not."
1519 (< gnus-score gnus-agent-low-score))
1521 (defun gnus-agent-high-scored-p ()
1522 "Say whether an article has a high score or not."
1523 (> gnus-score gnus-agent-high-score))
1525 (defun gnus-category-make-function (cat)
1526 "Make a function from category CAT."
1527 (let ((func (gnus-category-make-function-1 cat)))
1528 (if (and (= (length func) 1)
1529 (symbolp (car func)))
1531 (gnus-byte-compile `(lambda () ,func)))))
1533 (defun gnus-agent-true ()
1537 (defun gnus-agent-false ()
1541 (defun gnus-category-make-function-1 (cat)
1542 "Make a function from category CAT."
1544 ;; Functions are just returned as is.
1546 (gnus-functionp cat))
1547 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1549 ;; More complex category.
1552 ((memq (car cat) '(& and))
1554 ((memq (car cat) '(| or))
1556 ((memq (car cat) gnus-category-not)
1558 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1560 (error "Unknown category type: %s" cat))))
1562 (defun gnus-get-predicate (predicate)
1563 "Return the predicate for CATEGORY."
1564 (or (cdr (assoc predicate gnus-category-predicate-cache))
1565 (cdar (push (cons predicate
1566 (gnus-category-make-function predicate))
1567 gnus-category-predicate-cache))))
1569 (defun gnus-group-category (group)
1570 "Return the category GROUP belongs to."
1571 (unless gnus-category-group-cache
1572 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1573 (let ((cs gnus-category-alist)
1575 (while (setq cat (pop cs))
1576 (setq groups (cadddr cat))
1578 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1579 (or (gnus-gethash group gnus-category-group-cache)
1580 (assq 'default gnus-category-alist)))
1582 (defun gnus-agent-expire ()
1583 "Expire all old articles."
1585 (let ((methods gnus-agent-covered-methods)
1586 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1587 gnus-command-method sym group articles
1588 history overview file histories elem art nov-file low info
1589 unreads marked article orig lowest highest)
1591 (setq overview (gnus-get-buffer-create " *expire overview*"))
1592 (while (setq gnus-command-method (pop methods))
1593 (when (file-exists-p (gnus-agent-lib-file "active"))
1595 (insert-file-contents-as-coding-system
1596 gnus-agent-file-coding-system (gnus-agent-lib-file "active"))
1597 (gnus-active-to-gnus-format
1599 (setq orig (gnus-make-hashtable
1600 (count-lines (point-min) (point-max))))))
1601 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1602 (gnus-agent-open-history)
1604 (setq gnus-agent-current-history
1605 (setq history (gnus-agent-history-buffer))))
1606 (goto-char (point-min))
1607 (when (> (buffer-size) 1)
1608 (goto-char (point-min))
1610 (skip-chars-forward "^\t")
1611 (if (let ((fetch-date (read (current-buffer))))
1612 (if (numberp fetch-date)
1614 ;; History file is corrupted.
1617 (format "File %s is corrupted!"
1618 (gnus-agent-lib-file "history")))
1622 ;; New article; we don't expire it.
1624 ;; Old article. Schedule it for possible nuking.
1626 (setq sym (let ((obarray expiry-hashtb) s)
1627 (setq s (read (current-buffer)))
1628 (if (stringp s) (intern s) s)))
1630 (set sym (cons (cons (read (current-buffer)) (point))
1631 (symbol-value sym)))
1632 (set sym (list (cons (read (current-buffer)) (point)))))
1633 (skip-chars-forward " "))
1635 ;; We now have all articles that can possibly be expired.
1638 (setq group (symbol-name sym)
1639 articles (sort (symbol-value sym) 'car-less-than-car)
1640 low (car (gnus-active group))
1641 info (gnus-get-info group)
1642 unreads (ignore-errors
1643 (gnus-list-of-unread-articles group))
1645 (gnus-uncompress-range
1646 (cdr (assq 'tick (gnus-info-marks info))))
1647 (gnus-uncompress-range
1648 (cdr (assq 'dormant (gnus-info-marks info))))
1649 (gnus-uncompress-range
1650 (cdr (assq 'save (gnus-info-marks info))))
1651 (gnus-uncompress-range
1652 (cdr (assq 'reply (gnus-info-marks info)))))
1653 nov-file (gnus-agent-article-name ".overview" group)
1656 (gnus-agent-load-alist group)
1657 (gnus-message 5 "Expiring articles in %s" group)
1658 (set-buffer overview)
1660 (when (file-exists-p nov-file)
1661 (nnheader-insert-file-contents nov-file))
1662 (goto-char (point-min))
1664 (while (setq elem (pop articles))
1665 (setq article (car elem))
1666 (when (or (null low)
1668 gnus-agent-expire-all
1669 (and (not (memq article unreads))
1670 (not (memq article marked))))
1671 ;; Find and nuke the NOV line.
1672 (while (and (not (eobp))
1674 (setq art (read (current-buffer)))))
1676 (if (and (numberp art)
1678 (gnus-agent-article-name
1679 (number-to-string art) group)))
1685 ;; Remove old NOV lines that have no articles.
1686 (gnus-delete-line)))
1691 ;; Nuke the article.
1692 (when (file-exists-p
1693 (setq file (gnus-agent-article-name
1694 (number-to-string article)
1697 ;; Schedule the history line for nuking.
1698 (push (cdr elem) histories)))
1699 (gnus-make-directory (file-name-directory nov-file))
1700 (write-region-as-coding-system
1701 gnus-agent-file-coding-system
1702 (point-min) (point-max) nov-file nil 'silent)
1703 ;; Delete the unwanted entries in the alist.
1704 (setq gnus-agent-article-alist
1705 (sort gnus-agent-article-alist 'car-less-than-car))
1706 (let* ((alist gnus-agent-article-alist)
1707 (prev (cons nil alist))
1711 (<= (caar alist) article))
1712 (if (or (not (cdar alist))
1714 (gnus-agent-article-name
1719 (push (caar alist) expired)
1720 (setcdr prev (setq alist (cdr alist))))
1722 alist (cdr alist))))
1723 (setq gnus-agent-article-alist (cdr first))
1724 (gnus-agent-save-alist group)
1725 ;; Mark all articles up to the first article
1726 ;; in `gnus-article-alist' as read.
1727 (when (and info (caar gnus-agent-article-alist))
1728 (setcar (nthcdr 2 info)
1731 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1732 ;; Maybe everything has been expired from `gnus-article-alist'
1733 ;; and so the above marking as read could not be conducted,
1734 ;; or there are expired article within the range of the alist.
1737 (or (not (caar gnus-agent-article-alist))
1739 (caar gnus-agent-article-alist))))
1740 (setcar (nthcdr 2 info)
1743 (nreverse expired))))
1745 (concat "(gnus-group-set-info '"
1746 (gnus-prin1-to-string info)
1749 (if (gnus-gethash group orig)
1750 (setcar (gnus-gethash group orig) lowest)
1751 (gnus-sethash group (cons lowest highest) orig))))
1753 (set-buffer history)
1754 (setq histories (nreverse (sort histories '<)))
1756 (goto-char (pop histories))
1758 (gnus-agent-save-history)
1759 (gnus-agent-close-history)
1760 (gnus-write-active-file (gnus-agent-lib-file "active") orig))
1761 (gnus-message 4 "Expiry...done")))))))
1764 (defun gnus-agent-batch ()
1766 (let ((init-file-user "")
1767 (gnus-always-read-dribble-file t))
1769 (gnus-group-send-drafts)
1770 (gnus-agent-fetch-session))
1772 (provide 'gnus-agent)
1774 ;;; gnus-agent.el ends here