1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
34 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
35 "Where the Gnus agent will store its files."
39 (defcustom gnus-agent-plugged-hook nil
40 "Hook run when plugging into the network."
44 (defcustom gnus-agent-unplugged-hook nil
45 "Hook run when unplugging from the network."
49 ;;; Internal variables
51 (defvar gnus-agent-history-buffers nil)
52 (defvar gnus-agent-buffer-alist nil)
53 (defvar gnus-agent-article-alist nil)
54 (defvar gnus-agent-group-alist nil)
55 (defvar gnus-agent-covered-methods nil)
56 (defvar gnus-category-alist nil)
57 (defvar gnus-agent-current-history nil)
58 (defvar gnus-agent-overview-buffer nil)
59 (defvar gnus-category-predicate-cache nil)
60 (defvar gnus-category-group-cache nil)
61 (defvar gnus-agent-spam-hashtb nil)
62 (defvar gnus-agent-file-name nil)
63 (defvar gnus-agent-send-mail-function nil)
64 (defvar gnus-agent-article-file-coding-system 'no-conversion)
74 (defun gnus-open-agent ()
76 (gnus-agent-read-servers)
78 (setq gnus-agent-overview-buffer
79 (get-buffer-create " *Gnus agent overview*"))
80 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
81 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
82 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
84 (gnus-add-shutdown 'gnus-close-agent 'gnus)
86 (defun gnus-close-agent ()
87 (setq gnus-agent-covered-methods nil
88 gnus-category-predicate-cache nil
89 gnus-category-group-cache nil
90 gnus-agent-spam-hashtb nil)
91 (gnus-kill-buffer gnus-agent-overview-buffer))
97 (defun gnus-agent-read-file (file)
98 "Load FILE and do a `read' there."
99 (nnheader-temp-write nil
101 (insert-file-contents file)
102 (goto-char (point-min))
103 (read (current-buffer)))))
105 (defsubst gnus-agent-method ()
106 (concat (symbol-name (car gnus-command-method)) "/"
107 (if (equal (cadr gnus-command-method) "")
109 (cadr gnus-command-method))))
111 (defsubst gnus-agent-directory ()
112 "Path of the Gnus agent directory."
113 (nnheader-concat gnus-agent-directory (gnus-agent-method) "/"))
115 (defun gnus-agent-lib-file (file)
116 "The full path of the Gnus agent library FILE."
117 (concat (gnus-agent-directory) "agent.lib/" file))
119 ;;; Fetching setup functions.
121 (defun gnus-agent-start-fetch ()
122 "Initialize data structures for efficient fetching."
123 (gnus-agent-open-history)
124 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
126 (defun gnus-agent-stop-fetch ()
127 "Save all data structures and clean up."
128 (gnus-agent-save-history)
129 (gnus-agent-close-history)
130 (setq gnus-agent-spam-hashtb nil)
132 (set-buffer nntp-server-buffer)
135 (defmacro gnus-agent-with-fetch (&rest forms)
139 (gnus-agent-start-fetch)
141 (gnus-agent-stop-fetch)))
143 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
144 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
150 (defvar gnus-agent-mode-hook nil
151 "Hook run when installing agent mode.")
153 (defvar gnus-agent-mode nil)
154 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
156 (defun gnus-agent-mode ()
157 "Minor mode for providing a agent support in Gnus buffers."
158 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
159 (symbol-name major-mode))
160 (match-string 1 (symbol-name major-mode))))
161 (mode (intern (format "gnus-agent-%s-mode" buffer))))
162 (set (make-local-variable 'gnus-agent-mode) t)
164 (set (make-local-variable mode) t)
166 (when (gnus-visual-p 'agent-menu 'menu)
167 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
168 (unless (assq 'gnus-agent-mode minor-mode-alist)
169 (push gnus-agent-mode-status minor-mode-alist))
170 (unless (assq mode minor-mode-map-alist)
171 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
173 minor-mode-map-alist))
174 (gnus-agent-toggle-plugged gnus-plugged)
175 (run-hooks 'gnus-agent-mode-hook)))
177 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
178 (gnus-define-keys gnus-agent-group-mode-map
179 "Ju" gnus-agent-fetch-group
180 "Jc" gnus-enter-category-buffer
181 "Jj" gnus-agent-toggle-plugged
182 "Js" gnus-agent-fetch-session
183 "JS" gnus-group-send-drafts
184 "Ja" gnus-agent-add-group)
186 (defun gnus-agent-group-make-menu-bar ()
187 (unless (boundp 'gnus-agent-group-menu)
189 gnus-agent-group-menu gnus-agent-group-mode-map ""
191 ["Toggle plugged" gnus-agent-toggle-plugged t]
192 ["List categories" gnus-enter-category-buffer t]
193 ["Send drafts" gnus-group-send-drafts gnus-plugged]
195 ["All" gnus-agent-fetch-session gnus-plugged]
196 ["Group" gnus-agent-fetch-group gnus-plugged])))))
198 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
199 (gnus-define-keys gnus-agent-summary-mode-map
200 "Jj" gnus-agent-toggle-plugged
201 "J#" gnus-agent-mark-article
202 "J\M-#" gnus-agent-unmark-article
203 "@" gnus-agent-toggle-mark
204 "Jc" gnus-agent-catchup)
206 (defun gnus-agent-summary-make-menu-bar ()
207 (unless (boundp 'gnus-agent-summary-menu)
209 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
211 ["Toggle plugged" gnus-agent-toggle-plugged t]
212 ["Mark as downloadable" gnus-agent-mark-article t]
213 ["Unmark as downloadable" gnus-agent-unmark-article t]
214 ["Toggle mark" gnus-agent-toggle-mark t]
215 ["Catchup undownloaded" gnus-agent-catchup t]))))
217 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
218 (gnus-define-keys gnus-agent-server-mode-map
219 "Jj" gnus-agent-toggle-plugged
220 "Ja" gnus-agent-add-server
221 "Jr" gnus-agent-remove-server)
223 (defun gnus-agent-server-make-menu-bar ()
224 (unless (boundp 'gnus-agent-server-menu)
226 gnus-agent-server-menu gnus-agent-server-mode-map ""
228 ["Toggle plugged" gnus-agent-toggle-plugged t]
229 ["Add" gnus-agent-add-server t]
230 ["Remove" gnus-agent-remove-server t]))))
232 (defun gnus-agent-toggle-plugged (plugged)
233 "Toggle whether Gnus is unplugged or not."
234 (interactive (list (not gnus-plugged)))
237 (run-hooks 'gnus-agent-plugged-hook)
238 (setcar (cdr gnus-agent-mode-status) " Plugged"))
239 (gnus-agent-close-connections)
240 (run-hooks 'gnus-agent-unplugged-hook)
241 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
242 (setq gnus-plugged plugged)
243 (set-buffer-modified-p t))
245 (defun gnus-agent-close-connections ()
246 "Close all methods covered by the Gnus agent."
247 (let ((methods gnus-agent-covered-methods))
249 (gnus-close-server (pop methods)))))
252 (defun gnus-unplugged ()
253 "Start Gnus unplugged."
255 (setq gnus-plugged nil)
259 (defun gnus-agentize ()
260 "Allow Gnus to be an offline newsreader.
261 The normal usage of this command is to put the following as the
262 last form in your `.gnus.el' file:
266 This will modify the `gnus-before-startup-hook', `gnus-post-method',
267 and `message-send-mail-function' variables, and install the Gnus
268 agent minor mode in all Gnus buffers."
271 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
272 (unless gnus-agent-send-mail-function
273 (setq gnus-agent-send-mail-function message-send-mail-function
274 message-send-mail-function 'gnus-agent-send-mail))
275 (unless gnus-agent-covered-methods
276 (setq gnus-agent-covered-methods (list gnus-select-method))))
278 (defun gnus-agent-queue-setup ()
279 "Make sure the queue group exists."
280 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
281 (gnus-request-create-group "queue" '(nndraft ""))
282 (let ((gnus-level-default-subscribed 1))
283 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
284 (gnus-group-set-parameter
285 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
287 (defun gnus-agent-send-mail ()
289 (funcall gnus-agent-send-mail-function)
290 (goto-char (point-min))
292 (concat "^" (regexp-quote mail-header-separator) "\n"))
294 (gnus-request-accept-article "nndraft:queue")))
297 ;;; Group mode commands
300 (defun gnus-agent-fetch-group (group)
301 "Put all new articles in GROUP into the agent."
302 (interactive (list (gnus-group-group-name)))
304 (error "No group on the current line"))
305 (let ((gnus-command-method (gnus-find-method-for-group group)))
306 (gnus-agent-with-fetch
307 (gnus-agent-fetch-group-1 group gnus-command-method)
308 (gnus-message 5 "Fetching %s...done" group))))
310 (defun gnus-agent-add-group (category arg)
311 "Add the current group to an agent category."
317 (mapcar (lambda (cat) (list (symbol-name (car cat))))
321 (let ((cat (assq category gnus-category-alist))
323 (gnus-group-iterate arg
325 (when (cadddr (setq c (gnus-group-category group)))
326 (setf (cadddr c) (delete group (cadddr c))))
327 (push group groups)))
328 (setf (cadddr cat) (nconc (cadddr cat) groups))
329 (gnus-category-write)))
332 ;;; Server mode commands
335 (defun gnus-agent-add-server (server)
336 "Enroll SERVER in the agent program."
337 (interactive (list (gnus-server-server-name)))
339 (error "No server on the current line"))
340 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
341 (when (member method gnus-agent-covered-methods)
342 (error "Server already in the agent program"))
343 (push method gnus-agent-covered-methods)
344 (gnus-agent-write-servers)
345 (message "Entered %s into the agent" server)))
347 (defun gnus-agent-remove-server (server)
348 "Remove SERVER from the agent program."
349 (interactive (list (gnus-server-server-name)))
351 (error "No server on the current line"))
352 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
353 (unless (member method gnus-agent-covered-methods)
354 (error "Server not in the agent program"))
355 (setq gnus-agent-covered-methods
356 (delete method gnus-agent-covered-methods))
357 (gnus-agent-write-servers)
358 (message "Removed %s from the agent" server)))
360 (defun gnus-agent-read-servers ()
361 "Read the alist of covered servers."
362 (setq gnus-agent-covered-methods
363 (gnus-agent-read-file
364 (nnheader-concat gnus-agent-directory "lib/servers"))))
366 (defun gnus-agent-write-servers ()
367 "Write the alist of covered servers."
368 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
369 (prin1 gnus-agent-covered-methods (current-buffer))))
375 (defun gnus-agent-mark-article (n &optional unmark)
376 "Mark the next N articles as downloadable.
377 If N is negative, mark backward instead. If UNMARK is non-nil, remove
378 the mark instead. The difference between N and the actual number of
379 articles marked is returned."
381 (gnus-set-global-variables)
382 (let ((backward (< n 0))
387 (gnus-summary-set-agent-mark
388 (gnus-summary-article-number) unmark)
389 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
392 (gnus-message 7 "No more articles"))
393 (gnus-summary-recenter)
394 (gnus-summary-position-point)
397 (defun gnus-agent-unmark-article (n)
398 "Remove the downloadable mark from the next N articles.
399 If N is negative, unmark backward instead. The difference between N and
400 the actual number of articles unmarked is returned."
402 (gnus-set-global-variables)
403 (gnus-agent-mark-article n t))
405 (defun gnus-agent-toggle-mark (n)
406 "Toggle the downloadable mark from the next N articles.
407 If N is negative, toggle backward instead. The difference between N and
408 the actual number of articles toggled is returned."
410 (gnus-set-global-variables)
411 (gnus-agent-mark-article n 'toggle))
413 (defun gnus-summary-set-agent-mark (article &optional unmark)
414 "Mark ARTICLE as downloadable."
415 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
416 (memq article gnus-newsgroup-downloadable)
418 (setq gnus-newsgroup-downloadable
419 (delq article gnus-newsgroup-downloadable))
421 (push article gnus-newsgroup-downloadable))
422 (gnus-summary-update-mark
423 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
426 (defun gnus-agent-get-undownloaded-list ()
427 "Mark all unfetched articles as read."
428 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
429 (when (and (not gnus-plugged)
430 (gnus-agent-method-p gnus-command-method))
431 (gnus-agent-load-alist gnus-newsgroup-name)
432 (let ((articles gnus-newsgroup-unreads)
434 (while (setq article (pop articles))
435 (unless (or (cdr (assq article gnus-agent-article-alist))
436 (memq article gnus-newsgroup-downloadable))
437 (push article gnus-newsgroup-undownloaded)))))))
439 (defun gnus-agent-catchup ()
440 "Mark all undownloaded articles as read."
443 (while gnus-newsgroup-undownloaded
444 (gnus-summary-mark-article
445 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
446 (gnus-summary-position-point))
449 ;;; Internal functions
452 (defun gnus-agent-save-active (method)
453 (when (gnus-agent-method-p method)
454 (let* ((gnus-command-method method)
455 (file (gnus-agent-lib-file "active")))
456 (gnus-make-directory (file-name-directory file))
457 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
458 (write-region (point-min) (point-max) file nil 'silent))
459 (when (file-exists-p (gnus-agent-lib-file "groups"))
460 (delete-file (gnus-agent-lib-file "groups"))))))
462 (defun gnus-agent-save-groups (method)
463 (let* ((gnus-command-method method)
464 (file (gnus-agent-lib-file "groups")))
465 (gnus-make-directory (file-name-directory file))
466 (write-region (point-min) (point-max) file nil 'silent))
467 (when (file-exists-p (gnus-agent-lib-file "active"))
468 (delete-file (gnus-agent-lib-file "active"))))
470 (defun gnus-agent-group-path (group)
471 "Translate GROUP into a path."
472 (nnheader-translate-file-chars
473 (nnheader-replace-chars-in-string group ?. ?/)))
477 (defun gnus-agent-method-p (method)
478 "Say whether METHOD is covered by the agent."
479 (member method gnus-agent-covered-methods))
481 (defun gnus-agent-get-function (method)
482 (if (and (not gnus-plugged)
483 (gnus-agent-method-p method))
489 ;;; History functions
491 (defun gnus-agent-history-buffer ()
492 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
494 (defun gnus-agent-open-history ()
496 (push (cons (gnus-agent-method)
497 (set-buffer (get-buffer-create
498 (format " *Gnus agent %s history*"
499 (gnus-agent-method)))))
500 gnus-agent-history-buffers)
503 (let ((file (gnus-agent-lib-file "history")))
504 (when (file-exists-p file)
506 (set (make-local-variable 'gnus-agent-file-name) file))))
508 (defun gnus-agent-save-history ()
510 (set-buffer gnus-agent-current-history)
511 (gnus-make-directory (file-name-directory gnus-agent-file-name))
512 (write-region (1+ (point-min)) (point-max)
513 gnus-agent-file-name nil 'silent)))
515 (defun gnus-agent-close-history ()
516 (when (gnus-buffer-live-p gnus-agent-current-history)
517 (kill-buffer gnus-agent-current-history)
518 (setq gnus-agent-history-buffers
519 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
520 gnus-agent-history-buffers))))
522 (defun gnus-agent-enter-history (id group-arts date)
524 (set-buffer gnus-agent-current-history)
525 (goto-char (point-max))
526 (insert id "\t" (number-to-string date) "\t")
528 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
532 (defun gnus-agent-article-in-history-p (id)
534 (set-buffer (gnus-agent-history-buffer))
535 (goto-char (point-min))
536 (search-forward (concat "\n" id "\t") nil t)))
538 (defun gnus-agent-history-path (id)
540 (set-buffer (gnus-agent-history-buffer))
541 (goto-char (point-min))
542 (when (search-forward (concat "\n" id "\t") nil t)
543 (let ((method (gnus-agent-method)))
545 (while (not (numberp (setq group (read (current-buffer)))))
546 (push (concat method "/" group) paths))
547 (nreverse paths))))))
553 (defun gnus-agent-fetch-articles (group articles)
554 "Fetch ARTICLES from GROUP and put them into the agent."
556 ;; Prune off articles that we have already fetched.
558 (cdr (assq (car articles) gnus-agent-article-alist)))
560 (let ((arts articles))
562 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
563 (setcdr arts (cddr arts))
564 (setq arts (cdr arts)))))
567 (gnus-agent-directory)
568 (gnus-agent-group-path group) "/"))
569 (date (gnus-time-to-day (current-time)))
571 pos alists crosses id elem)
572 (gnus-make-directory dir)
573 (gnus-message 7 "Fetching articles for %s..." group)
574 ;; Fetch the articles from the backend.
575 (if (gnus-check-backend-function 'retrieve-articles group)
576 (setq pos (gnus-retrieve-articles articles group))
577 (nnheader-temp-write nil
578 (let ((buf (current-buffer))
580 (while (setq article (pop articles))
581 (when (gnus-request-article article group)
582 (goto-char (point-max))
583 (push (cons article (point)) pos)
584 (insert-buffer-substring nntp-server-buffer)))
585 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
586 (setq pos (nreverse pos)))))
587 ;; Then save these articles into the agent.
589 (set-buffer nntp-server-buffer)
591 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
592 (goto-char (point-min))
593 (when (search-forward "\n\n" nil t)
594 (when (search-backward "\nXrefs: " nil t)
595 ;; Handle crossposting.
596 (skip-chars-forward "^ ")
597 (skip-chars-forward " ")
599 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
600 (push (cons (buffer-substring (match-beginning 1)
602 (buffer-substring (match-beginning 2)
605 (goto-char (match-end 0)))
606 (gnus-agent-crosspost crosses (caar pos))))
607 (goto-char (point-min))
608 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
609 (setq id "No-Message-ID-in-article")
610 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
611 (write-region (point-min) (point-max)
612 (concat dir (number-to-string (caar pos)))
614 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
616 (gnus-agent-enter-history
617 id (or crosses (list (cons group (caar pos)))) date)
620 (gnus-agent-save-alist group)))))
622 (defun gnus-agent-crosspost (crosses article)
623 (let (gnus-agent-article-alist group alist beg end)
625 (set-buffer gnus-agent-overview-buffer)
626 (when (nnheader-find-nov-line article)
629 (setq end (progn (forward-line 1) (point)))))
631 (setq group (caar crosses))
632 (unless (setq alist (assoc group gnus-agent-group-alist))
633 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
634 gnus-agent-group-alist))
635 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
637 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
639 (when (= (point-max) (point-min))
640 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
642 (insert-file-contents
643 (gnus-agent-article-name ".overview" group))))
644 (nnheader-find-nov-line (string-to-number (cdar crosses)))
645 (insert (string-to-number (cdar crosses)))
646 (insert-buffer-substring gnus-agent-overview-buffer beg end))
649 (defun gnus-agent-flush-cache ()
651 (while gnus-agent-buffer-alist
652 (set-buffer (cdar gnus-agent-buffer-alist))
653 (write-region (point-min) (point-max)
654 (gnus-agent-article-name ".overview"
655 (caar gnus-agent-buffer-alist))
657 (pop gnus-agent-buffer-alist))
658 (while gnus-agent-group-alist
659 (nnheader-temp-write (caar gnus-agent-group-alist)
660 (princ (cdar gnus-agent-group-alist))
662 (pop gnus-agent-group-alist))))
664 (defun gnus-agent-fetch-headers (group articles &optional force)
665 (gnus-agent-load-alist group)
666 ;; Find out what headers we need to retrieve.
669 (assq (car articles) gnus-agent-article-alist))
671 (let ((arts articles))
673 (if (assq (cadr arts) gnus-agent-article-alist)
674 (setcdr arts (cddr arts))
675 (setq arts (cdr arts)))))
678 (gnus-message 7 "Fetching headers for %s..." group)
680 (set-buffer nntp-server-buffer)
681 (unless (eq 'nov (gnus-retrieve-headers articles group))
682 (nnvirtual-convert-headers))
683 ;; Save these headers for later processing.
684 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
687 (setq file (gnus-agent-article-name ".overview" group)))
688 (gnus-agent-braid-nov group articles file))
689 (gnus-make-directory (file-name-directory file))
690 (write-region (point-min) (point-max) file nil 'silent)
691 (gnus-agent-save-alist group articles nil))
694 (defsubst gnus-agent-copy-nov-line (article)
696 (set-buffer gnus-agent-overview-buffer)
698 (if (eq article (read (current-buffer)))
699 (setq e (progn (forward-line 1) (point)))
701 (set-buffer nntp-server-buffer)
702 (insert-buffer-substring gnus-agent-overview-buffer b e)))
704 (defun gnus-agent-braid-nov (group articles file)
706 (set-buffer gnus-agent-overview-buffer)
707 (goto-char (point-min))
708 (set-buffer nntp-server-buffer)
710 (insert-file-contents file)
711 (goto-char (point-min))
712 (if (or (= (point-min) (point-max))
715 (< (read (current-buffer)) (car articles))))
716 ;; We have only headers that are after the older headers,
717 ;; so we just append them.
719 (goto-char (point-max))
720 (insert-buffer-substring gnus-agent-overview-buffer))
721 ;; We do it the hard way.
722 (nnheader-find-nov-line (car articles))
723 (gnus-agent-copy-nov-line (car articles))
727 (while (and (not (eobp))
728 (< (read (current-buffer)) (car articles)))
732 (gnus-agent-copy-nov-line (car articles))
733 (setq articles (cdr articles))))
736 (set-buffer gnus-agent-overview-buffer)
739 (set-buffer nntp-server-buffer)
740 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
742 (defun gnus-agent-load-alist (group &optional dir)
743 "Load the article-state alist for GROUP."
744 (setq gnus-agent-article-alist
745 (gnus-agent-read-file
747 (concat dir ".agentview")
748 (gnus-agent-article-name ".agentview" group)))))
750 (defun gnus-agent-save-alist (group &optional articles state dir)
751 "Load the article-state alist for GROUP."
752 (nnheader-temp-write (if dir
753 (concat dir ".agentview")
754 (gnus-agent-article-name ".agentview" group))
755 (princ (setq gnus-agent-article-alist
756 (nconc gnus-agent-article-alist
757 (mapcar (lambda (article) (cons article state))
762 (defun gnus-agent-article-name (article group)
763 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
764 (if (stringp article) article (string-to-number article))))
767 (defun gnus-agent-batch-fetch ()
768 "Start Gnus and fetch session."
771 (gnus-agent-fetch-session)
774 (defun gnus-agent-fetch-session ()
775 "Fetch all articles and headers that are eligible for fetching."
777 (unless gnus-agent-covered-methods
778 (error "No servers are covered by the Gnus agent"))
780 (error "Can't fetch articles while Gnus is unplugged"))
781 (let ((methods gnus-agent-covered-methods)
782 groups group gnus-command-method)
785 (setq gnus-command-method (car methods)
786 groups (gnus-groups-from-server (pop methods)))
787 (gnus-agent-with-fetch
788 (while (setq group (pop groups))
789 (gnus-agent-fetch-group-1 group gnus-command-method))))
790 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
792 (defun gnus-agent-fetch-group-1 (group method)
794 (let ((gnus-command-method method)
795 gnus-newsgroup-dependencies gnus-newsgroup-headers
796 gnus-newsgroup-scored gnus-headers gnus-score
797 gnus-use-cache articles score arts
798 category predicate info marks score-param)
800 (when (and (setq articles (gnus-list-of-unread-articles group))
801 (gnus-agent-fetch-headers group articles))
802 ;; Parse them and see which articles we want to fetch.
803 (setq gnus-newsgroup-dependencies
804 (make-vector (length articles) 0))
805 (setq gnus-newsgroup-headers
806 (gnus-get-newsgroup-headers-xover articles nil nil group))
807 (setq category (gnus-group-category group))
810 (or (gnus-group-get-parameter group 'agent-predicate)
813 (or (gnus-group-get-parameter group 'agent-score)
816 (gnus-score-headers (list (list score-param))))
818 (while (setq gnus-headers (pop gnus-newsgroup-headers))
820 (or (cdr (assq (mail-header-number gnus-headers)
821 gnus-newsgroup-scored))
822 gnus-summary-default-score))
823 (when (funcall predicate)
824 (push (mail-header-number gnus-headers)
826 ;; Fetch the articles.
828 (gnus-agent-fetch-articles group arts)))
829 ;; Perhaps we have some additional articles to fetch.
830 (setq arts (assq 'download (gnus-info-marks
831 (setq info (gnus-get-info group)))))
833 (gnus-agent-fetch-articles
834 group (gnus-uncompress-range (cdr arts)))
835 (setq marks (delq arts (gnus-info-marks info)))
836 (gnus-info-set-marks info marks))))
839 ;;; Agent Category Mode
842 (defvar gnus-category-mode-hook nil
843 "Hook run in `gnus-category-mode' buffers.")
845 (defvar gnus-category-line-format " %(%20c%): %g\n"
846 "Format of category lines.")
848 (defvar gnus-category-mode-line-format "Gnus: %%b"
849 "The format specification for the category mode line.")
851 (defvar gnus-agent-short-article 100
852 "Articles that have fewer lines than this are short.")
854 (defvar gnus-agent-long-article 200
855 "Articles that have more lines than this are long.")
857 (defvar gnus-agent-low-score 0
858 "Articles that have a score lower than this have a low score.")
860 (defvar gnus-agent-high-score 0
861 "Articles that have a score higher than this have a high score.")
864 ;;; Internal variables.
866 (defvar gnus-category-buffer "*Agent Category*")
868 (defvar gnus-category-line-format-alist
872 (defvar gnus-category-mode-line-format-alist
873 `((?u user-defined ?s)))
875 (defvar gnus-category-line-format-spec nil)
876 (defvar gnus-category-mode-line-format-spec nil)
878 (defvar gnus-category-mode-map nil)
879 (put 'gnus-category-mode 'mode-class 'special)
881 (unless gnus-category-mode-map
882 (setq gnus-category-mode-map (make-sparse-keymap))
883 (suppress-keymap gnus-category-mode-map)
885 (gnus-define-keys gnus-category-mode-map
886 "q" gnus-category-exit
887 "k" gnus-category-kill
888 "c" gnus-category-copy
889 "a" gnus-category-add
890 "p" gnus-category-edit-predicate
891 "g" gnus-category-edit-groups
892 "s" gnus-category-edit-score
893 "l" gnus-category-list
895 "\C-c\C-i" gnus-info-find-node
896 "\C-c\C-b" gnus-bug))
898 (defvar gnus-category-menu-hook nil
899 "*Hook run after the creation of the menu.")
901 (defun gnus-category-make-menu-bar ()
902 (gnus-turn-off-edit-menu 'category)
903 (unless (boundp 'gnus-category-menu)
905 gnus-category-menu gnus-category-mode-map ""
907 ["Add" gnus-category-add t]
908 ["Kill" gnus-category-kill t]
909 ["Copy" gnus-category-copy t]
910 ["Edit predicate" gnus-category-edit-predicate t]
911 ["Edit score" gnus-category-edit-score t]
912 ["Edit groups" gnus-category-edit-groups t]
913 ["Exit" gnus-category-exit t]))
915 (run-hooks 'gnus-category-menu-hook)))
917 (defun gnus-category-mode ()
918 "Major mode for listing and editing agent categories.
920 All normal editing commands are switched off.
921 \\<gnus-category-mode-map>
922 For more in-depth information on this mode, read the manual
923 (`\\[gnus-info-find-node]').
925 The following commands are available:
927 \\{gnus-category-mode-map}"
929 (when (gnus-visual-p 'category-menu 'menu)
930 (gnus-category-make-menu-bar))
931 (kill-all-local-variables)
932 (gnus-simplify-mode-line)
933 (setq major-mode 'gnus-category-mode)
934 (setq mode-name "Category")
935 (gnus-set-default-directory)
936 (setq mode-line-process nil)
937 (use-local-map gnus-category-mode-map)
938 (buffer-disable-undo (current-buffer))
939 (setq truncate-lines t)
940 (setq buffer-read-only t)
941 (run-hooks 'gnus-category-mode-hook))
943 (defalias 'gnus-category-position-point 'gnus-goto-colon)
945 (defun gnus-category-insert-line (category)
946 (let* ((name (car category))
947 (groups (length (cadddr category))))
949 (gnus-add-text-properties
953 (eval gnus-category-line-format-spec))
954 (list 'gnus-category name))))
956 (defun gnus-enter-category-buffer ()
957 "Go to the Category buffer."
959 (gnus-category-setup-buffer)
960 (gnus-configure-windows 'category)
961 (gnus-category-prepare))
963 (defun gnus-category-setup-buffer ()
964 (unless (get-buffer gnus-category-buffer)
966 (set-buffer (get-buffer-create gnus-category-buffer))
967 (gnus-add-current-to-buffer-list)
968 (gnus-category-mode))))
970 (defun gnus-category-prepare ()
971 (gnus-set-format 'category-mode)
972 (gnus-set-format 'category t)
973 (let ((alist gnus-category-alist)
974 (buffer-read-only nil))
977 (gnus-category-insert-line (pop alist)))
978 (goto-char (point-min))
979 (gnus-category-position-point)))
981 (defun gnus-category-name ()
982 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
983 (error "No category on the current line")))
985 (defun gnus-category-read ()
986 "Read the category alist."
987 (setq gnus-category-alist
988 (or (gnus-agent-read-file
989 (nnheader-concat gnus-agent-directory "lib/categories"))
990 (list (list 'default 'true nil nil)))))
992 (defun gnus-category-write ()
993 "Write the category alist."
994 (setq gnus-category-predicate-cache nil
995 gnus-category-group-cache nil)
996 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
997 (prin1 gnus-category-alist (current-buffer))))
999 (defun gnus-category-edit-predicate (category)
1000 "Edit the predicate for CATEGORY."
1001 (interactive (list (gnus-category-name)))
1002 (let ((info (assq category gnus-category-alist)))
1004 (cadr info) (format "Editing the predicate for category %s" category)
1005 `(lambda (predicate)
1006 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1007 (gnus-category-write)
1008 (gnus-category-list)))))
1010 (defun gnus-category-edit-score (category)
1011 "Edit the score expression for CATEGORY."
1012 (interactive (list (gnus-category-name)))
1013 (let ((info (assq category gnus-category-alist)))
1016 (format "Editing the score expression for category %s" category)
1018 (setf (caddr (assq ',category gnus-category-alist)) groups)
1019 (gnus-category-write)
1020 (gnus-category-list)))))
1022 (defun gnus-category-edit-groups (category)
1023 "Edit the group list for CATEGORY."
1024 (interactive (list (gnus-category-name)))
1025 (let ((info (assq category gnus-category-alist)))
1027 (cadddr info) (format "Editing the group list for category %s" category)
1029 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1030 (gnus-category-write)
1031 (gnus-category-list)))))
1033 (defun gnus-category-kill (category)
1034 "Kill the current category."
1035 (interactive (list (gnus-category-name)))
1036 (let ((info (assq category gnus-category-alist))
1037 (buffer-read-only nil))
1039 (gnus-category-write)
1040 (setq gnus-category-alist (delq info gnus-category-alist))))
1042 (defun gnus-category-copy (category to)
1043 "Copy the current category."
1044 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1045 (let ((info (assq category gnus-category-alist)))
1046 (push (list to (gnus-copy-sequence (cadr info))
1047 (gnus-copy-sequence (caddr info)) nil)
1048 gnus-category-alist)
1049 (gnus-category-write)
1050 (gnus-category-list)))
1052 (defun gnus-category-add (category)
1053 "Create a new category."
1054 (interactive "SCategory name: ")
1055 (when (assq category gnus-category-alist)
1056 (error "Category %s already exists" category))
1057 (push (list category 'true nil nil)
1058 gnus-category-alist)
1059 (gnus-category-write)
1060 (gnus-category-list))
1062 (defun gnus-category-list ()
1063 "List all categories."
1065 (gnus-category-prepare))
1067 (defun gnus-category-exit ()
1068 "Return to the group buffer."
1070 (kill-buffer (current-buffer))
1071 (gnus-configure-windows 'group t))
1073 ;; To avoid having 8-bit characters in the source file.
1074 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1076 (defvar gnus-category-predicate-alist
1077 '((spam . gnus-agent-spam-p)
1078 (short . gnus-agent-short-p)
1079 (long . gnus-agent-long-p)
1080 (low . gnus-agent-low-scored-p)
1081 (high . gnus-agent-high-scored-p)
1082 (true . gnus-agent-true)
1083 (false . gnus-agent-false))
1084 "Mapping from short score predicate symbols to predicate functions.")
1086 (defun gnus-agent-spam-p ()
1087 "Say whether an article is spam or not."
1088 (unless gnus-agent-spam-hashtb
1089 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1090 (if (not (equal (mail-header-references gnus-headers) ""))
1092 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1094 (gnus-gethash string gnus-agent-spam-hashtb)
1095 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1097 (defun gnus-agent-short-p ()
1098 "Say whether an article is short or not."
1099 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1101 (defun gnus-agent-long-p ()
1102 "Say whether an article is long or not."
1103 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1105 (defun gnus-agent-low-scored-p ()
1106 "Say whether an article has a low score or not."
1107 (< gnus-score gnus-agent-low-score))
1109 (defun gnus-agent-high-scored-p ()
1110 "Say whether an article has a high score or not."
1111 (> gnus-score gnus-agent-low-score))
1113 (defun gnus-category-make-function (cat)
1114 "Make a function from category CAT."
1115 `(lambda () ,(gnus-category-make-function-1 cat)))
1117 (defun gnus-agent-true ()
1121 (defun gnus-agent-false ()
1125 (defun gnus-category-make-function-1 (cat)
1126 "Make a function from category CAT."
1128 ;; Functions are just returned as is.
1130 (gnus-functionp cat))
1131 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1133 ;; More complex category.
1136 ((memq (car cat) '(& and))
1138 ((memq (car cat) '(| or))
1140 ((memq (car cat) gnus-category-not)
1142 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1144 (error "Unknown category type: %s" cat))))
1146 (defun gnus-get-predicate (predicate)
1147 "Return the predicate for CATEGORY."
1148 (or (cdr (assoc predicate gnus-category-predicate-cache))
1149 (cdar (push (cons predicate
1150 (gnus-category-make-function predicate))
1151 gnus-category-predicate-cache))))
1153 (defun gnus-group-category (group)
1154 "Return the category GROUP belongs to."
1155 (unless gnus-category-group-cache
1156 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1157 (let ((cs gnus-category-alist)
1159 (while (setq cat (pop cs))
1160 (setq groups (cadddr cat))
1162 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1163 (or (gnus-gethash group gnus-category-group-cache)
1164 (assq 'default gnus-category-alist)))
1166 (defun gnus-agent-expire ()
1167 "Expire all old articles."
1169 (let ((methods gnus-agent-covered-methods)
1170 (alist (cdr gnus-newsrc-alist))
1171 gnus-command-method ofiles info method file group)
1172 (while (setq gnus-command-method (pop methods))
1173 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1174 (gnus-agent-directory)))))
1175 (while (setq info (pop alist))
1176 (when (and (gnus-agent-method-p
1177 (setq gnus-command-method
1178 (gnus-find-method-for-group
1179 (setq group (gnus-info-group info)))))
1183 (gnus-agent-directory)
1184 (gnus-agent-group-path group) "/.overview"))
1186 (setq ofiles (delete file ofiles))
1187 (gnus-agent-expire-group file group)))
1189 (gnus-agent-expire-group (pop ofiles)))))
1191 (defun gnus-agent-expire-directory (dir)
1192 "Expire all groups in DIR recursively."
1193 (when (file-directory-p dir)
1194 (let ((files (directory-files dir t))
1196 (while (setq file (pop files))
1198 ((member (file-name-nondirectory file) '("." ".."))
1201 ((file-directory-p file)
1203 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1204 ((string-match "\\.overview$" file)
1206 (push file ofiles))))
1209 (defun gnus-agent-expire-group (overview &optional group)
1210 "Expire articles in OVERVIEW."
1211 (gnus-message 5 "Expiring %s..." overview)
1212 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1213 (dir (file-name-directory overview))
1214 (info (when group (gnus-get-info group)))
1215 headers article file point unreads)
1216 (gnus-agent-load-alist nil dir)
1220 (gnus-list-of-unread-articles group)
1221 (gnus-uncompress-range
1222 (cdr (assq 'tick (gnus-info-marks info))))
1223 (gnus-uncompress-range
1224 (cdr (assq 'dormant (gnus-info-marks info)))))))
1225 (nnheader-temp-write overview
1226 (insert-file-contents overview)
1227 (goto-char (point-min))
1229 (setq point (point))
1231 (setq headers (inline (nnheader-parse-nov)))
1235 (setq headers nil)))
1237 (unless (memq (setq article (mail-header-number headers)) unreads)
1240 (inline (nnmail-date-to-time
1241 (mail-header-date headers)))))
1245 (setq gnus-agent-article-alist
1246 (delq (assq article gnus-agent-article-alist)
1247 gnus-agent-article-alist))
1248 (when (file-exists-p
1249 (setq file (concat dir (number-to-string article))))
1250 (delete-file file))))))
1251 (gnus-agent-save-alist nil nil nil dir))))
1253 (provide 'gnus-agent)
1255 ;;; gnus-agent.el ends here