1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
33 "Where the Gnus agent will store its files."
37 (defcustom gnus-agent-plugged-hook nil
38 "Hook run when plugging into the network."
42 (defcustom gnus-agent-unplugged-hook nil
43 "Hook run when unplugging from the network."
47 (defcustom gnus-agent-handle-level gnus-level-subscribed
48 "Groups on levels higher than this variable will be ignored by the Agent."
52 (defcustom gnus-agent-expire-days 7
53 "Read articles older than this will be expired."
57 (defcustom gnus-agent-expire-all nil
58 "If non-nil, also expire unread, ticked and dormant articles.
59 If nil, only read articles will be expired."
63 (defcustom gnus-agent-group-mode-hook nil
64 "Hook run in Agent group minor modes."
68 (defcustom gnus-agent-summary-mode-hook nil
69 "Hook run in Agent summary minor modes."
73 (defcustom gnus-agent-server-mode-hook nil
74 "Hook run in Agent summary minor modes."
78 ;;; Internal variables
80 (defvar gnus-agent-history-buffers nil)
81 (defvar gnus-agent-buffer-alist nil)
82 (defvar gnus-agent-article-alist nil)
83 (defvar gnus-agent-group-alist nil)
84 (defvar gnus-agent-covered-methods nil)
85 (defvar gnus-category-alist nil)
86 (defvar gnus-agent-current-history nil)
87 (defvar gnus-agent-overview-buffer nil)
88 (defvar gnus-category-predicate-cache nil)
89 (defvar gnus-category-group-cache nil)
90 (defvar gnus-agent-spam-hashtb nil)
91 (defvar gnus-agent-file-name nil)
92 (defvar gnus-agent-send-mail-function nil)
93 (defvar gnus-agent-article-file-coding-system 'no-conversion)
103 (defun gnus-open-agent ()
105 (gnus-agent-read-servers)
107 (setq gnus-agent-overview-buffer
108 (get-buffer-create " *Gnus agent overview*"))
109 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
110 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
111 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
113 (gnus-add-shutdown 'gnus-close-agent 'gnus)
115 (defun gnus-close-agent ()
116 (setq gnus-agent-covered-methods nil
117 gnus-category-predicate-cache nil
118 gnus-category-group-cache nil
119 gnus-agent-spam-hashtb nil)
120 (gnus-kill-buffer gnus-agent-overview-buffer))
123 ;;; Utility functions
126 (defun gnus-agent-read-file (file)
127 "Load FILE and do a `read' there."
128 (nnheader-temp-write nil
130 (insert-file-contents file)
131 (goto-char (point-min))
132 (read (current-buffer)))))
134 (defsubst gnus-agent-method ()
135 (concat (symbol-name (car gnus-command-method)) "/"
136 (if (equal (cadr gnus-command-method) "")
138 (cadr gnus-command-method))))
140 (defsubst gnus-agent-directory ()
141 "Path of the Gnus agent directory."
142 (nnheader-concat gnus-agent-directory
143 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
145 (defun gnus-agent-lib-file (file)
146 "The full path of the Gnus agent library FILE."
147 (concat (gnus-agent-directory) "agent.lib/" file))
149 ;;; Fetching setup functions.
151 (defun gnus-agent-start-fetch ()
152 "Initialize data structures for efficient fetching."
153 (gnus-agent-open-history)
154 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
156 (defun gnus-agent-stop-fetch ()
157 "Save all data structures and clean up."
158 (gnus-agent-save-history)
159 (gnus-agent-close-history)
160 (setq gnus-agent-spam-hashtb nil)
162 (set-buffer nntp-server-buffer)
165 (defmacro gnus-agent-with-fetch (&rest forms)
169 (gnus-agent-start-fetch)
171 (gnus-agent-stop-fetch)))
173 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
174 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
180 (defvar gnus-agent-mode-hook nil
181 "Hook run when installing agent mode.")
183 (defvar gnus-agent-mode nil)
184 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
186 (defun gnus-agent-mode ()
187 "Minor mode for providing a agent support in Gnus buffers."
188 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
189 (symbol-name major-mode))
190 (match-string 1 (symbol-name major-mode))))
191 (mode (intern (format "gnus-agent-%s-mode" buffer))))
192 (set (make-local-variable 'gnus-agent-mode) t)
194 (set (make-local-variable mode) t)
196 (when (gnus-visual-p 'agent-menu 'menu)
197 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
198 (unless (assq 'gnus-agent-mode minor-mode-alist)
199 (push gnus-agent-mode-status minor-mode-alist))
200 (unless (assq mode minor-mode-map-alist)
201 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
203 minor-mode-map-alist))
204 (gnus-agent-toggle-plugged gnus-plugged)
205 (gnus-run-hooks 'gnus-agent-mode-hook
206 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
208 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
209 (gnus-define-keys gnus-agent-group-mode-map
210 "Ju" gnus-agent-fetch-groups
211 "Jc" gnus-enter-category-buffer
212 "Jj" gnus-agent-toggle-plugged
213 "Js" gnus-agent-fetch-session
214 "JS" gnus-group-send-drafts
215 "Ja" gnus-agent-add-group)
217 (defun gnus-agent-group-make-menu-bar ()
218 (unless (boundp 'gnus-agent-group-menu)
220 gnus-agent-group-menu gnus-agent-group-mode-map ""
222 ["Toggle plugged" gnus-agent-toggle-plugged t]
223 ["List categories" gnus-enter-category-buffer t]
224 ["Send drafts" gnus-group-send-drafts gnus-plugged]
226 ["All" gnus-agent-fetch-session gnus-plugged]
227 ["Group" gnus-agent-fetch-group gnus-plugged])))))
229 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
230 (gnus-define-keys gnus-agent-summary-mode-map
231 "Jj" gnus-agent-toggle-plugged
232 "J#" gnus-agent-mark-article
233 "J\M-#" gnus-agent-unmark-article
234 "@" gnus-agent-toggle-mark
235 "Jc" gnus-agent-catchup)
237 (defun gnus-agent-summary-make-menu-bar ()
238 (unless (boundp 'gnus-agent-summary-menu)
240 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
242 ["Toggle plugged" gnus-agent-toggle-plugged t]
243 ["Mark as downloadable" gnus-agent-mark-article t]
244 ["Unmark as downloadable" gnus-agent-unmark-article t]
245 ["Toggle mark" gnus-agent-toggle-mark t]
246 ["Catchup undownloaded" gnus-agent-catchup t]))))
248 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
249 (gnus-define-keys gnus-agent-server-mode-map
250 "Jj" gnus-agent-toggle-plugged
251 "Ja" gnus-agent-add-server
252 "Jr" gnus-agent-remove-server)
254 (defun gnus-agent-server-make-menu-bar ()
255 (unless (boundp 'gnus-agent-server-menu)
257 gnus-agent-server-menu gnus-agent-server-mode-map ""
259 ["Toggle plugged" gnus-agent-toggle-plugged t]
260 ["Add" gnus-agent-add-server t]
261 ["Remove" gnus-agent-remove-server t]))))
263 (defun gnus-agent-toggle-plugged (plugged)
264 "Toggle whether Gnus is unplugged or not."
265 (interactive (list (not gnus-plugged)))
268 (setq gnus-plugged plugged)
269 (gnus-run-hooks 'gnus-agent-plugged-hook)
270 (setcar (cdr gnus-agent-mode-status) " Plugged"))
271 (gnus-agent-close-connections)
272 (setq gnus-plugged plugged)
273 (gnus-run-hooks 'gnus-agent-unplugged-hook)
274 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
275 (set-buffer-modified-p t))
277 (defun gnus-agent-close-connections ()
278 "Close all methods covered by the Gnus agent."
279 (let ((methods gnus-agent-covered-methods))
281 (gnus-close-server (pop methods)))))
284 (defun gnus-unplugged ()
285 "Start Gnus unplugged."
287 (setq gnus-plugged nil)
291 (defun gnus-plugged ()
292 "Start Gnus plugged."
294 (setq gnus-plugged t)
298 (defun gnus-agentize ()
299 "Allow Gnus to be an offline newsreader.
300 The normal usage of this command is to put the following as the
301 last form in your `.gnus.el' file:
305 This will modify the `gnus-before-startup-hook', `gnus-post-method',
306 and `message-send-mail-function' variables, and install the Gnus
307 agent minor mode in all Gnus buffers."
310 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
311 (unless gnus-agent-send-mail-function
312 (setq gnus-agent-send-mail-function message-send-mail-function
313 message-send-mail-function 'gnus-agent-send-mail))
314 (unless gnus-agent-covered-methods
315 (setq gnus-agent-covered-methods (list gnus-select-method))))
317 (defun gnus-agent-queue-setup ()
318 "Make sure the queue group exists."
319 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
320 (gnus-request-create-group "queue" '(nndraft ""))
321 (let ((gnus-level-default-subscribed 1))
322 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
323 (gnus-group-set-parameter
324 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
326 (defun gnus-agent-send-mail ()
328 (funcall gnus-agent-send-mail-function)
329 (goto-char (point-min))
331 (concat "^" (regexp-quote mail-header-separator) "\n"))
333 (gnus-request-accept-article "nndraft:queue")))
336 ;;; Group mode commands
339 (defun gnus-agent-fetch-groups (n)
340 "Put all new articles in the current groups into the agent."
342 (gnus-group-iterate n 'gnus-agent-fetch-group))
344 (defun gnus-agent-fetch-group (group)
345 "Put all new articles in GROUP into the agent."
346 (interactive (list (gnus-group-group-name)))
348 (error "No group on the current line"))
349 (let ((gnus-command-method (gnus-find-method-for-group group)))
350 (gnus-agent-with-fetch
351 (gnus-agent-fetch-group-1 group gnus-command-method)
352 (gnus-message 5 "Fetching %s...done" group))))
354 (defun gnus-agent-add-group (category arg)
355 "Add the current group to an agent category."
361 (mapcar (lambda (cat) (list (symbol-name (car cat))))
365 (let ((cat (assq category gnus-category-alist))
367 (gnus-group-iterate arg
369 (when (cadddr (setq c (gnus-group-category group)))
370 (setf (cadddr c) (delete group (cadddr c))))
371 (push group groups)))
372 (setf (cadddr cat) (nconc (cadddr cat) groups))
373 (gnus-category-write)))
376 ;;; Server mode commands
379 (defun gnus-agent-add-server (server)
380 "Enroll SERVER in the agent program."
381 (interactive (list (gnus-server-server-name)))
383 (error "No server on the current line"))
384 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
385 (when (member method gnus-agent-covered-methods)
386 (error "Server already in the agent program"))
387 (push method gnus-agent-covered-methods)
388 (gnus-agent-write-servers)
389 (message "Entered %s into the agent" server)))
391 (defun gnus-agent-remove-server (server)
392 "Remove SERVER from the agent program."
393 (interactive (list (gnus-server-server-name)))
395 (error "No server on the current line"))
396 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
397 (unless (member method gnus-agent-covered-methods)
398 (error "Server not in the agent program"))
399 (setq gnus-agent-covered-methods
400 (delete method gnus-agent-covered-methods))
401 (gnus-agent-write-servers)
402 (message "Removed %s from the agent" server)))
404 (defun gnus-agent-read-servers ()
405 "Read the alist of covered servers."
406 (setq gnus-agent-covered-methods
407 (gnus-agent-read-file
408 (nnheader-concat gnus-agent-directory "lib/servers"))))
410 (defun gnus-agent-write-servers ()
411 "Write the alist of covered servers."
412 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
413 (prin1 gnus-agent-covered-methods (current-buffer))))
419 (defun gnus-agent-mark-article (n &optional unmark)
420 "Mark the next N articles as downloadable.
421 If N is negative, mark backward instead. If UNMARK is non-nil, remove
422 the mark instead. The difference between N and the actual number of
423 articles marked is returned."
425 (let ((backward (< n 0))
430 (gnus-summary-set-agent-mark
431 (gnus-summary-article-number) unmark)
432 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
435 (gnus-message 7 "No more articles"))
436 (gnus-summary-recenter)
437 (gnus-summary-position-point)
440 (defun gnus-agent-unmark-article (n)
441 "Remove the downloadable mark from the next N articles.
442 If N is negative, unmark backward instead. The difference between N and
443 the actual number of articles unmarked is returned."
445 (gnus-agent-mark-article n t))
447 (defun gnus-agent-toggle-mark (n)
448 "Toggle the downloadable mark from the next N articles.
449 If N is negative, toggle backward instead. The difference between N and
450 the actual number of articles toggled is returned."
452 (gnus-agent-mark-article n 'toggle))
454 (defun gnus-summary-set-agent-mark (article &optional unmark)
455 "Mark ARTICLE as downloadable."
456 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
457 (memq article gnus-newsgroup-downloadable)
461 (setq gnus-newsgroup-downloadable
462 (delq article gnus-newsgroup-downloadable))
463 (push article gnus-newsgroup-undownloaded))
464 (setq gnus-newsgroup-undownloaded
465 (delq article gnus-newsgroup-undownloaded))
466 (push article gnus-newsgroup-downloadable))
467 (gnus-summary-update-mark
468 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
471 (defun gnus-agent-get-undownloaded-list ()
472 "Mark all unfetched articles as read."
473 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
474 (when (and (not gnus-plugged)
475 (gnus-agent-method-p gnus-command-method))
476 (gnus-agent-load-alist gnus-newsgroup-name)
477 (let ((articles gnus-newsgroup-unreads)
479 (while (setq article (pop articles))
480 (unless (or (cdr (assq article gnus-agent-article-alist))
481 (memq article gnus-newsgroup-downloadable))
482 (push article gnus-newsgroup-undownloaded)))))))
484 (defun gnus-agent-catchup ()
485 "Mark all undownloaded articles as read."
488 (while gnus-newsgroup-undownloaded
489 (gnus-summary-mark-article
490 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
491 (gnus-summary-position-point))
494 ;;; Internal functions
497 (defun gnus-agent-save-active (method)
498 (when (gnus-agent-method-p method)
499 (let* ((gnus-command-method method)
500 (file (gnus-agent-lib-file "active")))
501 (gnus-make-directory (file-name-directory file))
502 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
503 (write-region (point-min) (point-max) file nil 'silent))
504 (when (file-exists-p (gnus-agent-lib-file "groups"))
505 (delete-file (gnus-agent-lib-file "groups"))))))
507 (defun gnus-agent-save-groups (method)
508 (let* ((gnus-command-method method)
509 (file (gnus-agent-lib-file "groups")))
510 (gnus-make-directory (file-name-directory file))
511 (write-region (point-min) (point-max) file nil 'silent))
512 (when (file-exists-p (gnus-agent-lib-file "active"))
513 (delete-file (gnus-agent-lib-file "active"))))
515 (defun gnus-agent-group-path (group)
516 "Translate GROUP into a path."
517 (if nnmail-use-long-file-names
518 (gnus-group-real-name group)
519 (nnheader-replace-chars-in-string
520 (nnheader-translate-file-chars (gnus-group-real-name group))
525 (defun gnus-agent-method-p (method)
526 "Say whether METHOD is covered by the agent."
527 (member method gnus-agent-covered-methods))
529 (defun gnus-agent-get-function (method)
530 (if (and (not gnus-plugged)
531 (gnus-agent-method-p method))
537 ;;; History functions
539 (defun gnus-agent-history-buffer ()
540 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
542 (defun gnus-agent-open-history ()
544 (push (cons (gnus-agent-method)
545 (set-buffer (get-buffer-create
546 (format " *Gnus agent %s history*"
547 (gnus-agent-method)))))
548 gnus-agent-history-buffers)
551 (let ((file (gnus-agent-lib-file "history")))
552 (when (file-exists-p file)
554 (set (make-local-variable 'gnus-agent-file-name) file))))
556 (defun gnus-agent-save-history ()
558 (set-buffer gnus-agent-current-history)
559 (gnus-make-directory (file-name-directory gnus-agent-file-name))
560 (write-region (1+ (point-min)) (point-max)
561 gnus-agent-file-name nil 'silent)))
563 (defun gnus-agent-close-history ()
564 (when (gnus-buffer-live-p gnus-agent-current-history)
565 (kill-buffer gnus-agent-current-history)
566 (setq gnus-agent-history-buffers
567 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
568 gnus-agent-history-buffers))))
570 (defun gnus-agent-enter-history (id group-arts date)
572 (set-buffer gnus-agent-current-history)
573 (goto-char (point-max))
574 (insert id "\t" (number-to-string date) "\t")
576 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
580 (defun gnus-agent-article-in-history-p (id)
582 (set-buffer (gnus-agent-history-buffer))
583 (goto-char (point-min))
584 (search-forward (concat "\n" id "\t") nil t)))
586 (defun gnus-agent-history-path (id)
588 (set-buffer (gnus-agent-history-buffer))
589 (goto-char (point-min))
590 (when (search-forward (concat "\n" id "\t") nil t)
591 (let ((method (gnus-agent-method)))
593 (while (not (numberp (setq group (read (current-buffer)))))
594 (push (concat method "/" group) paths))
595 (nreverse paths))))))
601 (defun gnus-agent-fetch-articles (group articles)
602 "Fetch ARTICLES from GROUP and put them into the agent."
604 ;; Prune off articles that we have already fetched.
606 (cdr (assq (car articles) gnus-agent-article-alist)))
608 (let ((arts articles))
610 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
611 (setcdr arts (cddr arts))
612 (setq arts (cdr arts)))))
615 (gnus-agent-directory)
616 (gnus-agent-group-path group) "/"))
617 (date (gnus-time-to-day (current-time)))
619 pos alists crosses id elem)
620 (gnus-make-directory dir)
621 (gnus-message 7 "Fetching articles for %s..." group)
622 ;; Fetch the articles from the backend.
623 (if (gnus-check-backend-function 'retrieve-articles group)
624 (setq pos (gnus-retrieve-articles articles group))
625 (nnheader-temp-write nil
626 (let ((buf (current-buffer))
628 (while (setq article (pop articles))
629 (when (gnus-request-article article group)
630 (goto-char (point-max))
631 (push (cons article (point)) pos)
632 (insert-buffer-substring nntp-server-buffer)))
633 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
634 (setq pos (nreverse pos)))))
635 ;; Then save these articles into the agent.
637 (set-buffer nntp-server-buffer)
639 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
640 (goto-char (point-min))
641 (when (search-forward "\n\n" nil t)
642 (when (search-backward "\nXrefs: " nil t)
643 ;; Handle crossposting.
644 (skip-chars-forward "^ ")
645 (skip-chars-forward " ")
647 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
648 (push (cons (buffer-substring (match-beginning 1)
650 (buffer-substring (match-beginning 2)
653 (goto-char (match-end 0)))
654 (gnus-agent-crosspost crosses (caar pos))))
655 (goto-char (point-min))
656 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
657 (setq id "No-Message-ID-in-article")
658 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
659 (let ((coding-system-for-write
660 gnus-agent-article-file-coding-system))
661 (write-region (point-min) (point-max)
662 (concat dir (number-to-string (caar pos)))
664 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
666 (gnus-agent-enter-history
667 id (or crosses (list (cons group (caar pos)))) date)
670 (gnus-agent-save-alist group)))))
672 (defun gnus-agent-crosspost (crosses article)
673 (let (gnus-agent-article-alist group alist beg end)
675 (set-buffer gnus-agent-overview-buffer)
676 (when (nnheader-find-nov-line article)
679 (setq end (progn (forward-line 1) (point)))))
681 (setq group (caar crosses))
682 (unless (setq alist (assoc group gnus-agent-group-alist))
683 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
684 gnus-agent-group-alist))
685 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
687 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
689 (when (= (point-max) (point-min))
690 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
692 (insert-file-contents
693 (gnus-agent-article-name ".overview" group))))
694 (nnheader-find-nov-line (string-to-number (cdar crosses)))
695 (insert (string-to-number (cdar crosses)))
696 (insert-buffer-substring gnus-agent-overview-buffer beg end))
699 (defun gnus-agent-flush-cache ()
701 (while gnus-agent-buffer-alist
702 (set-buffer (cdar gnus-agent-buffer-alist))
703 (write-region (point-min) (point-max)
704 (gnus-agent-article-name ".overview"
705 (caar gnus-agent-buffer-alist))
707 (pop gnus-agent-buffer-alist))
708 (while gnus-agent-group-alist
709 (nnheader-temp-write (caar gnus-agent-group-alist)
710 (princ (cdar gnus-agent-group-alist))
712 (pop gnus-agent-group-alist))))
714 (defun gnus-agent-fetch-headers (group articles &optional force)
715 (gnus-agent-load-alist group)
716 ;; Find out what headers we need to retrieve.
719 (assq (car articles) gnus-agent-article-alist))
721 (let ((arts articles))
723 (if (assq (cadr arts) gnus-agent-article-alist)
724 (setcdr arts (cddr arts))
725 (setq arts (cdr arts)))))
728 (gnus-message 7 "Fetching headers for %s..." group)
730 (set-buffer nntp-server-buffer)
731 (unless (eq 'nov (gnus-retrieve-headers articles group))
732 (nnvirtual-convert-headers))
733 ;; Save these headers for later processing.
734 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
737 (setq file (gnus-agent-article-name ".overview" group)))
738 (gnus-agent-braid-nov group articles file))
739 (gnus-make-directory (nnheader-translate-file-chars
740 (file-name-directory file)))
741 (write-region (point-min) (point-max) file nil 'silent)
742 (gnus-agent-save-alist group articles nil))
745 (defsubst gnus-agent-copy-nov-line (article)
747 (set-buffer gnus-agent-overview-buffer)
749 (if (eq article (read (current-buffer)))
750 (setq e (progn (forward-line 1) (point)))
752 (set-buffer nntp-server-buffer)
753 (insert-buffer-substring gnus-agent-overview-buffer b e)))
755 (defun gnus-agent-braid-nov (group articles file)
757 (set-buffer gnus-agent-overview-buffer)
758 (goto-char (point-min))
759 (set-buffer nntp-server-buffer)
761 (insert-file-contents file)
762 (goto-char (point-min))
763 (if (or (= (point-min) (point-max))
766 (< (read (current-buffer)) (car articles))))
767 ;; We have only headers that are after the older headers,
768 ;; so we just append them.
770 (goto-char (point-max))
771 (insert-buffer-substring gnus-agent-overview-buffer))
772 ;; We do it the hard way.
773 (nnheader-find-nov-line (car articles))
774 (gnus-agent-copy-nov-line (car articles))
778 (while (and (not (eobp))
779 (< (read (current-buffer)) (car articles)))
783 (gnus-agent-copy-nov-line (car articles))
784 (setq articles (cdr articles))))
787 (set-buffer gnus-agent-overview-buffer)
790 (set-buffer nntp-server-buffer)
791 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
793 (defun gnus-agent-load-alist (group &optional dir)
794 "Load the article-state alist for GROUP."
795 (setq gnus-agent-article-alist
796 (gnus-agent-read-file
798 (concat dir ".agentview")
799 (gnus-agent-article-name ".agentview" group)))))
801 (defun gnus-agent-save-alist (group &optional articles state dir)
802 "Load the article-state alist for GROUP."
803 (nnheader-temp-write (if dir
804 (concat dir ".agentview")
805 (gnus-agent-article-name ".agentview" group))
806 (princ (setq gnus-agent-article-alist
807 (nconc gnus-agent-article-alist
808 (mapcar (lambda (article) (cons article state))
813 (defun gnus-agent-article-name (article group)
814 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
815 (if (stringp article) article (string-to-number article))))
818 (defun gnus-agent-batch-fetch ()
819 "Start Gnus and fetch session."
822 (gnus-agent-fetch-session)
825 (defun gnus-agent-fetch-session ()
826 "Fetch all articles and headers that are eligible for fetching."
828 (unless gnus-agent-covered-methods
829 (error "No servers are covered by the Gnus agent"))
831 (error "Can't fetch articles while Gnus is unplugged"))
832 (let ((methods gnus-agent-covered-methods)
833 groups group gnus-command-method)
836 (setq gnus-command-method (car methods))
837 (when (or (gnus-server-opened gnus-command-method)
838 (gnus-open-server gnus-command-method))
839 (setq groups (gnus-groups-from-server (pop methods)))
840 (gnus-agent-with-fetch
841 (while (setq group (pop groups))
842 (when (<= (gnus-group-level group) gnus-agent-handle-level)
843 (gnus-agent-fetch-group-1 group gnus-command-method))))))
844 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
846 (defun gnus-agent-fetch-group-1 (group method)
848 (let ((gnus-command-method method)
849 gnus-newsgroup-dependencies gnus-newsgroup-headers
850 gnus-newsgroup-scored gnus-headers gnus-score
851 gnus-use-cache articles score arts
852 category predicate info marks score-param)
854 (when (and (setq articles (gnus-list-of-unread-articles group))
855 (gnus-agent-fetch-headers group articles))
856 ;; Parse them and see which articles we want to fetch.
857 (setq gnus-newsgroup-dependencies
858 (make-vector (length articles) 0))
859 (setq gnus-newsgroup-headers
860 (gnus-get-newsgroup-headers-xover articles nil nil group))
861 (setq category (gnus-group-category group))
864 (or (gnus-group-get-parameter group 'agent-predicate)
867 (or (gnus-group-get-parameter group 'agent-score)
870 (gnus-score-headers (list (list score-param))))
872 (while (setq gnus-headers (pop gnus-newsgroup-headers))
874 (or (cdr (assq (mail-header-number gnus-headers)
875 gnus-newsgroup-scored))
876 gnus-summary-default-score))
877 (when (funcall predicate)
878 (push (mail-header-number gnus-headers)
880 ;; Fetch the articles.
882 (gnus-agent-fetch-articles group arts)))
883 ;; Perhaps we have some additional articles to fetch.
884 (setq arts (assq 'download (gnus-info-marks
885 (setq info (gnus-get-info group)))))
887 (gnus-agent-fetch-articles
888 group (gnus-uncompress-range (cdr arts)))
889 (setq marks (delq arts (gnus-info-marks info)))
890 (gnus-info-set-marks info marks))))
893 ;;; Agent Category Mode
896 (defvar gnus-category-mode-hook nil
897 "Hook run in `gnus-category-mode' buffers.")
899 (defvar gnus-category-line-format " %(%20c%): %g\n"
900 "Format of category lines.")
902 (defvar gnus-category-mode-line-format "Gnus: %%b"
903 "The format specification for the category mode line.")
905 (defvar gnus-agent-short-article 100
906 "Articles that have fewer lines than this are short.")
908 (defvar gnus-agent-long-article 200
909 "Articles that have more lines than this are long.")
911 (defvar gnus-agent-low-score 0
912 "Articles that have a score lower than this have a low score.")
914 (defvar gnus-agent-high-score 0
915 "Articles that have a score higher than this have a high score.")
918 ;;; Internal variables.
920 (defvar gnus-category-buffer "*Agent Category*")
922 (defvar gnus-category-line-format-alist
926 (defvar gnus-category-mode-line-format-alist
927 `((?u user-defined ?s)))
929 (defvar gnus-category-line-format-spec nil)
930 (defvar gnus-category-mode-line-format-spec nil)
932 (defvar gnus-category-mode-map nil)
933 (put 'gnus-category-mode 'mode-class 'special)
935 (unless gnus-category-mode-map
936 (setq gnus-category-mode-map (make-sparse-keymap))
937 (suppress-keymap gnus-category-mode-map)
939 (gnus-define-keys gnus-category-mode-map
940 "q" gnus-category-exit
941 "k" gnus-category-kill
942 "c" gnus-category-copy
943 "a" gnus-category-add
944 "p" gnus-category-edit-predicate
945 "g" gnus-category-edit-groups
946 "s" gnus-category-edit-score
947 "l" gnus-category-list
949 "\C-c\C-i" gnus-info-find-node
950 "\C-c\C-b" gnus-bug))
952 (defvar gnus-category-menu-hook nil
953 "*Hook run after the creation of the menu.")
955 (defun gnus-category-make-menu-bar ()
956 (gnus-turn-off-edit-menu 'category)
957 (unless (boundp 'gnus-category-menu)
959 gnus-category-menu gnus-category-mode-map ""
961 ["Add" gnus-category-add t]
962 ["Kill" gnus-category-kill t]
963 ["Copy" gnus-category-copy t]
964 ["Edit predicate" gnus-category-edit-predicate t]
965 ["Edit score" gnus-category-edit-score t]
966 ["Edit groups" gnus-category-edit-groups t]
967 ["Exit" gnus-category-exit t]))
969 (gnus-run-hooks 'gnus-category-menu-hook)))
971 (defun gnus-category-mode ()
972 "Major mode for listing and editing agent categories.
974 All normal editing commands are switched off.
975 \\<gnus-category-mode-map>
976 For more in-depth information on this mode, read the manual
977 (`\\[gnus-info-find-node]').
979 The following commands are available:
981 \\{gnus-category-mode-map}"
983 (when (gnus-visual-p 'category-menu 'menu)
984 (gnus-category-make-menu-bar))
985 (kill-all-local-variables)
986 (gnus-simplify-mode-line)
987 (setq major-mode 'gnus-category-mode)
988 (setq mode-name "Category")
989 (gnus-set-default-directory)
990 (setq mode-line-process nil)
991 (use-local-map gnus-category-mode-map)
992 (buffer-disable-undo (current-buffer))
993 (setq truncate-lines t)
994 (setq buffer-read-only t)
995 (gnus-run-hooks 'gnus-category-mode-hook))
997 (defalias 'gnus-category-position-point 'gnus-goto-colon)
999 (defun gnus-category-insert-line (category)
1000 (let* ((name (car category))
1001 (groups (length (cadddr category))))
1003 (gnus-add-text-properties
1007 (eval gnus-category-line-format-spec))
1008 (list 'gnus-category name))))
1010 (defun gnus-enter-category-buffer ()
1011 "Go to the Category buffer."
1013 (gnus-category-setup-buffer)
1014 (gnus-configure-windows 'category)
1015 (gnus-category-prepare))
1017 (defun gnus-category-setup-buffer ()
1018 (unless (get-buffer gnus-category-buffer)
1020 (set-buffer (get-buffer-create gnus-category-buffer))
1021 (gnus-add-current-to-buffer-list)
1022 (gnus-category-mode))))
1024 (defun gnus-category-prepare ()
1025 (gnus-set-format 'category-mode)
1026 (gnus-set-format 'category t)
1027 (let ((alist gnus-category-alist)
1028 (buffer-read-only nil))
1031 (gnus-category-insert-line (pop alist)))
1032 (goto-char (point-min))
1033 (gnus-category-position-point)))
1035 (defun gnus-category-name ()
1036 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1037 (error "No category on the current line")))
1039 (defun gnus-category-read ()
1040 "Read the category alist."
1041 (setq gnus-category-alist
1042 (or (gnus-agent-read-file
1043 (nnheader-concat gnus-agent-directory "lib/categories"))
1044 (list (list 'default 'short nil nil)))))
1046 (defun gnus-category-write ()
1047 "Write the category alist."
1048 (setq gnus-category-predicate-cache nil
1049 gnus-category-group-cache nil)
1050 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1051 (prin1 gnus-category-alist (current-buffer))))
1053 (defun gnus-category-edit-predicate (category)
1054 "Edit the predicate for CATEGORY."
1055 (interactive (list (gnus-category-name)))
1056 (let ((info (assq category gnus-category-alist)))
1058 (cadr info) (format "Editing the predicate for category %s" category)
1059 `(lambda (predicate)
1060 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1061 (gnus-category-write)
1062 (gnus-category-list)))))
1064 (defun gnus-category-edit-score (category)
1065 "Edit the score expression for CATEGORY."
1066 (interactive (list (gnus-category-name)))
1067 (let ((info (assq category gnus-category-alist)))
1070 (format "Editing the score expression for category %s" category)
1072 (setf (caddr (assq ',category gnus-category-alist)) groups)
1073 (gnus-category-write)
1074 (gnus-category-list)))))
1076 (defun gnus-category-edit-groups (category)
1077 "Edit the group list for CATEGORY."
1078 (interactive (list (gnus-category-name)))
1079 (let ((info (assq category gnus-category-alist)))
1081 (cadddr info) (format "Editing the group list for category %s" category)
1083 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1084 (gnus-category-write)
1085 (gnus-category-list)))))
1087 (defun gnus-category-kill (category)
1088 "Kill the current category."
1089 (interactive (list (gnus-category-name)))
1090 (let ((info (assq category gnus-category-alist))
1091 (buffer-read-only nil))
1093 (gnus-category-write)
1094 (setq gnus-category-alist (delq info gnus-category-alist))))
1096 (defun gnus-category-copy (category to)
1097 "Copy the current category."
1098 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1099 (let ((info (assq category gnus-category-alist)))
1100 (push (list to (gnus-copy-sequence (cadr info))
1101 (gnus-copy-sequence (caddr info)) nil)
1102 gnus-category-alist)
1103 (gnus-category-write)
1104 (gnus-category-list)))
1106 (defun gnus-category-add (category)
1107 "Create a new category."
1108 (interactive "SCategory name: ")
1109 (when (assq category gnus-category-alist)
1110 (error "Category %s already exists" category))
1111 (push (list category 'true nil nil)
1112 gnus-category-alist)
1113 (gnus-category-write)
1114 (gnus-category-list))
1116 (defun gnus-category-list ()
1117 "List all categories."
1119 (gnus-category-prepare))
1121 (defun gnus-category-exit ()
1122 "Return to the group buffer."
1124 (kill-buffer (current-buffer))
1125 (gnus-configure-windows 'group t))
1127 ;; To avoid having 8-bit characters in the source file.
1128 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1130 (defvar gnus-category-predicate-alist
1131 '((spam . gnus-agent-spam-p)
1132 (short . gnus-agent-short-p)
1133 (long . gnus-agent-long-p)
1134 (low . gnus-agent-low-scored-p)
1135 (high . gnus-agent-high-scored-p)
1136 (true . gnus-agent-true)
1137 (false . gnus-agent-false))
1138 "Mapping from short score predicate symbols to predicate functions.")
1140 (defun gnus-agent-spam-p ()
1141 "Say whether an article is spam or not."
1142 (unless gnus-agent-spam-hashtb
1143 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1144 (if (not (equal (mail-header-references gnus-headers) ""))
1146 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1148 (gnus-gethash string gnus-agent-spam-hashtb)
1149 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1151 (defun gnus-agent-short-p ()
1152 "Say whether an article is short or not."
1153 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1155 (defun gnus-agent-long-p ()
1156 "Say whether an article is long or not."
1157 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1159 (defun gnus-agent-low-scored-p ()
1160 "Say whether an article has a low score or not."
1161 (< gnus-score gnus-agent-low-score))
1163 (defun gnus-agent-high-scored-p ()
1164 "Say whether an article has a high score or not."
1165 (> gnus-score gnus-agent-high-score))
1167 (defun gnus-category-make-function (cat)
1168 "Make a function from category CAT."
1169 `(lambda () ,(gnus-category-make-function-1 cat)))
1171 (defun gnus-agent-true ()
1175 (defun gnus-agent-false ()
1179 (defun gnus-category-make-function-1 (cat)
1180 "Make a function from category CAT."
1182 ;; Functions are just returned as is.
1184 (gnus-functionp cat))
1185 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1187 ;; More complex category.
1190 ((memq (car cat) '(& and))
1192 ((memq (car cat) '(| or))
1194 ((memq (car cat) gnus-category-not)
1196 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1198 (error "Unknown category type: %s" cat))))
1200 (defun gnus-get-predicate (predicate)
1201 "Return the predicate for CATEGORY."
1202 (or (cdr (assoc predicate gnus-category-predicate-cache))
1203 (cdar (push (cons predicate
1204 (gnus-category-make-function predicate))
1205 gnus-category-predicate-cache))))
1207 (defun gnus-group-category (group)
1208 "Return the category GROUP belongs to."
1209 (unless gnus-category-group-cache
1210 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1211 (let ((cs gnus-category-alist)
1213 (while (setq cat (pop cs))
1214 (setq groups (cadddr cat))
1216 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1217 (or (gnus-gethash group gnus-category-group-cache)
1218 (assq 'default gnus-category-alist)))
1220 (defun gnus-agent-expire ()
1221 "Expire all old articles."
1223 (let ((methods gnus-agent-covered-methods)
1224 (day (- (gnus-time-to-day (current-time)) gnus-agent-expire-days))
1225 (expiry-hashtb (gnus-make-hashtable 1023))
1226 gnus-command-method sym group articles
1227 history overview file histories elem art nov-file low info
1228 unreads marked article)
1230 (setq overview (get-buffer-create " *expire overview*"))
1231 (while (setq gnus-command-method (pop methods))
1232 (gnus-agent-open-history)
1234 (setq gnus-agent-current-history
1235 (setq history (gnus-agent-history-buffer))))
1236 (unless (zerop (buffer-size))
1237 (goto-char (point-min))
1239 (skip-chars-forward "^\t")
1240 (if (> (read (current-buffer)) day)
1241 ;; New article; we don't expire it.
1243 ;; Old article. Schedule it for possible nuking.
1245 (setq sym (let ((obarray expiry-hashtb))
1246 (read (current-buffer))))
1248 (set sym (cons (cons (read (current-buffer)) (point))
1249 (symbol-value sym)))
1250 (set sym (list (cons (read (current-buffer)) (point)))))
1251 (skip-chars-forward " "))
1253 ;; We now have all articles that can possibly be expired.
1256 (setq group (symbol-name sym)
1257 articles (sort (symbol-value sym) 'car-less-than-car)
1258 low (car (gnus-active group))
1259 info (gnus-get-info group)
1260 unreads (ignore-errors (gnus-list-of-unread-articles group))
1261 marked (nconc (gnus-uncompress-range
1262 (cdr (assq 'ticked (gnus-info-marks info))))
1263 (gnus-uncompress-range
1265 (gnus-info-marks info)))))
1266 nov-file (gnus-agent-article-name ".overview" group))
1267 (gnus-message 5 "Expiring articles in %s" group)
1268 (set-buffer overview)
1270 (when (file-exists-p nov-file)
1271 (insert-file-contents nov-file))
1272 (goto-char (point-min))
1273 (while (setq elem (pop articles))
1274 (setq article (car elem))
1275 (when (or (null low)
1277 gnus-agent-expire-all
1278 (and (not (memq article unreads))
1279 (not (memq article marked))))
1280 ;; Find and nuke the NOV line.
1281 (while (and (not (eobp))
1282 (< (setq art (read (current-buffer))) article))
1288 ;; Nuke the article.
1289 (when (file-exists-p (setq file (gnus-agent-article-name
1290 (number-to-string article)
1293 ;; Schedule the history line for nuking.
1294 (push (cdr elem) histories)))
1295 (write-region (point-min) (point-max) nov-file nil 'silent))
1297 (set-buffer history)
1298 (setq histories (nreverse (sort histories '<)))
1300 (goto-char (pop histories))
1302 (gnus-agent-save-history)
1303 (gnus-agent-close-history))
1304 (gnus-message 4 "Expiry...done")))))
1307 (defun gnus-agent-batch ()
1309 (let ((init-file-user "")
1310 (gnus-always-read-dribble-file t))
1312 (gnus-group-send-drafts)
1313 (gnus-agent-fetch-session))
1315 (provide 'gnus-agent)
1317 ;;; gnus-agent.el ends here