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-agentize ()
265 "Allow Gnus to be an offline newsreader.
266 The normal usage of this command is to put the following as the
267 last form in your `.gnus.el' file:
271 This will modify the `gnus-before-startup-hook', `gnus-post-method',
272 and `message-send-mail-function' variables, and install the Gnus
273 agent minor mode in all Gnus buffers."
276 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
277 (unless gnus-agent-send-mail-function
278 (setq gnus-agent-send-mail-function message-send-mail-function
279 message-send-mail-function 'gnus-agent-send-mail))
280 (unless gnus-agent-covered-methods
281 (setq gnus-agent-covered-methods (list gnus-select-method))))
283 (defun gnus-agent-queue-setup ()
284 "Make sure the queue group exists."
285 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
286 (gnus-request-create-group "queue" '(nndraft ""))
287 (let ((gnus-level-default-subscribed 1))
288 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
289 (gnus-group-set-parameter
290 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
292 (defun gnus-agent-send-mail ()
294 (funcall gnus-agent-send-mail-function)
295 (goto-char (point-min))
297 (concat "^" (regexp-quote mail-header-separator) "\n"))
299 (gnus-request-accept-article "nndraft:queue")))
302 ;;; Group mode commands
305 (defun gnus-agent-fetch-groups (n)
306 "Put all new articles in the current groups into the agent."
308 (gnus-group-iterate n 'gnus-agent-fetch-group))
310 (defun gnus-agent-fetch-group (group)
311 "Put all new articles in GROUP into the agent."
312 (interactive (list (gnus-group-group-name)))
314 (error "No group on the current line"))
315 (let ((gnus-command-method (gnus-find-method-for-group group)))
316 (gnus-agent-with-fetch
317 (gnus-agent-fetch-group-1 group gnus-command-method)
318 (gnus-message 5 "Fetching %s...done" group))))
320 (defun gnus-agent-add-group (category arg)
321 "Add the current group to an agent category."
327 (mapcar (lambda (cat) (list (symbol-name (car cat))))
331 (let ((cat (assq category gnus-category-alist))
333 (gnus-group-iterate arg
335 (when (cadddr (setq c (gnus-group-category group)))
336 (setf (cadddr c) (delete group (cadddr c))))
337 (push group groups)))
338 (setf (cadddr cat) (nconc (cadddr cat) groups))
339 (gnus-category-write)))
342 ;;; Server mode commands
345 (defun gnus-agent-add-server (server)
346 "Enroll SERVER in the agent program."
347 (interactive (list (gnus-server-server-name)))
349 (error "No server on the current line"))
350 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
351 (when (member method gnus-agent-covered-methods)
352 (error "Server already in the agent program"))
353 (push method gnus-agent-covered-methods)
354 (gnus-agent-write-servers)
355 (message "Entered %s into the agent" server)))
357 (defun gnus-agent-remove-server (server)
358 "Remove SERVER from the agent program."
359 (interactive (list (gnus-server-server-name)))
361 (error "No server on the current line"))
362 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
363 (unless (member method gnus-agent-covered-methods)
364 (error "Server not in the agent program"))
365 (setq gnus-agent-covered-methods
366 (delete method gnus-agent-covered-methods))
367 (gnus-agent-write-servers)
368 (message "Removed %s from the agent" server)))
370 (defun gnus-agent-read-servers ()
371 "Read the alist of covered servers."
372 (setq gnus-agent-covered-methods
373 (gnus-agent-read-file
374 (nnheader-concat gnus-agent-directory "lib/servers"))))
376 (defun gnus-agent-write-servers ()
377 "Write the alist of covered servers."
378 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
379 (prin1 gnus-agent-covered-methods (current-buffer))))
385 (defun gnus-agent-mark-article (n &optional unmark)
386 "Mark the next N articles as downloadable.
387 If N is negative, mark backward instead. If UNMARK is non-nil, remove
388 the mark instead. The difference between N and the actual number of
389 articles marked is returned."
391 (let ((backward (< n 0))
396 (gnus-summary-set-agent-mark
397 (gnus-summary-article-number) unmark)
398 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
401 (gnus-message 7 "No more articles"))
402 (gnus-summary-recenter)
403 (gnus-summary-position-point)
406 (defun gnus-agent-unmark-article (n)
407 "Remove the downloadable mark from the next N articles.
408 If N is negative, unmark backward instead. The difference between N and
409 the actual number of articles unmarked is returned."
411 (gnus-agent-mark-article n t))
413 (defun gnus-agent-toggle-mark (n)
414 "Toggle the downloadable mark from the next N articles.
415 If N is negative, toggle backward instead. The difference between N and
416 the actual number of articles toggled is returned."
418 (gnus-agent-mark-article n 'toggle))
420 (defun gnus-summary-set-agent-mark (article &optional unmark)
421 "Mark ARTICLE as downloadable."
422 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
423 (memq article gnus-newsgroup-downloadable)
427 (setq gnus-newsgroup-downloadable
428 (delq article gnus-newsgroup-downloadable))
429 (push article gnus-newsgroup-undownloaded))
430 (setq gnus-newsgroup-undownloaded
431 (delq article gnus-newsgroup-undownloaded))
432 (push article gnus-newsgroup-downloadable))
433 (gnus-summary-update-mark
434 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
437 (defun gnus-agent-get-undownloaded-list ()
438 "Mark all unfetched articles as read."
439 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
440 (when (and (not gnus-plugged)
441 (gnus-agent-method-p gnus-command-method))
442 (gnus-agent-load-alist gnus-newsgroup-name)
443 (let ((articles gnus-newsgroup-unreads)
445 (while (setq article (pop articles))
446 (unless (or (cdr (assq article gnus-agent-article-alist))
447 (memq article gnus-newsgroup-downloadable))
448 (push article gnus-newsgroup-undownloaded)))))))
450 (defun gnus-agent-catchup ()
451 "Mark all undownloaded articles as read."
454 (while gnus-newsgroup-undownloaded
455 (gnus-summary-mark-article
456 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
457 (gnus-summary-position-point))
460 ;;; Internal functions
463 (defun gnus-agent-save-active (method)
464 (when (gnus-agent-method-p method)
465 (let* ((gnus-command-method method)
466 (file (gnus-agent-lib-file "active")))
467 (gnus-make-directory (file-name-directory file))
468 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
469 (write-region (point-min) (point-max) file nil 'silent))
470 (when (file-exists-p (gnus-agent-lib-file "groups"))
471 (delete-file (gnus-agent-lib-file "groups"))))))
473 (defun gnus-agent-save-groups (method)
474 (let* ((gnus-command-method method)
475 (file (gnus-agent-lib-file "groups")))
476 (gnus-make-directory (file-name-directory file))
477 (write-region (point-min) (point-max) file nil 'silent))
478 (when (file-exists-p (gnus-agent-lib-file "active"))
479 (delete-file (gnus-agent-lib-file "active"))))
481 (defun gnus-agent-group-path (group)
482 "Translate GROUP into a path."
483 (if nnmail-use-long-file-names
485 (nnheader-replace-chars-in-string
486 (nnheader-translate-file-chars group)
491 (defun gnus-agent-method-p (method)
492 "Say whether METHOD is covered by the agent."
493 (member method gnus-agent-covered-methods))
495 (defun gnus-agent-get-function (method)
496 (if (and (not gnus-plugged)
497 (gnus-agent-method-p method))
503 ;;; History functions
505 (defun gnus-agent-history-buffer ()
506 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
508 (defun gnus-agent-open-history ()
510 (push (cons (gnus-agent-method)
511 (set-buffer (get-buffer-create
512 (format " *Gnus agent %s history*"
513 (gnus-agent-method)))))
514 gnus-agent-history-buffers)
517 (let ((file (gnus-agent-lib-file "history")))
518 (when (file-exists-p file)
520 (set (make-local-variable 'gnus-agent-file-name) file))))
522 (defun gnus-agent-save-history ()
524 (set-buffer gnus-agent-current-history)
525 (gnus-make-directory (file-name-directory gnus-agent-file-name))
526 (write-region (1+ (point-min)) (point-max)
527 gnus-agent-file-name nil 'silent)))
529 (defun gnus-agent-close-history ()
530 (when (gnus-buffer-live-p gnus-agent-current-history)
531 (kill-buffer gnus-agent-current-history)
532 (setq gnus-agent-history-buffers
533 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
534 gnus-agent-history-buffers))))
536 (defun gnus-agent-enter-history (id group-arts date)
538 (set-buffer gnus-agent-current-history)
539 (goto-char (point-max))
540 (insert id "\t" (number-to-string date) "\t")
542 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
546 (defun gnus-agent-article-in-history-p (id)
548 (set-buffer (gnus-agent-history-buffer))
549 (goto-char (point-min))
550 (search-forward (concat "\n" id "\t") nil t)))
552 (defun gnus-agent-history-path (id)
554 (set-buffer (gnus-agent-history-buffer))
555 (goto-char (point-min))
556 (when (search-forward (concat "\n" id "\t") nil t)
557 (let ((method (gnus-agent-method)))
559 (while (not (numberp (setq group (read (current-buffer)))))
560 (push (concat method "/" group) paths))
561 (nreverse paths))))))
567 (defun gnus-agent-fetch-articles (group articles)
568 "Fetch ARTICLES from GROUP and put them into the agent."
570 ;; Prune off articles that we have already fetched.
572 (cdr (assq (car articles) gnus-agent-article-alist)))
574 (let ((arts articles))
576 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
577 (setcdr arts (cddr arts))
578 (setq arts (cdr arts)))))
581 (gnus-agent-directory)
582 (gnus-agent-group-path group) "/"))
583 (date (gnus-time-to-day (current-time)))
585 pos alists crosses id elem)
586 (gnus-make-directory dir)
587 (gnus-message 7 "Fetching articles for %s..." group)
588 ;; Fetch the articles from the backend.
589 (if (gnus-check-backend-function 'retrieve-articles group)
590 (setq pos (gnus-retrieve-articles articles group))
591 (nnheader-temp-write nil
592 (let ((buf (current-buffer))
594 (while (setq article (pop articles))
595 (when (gnus-request-article article group)
596 (goto-char (point-max))
597 (push (cons article (point)) pos)
598 (insert-buffer-substring nntp-server-buffer)))
599 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
600 (setq pos (nreverse pos)))))
601 ;; Then save these articles into the agent.
603 (set-buffer nntp-server-buffer)
605 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
606 (goto-char (point-min))
607 (when (search-forward "\n\n" nil t)
608 (when (search-backward "\nXrefs: " nil t)
609 ;; Handle crossposting.
610 (skip-chars-forward "^ ")
611 (skip-chars-forward " ")
613 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
614 (push (cons (buffer-substring (match-beginning 1)
616 (buffer-substring (match-beginning 2)
619 (goto-char (match-end 0)))
620 (gnus-agent-crosspost crosses (caar pos))))
621 (goto-char (point-min))
622 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
623 (setq id "No-Message-ID-in-article")
624 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
625 (let ((coding-system-for-write
626 gnus-agent-article-file-coding-system))
627 (write-region (point-min) (point-max)
628 (concat dir (number-to-string (caar pos)))
630 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
632 (gnus-agent-enter-history
633 id (or crosses (list (cons group (caar pos)))) date)
636 (gnus-agent-save-alist group)))))
638 (defun gnus-agent-crosspost (crosses article)
639 (let (gnus-agent-article-alist group alist beg end)
641 (set-buffer gnus-agent-overview-buffer)
642 (when (nnheader-find-nov-line article)
645 (setq end (progn (forward-line 1) (point)))))
647 (setq group (caar crosses))
648 (unless (setq alist (assoc group gnus-agent-group-alist))
649 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
650 gnus-agent-group-alist))
651 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
653 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
655 (when (= (point-max) (point-min))
656 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
658 (insert-file-contents
659 (gnus-agent-article-name ".overview" group))))
660 (nnheader-find-nov-line (string-to-number (cdar crosses)))
661 (insert (string-to-number (cdar crosses)))
662 (insert-buffer-substring gnus-agent-overview-buffer beg end))
665 (defun gnus-agent-flush-cache ()
667 (while gnus-agent-buffer-alist
668 (set-buffer (cdar gnus-agent-buffer-alist))
669 (write-region (point-min) (point-max)
670 (gnus-agent-article-name ".overview"
671 (caar gnus-agent-buffer-alist))
673 (pop gnus-agent-buffer-alist))
674 (while gnus-agent-group-alist
675 (nnheader-temp-write (caar gnus-agent-group-alist)
676 (princ (cdar gnus-agent-group-alist))
678 (pop gnus-agent-group-alist))))
680 (defun gnus-agent-fetch-headers (group articles &optional force)
681 (gnus-agent-load-alist group)
682 ;; Find out what headers we need to retrieve.
685 (assq (car articles) gnus-agent-article-alist))
687 (let ((arts articles))
689 (if (assq (cadr arts) gnus-agent-article-alist)
690 (setcdr arts (cddr arts))
691 (setq arts (cdr arts)))))
694 (gnus-message 7 "Fetching headers for %s..." group)
696 (set-buffer nntp-server-buffer)
697 (unless (eq 'nov (gnus-retrieve-headers articles group))
698 (nnvirtual-convert-headers))
699 ;; Save these headers for later processing.
700 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
703 (setq file (gnus-agent-article-name ".overview" group)))
704 (gnus-agent-braid-nov group articles file))
705 (gnus-make-directory (nnheader-translate-file-chars
706 (file-name-directory file)))
707 (write-region (point-min) (point-max) file nil 'silent)
708 (gnus-agent-save-alist group articles nil))
711 (defsubst gnus-agent-copy-nov-line (article)
713 (set-buffer gnus-agent-overview-buffer)
715 (if (eq article (read (current-buffer)))
716 (setq e (progn (forward-line 1) (point)))
718 (set-buffer nntp-server-buffer)
719 (insert-buffer-substring gnus-agent-overview-buffer b e)))
721 (defun gnus-agent-braid-nov (group articles file)
723 (set-buffer gnus-agent-overview-buffer)
724 (goto-char (point-min))
725 (set-buffer nntp-server-buffer)
727 (insert-file-contents file)
728 (goto-char (point-min))
729 (if (or (= (point-min) (point-max))
732 (< (read (current-buffer)) (car articles))))
733 ;; We have only headers that are after the older headers,
734 ;; so we just append them.
736 (goto-char (point-max))
737 (insert-buffer-substring gnus-agent-overview-buffer))
738 ;; We do it the hard way.
739 (nnheader-find-nov-line (car articles))
740 (gnus-agent-copy-nov-line (car articles))
744 (while (and (not (eobp))
745 (< (read (current-buffer)) (car articles)))
749 (gnus-agent-copy-nov-line (car articles))
750 (setq articles (cdr articles))))
753 (set-buffer gnus-agent-overview-buffer)
756 (set-buffer nntp-server-buffer)
757 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
759 (defun gnus-agent-load-alist (group &optional dir)
760 "Load the article-state alist for GROUP."
761 (setq gnus-agent-article-alist
762 (gnus-agent-read-file
764 (concat dir ".agentview")
765 (gnus-agent-article-name ".agentview" group)))))
767 (defun gnus-agent-save-alist (group &optional articles state dir)
768 "Load the article-state alist for GROUP."
769 (nnheader-temp-write (if dir
770 (concat dir ".agentview")
771 (gnus-agent-article-name ".agentview" group))
772 (princ (setq gnus-agent-article-alist
773 (nconc gnus-agent-article-alist
774 (mapcar (lambda (article) (cons article state))
779 (defun gnus-agent-article-name (article group)
780 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
781 (if (stringp article) article (string-to-number article))))
784 (defun gnus-agent-batch-fetch ()
785 "Start Gnus and fetch session."
788 (gnus-agent-fetch-session)
791 (defun gnus-agent-fetch-session ()
792 "Fetch all articles and headers that are eligible for fetching."
794 (unless gnus-agent-covered-methods
795 (error "No servers are covered by the Gnus agent"))
797 (error "Can't fetch articles while Gnus is unplugged"))
798 (let ((methods gnus-agent-covered-methods)
799 groups group gnus-command-method)
802 (setq gnus-command-method (car methods)
803 groups (gnus-groups-from-server (pop methods)))
804 (gnus-agent-with-fetch
805 (while (setq group (pop groups))
806 (when (<= (gnus-group-level group) gnus-agent-handle-level)
807 (gnus-agent-fetch-group-1 group gnus-command-method)))))
808 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
810 (defun gnus-agent-fetch-group-1 (group method)
812 (let ((gnus-command-method method)
813 gnus-newsgroup-dependencies gnus-newsgroup-headers
814 gnus-newsgroup-scored gnus-headers gnus-score
815 gnus-use-cache articles score arts
816 category predicate info marks score-param)
818 (when (and (setq articles (gnus-list-of-unread-articles group))
819 (gnus-agent-fetch-headers group articles))
820 ;; Parse them and see which articles we want to fetch.
821 (setq gnus-newsgroup-dependencies
822 (make-vector (length articles) 0))
823 (setq gnus-newsgroup-headers
824 (gnus-get-newsgroup-headers-xover articles nil nil group))
825 (setq category (gnus-group-category group))
828 (or (gnus-group-get-parameter group 'agent-predicate)
831 (or (gnus-group-get-parameter group 'agent-score)
834 (gnus-score-headers (list (list score-param))))
836 (while (setq gnus-headers (pop gnus-newsgroup-headers))
838 (or (cdr (assq (mail-header-number gnus-headers)
839 gnus-newsgroup-scored))
840 gnus-summary-default-score))
841 (when (funcall predicate)
842 (push (mail-header-number gnus-headers)
844 ;; Fetch the articles.
846 (gnus-agent-fetch-articles group arts)))
847 ;; Perhaps we have some additional articles to fetch.
848 (setq arts (assq 'download (gnus-info-marks
849 (setq info (gnus-get-info group)))))
851 (gnus-agent-fetch-articles
852 group (gnus-uncompress-range (cdr arts)))
853 (setq marks (delq arts (gnus-info-marks info)))
854 (gnus-info-set-marks info marks))))
857 ;;; Agent Category Mode
860 (defvar gnus-category-mode-hook nil
861 "Hook run in `gnus-category-mode' buffers.")
863 (defvar gnus-category-line-format " %(%20c%): %g\n"
864 "Format of category lines.")
866 (defvar gnus-category-mode-line-format "Gnus: %%b"
867 "The format specification for the category mode line.")
869 (defvar gnus-agent-short-article 100
870 "Articles that have fewer lines than this are short.")
872 (defvar gnus-agent-long-article 200
873 "Articles that have more lines than this are long.")
875 (defvar gnus-agent-low-score 0
876 "Articles that have a score lower than this have a low score.")
878 (defvar gnus-agent-high-score 0
879 "Articles that have a score higher than this have a high score.")
882 ;;; Internal variables.
884 (defvar gnus-category-buffer "*Agent Category*")
886 (defvar gnus-category-line-format-alist
890 (defvar gnus-category-mode-line-format-alist
891 `((?u user-defined ?s)))
893 (defvar gnus-category-line-format-spec nil)
894 (defvar gnus-category-mode-line-format-spec nil)
896 (defvar gnus-category-mode-map nil)
897 (put 'gnus-category-mode 'mode-class 'special)
899 (unless gnus-category-mode-map
900 (setq gnus-category-mode-map (make-sparse-keymap))
901 (suppress-keymap gnus-category-mode-map)
903 (gnus-define-keys gnus-category-mode-map
904 "q" gnus-category-exit
905 "k" gnus-category-kill
906 "c" gnus-category-copy
907 "a" gnus-category-add
908 "p" gnus-category-edit-predicate
909 "g" gnus-category-edit-groups
910 "s" gnus-category-edit-score
911 "l" gnus-category-list
913 "\C-c\C-i" gnus-info-find-node
914 "\C-c\C-b" gnus-bug))
916 (defvar gnus-category-menu-hook nil
917 "*Hook run after the creation of the menu.")
919 (defun gnus-category-make-menu-bar ()
920 (gnus-turn-off-edit-menu 'category)
921 (unless (boundp 'gnus-category-menu)
923 gnus-category-menu gnus-category-mode-map ""
925 ["Add" gnus-category-add t]
926 ["Kill" gnus-category-kill t]
927 ["Copy" gnus-category-copy t]
928 ["Edit predicate" gnus-category-edit-predicate t]
929 ["Edit score" gnus-category-edit-score t]
930 ["Edit groups" gnus-category-edit-groups t]
931 ["Exit" gnus-category-exit t]))
933 (gnus-run-hooks 'gnus-category-menu-hook)))
935 (defun gnus-category-mode ()
936 "Major mode for listing and editing agent categories.
938 All normal editing commands are switched off.
939 \\<gnus-category-mode-map>
940 For more in-depth information on this mode, read the manual
941 (`\\[gnus-info-find-node]').
943 The following commands are available:
945 \\{gnus-category-mode-map}"
947 (when (gnus-visual-p 'category-menu 'menu)
948 (gnus-category-make-menu-bar))
949 (kill-all-local-variables)
950 (gnus-simplify-mode-line)
951 (setq major-mode 'gnus-category-mode)
952 (setq mode-name "Category")
953 (gnus-set-default-directory)
954 (setq mode-line-process nil)
955 (use-local-map gnus-category-mode-map)
956 (buffer-disable-undo (current-buffer))
957 (setq truncate-lines t)
958 (setq buffer-read-only t)
959 (gnus-run-hooks 'gnus-category-mode-hook))
961 (defalias 'gnus-category-position-point 'gnus-goto-colon)
963 (defun gnus-category-insert-line (category)
964 (let* ((name (car category))
965 (groups (length (cadddr category))))
967 (gnus-add-text-properties
971 (eval gnus-category-line-format-spec))
972 (list 'gnus-category name))))
974 (defun gnus-enter-category-buffer ()
975 "Go to the Category buffer."
977 (gnus-category-setup-buffer)
978 (gnus-configure-windows 'category)
979 (gnus-category-prepare))
981 (defun gnus-category-setup-buffer ()
982 (unless (get-buffer gnus-category-buffer)
984 (set-buffer (get-buffer-create gnus-category-buffer))
985 (gnus-add-current-to-buffer-list)
986 (gnus-category-mode))))
988 (defun gnus-category-prepare ()
989 (gnus-set-format 'category-mode)
990 (gnus-set-format 'category t)
991 (let ((alist gnus-category-alist)
992 (buffer-read-only nil))
995 (gnus-category-insert-line (pop alist)))
996 (goto-char (point-min))
997 (gnus-category-position-point)))
999 (defun gnus-category-name ()
1000 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1001 (error "No category on the current line")))
1003 (defun gnus-category-read ()
1004 "Read the category alist."
1005 (setq gnus-category-alist
1006 (or (gnus-agent-read-file
1007 (nnheader-concat gnus-agent-directory "lib/categories"))
1008 (list (list 'default 'short nil nil)))))
1010 (defun gnus-category-write ()
1011 "Write the category alist."
1012 (setq gnus-category-predicate-cache nil
1013 gnus-category-group-cache nil)
1014 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1015 (prin1 gnus-category-alist (current-buffer))))
1017 (defun gnus-category-edit-predicate (category)
1018 "Edit the predicate for CATEGORY."
1019 (interactive (list (gnus-category-name)))
1020 (let ((info (assq category gnus-category-alist)))
1022 (cadr info) (format "Editing the predicate for category %s" category)
1023 `(lambda (predicate)
1024 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1025 (gnus-category-write)
1026 (gnus-category-list)))))
1028 (defun gnus-category-edit-score (category)
1029 "Edit the score expression for CATEGORY."
1030 (interactive (list (gnus-category-name)))
1031 (let ((info (assq category gnus-category-alist)))
1034 (format "Editing the score expression for category %s" category)
1036 (setf (caddr (assq ',category gnus-category-alist)) groups)
1037 (gnus-category-write)
1038 (gnus-category-list)))))
1040 (defun gnus-category-edit-groups (category)
1041 "Edit the group list for CATEGORY."
1042 (interactive (list (gnus-category-name)))
1043 (let ((info (assq category gnus-category-alist)))
1045 (cadddr info) (format "Editing the group list for category %s" category)
1047 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1048 (gnus-category-write)
1049 (gnus-category-list)))))
1051 (defun gnus-category-kill (category)
1052 "Kill the current category."
1053 (interactive (list (gnus-category-name)))
1054 (let ((info (assq category gnus-category-alist))
1055 (buffer-read-only nil))
1057 (gnus-category-write)
1058 (setq gnus-category-alist (delq info gnus-category-alist))))
1060 (defun gnus-category-copy (category to)
1061 "Copy the current category."
1062 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1063 (let ((info (assq category gnus-category-alist)))
1064 (push (list to (gnus-copy-sequence (cadr info))
1065 (gnus-copy-sequence (caddr info)) nil)
1066 gnus-category-alist)
1067 (gnus-category-write)
1068 (gnus-category-list)))
1070 (defun gnus-category-add (category)
1071 "Create a new category."
1072 (interactive "SCategory name: ")
1073 (when (assq category gnus-category-alist)
1074 (error "Category %s already exists" category))
1075 (push (list category 'true nil nil)
1076 gnus-category-alist)
1077 (gnus-category-write)
1078 (gnus-category-list))
1080 (defun gnus-category-list ()
1081 "List all categories."
1083 (gnus-category-prepare))
1085 (defun gnus-category-exit ()
1086 "Return to the group buffer."
1088 (kill-buffer (current-buffer))
1089 (gnus-configure-windows 'group t))
1091 ;; To avoid having 8-bit characters in the source file.
1092 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1094 (defvar gnus-category-predicate-alist
1095 '((spam . gnus-agent-spam-p)
1096 (short . gnus-agent-short-p)
1097 (long . gnus-agent-long-p)
1098 (low . gnus-agent-low-scored-p)
1099 (high . gnus-agent-high-scored-p)
1100 (true . gnus-agent-true)
1101 (false . gnus-agent-false))
1102 "Mapping from short score predicate symbols to predicate functions.")
1104 (defun gnus-agent-spam-p ()
1105 "Say whether an article is spam or not."
1106 (unless gnus-agent-spam-hashtb
1107 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1108 (if (not (equal (mail-header-references gnus-headers) ""))
1110 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1112 (gnus-gethash string gnus-agent-spam-hashtb)
1113 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1115 (defun gnus-agent-short-p ()
1116 "Say whether an article is short or not."
1117 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1119 (defun gnus-agent-long-p ()
1120 "Say whether an article is long or not."
1121 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1123 (defun gnus-agent-low-scored-p ()
1124 "Say whether an article has a low score or not."
1125 (< gnus-score gnus-agent-low-score))
1127 (defun gnus-agent-high-scored-p ()
1128 "Say whether an article has a high score or not."
1129 (> gnus-score gnus-agent-low-score))
1131 (defun gnus-category-make-function (cat)
1132 "Make a function from category CAT."
1133 `(lambda () ,(gnus-category-make-function-1 cat)))
1135 (defun gnus-agent-true ()
1139 (defun gnus-agent-false ()
1143 (defun gnus-category-make-function-1 (cat)
1144 "Make a function from category CAT."
1146 ;; Functions are just returned as is.
1148 (gnus-functionp cat))
1149 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1151 ;; More complex category.
1154 ((memq (car cat) '(& and))
1156 ((memq (car cat) '(| or))
1158 ((memq (car cat) gnus-category-not)
1160 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1162 (error "Unknown category type: %s" cat))))
1164 (defun gnus-get-predicate (predicate)
1165 "Return the predicate for CATEGORY."
1166 (or (cdr (assoc predicate gnus-category-predicate-cache))
1167 (cdar (push (cons predicate
1168 (gnus-category-make-function predicate))
1169 gnus-category-predicate-cache))))
1171 (defun gnus-group-category (group)
1172 "Return the category GROUP belongs to."
1173 (unless gnus-category-group-cache
1174 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1175 (let ((cs gnus-category-alist)
1177 (while (setq cat (pop cs))
1178 (setq groups (cadddr cat))
1180 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1181 (or (gnus-gethash group gnus-category-group-cache)
1182 (assq 'default gnus-category-alist)))
1184 (defun gnus-agent-expire ()
1185 "Expire all old articles."
1187 (let ((methods gnus-agent-covered-methods)
1188 (alist (cdr gnus-newsrc-alist))
1189 gnus-command-method ofiles info method file group)
1190 (while (setq gnus-command-method (pop methods))
1191 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1192 (gnus-agent-directory)))))
1193 (while (setq info (pop alist))
1194 (when (and (gnus-agent-method-p
1195 (setq gnus-command-method
1196 (gnus-find-method-for-group
1197 (setq group (gnus-info-group info)))))
1201 (gnus-agent-directory)
1202 (gnus-agent-group-path group) "/.overview"))
1204 (setq ofiles (delete file ofiles))
1205 (gnus-agent-expire-group file group)))
1207 (gnus-agent-expire-group (pop ofiles)))))
1209 (defun gnus-agent-expire-directory (dir)
1210 "Expire all groups in DIR recursively."
1211 (when (file-directory-p dir)
1212 (let ((files (directory-files dir t))
1214 (while (setq file (pop files))
1216 ((member (file-name-nondirectory file) '("." ".."))
1219 ((file-directory-p file)
1221 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1222 ((string-match "\\.overview$" file)
1224 (push file ofiles))))
1227 (defun gnus-agent-expire-group (overview &optional group)
1228 "Expire articles in OVERVIEW."
1229 (gnus-message 5 "Expiring %s..." overview)
1230 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1231 (dir (file-name-directory overview))
1232 (info (when group (gnus-get-info group)))
1233 headers article file point unreads)
1234 (gnus-agent-load-alist nil dir)
1238 (gnus-list-of-unread-articles group)
1239 (gnus-uncompress-range
1240 (cdr (assq 'tick (gnus-info-marks info))))
1241 (gnus-uncompress-range
1242 (cdr (assq 'dormant (gnus-info-marks info)))))))
1243 (nnheader-temp-write overview
1244 (insert-file-contents overview)
1245 (goto-char (point-min))
1247 (setq point (point))
1249 (setq headers (inline (nnheader-parse-nov)))
1253 (setq headers nil)))
1255 (unless (memq (setq article (mail-header-number headers)) unreads)
1258 (inline (nnmail-date-to-time
1259 (mail-header-date headers)))))
1263 (setq gnus-agent-article-alist
1264 (delq (assq article gnus-agent-article-alist)
1265 gnus-agent-article-alist))
1266 (when (file-exists-p
1267 (setq file (concat dir (number-to-string article))))
1268 (delete-file file))))))
1269 (gnus-agent-save-alist nil nil nil dir))))
1272 (defun gnus-agent-batch ()
1274 (let ((init-file-user "")
1275 (gnus-always-read-dribble-file t))
1277 (gnus-group-send-drafts)
1278 (gnus-agent-fetch-session))
1280 (provide 'gnus-agent)
1282 ;;; gnus-agent.el ends here