1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
33 "Where the Gnus agent will store its files."
37 (defcustom gnus-agent-plugged-hook nil
38 "Hook run when plugging into the network."
42 (defcustom gnus-agent-unplugged-hook nil
43 "Hook run when unplugging from the network."
47 (defcustom gnus-agent-handle-level gnus-level-subscribed
48 "Groups on levels higher than this variable will be ignored by the Agent."
52 ;;; Internal variables
54 (defvar gnus-agent-history-buffers nil)
55 (defvar gnus-agent-buffer-alist nil)
56 (defvar gnus-agent-article-alist nil)
57 (defvar gnus-agent-group-alist nil)
58 (defvar gnus-agent-covered-methods nil)
59 (defvar gnus-category-alist nil)
60 (defvar gnus-agent-current-history nil)
61 (defvar gnus-agent-overview-buffer nil)
62 (defvar gnus-category-predicate-cache nil)
63 (defvar gnus-category-group-cache nil)
64 (defvar gnus-agent-spam-hashtb nil)
65 (defvar gnus-agent-file-name nil)
66 (defvar gnus-agent-send-mail-function nil)
67 (defvar gnus-agent-article-file-coding-system 'no-conversion)
77 (defun gnus-open-agent ()
79 (gnus-agent-read-servers)
81 (setq gnus-agent-overview-buffer
82 (get-buffer-create " *Gnus agent overview*"))
83 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
84 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
85 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
87 (gnus-add-shutdown 'gnus-close-agent 'gnus)
89 (defun gnus-close-agent ()
90 (setq gnus-agent-covered-methods nil
91 gnus-category-predicate-cache nil
92 gnus-category-group-cache nil
93 gnus-agent-spam-hashtb nil)
94 (gnus-kill-buffer gnus-agent-overview-buffer))
100 (defun gnus-agent-read-file (file)
101 "Load FILE and do a `read' there."
102 (nnheader-temp-write nil
104 (insert-file-contents file)
105 (goto-char (point-min))
106 (read (current-buffer)))))
108 (defsubst gnus-agent-method ()
109 (concat (symbol-name (car gnus-command-method)) "/"
110 (if (equal (cadr gnus-command-method) "")
112 (cadr gnus-command-method))))
114 (defsubst gnus-agent-directory ()
115 "Path of the Gnus agent directory."
116 (nnheader-concat gnus-agent-directory
117 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
119 (defun gnus-agent-lib-file (file)
120 "The full path of the Gnus agent library FILE."
121 (concat (gnus-agent-directory) "agent.lib/" file))
123 ;;; Fetching setup functions.
125 (defun gnus-agent-start-fetch ()
126 "Initialize data structures for efficient fetching."
127 (gnus-agent-open-history)
128 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
130 (defun gnus-agent-stop-fetch ()
131 "Save all data structures and clean up."
132 (gnus-agent-save-history)
133 (gnus-agent-close-history)
134 (setq gnus-agent-spam-hashtb nil)
136 (set-buffer nntp-server-buffer)
139 (defmacro gnus-agent-with-fetch (&rest forms)
143 (gnus-agent-start-fetch)
145 (gnus-agent-stop-fetch)))
147 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
148 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
154 (defvar gnus-agent-mode-hook nil
155 "Hook run when installing agent mode.")
157 (defvar gnus-agent-mode nil)
158 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
160 (defun gnus-agent-mode ()
161 "Minor mode for providing a agent support in Gnus buffers."
162 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
163 (symbol-name major-mode))
164 (match-string 1 (symbol-name major-mode))))
165 (mode (intern (format "gnus-agent-%s-mode" buffer))))
166 (set (make-local-variable 'gnus-agent-mode) t)
168 (set (make-local-variable mode) t)
170 (when (gnus-visual-p 'agent-menu 'menu)
171 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
172 (unless (assq 'gnus-agent-mode minor-mode-alist)
173 (push gnus-agent-mode-status minor-mode-alist))
174 (unless (assq mode minor-mode-map-alist)
175 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
177 minor-mode-map-alist))
178 (gnus-agent-toggle-plugged gnus-plugged)
179 (gnus-run-hooks 'gnus-agent-mode-hook)))
181 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
182 (gnus-define-keys gnus-agent-group-mode-map
183 "Ju" gnus-agent-fetch-groups
184 "Jc" gnus-enter-category-buffer
185 "Jj" gnus-agent-toggle-plugged
186 "Js" gnus-agent-fetch-session
187 "JS" gnus-group-send-drafts
188 "Ja" gnus-agent-add-group)
190 (defun gnus-agent-group-make-menu-bar ()
191 (unless (boundp 'gnus-agent-group-menu)
193 gnus-agent-group-menu gnus-agent-group-mode-map ""
195 ["Toggle plugged" gnus-agent-toggle-plugged t]
196 ["List categories" gnus-enter-category-buffer t]
197 ["Send drafts" gnus-group-send-drafts gnus-plugged]
199 ["All" gnus-agent-fetch-session gnus-plugged]
200 ["Group" gnus-agent-fetch-group gnus-plugged])))))
202 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
203 (gnus-define-keys gnus-agent-summary-mode-map
204 "Jj" gnus-agent-toggle-plugged
205 "J#" gnus-agent-mark-article
206 "J\M-#" gnus-agent-unmark-article
207 "@" gnus-agent-toggle-mark
208 "Jc" gnus-agent-catchup)
210 (defun gnus-agent-summary-make-menu-bar ()
211 (unless (boundp 'gnus-agent-summary-menu)
213 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
215 ["Toggle plugged" gnus-agent-toggle-plugged t]
216 ["Mark as downloadable" gnus-agent-mark-article t]
217 ["Unmark as downloadable" gnus-agent-unmark-article t]
218 ["Toggle mark" gnus-agent-toggle-mark t]
219 ["Catchup undownloaded" gnus-agent-catchup t]))))
221 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
222 (gnus-define-keys gnus-agent-server-mode-map
223 "Jj" gnus-agent-toggle-plugged
224 "Ja" gnus-agent-add-server
225 "Jr" gnus-agent-remove-server)
227 (defun gnus-agent-server-make-menu-bar ()
228 (unless (boundp 'gnus-agent-server-menu)
230 gnus-agent-server-menu gnus-agent-server-mode-map ""
232 ["Toggle plugged" gnus-agent-toggle-plugged t]
233 ["Add" gnus-agent-add-server t]
234 ["Remove" gnus-agent-remove-server t]))))
236 (defun gnus-agent-toggle-plugged (plugged)
237 "Toggle whether Gnus is unplugged or not."
238 (interactive (list (not gnus-plugged)))
241 (setq gnus-plugged plugged)
242 (gnus-run-hooks 'gnus-agent-plugged-hook)
243 (setcar (cdr gnus-agent-mode-status) " Plugged"))
244 (gnus-agent-close-connections)
245 (setq gnus-plugged plugged)
246 (gnus-run-hooks 'gnus-agent-unplugged-hook)
247 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
248 (set-buffer-modified-p t))
250 (defun gnus-agent-close-connections ()
251 "Close all methods covered by the Gnus agent."
252 (let ((methods gnus-agent-covered-methods))
254 (gnus-close-server (pop methods)))))
257 (defun gnus-unplugged ()
258 "Start Gnus unplugged."
260 (setq gnus-plugged nil)
264 (defun gnus-plugged ()
265 "Start Gnus plugged."
267 (setq gnus-plugged t)
271 (defun gnus-agentize ()
272 "Allow Gnus to be an offline newsreader.
273 The normal usage of this command is to put the following as the
274 last form in your `.gnus.el' file:
278 This will modify the `gnus-before-startup-hook', `gnus-post-method',
279 and `message-send-mail-function' variables, and install the Gnus
280 agent minor mode in all Gnus buffers."
283 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
284 (unless gnus-agent-send-mail-function
285 (setq gnus-agent-send-mail-function message-send-mail-function
286 message-send-mail-function 'gnus-agent-send-mail))
287 (unless gnus-agent-covered-methods
288 (setq gnus-agent-covered-methods (list gnus-select-method))))
290 (defun gnus-agent-queue-setup ()
291 "Make sure the queue group exists."
292 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
293 (gnus-request-create-group "queue" '(nndraft ""))
294 (let ((gnus-level-default-subscribed 1))
295 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
296 (gnus-group-set-parameter
297 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
299 (defun gnus-agent-send-mail ()
301 (funcall gnus-agent-send-mail-function)
302 (goto-char (point-min))
304 (concat "^" (regexp-quote mail-header-separator) "\n"))
306 (gnus-request-accept-article "nndraft:queue")))
309 ;;; Group mode commands
312 (defun gnus-agent-fetch-groups (n)
313 "Put all new articles in the current groups into the agent."
315 (gnus-group-iterate n 'gnus-agent-fetch-group))
317 (defun gnus-agent-fetch-group (group)
318 "Put all new articles in GROUP into the agent."
319 (interactive (list (gnus-group-group-name)))
321 (error "No group on the current line"))
322 (let ((gnus-command-method (gnus-find-method-for-group group)))
323 (gnus-agent-with-fetch
324 (gnus-agent-fetch-group-1 group gnus-command-method)
325 (gnus-message 5 "Fetching %s...done" group))))
327 (defun gnus-agent-add-group (category arg)
328 "Add the current group to an agent category."
334 (mapcar (lambda (cat) (list (symbol-name (car cat))))
338 (let ((cat (assq category gnus-category-alist))
340 (gnus-group-iterate arg
342 (when (cadddr (setq c (gnus-group-category group)))
343 (setf (cadddr c) (delete group (cadddr c))))
344 (push group groups)))
345 (setf (cadddr cat) (nconc (cadddr cat) groups))
346 (gnus-category-write)))
349 ;;; Server mode commands
352 (defun gnus-agent-add-server (server)
353 "Enroll SERVER in 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 (when (member method gnus-agent-covered-methods)
359 (error "Server already in the agent program"))
360 (push method gnus-agent-covered-methods)
361 (gnus-agent-write-servers)
362 (message "Entered %s into the agent" server)))
364 (defun gnus-agent-remove-server (server)
365 "Remove SERVER from the agent program."
366 (interactive (list (gnus-server-server-name)))
368 (error "No server on the current line"))
369 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
370 (unless (member method gnus-agent-covered-methods)
371 (error "Server not in the agent program"))
372 (setq gnus-agent-covered-methods
373 (delete method gnus-agent-covered-methods))
374 (gnus-agent-write-servers)
375 (message "Removed %s from the agent" server)))
377 (defun gnus-agent-read-servers ()
378 "Read the alist of covered servers."
379 (setq gnus-agent-covered-methods
380 (gnus-agent-read-file
381 (nnheader-concat gnus-agent-directory "lib/servers"))))
383 (defun gnus-agent-write-servers ()
384 "Write the alist of covered servers."
385 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
386 (prin1 gnus-agent-covered-methods (current-buffer))))
392 (defun gnus-agent-mark-article (n &optional unmark)
393 "Mark the next N articles as downloadable.
394 If N is negative, mark backward instead. If UNMARK is non-nil, remove
395 the mark instead. The difference between N and the actual number of
396 articles marked is returned."
398 (let ((backward (< n 0))
403 (gnus-summary-set-agent-mark
404 (gnus-summary-article-number) unmark)
405 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
408 (gnus-message 7 "No more articles"))
409 (gnus-summary-recenter)
410 (gnus-summary-position-point)
413 (defun gnus-agent-unmark-article (n)
414 "Remove the downloadable mark from the next N articles.
415 If N is negative, unmark backward instead. The difference between N and
416 the actual number of articles unmarked is returned."
418 (gnus-agent-mark-article n t))
420 (defun gnus-agent-toggle-mark (n)
421 "Toggle the downloadable mark from the next N articles.
422 If N is negative, toggle backward instead. The difference between N and
423 the actual number of articles toggled is returned."
425 (gnus-agent-mark-article n 'toggle))
427 (defun gnus-summary-set-agent-mark (article &optional unmark)
428 "Mark ARTICLE as downloadable."
429 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
430 (memq article gnus-newsgroup-downloadable)
434 (setq gnus-newsgroup-downloadable
435 (delq article gnus-newsgroup-downloadable))
436 (push article gnus-newsgroup-undownloaded))
437 (setq gnus-newsgroup-undownloaded
438 (delq article gnus-newsgroup-undownloaded))
439 (push article gnus-newsgroup-downloadable))
440 (gnus-summary-update-mark
441 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
444 (defun gnus-agent-get-undownloaded-list ()
445 "Mark all unfetched articles as read."
446 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
447 (when (and (not gnus-plugged)
448 (gnus-agent-method-p gnus-command-method))
449 (gnus-agent-load-alist gnus-newsgroup-name)
450 (let ((articles gnus-newsgroup-unreads)
452 (while (setq article (pop articles))
453 (unless (or (cdr (assq article gnus-agent-article-alist))
454 (memq article gnus-newsgroup-downloadable))
455 (push article gnus-newsgroup-undownloaded)))))))
457 (defun gnus-agent-catchup ()
458 "Mark all undownloaded articles as read."
461 (while gnus-newsgroup-undownloaded
462 (gnus-summary-mark-article
463 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
464 (gnus-summary-position-point))
467 ;;; Internal functions
470 (defun gnus-agent-save-active (method)
471 (when (gnus-agent-method-p method)
472 (let* ((gnus-command-method method)
473 (file (gnus-agent-lib-file "active")))
474 (gnus-make-directory (file-name-directory file))
475 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
476 (write-region (point-min) (point-max) file nil 'silent))
477 (when (file-exists-p (gnus-agent-lib-file "groups"))
478 (delete-file (gnus-agent-lib-file "groups"))))))
480 (defun gnus-agent-save-groups (method)
481 (let* ((gnus-command-method method)
482 (file (gnus-agent-lib-file "groups")))
483 (gnus-make-directory (file-name-directory file))
484 (write-region (point-min) (point-max) file nil 'silent))
485 (when (file-exists-p (gnus-agent-lib-file "active"))
486 (delete-file (gnus-agent-lib-file "active"))))
488 (defun gnus-agent-group-path (group)
489 "Translate GROUP into a path."
490 (if nnmail-use-long-file-names
492 (nnheader-replace-chars-in-string
493 (nnheader-translate-file-chars group)
498 (defun gnus-agent-method-p (method)
499 "Say whether METHOD is covered by the agent."
500 (member method gnus-agent-covered-methods))
502 (defun gnus-agent-get-function (method)
503 (if (and (not gnus-plugged)
504 (gnus-agent-method-p method))
510 ;;; History functions
512 (defun gnus-agent-history-buffer ()
513 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
515 (defun gnus-agent-open-history ()
517 (push (cons (gnus-agent-method)
518 (set-buffer (get-buffer-create
519 (format " *Gnus agent %s history*"
520 (gnus-agent-method)))))
521 gnus-agent-history-buffers)
524 (let ((file (gnus-agent-lib-file "history")))
525 (when (file-exists-p file)
527 (set (make-local-variable 'gnus-agent-file-name) file))))
529 (defun gnus-agent-save-history ()
531 (set-buffer gnus-agent-current-history)
532 (gnus-make-directory (file-name-directory gnus-agent-file-name))
533 (write-region (1+ (point-min)) (point-max)
534 gnus-agent-file-name nil 'silent)))
536 (defun gnus-agent-close-history ()
537 (when (gnus-buffer-live-p gnus-agent-current-history)
538 (kill-buffer gnus-agent-current-history)
539 (setq gnus-agent-history-buffers
540 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
541 gnus-agent-history-buffers))))
543 (defun gnus-agent-enter-history (id group-arts date)
545 (set-buffer gnus-agent-current-history)
546 (goto-char (point-max))
547 (insert id "\t" (number-to-string date) "\t")
549 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
553 (defun gnus-agent-article-in-history-p (id)
555 (set-buffer (gnus-agent-history-buffer))
556 (goto-char (point-min))
557 (search-forward (concat "\n" id "\t") nil t)))
559 (defun gnus-agent-history-path (id)
561 (set-buffer (gnus-agent-history-buffer))
562 (goto-char (point-min))
563 (when (search-forward (concat "\n" id "\t") nil t)
564 (let ((method (gnus-agent-method)))
566 (while (not (numberp (setq group (read (current-buffer)))))
567 (push (concat method "/" group) paths))
568 (nreverse paths))))))
574 (defun gnus-agent-fetch-articles (group articles)
575 "Fetch ARTICLES from GROUP and put them into the agent."
577 ;; Prune off articles that we have already fetched.
579 (cdr (assq (car articles) gnus-agent-article-alist)))
581 (let ((arts articles))
583 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
584 (setcdr arts (cddr arts))
585 (setq arts (cdr arts)))))
588 (gnus-agent-directory)
589 (gnus-agent-group-path group) "/"))
590 (date (gnus-time-to-day (current-time)))
592 pos alists crosses id elem)
593 (gnus-make-directory dir)
594 (gnus-message 7 "Fetching articles for %s..." group)
595 ;; Fetch the articles from the backend.
596 (if (gnus-check-backend-function 'retrieve-articles group)
597 (setq pos (gnus-retrieve-articles articles group))
598 (nnheader-temp-write nil
599 (let ((buf (current-buffer))
601 (while (setq article (pop articles))
602 (when (gnus-request-article article group)
603 (goto-char (point-max))
604 (push (cons article (point)) pos)
605 (insert-buffer-substring nntp-server-buffer)))
606 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
607 (setq pos (nreverse pos)))))
608 ;; Then save these articles into the agent.
610 (set-buffer nntp-server-buffer)
612 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
613 (goto-char (point-min))
614 (when (search-forward "\n\n" nil t)
615 (when (search-backward "\nXrefs: " nil t)
616 ;; Handle crossposting.
617 (skip-chars-forward "^ ")
618 (skip-chars-forward " ")
620 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
621 (push (cons (buffer-substring (match-beginning 1)
623 (buffer-substring (match-beginning 2)
626 (goto-char (match-end 0)))
627 (gnus-agent-crosspost crosses (caar pos))))
628 (goto-char (point-min))
629 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
630 (setq id "No-Message-ID-in-article")
631 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
632 (let ((coding-system-for-write
633 gnus-agent-article-file-coding-system))
634 (write-region (point-min) (point-max)
635 (concat dir (number-to-string (caar pos)))
637 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
639 (gnus-agent-enter-history
640 id (or crosses (list (cons group (caar pos)))) date)
643 (gnus-agent-save-alist group)))))
645 (defun gnus-agent-crosspost (crosses article)
646 (let (gnus-agent-article-alist group alist beg end)
648 (set-buffer gnus-agent-overview-buffer)
649 (when (nnheader-find-nov-line article)
652 (setq end (progn (forward-line 1) (point)))))
654 (setq group (caar crosses))
655 (unless (setq alist (assoc group gnus-agent-group-alist))
656 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
657 gnus-agent-group-alist))
658 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
660 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
662 (when (= (point-max) (point-min))
663 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
665 (insert-file-contents
666 (gnus-agent-article-name ".overview" group))))
667 (nnheader-find-nov-line (string-to-number (cdar crosses)))
668 (insert (string-to-number (cdar crosses)))
669 (insert-buffer-substring gnus-agent-overview-buffer beg end))
672 (defun gnus-agent-flush-cache ()
674 (while gnus-agent-buffer-alist
675 (set-buffer (cdar gnus-agent-buffer-alist))
676 (write-region (point-min) (point-max)
677 (gnus-agent-article-name ".overview"
678 (caar gnus-agent-buffer-alist))
680 (pop gnus-agent-buffer-alist))
681 (while gnus-agent-group-alist
682 (nnheader-temp-write (caar gnus-agent-group-alist)
683 (princ (cdar gnus-agent-group-alist))
685 (pop gnus-agent-group-alist))))
687 (defun gnus-agent-fetch-headers (group articles &optional force)
688 (gnus-agent-load-alist group)
689 ;; Find out what headers we need to retrieve.
692 (assq (car articles) gnus-agent-article-alist))
694 (let ((arts articles))
696 (if (assq (cadr arts) gnus-agent-article-alist)
697 (setcdr arts (cddr arts))
698 (setq arts (cdr arts)))))
701 (gnus-message 7 "Fetching headers for %s..." group)
703 (set-buffer nntp-server-buffer)
704 (unless (eq 'nov (gnus-retrieve-headers articles group))
705 (nnvirtual-convert-headers))
706 ;; Save these headers for later processing.
707 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
710 (setq file (gnus-agent-article-name ".overview" group)))
711 (gnus-agent-braid-nov group articles file))
712 (gnus-make-directory (nnheader-translate-file-chars
713 (file-name-directory file)))
714 (write-region (point-min) (point-max) file nil 'silent)
715 (gnus-agent-save-alist group articles nil))
718 (defsubst gnus-agent-copy-nov-line (article)
720 (set-buffer gnus-agent-overview-buffer)
722 (if (eq article (read (current-buffer)))
723 (setq e (progn (forward-line 1) (point)))
725 (set-buffer nntp-server-buffer)
726 (insert-buffer-substring gnus-agent-overview-buffer b e)))
728 (defun gnus-agent-braid-nov (group articles file)
730 (set-buffer gnus-agent-overview-buffer)
731 (goto-char (point-min))
732 (set-buffer nntp-server-buffer)
734 (insert-file-contents file)
735 (goto-char (point-min))
736 (if (or (= (point-min) (point-max))
739 (< (read (current-buffer)) (car articles))))
740 ;; We have only headers that are after the older headers,
741 ;; so we just append them.
743 (goto-char (point-max))
744 (insert-buffer-substring gnus-agent-overview-buffer))
745 ;; We do it the hard way.
746 (nnheader-find-nov-line (car articles))
747 (gnus-agent-copy-nov-line (car articles))
751 (while (and (not (eobp))
752 (< (read (current-buffer)) (car articles)))
756 (gnus-agent-copy-nov-line (car articles))
757 (setq articles (cdr articles))))
760 (set-buffer gnus-agent-overview-buffer)
763 (set-buffer nntp-server-buffer)
764 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
766 (defun gnus-agent-load-alist (group &optional dir)
767 "Load the article-state alist for GROUP."
768 (setq gnus-agent-article-alist
769 (gnus-agent-read-file
771 (concat dir ".agentview")
772 (gnus-agent-article-name ".agentview" group)))))
774 (defun gnus-agent-save-alist (group &optional articles state dir)
775 "Load the article-state alist for GROUP."
776 (nnheader-temp-write (if dir
777 (concat dir ".agentview")
778 (gnus-agent-article-name ".agentview" group))
779 (princ (setq gnus-agent-article-alist
780 (nconc gnus-agent-article-alist
781 (mapcar (lambda (article) (cons article state))
786 (defun gnus-agent-article-name (article group)
787 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
788 (if (stringp article) article (string-to-number article))))
791 (defun gnus-agent-batch-fetch ()
792 "Start Gnus and fetch session."
795 (gnus-agent-fetch-session)
798 (defun gnus-agent-fetch-session ()
799 "Fetch all articles and headers that are eligible for fetching."
801 (unless gnus-agent-covered-methods
802 (error "No servers are covered by the Gnus agent"))
804 (error "Can't fetch articles while Gnus is unplugged"))
805 (let ((methods gnus-agent-covered-methods)
806 groups group gnus-command-method)
809 (setq gnus-command-method (car methods)
810 groups (gnus-groups-from-server (pop methods)))
811 (gnus-agent-with-fetch
812 (while (setq group (pop groups))
813 (when (<= (gnus-group-level group) gnus-agent-handle-level)
814 (gnus-agent-fetch-group-1 group gnus-command-method)))))
815 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
817 (defun gnus-agent-fetch-group-1 (group method)
819 (let ((gnus-command-method method)
820 gnus-newsgroup-dependencies gnus-newsgroup-headers
821 gnus-newsgroup-scored gnus-headers gnus-score
822 gnus-use-cache articles score arts
823 category predicate info marks score-param)
825 (when (and (setq articles (gnus-list-of-unread-articles group))
826 (gnus-agent-fetch-headers group articles))
827 ;; Parse them and see which articles we want to fetch.
828 (setq gnus-newsgroup-dependencies
829 (make-vector (length articles) 0))
830 (setq gnus-newsgroup-headers
831 (gnus-get-newsgroup-headers-xover articles nil nil group))
832 (setq category (gnus-group-category group))
835 (or (gnus-group-get-parameter group 'agent-predicate)
838 (or (gnus-group-get-parameter group 'agent-score)
841 (gnus-score-headers (list (list score-param))))
843 (while (setq gnus-headers (pop gnus-newsgroup-headers))
845 (or (cdr (assq (mail-header-number gnus-headers)
846 gnus-newsgroup-scored))
847 gnus-summary-default-score))
848 (when (funcall predicate)
849 (push (mail-header-number gnus-headers)
851 ;; Fetch the articles.
853 (gnus-agent-fetch-articles group arts)))
854 ;; Perhaps we have some additional articles to fetch.
855 (setq arts (assq 'download (gnus-info-marks
856 (setq info (gnus-get-info group)))))
858 (gnus-agent-fetch-articles
859 group (gnus-uncompress-range (cdr arts)))
860 (setq marks (delq arts (gnus-info-marks info)))
861 (gnus-info-set-marks info marks))))
864 ;;; Agent Category Mode
867 (defvar gnus-category-mode-hook nil
868 "Hook run in `gnus-category-mode' buffers.")
870 (defvar gnus-category-line-format " %(%20c%): %g\n"
871 "Format of category lines.")
873 (defvar gnus-category-mode-line-format "Gnus: %%b"
874 "The format specification for the category mode line.")
876 (defvar gnus-agent-short-article 100
877 "Articles that have fewer lines than this are short.")
879 (defvar gnus-agent-long-article 200
880 "Articles that have more lines than this are long.")
882 (defvar gnus-agent-low-score 0
883 "Articles that have a score lower than this have a low score.")
885 (defvar gnus-agent-high-score 0
886 "Articles that have a score higher than this have a high score.")
889 ;;; Internal variables.
891 (defvar gnus-category-buffer "*Agent Category*")
893 (defvar gnus-category-line-format-alist
897 (defvar gnus-category-mode-line-format-alist
898 `((?u user-defined ?s)))
900 (defvar gnus-category-line-format-spec nil)
901 (defvar gnus-category-mode-line-format-spec nil)
903 (defvar gnus-category-mode-map nil)
904 (put 'gnus-category-mode 'mode-class 'special)
906 (unless gnus-category-mode-map
907 (setq gnus-category-mode-map (make-sparse-keymap))
908 (suppress-keymap gnus-category-mode-map)
910 (gnus-define-keys gnus-category-mode-map
911 "q" gnus-category-exit
912 "k" gnus-category-kill
913 "c" gnus-category-copy
914 "a" gnus-category-add
915 "p" gnus-category-edit-predicate
916 "g" gnus-category-edit-groups
917 "s" gnus-category-edit-score
918 "l" gnus-category-list
920 "\C-c\C-i" gnus-info-find-node
921 "\C-c\C-b" gnus-bug))
923 (defvar gnus-category-menu-hook nil
924 "*Hook run after the creation of the menu.")
926 (defun gnus-category-make-menu-bar ()
927 (gnus-turn-off-edit-menu 'category)
928 (unless (boundp 'gnus-category-menu)
930 gnus-category-menu gnus-category-mode-map ""
932 ["Add" gnus-category-add t]
933 ["Kill" gnus-category-kill t]
934 ["Copy" gnus-category-copy t]
935 ["Edit predicate" gnus-category-edit-predicate t]
936 ["Edit score" gnus-category-edit-score t]
937 ["Edit groups" gnus-category-edit-groups t]
938 ["Exit" gnus-category-exit t]))
940 (gnus-run-hooks 'gnus-category-menu-hook)))
942 (defun gnus-category-mode ()
943 "Major mode for listing and editing agent categories.
945 All normal editing commands are switched off.
946 \\<gnus-category-mode-map>
947 For more in-depth information on this mode, read the manual
948 (`\\[gnus-info-find-node]').
950 The following commands are available:
952 \\{gnus-category-mode-map}"
954 (when (gnus-visual-p 'category-menu 'menu)
955 (gnus-category-make-menu-bar))
956 (kill-all-local-variables)
957 (gnus-simplify-mode-line)
958 (setq major-mode 'gnus-category-mode)
959 (setq mode-name "Category")
960 (gnus-set-default-directory)
961 (setq mode-line-process nil)
962 (use-local-map gnus-category-mode-map)
963 (buffer-disable-undo (current-buffer))
964 (setq truncate-lines t)
965 (setq buffer-read-only t)
966 (gnus-run-hooks 'gnus-category-mode-hook))
968 (defalias 'gnus-category-position-point 'gnus-goto-colon)
970 (defun gnus-category-insert-line (category)
971 (let* ((name (car category))
972 (groups (length (cadddr category))))
974 (gnus-add-text-properties
978 (eval gnus-category-line-format-spec))
979 (list 'gnus-category name))))
981 (defun gnus-enter-category-buffer ()
982 "Go to the Category buffer."
984 (gnus-category-setup-buffer)
985 (gnus-configure-windows 'category)
986 (gnus-category-prepare))
988 (defun gnus-category-setup-buffer ()
989 (unless (get-buffer gnus-category-buffer)
991 (set-buffer (get-buffer-create gnus-category-buffer))
992 (gnus-add-current-to-buffer-list)
993 (gnus-category-mode))))
995 (defun gnus-category-prepare ()
996 (gnus-set-format 'category-mode)
997 (gnus-set-format 'category t)
998 (let ((alist gnus-category-alist)
999 (buffer-read-only nil))
1002 (gnus-category-insert-line (pop alist)))
1003 (goto-char (point-min))
1004 (gnus-category-position-point)))
1006 (defun gnus-category-name ()
1007 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1008 (error "No category on the current line")))
1010 (defun gnus-category-read ()
1011 "Read the category alist."
1012 (setq gnus-category-alist
1013 (or (gnus-agent-read-file
1014 (nnheader-concat gnus-agent-directory "lib/categories"))
1015 (list (list 'default 'short nil nil)))))
1017 (defun gnus-category-write ()
1018 "Write the category alist."
1019 (setq gnus-category-predicate-cache nil
1020 gnus-category-group-cache nil)
1021 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1022 (prin1 gnus-category-alist (current-buffer))))
1024 (defun gnus-category-edit-predicate (category)
1025 "Edit the predicate for CATEGORY."
1026 (interactive (list (gnus-category-name)))
1027 (let ((info (assq category gnus-category-alist)))
1029 (cadr info) (format "Editing the predicate for category %s" category)
1030 `(lambda (predicate)
1031 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1032 (gnus-category-write)
1033 (gnus-category-list)))))
1035 (defun gnus-category-edit-score (category)
1036 "Edit the score expression for CATEGORY."
1037 (interactive (list (gnus-category-name)))
1038 (let ((info (assq category gnus-category-alist)))
1041 (format "Editing the score expression for category %s" category)
1043 (setf (caddr (assq ',category gnus-category-alist)) groups)
1044 (gnus-category-write)
1045 (gnus-category-list)))))
1047 (defun gnus-category-edit-groups (category)
1048 "Edit the group list for CATEGORY."
1049 (interactive (list (gnus-category-name)))
1050 (let ((info (assq category gnus-category-alist)))
1052 (cadddr info) (format "Editing the group list for category %s" category)
1054 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1055 (gnus-category-write)
1056 (gnus-category-list)))))
1058 (defun gnus-category-kill (category)
1059 "Kill the current category."
1060 (interactive (list (gnus-category-name)))
1061 (let ((info (assq category gnus-category-alist))
1062 (buffer-read-only nil))
1064 (gnus-category-write)
1065 (setq gnus-category-alist (delq info gnus-category-alist))))
1067 (defun gnus-category-copy (category to)
1068 "Copy the current category."
1069 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1070 (let ((info (assq category gnus-category-alist)))
1071 (push (list to (gnus-copy-sequence (cadr info))
1072 (gnus-copy-sequence (caddr info)) nil)
1073 gnus-category-alist)
1074 (gnus-category-write)
1075 (gnus-category-list)))
1077 (defun gnus-category-add (category)
1078 "Create a new category."
1079 (interactive "SCategory name: ")
1080 (when (assq category gnus-category-alist)
1081 (error "Category %s already exists" category))
1082 (push (list category 'true nil nil)
1083 gnus-category-alist)
1084 (gnus-category-write)
1085 (gnus-category-list))
1087 (defun gnus-category-list ()
1088 "List all categories."
1090 (gnus-category-prepare))
1092 (defun gnus-category-exit ()
1093 "Return to the group buffer."
1095 (kill-buffer (current-buffer))
1096 (gnus-configure-windows 'group t))
1098 ;; To avoid having 8-bit characters in the source file.
1099 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1101 (defvar gnus-category-predicate-alist
1102 '((spam . gnus-agent-spam-p)
1103 (short . gnus-agent-short-p)
1104 (long . gnus-agent-long-p)
1105 (low . gnus-agent-low-scored-p)
1106 (high . gnus-agent-high-scored-p)
1107 (true . gnus-agent-true)
1108 (false . gnus-agent-false))
1109 "Mapping from short score predicate symbols to predicate functions.")
1111 (defun gnus-agent-spam-p ()
1112 "Say whether an article is spam or not."
1113 (unless gnus-agent-spam-hashtb
1114 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1115 (if (not (equal (mail-header-references gnus-headers) ""))
1117 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1119 (gnus-gethash string gnus-agent-spam-hashtb)
1120 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1122 (defun gnus-agent-short-p ()
1123 "Say whether an article is short or not."
1124 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1126 (defun gnus-agent-long-p ()
1127 "Say whether an article is long or not."
1128 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1130 (defun gnus-agent-low-scored-p ()
1131 "Say whether an article has a low score or not."
1132 (< gnus-score gnus-agent-low-score))
1134 (defun gnus-agent-high-scored-p ()
1135 "Say whether an article has a high score or not."
1136 (> gnus-score gnus-agent-low-score))
1138 (defun gnus-category-make-function (cat)
1139 "Make a function from category CAT."
1140 `(lambda () ,(gnus-category-make-function-1 cat)))
1142 (defun gnus-agent-true ()
1146 (defun gnus-agent-false ()
1150 (defun gnus-category-make-function-1 (cat)
1151 "Make a function from category CAT."
1153 ;; Functions are just returned as is.
1155 (gnus-functionp cat))
1156 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1158 ;; More complex category.
1161 ((memq (car cat) '(& and))
1163 ((memq (car cat) '(| or))
1165 ((memq (car cat) gnus-category-not)
1167 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1169 (error "Unknown category type: %s" cat))))
1171 (defun gnus-get-predicate (predicate)
1172 "Return the predicate for CATEGORY."
1173 (or (cdr (assoc predicate gnus-category-predicate-cache))
1174 (cdar (push (cons predicate
1175 (gnus-category-make-function predicate))
1176 gnus-category-predicate-cache))))
1178 (defun gnus-group-category (group)
1179 "Return the category GROUP belongs to."
1180 (unless gnus-category-group-cache
1181 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1182 (let ((cs gnus-category-alist)
1184 (while (setq cat (pop cs))
1185 (setq groups (cadddr cat))
1187 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1188 (or (gnus-gethash group gnus-category-group-cache)
1189 (assq 'default gnus-category-alist)))
1191 (defun gnus-agent-expire ()
1192 "Expire all old articles."
1194 (let ((methods gnus-agent-covered-methods)
1195 (alist (cdr gnus-newsrc-alist))
1196 gnus-command-method ofiles info method file group)
1197 (while (setq gnus-command-method (pop methods))
1198 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1199 (gnus-agent-directory)))))
1200 (while (setq info (pop alist))
1201 (when (and (gnus-agent-method-p
1202 (setq gnus-command-method
1203 (gnus-find-method-for-group
1204 (setq group (gnus-info-group info)))))
1208 (gnus-agent-directory)
1209 (gnus-agent-group-path group) "/.overview"))
1211 (setq ofiles (delete file ofiles))
1212 (gnus-agent-expire-group file group)))
1214 (gnus-agent-expire-group (pop ofiles)))))
1216 (defun gnus-agent-expire-directory (dir)
1217 "Expire all groups in DIR recursively."
1218 (when (file-directory-p dir)
1219 (let ((files (directory-files dir t))
1221 (while (setq file (pop files))
1223 ((member (file-name-nondirectory file) '("." ".."))
1226 ((file-directory-p file)
1228 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1229 ((string-match "\\.overview$" file)
1231 (push file ofiles))))
1234 (defun gnus-agent-expire-group (overview &optional group)
1235 "Expire articles in OVERVIEW."
1236 (gnus-message 5 "Expiring %s..." overview)
1237 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1238 (dir (file-name-directory overview))
1239 (info (when group (gnus-get-info group)))
1240 headers article file point unreads)
1241 (gnus-agent-load-alist nil dir)
1245 (gnus-list-of-unread-articles group)
1246 (gnus-uncompress-range
1247 (cdr (assq 'tick (gnus-info-marks info))))
1248 (gnus-uncompress-range
1249 (cdr (assq 'dormant (gnus-info-marks info)))))))
1250 (nnheader-temp-write overview
1251 (insert-file-contents overview)
1252 (goto-char (point-min))
1254 (setq point (point))
1256 (setq headers (inline (nnheader-parse-nov)))
1260 (setq headers nil)))
1262 (unless (memq (setq article (mail-header-number headers)) unreads)
1265 (inline (nnmail-date-to-time
1266 (mail-header-date headers)))))
1270 (setq gnus-agent-article-alist
1271 (delq (assq article gnus-agent-article-alist)
1272 gnus-agent-article-alist))
1273 (when (file-exists-p
1274 (setq file (concat dir (number-to-string article))))
1275 (delete-file file))))))
1276 (gnus-agent-save-alist nil nil nil dir))))
1279 (defun gnus-agent-batch ()
1281 (let ((init-file-user "")
1282 (gnus-always-read-dribble-file t))
1284 (gnus-group-send-drafts)
1285 (gnus-agent-fetch-session))
1287 (provide 'gnus-agent)
1289 ;;; gnus-agent.el ends here