1 ;;; gnus-agent.el --- unplugged support for Semi-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))
712 ;; To gnus-agent-expire work fine with no Xref field in .overview
713 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
714 (goto-char (point-min))
716 (goto-char (point-at-eol))
719 ;; Tatsuya Ichikawa <ichikawa@hv.epson.co.jp>
720 ;; To gnus-agent-expire work fine with no Xref field in .overview
722 ;; Save these headers for later processing.
723 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
726 (setq file (gnus-agent-article-name ".overview" group)))
727 (gnus-agent-braid-nov group articles file))
728 (gnus-make-directory (nnheader-translate-file-chars
729 (file-name-directory file)))
730 (write-region (point-min) (point-max) file nil 'silent)
731 (gnus-agent-save-alist group articles nil))
734 (defsubst gnus-agent-copy-nov-line (article)
736 (set-buffer gnus-agent-overview-buffer)
738 (if (eq article (read (current-buffer)))
739 (setq e (progn (forward-line 1) (point)))
741 (set-buffer nntp-server-buffer)
742 (insert-buffer-substring gnus-agent-overview-buffer b e)))
744 (defun gnus-agent-braid-nov (group articles file)
746 (set-buffer gnus-agent-overview-buffer)
747 (goto-char (point-min))
748 (set-buffer nntp-server-buffer)
750 (insert-file-contents file)
751 (goto-char (point-min))
752 (if (or (= (point-min) (point-max))
755 (< (read (current-buffer)) (car articles))))
756 ;; We have only headers that are after the older headers,
757 ;; so we just append them.
759 (goto-char (point-max))
760 (insert-buffer-substring gnus-agent-overview-buffer))
761 ;; We do it the hard way.
762 (nnheader-find-nov-line (car articles))
763 (gnus-agent-copy-nov-line (car articles))
767 (while (and (not (eobp))
768 (< (read (current-buffer)) (car articles)))
772 (gnus-agent-copy-nov-line (car articles))
773 (setq articles (cdr articles))))
776 (set-buffer gnus-agent-overview-buffer)
779 (set-buffer nntp-server-buffer)
780 (insert-buffer-substring gnus-agent-overview-buffer b e))))))
782 (defun gnus-agent-load-alist (group &optional dir)
783 "Load the article-state alist for GROUP."
784 (setq gnus-agent-article-alist
785 (gnus-agent-read-file
787 (concat dir ".agentview")
788 (gnus-agent-article-name ".agentview" group)))))
790 (defun gnus-agent-save-alist (group &optional articles state dir)
791 "Load the article-state alist for GROUP."
792 (nnheader-temp-write (if dir
793 (concat dir ".agentview")
794 (gnus-agent-article-name ".agentview" group))
795 (princ (setq gnus-agent-article-alist
796 (nconc gnus-agent-article-alist
797 (mapcar (lambda (article) (cons article state))
802 (defun gnus-agent-article-name (article group)
803 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
804 (if (stringp article) article (string-to-number article))))
807 (defun gnus-agent-batch-fetch ()
808 "Start Gnus and fetch session."
811 (gnus-agent-fetch-session)
814 (defun gnus-agent-fetch-session ()
815 "Fetch all articles and headers that are eligible for fetching."
817 (unless gnus-agent-covered-methods
818 (error "No servers are covered by the Gnus agent"))
820 (error "Can't fetch articles while Gnus is unplugged"))
821 (let ((methods gnus-agent-covered-methods)
822 groups group gnus-command-method)
825 (setq gnus-command-method (car methods)
826 groups (gnus-groups-from-server (pop methods)))
827 (gnus-agent-with-fetch
828 (while (setq group (pop groups))
829 (when (<= (gnus-group-level group) gnus-agent-handle-level)
830 (gnus-agent-fetch-group-1 group gnus-command-method)))))
831 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
833 (defun gnus-agent-fetch-group-1 (group method)
835 (let ((gnus-command-method method)
836 gnus-newsgroup-dependencies gnus-newsgroup-headers
837 gnus-newsgroup-scored gnus-headers gnus-score
838 gnus-use-cache articles score arts
839 category predicate info marks score-param)
841 (when (and (setq articles (gnus-list-of-unread-articles group))
842 (gnus-agent-fetch-headers group articles))
843 ;; Parse them and see which articles we want to fetch.
844 (setq gnus-newsgroup-dependencies
845 (make-vector (length articles) 0))
846 (setq gnus-newsgroup-headers
847 (gnus-get-newsgroup-headers-xover articles nil nil group))
848 (setq category (gnus-group-category group))
851 (or (gnus-group-get-parameter group 'agent-predicate)
854 (or (gnus-group-get-parameter group 'agent-score)
857 (gnus-score-headers (list (list score-param))))
859 (while (setq gnus-headers (pop gnus-newsgroup-headers))
861 (or (cdr (assq (mail-header-number gnus-headers)
862 gnus-newsgroup-scored))
863 gnus-summary-default-score))
864 (when (funcall predicate)
865 (push (mail-header-number gnus-headers)
867 ;; Fetch the articles.
869 (gnus-agent-fetch-articles group arts)))
870 ;; Perhaps we have some additional articles to fetch.
871 (setq arts (assq 'download (gnus-info-marks
872 (setq info (gnus-get-info group)))))
874 (gnus-agent-fetch-articles
875 group (gnus-uncompress-range (cdr arts)))
876 (setq marks (delq arts (gnus-info-marks info)))
877 (gnus-info-set-marks info marks))))
880 ;;; Agent Category Mode
883 (defvar gnus-category-mode-hook nil
884 "Hook run in `gnus-category-mode' buffers.")
886 (defvar gnus-category-line-format " %(%20c%): %g\n"
887 "Format of category lines.")
889 (defvar gnus-category-mode-line-format "Gnus: %%b"
890 "The format specification for the category mode line.")
892 (defvar gnus-agent-short-article 100
893 "Articles that have fewer lines than this are short.")
895 (defvar gnus-agent-long-article 200
896 "Articles that have more lines than this are long.")
898 (defvar gnus-agent-low-score 0
899 "Articles that have a score lower than this have a low score.")
901 (defvar gnus-agent-high-score 0
902 "Articles that have a score higher than this have a high score.")
905 ;;; Internal variables.
907 (defvar gnus-category-buffer "*Agent Category*")
909 (defvar gnus-category-line-format-alist
913 (defvar gnus-category-mode-line-format-alist
914 `((?u user-defined ?s)))
916 (defvar gnus-category-line-format-spec nil)
917 (defvar gnus-category-mode-line-format-spec nil)
919 (defvar gnus-category-mode-map nil)
920 (put 'gnus-category-mode 'mode-class 'special)
922 (unless gnus-category-mode-map
923 (setq gnus-category-mode-map (make-sparse-keymap))
924 (suppress-keymap gnus-category-mode-map)
926 (gnus-define-keys gnus-category-mode-map
927 "q" gnus-category-exit
928 "k" gnus-category-kill
929 "c" gnus-category-copy
930 "a" gnus-category-add
931 "p" gnus-category-edit-predicate
932 "g" gnus-category-edit-groups
933 "s" gnus-category-edit-score
934 "l" gnus-category-list
936 "\C-c\C-i" gnus-info-find-node
937 "\C-c\C-b" gnus-bug))
939 (defvar gnus-category-menu-hook nil
940 "*Hook run after the creation of the menu.")
942 (defun gnus-category-make-menu-bar ()
943 (gnus-turn-off-edit-menu 'category)
944 (unless (boundp 'gnus-category-menu)
946 gnus-category-menu gnus-category-mode-map ""
948 ["Add" gnus-category-add t]
949 ["Kill" gnus-category-kill t]
950 ["Copy" gnus-category-copy t]
951 ["Edit predicate" gnus-category-edit-predicate t]
952 ["Edit score" gnus-category-edit-score t]
953 ["Edit groups" gnus-category-edit-groups t]
954 ["Exit" gnus-category-exit t]))
956 (gnus-run-hooks 'gnus-category-menu-hook)))
958 (defun gnus-category-mode ()
959 "Major mode for listing and editing agent categories.
961 All normal editing commands are switched off.
962 \\<gnus-category-mode-map>
963 For more in-depth information on this mode, read the manual
964 (`\\[gnus-info-find-node]').
966 The following commands are available:
968 \\{gnus-category-mode-map}"
970 (when (gnus-visual-p 'category-menu 'menu)
971 (gnus-category-make-menu-bar))
972 (kill-all-local-variables)
973 (gnus-simplify-mode-line)
974 (setq major-mode 'gnus-category-mode)
975 (setq mode-name "Category")
976 (gnus-set-default-directory)
977 (setq mode-line-process nil)
978 (use-local-map gnus-category-mode-map)
979 (buffer-disable-undo (current-buffer))
980 (setq truncate-lines t)
981 (setq buffer-read-only t)
982 (gnus-run-hooks 'gnus-category-mode-hook))
984 (defalias 'gnus-category-position-point 'gnus-goto-colon)
986 (defun gnus-category-insert-line (category)
987 (let* ((name (car category))
988 (groups (length (cadddr category))))
990 (gnus-add-text-properties
994 (eval gnus-category-line-format-spec))
995 (list 'gnus-category name))))
997 (defun gnus-enter-category-buffer ()
998 "Go to the Category buffer."
1000 (gnus-category-setup-buffer)
1001 (gnus-configure-windows 'category)
1002 (gnus-category-prepare))
1004 (defun gnus-category-setup-buffer ()
1005 (unless (get-buffer gnus-category-buffer)
1007 (set-buffer (get-buffer-create gnus-category-buffer))
1008 (gnus-add-current-to-buffer-list)
1009 (gnus-category-mode))))
1011 (defun gnus-category-prepare ()
1012 (gnus-set-format 'category-mode)
1013 (gnus-set-format 'category t)
1014 (let ((alist gnus-category-alist)
1015 (buffer-read-only nil))
1018 (gnus-category-insert-line (pop alist)))
1019 (goto-char (point-min))
1020 (gnus-category-position-point)))
1022 (defun gnus-category-name ()
1023 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1024 (error "No category on the current line")))
1026 (defun gnus-category-read ()
1027 "Read the category alist."
1028 (setq gnus-category-alist
1029 (or (gnus-agent-read-file
1030 (nnheader-concat gnus-agent-directory "lib/categories"))
1031 (list (list 'default 'short nil nil)))))
1033 (defun gnus-category-write ()
1034 "Write the category alist."
1035 (setq gnus-category-predicate-cache nil
1036 gnus-category-group-cache nil)
1037 (nnheader-temp-write (nnheader-concat gnus-agent-directory "lib/categories")
1038 (prin1 gnus-category-alist (current-buffer))))
1040 (defun gnus-category-edit-predicate (category)
1041 "Edit the predicate for CATEGORY."
1042 (interactive (list (gnus-category-name)))
1043 (let ((info (assq category gnus-category-alist)))
1045 (cadr info) (format "Editing the predicate for category %s" category)
1046 `(lambda (predicate)
1047 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1048 (gnus-category-write)
1049 (gnus-category-list)))))
1051 (defun gnus-category-edit-score (category)
1052 "Edit the score expression for CATEGORY."
1053 (interactive (list (gnus-category-name)))
1054 (let ((info (assq category gnus-category-alist)))
1057 (format "Editing the score expression for category %s" category)
1059 (setf (caddr (assq ',category gnus-category-alist)) groups)
1060 (gnus-category-write)
1061 (gnus-category-list)))))
1063 (defun gnus-category-edit-groups (category)
1064 "Edit the group list for CATEGORY."
1065 (interactive (list (gnus-category-name)))
1066 (let ((info (assq category gnus-category-alist)))
1068 (cadddr info) (format "Editing the group list for category %s" category)
1070 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1071 (gnus-category-write)
1072 (gnus-category-list)))))
1074 (defun gnus-category-kill (category)
1075 "Kill the current category."
1076 (interactive (list (gnus-category-name)))
1077 (let ((info (assq category gnus-category-alist))
1078 (buffer-read-only nil))
1080 (gnus-category-write)
1081 (setq gnus-category-alist (delq info gnus-category-alist))))
1083 (defun gnus-category-copy (category to)
1084 "Copy the current category."
1085 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1086 (let ((info (assq category gnus-category-alist)))
1087 (push (list to (gnus-copy-sequence (cadr info))
1088 (gnus-copy-sequence (caddr info)) nil)
1089 gnus-category-alist)
1090 (gnus-category-write)
1091 (gnus-category-list)))
1093 (defun gnus-category-add (category)
1094 "Create a new category."
1095 (interactive "SCategory name: ")
1096 (when (assq category gnus-category-alist)
1097 (error "Category %s already exists" category))
1098 (push (list category 'true nil nil)
1099 gnus-category-alist)
1100 (gnus-category-write)
1101 (gnus-category-list))
1103 (defun gnus-category-list ()
1104 "List all categories."
1106 (gnus-category-prepare))
1108 (defun gnus-category-exit ()
1109 "Return to the group buffer."
1111 (kill-buffer (current-buffer))
1112 (gnus-configure-windows 'group t))
1114 ;; To avoid having 8-bit characters in the source file.
1115 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1117 (defvar gnus-category-predicate-alist
1118 '((spam . gnus-agent-spam-p)
1119 (short . gnus-agent-short-p)
1120 (long . gnus-agent-long-p)
1121 (low . gnus-agent-low-scored-p)
1122 (high . gnus-agent-high-scored-p)
1123 (true . gnus-agent-true)
1124 (false . gnus-agent-false))
1125 "Mapping from short score predicate symbols to predicate functions.")
1127 (defun gnus-agent-spam-p ()
1128 "Say whether an article is spam or not."
1129 (unless gnus-agent-spam-hashtb
1130 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1131 (if (not (equal (mail-header-references gnus-headers) ""))
1133 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1135 (gnus-gethash string gnus-agent-spam-hashtb)
1136 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1138 (defun gnus-agent-short-p ()
1139 "Say whether an article is short or not."
1140 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1142 (defun gnus-agent-long-p ()
1143 "Say whether an article is long or not."
1144 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1146 (defun gnus-agent-low-scored-p ()
1147 "Say whether an article has a low score or not."
1148 (< gnus-score gnus-agent-low-score))
1150 (defun gnus-agent-high-scored-p ()
1151 "Say whether an article has a high score or not."
1152 (> gnus-score gnus-agent-low-score))
1154 (defun gnus-category-make-function (cat)
1155 "Make a function from category CAT."
1156 `(lambda () ,(gnus-category-make-function-1 cat)))
1158 (defun gnus-agent-true ()
1162 (defun gnus-agent-false ()
1166 (defun gnus-category-make-function-1 (cat)
1167 "Make a function from category CAT."
1169 ;; Functions are just returned as is.
1171 (gnus-functionp cat))
1172 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1174 ;; More complex category.
1177 ((memq (car cat) '(& and))
1179 ((memq (car cat) '(| or))
1181 ((memq (car cat) gnus-category-not)
1183 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1185 (error "Unknown category type: %s" cat))))
1187 (defun gnus-get-predicate (predicate)
1188 "Return the predicate for CATEGORY."
1189 (or (cdr (assoc predicate gnus-category-predicate-cache))
1190 (cdar (push (cons predicate
1191 (gnus-category-make-function predicate))
1192 gnus-category-predicate-cache))))
1194 (defun gnus-group-category (group)
1195 "Return the category GROUP belongs to."
1196 (unless gnus-category-group-cache
1197 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1198 (let ((cs gnus-category-alist)
1200 (while (setq cat (pop cs))
1201 (setq groups (cadddr cat))
1203 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1204 (or (gnus-gethash group gnus-category-group-cache)
1205 (assq 'default gnus-category-alist)))
1207 (defun gnus-agent-expire ()
1208 "Expire all old articles."
1210 (let ((methods gnus-agent-covered-methods)
1211 (day (- (gnus-time-to-day (current-time)) gnus-agent-expire-days))
1212 (expiry-hashtb (gnus-make-hashtable 1023))
1213 gnus-command-method sym group articles
1214 history overview file histories elem art nov-file low info
1215 unreads marked article)
1217 (setq overview (get-buffer-create " *expire overview*"))
1218 (while (setq gnus-command-method (pop methods))
1219 (gnus-agent-open-history)
1221 (setq gnus-agent-current-history
1222 (setq history (gnus-agent-history-buffer))))
1223 (goto-char (point-min))
1225 (skip-chars-forward "^\t")
1226 (if (> (read (current-buffer)) day)
1227 ;; New article; we don't expire it.
1229 ;; Old article. Schedule it for possible nuking.
1231 (setq sym (let ((obarray expiry-hashtb))
1232 (read (current-buffer))))
1234 (set sym (cons (cons (read (current-buffer)) (point))
1235 (symbol-value sym)))
1236 (set sym (list (cons (read (current-buffer)) (point)))))
1237 (skip-chars-forward " "))
1239 ;; We now have all articles that can possibly be expired.
1242 (setq group (symbol-name sym)
1243 articles (sort (symbol-value sym) 'car-less-than-car)
1244 low (car (gnus-active group))
1245 info (gnus-get-info group)
1246 unreads (ignore-errors (gnus-list-of-unread-articles group))
1247 marked (nconc (gnus-uncompress-range
1248 (cdr (assq 'ticked (gnus-info-marks info))))
1249 (gnus-uncompress-range
1250 (cdr (assq 'dormant (gnus-info-marks info)))))
1251 nov-file (gnus-agent-article-name ".overview" group))
1252 (gnus-message 5 "Expiring articles in %s" group)
1253 (set-buffer overview)
1255 (when (file-exists-p nov-file)
1256 (insert-file-contents nov-file))
1257 (goto-char (point-min))
1258 (while (setq elem (pop articles))
1259 (setq article (car elem))
1260 (when (or (null low)
1262 (and (not (memq article unreads))
1263 (not (memq article marked))))
1264 ;; Find and nuke the NOV line.
1265 (while (and (not (eobp))
1266 (< (setq art (read (current-buffer))) article))
1272 ;; Nuke the article.
1273 (when (file-exists-p (setq file (gnus-agent-article-name
1274 (number-to-string article)
1277 ;; Schedule the history line for nuking.
1278 (push (cdr elem) histories)))
1279 (write-region (point-min) (point-max) nov-file nil 'silent))
1281 (set-buffer history)
1282 (setq histories (nreverse (sort histories '<)))
1284 (goto-char (pop histories))
1286 (gnus-agent-save-history)
1287 (gnus-agent-close-history)))))
1290 (defun gnus-agent-batch ()
1292 (let ((init-file-user "")
1293 (gnus-always-read-dribble-file t))
1295 (gnus-group-send-drafts)
1296 (gnus-agent-fetch-session))
1298 (provide 'gnus-agent)
1300 ;;; gnus-agent.el ends here