1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (defcustom gnus-agent-expire-all nil
58 "If non-nil, also expire unread, ticked and dormant articles.
59 If nil, only read articles will be expired."
63 (defcustom gnus-agent-group-mode-hook nil
64 "Hook run in Agent group minor modes."
68 (defcustom gnus-agent-summary-mode-hook nil
69 "Hook run in Agent summary minor modes."
73 (defcustom gnus-agent-server-mode-hook nil
74 "Hook run in Agent summary minor modes."
78 ;;; Internal variables
80 (defvar gnus-agent-meta-information-header "X-Gnus-Agent-Meta-Information")
82 (defvar gnus-agent-history-buffers nil)
83 (defvar gnus-agent-buffer-alist nil)
84 (defvar gnus-agent-article-alist nil)
85 (defvar gnus-agent-group-alist nil)
86 (defvar gnus-agent-covered-methods nil)
87 (defvar gnus-category-alist nil)
88 (defvar gnus-agent-current-history nil)
89 (defvar gnus-agent-overview-buffer nil)
90 (defvar gnus-category-predicate-cache nil)
91 (defvar gnus-category-group-cache nil)
92 (defvar gnus-agent-spam-hashtb nil)
93 (defvar gnus-agent-file-name nil)
94 (defvar gnus-agent-send-mail-function nil)
95 (defvar gnus-agent-article-file-coding-system 'no-conversion)
105 (defun gnus-open-agent ()
107 (gnus-agent-read-servers)
109 (setq gnus-agent-overview-buffer
110 (gnus-get-buffer-create " *Gnus agent overview*"))
111 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
112 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
113 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
115 (gnus-add-shutdown 'gnus-close-agent 'gnus)
117 (defun gnus-close-agent ()
118 (setq gnus-agent-covered-methods nil
119 gnus-category-predicate-cache nil
120 gnus-category-group-cache nil
121 gnus-agent-spam-hashtb nil)
122 (gnus-kill-buffer gnus-agent-overview-buffer))
125 ;;; Utility functions
128 (defun gnus-agent-read-file (file)
129 "Load FILE and do a `read' there."
132 (nnheader-insert-file-contents file)
133 (goto-char (point-min))
134 (read (current-buffer)))))
136 (defsubst gnus-agent-method ()
137 (concat (symbol-name (car gnus-command-method)) "/"
138 (if (equal (cadr gnus-command-method) "")
140 (cadr gnus-command-method))))
142 (defsubst gnus-agent-directory ()
143 "Path of the Gnus agent directory."
144 (nnheader-concat gnus-agent-directory
145 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
147 (defun gnus-agent-lib-file (file)
148 "The full path of the Gnus agent library FILE."
149 (concat (gnus-agent-directory) "agent.lib/" file))
151 ;;; Fetching setup functions.
153 (defun gnus-agent-start-fetch ()
154 "Initialize data structures for efficient fetching."
155 (gnus-agent-open-history)
156 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
158 (defun gnus-agent-stop-fetch ()
159 "Save all data structures and clean up."
160 (gnus-agent-save-history)
161 (gnus-agent-close-history)
162 (setq gnus-agent-spam-hashtb nil)
164 (set-buffer nntp-server-buffer)
167 (defmacro gnus-agent-with-fetch (&rest forms)
171 (gnus-agent-start-fetch)
173 (gnus-agent-stop-fetch)))
175 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
176 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
182 (defvar gnus-agent-mode-hook nil
183 "Hook run when installing agent mode.")
185 (defvar gnus-agent-mode nil)
186 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
188 (defun gnus-agent-mode ()
189 "Minor mode for providing a agent support in Gnus buffers."
190 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
191 (symbol-name major-mode))
192 (match-string 1 (symbol-name major-mode))))
193 (mode (intern (format "gnus-agent-%s-mode" buffer))))
194 (set (make-local-variable 'gnus-agent-mode) t)
196 (set (make-local-variable mode) t)
198 (when (gnus-visual-p 'agent-menu 'menu)
199 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
200 (unless (assq 'gnus-agent-mode minor-mode-alist)
201 (push gnus-agent-mode-status minor-mode-alist))
202 (unless (assq mode minor-mode-map-alist)
203 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
205 minor-mode-map-alist))
206 (gnus-agent-toggle-plugged gnus-plugged)
207 (gnus-run-hooks 'gnus-agent-mode-hook
208 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
210 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
211 (gnus-define-keys gnus-agent-group-mode-map
212 "Ju" gnus-agent-fetch-groups
213 "Jc" gnus-enter-category-buffer
214 "Jj" gnus-agent-toggle-plugged
215 "Js" gnus-agent-fetch-session
216 "JS" gnus-group-send-drafts
217 "Ja" gnus-agent-add-group)
219 (defun gnus-agent-group-make-menu-bar ()
220 (unless (boundp 'gnus-agent-group-menu)
222 gnus-agent-group-menu gnus-agent-group-mode-map ""
224 ["Toggle plugged" gnus-agent-toggle-plugged t]
225 ["List categories" gnus-enter-category-buffer t]
226 ["Send drafts" gnus-group-send-drafts gnus-plugged]
228 ["All" gnus-agent-fetch-session gnus-plugged]
229 ["Group" gnus-agent-fetch-group gnus-plugged])))))
231 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
232 (gnus-define-keys gnus-agent-summary-mode-map
233 "Jj" gnus-agent-toggle-plugged
234 "J#" gnus-agent-mark-article
235 "J\M-#" gnus-agent-unmark-article
236 "@" gnus-agent-toggle-mark
237 "Jc" gnus-agent-catchup)
239 (defun gnus-agent-summary-make-menu-bar ()
240 (unless (boundp 'gnus-agent-summary-menu)
242 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
244 ["Toggle plugged" gnus-agent-toggle-plugged t]
245 ["Mark as downloadable" gnus-agent-mark-article t]
246 ["Unmark as downloadable" gnus-agent-unmark-article t]
247 ["Toggle mark" gnus-agent-toggle-mark t]
248 ["Catchup undownloaded" gnus-agent-catchup t]))))
250 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
251 (gnus-define-keys gnus-agent-server-mode-map
252 "Jj" gnus-agent-toggle-plugged
253 "Ja" gnus-agent-add-server
254 "Jr" gnus-agent-remove-server)
256 (defun gnus-agent-server-make-menu-bar ()
257 (unless (boundp 'gnus-agent-server-menu)
259 gnus-agent-server-menu gnus-agent-server-mode-map ""
261 ["Toggle plugged" gnus-agent-toggle-plugged t]
262 ["Add" gnus-agent-add-server t]
263 ["Remove" gnus-agent-remove-server t]))))
265 (defun gnus-agent-toggle-plugged (plugged)
266 "Toggle whether Gnus is unplugged or not."
267 (interactive (list (not gnus-plugged)))
270 (setq gnus-plugged plugged)
271 (gnus-run-hooks 'gnus-agent-plugged-hook)
272 (setcar (cdr gnus-agent-mode-status) " Plugged"))
273 (gnus-agent-close-connections)
274 (setq gnus-plugged plugged)
275 (gnus-run-hooks 'gnus-agent-unplugged-hook)
276 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
277 (set-buffer-modified-p t))
279 (defun gnus-agent-close-connections ()
280 "Close all methods covered by the Gnus agent."
281 (let ((methods gnus-agent-covered-methods))
283 (gnus-close-server (pop methods)))))
286 (defun gnus-unplugged ()
287 "Start Gnus unplugged."
289 (setq gnus-plugged nil)
293 (defun gnus-plugged ()
294 "Start Gnus plugged."
296 (setq gnus-plugged t)
300 (defun gnus-agentize ()
301 "Allow Gnus to be an offline newsreader.
302 The normal usage of this command is to put the following as the
303 last form in your `.gnus.el' file:
307 This will modify the `gnus-before-startup-hook', `gnus-post-method',
308 and `message-send-mail-function' variables, and install the Gnus
309 agent minor mode in all Gnus buffers."
312 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
313 (unless gnus-agent-send-mail-function
314 (setq gnus-agent-send-mail-function message-send-mail-function
315 message-send-mail-function 'gnus-agent-send-mail))
316 (unless gnus-agent-covered-methods
317 (setq gnus-agent-covered-methods (list gnus-select-method))))
319 (defun gnus-agent-queue-setup ()
320 "Make sure the queue group exists."
321 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
322 (gnus-request-create-group "queue" '(nndraft ""))
323 (let ((gnus-level-default-subscribed 1))
324 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
325 (gnus-group-set-parameter
326 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
328 (defun gnus-agent-send-mail ()
330 (funcall gnus-agent-send-mail-function)
331 (goto-char (point-min))
333 (concat "^" (regexp-quote mail-header-separator) "\n"))
335 (gnus-agent-insert-meta-information 'mail)
336 (gnus-request-accept-article "nndraft:queue")))
338 (defun gnus-agent-insert-meta-information (type &optional method)
339 "Insert meta-information into the message that says how it's to be posted.
340 TYPE can be either `mail' or `news'. If the latter METHOD can
343 (message-remove-header gnus-agent-meta-information-header)
344 (goto-char (point-min))
345 (insert gnus-agent-meta-information-header ": "
346 (symbol-name type) " " (format "%S" method)
349 (while (search-backward "\n" nil t)
350 (replace-match "\\n" t t))))
353 ;;; Group mode commands
356 (defun gnus-agent-fetch-groups (n)
357 "Put all new articles in the current groups into the Agent."
359 (gnus-group-iterate n 'gnus-agent-fetch-group))
361 (defun gnus-agent-fetch-group (group)
362 "Put all new articles in GROUP into the Agent."
363 (interactive (list (gnus-group-group-name)))
365 (error "No group on the current line"))
366 (let ((gnus-command-method (gnus-find-method-for-group group)))
367 (gnus-agent-with-fetch
368 (gnus-agent-fetch-group-1 group gnus-command-method)
369 (gnus-message 5 "Fetching %s...done" group))))
371 (defun gnus-agent-add-group (category arg)
372 "Add the current group to an agent category."
378 (mapcar (lambda (cat) (list (symbol-name (car cat))))
382 (let ((cat (assq category gnus-category-alist))
384 (gnus-group-iterate arg
386 (when (cadddr (setq c (gnus-group-category group)))
387 (setf (cadddr c) (delete group (cadddr c))))
388 (push group groups)))
389 (setf (cadddr cat) (nconc (cadddr cat) groups))
390 (gnus-category-write)))
393 ;;; Server mode commands
396 (defun gnus-agent-add-server (server)
397 "Enroll SERVER in the agent program."
398 (interactive (list (gnus-server-server-name)))
400 (error "No server on the current line"))
401 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
402 (when (member method gnus-agent-covered-methods)
403 (error "Server already in the agent program"))
404 (push method gnus-agent-covered-methods)
405 (gnus-agent-write-servers)
406 (message "Entered %s into the Agent" server)))
408 (defun gnus-agent-remove-server (server)
409 "Remove SERVER from the agent program."
410 (interactive (list (gnus-server-server-name)))
412 (error "No server on the current line"))
413 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
414 (unless (member method gnus-agent-covered-methods)
415 (error "Server not in the agent program"))
416 (setq gnus-agent-covered-methods
417 (delete method gnus-agent-covered-methods))
418 (gnus-agent-write-servers)
419 (message "Removed %s from the agent" server)))
421 (defun gnus-agent-read-servers ()
422 "Read the alist of covered servers."
423 (setq gnus-agent-covered-methods
424 (gnus-agent-read-file
425 (nnheader-concat gnus-agent-directory "lib/servers"))))
427 (defun gnus-agent-write-servers ()
428 "Write the alist of covered servers."
429 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
430 (prin1 gnus-agent-covered-methods (current-buffer))))
436 (defun gnus-agent-mark-article (n &optional unmark)
437 "Mark the next N articles as downloadable.
438 If N is negative, mark backward instead. If UNMARK is non-nil, remove
439 the mark instead. The difference between N and the actual number of
440 articles marked is returned."
442 (let ((backward (< n 0))
447 (gnus-summary-set-agent-mark
448 (gnus-summary-article-number) unmark)
449 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
452 (gnus-message 7 "No more articles"))
453 (gnus-summary-recenter)
454 (gnus-summary-position-point)
457 (defun gnus-agent-unmark-article (n)
458 "Remove the downloadable mark from the next N articles.
459 If N is negative, unmark backward instead. The difference between N and
460 the actual number of articles unmarked is returned."
462 (gnus-agent-mark-article n t))
464 (defun gnus-agent-toggle-mark (n)
465 "Toggle the downloadable mark from the next N articles.
466 If N is negative, toggle backward instead. The difference between N and
467 the actual number of articles toggled is returned."
469 (gnus-agent-mark-article n 'toggle))
471 (defun gnus-summary-set-agent-mark (article &optional unmark)
472 "Mark ARTICLE as downloadable."
473 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
474 (memq article gnus-newsgroup-downloadable)
478 (setq gnus-newsgroup-downloadable
479 (delq article gnus-newsgroup-downloadable))
480 (push article gnus-newsgroup-undownloaded))
481 (setq gnus-newsgroup-undownloaded
482 (delq article gnus-newsgroup-undownloaded))
483 (push article gnus-newsgroup-downloadable))
484 (gnus-summary-update-mark
485 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
488 (defun gnus-agent-get-undownloaded-list ()
489 "Mark all unfetched articles as read."
490 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
491 (when (and (not gnus-plugged)
492 (gnus-agent-method-p gnus-command-method))
493 (gnus-agent-load-alist gnus-newsgroup-name)
494 (let ((articles gnus-newsgroup-unreads)
496 (while (setq article (pop articles))
497 (unless (or (cdr (assq article gnus-agent-article-alist))
498 (memq article gnus-newsgroup-downloadable))
499 (push article gnus-newsgroup-undownloaded)))))))
501 (defun gnus-agent-catchup ()
502 "Mark all undownloaded articles as read."
505 (while gnus-newsgroup-undownloaded
506 (gnus-summary-mark-article
507 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
508 (gnus-summary-position-point))
511 ;;; Internal functions
514 (defun gnus-agent-save-active (method)
515 (when (gnus-agent-method-p method)
516 (let* ((gnus-command-method method)
517 (file (gnus-agent-lib-file "active")))
518 (gnus-make-directory (file-name-directory file))
519 (let ((coding-system-for-write gnus-agent-article-file-coding-system))
520 (write-region (point-min) (point-max) file nil 'silent))
521 (when (file-exists-p (gnus-agent-lib-file "groups"))
522 (delete-file (gnus-agent-lib-file "groups"))))))
524 (defun gnus-agent-save-groups (method)
525 (let* ((gnus-command-method method)
526 (file (gnus-agent-lib-file "groups")))
527 (gnus-make-directory (file-name-directory file))
528 (write-region (point-min) (point-max) file nil 'silent))
529 (when (file-exists-p (gnus-agent-lib-file "active"))
530 (delete-file (gnus-agent-lib-file "active"))))
532 (defun gnus-agent-save-group-info (method group active)
533 (when (gnus-agent-method-p method)
534 (let* ((gnus-command-method method)
535 (file (if nntp-server-list-active-group
536 (gnus-agent-lib-file "active")
537 (gnus-agent-lib-file "groups"))))
538 (gnus-make-directory (file-name-directory file))
540 (when (file-exists-p file)
541 (nnheader-insert-file-contents file))
542 (goto-char (point-min))
543 (if nntp-server-list-active-group
545 (when (re-search-forward
546 (concat "^" (regexp-quote group) " ") nil t)
548 (insert group " " (number-to-string (cdr active)) " "
549 (number-to-string (car active)) "\n"))
550 (when (re-search-forward (concat (regexp-quote group) " ") nil t)
552 (insert-buffer-substring nntp-server-buffer))))))
554 (defun gnus-agent-group-path (group)
555 "Translate GROUP into a path."
556 (if nnmail-use-long-file-names
557 (gnus-group-real-name group)
558 (nnheader-replace-chars-in-string
559 (nnheader-translate-file-chars (gnus-group-real-name group))
564 (defun gnus-agent-method-p (method)
565 "Say whether METHOD is covered by the agent."
566 (member method gnus-agent-covered-methods))
568 (defun gnus-agent-get-function (method)
569 (if (and (not gnus-plugged)
570 (gnus-agent-method-p method))
576 ;;; History functions
578 (defun gnus-agent-history-buffer ()
579 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
581 (defun gnus-agent-open-history ()
583 (push (cons (gnus-agent-method)
584 (set-buffer (gnus-get-buffer-create
585 (format " *Gnus agent %s history*"
586 (gnus-agent-method)))))
587 gnus-agent-history-buffers)
590 (let ((file (gnus-agent-lib-file "history")))
591 (when (file-exists-p file)
593 (set (make-local-variable 'gnus-agent-file-name) file))))
595 (defun gnus-agent-save-history ()
597 (set-buffer gnus-agent-current-history)
598 (gnus-make-directory (file-name-directory gnus-agent-file-name))
599 (write-region (1+ (point-min)) (point-max)
600 gnus-agent-file-name nil 'silent)))
602 (defun gnus-agent-close-history ()
603 (when (gnus-buffer-live-p gnus-agent-current-history)
604 (kill-buffer gnus-agent-current-history)
605 (setq gnus-agent-history-buffers
606 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
607 gnus-agent-history-buffers))))
609 (defun gnus-agent-enter-history (id group-arts date)
611 (set-buffer gnus-agent-current-history)
612 (goto-char (point-max))
613 (insert id "\t" (number-to-string date) "\t")
615 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
619 (defun gnus-agent-article-in-history-p (id)
621 (set-buffer (gnus-agent-history-buffer))
622 (goto-char (point-min))
623 (search-forward (concat "\n" id "\t") nil t)))
625 (defun gnus-agent-history-path (id)
627 (set-buffer (gnus-agent-history-buffer))
628 (goto-char (point-min))
629 (when (search-forward (concat "\n" id "\t") nil t)
630 (let ((method (gnus-agent-method)))
632 (while (not (numberp (setq group (read (current-buffer)))))
633 (push (concat method "/" group) paths))
634 (nreverse paths))))))
640 (defun gnus-agent-fetch-articles (group articles)
641 "Fetch ARTICLES from GROUP and put them into the Agent."
643 ;; Prune off articles that we have already fetched.
645 (cdr (assq (car articles) gnus-agent-article-alist)))
647 (let ((arts articles))
649 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
650 (setcdr arts (cddr arts))
651 (setq arts (cdr arts)))))
654 (gnus-agent-directory)
655 (gnus-agent-group-path group) "/"))
656 (date (time-to-day (current-time)))
659 (gnus-make-directory dir)
660 (gnus-message 7 "Fetching articles for %s..." group)
661 ;; Fetch the articles from the backend.
662 (if (gnus-check-backend-function 'retrieve-articles group)
663 (setq pos (gnus-retrieve-articles articles group))
666 (while (setq article (pop articles))
667 (when (gnus-request-article article group)
668 (goto-char (point-max))
669 (push (cons article (point)) pos)
670 (insert-buffer-substring nntp-server-buffer)))
671 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
672 (setq pos (nreverse pos)))))
673 ;; Then save these articles into the Agent.
675 (set-buffer nntp-server-buffer)
677 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
678 (goto-char (point-min))
679 (when (search-forward "\n\n" nil t)
680 (when (search-backward "\nXrefs: " nil t)
681 ;; Handle crossposting.
682 (skip-chars-forward "^ ")
683 (skip-chars-forward " ")
685 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
686 (push (cons (buffer-substring (match-beginning 1)
688 (buffer-substring (match-beginning 2)
691 (goto-char (match-end 0)))
692 (gnus-agent-crosspost crosses (caar pos))))
693 (goto-char (point-min))
694 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
695 (setq id "No-Message-ID-in-article")
696 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
697 (let ((coding-system-for-write
698 gnus-agent-article-file-coding-system))
699 (write-region (point-min) (point-max)
700 (concat dir (number-to-string (caar pos)))
702 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
704 (gnus-agent-enter-history
705 id (or crosses (list (cons group (caar pos)))) date)
708 (gnus-agent-save-alist group)))))
710 (defun gnus-agent-crosspost (crosses article)
711 (let (gnus-agent-article-alist group alist beg end)
713 (set-buffer gnus-agent-overview-buffer)
714 (when (nnheader-find-nov-line article)
717 (setq end (progn (forward-line 1) (point)))))
719 (setq group (caar crosses))
720 (unless (setq alist (assoc group gnus-agent-group-alist))
721 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
722 gnus-agent-group-alist))
723 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
725 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
727 (when (= (point-max) (point-min))
728 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
730 (nnheader-insert-file-contents
731 (gnus-agent-article-name ".overview" group))))
732 (nnheader-find-nov-line (string-to-number (cdar crosses)))
733 (insert (string-to-number (cdar crosses)))
734 (insert-buffer-substring gnus-agent-overview-buffer beg end))
737 (defun gnus-agent-flush-cache ()
739 (while gnus-agent-buffer-alist
740 (set-buffer (cdar gnus-agent-buffer-alist))
741 (write-region (point-min) (point-max)
742 (gnus-agent-article-name ".overview"
743 (caar gnus-agent-buffer-alist))
745 (pop gnus-agent-buffer-alist))
746 (while gnus-agent-group-alist
747 (with-temp-file (caar gnus-agent-group-alist)
748 (princ (cdar gnus-agent-group-alist))
750 (pop gnus-agent-group-alist))))
752 (defun gnus-agent-fetch-headers (group articles &optional force)
753 (gnus-agent-load-alist group)
754 ;; Find out what headers we need to retrieve.
757 (assq (car articles) gnus-agent-article-alist))
759 (let ((arts articles))
761 (if (assq (cadr arts) gnus-agent-article-alist)
762 (setcdr arts (cddr arts))
763 (setq arts (cdr arts)))))
766 (gnus-message 7 "Fetching headers for %s..." group)
768 (set-buffer nntp-server-buffer)
769 (unless (eq 'nov (gnus-retrieve-headers articles group))
770 (nnvirtual-convert-headers))
772 ;; To gnus-agent-expire work fine with no Xref field in .overview
773 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
774 (goto-char (point-min))
776 (goto-char (point-at-eol))
779 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
780 ;; To gnus-agent-expire work fine with no Xref field in .overview
782 ;; Save these headers for later processing.
783 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
786 (setq file (gnus-agent-article-name ".overview" group)))
787 (gnus-agent-braid-nov group articles file))
788 (gnus-make-directory (nnheader-translate-file-chars
789 (file-name-directory file)))
790 (write-region (point-min) (point-max) file nil 'silent)
791 (gnus-agent-save-alist group articles nil)
792 (gnus-agent-enter-history "last-header-fetched-for-session"
793 (list (cons group (nth (- (length articles) 1) articles)))
794 (time-to-day (current-time)))
797 (defsubst gnus-agent-copy-nov-line (article)
799 (set-buffer gnus-agent-overview-buffer)
801 (if (eq article (read (current-buffer)))
802 (setq e (progn (forward-line 1) (point)))
804 (set-buffer nntp-server-buffer)
805 (insert-buffer-substring gnus-agent-overview-buffer b e)))
807 (defun gnus-agent-braid-nov (group articles file)
809 (set-buffer gnus-agent-overview-buffer)
810 (goto-char (point-min))
811 (set-buffer nntp-server-buffer)
813 (nnheader-insert-file-contents file)
814 (goto-char (point-max))
815 (if (or (= (point-min) (point-max))
818 (< (read (current-buffer)) (car articles))))
819 ;; We have only headers that are after the older headers,
820 ;; so we just append them.
822 (goto-char (point-max))
823 (insert-buffer-substring gnus-agent-overview-buffer))
824 ;; We do it the hard way.
825 (nnheader-find-nov-line (car articles))
826 (gnus-agent-copy-nov-line (car articles))
830 (while (and (not (eobp))
831 (< (read (current-buffer)) (car articles)))
835 (gnus-agent-copy-nov-line (car articles))
836 (setq articles (cdr articles))))
839 (set-buffer gnus-agent-overview-buffer)
842 (set-buffer nntp-server-buffer)
843 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
845 (defun gnus-agent-load-alist (group &optional dir)
846 "Load the article-state alist for GROUP."
847 (setq gnus-agent-article-alist
848 (gnus-agent-read-file
850 (concat dir ".agentview")
851 (gnus-agent-article-name ".agentview" group)))))
853 (defun gnus-agent-save-alist (group &optional articles state dir)
854 "Load the article-state alist for GROUP."
855 (with-temp-file (if dir
856 (concat dir ".agentview")
857 (gnus-agent-article-name ".agentview" group))
858 (princ (setq gnus-agent-article-alist
859 (nconc gnus-agent-article-alist
860 (mapcar (lambda (article) (cons article state))
865 (defun gnus-agent-article-name (article group)
866 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
867 (if (stringp article) article (string-to-number article))))
870 (defun gnus-agent-batch-fetch ()
871 "Start Gnus and fetch session."
874 (gnus-agent-fetch-session)
877 (defun gnus-agent-fetch-session ()
878 "Fetch all articles and headers that are eligible for fetching."
880 (unless gnus-agent-covered-methods
881 (error "No servers are covered by the Gnus agent"))
883 (error "Can't fetch articles while Gnus is unplugged"))
884 (let ((methods gnus-agent-covered-methods)
885 groups group gnus-command-method)
888 (setq gnus-command-method (car methods))
889 (when (or (gnus-server-opened gnus-command-method)
890 (gnus-open-server gnus-command-method))
891 (setq groups (gnus-groups-from-server (car methods)))
892 (gnus-agent-with-fetch
893 (while (setq group (pop groups))
894 (when (<= (gnus-group-level group) gnus-agent-handle-level)
895 (gnus-agent-fetch-group-1 group gnus-command-method)))))
897 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
899 (defun gnus-agent-fetch-group-1 (group method)
901 (let ((gnus-command-method method)
902 gnus-newsgroup-dependencies gnus-newsgroup-headers
903 gnus-newsgroup-scored gnus-headers gnus-score
904 gnus-use-cache articles score arts
905 category predicate info marks score-param)
907 (when (and (or (gnus-active group) (gnus-activate-group group))
908 (setq articles (gnus-list-of-unread-articles group))
909 (gnus-agent-fetch-headers group articles))
910 ;; Parse them and see which articles we want to fetch.
911 (setq gnus-newsgroup-dependencies
912 (make-vector (length articles) 0))
913 (setq gnus-newsgroup-headers
914 (gnus-get-newsgroup-headers-xover articles nil nil group))
915 (setq category (gnus-group-category group))
918 (or (gnus-group-get-parameter group 'agent-predicate)
921 (or (gnus-group-get-parameter group 'agent-score)
924 (gnus-score-headers (list (list score-param))))
926 (while (setq gnus-headers (pop gnus-newsgroup-headers))
928 (or (cdr (assq (mail-header-number gnus-headers)
929 gnus-newsgroup-scored))
930 gnus-summary-default-score))
931 (when (funcall predicate)
932 (push (mail-header-number gnus-headers)
934 ;; Fetch the articles.
936 (gnus-agent-fetch-articles group arts)))
937 ;; Perhaps we have some additional articles to fetch.
938 (setq arts (assq 'download (gnus-info-marks
939 (setq info (gnus-get-info group)))))
941 (gnus-agent-fetch-articles
942 group (gnus-uncompress-range (cdr arts)))
943 (setq marks (delq arts (gnus-info-marks info)))
944 (gnus-info-set-marks info marks))))
947 ;;; Agent Category Mode
950 (defvar gnus-category-mode-hook nil
951 "Hook run in `gnus-category-mode' buffers.")
953 (defvar gnus-category-line-format " %(%20c%): %g\n"
954 "Format of category lines.")
956 (defvar gnus-category-mode-line-format "Gnus: %%b"
957 "The format specification for the category mode line.")
959 (defvar gnus-agent-short-article 100
960 "Articles that have fewer lines than this are short.")
962 (defvar gnus-agent-long-article 200
963 "Articles that have more lines than this are long.")
965 (defvar gnus-agent-low-score 0
966 "Articles that have a score lower than this have a low score.")
968 (defvar gnus-agent-high-score 0
969 "Articles that have a score higher than this have a high score.")
972 ;;; Internal variables.
974 (defvar gnus-category-buffer "*Agent Category*")
976 (defvar gnus-category-line-format-alist
977 `((?c gnus-tmp-name ?s)
978 (?g gnus-tmp-groups ?d)))
980 (defvar gnus-category-mode-line-format-alist
981 `((?u user-defined ?s)))
983 (defvar gnus-category-line-format-spec nil)
984 (defvar gnus-category-mode-line-format-spec nil)
986 (defvar gnus-category-mode-map nil)
987 (put 'gnus-category-mode 'mode-class 'special)
989 (unless gnus-category-mode-map
990 (setq gnus-category-mode-map (make-sparse-keymap))
991 (suppress-keymap gnus-category-mode-map)
993 (gnus-define-keys gnus-category-mode-map
994 "q" gnus-category-exit
995 "k" gnus-category-kill
996 "c" gnus-category-copy
997 "a" gnus-category-add
998 "p" gnus-category-edit-predicate
999 "g" gnus-category-edit-groups
1000 "s" gnus-category-edit-score
1001 "l" gnus-category-list
1003 "\C-c\C-i" gnus-info-find-node
1004 "\C-c\C-b" gnus-bug))
1006 (defvar gnus-category-menu-hook nil
1007 "*Hook run after the creation of the menu.")
1009 (defun gnus-category-make-menu-bar ()
1010 (gnus-turn-off-edit-menu 'category)
1011 (unless (boundp 'gnus-category-menu)
1013 gnus-category-menu gnus-category-mode-map ""
1015 ["Add" gnus-category-add t]
1016 ["Kill" gnus-category-kill t]
1017 ["Copy" gnus-category-copy t]
1018 ["Edit predicate" gnus-category-edit-predicate t]
1019 ["Edit score" gnus-category-edit-score t]
1020 ["Edit groups" gnus-category-edit-groups t]
1021 ["Exit" gnus-category-exit t]))
1023 (gnus-run-hooks 'gnus-category-menu-hook)))
1025 (defun gnus-category-mode ()
1026 "Major mode for listing and editing agent categories.
1028 All normal editing commands are switched off.
1029 \\<gnus-category-mode-map>
1030 For more in-depth information on this mode, read the manual
1031 (`\\[gnus-info-find-node]').
1033 The following commands are available:
1035 \\{gnus-category-mode-map}"
1037 (when (gnus-visual-p 'category-menu 'menu)
1038 (gnus-category-make-menu-bar))
1039 (kill-all-local-variables)
1040 (gnus-simplify-mode-line)
1041 (setq major-mode 'gnus-category-mode)
1042 (setq mode-name "Category")
1043 (gnus-set-default-directory)
1044 (setq mode-line-process nil)
1045 (use-local-map gnus-category-mode-map)
1046 (buffer-disable-undo (current-buffer))
1047 (setq truncate-lines t)
1048 (setq buffer-read-only t)
1049 (gnus-run-hooks 'gnus-category-mode-hook))
1051 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1053 (defun gnus-category-insert-line (category)
1054 (let* ((gnus-tmp-name (car category))
1055 (gnus-tmp-groups (length (cadddr category))))
1057 (gnus-add-text-properties
1061 (eval gnus-category-line-format-spec))
1062 (list 'gnus-category gnus-tmp-name))))
1064 (defun gnus-enter-category-buffer ()
1065 "Go to the Category buffer."
1067 (gnus-category-setup-buffer)
1068 (gnus-configure-windows 'category)
1069 (gnus-category-prepare))
1071 (defun gnus-category-setup-buffer ()
1072 (unless (get-buffer gnus-category-buffer)
1074 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1075 (gnus-category-mode))))
1077 (defun gnus-category-prepare ()
1078 (gnus-set-format 'category-mode)
1079 (gnus-set-format 'category t)
1080 (let ((alist gnus-category-alist)
1081 (buffer-read-only nil))
1084 (gnus-category-insert-line (pop alist)))
1085 (goto-char (point-min))
1086 (gnus-category-position-point)))
1088 (defun gnus-category-name ()
1089 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1090 (error "No category on the current line")))
1092 (defun gnus-category-read ()
1093 "Read the category alist."
1094 (setq gnus-category-alist
1095 (or (gnus-agent-read-file
1096 (nnheader-concat gnus-agent-directory "lib/categories"))
1097 (list (list 'default 'short nil nil)))))
1099 (defun gnus-category-write ()
1100 "Write the category alist."
1101 (setq gnus-category-predicate-cache nil
1102 gnus-category-group-cache nil)
1103 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1104 (prin1 gnus-category-alist (current-buffer))))
1106 (defun gnus-category-edit-predicate (category)
1107 "Edit the predicate for CATEGORY."
1108 (interactive (list (gnus-category-name)))
1109 (let ((info (assq category gnus-category-alist)))
1111 (cadr info) (format "Editing the predicate for category %s" category)
1112 `(lambda (predicate)
1113 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1114 (gnus-category-write)
1115 (gnus-category-list)))))
1117 (defun gnus-category-edit-score (category)
1118 "Edit the score expression for CATEGORY."
1119 (interactive (list (gnus-category-name)))
1120 (let ((info (assq category gnus-category-alist)))
1123 (format "Editing the score expression for category %s" category)
1125 (setf (caddr (assq ',category gnus-category-alist)) groups)
1126 (gnus-category-write)
1127 (gnus-category-list)))))
1129 (defun gnus-category-edit-groups (category)
1130 "Edit the group list for CATEGORY."
1131 (interactive (list (gnus-category-name)))
1132 (let ((info (assq category gnus-category-alist)))
1134 (cadddr info) (format "Editing the group list for category %s" category)
1136 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1137 (gnus-category-write)
1138 (gnus-category-list)))))
1140 (defun gnus-category-kill (category)
1141 "Kill the current category."
1142 (interactive (list (gnus-category-name)))
1143 (let ((info (assq category gnus-category-alist))
1144 (buffer-read-only nil))
1146 (gnus-category-write)
1147 (setq gnus-category-alist (delq info gnus-category-alist))))
1149 (defun gnus-category-copy (category to)
1150 "Copy the current category."
1151 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1152 (let ((info (assq category gnus-category-alist)))
1153 (push (list to (gnus-copy-sequence (cadr info))
1154 (gnus-copy-sequence (caddr info)) nil)
1155 gnus-category-alist)
1156 (gnus-category-write)
1157 (gnus-category-list)))
1159 (defun gnus-category-add (category)
1160 "Create a new category."
1161 (interactive "SCategory name: ")
1162 (when (assq category gnus-category-alist)
1163 (error "Category %s already exists" category))
1164 (push (list category 'true nil nil)
1165 gnus-category-alist)
1166 (gnus-category-write)
1167 (gnus-category-list))
1169 (defun gnus-category-list ()
1170 "List all categories."
1172 (gnus-category-prepare))
1174 (defun gnus-category-exit ()
1175 "Return to the group buffer."
1177 (kill-buffer (current-buffer))
1178 (gnus-configure-windows 'group t))
1180 ;; To avoid having 8-bit characters in the source file.
1181 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1183 (defvar gnus-category-predicate-alist
1184 '((spam . gnus-agent-spam-p)
1185 (short . gnus-agent-short-p)
1186 (long . gnus-agent-long-p)
1187 (low . gnus-agent-low-scored-p)
1188 (high . gnus-agent-high-scored-p)
1189 (true . gnus-agent-true)
1190 (false . gnus-agent-false))
1191 "Mapping from short score predicate symbols to predicate functions.")
1193 (defun gnus-agent-spam-p ()
1194 "Say whether an article is spam or not."
1195 (unless gnus-agent-spam-hashtb
1196 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1197 (if (not (equal (mail-header-references gnus-headers) ""))
1199 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1201 (gnus-gethash string gnus-agent-spam-hashtb)
1202 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1204 (defun gnus-agent-short-p ()
1205 "Say whether an article is short or not."
1206 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1208 (defun gnus-agent-long-p ()
1209 "Say whether an article is long or not."
1210 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1212 (defun gnus-agent-low-scored-p ()
1213 "Say whether an article has a low score or not."
1214 (< gnus-score gnus-agent-low-score))
1216 (defun gnus-agent-high-scored-p ()
1217 "Say whether an article has a high score or not."
1218 (> gnus-score gnus-agent-high-score))
1220 (defun gnus-category-make-function (cat)
1221 "Make a function from category CAT."
1222 `(lambda () ,(gnus-category-make-function-1 cat)))
1224 (defun gnus-agent-true ()
1228 (defun gnus-agent-false ()
1232 (defun gnus-category-make-function-1 (cat)
1233 "Make a function from category CAT."
1235 ;; Functions are just returned as is.
1237 (gnus-functionp cat))
1238 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1240 ;; More complex category.
1243 ((memq (car cat) '(& and))
1245 ((memq (car cat) '(| or))
1247 ((memq (car cat) gnus-category-not)
1249 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1251 (error "Unknown category type: %s" cat))))
1253 (defun gnus-get-predicate (predicate)
1254 "Return the predicate for CATEGORY."
1255 (or (cdr (assoc predicate gnus-category-predicate-cache))
1256 (cdar (push (cons predicate
1257 (gnus-category-make-function predicate))
1258 gnus-category-predicate-cache))))
1260 (defun gnus-group-category (group)
1261 "Return the category GROUP belongs to."
1262 (unless gnus-category-group-cache
1263 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1264 (let ((cs gnus-category-alist)
1266 (while (setq cat (pop cs))
1267 (setq groups (cadddr cat))
1269 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1270 (or (gnus-gethash group gnus-category-group-cache)
1271 (assq 'default gnus-category-alist)))
1273 (defun gnus-agent-expire ()
1274 "Expire all old articles."
1276 (let ((methods gnus-agent-covered-methods)
1277 (day (- (time-to-day (current-time)) gnus-agent-expire-days))
1278 gnus-command-method sym group articles
1279 history overview file histories elem art nov-file low info
1280 unreads marked article)
1282 (setq overview (gnus-get-buffer-create " *expire overview*"))
1283 (while (setq gnus-command-method (pop methods))
1284 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1285 (gnus-agent-open-history)
1287 (setq gnus-agent-current-history
1288 (setq history (gnus-agent-history-buffer))))
1289 (goto-char (point-min))
1290 (when (> (buffer-size) 1)
1291 (goto-char (point-min))
1293 (skip-chars-forward "^\t")
1294 (if (> (read (current-buffer)) day)
1295 ;; New article; we don't expire it.
1297 ;; Old article. Schedule it for possible nuking.
1299 (setq sym (let ((obarray expiry-hashtb))
1300 (read (current-buffer))))
1302 (set sym (cons (cons (read (current-buffer)) (point))
1303 (symbol-value sym)))
1304 (set sym (list (cons (read (current-buffer)) (point)))))
1305 (skip-chars-forward " "))
1307 ;; We now have all articles that can possibly be expired.
1310 (setq group (symbol-name sym)
1311 articles (sort (symbol-value sym) 'car-less-than-car)
1312 low (car (gnus-active group))
1313 info (gnus-get-info group)
1314 unreads (ignore-errors (gnus-list-of-unread-articles group))
1315 marked (nconc (gnus-uncompress-range
1316 (cdr (assq 'tick (gnus-info-marks info))))
1317 (gnus-uncompress-range
1319 (gnus-info-marks info)))))
1320 nov-file (gnus-agent-article-name ".overview" group))
1321 (gnus-agent-load-alist group)
1322 (gnus-message 5 "Expiring articles in %s" group)
1323 (set-buffer overview)
1325 (when (file-exists-p nov-file)
1326 (nnheader-insert-file-contents nov-file))
1327 (goto-char (point-min))
1329 (while (setq elem (pop articles))
1330 (setq article (car elem))
1331 (when (or (null low)
1333 gnus-agent-expire-all
1334 (and (not (memq article unreads))
1335 (not (memq article marked))))
1336 ;; Find and nuke the NOV line.
1337 (while (and (not (eobp))
1339 (setq art (read (current-buffer)))))
1342 (gnus-agent-article-name
1343 (number-to-string art) group))
1345 ;; Remove old NOV lines that have no articles.
1346 (gnus-delete-line)))
1351 ;; Nuke the article.
1352 (when (file-exists-p (setq file (gnus-agent-article-name
1353 (number-to-string article)
1356 ;; Schedule the history line for nuking.
1357 (push (cdr elem) histories)))
1358 (gnus-make-directory (file-name-directory nov-file))
1359 (write-region (point-min) (point-max) nov-file nil 'silent)
1360 ;; Delete the unwanted entries in the alist.
1361 (setq gnus-agent-article-alist
1362 (sort gnus-agent-article-alist 'car-less-than-car))
1363 (let* ((alist gnus-agent-article-alist)
1364 (prev (cons nil alist))
1367 (<= (caar alist) article))
1368 (if (or (not (cdar alist))
1370 (gnus-agent-article-name
1374 (setcdr prev (setq alist (cdr alist)))
1376 alist (cdr alist))))
1377 (setq gnus-agent-article-alist (cdr first))
1378 ;;; Mark all articles up to the first article
1379 ;;; in `gnus-article-alist' as read.
1380 (when (caar gnus-agent-article-alist)
1381 (setcar (nthcdr 2 info)
1384 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1386 (concat "(gnus-group-set-info '"
1387 (gnus-prin1-to-string info)
1389 (gnus-agent-save-alist group)))
1391 (set-buffer history)
1392 (setq histories (nreverse (sort histories '<)))
1394 (goto-char (pop histories))
1396 (gnus-agent-save-history)
1397 (gnus-agent-close-history))
1398 (gnus-message 4 "Expiry...done"))))))
1401 (defun gnus-agent-batch ()
1403 (let ((init-file-user "")
1404 (gnus-always-read-dribble-file t))
1406 (gnus-group-send-drafts)
1407 (gnus-agent-fetch-session))
1409 (provide 'gnus-agent)
1411 ;;; gnus-agent.el ends here