1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
33 (if (featurep 'xemacs)
39 (autoload 'gnus-server-update-server "gnus-srvr"))
41 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
42 "Where the Gnus agent will store its files."
46 (defcustom gnus-agent-plugged-hook nil
47 "Hook run when plugging into the network."
51 (defcustom gnus-agent-unplugged-hook nil
52 "Hook run when unplugging from the network."
56 (defcustom gnus-agent-handle-level gnus-level-subscribed
57 "Groups on levels higher than this variable will be ignored by the Agent."
61 (defcustom gnus-agent-expire-days 7
62 "Read articles older than this will be expired."
66 (defcustom gnus-agent-expire-all nil
67 "If non-nil, also expire unread, ticked and dormant articles.
68 If nil, only read articles will be expired."
72 (defcustom gnus-agent-group-mode-hook nil
73 "Hook run in Agent group minor modes."
77 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
78 (when (featurep 'xemacs)
79 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
81 (defcustom gnus-agent-summary-mode-hook nil
82 "Hook run in Agent summary minor modes."
86 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
87 (when (featurep 'xemacs)
88 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
90 (defcustom gnus-agent-server-mode-hook nil
91 "Hook run in Agent summary minor modes."
95 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
96 (when (featurep 'xemacs)
97 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
99 (defcustom gnus-agent-confirmation-function 'y-or-n-p
100 "Function to confirm when error happens."
105 (defcustom gnus-agent-synchronize-flags 'ask
106 "Indicate if flags are synchronized when you plug in.
107 If this is `ask' the hook will query the user."
109 :type '(choice (const :tag "Always" t)
110 (const :tag "Never" nil)
111 (const :tag "Ask" ask))
114 ;;; Internal variables
116 (defvar gnus-agent-history-buffers nil)
117 (defvar gnus-agent-buffer-alist nil)
118 (defvar gnus-agent-article-alist nil)
119 (defvar gnus-agent-group-alist nil)
120 (defvar gnus-agent-covered-methods nil)
121 (defvar gnus-category-alist nil)
122 (defvar gnus-agent-current-history nil)
123 (defvar gnus-agent-overview-buffer nil)
124 (defvar gnus-category-predicate-cache nil)
125 (defvar gnus-category-group-cache nil)
126 (defvar gnus-agent-spam-hashtb nil)
127 (defvar gnus-agent-file-name nil)
128 (defvar gnus-agent-send-mail-function nil)
129 (defvar gnus-agent-file-coding-system 'raw-text)
132 (defvar gnus-headers)
139 (defun gnus-open-agent ()
141 (gnus-agent-read-servers)
143 (gnus-agent-create-buffer)
144 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
145 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
146 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
148 (defun gnus-agent-create-buffer ()
149 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
151 (setq gnus-agent-overview-buffer
152 (gnus-get-buffer-create " *Gnus agent overview*"))
153 (with-current-buffer gnus-agent-overview-buffer
154 (mm-enable-multibyte))
157 (gnus-add-shutdown 'gnus-close-agent 'gnus)
159 (defun gnus-close-agent ()
160 (setq gnus-agent-covered-methods nil
161 gnus-category-predicate-cache nil
162 gnus-category-group-cache nil
163 gnus-agent-spam-hashtb nil)
164 (gnus-kill-buffer gnus-agent-overview-buffer))
167 ;;; Utility functions
170 (defun gnus-agent-read-file (file)
171 "Load FILE and do a `read' there."
174 (nnheader-insert-file-contents file)
175 (goto-char (point-min))
176 (read (current-buffer)))))
178 (defsubst gnus-agent-method ()
179 (concat (symbol-name (car gnus-command-method)) "/"
180 (if (equal (cadr gnus-command-method) "")
182 (cadr gnus-command-method))))
184 (defsubst gnus-agent-directory ()
185 "Path of the Gnus agent directory."
186 (nnheader-concat gnus-agent-directory
187 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
189 (defun gnus-agent-lib-file (file)
190 "The full path of the Gnus agent library FILE."
191 (expand-file-name file
192 (file-name-as-directory
193 (expand-file-name "agent.lib" (gnus-agent-directory)))))
195 ;;; Fetching setup functions.
197 (defun gnus-agent-start-fetch ()
198 "Initialize data structures for efficient fetching."
199 (gnus-agent-open-history)
200 (setq gnus-agent-current-history (gnus-agent-history-buffer))
201 (gnus-agent-create-buffer))
203 (defun gnus-agent-stop-fetch ()
204 "Save all data structures and clean up."
205 (gnus-agent-save-history)
206 (gnus-agent-close-history)
207 (setq gnus-agent-spam-hashtb nil)
209 (set-buffer nntp-server-buffer)
212 (defmacro gnus-agent-with-fetch (&rest forms)
215 (let ((gnus-agent-fetching t))
216 (gnus-agent-start-fetch)
218 (gnus-agent-stop-fetch)))
220 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
221 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
227 (defvar gnus-agent-mode-hook nil
228 "Hook run when installing agent mode.")
230 (defvar gnus-agent-mode nil)
231 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
233 (defun gnus-agent-mode ()
234 "Minor mode for providing a agent support in Gnus buffers."
235 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
236 (symbol-name major-mode))
237 (match-string 1 (symbol-name major-mode))))
238 (mode (intern (format "gnus-agent-%s-mode" buffer))))
239 (set (make-local-variable 'gnus-agent-mode) t)
241 (set (make-local-variable mode) t)
243 (when (gnus-visual-p 'agent-menu 'menu)
244 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
245 (unless (assq 'gnus-agent-mode minor-mode-alist)
246 (push gnus-agent-mode-status minor-mode-alist))
247 (unless (assq mode minor-mode-map-alist)
248 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
250 minor-mode-map-alist))
251 (when (eq major-mode 'gnus-group-mode)
252 (gnus-agent-toggle-plugged gnus-plugged))
253 (gnus-run-hooks 'gnus-agent-mode-hook
254 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
256 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
257 (gnus-define-keys gnus-agent-group-mode-map
258 "Ju" gnus-agent-fetch-groups
259 "Jc" gnus-enter-category-buffer
260 "Jj" gnus-agent-toggle-plugged
261 "Js" gnus-agent-fetch-session
262 "JY" gnus-agent-synchronize-flags
263 "JS" gnus-group-send-drafts
264 "Ja" gnus-agent-add-group
265 "Jr" gnus-agent-remove-group)
267 (defun gnus-agent-group-make-menu-bar ()
268 (unless (boundp 'gnus-agent-group-menu)
270 gnus-agent-group-menu gnus-agent-group-mode-map ""
272 ["Toggle plugged" gnus-agent-toggle-plugged t]
273 ["List categories" gnus-enter-category-buffer t]
274 ["Send drafts" gnus-group-send-drafts gnus-plugged]
276 ["All" gnus-agent-fetch-session gnus-plugged]
277 ["Group" gnus-agent-fetch-group gnus-plugged])))))
279 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
280 (gnus-define-keys gnus-agent-summary-mode-map
281 "Jj" gnus-agent-toggle-plugged
282 "Ju" gnus-agent-summary-fetch-group
283 "J#" gnus-agent-mark-article
284 "J\M-#" gnus-agent-unmark-article
285 "@" gnus-agent-toggle-mark
286 "Jc" gnus-agent-catchup)
288 (defun gnus-agent-summary-make-menu-bar ()
289 (unless (boundp 'gnus-agent-summary-menu)
291 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
293 ["Toggle plugged" gnus-agent-toggle-plugged t]
294 ["Mark as downloadable" gnus-agent-mark-article t]
295 ["Unmark as downloadable" gnus-agent-unmark-article t]
296 ["Toggle mark" gnus-agent-toggle-mark t]
297 ["Fetch downloadable" gnus-aget-summary-fetch-group t]
298 ["Catchup undownloaded" gnus-agent-catchup t]))))
300 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
301 (gnus-define-keys gnus-agent-server-mode-map
302 "Jj" gnus-agent-toggle-plugged
303 "Ja" gnus-agent-add-server
304 "Jr" gnus-agent-remove-server)
306 (defun gnus-agent-server-make-menu-bar ()
307 (unless (boundp 'gnus-agent-server-menu)
309 gnus-agent-server-menu gnus-agent-server-mode-map ""
311 ["Toggle plugged" gnus-agent-toggle-plugged t]
312 ["Add" gnus-agent-add-server t]
313 ["Remove" gnus-agent-remove-server t]))))
315 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
316 (if (and (fboundp 'propertize)
317 (fboundp 'make-mode-line-mouse-map))
318 (propertize string 'local-map
319 (make-mode-line-mouse-map mouse-button mouse-func))
322 (defun gnus-agent-toggle-plugged (plugged)
323 "Toggle whether Gnus is unplugged or not."
324 (interactive (list (not gnus-plugged)))
327 (setq gnus-plugged plugged)
328 (gnus-agent-possibly-synchronize-flags)
329 (gnus-run-hooks 'gnus-agent-plugged-hook)
330 (setcar (cdr gnus-agent-mode-status)
331 (gnus-agent-make-mode-line-string " Plugged"
333 'gnus-agent-toggle-plugged)))
334 (gnus-agent-close-connections)
335 (setq gnus-plugged plugged)
336 (gnus-run-hooks 'gnus-agent-unplugged-hook)
337 (setcar (cdr gnus-agent-mode-status)
338 (gnus-agent-make-mode-line-string " Unplugged"
340 'gnus-agent-toggle-plugged)))
341 (set-buffer-modified-p t))
343 (defun gnus-agent-close-connections ()
344 "Close all methods covered by the Gnus agent."
345 (let ((methods gnus-agent-covered-methods))
347 (gnus-close-server (pop methods)))))
350 (defun gnus-unplugged ()
351 "Start Gnus unplugged."
353 (setq gnus-plugged nil)
357 (defun gnus-plugged ()
358 "Start Gnus plugged."
360 (setq gnus-plugged t)
364 (defun gnus-agentize ()
365 "Allow Gnus to be an offline newsreader.
366 The normal usage of this command is to put the following as the
367 last form in your `.gnus.el' file:
371 This will modify the `gnus-setup-news-hook', and
372 `message-send-mail-real-function' variables, and install the Gnus agent
373 minor mode in all Gnus buffers."
376 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
377 (unless gnus-agent-send-mail-function
378 (setq gnus-agent-send-mail-function (or
379 message-send-mail-real-function
380 message-send-mail-function)
381 message-send-mail-real-function 'gnus-agent-send-mail))
382 (unless gnus-agent-covered-methods
383 (setq gnus-agent-covered-methods (list gnus-select-method))))
385 (defun gnus-agent-queue-setup ()
386 "Make sure the queue group exists."
387 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
388 (gnus-request-create-group "queue" '(nndraft ""))
389 (let ((gnus-level-default-subscribed 1))
390 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
391 (gnus-group-set-parameter
392 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
394 (defun gnus-agent-send-mail ()
396 (funcall gnus-agent-send-mail-function)
397 (goto-char (point-min))
399 (concat "^" (regexp-quote mail-header-separator) "\n"))
401 (gnus-agent-insert-meta-information 'mail)
402 (gnus-request-accept-article "nndraft:queue" nil t t)))
404 (defun gnus-agent-insert-meta-information (type &optional method)
405 "Insert meta-information into the message that says how it's to be posted.
406 TYPE can be either `mail' or `news'. If the latter, then METHOD can
409 (message-remove-header gnus-agent-meta-information-header)
410 (goto-char (point-min))
411 (insert gnus-agent-meta-information-header ": "
412 (symbol-name type) " " (format "%S" method)
415 (while (search-backward "\n" nil t)
416 (replace-match "\\n" t t))))
418 (defun gnus-agent-restore-gcc ()
419 "Restore GCC field from saved header."
421 (goto-char (point-min))
422 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
423 (replace-match "Gcc:" 'fixedcase))))
425 (defun gnus-agent-any-covered-gcc ()
427 (message-narrow-to-headers)
428 (let* ((gcc (mail-fetch-field "gcc" nil t))
430 (mapcar 'gnus-inews-group-method
431 (message-unquote-tokens
432 (message-tokenize-header
435 (while (and (not covered) methods)
436 (setq covered (gnus-agent-method-p (car methods))
437 methods (cdr methods)))
440 (defun gnus-agent-possibly-save-gcc ()
441 "Save GCC if Gnus is unplugged."
442 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
444 (goto-char (point-min))
445 (let ((case-fold-search t))
446 (while (re-search-forward "^gcc:" nil t)
447 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
449 (defun gnus-agent-possibly-do-gcc ()
450 "Do GCC if Gnus is plugged."
451 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
452 (gnus-inews-do-gcc)))
455 ;;; Group mode commands
458 (defun gnus-agent-fetch-groups (n)
459 "Put all new articles in the current groups into the Agent."
462 (error "Groups can't be fetched when Gnus is unplugged"))
463 (gnus-group-iterate n 'gnus-agent-fetch-group))
465 (defun gnus-agent-fetch-group (group)
466 "Put all new articles in GROUP into the Agent."
467 (interactive (list (gnus-group-group-name)))
468 (let ((state gnus-plugged))
472 (error "No group on the current line"))
474 (gnus-agent-toggle-plugged gnus-plugged))
475 (let ((gnus-command-method (gnus-find-method-for-group group)))
476 (gnus-agent-with-fetch
477 (gnus-agent-fetch-group-1 group gnus-command-method)
478 (gnus-message 5 "Fetching %s...done" group))))
479 (when (and (not state)
481 (gnus-agent-toggle-plugged gnus-plugged)))))
483 (defun gnus-agent-add-group (category arg)
484 "Add the current group to an agent category."
490 (mapcar (lambda (cat) (list (symbol-name (car cat))))
494 (let ((cat (assq category gnus-category-alist))
496 (gnus-group-iterate arg
498 (when (cadddr (setq c (gnus-group-category group)))
499 (setf (cadddr c) (delete group (cadddr c))))
500 (push group groups)))
501 (setf (cadddr cat) (nconc (cadddr cat) groups))
502 (gnus-category-write)))
504 (defun gnus-agent-remove-group (arg)
505 "Remove the current group from its agent category, if any."
508 (gnus-group-iterate arg
510 (when (cadddr (setq c (gnus-group-category group)))
511 (setf (cadddr c) (delete group (cadddr c))))))
512 (gnus-category-write)))
514 (defun gnus-agent-synchronize-flags ()
515 "Synchronize unplugged flags with servers."
518 (dolist (gnus-command-method gnus-agent-covered-methods)
519 (when (file-exists-p (gnus-agent-lib-file "flags"))
520 (gnus-agent-synchronize-flags-server gnus-command-method)))))
522 (defun gnus-agent-possibly-synchronize-flags ()
523 "Synchronize flags according to `gnus-agent-synchronize-flags'."
526 (dolist (gnus-command-method gnus-agent-covered-methods)
527 (when (file-exists-p (gnus-agent-lib-file "flags"))
528 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
530 (defun gnus-agent-synchronize-flags-server (method)
531 "Synchronize flags set when unplugged for server."
532 (let ((gnus-command-method method))
533 (when (file-exists-p (gnus-agent-lib-file "flags"))
534 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
536 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
537 (if (null (gnus-check-server gnus-command-method))
538 (message "Couldn't open server %s" (nth 1 gnus-command-method))
540 (if (null (eval (read (current-buffer))))
541 (progn (forward-line)
543 (write-file (gnus-agent-lib-file "flags"))
544 (error "Couldn't set flags from file %s"
545 (gnus-agent-lib-file "flags"))))
546 (delete-file (gnus-agent-lib-file "flags")))
549 (defun gnus-agent-possibly-synchronize-flags-server (method)
550 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
551 (when (or (and gnus-agent-synchronize-flags
552 (not (eq gnus-agent-synchronize-flags 'ask)))
553 (and (eq gnus-agent-synchronize-flags 'ask)
554 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
556 (gnus-agent-synchronize-flags-server method)))
559 ;;; Server mode commands
562 (defun gnus-agent-add-server (server)
563 "Enroll SERVER in the agent program."
564 (interactive (list (gnus-server-server-name)))
566 (error "No server on the current line"))
567 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
568 (when (gnus-agent-method-p method)
569 (error "Server already in the agent program"))
570 (push method gnus-agent-covered-methods)
571 (gnus-server-update-server server)
572 (gnus-agent-write-servers)
573 (message "Entered %s into the Agent" server)))
575 (defun gnus-agent-remove-server (server)
576 "Remove SERVER from 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 (unless (gnus-agent-method-p method)
582 (error "Server not in the agent program"))
583 (setq gnus-agent-covered-methods
584 (delete method gnus-agent-covered-methods))
585 (gnus-server-update-server server)
586 (gnus-agent-write-servers)
587 (message "Removed %s from the agent" server)))
589 (defun gnus-agent-read-servers ()
590 "Read the alist of covered servers."
591 (setq gnus-agent-covered-methods
592 (gnus-agent-read-file
593 (nnheader-concat gnus-agent-directory "lib/servers"))))
595 (defun gnus-agent-write-servers ()
596 "Write the alist of covered servers."
597 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
598 (let ((coding-system-for-write nnheader-file-coding-system)
599 (file-name-coding-system nnmail-pathname-coding-system))
600 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
601 (prin1 gnus-agent-covered-methods (current-buffer)))))
607 (defun gnus-agent-mark-article (n &optional unmark)
608 "Mark the next N articles as downloadable.
609 If N is negative, mark backward instead. If UNMARK is non-nil, remove
610 the mark instead. The difference between N and the actual number of
611 articles marked is returned."
613 (let ((backward (< n 0))
618 (gnus-summary-set-agent-mark
619 (gnus-summary-article-number) unmark)
620 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
623 (gnus-message 7 "No more articles"))
624 (gnus-summary-recenter)
625 (gnus-summary-position-point)
628 (defun gnus-agent-unmark-article (n)
629 "Remove the downloadable mark from the next N articles.
630 If N is negative, unmark backward instead. The difference between N and
631 the actual number of articles unmarked is returned."
633 (gnus-agent-mark-article n t))
635 (defun gnus-agent-toggle-mark (n)
636 "Toggle the downloadable mark from the next N articles.
637 If N is negative, toggle backward instead. The difference between N and
638 the actual number of articles toggled is returned."
640 (gnus-agent-mark-article n 'toggle))
642 (defun gnus-summary-set-agent-mark (article &optional unmark)
643 "Mark ARTICLE as downloadable."
644 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
645 (memq article gnus-newsgroup-downloadable)
649 (setq gnus-newsgroup-downloadable
650 (delq article gnus-newsgroup-downloadable))
651 (push article gnus-newsgroup-undownloaded))
652 (setq gnus-newsgroup-undownloaded
653 (delq article gnus-newsgroup-undownloaded))
654 (push article gnus-newsgroup-downloadable))
655 (gnus-summary-update-mark
656 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
659 (defun gnus-agent-get-undownloaded-list ()
660 "Mark all unfetched articles as read."
661 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
662 (when (and (not gnus-plugged)
663 (gnus-agent-method-p gnus-command-method))
664 (gnus-agent-load-alist gnus-newsgroup-name)
665 ;; First mark all undownloaded articles as undownloaded.
666 (dolist (article (mapcar (lambda (header) (mail-header-number header))
667 gnus-newsgroup-headers))
668 (unless (or (cdr (assq article gnus-agent-article-alist))
669 (memq article gnus-newsgroup-downloadable)
670 (memq article gnus-newsgroup-cached))
671 (push article gnus-newsgroup-undownloaded)))
672 ;; Then mark downloaded downloadable as not-downloadable,
673 ;; if you get my drift.
674 (dolist (article gnus-newsgroup-downloadable)
675 (when (cdr (assq article gnus-agent-article-alist))
676 (setq gnus-newsgroup-downloadable
677 (delq article gnus-newsgroup-downloadable)))))))
679 (defun gnus-agent-catchup ()
680 "Mark all undownloaded articles as read."
683 (while gnus-newsgroup-undownloaded
684 (gnus-summary-mark-article
685 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
686 (gnus-summary-position-point))
688 (defun gnus-agent-summary-fetch-group ()
689 "Fetch the downloadable articles in the group."
691 (let ((articles gnus-newsgroup-downloadable)
692 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
693 (state gnus-plugged))
697 (gnus-agent-toggle-plugged t))
699 (error "No articles to download"))
700 (gnus-agent-with-fetch
701 (gnus-agent-fetch-articles gnus-newsgroup-name articles))
703 (dolist (article articles)
704 (setq gnus-newsgroup-downloadable
705 (delq article gnus-newsgroup-downloadable))
706 (gnus-summary-mark-article article gnus-unread-mark))))
707 (when (and (not state)
709 (gnus-agent-toggle-plugged nil)))))
712 ;;; Internal functions
715 (defun gnus-agent-save-active (method)
716 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
718 (defun gnus-agent-save-active-1 (method function)
719 (when (gnus-agent-method-p method)
720 (let* ((gnus-command-method method)
721 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
722 (file (gnus-agent-lib-file "active")))
723 (funcall function nil new)
724 (gnus-agent-write-active file new)
726 (nnheader-insert-file-contents file))))
728 (defun gnus-agent-write-active (file new)
729 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
730 (file (gnus-agent-lib-file "active"))
732 (when (file-exists-p file)
734 (nnheader-insert-file-contents file)
735 (gnus-active-to-gnus-format nil orig))
738 (when (and sym (boundp sym))
739 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
740 (setq elem (symbol-value osym)))
741 (if (> (car elem) (car (symbol-value sym)))
742 (setcar elem (car (symbol-value sym)))
743 (setcdr elem (cdr (symbol-value sym))))
744 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
746 (gnus-make-directory (file-name-directory file))
747 (let ((coding-system-for-write gnus-agent-file-coding-system))
748 ;; The hashtable contains real names of groups, no more prefix
749 ;; removing, so set `full' to `t'.
750 (gnus-write-active-file file orig t))))
752 (defun gnus-agent-save-groups (method)
753 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
755 (defun gnus-agent-save-group-info (method group active)
756 (when (gnus-agent-method-p method)
757 (let* ((gnus-command-method method)
758 (coding-system-for-write nnheader-file-coding-system)
759 (file-name-coding-system nnmail-pathname-coding-system)
760 (file (gnus-agent-lib-file "active"))
762 (gnus-make-directory (file-name-directory file))
764 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
765 (mm-disable-multibyte)
766 (when (file-exists-p file)
767 (nnheader-insert-file-contents file))
768 (goto-char (point-min))
769 (when (re-search-forward
770 (concat "^" (regexp-quote group) " ") nil t)
772 (read (current-buffer)) ;; max
773 (setq oactive-min (read (current-buffer)))) ;; min
775 (insert (format "%S %d %d y\n" (intern group)
777 (or oactive-min (car active))))
778 (goto-char (point-max))
779 (while (search-backward "\\." nil t)
782 (defun gnus-agent-group-path (group)
783 "Translate GROUP into a path."
784 (if nnmail-use-long-file-names
785 (gnus-group-real-name group)
786 (nnheader-translate-file-chars
787 (nnheader-replace-chars-in-string
788 (nnheader-replace-duplicate-chars-in-string
789 (nnheader-replace-chars-in-string
790 (gnus-group-real-name group)
797 (defun gnus-agent-method-p (method)
798 "Say whether METHOD is covered by the agent."
799 (member method gnus-agent-covered-methods))
801 (defun gnus-agent-get-function (method)
802 (if (and (not gnus-plugged)
803 (gnus-agent-method-p method))
809 ;;; History functions
811 (defun gnus-agent-history-buffer ()
812 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
814 (defun gnus-agent-open-history ()
816 (push (cons (gnus-agent-method)
817 (set-buffer (gnus-get-buffer-create
818 (format " *Gnus agent %s history*"
819 (gnus-agent-method)))))
820 gnus-agent-history-buffers)
821 (mm-disable-multibyte) ;; everything is binary
824 (let ((file (gnus-agent-lib-file "history")))
825 (when (file-exists-p file)
826 (nnheader-insert-file-contents file))
827 (set (make-local-variable 'gnus-agent-file-name) file))))
829 (defun gnus-agent-save-history ()
831 (set-buffer gnus-agent-current-history)
832 (gnus-make-directory (file-name-directory gnus-agent-file-name))
833 (let ((coding-system-for-write gnus-agent-file-coding-system))
834 (write-region (1+ (point-min)) (point-max)
835 gnus-agent-file-name nil 'silent))))
837 (defun gnus-agent-close-history ()
838 (when (gnus-buffer-live-p gnus-agent-current-history)
839 (kill-buffer gnus-agent-current-history)
840 (setq gnus-agent-history-buffers
841 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
842 gnus-agent-history-buffers))))
844 (defun gnus-agent-enter-history (id group-arts date)
846 (set-buffer gnus-agent-current-history)
847 (goto-char (point-max))
849 (insert id "\t" (number-to-string date) "\t")
851 (insert (format "%S" (intern (caar group-arts)))
852 " " (number-to-string (cdr (pop group-arts)))
855 (while (search-backward "\\." p t)
858 (defun gnus-agent-article-in-history-p (id)
860 (set-buffer (gnus-agent-history-buffer))
861 (goto-char (point-min))
862 (search-forward (concat "\n" id "\t") nil t)))
864 (defun gnus-agent-history-path (id)
866 (set-buffer (gnus-agent-history-buffer))
867 (goto-char (point-min))
868 (when (search-forward (concat "\n" id "\t") nil t)
869 (let ((method (gnus-agent-method)))
871 (while (not (numberp (setq group (read (current-buffer)))))
872 (push (concat method "/" group) paths))
873 (nreverse paths))))))
879 (defun gnus-agent-fetch-articles (group articles)
880 "Fetch ARTICLES from GROUP and put them into the Agent."
882 ;; Prune off articles that we have already fetched.
884 (cdr (assq (car articles) gnus-agent-article-alist)))
886 (let ((arts articles))
888 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
889 (setcdr arts (cddr arts))
890 (setq arts (cdr arts)))))
893 (gnus-agent-directory)
894 (gnus-agent-group-path group) "/"))
895 (date (time-to-days (current-time)))
898 (gnus-make-directory dir)
899 (gnus-message 7 "Fetching articles for %s..." group)
900 ;; Fetch the articles from the backend.
901 (if (gnus-check-backend-function 'retrieve-articles group)
902 (setq pos (gnus-retrieve-articles articles group))
905 (while (setq article (pop articles))
907 (gnus-backlog-request-article group article
909 (gnus-request-article article group))
910 (goto-char (point-max))
911 (push (cons article (point)) pos)
912 (insert-buffer-substring nntp-server-buffer)))
913 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
914 (setq pos (nreverse pos)))))
915 ;; Then save these articles into the Agent.
917 (set-buffer nntp-server-buffer)
919 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
920 (goto-char (point-min))
921 (when (search-forward "\n\n" nil t)
922 (when (search-backward "\nXrefs: " nil t)
923 ;; Handle crossposting.
924 (skip-chars-forward "^ ")
925 (skip-chars-forward " ")
927 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
928 (push (cons (buffer-substring (match-beginning 1)
930 (buffer-substring (match-beginning 2)
933 (goto-char (match-end 0)))
934 (gnus-agent-crosspost crosses (caar pos))))
935 (goto-char (point-min))
936 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
937 (setq id "No-Message-ID-in-article")
938 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
939 (let ((coding-system-for-write
940 gnus-agent-file-coding-system))
941 (write-region (point-min) (point-max)
942 (concat dir (number-to-string (caar pos)))
944 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
946 (gnus-agent-enter-history
947 id (or crosses (list (cons group (caar pos)))) date)
950 (gnus-agent-save-alist group)))))
952 (defun gnus-agent-crosspost (crosses article)
953 (let (gnus-agent-article-alist group alist beg end)
955 (set-buffer gnus-agent-overview-buffer)
956 (when (nnheader-find-nov-line article)
959 (setq end (progn (forward-line 1) (point)))))
961 (setq group (caar crosses))
962 (unless (setq alist (assoc group gnus-agent-group-alist))
963 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
964 gnus-agent-group-alist))
965 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
967 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
969 (when (= (point-max) (point-min))
970 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
972 (nnheader-insert-file-contents
973 (gnus-agent-article-name ".overview" group))))
974 (nnheader-find-nov-line (string-to-number (cdar crosses)))
975 (insert (string-to-number (cdar crosses)))
976 (insert-buffer-substring gnus-agent-overview-buffer beg end))
979 (defun gnus-agent-flush-cache ()
981 (while gnus-agent-buffer-alist
982 (set-buffer (cdar gnus-agent-buffer-alist))
983 (let ((coding-system-for-write
984 gnus-agent-file-coding-system))
985 (write-region (point-min) (point-max)
986 (gnus-agent-article-name ".overview"
987 (caar gnus-agent-buffer-alist))
989 (pop gnus-agent-buffer-alist))
990 (while gnus-agent-group-alist
991 (with-temp-file (caar gnus-agent-group-alist)
992 (princ (cdar gnus-agent-group-alist))
994 (pop gnus-agent-group-alist))))
996 (defun gnus-agent-fetch-headers (group &optional force)
997 (let ((articles (gnus-list-of-unread-articles group))
998 (gnus-decode-encoded-word-function 'identity)
999 (file (gnus-agent-article-name ".overview" group)))
1000 ;; Add article with marks to list of article headers we want to fetch.
1001 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1002 (setq articles (gnus-union (gnus-uncompress-sequence (cdr arts))
1004 (setq articles (sort articles '<))
1005 ;; Remove known articles.
1006 (when (gnus-agent-load-alist group)
1007 (setq articles (gnus-sorted-intersection
1009 (gnus-uncompress-range
1010 (cons (1+ (caar (last gnus-agent-article-alist)))
1011 (cdr (gnus-active group)))))))
1013 (gnus-make-directory (nnheader-translate-file-chars
1014 (file-name-directory file) t))
1016 (gnus-message 7 "Fetching headers for %s..." group)
1018 (set-buffer nntp-server-buffer)
1019 (unless (eq 'nov (gnus-retrieve-headers articles group))
1020 (nnvirtual-convert-headers))
1021 ;; Save these headers for later processing.
1022 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1023 (when (file-exists-p file)
1024 (gnus-agent-braid-nov group articles file))
1025 (let ((coding-system-for-write
1026 gnus-agent-file-coding-system))
1027 (write-region (point-min) (point-max) file nil 'silent))
1028 (gnus-agent-save-alist group articles nil)
1029 (gnus-agent-enter-history
1030 "last-header-fetched-for-session"
1031 (list (cons group (nth (- (length articles) 1) articles)))
1032 (time-to-days (current-time)))
1035 (defsubst gnus-agent-copy-nov-line (article)
1037 (set-buffer gnus-agent-overview-buffer)
1039 (if (eq article (read (current-buffer)))
1040 (setq e (progn (forward-line 1) (point)))
1044 (set-buffer nntp-server-buffer)
1045 (insert-buffer-substring gnus-agent-overview-buffer b e)))
1047 (defun gnus-agent-braid-nov (group articles file)
1048 (set-buffer gnus-agent-overview-buffer)
1049 (goto-char (point-min))
1050 (set-buffer nntp-server-buffer)
1052 (nnheader-insert-file-contents file)
1053 (goto-char (point-max))
1054 (if (or (= (point-min) (point-max))
1057 (< (read (current-buffer)) (car articles))))
1058 ;; We have only headers that are after the older headers,
1059 ;; so we just append them.
1061 (goto-char (point-max))
1062 (insert-buffer-substring gnus-agent-overview-buffer))
1063 ;; We do it the hard way.
1064 (nnheader-find-nov-line (car articles))
1065 (gnus-agent-copy-nov-line (car articles))
1067 (while (and articles
1069 (while (and (not (eobp))
1070 (< (read (current-buffer)) (car articles)))
1074 (gnus-agent-copy-nov-line (car articles))
1075 (setq articles (cdr articles))))
1078 (set-buffer gnus-agent-overview-buffer)
1081 (set-buffer nntp-server-buffer)
1082 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
1084 (defun gnus-agent-load-alist (group &optional dir)
1085 "Load the article-state alist for GROUP."
1086 (setq gnus-agent-article-alist
1087 (gnus-agent-read-file
1089 (expand-file-name ".agentview" dir)
1090 (gnus-agent-article-name ".agentview" group)))))
1092 (defun gnus-agent-save-alist (group &optional articles state dir)
1093 "Save the article-state alist for GROUP."
1094 (let ((file-name-coding-system nnmail-pathname-coding-system)
1095 print-level print-length)
1096 (with-temp-file (if dir
1097 (expand-file-name ".agentview" dir)
1098 (gnus-agent-article-name ".agentview" group))
1099 (princ (setq gnus-agent-article-alist
1100 (nconc gnus-agent-article-alist
1101 (mapcar (lambda (article) (cons article state))
1106 (defun gnus-agent-article-name (article group)
1107 (expand-file-name (if (stringp article) article (string-to-number article))
1108 (file-name-as-directory
1109 (expand-file-name (gnus-agent-group-path group)
1110 (gnus-agent-directory)))))
1112 (defun gnus-agent-batch-confirmation (msg)
1113 "Show error message and return t."
1114 (gnus-message 1 msg)
1118 (defun gnus-agent-batch-fetch ()
1119 "Start Gnus and fetch session."
1122 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1123 (gnus-agent-fetch-session))
1126 (defun gnus-agent-fetch-session ()
1127 "Fetch all articles and headers that are eligible for fetching."
1129 (unless gnus-agent-covered-methods
1130 (error "No servers are covered by the Gnus agent"))
1131 (unless gnus-plugged
1132 (error "Can't fetch articles while Gnus is unplugged"))
1133 (let ((methods gnus-agent-covered-methods)
1134 groups group gnus-command-method)
1139 (setq gnus-command-method (car methods))
1140 (when (or (gnus-server-opened gnus-command-method)
1141 (gnus-open-server gnus-command-method))
1142 (setq groups (gnus-groups-from-server (car methods)))
1143 (gnus-agent-with-fetch
1144 (while (setq group (pop groups))
1145 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1146 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1148 (unless (funcall gnus-agent-confirmation-function
1149 (format "Error (%s). Continue? " err))
1150 (error "Cannot fetch articles into the Gnus agent")))
1152 (unless (funcall gnus-agent-confirmation-function
1153 (format "Quit fetching session (%s). Continue? "
1155 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1157 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1159 (defun gnus-agent-fetch-group-1 (group method)
1161 (let ((gnus-command-method method)
1162 (gnus-newsgroup-name group)
1163 gnus-newsgroup-dependencies gnus-newsgroup-headers
1164 gnus-newsgroup-scored gnus-headers gnus-score
1165 gnus-use-cache articles arts
1166 category predicate info marks score-param
1167 (gnus-summary-expunge-below gnus-summary-expunge-below)
1168 (gnus-summary-mark-below gnus-summary-mark-below)
1169 (gnus-orphan-score gnus-orphan-score)
1170 ;; Maybe some other gnus-summary local variables should also
1173 (unless (gnus-check-group group)
1174 (error "Can't open server for %s" group))
1176 (when (and (or (gnus-active group)
1177 (gnus-activate-group group))
1178 (setq articles (gnus-agent-fetch-headers group))
1179 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1180 ;; Parse them and see which articles we want to fetch.
1181 (setq gnus-newsgroup-dependencies
1182 (make-vector (length articles) 0))
1183 (setq gnus-newsgroup-headers
1184 (gnus-get-newsgroup-headers-xover articles nil nil
1186 ;; `gnus-agent-overview-buffer' may be killed for
1187 ;; timeout reason. If so, recreate it.
1188 (gnus-agent-create-buffer)))
1189 (setq category (gnus-group-category group))
1192 (or (gnus-group-find-parameter group 'agent-predicate t)
1194 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1195 ;; Simple implementation
1196 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1199 (or (gnus-group-get-parameter group 'agent-score t)
1201 ;; Translate score-param into real one
1204 ((eq score-param 'file)
1205 (setq score-param (gnus-all-score-files group)))
1206 ((stringp (car score-param)))
1208 (setq score-param (list (list score-param)))))
1210 (gnus-score-headers score-param))
1211 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1213 (or (cdr (assq (mail-header-number gnus-headers)
1214 gnus-newsgroup-scored))
1215 gnus-summary-default-score))
1216 (when (funcall predicate)
1217 (push (mail-header-number gnus-headers)
1219 ;; Fetch the articles.
1221 (gnus-agent-fetch-articles group arts)))
1222 ;; Perhaps we have some additional articles to fetch.
1223 (setq arts (assq 'download (gnus-info-marks
1224 (setq info (gnus-get-info group)))))
1226 (gnus-agent-fetch-articles
1227 group (gnus-uncompress-range (cdr arts)))
1228 (setq marks (delq arts (gnus-info-marks info)))
1229 (gnus-info-set-marks info marks)
1231 (concat "(gnus-group-set-info '"
1232 (gnus-prin1-to-string info)
1236 ;;; Agent Category Mode
1239 (defvar gnus-category-mode-hook nil
1240 "Hook run in `gnus-category-mode' buffers.")
1242 (defvar gnus-category-line-format " %(%20c%): %g\n"
1243 "Format of category lines.")
1245 (defvar gnus-category-mode-line-format "Gnus: %%b"
1246 "The format specification for the category mode line.")
1248 (defvar gnus-agent-short-article 100
1249 "Articles that have fewer lines than this are short.")
1251 (defvar gnus-agent-long-article 200
1252 "Articles that have more lines than this are long.")
1254 (defvar gnus-agent-low-score 0
1255 "Articles that have a score lower than this have a low score.")
1257 (defvar gnus-agent-high-score 0
1258 "Articles that have a score higher than this have a high score.")
1261 ;;; Internal variables.
1263 (defvar gnus-category-buffer "*Agent Category*")
1265 (defvar gnus-category-line-format-alist
1266 `((?c gnus-tmp-name ?s)
1267 (?g gnus-tmp-groups ?d)))
1269 (defvar gnus-category-mode-line-format-alist
1270 `((?u user-defined ?s)))
1272 (defvar gnus-category-line-format-spec nil)
1273 (defvar gnus-category-mode-line-format-spec nil)
1275 (defvar gnus-category-mode-map nil)
1276 (put 'gnus-category-mode 'mode-class 'special)
1278 (unless gnus-category-mode-map
1279 (setq gnus-category-mode-map (make-sparse-keymap))
1280 (suppress-keymap gnus-category-mode-map)
1282 (gnus-define-keys gnus-category-mode-map
1283 "q" gnus-category-exit
1284 "k" gnus-category-kill
1285 "c" gnus-category-copy
1286 "a" gnus-category-add
1287 "p" gnus-category-edit-predicate
1288 "g" gnus-category-edit-groups
1289 "s" gnus-category-edit-score
1290 "l" gnus-category-list
1292 "\C-c\C-i" gnus-info-find-node
1293 "\C-c\C-b" gnus-bug))
1295 (defvar gnus-category-menu-hook nil
1296 "*Hook run after the creation of the menu.")
1298 (defun gnus-category-make-menu-bar ()
1299 (gnus-turn-off-edit-menu 'category)
1300 (unless (boundp 'gnus-category-menu)
1302 gnus-category-menu gnus-category-mode-map ""
1304 ["Add" gnus-category-add t]
1305 ["Kill" gnus-category-kill t]
1306 ["Copy" gnus-category-copy t]
1307 ["Edit predicate" gnus-category-edit-predicate t]
1308 ["Edit score" gnus-category-edit-score t]
1309 ["Edit groups" gnus-category-edit-groups t]
1310 ["Exit" gnus-category-exit t]))
1312 (gnus-run-hooks 'gnus-category-menu-hook)))
1314 (defun gnus-category-mode ()
1315 "Major mode for listing and editing agent categories.
1317 All normal editing commands are switched off.
1318 \\<gnus-category-mode-map>
1319 For more in-depth information on this mode, read the manual
1320 (`\\[gnus-info-find-node]').
1322 The following commands are available:
1324 \\{gnus-category-mode-map}"
1326 (when (gnus-visual-p 'category-menu 'menu)
1327 (gnus-category-make-menu-bar))
1328 (kill-all-local-variables)
1329 (gnus-simplify-mode-line)
1330 (setq major-mode 'gnus-category-mode)
1331 (setq mode-name "Category")
1332 (gnus-set-default-directory)
1333 (setq mode-line-process nil)
1334 (use-local-map gnus-category-mode-map)
1335 (buffer-disable-undo)
1336 (setq truncate-lines t)
1337 (setq buffer-read-only t)
1338 (gnus-run-hooks 'gnus-category-mode-hook))
1340 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1342 (defun gnus-category-insert-line (category)
1343 (let* ((gnus-tmp-name (car category))
1344 (gnus-tmp-groups (length (cadddr category))))
1346 (gnus-add-text-properties
1350 (eval gnus-category-line-format-spec))
1351 (list 'gnus-category gnus-tmp-name))))
1353 (defun gnus-enter-category-buffer ()
1354 "Go to the Category buffer."
1356 (gnus-category-setup-buffer)
1357 (gnus-configure-windows 'category)
1358 (gnus-category-prepare))
1360 (defun gnus-category-setup-buffer ()
1361 (unless (get-buffer gnus-category-buffer)
1363 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1364 (gnus-category-mode))))
1366 (defun gnus-category-prepare ()
1367 (gnus-set-format 'category-mode)
1368 (gnus-set-format 'category t)
1369 (let ((alist gnus-category-alist)
1370 (buffer-read-only nil))
1373 (gnus-category-insert-line (pop alist)))
1374 (goto-char (point-min))
1375 (gnus-category-position-point)))
1377 (defun gnus-category-name ()
1378 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1379 (error "No category on the current line")))
1381 (defun gnus-category-read ()
1382 "Read the category alist."
1383 (setq gnus-category-alist
1384 (or (gnus-agent-read-file
1385 (nnheader-concat gnus-agent-directory "lib/categories"))
1386 (list (list 'default 'short nil nil)))))
1388 (defun gnus-category-write ()
1389 "Write the category alist."
1390 (setq gnus-category-predicate-cache nil
1391 gnus-category-group-cache nil)
1392 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1393 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1394 (prin1 gnus-category-alist (current-buffer))))
1396 (defun gnus-category-edit-predicate (category)
1397 "Edit the predicate for CATEGORY."
1398 (interactive (list (gnus-category-name)))
1399 (let ((info (assq category gnus-category-alist)))
1401 (cadr info) (format "Editing the predicate for category %s" category)
1402 `(lambda (predicate)
1403 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1404 (gnus-category-write)
1405 (gnus-category-list)))))
1407 (defun gnus-category-edit-score (category)
1408 "Edit the score expression for CATEGORY."
1409 (interactive (list (gnus-category-name)))
1410 (let ((info (assq category gnus-category-alist)))
1413 (format "Editing the score expression for category %s" category)
1415 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1416 (gnus-category-write)
1417 (gnus-category-list)))))
1419 (defun gnus-category-edit-groups (category)
1420 "Edit the group list for CATEGORY."
1421 (interactive (list (gnus-category-name)))
1422 (let ((info (assq category gnus-category-alist)))
1424 (cadddr info) (format "Editing the group list for category %s" category)
1426 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1427 (gnus-category-write)
1428 (gnus-category-list)))))
1430 (defun gnus-category-kill (category)
1431 "Kill the current category."
1432 (interactive (list (gnus-category-name)))
1433 (let ((info (assq category gnus-category-alist))
1434 (buffer-read-only nil))
1436 (setq gnus-category-alist (delq info gnus-category-alist))
1437 (gnus-category-write)))
1439 (defun gnus-category-copy (category to)
1440 "Copy the current category."
1441 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1442 (let ((info (assq category gnus-category-alist)))
1443 (push (list to (gnus-copy-sequence (cadr info))
1444 (gnus-copy-sequence (caddr info)) nil)
1445 gnus-category-alist)
1446 (gnus-category-write)
1447 (gnus-category-list)))
1449 (defun gnus-category-add (category)
1450 "Create a new category."
1451 (interactive "SCategory name: ")
1452 (when (assq category gnus-category-alist)
1453 (error "Category %s already exists" category))
1454 (push (list category 'false nil nil)
1455 gnus-category-alist)
1456 (gnus-category-write)
1457 (gnus-category-list))
1459 (defun gnus-category-list ()
1460 "List all categories."
1462 (gnus-category-prepare))
1464 (defun gnus-category-exit ()
1465 "Return to the group buffer."
1467 (kill-buffer (current-buffer))
1468 (gnus-configure-windows 'group t))
1470 ;; To avoid having 8-bit characters in the source file.
1471 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1473 (defvar gnus-category-predicate-alist
1474 '((spam . gnus-agent-spam-p)
1475 (short . gnus-agent-short-p)
1476 (long . gnus-agent-long-p)
1477 (low . gnus-agent-low-scored-p)
1478 (high . gnus-agent-high-scored-p)
1479 (true . gnus-agent-true)
1480 (false . gnus-agent-false))
1481 "Mapping from short score predicate symbols to predicate functions.")
1483 (defun gnus-agent-spam-p ()
1484 "Say whether an article is spam or not."
1485 (unless gnus-agent-spam-hashtb
1486 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1487 (if (not (equal (mail-header-references gnus-headers) ""))
1489 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1491 (gnus-gethash string gnus-agent-spam-hashtb)
1492 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1494 (defun gnus-agent-short-p ()
1495 "Say whether an article is short or not."
1496 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1498 (defun gnus-agent-long-p ()
1499 "Say whether an article is long or not."
1500 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1502 (defun gnus-agent-low-scored-p ()
1503 "Say whether an article has a low score or not."
1504 (< gnus-score gnus-agent-low-score))
1506 (defun gnus-agent-high-scored-p ()
1507 "Say whether an article has a high score or not."
1508 (> gnus-score gnus-agent-high-score))
1510 (defun gnus-category-make-function (cat)
1511 "Make a function from category CAT."
1512 (let ((func (gnus-category-make-function-1 cat)))
1513 (if (and (= (length func) 1)
1514 (symbolp (car func)))
1516 (gnus-byte-compile `(lambda () ,func)))))
1518 (defun gnus-agent-true ()
1522 (defun gnus-agent-false ()
1526 (defun gnus-category-make-function-1 (cat)
1527 "Make a function from category CAT."
1529 ;; Functions are just returned as is.
1531 (gnus-functionp cat))
1532 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1534 ;; More complex category.
1537 ((memq (car cat) '(& and))
1539 ((memq (car cat) '(| or))
1541 ((memq (car cat) gnus-category-not)
1543 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1545 (error "Unknown category type: %s" cat))))
1547 (defun gnus-get-predicate (predicate)
1548 "Return the predicate for CATEGORY."
1549 (or (cdr (assoc predicate gnus-category-predicate-cache))
1550 (cdar (push (cons predicate
1551 (gnus-category-make-function predicate))
1552 gnus-category-predicate-cache))))
1554 (defun gnus-group-category (group)
1555 "Return the category GROUP belongs to."
1556 (unless gnus-category-group-cache
1557 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1558 (let ((cs gnus-category-alist)
1560 (while (setq cat (pop cs))
1561 (setq groups (cadddr cat))
1563 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1564 (or (gnus-gethash group gnus-category-group-cache)
1565 (assq 'default gnus-category-alist)))
1567 (defun gnus-agent-expire ()
1568 "Expire all old articles."
1570 (let ((methods gnus-agent-covered-methods)
1571 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1572 gnus-command-method sym group articles
1573 history overview file histories elem art nov-file low info
1574 unreads marked article orig lowest highest)
1576 (setq overview (gnus-get-buffer-create " *expire overview*"))
1577 (while (setq gnus-command-method (pop methods))
1578 (when (file-exists-p (gnus-agent-lib-file "active"))
1580 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1581 (gnus-active-to-gnus-format
1583 (setq orig (gnus-make-hashtable
1584 (count-lines (point-min) (point-max))))))
1585 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1586 (gnus-agent-open-history)
1588 (setq gnus-agent-current-history
1589 (setq history (gnus-agent-history-buffer))))
1590 (goto-char (point-min))
1591 (when (> (buffer-size) 1)
1592 (goto-char (point-min))
1594 (skip-chars-forward "^\t")
1595 (if (let ((fetch-date (read (current-buffer))))
1596 (if (numberp fetch-date)
1598 ;; History file is corrupted.
1601 (format "File %s is corrupted!"
1602 (gnus-agent-lib-file "history")))
1606 ;; New article; we don't expire it.
1608 ;; Old article. Schedule it for possible nuking.
1610 (setq sym (let ((obarray expiry-hashtb) s)
1611 (setq s (read (current-buffer)))
1612 (if (stringp s) (intern s) s)))
1614 (set sym (cons (cons (read (current-buffer)) (point))
1615 (symbol-value sym)))
1616 (set sym (list (cons (read (current-buffer)) (point)))))
1617 (skip-chars-forward " "))
1619 ;; We now have all articles that can possibly be expired.
1622 (setq group (symbol-name sym)
1623 articles (sort (symbol-value sym) 'car-less-than-car)
1624 low (car (gnus-active group))
1625 info (gnus-get-info group)
1626 unreads (ignore-errors
1627 (gnus-list-of-unread-articles group))
1629 (gnus-uncompress-range
1630 (cdr (assq 'tick (gnus-info-marks info))))
1631 (gnus-uncompress-range
1633 (gnus-info-marks info)))))
1634 nov-file (gnus-agent-article-name ".overview" group)
1637 (gnus-agent-load-alist group)
1638 (gnus-message 5 "Expiring articles in %s" group)
1639 (set-buffer overview)
1641 (when (file-exists-p nov-file)
1642 (nnheader-insert-file-contents nov-file))
1643 (goto-char (point-min))
1645 (while (setq elem (pop articles))
1646 (setq article (car elem))
1647 (when (or (null low)
1649 gnus-agent-expire-all
1650 (and (not (memq article unreads))
1651 (not (memq article marked))))
1652 ;; Find and nuke the NOV line.
1653 (while (and (not (eobp))
1655 (setq art (read (current-buffer)))))
1657 (if (and (numberp art)
1659 (gnus-agent-article-name
1660 (number-to-string art) group)))
1666 ;; Remove old NOV lines that have no articles.
1667 (gnus-delete-line)))
1672 ;; Nuke the article.
1673 (when (file-exists-p
1674 (setq file (gnus-agent-article-name
1675 (number-to-string article)
1678 ;; Schedule the history line for nuking.
1679 (push (cdr elem) histories)))
1680 (gnus-make-directory (file-name-directory nov-file))
1681 (let ((coding-system-for-write
1682 gnus-agent-file-coding-system))
1683 (write-region (point-min) (point-max) nov-file nil 'silent))
1684 ;; Delete the unwanted entries in the alist.
1685 (setq gnus-agent-article-alist
1686 (sort gnus-agent-article-alist 'car-less-than-car))
1687 (let* ((alist gnus-agent-article-alist)
1688 (prev (cons nil alist))
1692 (<= (caar alist) article))
1693 (if (or (not (cdar alist))
1695 (gnus-agent-article-name
1700 (push (caar alist) expired)
1701 (setcdr prev (setq alist (cdr alist))))
1703 alist (cdr alist))))
1704 (setq gnus-agent-article-alist (cdr first))
1705 (gnus-agent-save-alist group)
1706 ;; Mark all articles up to the first article
1707 ;; in `gnus-article-alist' as read.
1708 (when (and info (caar gnus-agent-article-alist))
1709 (setcar (nthcdr 2 info)
1712 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1713 ;; Maybe everything has been expired from `gnus-article-alist'
1714 ;; and so the above marking as read could not be conducted,
1715 ;; or there are expired article within the range of the alist.
1718 (or (not (caar gnus-agent-article-alist))
1720 (caar gnus-agent-article-alist))))
1721 (setcar (nthcdr 2 info)
1724 (nreverse expired))))
1726 (concat "(gnus-group-set-info '"
1727 (gnus-prin1-to-string info)
1730 (if (gnus-gethash group orig)
1731 (setcar (gnus-gethash group orig) lowest)
1732 (gnus-sethash group (cons lowest highest) orig))))
1734 (set-buffer history)
1735 (setq histories (nreverse (sort histories '<)))
1737 (goto-char (pop histories))
1739 (gnus-agent-save-history)
1740 (gnus-agent-close-history)
1741 (gnus-write-active-file
1742 (gnus-agent-lib-file "active") orig))
1743 (gnus-message 4 "Expiry...done")))))))
1746 (defun gnus-agent-batch ()
1748 (let ((init-file-user "")
1749 (gnus-always-read-dribble-file t))
1751 (gnus-group-send-drafts)
1752 (gnus-agent-fetch-session))
1754 (provide 'gnus-agent)
1756 ;;; gnus-agent.el ends here