1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
34 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
35 "Where the Gnus agent will store its files."
39 (defcustom gnus-agent-plugged-hook nil
40 "Hook run when plugging into the network."
44 (defcustom gnus-agent-unplugged-hook nil
45 "Hook run when unplugging from the network."
49 ;;; Internal variables
51 (defvar gnus-agent-history-buffers nil)
52 (defvar gnus-agent-buffer-alist nil)
53 (defvar gnus-agent-article-alist nil)
54 (defvar gnus-agent-group-alist nil)
55 (defvar gnus-agent-covered-methods nil)
56 (defvar gnus-category-alist nil)
57 (defvar gnus-agent-current-history nil)
58 (defvar gnus-agent-overview-buffer nil)
59 (defvar gnus-category-predicate-cache nil)
60 (defvar gnus-category-group-cache nil)
61 (defvar gnus-agent-spam-hashtb nil)
62 (defvar gnus-agent-file-name nil)
63 (defvar gnus-agent-send-mail-function nil)
64 (defvar gnus-agent-article-file-coding-system 'no-conversion)
74 (defun gnus-open-agent ()
76 (gnus-agent-read-servers)
78 (setq gnus-agent-overview-buffer
79 (get-buffer-create " *Gnus agent overview*"))
80 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
81 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
82 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
84 (gnus-add-shutdown 'gnus-close-agent 'gnus)
86 (defun gnus-close-agent ()
87 (setq gnus-agent-covered-methods nil
88 gnus-category-predicate-cache nil
89 gnus-category-group-cache nil
90 gnus-agent-spam-hashtb nil)
91 (gnus-kill-buffer gnus-agent-overview-buffer))
97 (defun gnus-agent-read-file (file)
98 "Load FILE and do a `read' there."
99 (nnheader-temp-write nil
101 (insert-file-contents file)
102 (goto-char (point-min))
103 (read (current-buffer)))))
105 (defsubst gnus-agent-method ()
106 (concat (symbol-name (car gnus-command-method)) "/"
107 (if (equal (cadr gnus-command-method) "")
109 (cadr gnus-command-method))))
111 (defsubst gnus-agent-directory ()
112 "Path of the Gnus agent directory."
113 (nnheader-concat gnus-agent-directory (gnus-agent-method) "/"))
115 (defun gnus-agent-lib-file (file)
116 "The full path of the Gnus agent library FILE."
117 (concat (gnus-agent-directory) "agent.lib/" file))
119 ;;; Fetching setup functions.
121 (defun gnus-agent-start-fetch ()
122 "Initialize data structures for efficient fetching."
123 (gnus-agent-open-history)
124 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
126 (defun gnus-agent-stop-fetch ()
127 "Save all data structures and clean up."
128 (gnus-agent-save-history)
129 (gnus-agent-close-history)
130 (setq gnus-agent-spam-hashtb nil)
132 (set-buffer nntp-server-buffer)
135 (defmacro gnus-agent-with-fetch (&rest forms)
139 (gnus-agent-start-fetch)
141 (gnus-agent-stop-fetch)))
143 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
144 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
150 (defvar gnus-agent-mode-hook nil
151 "Hook run when installing agent mode.")
153 (defvar gnus-agent-mode nil)
154 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
156 (defun gnus-agent-mode ()
157 "Minor mode for providing a agent support in Gnus buffers."
158 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
159 (symbol-name major-mode))
160 (match-string 1 (symbol-name major-mode))))
161 (mode (intern (format "gnus-agent-%s-mode" buffer))))
162 (set (make-local-variable 'gnus-agent-mode) t)
164 (set (make-local-variable mode) t)
166 (when (gnus-visual-p 'agent-menu 'menu)
167 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
168 (unless (assq 'gnus-agent-mode minor-mode-alist)
169 (push gnus-agent-mode-status minor-mode-alist))
170 (unless (assq mode minor-mode-map-alist)
171 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
173 minor-mode-map-alist))
174 (gnus-agent-toggle-plugged gnus-plugged)
175 (run-hooks 'gnus-agent-mode-hook)))
177 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
178 (gnus-define-keys gnus-agent-group-mode-map
179 "Ju" gnus-agent-fetch-group
180 "Jc" gnus-enter-category-buffer
181 "Jj" gnus-agent-toggle-plugged
182 "Js" gnus-agent-fetch-session
183 "JS" gnus-group-send-drafts
184 "Ja" gnus-agent-add-group)
186 (defun gnus-agent-group-make-menu-bar ()
187 (unless (boundp 'gnus-agent-group-menu)
189 gnus-agent-group-menu gnus-agent-group-mode-map ""
191 ["Toggle plugged" gnus-agent-toggle-plugged t]
192 ["List categories" gnus-enter-category-buffer t]
193 ["Send drafts" gnus-group-send-drafts gnus-plugged]
195 ["All" gnus-agent-fetch-session gnus-plugged]
196 ["Group" gnus-agent-fetch-group gnus-plugged])))))
198 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
199 (gnus-define-keys gnus-agent-summary-mode-map
200 "Jj" gnus-agent-toggle-plugged
201 "J#" gnus-agent-mark-article
202 "J\M-#" gnus-agent-unmark-article
203 "@" gnus-agent-toggle-mark
204 "Jc" gnus-agent-catchup)
206 (defun gnus-agent-summary-make-menu-bar ()
207 (unless (boundp 'gnus-agent-summary-menu)
209 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
211 ["Toggle plugged" gnus-agent-toggle-plugged t]
212 ["Mark as downloadable" gnus-agent-mark-article t]
213 ["Unmark as downloadable" gnus-agent-unmark-article t]
214 ["Toggle mark" gnus-agent-toggle-mark t]
215 ["Catchup undownloaded" gnus-agent-catchup t]))))
217 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
218 (gnus-define-keys gnus-agent-server-mode-map
219 "Jj" gnus-agent-toggle-plugged
220 "Ja" gnus-agent-add-server
221 "Jr" gnus-agent-remove-server)
223 (defun gnus-agent-server-make-menu-bar ()
224 (unless (boundp 'gnus-agent-server-menu)
226 gnus-agent-server-menu gnus-agent-server-mode-map ""
228 ["Toggle plugged" gnus-agent-toggle-plugged t]
229 ["Add" gnus-agent-add-server t]
230 ["Remove" gnus-agent-remove-server t]))))
232 (defun gnus-agent-toggle-plugged (plugged)
233 "Toggle whether Gnus is unplugged or not."
234 (interactive (list (not gnus-plugged)))
237 (run-hooks 'gnus-agent-plugged-hook)
238 (setcar (cdr gnus-agent-mode-status) " Plugged"))
239 (gnus-agent-close-connections)
240 (run-hooks 'gnus-agent-unplugged-hook)
241 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
242 (setq gnus-plugged plugged)
243 (set-buffer-modified-p t))
245 (defun gnus-agent-close-connections ()
246 "Close all methods covered by the Gnus agent."
247 (let ((methods gnus-agent-covered-methods))
249 (gnus-close-server (pop methods)))))
252 (defun gnus-unplugged ()
253 "Start Gnus unplugged."
255 (setq gnus-plugged nil)
259 (defun gnus-agentize ()
260 "Allow Gnus to be an offline newsreader.
261 The normal usage of this command is to put the following as the
262 last form in your `.gnus.el' file:
266 This will modify the `gnus-before-startup-hook', `gnus-post-method',
267 and `message-send-mail-function' variables, and install the Gnus
268 agent minor mode in all Gnus buffers."
271 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
272 (unless gnus-agent-send-mail-function
273 (setq gnus-agent-send-mail-function message-send-mail-function
274 message-send-mail-function 'gnus-agent-send-mail))
275 (unless gnus-agent-covered-methods
276 (setq gnus-agent-covered-methods (list gnus-select-method))))
278 (defun gnus-agent-queue-setup ()
279 "Make sure the queue group exists."
280 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
281 (gnus-request-create-group "queue" '(nndraft ""))
282 (let ((gnus-level-default-subscribed 1))
283 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
284 (gnus-group-set-parameter
285 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
287 (defun gnus-agent-send-mail ()
289 (funcall gnus-agent-send-mail-function)
290 (goto-char (point-min))
292 (concat "^" (regexp-quote mail-header-separator) "\n"))
294 (gnus-request-accept-article "nndraft:queue")))
297 ;;; Group mode commands
300 (defun gnus-agent-fetch-group (group)
301 "Put all new articles in GROUP into the agent."
302 (interactive (list (gnus-group-group-name)))
304 (error "No group on the current line"))
305 (let ((gnus-command-method (gnus-find-method-for-group group)))
306 (gnus-agent-with-fetch
307 (gnus-agent-fetch-group-1 group gnus-command-method)
308 (gnus-message 5 "Fetching %s...done" group))))
310 (defun gnus-agent-add-group (category arg)
311 "Add the current group to an agent category."
317 (mapcar (lambda (cat) (list (symbol-name (car cat))))
321 (let ((cat (assq category gnus-category-alist))
323 (gnus-group-iterate arg
325 (when (cadddr (setq c (gnus-group-category group)))
326 (setf (cadddr c) (delete group (cadddr c))))
327 (push group groups)))
328 (setf (cadddr cat) (nconc (cadddr cat) groups))
329 (gnus-category-write)))
332 ;;; Server mode commands
335 (defun gnus-agent-add-server (server)
336 "Enroll SERVER in the agent program."
337 (interactive (list (gnus-server-server-name)))
339 (error "No server on the current line"))
340 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
341 (when (member method gnus-agent-covered-methods)
342 (error "Server already in the agent program"))
343 (push method gnus-agent-covered-methods)
344 (gnus-agent-write-servers)
345 (message "Entered %s into the agent" server)))
347 (defun gnus-agent-remove-server (server)
348 "Remove SERVER from the agent program."
349 (interactive (list (gnus-server-server-name)))
351 (error "No server on the current line"))
352 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
353 (unless (member method gnus-agent-covered-methods)
354 (error "Server not in the agent program"))
355 (setq gnus-agent-covered-methods
356 (delete method gnus-agent-covered-methods))
357 (gnus-agent-write-servers)
358 (message "Removed %s from the agent" server)))
360 (defun gnus-agent-read-servers ()
361 "Read the alist of covered servers."
362 (setq gnus-agent-covered-methods
363 (gnus-agent-read-file
364 (nnheader-concat gnus-agent-directory "lib/servers"))))
366 (defun gnus-agent-write-servers ()
367 "Write the alist of covered servers."
368 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
369 (prin1 gnus-agent-covered-methods (current-buffer))))
375 (defun gnus-agent-mark-article (n &optional unmark)
376 "Mark the next N articles as downloadable.
377 If N is negative, mark backward instead. If UNMARK is non-nil, remove
378 the mark instead. The difference between N and the actual number of
379 articles marked is returned."
381 (let ((backward (< n 0))
386 (gnus-summary-set-agent-mark
387 (gnus-summary-article-number) unmark)
388 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
391 (gnus-message 7 "No more articles"))
392 (gnus-summary-recenter)
393 (gnus-summary-position-point)
396 (defun gnus-agent-unmark-article (n)
397 "Remove the downloadable mark from the next N articles.
398 If N is negative, unmark backward instead. The difference between N and
399 the actual number of articles unmarked is returned."
401 (gnus-agent-mark-article n t))
403 (defun gnus-agent-toggle-mark (n)
404 "Toggle the downloadable mark from the next N articles.
405 If N is negative, toggle backward instead. The difference between N and
406 the actual number of articles toggled is returned."
408 (gnus-agent-mark-article n 'toggle))
410 (defun gnus-summary-set-agent-mark (article &optional unmark)
411 "Mark ARTICLE as downloadable."
412 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
413 (memq article gnus-newsgroup-downloadable)
415 (setq gnus-newsgroup-downloadable
416 (delq article gnus-newsgroup-downloadable))
418 (push article gnus-newsgroup-downloadable))
419 (gnus-summary-update-mark
420 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
423 (defun gnus-agent-get-undownloaded-list ()
424 "Mark all unfetched articles as read."
425 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
426 (when (and (not gnus-plugged)
427 (gnus-agent-method-p gnus-command-method))
428 (gnus-agent-load-alist gnus-newsgroup-name)
429 (let ((articles gnus-newsgroup-unreads)
431 (while (setq article (pop articles))
432 (unless (or (cdr (assq article gnus-agent-article-alist))
433 (memq article gnus-newsgroup-downloadable))
434 (push article gnus-newsgroup-undownloaded)))))))
436 (defun gnus-agent-catchup ()
437 "Mark all undownloaded articles as read."
440 (while gnus-newsgroup-undownloaded
441 (gnus-summary-mark-article
442 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
443 (gnus-summary-position-point))
446 ;;; Internal functions
449 (defun gnus-agent-save-active (method)
450 (when (gnus-agent-method-p method)
451 (let* ((gnus-command-method method)
452 (file (gnus-agent-lib-file "active")))
453 (gnus-make-directory (file-name-directory file))
454 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
455 (write-region (point-min) (point-max) file nil 'silent))
456 (when (file-exists-p (gnus-agent-lib-file "groups"))
457 (delete-file (gnus-agent-lib-file "groups"))))))
459 (defun gnus-agent-save-groups (method)
460 (let* ((gnus-command-method method)
461 (file (gnus-agent-lib-file "groups")))
462 (gnus-make-directory (file-name-directory file))
463 (write-region (point-min) (point-max) file nil 'silent))
464 (when (file-exists-p (gnus-agent-lib-file "active"))
465 (delete-file (gnus-agent-lib-file "active"))))
467 (defun gnus-agent-group-path (group)
468 "Translate GROUP into a path."
469 (nnheader-translate-file-chars
470 (nnheader-replace-chars-in-string group ?. ?/)))
474 (defun gnus-agent-method-p (method)
475 "Say whether METHOD is covered by the agent."
476 (member method gnus-agent-covered-methods))
478 (defun gnus-agent-get-function (method)
479 (if (and (not gnus-plugged)
480 (gnus-agent-method-p method))
486 ;;; History functions
488 (defun gnus-agent-history-buffer ()
489 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
491 (defun gnus-agent-open-history ()
493 (push (cons (gnus-agent-method)
494 (set-buffer (get-buffer-create
495 (format " *Gnus agent %s history*"
496 (gnus-agent-method)))))
497 gnus-agent-history-buffers)
500 (let ((file (gnus-agent-lib-file "history")))
501 (when (file-exists-p file)
503 (set (make-local-variable 'gnus-agent-file-name) file))))
505 (defun gnus-agent-save-history ()
507 (set-buffer gnus-agent-current-history)
508 (gnus-make-directory (file-name-directory gnus-agent-file-name))
509 (write-region (1+ (point-min)) (point-max)
510 gnus-agent-file-name nil 'silent)))
512 (defun gnus-agent-close-history ()
513 (when (gnus-buffer-live-p gnus-agent-current-history)
514 (kill-buffer gnus-agent-current-history)
515 (setq gnus-agent-history-buffers
516 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
517 gnus-agent-history-buffers))))
519 (defun gnus-agent-enter-history (id group-arts date)
521 (set-buffer gnus-agent-current-history)
522 (goto-char (point-max))
523 (insert id "\t" (number-to-string date) "\t")
525 (insert (caar group-arts) "/" (number-to-string (cdr (pop group-arts)))
529 (defun gnus-agent-article-in-history-p (id)
531 (set-buffer (gnus-agent-history-buffer))
532 (goto-char (point-min))
533 (search-forward (concat "\n" id "\t") nil t)))
535 (defun gnus-agent-history-path (id)
537 (set-buffer (gnus-agent-history-buffer))
538 (goto-char (point-min))
539 (when (search-forward (concat "\n" id "\t") nil t)
540 (let ((method (gnus-agent-method)))
542 (while (not (numberp (setq group (read (current-buffer)))))
543 (push (concat method "/" group) paths))
544 (nreverse paths))))))
550 (defun gnus-agent-fetch-articles (group articles)
551 "Fetch ARTICLES from GROUP and put them into the agent."
553 ;; Prune off articles that we have already fetched.
555 (cdr (assq (car articles) gnus-agent-article-alist)))
557 (let ((arts articles))
559 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
560 (setcdr arts (cddr arts))
561 (setq arts (cdr arts)))))
564 (gnus-agent-directory)
565 (gnus-agent-group-path group) "/"))
566 (date (gnus-time-to-day (current-time)))
568 pos alists crosses id elem)
569 (gnus-make-directory dir)
570 (gnus-message 7 "Fetching articles for %s..." group)
571 ;; Fetch the articles from the backend.
572 (if (gnus-check-backend-function 'retrieve-articles group)
573 (setq pos (gnus-retrieve-articles articles group))
574 (nnheader-temp-write nil
575 (let ((buf (current-buffer))
577 (while (setq article (pop articles))
578 (when (gnus-request-article article group)
579 (goto-char (point-max))
580 (push (cons article (point)) pos)
581 (insert-buffer-substring nntp-server-buffer)))
582 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
583 (setq pos (nreverse pos)))))
584 ;; Then save these articles into the agent.
586 (set-buffer nntp-server-buffer)
588 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
589 (goto-char (point-min))
590 (when (search-forward "\n\n" nil t)
591 (when (search-backward "\nXrefs: " nil t)
592 ;; Handle crossposting.
593 (skip-chars-forward "^ ")
594 (skip-chars-forward " ")
596 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
597 (push (cons (buffer-substring (match-beginning 1)
599 (buffer-substring (match-beginning 2)
602 (goto-char (match-end 0)))
603 (gnus-agent-crosspost crosses (caar pos))))
604 (goto-char (point-min))
605 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
606 (setq id "No-Message-ID-in-article")
607 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
608 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
609 (write-region (point-min) (point-max)
610 (concat dir (number-to-string (caar pos)))
612 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
614 (gnus-agent-enter-history
615 id (or crosses (list (cons group (caar pos)))) date)
618 (gnus-agent-save-alist group)))))
620 (defun gnus-agent-crosspost (crosses article)
621 (let (gnus-agent-article-alist group alist beg end)
623 (set-buffer gnus-agent-overview-buffer)
624 (when (nnheader-find-nov-line article)
627 (setq end (progn (forward-line 1) (point)))))
629 (setq group (caar crosses))
630 (unless (setq alist (assoc group gnus-agent-group-alist))
631 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
632 gnus-agent-group-alist))
633 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
635 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
637 (when (= (point-max) (point-min))
638 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
640 (insert-file-contents
641 (gnus-agent-article-name ".overview" group))))
642 (nnheader-find-nov-line (string-to-number (cdar crosses)))
643 (insert (string-to-number (cdar crosses)))
644 (insert-buffer-substring gnus-agent-overview-buffer beg end))
647 (defun gnus-agent-flush-cache ()
649 (while gnus-agent-buffer-alist
650 (set-buffer (cdar gnus-agent-buffer-alist))
651 (write-region (point-min) (point-max)
652 (gnus-agent-article-name ".overview"
653 (caar gnus-agent-buffer-alist))
655 (pop gnus-agent-buffer-alist))
656 (while gnus-agent-group-alist
657 (nnheader-temp-write (caar gnus-agent-group-alist)
658 (princ (cdar gnus-agent-group-alist))
660 (pop gnus-agent-group-alist))))
662 (defun gnus-agent-fetch-headers (group articles &optional force)
663 (gnus-agent-load-alist group)
664 ;; Find out what headers we need to retrieve.
667 (assq (car articles) gnus-agent-article-alist))
669 (let ((arts articles))
671 (if (assq (cadr arts) gnus-agent-article-alist)
672 (setcdr arts (cddr arts))
673 (setq arts (cdr arts)))))
676 (gnus-message 7 "Fetching headers for %s..." group)
678 (set-buffer nntp-server-buffer)
679 (unless (eq 'nov (gnus-retrieve-headers articles group))
680 (nnvirtual-convert-headers))
681 ;; Save these headers for later processing.
682 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
685 (setq file (gnus-agent-article-name ".overview" group)))
686 (gnus-agent-braid-nov group articles file))
687 (gnus-make-directory (nnheader-translate-file-chars
688 (file-name-directory file)))
689 (write-region (point-min) (point-max) file nil 'silent)
690 (gnus-agent-save-alist group articles nil))
693 (defsubst gnus-agent-copy-nov-line (article)
695 (set-buffer gnus-agent-overview-buffer)
697 (if (eq article (read (current-buffer)))
698 (setq e (progn (forward-line 1) (point)))
700 (set-buffer nntp-server-buffer)
701 (insert-buffer-substring gnus-agent-overview-buffer b e)))
703 (defun gnus-agent-braid-nov (group articles file)
705 (set-buffer gnus-agent-overview-buffer)
706 (goto-char (point-min))
707 (set-buffer nntp-server-buffer)
709 (insert-file-contents file)
710 (goto-char (point-min))
711 (if (or (= (point-min) (point-max))
714 (< (read (current-buffer)) (car articles))))
715 ;; We have only headers that are after the older headers,
716 ;; so we just append them.
718 (goto-char (point-max))
719 (insert-buffer-substring gnus-agent-overview-buffer))
720 ;; We do it the hard way.
721 (nnheader-find-nov-line (car articles))
722 (gnus-agent-copy-nov-line (car articles))
726 (while (and (not (eobp))
727 (< (read (current-buffer)) (car articles)))
731 (gnus-agent-copy-nov-line (car articles))
732 (setq articles (cdr articles))))
735 (set-buffer gnus-agent-overview-buffer)
738 (set-buffer nntp-server-buffer)
739 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
741 (defun gnus-agent-load-alist (group &optional dir)
742 "Load the article-state alist for GROUP."
743 (setq gnus-agent-article-alist
744 (gnus-agent-read-file
746 (concat dir ".agentview")
747 (gnus-agent-article-name ".agentview" group)))))
749 (defun gnus-agent-save-alist (group &optional articles state dir)
750 "Load the article-state alist for GROUP."
751 (nnheader-temp-write (if dir
752 (concat dir ".agentview")
753 (gnus-agent-article-name ".agentview" group))
754 (princ (setq gnus-agent-article-alist
755 (nconc gnus-agent-article-alist
756 (mapcar (lambda (article) (cons article state))
761 (defun gnus-agent-article-name (article group)
762 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
763 (if (stringp article) article (string-to-number article))))
766 (defun gnus-agent-batch-fetch ()
767 "Start Gnus and fetch session."
770 (gnus-agent-fetch-session)
773 (defun gnus-agent-fetch-session ()
774 "Fetch all articles and headers that are eligible for fetching."
776 (unless gnus-agent-covered-methods
777 (error "No servers are covered by the Gnus agent"))
779 (error "Can't fetch articles while Gnus is unplugged"))
780 (let ((methods gnus-agent-covered-methods)
781 groups group gnus-command-method)
784 (setq gnus-command-method (car methods)
785 groups (gnus-groups-from-server (pop methods)))
786 (gnus-agent-with-fetch
787 (while (setq group (pop groups))
788 (gnus-agent-fetch-group-1 group gnus-command-method))))
789 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
791 (defun gnus-agent-fetch-group-1 (group method)
793 (let ((gnus-command-method method)
794 gnus-newsgroup-dependencies gnus-newsgroup-headers
795 gnus-newsgroup-scored gnus-headers gnus-score
796 gnus-use-cache articles score arts
797 category predicate info marks score-param)
799 (when (and (setq articles (gnus-list-of-unread-articles group))
800 (gnus-agent-fetch-headers group articles))
801 ;; Parse them and see which articles we want to fetch.
802 (setq gnus-newsgroup-dependencies
803 (make-vector (length articles) 0))
804 (setq gnus-newsgroup-headers
805 (gnus-get-newsgroup-headers-xover articles nil nil group))
806 (setq category (gnus-group-category group))
809 (or (gnus-group-get-parameter group 'agent-predicate)
812 (or (gnus-group-get-parameter group 'agent-score)
815 (gnus-score-headers (list (list score-param))))
817 (while (setq gnus-headers (pop gnus-newsgroup-headers))
819 (or (cdr (assq (mail-header-number gnus-headers)
820 gnus-newsgroup-scored))
821 gnus-summary-default-score))
822 (when (funcall predicate)
823 (push (mail-header-number gnus-headers)
825 ;; Fetch the articles.
827 (gnus-agent-fetch-articles group arts)))
828 ;; Perhaps we have some additional articles to fetch.
829 (setq arts (assq 'download (gnus-info-marks
830 (setq info (gnus-get-info group)))))
832 (gnus-agent-fetch-articles
833 group (gnus-uncompress-range (cdr arts)))
834 (setq marks (delq arts (gnus-info-marks info)))
835 (gnus-info-set-marks info marks))))
838 ;;; Agent Category Mode
841 (defvar gnus-category-mode-hook nil
842 "Hook run in `gnus-category-mode' buffers.")
844 (defvar gnus-category-line-format " %(%20c%): %g\n"
845 "Format of category lines.")
847 (defvar gnus-category-mode-line-format "Gnus: %%b"
848 "The format specification for the category mode line.")
850 (defvar gnus-agent-short-article 100
851 "Articles that have fewer lines than this are short.")
853 (defvar gnus-agent-long-article 200
854 "Articles that have more lines than this are long.")
856 (defvar gnus-agent-low-score 0
857 "Articles that have a score lower than this have a low score.")
859 (defvar gnus-agent-high-score 0
860 "Articles that have a score higher than this have a high score.")
863 ;;; Internal variables.
865 (defvar gnus-category-buffer "*Agent Category*")
867 (defvar gnus-category-line-format-alist
871 (defvar gnus-category-mode-line-format-alist
872 `((?u user-defined ?s)))
874 (defvar gnus-category-line-format-spec nil)
875 (defvar gnus-category-mode-line-format-spec nil)
877 (defvar gnus-category-mode-map nil)
878 (put 'gnus-category-mode 'mode-class 'special)
880 (unless gnus-category-mode-map
881 (setq gnus-category-mode-map (make-sparse-keymap))
882 (suppress-keymap gnus-category-mode-map)
884 (gnus-define-keys gnus-category-mode-map
885 "q" gnus-category-exit
886 "k" gnus-category-kill
887 "c" gnus-category-copy
888 "a" gnus-category-add
889 "p" gnus-category-edit-predicate
890 "g" gnus-category-edit-groups
891 "s" gnus-category-edit-score
892 "l" gnus-category-list
894 "\C-c\C-i" gnus-info-find-node
895 "\C-c\C-b" gnus-bug))
897 (defvar gnus-category-menu-hook nil
898 "*Hook run after the creation of the menu.")
900 (defun gnus-category-make-menu-bar ()
901 (gnus-turn-off-edit-menu 'category)
902 (unless (boundp 'gnus-category-menu)
904 gnus-category-menu gnus-category-mode-map ""
906 ["Add" gnus-category-add t]
907 ["Kill" gnus-category-kill t]
908 ["Copy" gnus-category-copy t]
909 ["Edit predicate" gnus-category-edit-predicate t]
910 ["Edit score" gnus-category-edit-score t]
911 ["Edit groups" gnus-category-edit-groups t]
912 ["Exit" gnus-category-exit t]))
914 (run-hooks 'gnus-category-menu-hook)))
916 (defun gnus-category-mode ()
917 "Major mode for listing and editing agent categories.
919 All normal editing commands are switched off.
920 \\<gnus-category-mode-map>
921 For more in-depth information on this mode, read the manual
922 (`\\[gnus-info-find-node]').
924 The following commands are available:
926 \\{gnus-category-mode-map}"
928 (when (gnus-visual-p 'category-menu 'menu)
929 (gnus-category-make-menu-bar))
930 (kill-all-local-variables)
931 (gnus-simplify-mode-line)
932 (setq major-mode 'gnus-category-mode)
933 (setq mode-name "Category")
934 (gnus-set-default-directory)
935 (setq mode-line-process nil)
936 (use-local-map gnus-category-mode-map)
937 (buffer-disable-undo (current-buffer))
938 (setq truncate-lines t)
939 (setq buffer-read-only t)
940 (run-hooks 'gnus-category-mode-hook))
942 (defalias 'gnus-category-position-point 'gnus-goto-colon)
944 (defun gnus-category-insert-line (category)
945 (let* ((name (car category))
946 (groups (length (cadddr category))))
948 (gnus-add-text-properties
952 (eval gnus-category-line-format-spec))
953 (list 'gnus-category name))))
955 (defun gnus-enter-category-buffer ()
956 "Go to the Category buffer."
958 (gnus-category-setup-buffer)
959 (gnus-configure-windows 'category)
960 (gnus-category-prepare))
962 (defun gnus-category-setup-buffer ()
963 (unless (get-buffer gnus-category-buffer)
965 (set-buffer (get-buffer-create gnus-category-buffer))
966 (gnus-add-current-to-buffer-list)
967 (gnus-category-mode))))
969 (defun gnus-category-prepare ()
970 (gnus-set-format 'category-mode)
971 (gnus-set-format 'category t)
972 (let ((alist gnus-category-alist)
973 (buffer-read-only nil))
976 (gnus-category-insert-line (pop alist)))
977 (goto-char (point-min))
978 (gnus-category-position-point)))
980 (defun gnus-category-name ()
981 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
982 (error "No category on the current line")))
984 (defun gnus-category-read ()
985 "Read the category alist."
986 (setq gnus-category-alist
987 (or (gnus-agent-read-file
988 (nnheader-concat gnus-agent-directory "lib/categories"))
989 (list (list 'default 'true nil nil)))))
991 (defun gnus-category-write ()
992 "Write the category alist."
993 (setq gnus-category-predicate-cache nil
994 gnus-category-group-cache nil)
995 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
996 (prin1 gnus-category-alist (current-buffer))))
998 (defun gnus-category-edit-predicate (category)
999 "Edit the predicate for CATEGORY."
1000 (interactive (list (gnus-category-name)))
1001 (let ((info (assq category gnus-category-alist)))
1003 (cadr info) (format "Editing the predicate for category %s" category)
1004 `(lambda (predicate)
1005 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1006 (gnus-category-write)
1007 (gnus-category-list)))))
1009 (defun gnus-category-edit-score (category)
1010 "Edit the score expression for CATEGORY."
1011 (interactive (list (gnus-category-name)))
1012 (let ((info (assq category gnus-category-alist)))
1015 (format "Editing the score expression for category %s" category)
1017 (setf (caddr (assq ',category gnus-category-alist)) groups)
1018 (gnus-category-write)
1019 (gnus-category-list)))))
1021 (defun gnus-category-edit-groups (category)
1022 "Edit the group list for CATEGORY."
1023 (interactive (list (gnus-category-name)))
1024 (let ((info (assq category gnus-category-alist)))
1026 (cadddr info) (format "Editing the group list for category %s" category)
1028 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1029 (gnus-category-write)
1030 (gnus-category-list)))))
1032 (defun gnus-category-kill (category)
1033 "Kill the current category."
1034 (interactive (list (gnus-category-name)))
1035 (let ((info (assq category gnus-category-alist))
1036 (buffer-read-only nil))
1038 (gnus-category-write)
1039 (setq gnus-category-alist (delq info gnus-category-alist))))
1041 (defun gnus-category-copy (category to)
1042 "Copy the current category."
1043 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1044 (let ((info (assq category gnus-category-alist)))
1045 (push (list to (gnus-copy-sequence (cadr info))
1046 (gnus-copy-sequence (caddr info)) nil)
1047 gnus-category-alist)
1048 (gnus-category-write)
1049 (gnus-category-list)))
1051 (defun gnus-category-add (category)
1052 "Create a new category."
1053 (interactive "SCategory name: ")
1054 (when (assq category gnus-category-alist)
1055 (error "Category %s already exists" category))
1056 (push (list category 'true nil nil)
1057 gnus-category-alist)
1058 (gnus-category-write)
1059 (gnus-category-list))
1061 (defun gnus-category-list ()
1062 "List all categories."
1064 (gnus-category-prepare))
1066 (defun gnus-category-exit ()
1067 "Return to the group buffer."
1069 (kill-buffer (current-buffer))
1070 (gnus-configure-windows 'group t))
1072 ;; To avoid having 8-bit characters in the source file.
1073 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1075 (defvar gnus-category-predicate-alist
1076 '((spam . gnus-agent-spam-p)
1077 (short . gnus-agent-short-p)
1078 (long . gnus-agent-long-p)
1079 (low . gnus-agent-low-scored-p)
1080 (high . gnus-agent-high-scored-p)
1081 (true . gnus-agent-true)
1082 (false . gnus-agent-false))
1083 "Mapping from short score predicate symbols to predicate functions.")
1085 (defun gnus-agent-spam-p ()
1086 "Say whether an article is spam or not."
1087 (unless gnus-agent-spam-hashtb
1088 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1089 (if (not (equal (mail-header-references gnus-headers) ""))
1091 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1093 (gnus-gethash string gnus-agent-spam-hashtb)
1094 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1096 (defun gnus-agent-short-p ()
1097 "Say whether an article is short or not."
1098 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1100 (defun gnus-agent-long-p ()
1101 "Say whether an article is long or not."
1102 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1104 (defun gnus-agent-low-scored-p ()
1105 "Say whether an article has a low score or not."
1106 (< gnus-score gnus-agent-low-score))
1108 (defun gnus-agent-high-scored-p ()
1109 "Say whether an article has a high score or not."
1110 (> gnus-score gnus-agent-low-score))
1112 (defun gnus-category-make-function (cat)
1113 "Make a function from category CAT."
1114 `(lambda () ,(gnus-category-make-function-1 cat)))
1116 (defun gnus-agent-true ()
1120 (defun gnus-agent-false ()
1124 (defun gnus-category-make-function-1 (cat)
1125 "Make a function from category CAT."
1127 ;; Functions are just returned as is.
1129 (gnus-functionp cat))
1130 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1132 ;; More complex category.
1135 ((memq (car cat) '(& and))
1137 ((memq (car cat) '(| or))
1139 ((memq (car cat) gnus-category-not)
1141 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1143 (error "Unknown category type: %s" cat))))
1145 (defun gnus-get-predicate (predicate)
1146 "Return the predicate for CATEGORY."
1147 (or (cdr (assoc predicate gnus-category-predicate-cache))
1148 (cdar (push (cons predicate
1149 (gnus-category-make-function predicate))
1150 gnus-category-predicate-cache))))
1152 (defun gnus-group-category (group)
1153 "Return the category GROUP belongs to."
1154 (unless gnus-category-group-cache
1155 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1156 (let ((cs gnus-category-alist)
1158 (while (setq cat (pop cs))
1159 (setq groups (cadddr cat))
1161 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1162 (or (gnus-gethash group gnus-category-group-cache)
1163 (assq 'default gnus-category-alist)))
1165 (defun gnus-agent-expire ()
1166 "Expire all old articles."
1168 (let ((methods gnus-agent-covered-methods)
1169 (alist (cdr gnus-newsrc-alist))
1170 gnus-command-method ofiles info method file group)
1171 (while (setq gnus-command-method (pop methods))
1172 (setq ofiles (nconc ofiles (gnus-agent-expire-directory
1173 (gnus-agent-directory)))))
1174 (while (setq info (pop alist))
1175 (when (and (gnus-agent-method-p
1176 (setq gnus-command-method
1177 (gnus-find-method-for-group
1178 (setq group (gnus-info-group info)))))
1182 (gnus-agent-directory)
1183 (gnus-agent-group-path group) "/.overview"))
1185 (setq ofiles (delete file ofiles))
1186 (gnus-agent-expire-group file group)))
1188 (gnus-agent-expire-group (pop ofiles)))))
1190 (defun gnus-agent-expire-directory (dir)
1191 "Expire all groups in DIR recursively."
1192 (when (file-directory-p dir)
1193 (let ((files (directory-files dir t))
1195 (while (setq file (pop files))
1197 ((member (file-name-nondirectory file) '("." ".."))
1200 ((file-directory-p file)
1202 (setq ofiles (nconc ofiles (gnus-agent-expire-directory file))))
1203 ((string-match "\\.overview$" file)
1205 (push file ofiles))))
1208 (defun gnus-agent-expire-group (overview &optional group)
1209 "Expire articles in OVERVIEW."
1210 (gnus-message 5 "Expiring %s..." overview)
1211 (let ((odate (- (gnus-time-to-day (current-time)) 4))
1212 (dir (file-name-directory overview))
1213 (info (when group (gnus-get-info group)))
1214 headers article file point unreads)
1215 (gnus-agent-load-alist nil dir)
1219 (gnus-list-of-unread-articles group)
1220 (gnus-uncompress-range
1221 (cdr (assq 'tick (gnus-info-marks info))))
1222 (gnus-uncompress-range
1223 (cdr (assq 'dormant (gnus-info-marks info)))))))
1224 (nnheader-temp-write overview
1225 (insert-file-contents overview)
1226 (goto-char (point-min))
1228 (setq point (point))
1230 (setq headers (inline (nnheader-parse-nov)))
1234 (setq headers nil)))
1236 (unless (memq (setq article (mail-header-number headers)) unreads)
1239 (inline (nnmail-date-to-time
1240 (mail-header-date headers)))))
1244 (setq gnus-agent-article-alist
1245 (delq (assq article gnus-agent-article-alist)
1246 gnus-agent-article-alist))
1247 (when (file-exists-p
1248 (setq file (concat dir (number-to-string article))))
1249 (delete-file file))))))
1250 (gnus-agent-save-alist nil nil nil dir))))
1252 (provide 'gnus-agent)
1254 ;;; gnus-agent.el ends here