1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
34 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
35 "Where the Gnus agent will store its files."
39 (defcustom gnus-agent-plugged-hook nil
40 "Hook run when plugging into the network."
44 (defcustom gnus-agent-unplugged-hook nil
45 "Hook run when unplugging from the network."
49 ;;; Internal variables
51 (defvar gnus-agent-history-buffers nil)
52 (defvar gnus-agent-buffer-alist nil)
53 (defvar gnus-agent-article-alist nil)
54 (defvar gnus-agent-group-alist nil)
55 (defvar gnus-agent-covered-methods nil)
56 (defvar gnus-category-alist nil)
57 (defvar gnus-agent-current-history nil)
58 (defvar gnus-agent-overview-buffer nil)
59 (defvar gnus-category-predicate-cache nil)
60 (defvar gnus-category-group-cache nil)
61 (defvar gnus-agent-spam-hashtb nil)
62 (defvar gnus-agent-file-name nil)
63 (defvar gnus-agent-send-mail-function nil)
65 (defvar gnus-plugged t
66 "Whether Gnus is plugged or not.")
76 (defun gnus-open-agent ()
78 (gnus-agent-read-servers)
80 (setq gnus-agent-overview-buffer
81 (get-buffer-create " *Gnus agent overview*"))
82 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
83 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
84 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
86 (gnus-add-shutdown 'gnus-close-agent 'gnus)
88 (defun gnus-close-agent ()
89 (setq gnus-agent-covered-methods nil
90 gnus-category-predicate-cache nil
91 gnus-category-group-cache nil
92 gnus-agent-spam-hashtb nil)
93 (gnus-kill-buffer gnus-agent-overview-buffer))
99 (defun gnus-agent-read-file (file)
100 "Load FILE and do a `read' there."
101 (nnheader-temp-write nil
103 (insert-file-contents file)
104 (goto-char (point-min))
105 (read (current-buffer)))))
107 (defsubst gnus-agent-method ()
108 (concat (symbol-name (car gnus-command-method)) "/"
109 (if (equal (cadr gnus-command-method) "")
111 (cadr gnus-command-method))))
113 (defsubst gnus-agent-directory ()
114 "Path of the Gnus agent directory."
115 (nnheader-concat gnus-agent-directory (gnus-agent-method) "/"))
117 (defun gnus-agent-lib-file (file)
118 "The full path of the Gnus agent library FILE."
119 (concat (gnus-agent-directory) "agent.lib/" file))
125 (defvar gnus-agent-mode-hook nil
126 "Hook run when installing agent mode.")
128 (defvar gnus-agent-mode nil)
129 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
131 (defun gnus-agent-mode ()
132 "Minor mode for providing a agent support in Gnus buffers."
133 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
134 (symbol-name major-mode))
135 (match-string 1 (symbol-name major-mode))))
136 (mode (intern (format "gnus-agent-%s-mode" buffer))))
137 (set (make-local-variable 'gnus-agent-mode) t)
139 (set (make-local-variable mode) t)
141 (when (gnus-visual-p 'agent-menu 'menu)
142 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
143 (unless (assq 'gnus-agent-mode minor-mode-alist)
144 (push gnus-agent-mode-status minor-mode-alist))
145 (unless (assq mode minor-mode-map-alist)
146 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
148 minor-mode-map-alist))
149 (gnus-agent-toggle-plugged gnus-plugged)
150 (run-hooks 'gnus-agent-mode-hook)))
152 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
153 (gnus-define-keys gnus-agent-group-mode-map
154 "Ju" gnus-agent-fetch-group
155 "Jc" gnus-enter-category-buffer
156 "Jj" gnus-agent-toggle-plugged
157 "Js" gnus-agent-fetch-session
158 "JS" gnus-group-send-drafts
159 "Ja" gnus-agent-add-group)
161 (defun gnus-agent-group-make-menu-bar ()
162 (unless (boundp 'gnus-agent-group-menu)
164 gnus-agent-group-menu gnus-agent-group-mode-map ""
166 ["Toggle plugged" gnus-agent-toggle-plugged t]
167 ["List categories" gnus-enter-category-buffer t]
168 ["Send drafts" gnus-group-send-drafts gnus-plugged]
170 ["All" gnus-agent-fetch-session gnus-plugged]
171 ["Group" gnus-agent-fetch-group gnus-plugged])))))
173 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
174 (gnus-define-keys gnus-agent-summary-mode-map
175 "Jj" gnus-agent-toggle-plugged
176 "J#" gnus-agent-mark-article
177 "J\M-#" gnus-agent-unmark-article
178 "@" gnus-agent-toggle-mark
179 "Jc" gnus-agent-catchup)
181 (defun gnus-agent-summary-make-menu-bar ()
182 (unless (boundp 'gnus-agent-summary-menu)
184 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
186 ["Toggle plugged" gnus-agent-toggle-plugged t]
187 ["Mark as downloadable" gnus-agent-mark-article t]
188 ["Unmark as downloadable" gnus-agent-unmark-article t]
189 ["Toggle mark" gnus-agent-toggle-mark t]
190 ["Catchup undownloaded" gnus-agent-catchup t]))))
192 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
193 (gnus-define-keys gnus-agent-server-mode-map
194 "Jj" gnus-agent-toggle-plugged
195 "Ja" gnus-agent-add-server
196 "Jr" gnus-agent-remove-server)
198 (defun gnus-agent-server-make-menu-bar ()
199 (unless (boundp 'gnus-agent-server-menu)
201 gnus-agent-server-menu gnus-agent-server-mode-map ""
203 ["Toggle plugged" gnus-agent-toggle-plugged t]
204 ["Add" gnus-agent-add-server t]
205 ["Remove" gnus-agent-remove-server t]))))
207 (defun gnus-agent-toggle-plugged (plugged)
208 "Toggle whether Gnus is unplugged or not."
209 (interactive (list (not gnus-plugged)))
212 (run-hooks 'gnus-agent-plugged-hook)
213 (setcar (cdr gnus-agent-mode-status) " Plugged"))
214 (gnus-agent-close-connections)
215 (run-hooks 'gnus-agent-unplugged-hook)
216 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
217 (setq gnus-plugged plugged)
218 (set-buffer-modified-p t))
220 (defun gnus-agent-close-connections ()
221 "Close all methods covered by the Gnus agent."
222 (let ((methods gnus-agent-covered-methods))
224 (gnus-close-server (pop methods)))))
227 (defun gnus-unplugged ()
228 "Start Gnus unplugged."
230 (setq gnus-plugged nil)
234 (defun gnus-agentize ()
235 "Allow Gnus to be an offline newsreader.
236 The normal usage of this command is to put the following as the
237 last form in your `.gnus.el' file:
241 This will modify the `gnus-before-startup-hook', `gnus-post-method',
242 and `message-send-mail-function' variables, and install the Gnus
243 agent minor mode in all Gnus buffers."
245 (add-hook 'gnus-before-startup-hook 'gnus-open-agent)
246 (unless gnus-agent-send-mail-function
247 (setq gnus-agent-send-mail-function message-send-mail-function
248 message-send-mail-function 'gnus-agent-send-mail))
249 (unless gnus-agent-covered-methods
250 (setq gnus-agent-covered-methods (list gnus-select-method))))
252 (defun gnus-agent-send-mail ()
254 (funcall gnus-agent-send-mail-function)
255 (goto-char (point-min))
257 (concat "^" (regexp-quote mail-header-separator) "\n"))
259 (gnus-request-accept-article "nndraft:drafts")))
262 ;;; Group mode commands
265 (defun gnus-agent-fetch-group (group)
266 "Put all new articles in GROUP into the agent."
267 (interactive (list (gnus-group-group-name)))
269 (error "No group on the current line"))
270 (let ((gnus-command-method (gnus-find-method-for-group group)))
271 (gnus-agent-with-fetch
272 (gnus-agent-fetch-group-1 group gnus-command-method))))
274 (defun gnus-agent-add-group (category arg)
275 "Add the current group to an agent category."
281 (mapcar (lambda (cat) (list (symbol-name (car cat))))
285 (let ((cat (assq category gnus-category-alist))
287 (gnus-group-iterate arg
289 (when (cadddr (setq c (gnus-group-category group)))
290 (setf (cadddr c) (delete group (cadddr c))))
291 (push group groups)))
292 (setf (cadddr cat) (nconc (cadddr cat) groups))
293 (gnus-category-write)))
296 ;;; Server mode commands
299 (defun gnus-agent-add-server (server)
300 "Enroll SERVER in the agent program."
301 (interactive (list (gnus-server-server-name)))
303 (error "No server on the current line"))
304 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
305 (when (member method gnus-agent-covered-methods)
306 (error "Server already in the agent program"))
307 (push method gnus-agent-covered-methods)
308 (gnus-agent-write-servers)
309 (message "Entered %s into the agent" server)))
311 (defun gnus-agent-remove-server (server)
312 "Remove SERVER from the agent program."
313 (interactive (list (gnus-server-server-name)))
315 (error "No server on the current line"))
316 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
317 (unless (member method gnus-agent-covered-methods)
318 (error "Server not in the agent program"))
319 (setq gnus-agent-covered-methods
320 (delete method gnus-agent-covered-methods))
321 (gnus-agent-write-servers)
322 (message "Removed %s from the agent" server)))
324 (defun gnus-agent-read-servers ()
325 "Read the alist of covered servers."
326 (setq gnus-agent-covered-methods
327 (gnus-agent-read-file
328 (nnheader-concat gnus-agent-directory "lib/servers"))))
330 (defun gnus-agent-write-servers ()
331 "Write the alist of covered servers."
332 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
333 (prin1 gnus-agent-covered-methods (current-buffer))))
339 (defun gnus-agent-mark-article (n &optional unmark)
340 "Mark the next N articles as downloadable.
341 If N is negative, mark backward instead. If UNMARK is non-nil, remove
342 the mark instead. The difference between N and the actual number of
343 articles marked is returned."
345 (gnus-set-global-variables)
346 (let ((backward (< n 0))
351 (gnus-summary-set-agent-mark
352 (gnus-summary-article-number) unmark)
353 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
356 (gnus-message 7 "No more articles"))
357 (gnus-summary-recenter)
358 (gnus-summary-position-point)
361 (defun gnus-agent-unmark-article (n)
362 "Remove the downloadable mark from the next N articles.
363 If N is negative, unmark backward instead. The difference between N and
364 the actual number of articles unmarked is returned."
366 (gnus-set-global-variables)
367 (gnus-agent-mark-article n t))
369 (defun gnus-agent-toggle-mark (n)
370 "Toggle the downloadable mark from the next N articles.
371 If N is negative, toggle backward instead. The difference between N and
372 the actual number of articles toggled is returned."
374 (gnus-set-global-variables)
375 (gnus-agent-mark-article n 'toggle))
377 (defun gnus-summary-set-agent-mark (article &optional unmark)
378 "Mark ARTICLE as downloadable."
379 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
380 (memq article gnus-newsgroup-downloadable)
382 (setq gnus-newsgroup-downloadable
383 (delq article gnus-newsgroup-downloadable))
385 (push article gnus-newsgroup-downloadable))
386 (gnus-summary-update-mark
387 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
390 (defun gnus-agent-get-undownloaded-list ()
391 "Mark all unfetched articles as read."
392 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
393 (when (and (not gnus-plugged)
394 (gnus-agent-method-p gnus-command-method))
395 (gnus-agent-load-alist gnus-newsgroup-name)
396 (let ((articles gnus-newsgroup-unreads)
398 (while (setq article (pop articles))
399 (unless (or (cdr (assq article gnus-agent-article-alist))
400 (memq article gnus-newsgroup-downloadable))
401 (push article gnus-newsgroup-undownloaded)))))))
403 (defun gnus-agent-catchup ()
404 "Mark all undownloaded articles as read."
407 (while gnus-newsgroup-undownloaded
408 (gnus-summary-mark-article
409 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
410 (gnus-summary-position-point))
413 ;;; Internal functions
416 (defun gnus-agent-save-active (method)
417 (when (gnus-agent-method-p method)
418 (let* ((gnus-command-method method)
419 (file (gnus-agent-lib-file "active")))
420 (gnus-make-directory (file-name-directory file))
421 (write-region (point-min) (point-max) file nil 'silent)
422 (when (file-exists-p (gnus-agent-lib-file "groups"))
423 (delete-file (gnus-agent-lib-file "groups"))))))
425 (defun gnus-agent-save-groups (method)
426 (let* ((gnus-command-method method)
427 (file (gnus-agent-lib-file "groups")))
428 (gnus-make-directory (file-name-directory file))
429 (write-region (point-min) (point-max) file nil 'silent))
430 (when (file-exists-p (gnus-agent-lib-file "active"))
431 (delete-file (gnus-agent-lib-file "active"))))
433 (defun gnus-agent-group-path (group)
434 "Translate GROUP into a path."
435 (nnheader-replace-chars-in-string group ?. ?/))
439 (defun gnus-agent-method-p (method)
440 "Say whether METHOD is covered by the agent."
441 (member method gnus-agent-covered-methods))
443 (defun gnus-agent-get-function (method)
444 (if (and (not gnus-plugged)
445 (gnus-agent-method-p method))
451 ;;; History functions
453 (defun gnus-agent-history-buffer ()
454 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
456 (defun gnus-agent-open-history ()
458 (push (cons (gnus-agent-method)
459 (set-buffer (get-buffer-create
460 (format " *Gnus agent %s history*"
461 (gnus-agent-method)))))
462 gnus-agent-history-buffers)
465 (let ((file (gnus-agent-lib-file "history")))
466 (when (file-exists-p file)
468 (set (make-local-variable 'gnus-agent-file-name) file))))
470 (defun gnus-agent-save-history ()
472 (set-buffer gnus-agent-current-history)
473 (gnus-make-directory (file-name-directory gnus-agent-file-name))
474 (write-region (1+ (point-min)) (point-max)
475 gnus-agent-file-name nil 'silent)))
477 (defun gnus-agent-close-history ()
478 (when (gnus-buffer-live-p gnus-agent-current-history)
479 (kill-buffer gnus-agent-current-history)
480 (setq gnus-agent-history-buffers
481 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
482 gnus-agent-history-buffers))))
484 (defun gnus-agent-enter-history (id group-arts date)
486 (set-buffer gnus-agent-current-history)
487 (goto-char (point-max))
488 (insert id "\t" (number-to-string date) "\t")
490 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
494 (defun gnus-agent-article-in-history-p (id)
496 (set-buffer (gnus-agent-history-buffer))
497 (goto-char (point-min))
498 (search-forward (concat "\n" id "\t") nil t)))
500 (defun gnus-agent-history-path (id)
502 (set-buffer (gnus-agent-history-buffer))
503 (goto-char (point-min))
504 (when (search-forward (concat "\n" id "\t") nil t)
505 (let ((method (gnus-agent-method)))
507 (while (not (numberp (setq group (read (current-buffer)))))
508 (push (concat method "/" group) paths))
509 (nreverse paths))))))
515 (defun gnus-agent-start-fetch ()
516 "Initialize data structures for efficient fetching."
517 (gnus-agent-open-history)
518 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
520 (defun gnus-agent-stop-fetch ()
521 "Save all data structures and clean up."
522 (gnus-agent-save-history)
523 (gnus-agent-close-history)
524 (setq gnus-agent-spam-hashtb nil)
526 (set-buffer nntp-server-buffer)
529 (defmacro gnus-agent-with-fetch (&rest forms)
533 (gnus-agent-start-fetch)
535 (gnus-agent-stop-fetch)))
537 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
538 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
540 (defun gnus-agent-fetch-articles (group articles)
541 "Fetch ARTICLES from GROUP and put them into the agent."
543 ;; Prune off articles that we have already fetched.
545 (cdr (assq (car articles) gnus-agent-article-alist)))
547 (let ((arts articles))
549 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
550 (setcdr arts (cddr arts))
551 (setq arts (cdr arts)))))
554 (gnus-agent-directory)
555 (gnus-agent-group-path group) "/"))
556 (date (gnus-time-to-day (current-time)))
558 pos alists crosses id elem)
559 (gnus-make-directory dir)
560 (gnus-message 7 "Fetching articles for %s..." group)
561 ;; Fetch the articles from the backend.
562 (if (gnus-check-backend-function 'retrieve-articles group)
563 (setq pos (gnus-retrieve-articles articles group))
564 (nnheader-temp-write nil
565 (let ((buf (current-buffer))
567 (while (setq article (pop articles))
568 (when (gnus-request-article article group)
569 (goto-char (point-max))
570 (push (cons article (point)) pos)
571 (insert-buffer-substring nntp-server-buffer)))
572 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
573 (setq pos (nreverse pos)))))
574 ;; Then save these articles into the agent.
576 (set-buffer nntp-server-buffer)
578 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
579 (goto-char (point-min))
580 (when (search-forward "\n\n" nil t)
581 (when (search-backward "\nXrefs: " nil t)
582 ;; Handle crossposting.
583 (skip-chars-forward "^ ")
584 (skip-chars-forward " ")
586 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
587 (push (cons (buffer-substring (match-beginning 1)
589 (buffer-substring (match-beginning 2)
592 (goto-char (match-end 0)))
593 (gnus-agent-crosspost crosses (caar pos))))
594 (goto-char (point-min))
595 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
596 (setq id "No-Message-ID-in-article")
597 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
598 (write-region (point-min) (point-max)
599 (concat dir (number-to-string (caar pos)))
601 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
603 (gnus-agent-enter-history
604 id (or crosses (list (cons group (caar pos)))) date)
607 (gnus-agent-save-alist group)))))
609 (defun gnus-agent-crosspost (crosses article)
610 (let (gnus-agent-article-alist group alist beg end)
612 (set-buffer gnus-agent-overview-buffer)
613 (when (nnheader-find-nov-line article)
616 (setq end (progn (forward-line 1) (point)))))
618 (setq group (caar crosses))
619 (unless (setq alist (assoc group gnus-agent-group-alist))
620 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
621 gnus-agent-group-alist))
622 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
624 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
626 (when (= (point-max) (point-min))
627 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
629 (insert-file-contents
630 (gnus-agent-article-name ".overview" group))))
631 (nnheader-find-nov-line (string-to-number (cdar crosses)))
632 (insert (string-to-number (cdar crosses)))
633 (insert-buffer-substring gnus-agent-overview-buffer beg end))
636 (defun gnus-agent-flush-cache ()
638 (while gnus-agent-buffer-alist
639 (set-buffer (cdar gnus-agent-buffer-alist))
640 (write-region (point-min) (point-max)
641 (gnus-agent-article-name ".overview"
642 (caar gnus-agent-buffer-alist))
644 (pop gnus-agent-buffer-alist))
645 (while gnus-agent-group-alist
646 (nnheader-temp-write (caar gnus-agent-group-alist)
647 (princ (cdar gnus-agent-group-alist))
649 (pop gnus-agent-group-alist))))
651 (defun gnus-agent-fetch-headers (group articles &optional force)
652 (gnus-agent-load-alist group)
653 ;; Find out what headers we need to retrieve.
656 (assq (car articles) gnus-agent-article-alist))
658 (let ((arts articles))
660 (if (assq (cadr arts) gnus-agent-article-alist)
661 (setcdr arts (cddr arts))
662 (setq arts (cdr arts)))))
665 (gnus-message 7 "Fetching headers for %s..." group)
667 (set-buffer nntp-server-buffer)
668 (unless (eq 'nov (gnus-retrieve-headers articles group))
669 (nnvirtual-convert-headers))
670 ;; Save these headers for later processing.
671 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
674 (setq file (gnus-agent-article-name ".overview" group)))
675 (gnus-agent-braid-nov group articles file))
676 (gnus-make-directory (file-name-directory file))
677 (write-region (point-min) (point-max) file nil 'silent)
678 (gnus-agent-save-alist group articles nil))
681 (defsubst gnus-agent-copy-nov-line (article)
683 (set-buffer gnus-agent-overview-buffer)
685 (if (eq article (read (current-buffer)))
686 (setq e (progn (forward-line 1) (point)))
688 (set-buffer nntp-server-buffer)
689 (insert-buffer-substring gnus-agent-overview-buffer b e)))
691 (defun gnus-agent-braid-nov (group articles file)
693 (set-buffer gnus-agent-overview-buffer)
694 (goto-char (point-min))
695 (set-buffer nntp-server-buffer)
697 (insert-file-contents file)
698 (goto-char (point-min))
699 (if (or (= (point-min) (point-max))
702 (< (read (current-buffer)) (car articles))))
703 ;; We have only headers that are after the older headers,
704 ;; so we just append them.
706 (goto-char (point-max))
707 (insert-buffer-substring gnus-agent-overview-buffer))
708 ;; We do it the hard way.
709 (nnheader-find-nov-line (car articles))
710 (gnus-agent-copy-nov-line (car articles))
714 (while (and (not (eobp))
715 (< (read (current-buffer)) (car articles)))
719 (gnus-agent-copy-nov-line (car articles))
720 (setq articles (cdr articles))))
723 (set-buffer gnus-agent-overview-buffer)
726 (set-buffer nntp-server-buffer)
727 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
729 (defun gnus-agent-load-alist (group &optional dir)
730 "Load the article-state alist for GROUP."
731 (setq gnus-agent-article-alist
732 (gnus-agent-read-file
734 (concat dir ".agentview")
735 (gnus-agent-article-name ".agentview" group)))))
737 (defun gnus-agent-save-alist (group &optional articles state dir)
738 "Load the article-state alist for GROUP."
739 (nnheader-temp-write (if dir
740 (concat dir ".agentview")
741 (gnus-agent-article-name ".agentview" group))
742 (princ (setq gnus-agent-article-alist
743 (nconc gnus-agent-article-alist
744 (mapcar (lambda (article) (cons article state))
749 (defun gnus-agent-article-name (article group)
750 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
751 (if (stringp article) article (string-to-number article))))
753 (defun gnus-agent-fetch-session ()
754 "Fetch all articles and headers that are eligible for fetching."
756 (unless gnus-agent-covered-methods
757 (error "No servers are covered by the Gnus agent"))
759 (error "Can't fetch articles while Gnus is unplugged"))
760 (let ((methods gnus-agent-covered-methods)
761 groups group gnus-command-method)
764 (setq gnus-command-method (car methods)
765 groups (gnus-groups-from-server (pop methods)))
766 (gnus-agent-with-fetch
767 (while (setq group (pop groups))
768 (gnus-agent-fetch-group-1 group gnus-command-method))))
769 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
771 (defun gnus-agent-fetch-group-1 (group method)
773 (let ((gnus-command-method method)
774 gnus-newsgroup-dependencies gnus-newsgroup-headers
775 gnus-newsgroup-scored gnus-headers gnus-score
776 gnus-use-cache articles score arts
777 category predicate info marks score-param)
779 (when (and (setq articles (gnus-list-of-unread-articles group))
780 (gnus-agent-fetch-headers group articles))
781 ;; Parse them and see which articles we want to fetch.
782 (setq gnus-newsgroup-dependencies
783 (make-vector (length articles) 0))
784 (setq gnus-newsgroup-headers
785 (gnus-get-newsgroup-headers-xover articles nil nil group))
786 (setq category (gnus-group-category group))
789 (or (gnus-group-get-parameter group 'agent-predicate)
792 (or (gnus-group-get-parameter group 'agent-score)
795 (gnus-score-headers (list (list score-param))))
797 (while (setq gnus-headers (pop gnus-newsgroup-headers))
799 (or (cdr (assq (mail-header-number gnus-headers)
800 gnus-newsgroup-scored))
801 gnus-summary-default-score))
802 (when (funcall predicate)
803 (push (mail-header-number gnus-headers)
805 ;; Fetch the articles.
807 (gnus-agent-fetch-articles group arts)))
808 ;; Perhaps we have some additional articles to fetch.
809 (setq arts (assq 'download (gnus-info-marks
810 (setq info (gnus-get-info group)))))
812 (gnus-agent-fetch-articles
813 group (gnus-uncompress-range (cdr arts)))
814 (setq marks (delq arts (gnus-info-marks info)))
815 (gnus-info-set-marks info marks))))
818 ;;; Agent Category Mode
821 (defvar gnus-category-mode-hook nil
822 "Hook run in `gnus-category-mode' buffers.")
824 (defvar gnus-category-line-format " %(%20c%): %g\n"
825 "Format of category lines.")
827 (defvar gnus-category-mode-line-format "Gnus: %%b"
828 "The format specification for the category mode line.")
830 (defvar gnus-agent-short-article 100
831 "Articles that have fewer lines than this are short.")
833 (defvar gnus-agent-long-article 200
834 "Articles that have more lines than this are long.")
836 (defvar gnus-agent-low-score 0
837 "Articles that have a score lower than this have a low score.")
839 (defvar gnus-agent-high-score 0
840 "Articles that have a score higher than this have a high score.")
843 ;;; Internal variables.
845 (defvar gnus-category-buffer "*Agent Category*")
847 (defvar gnus-category-line-format-alist
851 (defvar gnus-category-mode-line-format-alist
852 `((?u user-defined ?s)))
854 (defvar gnus-category-line-format-spec nil)
855 (defvar gnus-category-mode-line-format-spec nil)
857 (defvar gnus-category-mode-map nil)
858 (put 'gnus-category-mode 'mode-class 'special)
860 (unless gnus-category-mode-map
861 (setq gnus-category-mode-map (make-sparse-keymap))
862 (suppress-keymap gnus-category-mode-map)
864 (gnus-define-keys gnus-category-mode-map
865 "q" gnus-category-exit
866 "k" gnus-category-kill
867 "c" gnus-category-copy
868 "a" gnus-category-add
869 "p" gnus-category-edit-predicate
870 "g" gnus-category-edit-groups
871 "s" gnus-category-edit-score
872 "l" gnus-category-list
874 "\C-c\C-i" gnus-info-find-node
875 "\C-c\C-b" gnus-bug))
877 (defvar gnus-category-menu-hook nil
878 "*Hook run after the creation of the menu.")
880 (defun gnus-category-make-menu-bar ()
881 (gnus-turn-off-edit-menu 'category)
882 (unless (boundp 'gnus-category-menu)
884 gnus-category-menu gnus-category-mode-map ""
886 ["Add" gnus-category-add t]
887 ["Kill" gnus-category-kill t]
888 ["Copy" gnus-category-copy t]
889 ["Edit predicate" gnus-category-edit-predicate t]
890 ["Edit score" gnus-category-edit-score t]
891 ["Edit groups" gnus-category-edit-groups t]
892 ["Exit" gnus-category-exit t]))
894 (run-hooks 'gnus-category-menu-hook)))
896 (defun gnus-category-mode ()
897 "Major mode for listing and editing agent categories.
899 All normal editing commands are switched off.
900 \\<gnus-category-mode-map>
901 For more in-depth information on this mode, read the manual
902 (`\\[gnus-info-find-node]').
904 The following commands are available:
906 \\{gnus-category-mode-map}"
908 (when (gnus-visual-p 'category-menu 'menu)
909 (gnus-category-make-menu-bar))
910 (kill-all-local-variables)
911 (gnus-simplify-mode-line)
912 (setq major-mode 'gnus-category-mode)
913 (setq mode-name "Category")
914 (gnus-set-default-directory)
915 (setq mode-line-process nil)
916 (use-local-map gnus-category-mode-map)
917 (buffer-disable-undo (current-buffer))
918 (setq truncate-lines t)
919 (setq buffer-read-only t)
920 (run-hooks 'gnus-category-mode-hook))
922 (defalias 'gnus-category-position-point 'gnus-goto-colon)
924 (defun gnus-category-insert-line (category)
925 (let* ((name (car category))
926 (groups (length (cadddr category))))
928 (gnus-add-text-properties
932 (eval gnus-category-line-format-spec))
933 (list 'gnus-category name))))
935 (defun gnus-enter-category-buffer ()
936 "Go to the Category buffer."
938 (gnus-category-setup-buffer)
939 (gnus-configure-windows 'category)
940 (gnus-category-prepare))
942 (defun gnus-category-setup-buffer ()
943 (unless (get-buffer gnus-category-buffer)
945 (set-buffer (get-buffer-create gnus-category-buffer))
946 (gnus-add-current-to-buffer-list)
947 (gnus-category-mode))))
949 (defun gnus-category-prepare ()
950 (gnus-set-format 'category-mode)
951 (gnus-set-format 'category t)
952 (let ((alist gnus-category-alist)
953 (buffer-read-only nil))
956 (gnus-category-insert-line (pop alist)))
957 (goto-char (point-min))
958 (gnus-category-position-point)))
960 (defun gnus-category-name ()
961 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
962 (error "No category on the current line")))
964 (defun gnus-category-read ()
965 "Read the category alist."
966 (setq gnus-category-alist
967 (or (gnus-agent-read-file
968 (nnheader-concat gnus-agent-directory "lib/categories"))
969 (list (list 'default 'true nil nil)))))
971 (defun gnus-category-write ()
972 "Write the category alist."
973 (setq gnus-category-predicate-cache nil
974 gnus-category-group-cache nil)
975 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
976 (prin1 gnus-category-alist (current-buffer))))
978 (defun gnus-category-edit-predicate (category)
979 "Edit the predicate for CATEGORY."
980 (interactive (list (gnus-category-name)))
981 (let ((info (assq category gnus-category-alist)))
983 (cadr info) (format "Editing the predicate for category %s" category)
985 (setf (cadr (assq ',category gnus-category-alist)) predicate)
986 (gnus-category-write)
987 (gnus-category-list)))))
989 (defun gnus-category-edit-score (category)
990 "Edit the score expression for CATEGORY."
991 (interactive (list (gnus-category-name)))
992 (let ((info (assq category gnus-category-alist)))
995 (format "Editing the score expression for category %s" category)
997 (setf (caddr (assq ',category gnus-category-alist)) groups)
998 (gnus-category-write)
999 (gnus-category-list)))))
1001 (defun gnus-category-edit-groups (category)
1002 "Edit the group list for CATEGORY."
1003 (interactive (list (gnus-category-name)))
1004 (let ((info (assq category gnus-category-alist)))
1006 (cadddr info) (format "Editing the group list for category %s" category)
1008 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1009 (gnus-category-write)
1010 (gnus-category-list)))))
1012 (defun gnus-category-kill (category)
1013 "Kill the current category."
1014 (interactive (list (gnus-category-name)))
1015 (let ((info (assq category gnus-category-alist))
1016 (buffer-read-only nil))
1018 (gnus-category-write)
1019 (setq gnus-category-alist (delq info gnus-category-alist))))
1021 (defun gnus-category-copy (category to)
1022 "Copy the current category."
1023 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1024 (let ((info (assq category gnus-category-alist)))
1025 (push (list to (gnus-copy-sequence (cadr info))
1026 (gnus-copy-sequence (caddr info)) nil)
1027 gnus-category-alist)
1028 (gnus-category-write)
1029 (gnus-category-list)))
1031 (defun gnus-category-add (category)
1032 "Create a new category."
1033 (interactive "SCategory name: ")
1034 (when (assq category gnus-category-alist)
1035 (error "Category %s already exists" category))
1036 (push (list category 'true nil nil)
1037 gnus-category-alist)
1038 (gnus-category-write)
1039 (gnus-category-list))
1041 (defun gnus-category-list ()
1042 "List all categories."
1044 (gnus-category-prepare))
1046 (defun gnus-category-exit ()
1047 "Return to the group buffer."
1049 (kill-buffer (current-buffer))
1050 (gnus-configure-windows 'group t))
1052 ;; To avoid having 8-bit characters in the source file.
1053 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1055 (defvar gnus-category-predicate-alist
1056 '((spam . gnus-agent-spam-p)
1057 (short . gnus-agent-short-p)
1058 (long . gnus-agent-long-p)
1059 (low . gnus-agent-low-scored-p)
1060 (high . gnus-agent-high-scored-p)
1061 (true . gnus-agent-true)
1062 (false . gnus-agent-false))
1063 "Mapping from short score predicate symbols to predicate functions.")
1065 (defun gnus-agent-spam-p ()
1066 "Say whether an article is spam or not."
1067 (unless gnus-agent-spam-hashtb
1068 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1069 (if (not (equal (mail-header-references gnus-headers) ""))
1071 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1073 (gnus-gethash string gnus-agent-spam-hashtb)
1074 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1076 (defun gnus-agent-short-p ()
1077 "Say whether an article is short or not."
1078 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1080 (defun gnus-agent-long-p ()
1081 "Say whether an article is long or not."
1082 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1084 (defun gnus-agent-low-scored-p ()
1085 "Say whether an article has a low score or not."
1086 (< gnus-score gnus-agent-low-score))
1088 (defun gnus-agent-high-scored-p ()
1089 "Say whether an article has a high score or not."
1090 (> gnus-score gnus-agent-low-score))
1092 (defun gnus-category-make-function (cat)
1093 "Make a function from category CAT."
1094 `(lambda () ,(gnus-category-make-function-1 cat)))
1096 (defun gnus-agent-true ()
1100 (defun gnus-agent-false ()
1104 (defun gnus-category-make-function-1 (cat)
1105 "Make a function from category CAT."
1107 ;; Functions are just returned as is.
1109 (gnus-functionp cat))
1110 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1112 ;; More complex category.
1115 ((memq (car cat) '(& and))
1117 ((memq (car cat) '(| or))
1119 ((memq (car cat) gnus-category-not)
1121 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1123 (error "Unknown category type: %s" cat))))
1125 (defun gnus-get-predicate (predicate)
1126 "Return the predicate for CATEGORY."
1127 (or (cdr (assoc predicate gnus-category-predicate-cache))
1128 (cdar (push (cons predicate
1129 (gnus-category-make-function predicate))
1130 gnus-category-predicate-cache))))
1132 (defun gnus-group-category (group)
1133 "Return the category GROUP belongs to."
1134 (unless gnus-category-group-cache
1135 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1136 (let ((cs gnus-category-alist)
1138 (while (setq cat (pop cs))
1139 (setq groups (cadddr cat))
1141 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1142 (or (gnus-gethash group gnus-category-group-cache)
1143 (assq 'default gnus-category-alist)))
1145 (defun gnus-agent-expire ()
1146 "Expire all old articles."
1148 (let ((methods gnus-agent-covered-methods)
1149 (alist (cdr gnus-newsrc-alist))
1150 gnus-command-method ofiles info method file group)
1151 (while (setq gnus-command-method (pop methods))
1152 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1153 (gnus-agent-directory)))))
1154 (while (setq info (pop alist))
1155 (when (and (gnus-agent-method-p
1156 (setq gnus-command-method
1157 (gnus-find-method-for-group
1158 (setq group (gnus-info-group info)))))
1162 (gnus-agent-directory)
1163 (gnus-agent-group-path group) "/.overview"))
1165 (setq ofiles (delete file ofiles))
1166 (gnus-agent-expire-group file group)))
1168 (gnus-agent-expire-group (pop ofiles)))))
1170 (defun gnus-agent-expire-directory (dir)
1171 "Expire all groups in DIR recursively."
1172 (when (file-directory-p dir)
1173 (let ((files (directory-files dir t))
1175 (while (setq file (pop files))
1177 ((member (file-name-nondirectory file) '("." ".."))
1180 ((file-directory-p file)
1182 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1183 ((string-match "\\.overview$" file)
1185 (push file ofiles))))
1188 (defun gnus-agent-expire-group (overview &optional group)
1189 "Expire articles in OVERVIEW."
1190 (gnus-message 5 "Expiring %s..." overview)
1191 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1192 (dir (file-name-directory overview))
1193 (info (when group (gnus-get-info group)))
1194 headers article file point unreads)
1195 (gnus-agent-load-alist nil dir)
1199 (gnus-list-of-unread-articles group)
1200 (gnus-uncompress-range
1201 (cdr (assq 'tick (gnus-info-marks info))))
1202 (gnus-uncompress-range
1203 (cdr (assq 'dormant (gnus-info-marks info)))))))
1204 (nnheader-temp-write overview
1205 (insert-file-contents overview)
1206 (goto-char (point-min))
1208 (setq point (point))
1210 (setq headers (inline (nnheader-parse-nov)))
1214 (setq headers nil)))
1216 (unless (memq (setq article (mail-header-number headers)) unreads)
1219 (inline (nnmail-date-to-time
1220 (mail-header-date headers)))))
1224 (setq gnus-agent-article-alist
1225 (delq (assq article gnus-agent-article-alist)
1226 gnus-agent-article-alist))
1227 (when (file-exists-p
1228 (setq file (concat dir (number-to-string article))))
1229 (delete-file file))))))
1230 (gnus-agent-save-alist nil nil nil dir))))
1232 (provide 'gnus-agent)
1234 ;;; gnus-agent.el ends here