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-groups
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-groups (n)
301 "Put all new articles in the current groups into the agent."
303 (gnus-group-iterate n 'gnus-agent-fetch-group))
305 (defun gnus-agent-fetch-group (group)
306 "Put all new articles in GROUP into the agent."
307 (interactive (list (gnus-group-group-name)))
309 (error "No group on the current line"))
310 (let ((gnus-command-method (gnus-find-method-for-group group)))
311 (gnus-agent-with-fetch
312 (gnus-agent-fetch-group-1 group gnus-command-method)
313 (gnus-message 5 "Fetching %s...done" group))))
315 (defun gnus-agent-add-group (category arg)
316 "Add the current group to an agent category."
322 (mapcar (lambda (cat) (list (symbol-name (car cat))))
326 (let ((cat (assq category gnus-category-alist))
328 (gnus-group-iterate arg
330 (when (cadddr (setq c (gnus-group-category group)))
331 (setf (cadddr c) (delete group (cadddr c))))
332 (push group groups)))
333 (setf (cadddr cat) (nconc (cadddr cat) groups))
334 (gnus-category-write)))
337 ;;; Server mode commands
340 (defun gnus-agent-add-server (server)
341 "Enroll SERVER in the agent program."
342 (interactive (list (gnus-server-server-name)))
344 (error "No server on the current line"))
345 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
346 (when (member method gnus-agent-covered-methods)
347 (error "Server already in the agent program"))
348 (push method gnus-agent-covered-methods)
349 (gnus-agent-write-servers)
350 (message "Entered %s into the agent" server)))
352 (defun gnus-agent-remove-server (server)
353 "Remove SERVER from the agent program."
354 (interactive (list (gnus-server-server-name)))
356 (error "No server on the current line"))
357 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
358 (unless (member method gnus-agent-covered-methods)
359 (error "Server not in the agent program"))
360 (setq gnus-agent-covered-methods
361 (delete method gnus-agent-covered-methods))
362 (gnus-agent-write-servers)
363 (message "Removed %s from the agent" server)))
365 (defun gnus-agent-read-servers ()
366 "Read the alist of covered servers."
367 (setq gnus-agent-covered-methods
368 (gnus-agent-read-file
369 (nnheader-concat gnus-agent-directory "lib/servers"))))
371 (defun gnus-agent-write-servers ()
372 "Write the alist of covered servers."
373 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
374 (prin1 gnus-agent-covered-methods (current-buffer))))
380 (defun gnus-agent-mark-article (n &optional unmark)
381 "Mark the next N articles as downloadable.
382 If N is negative, mark backward instead. If UNMARK is non-nil, remove
383 the mark instead. The difference between N and the actual number of
384 articles marked is returned."
386 (let ((backward (< n 0))
391 (gnus-summary-set-agent-mark
392 (gnus-summary-article-number) unmark)
393 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
396 (gnus-message 7 "No more articles"))
397 (gnus-summary-recenter)
398 (gnus-summary-position-point)
401 (defun gnus-agent-unmark-article (n)
402 "Remove the downloadable mark from the next N articles.
403 If N is negative, unmark backward instead. The difference between N and
404 the actual number of articles unmarked is returned."
406 (gnus-agent-mark-article n t))
408 (defun gnus-agent-toggle-mark (n)
409 "Toggle the downloadable mark from the next N articles.
410 If N is negative, toggle backward instead. The difference between N and
411 the actual number of articles toggled is returned."
413 (gnus-agent-mark-article n 'toggle))
415 (defun gnus-summary-set-agent-mark (article &optional unmark)
416 "Mark ARTICLE as downloadable."
417 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
418 (memq article gnus-newsgroup-downloadable)
422 (setq gnus-newsgroup-downloadable
423 (delq article gnus-newsgroup-downloadable))
424 (push article gnus-newsgroup-undownloaded))
425 (setq gnus-newsgroup-undownloaded
426 (delq article gnus-newsgroup-undownloaded))
427 (push article gnus-newsgroup-downloadable))
428 (gnus-summary-update-mark
429 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
432 (defun gnus-agent-get-undownloaded-list ()
433 "Mark all unfetched articles as read."
434 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
435 (when (and (not gnus-plugged)
436 (gnus-agent-method-p gnus-command-method))
437 (gnus-agent-load-alist gnus-newsgroup-name)
438 (let ((articles gnus-newsgroup-unreads)
440 (while (setq article (pop articles))
441 (unless (or (cdr (assq article gnus-agent-article-alist))
442 (memq article gnus-newsgroup-downloadable))
443 (push article gnus-newsgroup-undownloaded)))))))
445 (defun gnus-agent-catchup ()
446 "Mark all undownloaded articles as read."
449 (while gnus-newsgroup-undownloaded
450 (gnus-summary-mark-article
451 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
452 (gnus-summary-position-point))
455 ;;; Internal functions
458 (defun gnus-agent-save-active (method)
459 (when (gnus-agent-method-p method)
460 (let* ((gnus-command-method method)
461 (file (gnus-agent-lib-file "active")))
462 (gnus-make-directory (file-name-directory file))
463 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
464 (write-region (point-min) (point-max) file nil 'silent))
465 (when (file-exists-p (gnus-agent-lib-file "groups"))
466 (delete-file (gnus-agent-lib-file "groups"))))))
468 (defun gnus-agent-save-groups (method)
469 (let* ((gnus-command-method method)
470 (file (gnus-agent-lib-file "groups")))
471 (gnus-make-directory (file-name-directory file))
472 (write-region (point-min) (point-max) file nil 'silent))
473 (when (file-exists-p (gnus-agent-lib-file "active"))
474 (delete-file (gnus-agent-lib-file "active"))))
476 (defun gnus-agent-group-path (group)
477 "Translate GROUP into a path."
478 (nnheader-translate-file-chars
479 (nnheader-replace-chars-in-string group ?. ?/)))
483 (defun gnus-agent-method-p (method)
484 "Say whether METHOD is covered by the agent."
485 (member method gnus-agent-covered-methods))
487 (defun gnus-agent-get-function (method)
488 (if (and (not gnus-plugged)
489 (gnus-agent-method-p method))
495 ;;; History functions
497 (defun gnus-agent-history-buffer ()
498 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
500 (defun gnus-agent-open-history ()
502 (push (cons (gnus-agent-method)
503 (set-buffer (get-buffer-create
504 (format " *Gnus agent %s history*"
505 (gnus-agent-method)))))
506 gnus-agent-history-buffers)
509 (let ((file (gnus-agent-lib-file "history")))
510 (when (file-exists-p file)
512 (set (make-local-variable 'gnus-agent-file-name) file))))
514 (defun gnus-agent-save-history ()
516 (set-buffer gnus-agent-current-history)
517 (gnus-make-directory (file-name-directory gnus-agent-file-name))
518 (write-region (1+ (point-min)) (point-max)
519 gnus-agent-file-name nil 'silent)))
521 (defun gnus-agent-close-history ()
522 (when (gnus-buffer-live-p gnus-agent-current-history)
523 (kill-buffer gnus-agent-current-history)
524 (setq gnus-agent-history-buffers
525 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
526 gnus-agent-history-buffers))))
528 (defun gnus-agent-enter-history (id group-arts date)
530 (set-buffer gnus-agent-current-history)
531 (goto-char (point-max))
532 (insert id "\t" (number-to-string date) "\t")
534 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
538 (defun gnus-agent-article-in-history-p (id)
540 (set-buffer (gnus-agent-history-buffer))
541 (goto-char (point-min))
542 (search-forward (concat "\n" id "\t") nil t)))
544 (defun gnus-agent-history-path (id)
546 (set-buffer (gnus-agent-history-buffer))
547 (goto-char (point-min))
548 (when (search-forward (concat "\n" id "\t") nil t)
549 (let ((method (gnus-agent-method)))
551 (while (not (numberp (setq group (read (current-buffer)))))
552 (push (concat method "/" group) paths))
553 (nreverse paths))))))
559 (defun gnus-agent-fetch-articles (group articles)
560 "Fetch ARTICLES from GROUP and put them into the agent."
562 ;; Prune off articles that we have already fetched.
564 (cdr (assq (car articles) gnus-agent-article-alist)))
566 (let ((arts articles))
568 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
569 (setcdr arts (cddr arts))
570 (setq arts (cdr arts)))))
573 (gnus-agent-directory)
574 (gnus-agent-group-path group) "/"))
575 (date (gnus-time-to-day (current-time)))
577 pos alists crosses id elem)
578 (gnus-make-directory dir)
579 (gnus-message 7 "Fetching articles for %s..." group)
580 ;; Fetch the articles from the backend.
581 (if (gnus-check-backend-function 'retrieve-articles group)
582 (setq pos (gnus-retrieve-articles articles group))
583 (nnheader-temp-write nil
584 (let ((buf (current-buffer))
586 (while (setq article (pop articles))
587 (when (gnus-request-article article group)
588 (goto-char (point-max))
589 (push (cons article (point)) pos)
590 (insert-buffer-substring nntp-server-buffer)))
591 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
592 (setq pos (nreverse pos)))))
593 ;; Then save these articles into the agent.
595 (set-buffer nntp-server-buffer)
597 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
598 (goto-char (point-min))
599 (when (search-forward "\n\n" nil t)
600 (when (search-backward "\nXrefs: " nil t)
601 ;; Handle crossposting.
602 (skip-chars-forward "^ ")
603 (skip-chars-forward " ")
605 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
606 (push (cons (buffer-substring (match-beginning 1)
608 (buffer-substring (match-beginning 2)
611 (goto-char (match-end 0)))
612 (gnus-agent-crosspost crosses (caar pos))))
613 (goto-char (point-min))
614 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
615 (setq id "No-Message-ID-in-article")
616 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
617 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
618 (write-region (point-min) (point-max)
619 (concat dir (number-to-string (caar pos)))
621 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
623 (gnus-agent-enter-history
624 id (or crosses (list (cons group (caar pos)))) date)
627 (gnus-agent-save-alist group)))))
629 (defun gnus-agent-crosspost (crosses article)
630 (let (gnus-agent-article-alist group alist beg end)
632 (set-buffer gnus-agent-overview-buffer)
633 (when (nnheader-find-nov-line article)
636 (setq end (progn (forward-line 1) (point)))))
638 (setq group (caar crosses))
639 (unless (setq alist (assoc group gnus-agent-group-alist))
640 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
641 gnus-agent-group-alist))
642 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
644 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
646 (when (= (point-max) (point-min))
647 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
649 (insert-file-contents
650 (gnus-agent-article-name ".overview" group))))
651 (nnheader-find-nov-line (string-to-number (cdar crosses)))
652 (insert (string-to-number (cdar crosses)))
653 (insert-buffer-substring gnus-agent-overview-buffer beg end))
656 (defun gnus-agent-flush-cache ()
658 (while gnus-agent-buffer-alist
659 (set-buffer (cdar gnus-agent-buffer-alist))
660 (write-region (point-min) (point-max)
661 (gnus-agent-article-name ".overview"
662 (caar gnus-agent-buffer-alist))
664 (pop gnus-agent-buffer-alist))
665 (while gnus-agent-group-alist
666 (nnheader-temp-write (caar gnus-agent-group-alist)
667 (princ (cdar gnus-agent-group-alist))
669 (pop gnus-agent-group-alist))))
671 (defun gnus-agent-fetch-headers (group articles &optional force)
672 (gnus-agent-load-alist group)
673 ;; Find out what headers we need to retrieve.
676 (assq (car articles) gnus-agent-article-alist))
678 (let ((arts articles))
680 (if (assq (cadr arts) gnus-agent-article-alist)
681 (setcdr arts (cddr arts))
682 (setq arts (cdr arts)))))
685 (gnus-message 7 "Fetching headers for %s..." group)
687 (set-buffer nntp-server-buffer)
688 (unless (eq 'nov (gnus-retrieve-headers articles group))
689 (nnvirtual-convert-headers))
690 ;; Save these headers for later processing.
691 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
694 (setq file (gnus-agent-article-name ".overview" group)))
695 (gnus-agent-braid-nov group articles file))
696 (gnus-make-directory (nnheader-translate-file-chars
697 (file-name-directory file)))
698 (write-region (point-min) (point-max) file nil 'silent)
699 (gnus-agent-save-alist group articles nil))
702 (defsubst gnus-agent-copy-nov-line (article)
704 (set-buffer gnus-agent-overview-buffer)
706 (if (eq article (read (current-buffer)))
707 (setq e (progn (forward-line 1) (point)))
709 (set-buffer nntp-server-buffer)
710 (insert-buffer-substring gnus-agent-overview-buffer b e)))
712 (defun gnus-agent-braid-nov (group articles file)
714 (set-buffer gnus-agent-overview-buffer)
715 (goto-char (point-min))
716 (set-buffer nntp-server-buffer)
718 (insert-file-contents file)
719 (goto-char (point-min))
720 (if (or (= (point-min) (point-max))
723 (< (read (current-buffer)) (car articles))))
724 ;; We have only headers that are after the older headers,
725 ;; so we just append them.
727 (goto-char (point-max))
728 (insert-buffer-substring gnus-agent-overview-buffer))
729 ;; We do it the hard way.
730 (nnheader-find-nov-line (car articles))
731 (gnus-agent-copy-nov-line (car articles))
735 (while (and (not (eobp))
736 (< (read (current-buffer)) (car articles)))
740 (gnus-agent-copy-nov-line (car articles))
741 (setq articles (cdr articles))))
744 (set-buffer gnus-agent-overview-buffer)
747 (set-buffer nntp-server-buffer)
748 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
750 (defun gnus-agent-load-alist (group &optional dir)
751 "Load the article-state alist for GROUP."
752 (setq gnus-agent-article-alist
753 (gnus-agent-read-file
755 (concat dir ".agentview")
756 (gnus-agent-article-name ".agentview" group)))))
758 (defun gnus-agent-save-alist (group &optional articles state dir)
759 "Load the article-state alist for GROUP."
760 (nnheader-temp-write (if dir
761 (concat dir ".agentview")
762 (gnus-agent-article-name ".agentview" group))
763 (princ (setq gnus-agent-article-alist
764 (nconc gnus-agent-article-alist
765 (mapcar (lambda (article) (cons article state))
770 (defun gnus-agent-article-name (article group)
771 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
772 (if (stringp article) article (string-to-number article))))
775 (defun gnus-agent-batch-fetch ()
776 "Start Gnus and fetch session."
779 (gnus-agent-fetch-session)
782 (defun gnus-agent-fetch-session ()
783 "Fetch all articles and headers that are eligible for fetching."
785 (unless gnus-agent-covered-methods
786 (error "No servers are covered by the Gnus agent"))
788 (error "Can't fetch articles while Gnus is unplugged"))
789 (let ((methods gnus-agent-covered-methods)
790 groups group gnus-command-method)
793 (setq gnus-command-method (car methods)
794 groups (gnus-groups-from-server (pop methods)))
795 (gnus-agent-with-fetch
796 (while (setq group (pop groups))
797 (gnus-agent-fetch-group-1 group gnus-command-method))))
798 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
800 (defun gnus-agent-fetch-group-1 (group method)
802 (let ((gnus-command-method method)
803 gnus-newsgroup-dependencies gnus-newsgroup-headers
804 gnus-newsgroup-scored gnus-headers gnus-score
805 gnus-use-cache articles score arts
806 category predicate info marks score-param)
808 (when (and (setq articles (gnus-list-of-unread-articles group))
809 (gnus-agent-fetch-headers group articles))
810 ;; Parse them and see which articles we want to fetch.
811 (setq gnus-newsgroup-dependencies
812 (make-vector (length articles) 0))
813 (setq gnus-newsgroup-headers
814 (gnus-get-newsgroup-headers-xover articles nil nil group))
815 (setq category (gnus-group-category group))
818 (or (gnus-group-get-parameter group 'agent-predicate)
821 (or (gnus-group-get-parameter group 'agent-score)
824 (gnus-score-headers (list (list score-param))))
826 (while (setq gnus-headers (pop gnus-newsgroup-headers))
828 (or (cdr (assq (mail-header-number gnus-headers)
829 gnus-newsgroup-scored))
830 gnus-summary-default-score))
831 (when (funcall predicate)
832 (push (mail-header-number gnus-headers)
834 ;; Fetch the articles.
836 (gnus-agent-fetch-articles group arts)))
837 ;; Perhaps we have some additional articles to fetch.
838 (setq arts (assq 'download (gnus-info-marks
839 (setq info (gnus-get-info group)))))
841 (gnus-agent-fetch-articles
842 group (gnus-uncompress-range (cdr arts)))
843 (setq marks (delq arts (gnus-info-marks info)))
844 (gnus-info-set-marks info marks))))
847 ;;; Agent Category Mode
850 (defvar gnus-category-mode-hook nil
851 "Hook run in `gnus-category-mode' buffers.")
853 (defvar gnus-category-line-format " %(%20c%): %g\n"
854 "Format of category lines.")
856 (defvar gnus-category-mode-line-format "Gnus: %%b"
857 "The format specification for the category mode line.")
859 (defvar gnus-agent-short-article 100
860 "Articles that have fewer lines than this are short.")
862 (defvar gnus-agent-long-article 200
863 "Articles that have more lines than this are long.")
865 (defvar gnus-agent-low-score 0
866 "Articles that have a score lower than this have a low score.")
868 (defvar gnus-agent-high-score 0
869 "Articles that have a score higher than this have a high score.")
872 ;;; Internal variables.
874 (defvar gnus-category-buffer "*Agent Category*")
876 (defvar gnus-category-line-format-alist
880 (defvar gnus-category-mode-line-format-alist
881 `((?u user-defined ?s)))
883 (defvar gnus-category-line-format-spec nil)
884 (defvar gnus-category-mode-line-format-spec nil)
886 (defvar gnus-category-mode-map nil)
887 (put 'gnus-category-mode 'mode-class 'special)
889 (unless gnus-category-mode-map
890 (setq gnus-category-mode-map (make-sparse-keymap))
891 (suppress-keymap gnus-category-mode-map)
893 (gnus-define-keys gnus-category-mode-map
894 "q" gnus-category-exit
895 "k" gnus-category-kill
896 "c" gnus-category-copy
897 "a" gnus-category-add
898 "p" gnus-category-edit-predicate
899 "g" gnus-category-edit-groups
900 "s" gnus-category-edit-score
901 "l" gnus-category-list
903 "\C-c\C-i" gnus-info-find-node
904 "\C-c\C-b" gnus-bug))
906 (defvar gnus-category-menu-hook nil
907 "*Hook run after the creation of the menu.")
909 (defun gnus-category-make-menu-bar ()
910 (gnus-turn-off-edit-menu 'category)
911 (unless (boundp 'gnus-category-menu)
913 gnus-category-menu gnus-category-mode-map ""
915 ["Add" gnus-category-add t]
916 ["Kill" gnus-category-kill t]
917 ["Copy" gnus-category-copy t]
918 ["Edit predicate" gnus-category-edit-predicate t]
919 ["Edit score" gnus-category-edit-score t]
920 ["Edit groups" gnus-category-edit-groups t]
921 ["Exit" gnus-category-exit t]))
923 (run-hooks 'gnus-category-menu-hook)))
925 (defun gnus-category-mode ()
926 "Major mode for listing and editing agent categories.
928 All normal editing commands are switched off.
929 \\<gnus-category-mode-map>
930 For more in-depth information on this mode, read the manual
931 (`\\[gnus-info-find-node]').
933 The following commands are available:
935 \\{gnus-category-mode-map}"
937 (when (gnus-visual-p 'category-menu 'menu)
938 (gnus-category-make-menu-bar))
939 (kill-all-local-variables)
940 (gnus-simplify-mode-line)
941 (setq major-mode 'gnus-category-mode)
942 (setq mode-name "Category")
943 (gnus-set-default-directory)
944 (setq mode-line-process nil)
945 (use-local-map gnus-category-mode-map)
946 (buffer-disable-undo (current-buffer))
947 (setq truncate-lines t)
948 (setq buffer-read-only t)
949 (run-hooks 'gnus-category-mode-hook))
951 (defalias 'gnus-category-position-point 'gnus-goto-colon)
953 (defun gnus-category-insert-line (category)
954 (let* ((name (car category))
955 (groups (length (cadddr category))))
957 (gnus-add-text-properties
961 (eval gnus-category-line-format-spec))
962 (list 'gnus-category name))))
964 (defun gnus-enter-category-buffer ()
965 "Go to the Category buffer."
967 (gnus-category-setup-buffer)
968 (gnus-configure-windows 'category)
969 (gnus-category-prepare))
971 (defun gnus-category-setup-buffer ()
972 (unless (get-buffer gnus-category-buffer)
974 (set-buffer (get-buffer-create gnus-category-buffer))
975 (gnus-add-current-to-buffer-list)
976 (gnus-category-mode))))
978 (defun gnus-category-prepare ()
979 (gnus-set-format 'category-mode)
980 (gnus-set-format 'category t)
981 (let ((alist gnus-category-alist)
982 (buffer-read-only nil))
985 (gnus-category-insert-line (pop alist)))
986 (goto-char (point-min))
987 (gnus-category-position-point)))
989 (defun gnus-category-name ()
990 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
991 (error "No category on the current line")))
993 (defun gnus-category-read ()
994 "Read the category alist."
995 (setq gnus-category-alist
996 (or (gnus-agent-read-file
997 (nnheader-concat gnus-agent-directory "lib/categories"))
998 (list (list 'default 'true nil nil)))))
1000 (defun gnus-category-write ()
1001 "Write the category alist."
1002 (setq gnus-category-predicate-cache nil
1003 gnus-category-group-cache nil)
1004 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1005 (prin1 gnus-category-alist (current-buffer))))
1007 (defun gnus-category-edit-predicate (category)
1008 "Edit the predicate for CATEGORY."
1009 (interactive (list (gnus-category-name)))
1010 (let ((info (assq category gnus-category-alist)))
1012 (cadr info) (format "Editing the predicate for category %s" category)
1013 `(lambda (predicate)
1014 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1015 (gnus-category-write)
1016 (gnus-category-list)))))
1018 (defun gnus-category-edit-score (category)
1019 "Edit the score expression for CATEGORY."
1020 (interactive (list (gnus-category-name)))
1021 (let ((info (assq category gnus-category-alist)))
1024 (format "Editing the score expression for category %s" category)
1026 (setf (caddr (assq ',category gnus-category-alist)) groups)
1027 (gnus-category-write)
1028 (gnus-category-list)))))
1030 (defun gnus-category-edit-groups (category)
1031 "Edit the group list for CATEGORY."
1032 (interactive (list (gnus-category-name)))
1033 (let ((info (assq category gnus-category-alist)))
1035 (cadddr info) (format "Editing the group list for category %s" category)
1037 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1038 (gnus-category-write)
1039 (gnus-category-list)))))
1041 (defun gnus-category-kill (category)
1042 "Kill the current category."
1043 (interactive (list (gnus-category-name)))
1044 (let ((info (assq category gnus-category-alist))
1045 (buffer-read-only nil))
1047 (gnus-category-write)
1048 (setq gnus-category-alist (delq info gnus-category-alist))))
1050 (defun gnus-category-copy (category to)
1051 "Copy the current category."
1052 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1053 (let ((info (assq category gnus-category-alist)))
1054 (push (list to (gnus-copy-sequence (cadr info))
1055 (gnus-copy-sequence (caddr info)) nil)
1056 gnus-category-alist)
1057 (gnus-category-write)
1058 (gnus-category-list)))
1060 (defun gnus-category-add (category)
1061 "Create a new category."
1062 (interactive "SCategory name: ")
1063 (when (assq category gnus-category-alist)
1064 (error "Category %s already exists" category))
1065 (push (list category 'true nil nil)
1066 gnus-category-alist)
1067 (gnus-category-write)
1068 (gnus-category-list))
1070 (defun gnus-category-list ()
1071 "List all categories."
1073 (gnus-category-prepare))
1075 (defun gnus-category-exit ()
1076 "Return to the group buffer."
1078 (kill-buffer (current-buffer))
1079 (gnus-configure-windows 'group t))
1081 ;; To avoid having 8-bit characters in the source file.
1082 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1084 (defvar gnus-category-predicate-alist
1085 '((spam . gnus-agent-spam-p)
1086 (short . gnus-agent-short-p)
1087 (long . gnus-agent-long-p)
1088 (low . gnus-agent-low-scored-p)
1089 (high . gnus-agent-high-scored-p)
1090 (true . gnus-agent-true)
1091 (false . gnus-agent-false))
1092 "Mapping from short score predicate symbols to predicate functions.")
1094 (defun gnus-agent-spam-p ()
1095 "Say whether an article is spam or not."
1096 (unless gnus-agent-spam-hashtb
1097 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1098 (if (not (equal (mail-header-references gnus-headers) ""))
1100 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1102 (gnus-gethash string gnus-agent-spam-hashtb)
1103 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1105 (defun gnus-agent-short-p ()
1106 "Say whether an article is short or not."
1107 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1109 (defun gnus-agent-long-p ()
1110 "Say whether an article is long or not."
1111 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1113 (defun gnus-agent-low-scored-p ()
1114 "Say whether an article has a low score or not."
1115 (< gnus-score gnus-agent-low-score))
1117 (defun gnus-agent-high-scored-p ()
1118 "Say whether an article has a high score or not."
1119 (> gnus-score gnus-agent-low-score))
1121 (defun gnus-category-make-function (cat)
1122 "Make a function from category CAT."
1123 `(lambda () ,(gnus-category-make-function-1 cat)))
1125 (defun gnus-agent-true ()
1129 (defun gnus-agent-false ()
1133 (defun gnus-category-make-function-1 (cat)
1134 "Make a function from category CAT."
1136 ;; Functions are just returned as is.
1138 (gnus-functionp cat))
1139 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1141 ;; More complex category.
1144 ((memq (car cat) '(& and))
1146 ((memq (car cat) '(| or))
1148 ((memq (car cat) gnus-category-not)
1150 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1152 (error "Unknown category type: %s" cat))))
1154 (defun gnus-get-predicate (predicate)
1155 "Return the predicate for CATEGORY."
1156 (or (cdr (assoc predicate gnus-category-predicate-cache))
1157 (cdar (push (cons predicate
1158 (gnus-category-make-function predicate))
1159 gnus-category-predicate-cache))))
1161 (defun gnus-group-category (group)
1162 "Return the category GROUP belongs to."
1163 (unless gnus-category-group-cache
1164 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1165 (let ((cs gnus-category-alist)
1167 (while (setq cat (pop cs))
1168 (setq groups (cadddr cat))
1170 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1171 (or (gnus-gethash group gnus-category-group-cache)
1172 (assq 'default gnus-category-alist)))
1174 (defun gnus-agent-expire ()
1175 "Expire all old articles."
1177 (let ((methods gnus-agent-covered-methods)
1178 (alist (cdr gnus-newsrc-alist))
1179 gnus-command-method ofiles info method file group)
1180 (while (setq gnus-command-method (pop methods))
1181 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1182 (gnus-agent-directory)))))
1183 (while (setq info (pop alist))
1184 (when (and (gnus-agent-method-p
1185 (setq gnus-command-method
1186 (gnus-find-method-for-group
1187 (setq group (gnus-info-group info)))))
1191 (gnus-agent-directory)
1192 (gnus-agent-group-path group) "/.overview"))
1194 (setq ofiles (delete file ofiles))
1195 (gnus-agent-expire-group file group)))
1197 (gnus-agent-expire-group (pop ofiles)))))
1199 (defun gnus-agent-expire-directory (dir)
1200 "Expire all groups in DIR recursively."
1201 (when (file-directory-p dir)
1202 (let ((files (directory-files dir t))
1204 (while (setq file (pop files))
1206 ((member (file-name-nondirectory file) '("." ".."))
1209 ((file-directory-p file)
1211 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1212 ((string-match "\\.overview$" file)
1214 (push file ofiles))))
1217 (defun gnus-agent-expire-group (overview &optional group)
1218 "Expire articles in OVERVIEW."
1219 (gnus-message 5 "Expiring %s..." overview)
1220 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1221 (dir (file-name-directory overview))
1222 (info (when group (gnus-get-info group)))
1223 headers article file point unreads)
1224 (gnus-agent-load-alist nil dir)
1228 (gnus-list-of-unread-articles group)
1229 (gnus-uncompress-range
1230 (cdr (assq 'tick (gnus-info-marks info))))
1231 (gnus-uncompress-range
1232 (cdr (assq 'dormant (gnus-info-marks info)))))))
1233 (nnheader-temp-write overview
1234 (insert-file-contents overview)
1235 (goto-char (point-min))
1237 (setq point (point))
1239 (setq headers (inline (nnheader-parse-nov)))
1243 (setq headers nil)))
1245 (unless (memq (setq article (mail-header-number headers)) unreads)
1248 (inline (nnmail-date-to-time
1249 (mail-header-date headers)))))
1253 (setq gnus-agent-article-alist
1254 (delq (assq article gnus-agent-article-alist)
1255 gnus-agent-article-alist))
1256 (when (file-exists-p
1257 (setq file (concat dir (number-to-string article))))
1258 (delete-file file))))))
1259 (gnus-agent-save-alist nil nil nil dir))))
1261 (provide 'gnus-agent)
1263 ;;; gnus-agent.el ends here