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 (let ((backward (< n 0))
386 (gnus-summary-set-agent-mark
387 (gnus-summary-article-number) unmark)
388 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
391 (gnus-message 7 "No more articles"))
392 (gnus-summary-recenter)
393 (gnus-summary-position-point)
396 (defun gnus-agent-unmark-article (n)
397 "Remove the downloadable mark from the next N articles.
398 If N is negative, unmark backward instead. The difference between N and
399 the actual number of articles unmarked is returned."
401 (gnus-agent-mark-article n t))
403 (defun gnus-agent-toggle-mark (n)
404 "Toggle the downloadable mark from the next N articles.
405 If N is negative, toggle backward instead. The difference between N and
406 the actual number of articles toggled is returned."
408 (gnus-agent-mark-article n 'toggle))
410 (defun gnus-summary-set-agent-mark (article &optional unmark)
411 "Mark ARTICLE as downloadable."
412 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
413 (memq article gnus-newsgroup-downloadable)
417 (setq gnus-newsgroup-downloadable
418 (delq article gnus-newsgroup-downloadable))
419 (push article gnus-newsgroup-undownloaded))
420 (setq gnus-newsgroup-undownloaded
421 (delq article gnus-newsgroup-undownloaded))
422 (push article gnus-newsgroup-downloadable))
423 (gnus-summary-update-mark
424 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
427 (defun gnus-agent-get-undownloaded-list ()
428 "Mark all unfetched articles as read."
429 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
430 (when (and (not gnus-plugged)
431 (gnus-agent-method-p gnus-command-method))
432 (gnus-agent-load-alist gnus-newsgroup-name)
433 (let ((articles gnus-newsgroup-unreads)
435 (while (setq article (pop articles))
436 (unless (or (cdr (assq article gnus-agent-article-alist))
437 (memq article gnus-newsgroup-downloadable))
438 (push article gnus-newsgroup-undownloaded)))))))
440 (defun gnus-agent-catchup ()
441 "Mark all undownloaded articles as read."
444 (while gnus-newsgroup-undownloaded
445 (gnus-summary-mark-article
446 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
447 (gnus-summary-position-point))
450 ;;; Internal functions
453 (defun gnus-agent-save-active (method)
454 (when (gnus-agent-method-p method)
455 (let* ((gnus-command-method method)
456 (file (gnus-agent-lib-file "active")))
457 (gnus-make-directory (file-name-directory file))
458 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
459 (write-region (point-min) (point-max) file nil 'silent))
460 (when (file-exists-p (gnus-agent-lib-file "groups"))
461 (delete-file (gnus-agent-lib-file "groups"))))))
463 (defun gnus-agent-save-groups (method)
464 (let* ((gnus-command-method method)
465 (file (gnus-agent-lib-file "groups")))
466 (gnus-make-directory (file-name-directory file))
467 (write-region (point-min) (point-max) file nil 'silent))
468 (when (file-exists-p (gnus-agent-lib-file "active"))
469 (delete-file (gnus-agent-lib-file "active"))))
471 (defun gnus-agent-group-path (group)
472 "Translate GROUP into a path."
473 (nnheader-translate-file-chars
474 (nnheader-replace-chars-in-string group ?. ?/)))
478 (defun gnus-agent-method-p (method)
479 "Say whether METHOD is covered by the agent."
480 (member method gnus-agent-covered-methods))
482 (defun gnus-agent-get-function (method)
483 (if (and (not gnus-plugged)
484 (gnus-agent-method-p method))
490 ;;; History functions
492 (defun gnus-agent-history-buffer ()
493 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
495 (defun gnus-agent-open-history ()
497 (push (cons (gnus-agent-method)
498 (set-buffer (get-buffer-create
499 (format " *Gnus agent %s history*"
500 (gnus-agent-method)))))
501 gnus-agent-history-buffers)
504 (let ((file (gnus-agent-lib-file "history")))
505 (when (file-exists-p file)
507 (set (make-local-variable 'gnus-agent-file-name) file))))
509 (defun gnus-agent-save-history ()
511 (set-buffer gnus-agent-current-history)
512 (gnus-make-directory (file-name-directory gnus-agent-file-name))
513 (write-region (1+ (point-min)) (point-max)
514 gnus-agent-file-name nil 'silent)))
516 (defun gnus-agent-close-history ()
517 (when (gnus-buffer-live-p gnus-agent-current-history)
518 (kill-buffer gnus-agent-current-history)
519 (setq gnus-agent-history-buffers
520 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
521 gnus-agent-history-buffers))))
523 (defun gnus-agent-enter-history (id group-arts date)
525 (set-buffer gnus-agent-current-history)
526 (goto-char (point-max))
527 (insert id "\t" (number-to-string date) "\t")
529 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
533 (defun gnus-agent-article-in-history-p (id)
535 (set-buffer (gnus-agent-history-buffer))
536 (goto-char (point-min))
537 (search-forward (concat "\n" id "\t") nil t)))
539 (defun gnus-agent-history-path (id)
541 (set-buffer (gnus-agent-history-buffer))
542 (goto-char (point-min))
543 (when (search-forward (concat "\n" id "\t") nil t)
544 (let ((method (gnus-agent-method)))
546 (while (not (numberp (setq group (read (current-buffer)))))
547 (push (concat method "/" group) paths))
548 (nreverse paths))))))
554 (defun gnus-agent-fetch-articles (group articles)
555 "Fetch ARTICLES from GROUP and put them into the agent."
557 ;; Prune off articles that we have already fetched.
559 (cdr (assq (car articles) gnus-agent-article-alist)))
561 (let ((arts articles))
563 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
564 (setcdr arts (cddr arts))
565 (setq arts (cdr arts)))))
568 (gnus-agent-directory)
569 (gnus-agent-group-path group) "/"))
570 (date (gnus-time-to-day (current-time)))
572 pos alists crosses id elem)
573 (gnus-make-directory dir)
574 (gnus-message 7 "Fetching articles for %s..." group)
575 ;; Fetch the articles from the backend.
576 (if (gnus-check-backend-function 'retrieve-articles group)
577 (setq pos (gnus-retrieve-articles articles group))
578 (nnheader-temp-write nil
579 (let ((buf (current-buffer))
581 (while (setq article (pop articles))
582 (when (gnus-request-article article group)
583 (goto-char (point-max))
584 (push (cons article (point)) pos)
585 (insert-buffer-substring nntp-server-buffer)))
586 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
587 (setq pos (nreverse pos)))))
588 ;; Then save these articles into the agent.
590 (set-buffer nntp-server-buffer)
592 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
593 (goto-char (point-min))
594 (when (search-forward "\n\n" nil t)
595 (when (search-backward "\nXrefs: " nil t)
596 ;; Handle crossposting.
597 (skip-chars-forward "^ ")
598 (skip-chars-forward " ")
600 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
601 (push (cons (buffer-substring (match-beginning 1)
603 (buffer-substring (match-beginning 2)
606 (goto-char (match-end 0)))
607 (gnus-agent-crosspost crosses (caar pos))))
608 (goto-char (point-min))
609 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
610 (setq id "No-Message-ID-in-article")
611 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
612 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
613 (write-region (point-min) (point-max)
614 (concat dir (number-to-string (caar pos)))
616 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
618 (gnus-agent-enter-history
619 id (or crosses (list (cons group (caar pos)))) date)
622 (gnus-agent-save-alist group)))))
624 (defun gnus-agent-crosspost (crosses article)
625 (let (gnus-agent-article-alist group alist beg end)
627 (set-buffer gnus-agent-overview-buffer)
628 (when (nnheader-find-nov-line article)
631 (setq end (progn (forward-line 1) (point)))))
633 (setq group (caar crosses))
634 (unless (setq alist (assoc group gnus-agent-group-alist))
635 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
636 gnus-agent-group-alist))
637 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
639 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
641 (when (= (point-max) (point-min))
642 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
644 (insert-file-contents
645 (gnus-agent-article-name ".overview" group))))
646 (nnheader-find-nov-line (string-to-number (cdar crosses)))
647 (insert (string-to-number (cdar crosses)))
648 (insert-buffer-substring gnus-agent-overview-buffer beg end))
651 (defun gnus-agent-flush-cache ()
653 (while gnus-agent-buffer-alist
654 (set-buffer (cdar gnus-agent-buffer-alist))
655 (write-region (point-min) (point-max)
656 (gnus-agent-article-name ".overview"
657 (caar gnus-agent-buffer-alist))
659 (pop gnus-agent-buffer-alist))
660 (while gnus-agent-group-alist
661 (nnheader-temp-write (caar gnus-agent-group-alist)
662 (princ (cdar gnus-agent-group-alist))
664 (pop gnus-agent-group-alist))))
666 (defun gnus-agent-fetch-headers (group articles &optional force)
667 (gnus-agent-load-alist group)
668 ;; Find out what headers we need to retrieve.
671 (assq (car articles) gnus-agent-article-alist))
673 (let ((arts articles))
675 (if (assq (cadr arts) gnus-agent-article-alist)
676 (setcdr arts (cddr arts))
677 (setq arts (cdr arts)))))
680 (gnus-message 7 "Fetching headers for %s..." group)
682 (set-buffer nntp-server-buffer)
683 (unless (eq 'nov (gnus-retrieve-headers articles group))
684 (nnvirtual-convert-headers))
685 ;; Save these headers for later processing.
686 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
689 (setq file (gnus-agent-article-name ".overview" group)))
690 (gnus-agent-braid-nov group articles file))
691 (gnus-make-directory (nnheader-translate-file-chars
692 (file-name-directory file)))
693 (write-region (point-min) (point-max) file nil 'silent)
694 (gnus-agent-save-alist group articles nil))
697 (defsubst gnus-agent-copy-nov-line (article)
699 (set-buffer gnus-agent-overview-buffer)
701 (if (eq article (read (current-buffer)))
702 (setq e (progn (forward-line 1) (point)))
704 (set-buffer nntp-server-buffer)
705 (insert-buffer-substring gnus-agent-overview-buffer b e)))
707 (defun gnus-agent-braid-nov (group articles file)
709 (set-buffer gnus-agent-overview-buffer)
710 (goto-char (point-min))
711 (set-buffer nntp-server-buffer)
713 (insert-file-contents file)
714 (goto-char (point-min))
715 (if (or (= (point-min) (point-max))
718 (< (read (current-buffer)) (car articles))))
719 ;; We have only headers that are after the older headers,
720 ;; so we just append them.
722 (goto-char (point-max))
723 (insert-buffer-substring gnus-agent-overview-buffer))
724 ;; We do it the hard way.
725 (nnheader-find-nov-line (car articles))
726 (gnus-agent-copy-nov-line (car articles))
730 (while (and (not (eobp))
731 (< (read (current-buffer)) (car articles)))
735 (gnus-agent-copy-nov-line (car articles))
736 (setq articles (cdr articles))))
739 (set-buffer gnus-agent-overview-buffer)
742 (set-buffer nntp-server-buffer)
743 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
745 (defun gnus-agent-load-alist (group &optional dir)
746 "Load the article-state alist for GROUP."
747 (setq gnus-agent-article-alist
748 (gnus-agent-read-file
750 (concat dir ".agentview")
751 (gnus-agent-article-name ".agentview" group)))))
753 (defun gnus-agent-save-alist (group &optional articles state dir)
754 "Load the article-state alist for GROUP."
755 (nnheader-temp-write (if dir
756 (concat dir ".agentview")
757 (gnus-agent-article-name ".agentview" group))
758 (princ (setq gnus-agent-article-alist
759 (nconc gnus-agent-article-alist
760 (mapcar (lambda (article) (cons article state))
765 (defun gnus-agent-article-name (article group)
766 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
767 (if (stringp article) article (string-to-number article))))
770 (defun gnus-agent-batch-fetch ()
771 "Start Gnus and fetch session."
774 (gnus-agent-fetch-session)
777 (defun gnus-agent-fetch-session ()
778 "Fetch all articles and headers that are eligible for fetching."
780 (unless gnus-agent-covered-methods
781 (error "No servers are covered by the Gnus agent"))
783 (error "Can't fetch articles while Gnus is unplugged"))
784 (let ((methods gnus-agent-covered-methods)
785 groups group gnus-command-method)
788 (setq gnus-command-method (car methods)
789 groups (gnus-groups-from-server (pop methods)))
790 (gnus-agent-with-fetch
791 (while (setq group (pop groups))
792 (gnus-agent-fetch-group-1 group gnus-command-method))))
793 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
795 (defun gnus-agent-fetch-group-1 (group method)
797 (let ((gnus-command-method method)
798 gnus-newsgroup-dependencies gnus-newsgroup-headers
799 gnus-newsgroup-scored gnus-headers gnus-score
800 gnus-use-cache articles score arts
801 category predicate info marks score-param)
803 (when (and (setq articles (gnus-list-of-unread-articles group))
804 (gnus-agent-fetch-headers group articles))
805 ;; Parse them and see which articles we want to fetch.
806 (setq gnus-newsgroup-dependencies
807 (make-vector (length articles) 0))
808 (setq gnus-newsgroup-headers
809 (gnus-get-newsgroup-headers-xover articles nil nil group))
810 (setq category (gnus-group-category group))
813 (or (gnus-group-get-parameter group 'agent-predicate)
816 (or (gnus-group-get-parameter group 'agent-score)
819 (gnus-score-headers (list (list score-param))))
821 (while (setq gnus-headers (pop gnus-newsgroup-headers))
823 (or (cdr (assq (mail-header-number gnus-headers)
824 gnus-newsgroup-scored))
825 gnus-summary-default-score))
826 (when (funcall predicate)
827 (push (mail-header-number gnus-headers)
829 ;; Fetch the articles.
831 (gnus-agent-fetch-articles group arts)))
832 ;; Perhaps we have some additional articles to fetch.
833 (setq arts (assq 'download (gnus-info-marks
834 (setq info (gnus-get-info group)))))
836 (gnus-agent-fetch-articles
837 group (gnus-uncompress-range (cdr arts)))
838 (setq marks (delq arts (gnus-info-marks info)))
839 (gnus-info-set-marks info marks))))
842 ;;; Agent Category Mode
845 (defvar gnus-category-mode-hook nil
846 "Hook run in `gnus-category-mode' buffers.")
848 (defvar gnus-category-line-format " %(%20c%): %g\n"
849 "Format of category lines.")
851 (defvar gnus-category-mode-line-format "Gnus: %%b"
852 "The format specification for the category mode line.")
854 (defvar gnus-agent-short-article 100
855 "Articles that have fewer lines than this are short.")
857 (defvar gnus-agent-long-article 200
858 "Articles that have more lines than this are long.")
860 (defvar gnus-agent-low-score 0
861 "Articles that have a score lower than this have a low score.")
863 (defvar gnus-agent-high-score 0
864 "Articles that have a score higher than this have a high score.")
867 ;;; Internal variables.
869 (defvar gnus-category-buffer "*Agent Category*")
871 (defvar gnus-category-line-format-alist
875 (defvar gnus-category-mode-line-format-alist
876 `((?u user-defined ?s)))
878 (defvar gnus-category-line-format-spec nil)
879 (defvar gnus-category-mode-line-format-spec nil)
881 (defvar gnus-category-mode-map nil)
882 (put 'gnus-category-mode 'mode-class 'special)
884 (unless gnus-category-mode-map
885 (setq gnus-category-mode-map (make-sparse-keymap))
886 (suppress-keymap gnus-category-mode-map)
888 (gnus-define-keys gnus-category-mode-map
889 "q" gnus-category-exit
890 "k" gnus-category-kill
891 "c" gnus-category-copy
892 "a" gnus-category-add
893 "p" gnus-category-edit-predicate
894 "g" gnus-category-edit-groups
895 "s" gnus-category-edit-score
896 "l" gnus-category-list
898 "\C-c\C-i" gnus-info-find-node
899 "\C-c\C-b" gnus-bug))
901 (defvar gnus-category-menu-hook nil
902 "*Hook run after the creation of the menu.")
904 (defun gnus-category-make-menu-bar ()
905 (gnus-turn-off-edit-menu 'category)
906 (unless (boundp 'gnus-category-menu)
908 gnus-category-menu gnus-category-mode-map ""
910 ["Add" gnus-category-add t]
911 ["Kill" gnus-category-kill t]
912 ["Copy" gnus-category-copy t]
913 ["Edit predicate" gnus-category-edit-predicate t]
914 ["Edit score" gnus-category-edit-score t]
915 ["Edit groups" gnus-category-edit-groups t]
916 ["Exit" gnus-category-exit t]))
918 (run-hooks 'gnus-category-menu-hook)))
920 (defun gnus-category-mode ()
921 "Major mode for listing and editing agent categories.
923 All normal editing commands are switched off.
924 \\<gnus-category-mode-map>
925 For more in-depth information on this mode, read the manual
926 (`\\[gnus-info-find-node]').
928 The following commands are available:
930 \\{gnus-category-mode-map}"
932 (when (gnus-visual-p 'category-menu 'menu)
933 (gnus-category-make-menu-bar))
934 (kill-all-local-variables)
935 (gnus-simplify-mode-line)
936 (setq major-mode 'gnus-category-mode)
937 (setq mode-name "Category")
938 (gnus-set-default-directory)
939 (setq mode-line-process nil)
940 (use-local-map gnus-category-mode-map)
941 (buffer-disable-undo (current-buffer))
942 (setq truncate-lines t)
943 (setq buffer-read-only t)
944 (run-hooks 'gnus-category-mode-hook))
946 (defalias 'gnus-category-position-point 'gnus-goto-colon)
948 (defun gnus-category-insert-line (category)
949 (let* ((name (car category))
950 (groups (length (cadddr category))))
952 (gnus-add-text-properties
956 (eval gnus-category-line-format-spec))
957 (list 'gnus-category name))))
959 (defun gnus-enter-category-buffer ()
960 "Go to the Category buffer."
962 (gnus-category-setup-buffer)
963 (gnus-configure-windows 'category)
964 (gnus-category-prepare))
966 (defun gnus-category-setup-buffer ()
967 (unless (get-buffer gnus-category-buffer)
969 (set-buffer (get-buffer-create gnus-category-buffer))
970 (gnus-add-current-to-buffer-list)
971 (gnus-category-mode))))
973 (defun gnus-category-prepare ()
974 (gnus-set-format 'category-mode)
975 (gnus-set-format 'category t)
976 (let ((alist gnus-category-alist)
977 (buffer-read-only nil))
980 (gnus-category-insert-line (pop alist)))
981 (goto-char (point-min))
982 (gnus-category-position-point)))
984 (defun gnus-category-name ()
985 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
986 (error "No category on the current line")))
988 (defun gnus-category-read ()
989 "Read the category alist."
990 (setq gnus-category-alist
991 (or (gnus-agent-read-file
992 (nnheader-concat gnus-agent-directory "lib/categories"))
993 (list (list 'default 'true nil nil)))))
995 (defun gnus-category-write ()
996 "Write the category alist."
997 (setq gnus-category-predicate-cache nil
998 gnus-category-group-cache nil)
999 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1000 (prin1 gnus-category-alist (current-buffer))))
1002 (defun gnus-category-edit-predicate (category)
1003 "Edit the predicate for CATEGORY."
1004 (interactive (list (gnus-category-name)))
1005 (let ((info (assq category gnus-category-alist)))
1007 (cadr info) (format "Editing the predicate for category %s" category)
1008 `(lambda (predicate)
1009 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1010 (gnus-category-write)
1011 (gnus-category-list)))))
1013 (defun gnus-category-edit-score (category)
1014 "Edit the score expression for CATEGORY."
1015 (interactive (list (gnus-category-name)))
1016 (let ((info (assq category gnus-category-alist)))
1019 (format "Editing the score expression for category %s" category)
1021 (setf (caddr (assq ',category gnus-category-alist)) groups)
1022 (gnus-category-write)
1023 (gnus-category-list)))))
1025 (defun gnus-category-edit-groups (category)
1026 "Edit the group list for CATEGORY."
1027 (interactive (list (gnus-category-name)))
1028 (let ((info (assq category gnus-category-alist)))
1030 (cadddr info) (format "Editing the group list for category %s" category)
1032 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1033 (gnus-category-write)
1034 (gnus-category-list)))))
1036 (defun gnus-category-kill (category)
1037 "Kill the current category."
1038 (interactive (list (gnus-category-name)))
1039 (let ((info (assq category gnus-category-alist))
1040 (buffer-read-only nil))
1042 (gnus-category-write)
1043 (setq gnus-category-alist (delq info gnus-category-alist))))
1045 (defun gnus-category-copy (category to)
1046 "Copy the current category."
1047 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1048 (let ((info (assq category gnus-category-alist)))
1049 (push (list to (gnus-copy-sequence (cadr info))
1050 (gnus-copy-sequence (caddr info)) nil)
1051 gnus-category-alist)
1052 (gnus-category-write)
1053 (gnus-category-list)))
1055 (defun gnus-category-add (category)
1056 "Create a new category."
1057 (interactive "SCategory name: ")
1058 (when (assq category gnus-category-alist)
1059 (error "Category %s already exists" category))
1060 (push (list category 'true nil nil)
1061 gnus-category-alist)
1062 (gnus-category-write)
1063 (gnus-category-list))
1065 (defun gnus-category-list ()
1066 "List all categories."
1068 (gnus-category-prepare))
1070 (defun gnus-category-exit ()
1071 "Return to the group buffer."
1073 (kill-buffer (current-buffer))
1074 (gnus-configure-windows 'group t))
1076 ;; To avoid having 8-bit characters in the source file.
1077 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1079 (defvar gnus-category-predicate-alist
1080 '((spam . gnus-agent-spam-p)
1081 (short . gnus-agent-short-p)
1082 (long . gnus-agent-long-p)
1083 (low . gnus-agent-low-scored-p)
1084 (high . gnus-agent-high-scored-p)
1085 (true . gnus-agent-true)
1086 (false . gnus-agent-false))
1087 "Mapping from short score predicate symbols to predicate functions.")
1089 (defun gnus-agent-spam-p ()
1090 "Say whether an article is spam or not."
1091 (unless gnus-agent-spam-hashtb
1092 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1093 (if (not (equal (mail-header-references gnus-headers) ""))
1095 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1097 (gnus-gethash string gnus-agent-spam-hashtb)
1098 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1100 (defun gnus-agent-short-p ()
1101 "Say whether an article is short or not."
1102 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1104 (defun gnus-agent-long-p ()
1105 "Say whether an article is long or not."
1106 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1108 (defun gnus-agent-low-scored-p ()
1109 "Say whether an article has a low score or not."
1110 (< gnus-score gnus-agent-low-score))
1112 (defun gnus-agent-high-scored-p ()
1113 "Say whether an article has a high score or not."
1114 (> gnus-score gnus-agent-low-score))
1116 (defun gnus-category-make-function (cat)
1117 "Make a function from category CAT."
1118 `(lambda () ,(gnus-category-make-function-1 cat)))
1120 (defun gnus-agent-true ()
1124 (defun gnus-agent-false ()
1128 (defun gnus-category-make-function-1 (cat)
1129 "Make a function from category CAT."
1131 ;; Functions are just returned as is.
1133 (gnus-functionp cat))
1134 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1136 ;; More complex category.
1139 ((memq (car cat) '(& and))
1141 ((memq (car cat) '(| or))
1143 ((memq (car cat) gnus-category-not)
1145 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1147 (error "Unknown category type: %s" cat))))
1149 (defun gnus-get-predicate (predicate)
1150 "Return the predicate for CATEGORY."
1151 (or (cdr (assoc predicate gnus-category-predicate-cache))
1152 (cdar (push (cons predicate
1153 (gnus-category-make-function predicate))
1154 gnus-category-predicate-cache))))
1156 (defun gnus-group-category (group)
1157 "Return the category GROUP belongs to."
1158 (unless gnus-category-group-cache
1159 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1160 (let ((cs gnus-category-alist)
1162 (while (setq cat (pop cs))
1163 (setq groups (cadddr cat))
1165 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1166 (or (gnus-gethash group gnus-category-group-cache)
1167 (assq 'default gnus-category-alist)))
1169 (defun gnus-agent-expire ()
1170 "Expire all old articles."
1172 (let ((methods gnus-agent-covered-methods)
1173 (alist (cdr gnus-newsrc-alist))
1174 gnus-command-method ofiles info method file group)
1175 (while (setq gnus-command-method (pop methods))
1176 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1177 (gnus-agent-directory)))))
1178 (while (setq info (pop alist))
1179 (when (and (gnus-agent-method-p
1180 (setq gnus-command-method
1181 (gnus-find-method-for-group
1182 (setq group (gnus-info-group info)))))
1186 (gnus-agent-directory)
1187 (gnus-agent-group-path group) "/.overview"))
1189 (setq ofiles (delete file ofiles))
1190 (gnus-agent-expire-group file group)))
1192 (gnus-agent-expire-group (pop ofiles)))))
1194 (defun gnus-agent-expire-directory (dir)
1195 "Expire all groups in DIR recursively."
1196 (when (file-directory-p dir)
1197 (let ((files (directory-files dir t))
1199 (while (setq file (pop files))
1201 ((member (file-name-nondirectory file) '("." ".."))
1204 ((file-directory-p file)
1206 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1207 ((string-match "\\.overview$" file)
1209 (push file ofiles))))
1212 (defun gnus-agent-expire-group (overview &optional group)
1213 "Expire articles in OVERVIEW."
1214 (gnus-message 5 "Expiring %s..." overview)
1215 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1216 (dir (file-name-directory overview))
1217 (info (when group (gnus-get-info group)))
1218 headers article file point unreads)
1219 (gnus-agent-load-alist nil dir)
1223 (gnus-list-of-unread-articles group)
1224 (gnus-uncompress-range
1225 (cdr (assq 'tick (gnus-info-marks info))))
1226 (gnus-uncompress-range
1227 (cdr (assq 'dormant (gnus-info-marks info)))))))
1228 (nnheader-temp-write overview
1229 (insert-file-contents overview)
1230 (goto-char (point-min))
1232 (setq point (point))
1234 (setq headers (inline (nnheader-parse-nov)))
1238 (setq headers nil)))
1240 (unless (memq (setq article (mail-header-number headers)) unreads)
1243 (inline (nnmail-date-to-time
1244 (mail-header-date headers)))))
1248 (setq gnus-agent-article-alist
1249 (delq (assq article gnus-agent-article-alist)
1250 gnus-agent-article-alist))
1251 (when (file-exists-p
1252 (setq file (concat dir (number-to-string article))))
1253 (delete-file file))))))
1254 (gnus-agent-save-alist nil nil nil dir))))
1256 (provide 'gnus-agent)
1258 ;;; gnus-agent.el ends here