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 (defcustom gnus-agent-expire-days 7
53 "Read articles older than this will be expired."
57 ;;; Internal variables
59 (defvar gnus-agent-history-buffers nil)
60 (defvar gnus-agent-buffer-alist nil)
61 (defvar gnus-agent-article-alist nil)
62 (defvar gnus-agent-group-alist nil)
63 (defvar gnus-agent-covered-methods nil)
64 (defvar gnus-category-alist nil)
65 (defvar gnus-agent-current-history nil)
66 (defvar gnus-agent-overview-buffer nil)
67 (defvar gnus-category-predicate-cache nil)
68 (defvar gnus-category-group-cache nil)
69 (defvar gnus-agent-spam-hashtb nil)
70 (defvar gnus-agent-file-name nil)
71 (defvar gnus-agent-send-mail-function nil)
72 (defvar gnus-agent-article-file-coding-system 'no-conversion)
82 (defun gnus-open-agent ()
84 (gnus-agent-read-servers)
86 (setq gnus-agent-overview-buffer
87 (get-buffer-create " *Gnus agent overview*"))
88 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
89 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
90 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
92 (gnus-add-shutdown 'gnus-close-agent 'gnus)
94 (defun gnus-close-agent ()
95 (setq gnus-agent-covered-methods nil
96 gnus-category-predicate-cache nil
97 gnus-category-group-cache nil
98 gnus-agent-spam-hashtb nil)
99 (gnus-kill-buffer gnus-agent-overview-buffer))
102 ;;; Utility functions
105 (defun gnus-agent-read-file (file)
106 "Load FILE and do a `read' there."
107 (nnheader-temp-write nil
109 (insert-file-contents file)
110 (goto-char (point-min))
111 (read (current-buffer)))))
113 (defsubst gnus-agent-method ()
114 (concat (symbol-name (car gnus-command-method)) "/"
115 (if (equal (cadr gnus-command-method) "")
117 (cadr gnus-command-method))))
119 (defsubst gnus-agent-directory ()
120 "Path of the Gnus agent directory."
121 (nnheader-concat gnus-agent-directory
122 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
124 (defun gnus-agent-lib-file (file)
125 "The full path of the Gnus agent library FILE."
126 (concat (gnus-agent-directory) "agent.lib/" file))
128 ;;; Fetching setup functions.
130 (defun gnus-agent-start-fetch ()
131 "Initialize data structures for efficient fetching."
132 (gnus-agent-open-history)
133 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
135 (defun gnus-agent-stop-fetch ()
136 "Save all data structures and clean up."
137 (gnus-agent-save-history)
138 (gnus-agent-close-history)
139 (setq gnus-agent-spam-hashtb nil)
141 (set-buffer nntp-server-buffer)
144 (defmacro gnus-agent-with-fetch (&rest forms)
148 (gnus-agent-start-fetch)
150 (gnus-agent-stop-fetch)))
152 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
153 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
159 (defvar gnus-agent-mode-hook nil
160 "Hook run when installing agent mode.")
162 (defvar gnus-agent-mode nil)
163 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
165 (defun gnus-agent-mode ()
166 "Minor mode for providing a agent support in Gnus buffers."
167 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
168 (symbol-name major-mode))
169 (match-string 1 (symbol-name major-mode))))
170 (mode (intern (format "gnus-agent-%s-mode" buffer))))
171 (set (make-local-variable 'gnus-agent-mode) t)
173 (set (make-local-variable mode) t)
175 (when (gnus-visual-p 'agent-menu 'menu)
176 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
177 (unless (assq 'gnus-agent-mode minor-mode-alist)
178 (push gnus-agent-mode-status minor-mode-alist))
179 (unless (assq mode minor-mode-map-alist)
180 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
182 minor-mode-map-alist))
183 (gnus-agent-toggle-plugged gnus-plugged)
184 (gnus-run-hooks 'gnus-agent-mode-hook)))
186 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
187 (gnus-define-keys gnus-agent-group-mode-map
188 "Ju" gnus-agent-fetch-groups
189 "Jc" gnus-enter-category-buffer
190 "Jj" gnus-agent-toggle-plugged
191 "Js" gnus-agent-fetch-session
192 "JS" gnus-group-send-drafts
193 "Ja" gnus-agent-add-group)
195 (defun gnus-agent-group-make-menu-bar ()
196 (unless (boundp 'gnus-agent-group-menu)
198 gnus-agent-group-menu gnus-agent-group-mode-map ""
200 ["Toggle plugged" gnus-agent-toggle-plugged t]
201 ["List categories" gnus-enter-category-buffer t]
202 ["Send drafts" gnus-group-send-drafts gnus-plugged]
204 ["All" gnus-agent-fetch-session gnus-plugged]
205 ["Group" gnus-agent-fetch-group gnus-plugged])))))
207 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
208 (gnus-define-keys gnus-agent-summary-mode-map
209 "Jj" gnus-agent-toggle-plugged
210 "J#" gnus-agent-mark-article
211 "J\M-#" gnus-agent-unmark-article
212 "@" gnus-agent-toggle-mark
213 "Jc" gnus-agent-catchup)
215 (defun gnus-agent-summary-make-menu-bar ()
216 (unless (boundp 'gnus-agent-summary-menu)
218 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
220 ["Toggle plugged" gnus-agent-toggle-plugged t]
221 ["Mark as downloadable" gnus-agent-mark-article t]
222 ["Unmark as downloadable" gnus-agent-unmark-article t]
223 ["Toggle mark" gnus-agent-toggle-mark t]
224 ["Catchup undownloaded" gnus-agent-catchup t]))))
226 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
227 (gnus-define-keys gnus-agent-server-mode-map
228 "Jj" gnus-agent-toggle-plugged
229 "Ja" gnus-agent-add-server
230 "Jr" gnus-agent-remove-server)
232 (defun gnus-agent-server-make-menu-bar ()
233 (unless (boundp 'gnus-agent-server-menu)
235 gnus-agent-server-menu gnus-agent-server-mode-map ""
237 ["Toggle plugged" gnus-agent-toggle-plugged t]
238 ["Add" gnus-agent-add-server t]
239 ["Remove" gnus-agent-remove-server t]))))
241 (defun gnus-agent-toggle-plugged (plugged)
242 "Toggle whether Gnus is unplugged or not."
243 (interactive (list (not gnus-plugged)))
246 (setq gnus-plugged plugged)
247 (gnus-run-hooks 'gnus-agent-plugged-hook)
248 (setcar (cdr gnus-agent-mode-status) " Plugged"))
249 (gnus-agent-close-connections)
250 (setq gnus-plugged plugged)
251 (gnus-run-hooks 'gnus-agent-unplugged-hook)
252 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
253 (set-buffer-modified-p t))
255 (defun gnus-agent-close-connections ()
256 "Close all methods covered by the Gnus agent."
257 (let ((methods gnus-agent-covered-methods))
259 (gnus-close-server (pop methods)))))
262 (defun gnus-unplugged ()
263 "Start Gnus unplugged."
265 (setq gnus-plugged nil)
269 (defun gnus-plugged ()
270 "Start Gnus plugged."
272 (setq gnus-plugged t)
276 (defun gnus-agentize ()
277 "Allow Gnus to be an offline newsreader.
278 The normal usage of this command is to put the following as the
279 last form in your `.gnus.el' file:
283 This will modify the `gnus-before-startup-hook', `gnus-post-method',
284 and `message-send-mail-function' variables, and install the Gnus
285 agent minor mode in all Gnus buffers."
288 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
289 (unless gnus-agent-send-mail-function
290 (setq gnus-agent-send-mail-function message-send-mail-function
291 message-send-mail-function 'gnus-agent-send-mail))
292 (unless gnus-agent-covered-methods
293 (setq gnus-agent-covered-methods (list gnus-select-method))))
295 (defun gnus-agent-queue-setup ()
296 "Make sure the queue group exists."
297 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
298 (gnus-request-create-group "queue" '(nndraft ""))
299 (let ((gnus-level-default-subscribed 1))
300 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
301 (gnus-group-set-parameter
302 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
304 (defun gnus-agent-send-mail ()
306 (funcall gnus-agent-send-mail-function)
307 (goto-char (point-min))
309 (concat "^" (regexp-quote mail-header-separator) "\n"))
311 (gnus-request-accept-article "nndraft:queue")))
314 ;;; Group mode commands
317 (defun gnus-agent-fetch-groups (n)
318 "Put all new articles in the current groups into the agent."
320 (gnus-group-iterate n 'gnus-agent-fetch-group))
322 (defun gnus-agent-fetch-group (group)
323 "Put all new articles in GROUP into the agent."
324 (interactive (list (gnus-group-group-name)))
326 (error "No group on the current line"))
327 (let ((gnus-command-method (gnus-find-method-for-group group)))
328 (gnus-agent-with-fetch
329 (gnus-agent-fetch-group-1 group gnus-command-method)
330 (gnus-message 5 "Fetching %s...done" group))))
332 (defun gnus-agent-add-group (category arg)
333 "Add the current group to an agent category."
339 (mapcar (lambda (cat) (list (symbol-name (car cat))))
343 (let ((cat (assq category gnus-category-alist))
345 (gnus-group-iterate arg
347 (when (cadddr (setq c (gnus-group-category group)))
348 (setf (cadddr c) (delete group (cadddr c))))
349 (push group groups)))
350 (setf (cadddr cat) (nconc (cadddr cat) groups))
351 (gnus-category-write)))
354 ;;; Server mode commands
357 (defun gnus-agent-add-server (server)
358 "Enroll SERVER in 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 (when (member method gnus-agent-covered-methods)
364 (error "Server already in the agent program"))
365 (push method gnus-agent-covered-methods)
366 (gnus-agent-write-servers)
367 (message "Entered %s into the agent" server)))
369 (defun gnus-agent-remove-server (server)
370 "Remove SERVER from the agent program."
371 (interactive (list (gnus-server-server-name)))
373 (error "No server on the current line"))
374 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
375 (unless (member method gnus-agent-covered-methods)
376 (error "Server not in the agent program"))
377 (setq gnus-agent-covered-methods
378 (delete method gnus-agent-covered-methods))
379 (gnus-agent-write-servers)
380 (message "Removed %s from the agent" server)))
382 (defun gnus-agent-read-servers ()
383 "Read the alist of covered servers."
384 (setq gnus-agent-covered-methods
385 (gnus-agent-read-file
386 (nnheader-concat gnus-agent-directory "lib/servers"))))
388 (defun gnus-agent-write-servers ()
389 "Write the alist of covered servers."
390 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/servers")
391 (prin1 gnus-agent-covered-methods (current-buffer))))
397 (defun gnus-agent-mark-article (n &optional unmark)
398 "Mark the next N articles as downloadable.
399 If N is negative, mark backward instead. If UNMARK is non-nil, remove
400 the mark instead. The difference between N and the actual number of
401 articles marked is returned."
403 (let ((backward (< n 0))
408 (gnus-summary-set-agent-mark
409 (gnus-summary-article-number) unmark)
410 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
413 (gnus-message 7 "No more articles"))
414 (gnus-summary-recenter)
415 (gnus-summary-position-point)
418 (defun gnus-agent-unmark-article (n)
419 "Remove the downloadable mark from the next N articles.
420 If N is negative, unmark backward instead. The difference between N and
421 the actual number of articles unmarked is returned."
423 (gnus-agent-mark-article n t))
425 (defun gnus-agent-toggle-mark (n)
426 "Toggle the downloadable mark from the next N articles.
427 If N is negative, toggle backward instead. The difference between N and
428 the actual number of articles toggled is returned."
430 (gnus-agent-mark-article n 'toggle))
432 (defun gnus-summary-set-agent-mark (article &optional unmark)
433 "Mark ARTICLE as downloadable."
434 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
435 (memq article gnus-newsgroup-downloadable)
439 (setq gnus-newsgroup-downloadable
440 (delq article gnus-newsgroup-downloadable))
441 (push article gnus-newsgroup-undownloaded))
442 (setq gnus-newsgroup-undownloaded
443 (delq article gnus-newsgroup-undownloaded))
444 (push article gnus-newsgroup-downloadable))
445 (gnus-summary-update-mark
446 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
449 (defun gnus-agent-get-undownloaded-list ()
450 "Mark all unfetched articles as read."
451 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
452 (when (and (not gnus-plugged)
453 (gnus-agent-method-p gnus-command-method))
454 (gnus-agent-load-alist gnus-newsgroup-name)
455 (let ((articles gnus-newsgroup-unreads)
457 (while (setq article (pop articles))
458 (unless (or (cdr (assq article gnus-agent-article-alist))
459 (memq article gnus-newsgroup-downloadable))
460 (push article gnus-newsgroup-undownloaded)))))))
462 (defun gnus-agent-catchup ()
463 "Mark all undownloaded articles as read."
466 (while gnus-newsgroup-undownloaded
467 (gnus-summary-mark-article
468 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
469 (gnus-summary-position-point))
472 ;;; Internal functions
475 (defun gnus-agent-save-active (method)
476 (when (gnus-agent-method-p method)
477 (let* ((gnus-command-method method)
478 (file (gnus-agent-lib-file "active")))
479 (gnus-make-directory (file-name-directory file))
480 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
481 (write-region (point-min) (point-max) file nil 'silent))
482 (when (file-exists-p (gnus-agent-lib-file "groups"))
483 (delete-file (gnus-agent-lib-file "groups"))))))
485 (defun gnus-agent-save-groups (method)
486 (let* ((gnus-command-method method)
487 (file (gnus-agent-lib-file "groups")))
488 (gnus-make-directory (file-name-directory file))
489 (write-region (point-min) (point-max) file nil 'silent))
490 (when (file-exists-p (gnus-agent-lib-file "active"))
491 (delete-file (gnus-agent-lib-file "active"))))
493 (defun gnus-agent-group-path (group)
494 "Translate GROUP into a path."
495 (if nnmail-use-long-file-names
496 (gnus-group-real-name group)
497 (nnheader-replace-chars-in-string
498 (nnheader-translate-file-chars (gnus-group-real-name group))
503 (defun gnus-agent-method-p (method)
504 "Say whether METHOD is covered by the agent."
505 (member method gnus-agent-covered-methods))
507 (defun gnus-agent-get-function (method)
508 (if (and (not gnus-plugged)
509 (gnus-agent-method-p method))
515 ;;; History functions
517 (defun gnus-agent-history-buffer ()
518 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
520 (defun gnus-agent-open-history ()
522 (push (cons (gnus-agent-method)
523 (set-buffer (get-buffer-create
524 (format " *Gnus agent %s history*"
525 (gnus-agent-method)))))
526 gnus-agent-history-buffers)
529 (let ((file (gnus-agent-lib-file "history")))
530 (when (file-exists-p file)
532 (set (make-local-variable 'gnus-agent-file-name) file))))
534 (defun gnus-agent-save-history ()
536 (set-buffer gnus-agent-current-history)
537 (gnus-make-directory (file-name-directory gnus-agent-file-name))
538 (write-region (1+ (point-min)) (point-max)
539 gnus-agent-file-name nil 'silent)))
541 (defun gnus-agent-close-history ()
542 (when (gnus-buffer-live-p gnus-agent-current-history)
543 (kill-buffer gnus-agent-current-history)
544 (setq gnus-agent-history-buffers
545 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
546 gnus-agent-history-buffers))))
548 (defun gnus-agent-enter-history (id group-arts date)
550 (set-buffer gnus-agent-current-history)
551 (goto-char (point-max))
552 (insert id "\t" (number-to-string date) "\t")
554 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
558 (defun gnus-agent-article-in-history-p (id)
560 (set-buffer (gnus-agent-history-buffer))
561 (goto-char (point-min))
562 (search-forward (concat "\n" id "\t") nil t)))
564 (defun gnus-agent-history-path (id)
566 (set-buffer (gnus-agent-history-buffer))
567 (goto-char (point-min))
568 (when (search-forward (concat "\n" id "\t") nil t)
569 (let ((method (gnus-agent-method)))
571 (while (not (numberp (setq group (read (current-buffer)))))
572 (push (concat method "/" group) paths))
573 (nreverse paths))))))
579 (defun gnus-agent-fetch-articles (group articles)
580 "Fetch ARTICLES from GROUP and put them into the agent."
582 ;; Prune off articles that we have already fetched.
584 (cdr (assq (car articles) gnus-agent-article-alist)))
586 (let ((arts articles))
588 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
589 (setcdr arts (cddr arts))
590 (setq arts (cdr arts)))))
593 (gnus-agent-directory)
594 (gnus-agent-group-path group) "/"))
595 (date (gnus-time-to-day (current-time)))
597 pos alists crosses id elem)
598 (gnus-make-directory dir)
599 (gnus-message 7 "Fetching articles for %s..." group)
600 ;; Fetch the articles from the backend.
601 (if (gnus-check-backend-function 'retrieve-articles group)
602 (setq pos (gnus-retrieve-articles articles group))
603 (nnheader-temp-write nil
604 (let ((buf (current-buffer))
606 (while (setq article (pop articles))
607 (when (gnus-request-article article group)
608 (goto-char (point-max))
609 (push (cons article (point)) pos)
610 (insert-buffer-substring nntp-server-buffer)))
611 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
612 (setq pos (nreverse pos)))))
613 ;; Then save these articles into the agent.
615 (set-buffer nntp-server-buffer)
617 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
618 (goto-char (point-min))
619 (when (search-forward "\n\n" nil t)
620 (when (search-backward "\nXrefs: " nil t)
621 ;; Handle crossposting.
622 (skip-chars-forward "^ ")
623 (skip-chars-forward " ")
625 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
626 (push (cons (buffer-substring (match-beginning 1)
628 (buffer-substring (match-beginning 2)
631 (goto-char (match-end 0)))
632 (gnus-agent-crosspost crosses (caar pos))))
633 (goto-char (point-min))
634 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
635 (setq id "No-Message-ID-in-article")
636 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
637 (let ((coding-system-for-write
638 gnus-agent-article-file-coding-system))
639 (write-region (point-min) (point-max)
640 (concat dir (number-to-string (caar pos)))
642 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
644 (gnus-agent-enter-history
645 id (or crosses (list (cons group (caar pos)))) date)
648 (gnus-agent-save-alist group)))))
650 (defun gnus-agent-crosspost (crosses article)
651 (let (gnus-agent-article-alist group alist beg end)
653 (set-buffer gnus-agent-overview-buffer)
654 (when (nnheader-find-nov-line article)
657 (setq end (progn (forward-line 1) (point)))))
659 (setq group (caar crosses))
660 (unless (setq alist (assoc group gnus-agent-group-alist))
661 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
662 gnus-agent-group-alist))
663 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
665 (set-buffer (get-buffer-create (format " *Gnus agent overview %s*"
667 (when (= (point-max) (point-min))
668 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
670 (insert-file-contents
671 (gnus-agent-article-name ".overview" group))))
672 (nnheader-find-nov-line (string-to-number (cdar crosses)))
673 (insert (string-to-number (cdar crosses)))
674 (insert-buffer-substring gnus-agent-overview-buffer beg end))
677 (defun gnus-agent-flush-cache ()
679 (while gnus-agent-buffer-alist
680 (set-buffer (cdar gnus-agent-buffer-alist))
681 (write-region (point-min) (point-max)
682 (gnus-agent-article-name ".overview"
683 (caar gnus-agent-buffer-alist))
685 (pop gnus-agent-buffer-alist))
686 (while gnus-agent-group-alist
687 (nnheader-temp-write (caar gnus-agent-group-alist)
688 (princ (cdar gnus-agent-group-alist))
690 (pop gnus-agent-group-alist))))
692 (defun gnus-agent-fetch-headers (group articles &optional force)
693 (gnus-agent-load-alist group)
694 ;; Find out what headers we need to retrieve.
697 (assq (car articles) gnus-agent-article-alist))
699 (let ((arts articles))
701 (if (assq (cadr arts) gnus-agent-article-alist)
702 (setcdr arts (cddr arts))
703 (setq arts (cdr arts)))))
706 (gnus-message 7 "Fetching headers for %s..." group)
708 (set-buffer nntp-server-buffer)
709 (unless (eq 'nov (gnus-retrieve-headers articles group))
710 (nnvirtual-convert-headers))
711 ;; Save these headers for later processing.
712 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
715 (setq file (gnus-agent-article-name ".overview" group)))
716 (gnus-agent-braid-nov group articles file))
717 (gnus-make-directory (nnheader-translate-file-chars
718 (file-name-directory file)))
719 (write-region (point-min) (point-max) file nil 'silent)
720 (gnus-agent-save-alist group articles nil))
723 (defsubst gnus-agent-copy-nov-line (article)
725 (set-buffer gnus-agent-overview-buffer)
727 (if (eq article (read (current-buffer)))
728 (setq e (progn (forward-line 1) (point)))
730 (set-buffer nntp-server-buffer)
731 (insert-buffer-substring gnus-agent-overview-buffer b e)))
733 (defun gnus-agent-braid-nov (group articles file)
735 (set-buffer gnus-agent-overview-buffer)
736 (goto-char (point-min))
737 (set-buffer nntp-server-buffer)
739 (insert-file-contents file)
740 (goto-char (point-min))
741 (if (or (= (point-min) (point-max))
744 (< (read (current-buffer)) (car articles))))
745 ;; We have only headers that are after the older headers,
746 ;; so we just append them.
748 (goto-char (point-max))
749 (insert-buffer-substring gnus-agent-overview-buffer))
750 ;; We do it the hard way.
751 (nnheader-find-nov-line (car articles))
752 (gnus-agent-copy-nov-line (car articles))
756 (while (and (not (eobp))
757 (< (read (current-buffer)) (car articles)))
761 (gnus-agent-copy-nov-line (car articles))
762 (setq articles (cdr articles))))
765 (set-buffer gnus-agent-overview-buffer)
768 (set-buffer nntp-server-buffer)
769 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
771 (defun gnus-agent-load-alist (group &optional dir)
772 "Load the article-state alist for GROUP."
773 (setq gnus-agent-article-alist
774 (gnus-agent-read-file
776 (concat dir ".agentview")
777 (gnus-agent-article-name ".agentview" group)))))
779 (defun gnus-agent-save-alist (group &optional articles state dir)
780 "Load the article-state alist for GROUP."
781 (nnheader-temp-write (if dir
782 (concat dir ".agentview")
783 (gnus-agent-article-name ".agentview" group))
784 (princ (setq gnus-agent-article-alist
785 (nconc gnus-agent-article-alist
786 (mapcar (lambda (article) (cons article state))
791 (defun gnus-agent-article-name (article group)
792 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
793 (if (stringp article) article (string-to-number article))))
796 (defun gnus-agent-batch-fetch ()
797 "Start Gnus and fetch session."
800 (gnus-agent-fetch-session)
803 (defun gnus-agent-fetch-session ()
804 "Fetch all articles and headers that are eligible for fetching."
806 (unless gnus-agent-covered-methods
807 (error "No servers are covered by the Gnus agent"))
809 (error "Can't fetch articles while Gnus is unplugged"))
810 (let ((methods gnus-agent-covered-methods)
811 groups group gnus-command-method)
814 (setq gnus-command-method (car methods)
815 groups (gnus-groups-from-server (pop methods)))
816 (gnus-agent-with-fetch
817 (while (setq group (pop groups))
818 (when (<= (gnus-group-level group) gnus-agent-handle-level)
819 (gnus-agent-fetch-group-1 group gnus-command-method)))))
820 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
822 (defun gnus-agent-fetch-group-1 (group method)
824 (let ((gnus-command-method method)
825 gnus-newsgroup-dependencies gnus-newsgroup-headers
826 gnus-newsgroup-scored gnus-headers gnus-score
827 gnus-use-cache articles score arts
828 category predicate info marks score-param)
830 (when (and (setq articles (gnus-list-of-unread-articles group))
831 (gnus-agent-fetch-headers group articles))
832 ;; Parse them and see which articles we want to fetch.
833 (setq gnus-newsgroup-dependencies
834 (make-vector (length articles) 0))
835 (setq gnus-newsgroup-headers
836 (gnus-get-newsgroup-headers-xover articles nil nil group))
837 (setq category (gnus-group-category group))
840 (or (gnus-group-get-parameter group 'agent-predicate)
843 (or (gnus-group-get-parameter group 'agent-score)
846 (gnus-score-headers (list (list score-param))))
848 (while (setq gnus-headers (pop gnus-newsgroup-headers))
850 (or (cdr (assq (mail-header-number gnus-headers)
851 gnus-newsgroup-scored))
852 gnus-summary-default-score))
853 (when (funcall predicate)
854 (push (mail-header-number gnus-headers)
856 ;; Fetch the articles.
858 (gnus-agent-fetch-articles group arts)))
859 ;; Perhaps we have some additional articles to fetch.
860 (setq arts (assq 'download (gnus-info-marks
861 (setq info (gnus-get-info group)))))
863 (gnus-agent-fetch-articles
864 group (gnus-uncompress-range (cdr arts)))
865 (setq marks (delq arts (gnus-info-marks info)))
866 (gnus-info-set-marks info marks))))
869 ;;; Agent Category Mode
872 (defvar gnus-category-mode-hook nil
873 "Hook run in `gnus-category-mode' buffers.")
875 (defvar gnus-category-line-format " %(%20c%): %g\n"
876 "Format of category lines.")
878 (defvar gnus-category-mode-line-format "Gnus: %%b"
879 "The format specification for the category mode line.")
881 (defvar gnus-agent-short-article 100
882 "Articles that have fewer lines than this are short.")
884 (defvar gnus-agent-long-article 200
885 "Articles that have more lines than this are long.")
887 (defvar gnus-agent-low-score 0
888 "Articles that have a score lower than this have a low score.")
890 (defvar gnus-agent-high-score 0
891 "Articles that have a score higher than this have a high score.")
894 ;;; Internal variables.
896 (defvar gnus-category-buffer "*Agent Category*")
898 (defvar gnus-category-line-format-alist
902 (defvar gnus-category-mode-line-format-alist
903 `((?u user-defined ?s)))
905 (defvar gnus-category-line-format-spec nil)
906 (defvar gnus-category-mode-line-format-spec nil)
908 (defvar gnus-category-mode-map nil)
909 (put 'gnus-category-mode 'mode-class 'special)
911 (unless gnus-category-mode-map
912 (setq gnus-category-mode-map (make-sparse-keymap))
913 (suppress-keymap gnus-category-mode-map)
915 (gnus-define-keys gnus-category-mode-map
916 "q" gnus-category-exit
917 "k" gnus-category-kill
918 "c" gnus-category-copy
919 "a" gnus-category-add
920 "p" gnus-category-edit-predicate
921 "g" gnus-category-edit-groups
922 "s" gnus-category-edit-score
923 "l" gnus-category-list
925 "\C-c\C-i" gnus-info-find-node
926 "\C-c\C-b" gnus-bug))
928 (defvar gnus-category-menu-hook nil
929 "*Hook run after the creation of the menu.")
931 (defun gnus-category-make-menu-bar ()
932 (gnus-turn-off-edit-menu 'category)
933 (unless (boundp 'gnus-category-menu)
935 gnus-category-menu gnus-category-mode-map ""
937 ["Add" gnus-category-add t]
938 ["Kill" gnus-category-kill t]
939 ["Copy" gnus-category-copy t]
940 ["Edit predicate" gnus-category-edit-predicate t]
941 ["Edit score" gnus-category-edit-score t]
942 ["Edit groups" gnus-category-edit-groups t]
943 ["Exit" gnus-category-exit t]))
945 (gnus-run-hooks 'gnus-category-menu-hook)))
947 (defun gnus-category-mode ()
948 "Major mode for listing and editing agent categories.
950 All normal editing commands are switched off.
951 \\<gnus-category-mode-map>
952 For more in-depth information on this mode, read the manual
953 (`\\[gnus-info-find-node]').
955 The following commands are available:
957 \\{gnus-category-mode-map}"
959 (when (gnus-visual-p 'category-menu 'menu)
960 (gnus-category-make-menu-bar))
961 (kill-all-local-variables)
962 (gnus-simplify-mode-line)
963 (setq major-mode 'gnus-category-mode)
964 (setq mode-name "Category")
965 (gnus-set-default-directory)
966 (setq mode-line-process nil)
967 (use-local-map gnus-category-mode-map)
968 (buffer-disable-undo (current-buffer))
969 (setq truncate-lines t)
970 (setq buffer-read-only t)
971 (gnus-run-hooks 'gnus-category-mode-hook))
973 (defalias 'gnus-category-position-point 'gnus-goto-colon)
975 (defun gnus-category-insert-line (category)
976 (let* ((name (car category))
977 (groups (length (cadddr category))))
979 (gnus-add-text-properties
983 (eval gnus-category-line-format-spec))
984 (list 'gnus-category name))))
986 (defun gnus-enter-category-buffer ()
987 "Go to the Category buffer."
989 (gnus-category-setup-buffer)
990 (gnus-configure-windows 'category)
991 (gnus-category-prepare))
993 (defun gnus-category-setup-buffer ()
994 (unless (get-buffer gnus-category-buffer)
996 (set-buffer (get-buffer-create gnus-category-buffer))
997 (gnus-add-current-to-buffer-list)
998 (gnus-category-mode))))
1000 (defun gnus-category-prepare ()
1001 (gnus-set-format 'category-mode)
1002 (gnus-set-format 'category t)
1003 (let ((alist gnus-category-alist)
1004 (buffer-read-only nil))
1007 (gnus-category-insert-line (pop alist)))
1008 (goto-char (point-min))
1009 (gnus-category-position-point)))
1011 (defun gnus-category-name ()
1012 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1013 (error "No category on the current line")))
1015 (defun gnus-category-read ()
1016 "Read the category alist."
1017 (setq gnus-category-alist
1018 (or (gnus-agent-read-file
1019 (nnheader-concat gnus-agent-directory "lib/categories"))
1020 (list (list 'default 'short nil nil)))))
1022 (defun gnus-category-write ()
1023 "Write the category alist."
1024 (setq gnus-category-predicate-cache nil
1025 gnus-category-group-cache nil)
1026 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1027 (prin1 gnus-category-alist (current-buffer))))
1029 (defun gnus-category-edit-predicate (category)
1030 "Edit the predicate for CATEGORY."
1031 (interactive (list (gnus-category-name)))
1032 (let ((info (assq category gnus-category-alist)))
1034 (cadr info) (format "Editing the predicate for category %s" category)
1035 `(lambda (predicate)
1036 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1037 (gnus-category-write)
1038 (gnus-category-list)))))
1040 (defun gnus-category-edit-score (category)
1041 "Edit the score expression for CATEGORY."
1042 (interactive (list (gnus-category-name)))
1043 (let ((info (assq category gnus-category-alist)))
1046 (format "Editing the score expression for category %s" category)
1048 (setf (caddr (assq ',category gnus-category-alist)) groups)
1049 (gnus-category-write)
1050 (gnus-category-list)))))
1052 (defun gnus-category-edit-groups (category)
1053 "Edit the group list for CATEGORY."
1054 (interactive (list (gnus-category-name)))
1055 (let ((info (assq category gnus-category-alist)))
1057 (cadddr info) (format "Editing the group list for category %s" category)
1059 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1060 (gnus-category-write)
1061 (gnus-category-list)))))
1063 (defun gnus-category-kill (category)
1064 "Kill the current category."
1065 (interactive (list (gnus-category-name)))
1066 (let ((info (assq category gnus-category-alist))
1067 (buffer-read-only nil))
1069 (gnus-category-write)
1070 (setq gnus-category-alist (delq info gnus-category-alist))))
1072 (defun gnus-category-copy (category to)
1073 "Copy the current category."
1074 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1075 (let ((info (assq category gnus-category-alist)))
1076 (push (list to (gnus-copy-sequence (cadr info))
1077 (gnus-copy-sequence (caddr info)) nil)
1078 gnus-category-alist)
1079 (gnus-category-write)
1080 (gnus-category-list)))
1082 (defun gnus-category-add (category)
1083 "Create a new category."
1084 (interactive "SCategory name: ")
1085 (when (assq category gnus-category-alist)
1086 (error "Category %s already exists" category))
1087 (push (list category 'true nil nil)
1088 gnus-category-alist)
1089 (gnus-category-write)
1090 (gnus-category-list))
1092 (defun gnus-category-list ()
1093 "List all categories."
1095 (gnus-category-prepare))
1097 (defun gnus-category-exit ()
1098 "Return to the group buffer."
1100 (kill-buffer (current-buffer))
1101 (gnus-configure-windows 'group t))
1103 ;; To avoid having 8-bit characters in the source file.
1104 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1106 (defvar gnus-category-predicate-alist
1107 '((spam . gnus-agent-spam-p)
1108 (short . gnus-agent-short-p)
1109 (long . gnus-agent-long-p)
1110 (low . gnus-agent-low-scored-p)
1111 (high . gnus-agent-high-scored-p)
1112 (true . gnus-agent-true)
1113 (false . gnus-agent-false))
1114 "Mapping from short score predicate symbols to predicate functions.")
1116 (defun gnus-agent-spam-p ()
1117 "Say whether an article is spam or not."
1118 (unless gnus-agent-spam-hashtb
1119 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1120 (if (not (equal (mail-header-references gnus-headers) ""))
1122 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1124 (gnus-gethash string gnus-agent-spam-hashtb)
1125 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1127 (defun gnus-agent-short-p ()
1128 "Say whether an article is short or not."
1129 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1131 (defun gnus-agent-long-p ()
1132 "Say whether an article is long or not."
1133 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1135 (defun gnus-agent-low-scored-p ()
1136 "Say whether an article has a low score or not."
1137 (< gnus-score gnus-agent-low-score))
1139 (defun gnus-agent-high-scored-p ()
1140 "Say whether an article has a high score or not."
1141 (> gnus-score gnus-agent-low-score))
1143 (defun gnus-category-make-function (cat)
1144 "Make a function from category CAT."
1145 `(lambda () ,(gnus-category-make-function-1 cat)))
1147 (defun gnus-agent-true ()
1151 (defun gnus-agent-false ()
1155 (defun gnus-category-make-function-1 (cat)
1156 "Make a function from category CAT."
1158 ;; Functions are just returned as is.
1160 (gnus-functionp cat))
1161 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1163 ;; More complex category.
1166 ((memq (car cat) '(& and))
1168 ((memq (car cat) '(| or))
1170 ((memq (car cat) gnus-category-not)
1172 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1174 (error "Unknown category type: %s" cat))))
1176 (defun gnus-get-predicate (predicate)
1177 "Return the predicate for CATEGORY."
1178 (or (cdr (assoc predicate gnus-category-predicate-cache))
1179 (cdar (push (cons predicate
1180 (gnus-category-make-function predicate))
1181 gnus-category-predicate-cache))))
1183 (defun gnus-group-category (group)
1184 "Return the category GROUP belongs to."
1185 (unless gnus-category-group-cache
1186 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1187 (let ((cs gnus-category-alist)
1189 (while (setq cat (pop cs))
1190 (setq groups (cadddr cat))
1192 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1193 (or (gnus-gethash group gnus-category-group-cache)
1194 (assq 'default gnus-category-alist)))
1196 (defun gnus-agent-expire ()
1197 "Expire all old articles."
1199 (let ((methods gnus-agent-covered-methods)
1200 (day (- (gnus-time-to-day (current-time)) gnus-agent-expire-days))
1201 (expiry-hashtb (gnus-make-hashtable 1023))
1202 gnus-command-method sym group articles
1203 history overview file histories elem art nov-file low info
1204 unreads marked article)
1206 (setq overview (get-buffer-create " *expire overview*"))
1207 (while (setq gnus-command-method (pop methods))
1208 (gnus-agent-open-history)
1210 (setq gnus-agent-current-history
1211 (setq history (gnus-agent-history-buffer))))
1212 (goto-char (point-min))
1214 (skip-chars-forward "^\t")
1215 (if (> (read (current-buffer)) day)
1216 ;; New article; we don't expire it.
1218 ;; Old article. Schedule it for possible nuking.
1220 (setq sym (let ((obarray expiry-hashtb))
1221 (read (current-buffer))))
1223 (set sym (cons (cons (read (current-buffer)) (point))
1224 (symbol-value sym)))
1225 (set sym (list (cons (read (current-buffer)) (point)))))
1226 (skip-chars-forward " "))
1228 ;; We now have all articles that can possibly be expired.
1231 (setq group (symbol-name sym)
1232 articles (sort (symbol-value sym) 'car-less-than-car)
1233 low (car (gnus-active group))
1234 info (gnus-get-info group)
1235 unreads (ignore-errors (gnus-list-of-unread-articles group))
1236 marked (nconc (gnus-uncompress-range
1237 (cdr (assq 'ticked (gnus-info-marks info))))
1238 (gnus-uncompress-range
1239 (cdr (assq 'dormant (gnus-info-marks info)))))
1240 nov-file (gnus-agent-article-name ".overview" group))
1241 (gnus-message 5 "Expiring articles in %s" group)
1242 (set-buffer overview)
1244 (when (file-exists-p nov-file)
1245 (insert-file-contents nov-file))
1246 (goto-char (point-min))
1247 (while (setq elem (pop articles))
1248 (setq article (car elem))
1249 (when (or (null low)
1251 (and (not (memq article unreads))
1252 (not (memq article marked))))
1253 ;; Find and nuke the NOV line.
1254 (while (and (not (eobp))
1255 (< (setq art (read (current-buffer))) article))
1261 ;; Nuke the article.
1262 (when (file-exists-p (setq file (gnus-agent-article-name
1263 (number-to-string article)
1266 ;; Schedule the history line for nuking.
1267 (push (cdr elem) histories)))
1268 (write-region (point-min) (point-max) nov-file nil 'silent))
1270 (set-buffer history)
1271 (setq histories (nreverse (sort histories '<)))
1273 (goto-char (pop histories))
1275 (gnus-agent-save-history)
1276 (gnus-agent-close-history)))))
1279 (defun gnus-agent-batch ()
1281 (let ((init-file-user "")
1282 (gnus-always-read-dribble-file t))
1284 (gnus-group-send-drafts)
1285 (gnus-agent-fetch-session))
1287 (provide 'gnus-agent)
1289 ;;; gnus-agent.el ends here