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))
700 ;; To gnus-agent-expire work fine with no Xref field in .overview
701 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
702 (goto-char (point-min))
704 (goto-char (point-at-eol))
707 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
708 ;; To gnus-agent-expire work fine with no Xref field in .overview
710 ;; Save these headers for later processing.
711 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
714 (setq file (gnus-agent-article-name ".overview" group)))
715 (gnus-agent-braid-nov group articles file))
716 (gnus-make-directory (nnheader-translate-file-chars
717 (file-name-directory file)))
718 (write-region (point-min) (point-max) file nil 'silent)
719 (gnus-agent-save-alist group articles nil))
722 (defsubst gnus-agent-copy-nov-line (article)
724 (set-buffer gnus-agent-overview-buffer)
726 (if (eq article (read (current-buffer)))
727 (setq e (progn (forward-line 1) (point)))
729 (set-buffer nntp-server-buffer)
730 (insert-buffer-substring gnus-agent-overview-buffer b e)))
732 (defun gnus-agent-braid-nov (group articles file)
734 (set-buffer gnus-agent-overview-buffer)
735 (goto-char (point-min))
736 (set-buffer nntp-server-buffer)
738 (insert-file-contents file)
739 (goto-char (point-min))
740 (if (or (= (point-min) (point-max))
743 (< (read (current-buffer)) (car articles))))
744 ;; We have only headers that are after the older headers,
745 ;; so we just append them.
747 (goto-char (point-max))
748 (insert-buffer-substring gnus-agent-overview-buffer))
749 ;; We do it the hard way.
750 (nnheader-find-nov-line (car articles))
751 (gnus-agent-copy-nov-line (car articles))
755 (while (and (not (eobp))
756 (< (read (current-buffer)) (car articles)))
760 (gnus-agent-copy-nov-line (car articles))
761 (setq articles (cdr articles))))
764 (set-buffer gnus-agent-overview-buffer)
767 (set-buffer nntp-server-buffer)
768 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
770 (defun gnus-agent-load-alist (group &optional dir)
771 "Load the article-state alist for GROUP."
772 (setq gnus-agent-article-alist
773 (gnus-agent-read-file
775 (concat dir ".agentview")
776 (gnus-agent-article-name ".agentview" group)))))
778 (defun gnus-agent-save-alist (group &optional articles state dir)
779 "Load the article-state alist for GROUP."
780 (nnheader-temp-write (if dir
781 (concat dir ".agentview")
782 (gnus-agent-article-name ".agentview" group))
783 (princ (setq gnus-agent-article-alist
784 (nconc gnus-agent-article-alist
785 (mapcar (lambda (article) (cons article state))
790 (defun gnus-agent-article-name (article group)
791 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
792 (if (stringp article) article (string-to-number article))))
795 (defun gnus-agent-batch-fetch ()
796 "Start Gnus and fetch session."
799 (gnus-agent-fetch-session)
802 (defun gnus-agent-fetch-session ()
803 "Fetch all articles and headers that are eligible for fetching."
805 (unless gnus-agent-covered-methods
806 (error "No servers are covered by the Gnus agent"))
808 (error "Can't fetch articles while Gnus is unplugged"))
809 (let ((methods gnus-agent-covered-methods)
810 groups group gnus-command-method)
813 (setq gnus-command-method (car methods)
814 groups (gnus-groups-from-server (pop methods)))
815 (gnus-agent-with-fetch
816 (while (setq group (pop groups))
817 (when (<= (gnus-group-level group) gnus-agent-handle-level)
818 (gnus-agent-fetch-group-1 group gnus-command-method)))))
819 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
821 (defun gnus-agent-fetch-group-1 (group method)
823 (let ((gnus-command-method method)
824 gnus-newsgroup-dependencies gnus-newsgroup-headers
825 gnus-newsgroup-scored gnus-headers gnus-score
826 gnus-use-cache articles score arts
827 category predicate info marks score-param)
829 (when (and (setq articles (gnus-list-of-unread-articles group))
830 (gnus-agent-fetch-headers group articles))
831 ;; Parse them and see which articles we want to fetch.
832 (setq gnus-newsgroup-dependencies
833 (make-vector (length articles) 0))
834 (setq gnus-newsgroup-headers
835 (gnus-get-newsgroup-headers-xover articles nil nil group))
836 (setq category (gnus-group-category group))
839 (or (gnus-group-get-parameter group 'agent-predicate)
842 (or (gnus-group-get-parameter group 'agent-score)
845 (gnus-score-headers (list (list score-param))))
847 (while (setq gnus-headers (pop gnus-newsgroup-headers))
849 (or (cdr (assq (mail-header-number gnus-headers)
850 gnus-newsgroup-scored))
851 gnus-summary-default-score))
852 (when (funcall predicate)
853 (push (mail-header-number gnus-headers)
855 ;; Fetch the articles.
857 (gnus-agent-fetch-articles group arts)))
858 ;; Perhaps we have some additional articles to fetch.
859 (setq arts (assq 'download (gnus-info-marks
860 (setq info (gnus-get-info group)))))
862 (gnus-agent-fetch-articles
863 group (gnus-uncompress-range (cdr arts)))
864 (setq marks (delq arts (gnus-info-marks info)))
865 (gnus-info-set-marks info marks))))
868 ;;; Agent Category Mode
871 (defvar gnus-category-mode-hook nil
872 "Hook run in `gnus-category-mode' buffers.")
874 (defvar gnus-category-line-format " %(%20c%): %g\n"
875 "Format of category lines.")
877 (defvar gnus-category-mode-line-format "Gnus: %%b"
878 "The format specification for the category mode line.")
880 (defvar gnus-agent-short-article 100
881 "Articles that have fewer lines than this are short.")
883 (defvar gnus-agent-long-article 200
884 "Articles that have more lines than this are long.")
886 (defvar gnus-agent-low-score 0
887 "Articles that have a score lower than this have a low score.")
889 (defvar gnus-agent-high-score 0
890 "Articles that have a score higher than this have a high score.")
893 ;;; Internal variables.
895 (defvar gnus-category-buffer "*Agent Category*")
897 (defvar gnus-category-line-format-alist
901 (defvar gnus-category-mode-line-format-alist
902 `((?u user-defined ?s)))
904 (defvar gnus-category-line-format-spec nil)
905 (defvar gnus-category-mode-line-format-spec nil)
907 (defvar gnus-category-mode-map nil)
908 (put 'gnus-category-mode 'mode-class 'special)
910 (unless gnus-category-mode-map
911 (setq gnus-category-mode-map (make-sparse-keymap))
912 (suppress-keymap gnus-category-mode-map)
914 (gnus-define-keys gnus-category-mode-map
915 "q" gnus-category-exit
916 "k" gnus-category-kill
917 "c" gnus-category-copy
918 "a" gnus-category-add
919 "p" gnus-category-edit-predicate
920 "g" gnus-category-edit-groups
921 "s" gnus-category-edit-score
922 "l" gnus-category-list
924 "\C-c\C-i" gnus-info-find-node
925 "\C-c\C-b" gnus-bug))
927 (defvar gnus-category-menu-hook nil
928 "*Hook run after the creation of the menu.")
930 (defun gnus-category-make-menu-bar ()
931 (gnus-turn-off-edit-menu 'category)
932 (unless (boundp 'gnus-category-menu)
934 gnus-category-menu gnus-category-mode-map ""
936 ["Add" gnus-category-add t]
937 ["Kill" gnus-category-kill t]
938 ["Copy" gnus-category-copy t]
939 ["Edit predicate" gnus-category-edit-predicate t]
940 ["Edit score" gnus-category-edit-score t]
941 ["Edit groups" gnus-category-edit-groups t]
942 ["Exit" gnus-category-exit t]))
944 (gnus-run-hooks 'gnus-category-menu-hook)))
946 (defun gnus-category-mode ()
947 "Major mode for listing and editing agent categories.
949 All normal editing commands are switched off.
950 \\<gnus-category-mode-map>
951 For more in-depth information on this mode, read the manual
952 (`\\[gnus-info-find-node]').
954 The following commands are available:
956 \\{gnus-category-mode-map}"
958 (when (gnus-visual-p 'category-menu 'menu)
959 (gnus-category-make-menu-bar))
960 (kill-all-local-variables)
961 (gnus-simplify-mode-line)
962 (setq major-mode 'gnus-category-mode)
963 (setq mode-name "Category")
964 (gnus-set-default-directory)
965 (setq mode-line-process nil)
966 (use-local-map gnus-category-mode-map)
967 (buffer-disable-undo (current-buffer))
968 (setq truncate-lines t)
969 (setq buffer-read-only t)
970 (gnus-run-hooks 'gnus-category-mode-hook))
972 (defalias 'gnus-category-position-point 'gnus-goto-colon)
974 (defun gnus-category-insert-line (category)
975 (let* ((name (car category))
976 (groups (length (cadddr category))))
978 (gnus-add-text-properties
982 (eval gnus-category-line-format-spec))
983 (list 'gnus-category name))))
985 (defun gnus-enter-category-buffer ()
986 "Go to the Category buffer."
988 (gnus-category-setup-buffer)
989 (gnus-configure-windows 'category)
990 (gnus-category-prepare))
992 (defun gnus-category-setup-buffer ()
993 (unless (get-buffer gnus-category-buffer)
995 (set-buffer (get-buffer-create gnus-category-buffer))
996 (gnus-add-current-to-buffer-list)
997 (gnus-category-mode))))
999 (defun gnus-category-prepare ()
1000 (gnus-set-format 'category-mode)
1001 (gnus-set-format 'category t)
1002 (let ((alist gnus-category-alist)
1003 (buffer-read-only nil))
1006 (gnus-category-insert-line (pop alist)))
1007 (goto-char (point-min))
1008 (gnus-category-position-point)))
1010 (defun gnus-category-name ()
1011 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1012 (error "No category on the current line")))
1014 (defun gnus-category-read ()
1015 "Read the category alist."
1016 (setq gnus-category-alist
1017 (or (gnus-agent-read-file
1018 (nnheader-concat gnus-agent-directory "lib/categories"))
1019 (list (list 'default 'short nil nil)))))
1021 (defun gnus-category-write ()
1022 "Write the category alist."
1023 (setq gnus-category-predicate-cache nil
1024 gnus-category-group-cache nil)
1025 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1026 (prin1 gnus-category-alist (current-buffer))))
1028 (defun gnus-category-edit-predicate (category)
1029 "Edit the predicate for CATEGORY."
1030 (interactive (list (gnus-category-name)))
1031 (let ((info (assq category gnus-category-alist)))
1033 (cadr info) (format "Editing the predicate for category %s" category)
1034 `(lambda (predicate)
1035 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1036 (gnus-category-write)
1037 (gnus-category-list)))))
1039 (defun gnus-category-edit-score (category)
1040 "Edit the score expression for CATEGORY."
1041 (interactive (list (gnus-category-name)))
1042 (let ((info (assq category gnus-category-alist)))
1045 (format "Editing the score expression for category %s" category)
1047 (setf (caddr (assq ',category gnus-category-alist)) groups)
1048 (gnus-category-write)
1049 (gnus-category-list)))))
1051 (defun gnus-category-edit-groups (category)
1052 "Edit the group list for CATEGORY."
1053 (interactive (list (gnus-category-name)))
1054 (let ((info (assq category gnus-category-alist)))
1056 (cadddr info) (format "Editing the group list for category %s" category)
1058 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1059 (gnus-category-write)
1060 (gnus-category-list)))))
1062 (defun gnus-category-kill (category)
1063 "Kill the current category."
1064 (interactive (list (gnus-category-name)))
1065 (let ((info (assq category gnus-category-alist))
1066 (buffer-read-only nil))
1068 (gnus-category-write)
1069 (setq gnus-category-alist (delq info gnus-category-alist))))
1071 (defun gnus-category-copy (category to)
1072 "Copy the current category."
1073 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1074 (let ((info (assq category gnus-category-alist)))
1075 (push (list to (gnus-copy-sequence (cadr info))
1076 (gnus-copy-sequence (caddr info)) nil)
1077 gnus-category-alist)
1078 (gnus-category-write)
1079 (gnus-category-list)))
1081 (defun gnus-category-add (category)
1082 "Create a new category."
1083 (interactive "SCategory name: ")
1084 (when (assq category gnus-category-alist)
1085 (error "Category %s already exists" category))
1086 (push (list category 'true nil nil)
1087 gnus-category-alist)
1088 (gnus-category-write)
1089 (gnus-category-list))
1091 (defun gnus-category-list ()
1092 "List all categories."
1094 (gnus-category-prepare))
1096 (defun gnus-category-exit ()
1097 "Return to the group buffer."
1099 (kill-buffer (current-buffer))
1100 (gnus-configure-windows 'group t))
1102 ;; To avoid having 8-bit characters in the source file.
1103 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1105 (defvar gnus-category-predicate-alist
1106 '((spam . gnus-agent-spam-p)
1107 (short . gnus-agent-short-p)
1108 (long . gnus-agent-long-p)
1109 (low . gnus-agent-low-scored-p)
1110 (high . gnus-agent-high-scored-p)
1111 (true . gnus-agent-true)
1112 (false . gnus-agent-false))
1113 "Mapping from short score predicate symbols to predicate functions.")
1115 (defun gnus-agent-spam-p ()
1116 "Say whether an article is spam or not."
1117 (unless gnus-agent-spam-hashtb
1118 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1119 (if (not (equal (mail-header-references gnus-headers) ""))
1121 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1123 (gnus-gethash string gnus-agent-spam-hashtb)
1124 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1126 (defun gnus-agent-short-p ()
1127 "Say whether an article is short or not."
1128 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1130 (defun gnus-agent-long-p ()
1131 "Say whether an article is long or not."
1132 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1134 (defun gnus-agent-low-scored-p ()
1135 "Say whether an article has a low score or not."
1136 (< gnus-score gnus-agent-low-score))
1138 (defun gnus-agent-high-scored-p ()
1139 "Say whether an article has a high score or not."
1140 (> gnus-score gnus-agent-low-score))
1142 (defun gnus-category-make-function (cat)
1143 "Make a function from category CAT."
1144 `(lambda () ,(gnus-category-make-function-1 cat)))
1146 (defun gnus-agent-true ()
1150 (defun gnus-agent-false ()
1154 (defun gnus-category-make-function-1 (cat)
1155 "Make a function from category CAT."
1157 ;; Functions are just returned as is.
1159 (gnus-functionp cat))
1160 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1162 ;; More complex category.
1165 ((memq (car cat) '(& and))
1167 ((memq (car cat) '(| or))
1169 ((memq (car cat) gnus-category-not)
1171 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1173 (error "Unknown category type: %s" cat))))
1175 (defun gnus-get-predicate (predicate)
1176 "Return the predicate for CATEGORY."
1177 (or (cdr (assoc predicate gnus-category-predicate-cache))
1178 (cdar (push (cons predicate
1179 (gnus-category-make-function predicate))
1180 gnus-category-predicate-cache))))
1182 (defun gnus-group-category (group)
1183 "Return the category GROUP belongs to."
1184 (unless gnus-category-group-cache
1185 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1186 (let ((cs gnus-category-alist)
1188 (while (setq cat (pop cs))
1189 (setq groups (cadddr cat))
1191 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1192 (or (gnus-gethash group gnus-category-group-cache)
1193 (assq 'default gnus-category-alist)))
1195 (defun gnus-agent-expire ()
1196 "Expire all old articles."
1198 (let ((methods gnus-agent-covered-methods)
1199 (alist (cdr gnus-newsrc-alist))
1200 gnus-command-method ofiles info method file group)
1201 (while (setq gnus-command-method (pop methods))
1202 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1203 (gnus-agent-directory)))))
1204 (while (setq info (pop alist))
1205 (when (and (gnus-agent-method-p
1206 (setq gnus-command-method
1207 (gnus-find-method-for-group
1208 (setq group (gnus-info-group info)))))
1212 (gnus-agent-directory)
1213 (gnus-agent-group-path group) "/.overview"))
1215 (setq ofiles (delete file ofiles))
1216 (gnus-agent-expire-group file group)))
1218 (gnus-agent-expire-group (pop ofiles)))))
1220 (defun gnus-agent-expire-directory (dir)
1221 "Expire all groups in DIR recursively."
1222 (when (file-directory-p dir)
1223 (let ((files (directory-files dir t))
1225 (while (setq file (pop files))
1227 ((member (file-name-nondirectory file) '("." ".."))
1230 ((file-directory-p file)
1232 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1233 ((string-match "\\.overview$" file)
1235 (push file ofiles))))
1238 (defun gnus-agent-expire-group (overview &optional group)
1239 "Expire articles in OVERVIEW."
1240 (gnus-message 5 "Expiring %s..." overview)
1241 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1242 (dir (file-name-directory overview))
1243 (info (when group (gnus-get-info group)))
1244 headers article file point unreads)
1245 (gnus-agent-load-alist nil dir)
1249 (gnus-list-of-unread-articles group)
1250 (gnus-uncompress-range
1251 (cdr (assq 'tick (gnus-info-marks info))))
1252 (gnus-uncompress-range
1253 (cdr (assq 'dormant (gnus-info-marks info)))))))
1254 (nnheader-temp-write overview
1255 (insert-file-contents overview)
1256 (goto-char (point-min))
1258 (setq point (point))
1260 (setq headers (inline (nnheader-parse-nov)))
1264 (setq headers nil)))
1266 (if (memq (setq article (mail-header-number headers)) unreads)
1270 (inline (nnmail-date-to-time
1271 (mail-header-date headers)))))
1275 (setq gnus-agent-article-alist
1276 (delq (assq article gnus-agent-article-alist)
1277 gnus-agent-article-alist))
1278 (when (file-exists-p
1279 (setq file (concat dir (number-to-string article))))
1280 (delete-file file))))))
1281 (gnus-agent-save-alist nil nil nil dir))))
1284 (defun gnus-agent-batch ()
1286 (let ((init-file-user "")
1287 (gnus-always-read-dribble-file t))
1289 (gnus-group-send-drafts)
1290 (gnus-agent-fetch-session))
1292 (provide 'gnus-agent)
1294 ;;; gnus-agent.el ends here