1 ;;; nnkiboze.el --- select virtual news access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The other access methods (nntp, nnspool, etc) are general news
29 ;; access methods. This module relies on Gnus and can't be used
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
43 (nnoo-declare nnkiboze)
44 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
45 "nnkiboze will put its files in this directory.")
47 (defvoo nnkiboze-level 9
48 "The maximum level to be searched for articles.")
50 (defvoo nnkiboze-remove-read-articles t
51 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
53 (defvoo nnkiboze-ephemeral nil
54 "If non-nil, don't store any data anywhere.")
56 (defvoo nnkiboze-scores nil
57 "Score rules for generating the nnkiboze group.")
59 (defvoo nnkiboze-regexp nil
60 "Regexp for matching component groups.")
62 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
63 "Coding system for nnkiboze files.")
67 (defconst nnkiboze-version "nnkiboze 1.0")
69 (defvoo nnkiboze-current-group nil)
70 (defvoo nnkiboze-status-string "")
72 (defvoo nnkiboze-headers nil)
76 ;;; Interface functions.
78 (nnoo-define-basics nnkiboze)
80 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
81 (nnkiboze-possibly-change-group group)
82 (unless gnus-nov-is-evil
83 (if (stringp (car articles))
85 (let ((nov (nnkiboze-nov-file-name)))
86 (when (file-exists-p nov)
88 (set-buffer nntp-server-buffer)
90 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
91 (nnheader-insert-file-contents nov))
92 (nnheader-nov-delete-outside-range
93 (car articles) (car (last articles)))
96 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
97 (nnkiboze-possibly-change-group newsgroup)
98 (if (not (numberp article))
99 ;; This is a real kludge. It might not work at times, but it
100 ;; does no harm I think. The only alternative is to offer no
101 ;; article fetching by message-id at all.
102 (nntp-request-article article newsgroup gnus-nntp-server buffer)
103 (let* ((header (gnus-summary-article-header article))
104 (xref (mail-header-xref header))
107 (error "nnkiboze: No xref"))
108 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
109 (error "nnkiboze: Malformed xref"))
110 (setq num (string-to-int (match-string 2 xref))
111 group (match-string 1 xref))
112 (or (with-current-buffer buffer
113 (or (gnus-cache-request-article num group)
114 (gnus-agent-request-article num group)))
115 (gnus-request-article num group buffer)))))
117 (deffoo nnkiboze-request-scan (&optional group server)
118 (nnkiboze-generate-group (concat "nnkiboze:" group)))
120 (deffoo nnkiboze-request-group (group &optional server dont-check)
121 "Make GROUP the current newsgroup."
122 (nnkiboze-possibly-change-group group)
125 (let ((nov-file (nnkiboze-nov-file-name))
128 (set-buffer nntp-server-buffer)
130 (unless (file-exists-p nov-file)
131 (nnkiboze-request-scan group))
132 (if (not (file-exists-p nov-file))
133 (nnheader-report 'nnkiboze "Can't select group %s" group)
134 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
135 (nnheader-insert-file-contents nov-file))
136 (if (zerop (buffer-size))
137 (nnheader-insert "211 0 0 0 %s\n" group)
138 (goto-char (point-min))
139 (when (looking-at "[0-9]+")
140 (setq beg (read (current-buffer))))
141 (goto-char (point-max))
142 (when (re-search-backward "^[0-9]" nil t)
143 (setq end (read (current-buffer))))
144 (setq total (count-lines (point-min) (point-max)))
145 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
147 (deffoo nnkiboze-close-group (group &optional server)
148 (nnkiboze-possibly-change-group group)
149 ;; Remove NOV lines of articles that are marked as read.
150 (when (and (file-exists-p (nnkiboze-nov-file-name))
151 nnkiboze-remove-read-articles)
152 (let ((coding-system-for-write nnkiboze-file-coding-system)
153 (output-coding-system nnkiboze-file-coding-system))
154 (with-temp-file (nnkiboze-nov-file-name)
155 (let ((cur (current-buffer))
156 (nnheader-file-coding-system nnkiboze-file-coding-system))
157 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
158 (goto-char (point-min))
160 (if (not (gnus-article-read-p (read cur)))
162 (gnus-delete-line))))))
163 (setq nnkiboze-current-group nil)))
165 (deffoo nnkiboze-open-server (server &optional defs)
166 (unless (assq 'nnkiboze-regexp defs)
167 (push `(nnkiboze-regexp ,server)
169 (nnoo-change-server 'nnkiboze server defs))
171 (deffoo nnkiboze-request-delete-group (group &optional force server)
172 (nnkiboze-possibly-change-group group)
175 (nnkiboze-score-file group)
176 (list (nnkiboze-nov-file-name)
177 (nnkiboze-nov-file-name ".newsrc")))))
179 (and (file-exists-p (car files))
180 (file-writable-p (car files))
181 (delete-file (car files)))
182 (setq files (cdr files)))))
183 (setq nnkiboze-current-group nil)
186 (nnoo-define-skeleton nnkiboze)
189 ;;; Internal functions.
191 (defun nnkiboze-possibly-change-group (group)
192 (setq nnkiboze-current-group group))
194 (defun nnkiboze-prefixed-name (group)
195 (gnus-group-prefixed-name group '(nnkiboze "")))
198 (defun nnkiboze-generate-groups ()
199 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
200 Finds out what articles are to be part of the nnkiboze groups."
202 (let ((nnmail-spool-file nil)
204 (gnus-use-dribble-file nil)
205 (gnus-read-active-file t)
206 (gnus-expert-user t))
208 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
209 (newsrc (cdr gnus-newsrc-alist))
210 gnus-newsrc-hashtb info)
211 (gnus-make-hashtable-from-newsrc-alist)
212 ;; We have copied all the newsrc alist info over to local copies
213 ;; so that we can mess all we want with these lists.
214 (while (setq info (pop newsrc))
215 (when (string-match "nnkiboze" (gnus-info-group info))
216 ;; For each kiboze group, we call this function to generate
218 (nnkiboze-generate-group (gnus-info-group info) t))))
220 (set-buffer gnus-group-buffer)
221 (gnus-group-list-groups)))
223 (defun nnkiboze-score-file (group)
224 (list (expand-file-name
225 (concat (file-name-as-directory gnus-kill-files-directory)
226 (nnheader-translate-file-chars
227 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
228 "." gnus-score-file-suffix))))))
230 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
231 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
232 (newsrc-file (concat nnkiboze-directory
233 (nnheader-translate-file-chars
234 (concat group ".newsrc"))))
235 (nov-file (concat nnkiboze-directory
236 (nnheader-translate-file-chars
237 (concat group ".nov"))))
238 method nnkiboze-newsrc gname newsrc active
239 ginfo lowest glevel orig-info nov-buffer
240 ;; Bind various things to nil to make group entry faster.
242 (gnus-large-newsgroup nil)
243 (gnus-score-find-score-files-function 'nnkiboze-score-file)
244 ;; Use only nnkiboze-score-file!
245 (gnus-score-use-all-scores nil)
247 (gnus-verbose (min gnus-verbose 3))
248 gnus-select-group-hook gnus-summary-prepare-hook
249 gnus-thread-sort-functions gnus-show-threads
250 gnus-visual gnus-suppress-duplicates num-unread)
252 (error "No such group: %s" group))
253 ;; Load the kiboze newsrc file for this group.
254 (when (file-exists-p newsrc-file)
256 (let ((coding-system-for-write nnkiboze-file-coding-system)
257 (output-coding-system nnkiboze-file-coding-system))
258 (gnus-make-directory (file-name-directory nov-file))
259 (with-temp-file nov-file
260 (when (file-exists-p nov-file)
261 (nnheader-insert-file-contents nov-file))
262 (setq nov-buffer (current-buffer))
263 ;; Go through the active hashtb and add new all groups that match the
267 (and (string-match nnkiboze-regexp
268 (setq gname (symbol-name group))) ; Match
269 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
270 (numberp (car (symbol-value group))) ; It is active
271 (or (> nnkiboze-level 7)
273 (nth 1 (nth 2 (gnus-gethash
274 gname gnus-newsrc-hashtb))))
275 (>= nnkiboze-level glevel)))
276 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
277 (push (cons gname (1- (car (symbol-value group))))
280 ;; `newsrc' is set to the list of groups that possibly are
281 ;; component groups to this kiboze group. This list has elements
282 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
283 ;; number that has been kibozed in GROUP in this kiboze group.
284 (setq newsrc nnkiboze-newsrc)
286 (if (not (setq active (gnus-gethash
287 (caar newsrc) gnus-active-hashtb)))
288 ;; This group isn't active after all, so we remove it from
289 ;; the list of component groups.
290 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
291 (setq lowest (cdar newsrc))
292 ;; Ok, we have a valid component group, so we jump to it.
293 (switch-to-buffer gnus-group-buffer)
294 (gnus-group-jump-to-group (caar newsrc))
295 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
296 (setq ginfo (gnus-get-info (gnus-group-group-name))
297 orig-info (gnus-copy-sequence ginfo)
298 num-unread (car (gnus-gethash (caar newsrc)
299 gnus-newsrc-hashtb)))
302 ;; We set all list of article marks to nil. Since we operate
303 ;; on copies of the real lists, we can destroy anything we
306 (setcar (nthcdr 3 ginfo) nil))
307 ;; We set the list of read articles to be what we expect for
308 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
310 (setcar (nthcdr 2 ginfo)
311 (and (not (= lowest 1)) (cons 1 lowest))))
312 (when (and (or (not ginfo)
313 (> (length (gnus-list-of-unread-articles
318 (gnus-group-select-group nil))
319 (eq major-mode 'gnus-summary-mode)))
320 ;; We are now in the group where we want to be.
321 (setq method (gnus-find-method-for-group
322 gnus-newsgroup-name))
323 (when (eq method gnus-select-method)
325 ;; We go through the list of scored articles.
326 (while gnus-newsgroup-scored
327 (when (> (caar gnus-newsgroup-scored) lowest)
328 ;; If it has a good score, then we enter this article
329 ;; into the kiboze group.
332 (gnus-summary-article-header
333 (caar gnus-newsgroup-scored))
334 gnus-newsgroup-name))
335 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
336 ;; That's it. We exit this group.
337 (when (eq major-mode 'gnus-summary-mode)
338 (kill-buffer (current-buffer)))))
339 ;; Restore the proper info.
341 (setcdr ginfo (cdr orig-info)))
342 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
344 (setcdr (car newsrc) (car active))
345 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
346 (setq newsrc (cdr newsrc)))))
347 ;; We save the kiboze newsrc for this group.
348 (gnus-make-directory (file-name-directory newsrc-file))
349 (with-temp-file newsrc-file
350 (insert "(setq nnkiboze-newsrc '")
351 (gnus-prin1 nnkiboze-newsrc)
353 (unless inhibit-list-groups
355 (set-buffer gnus-group-buffer)
356 (gnus-group-list-groups)))
359 (defun nnkiboze-enter-nov (buffer header group)
362 (goto-char (point-max))
363 (let ((prefix (gnus-group-real-prefix group))
364 (oheader (copy-sequence header))
366 (if (zerop (forward-line -1))
368 (setq article (1+ (read (current-buffer))))
371 (mail-header-set-number oheader article)
373 (insert (or (mail-header-xref oheader) ""))
374 (goto-char (point-min))
375 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
376 (goto-char (match-beginning 0))
377 (or (eobp) (forward-char 1)))
378 ;; The first Xref has to be the group this article
379 ;; really came for - this is the article nnkiboze
380 ;; will request when it is asked for the article.
381 (insert " " group ":"
382 (int-to-string (mail-header-number header)) " ")
383 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
384 (goto-char (1+ (match-beginning 0)))
386 (mail-header-set-xref oheader (buffer-string)))
387 (nnheader-insert-nov oheader))))
389 (defun nnkiboze-nov-file-name (&optional suffix)
390 (concat (file-name-as-directory nnkiboze-directory)
391 (nnheader-translate-file-chars
392 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
393 (or suffix ".nov")))))
397 ;;; nnkiboze.el ends here