1 ;;; nnkiboze.el --- select virtual news access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; The other access methods (nntp, nnspool, etc) are general news
27 ;; access methods. This module relies on Gnus and can't be used
37 (eval-when-compile (require 'cl))
39 (nnoo-declare nnkiboze)
40 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
41 "nnkiboze will put its files in this directory.")
43 (defvoo nnkiboze-level 9
44 "The maximum level to be searched for articles.")
46 (defvoo nnkiboze-remove-read-articles t
47 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
49 (defvoo nnkiboze-ephemeral nil
50 "If non-nil, don't store any data anywhere.")
52 (defvoo nnkiboze-scores nil
53 "Score rules for generating the nnkiboze group.")
55 (defvoo nnkiboze-regexp nil
56 "Regexp for matching component groups.")
60 (defconst nnkiboze-version "nnkiboze 1.0")
62 (defvoo nnkiboze-current-group nil)
63 (defvoo nnkiboze-status-string "")
65 (defvoo nnkiboze-headers nil)
69 ;;; Interface functions.
71 (nnoo-define-basics nnkiboze)
73 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
74 (nnkiboze-possibly-change-group group)
75 (unless gnus-nov-is-evil
76 (if (stringp (car articles))
78 (let ((nov (nnkiboze-nov-file-name)))
79 (when (file-exists-p nov)
81 (set-buffer nntp-server-buffer)
83 (nnheader-insert-file-contents nov)
84 (nnheader-nov-delete-outside-range
85 (car articles) (car (last articles)))
88 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
89 (nnkiboze-possibly-change-group newsgroup)
90 (if (not (numberp article))
91 ;; This is a real kludge. It might not work at times, but it
92 ;; does no harm I think. The only alternative is to offer no
93 ;; article fetching by message-id at all.
94 (nntp-request-article article newsgroup gnus-nntp-server buffer)
95 (let* ((header (gnus-summary-article-header article))
96 (xref (mail-header-xref header)))
98 (error "nnkiboze: No xref"))
99 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
100 (error "nnkiboze: Malformed xref"))
101 (gnus-request-article (string-to-int (match-string 2 xref))
102 (match-string 1 xref)
105 (deffoo nnkiboze-request-scan (&optional group server)
106 (nnkiboze-generate-group (concat "nnkiboze:" group)))
108 (deffoo nnkiboze-request-group (group &optional server dont-check)
109 "Make GROUP the current newsgroup."
110 (nnkiboze-possibly-change-group group)
113 (let ((nov-file (nnkiboze-nov-file-name))
116 (set-buffer nntp-server-buffer)
118 (unless (file-exists-p nov-file)
119 (nnkiboze-request-scan group))
120 (if (not (file-exists-p nov-file))
121 (nnheader-report 'nnkiboze "Can't select group %s" group)
122 (nnheader-insert-file-contents nov-file)
123 (if (zerop (buffer-size))
124 (nnheader-insert "211 0 0 0 %s\n" group)
125 (goto-char (point-min))
126 (when (looking-at "[0-9]+")
127 (setq beg (read (current-buffer))))
128 (goto-char (point-max))
129 (when (re-search-backward "^[0-9]" nil t)
130 (setq end (read (current-buffer))))
131 (setq total (count-lines (point-min) (point-max)))
132 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
134 (deffoo nnkiboze-close-group (group &optional server)
135 (nnkiboze-possibly-change-group group)
136 ;; Remove NOV lines of articles that are marked as read.
137 (when (and (file-exists-p (nnkiboze-nov-file-name))
138 nnkiboze-remove-read-articles)
139 (nnheader-temp-write (nnkiboze-nov-file-name)
140 (let ((cur (current-buffer)))
141 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
142 (goto-char (point-min))
144 (if (not (gnus-article-read-p (read cur)))
146 (gnus-delete-line))))))
147 (setq nnkiboze-current-group nil))
149 (deffoo nnkiboze-open-server (server &optional defs)
150 (unless (assq 'nnkiboze-regexp defs)
151 (push `(nnkiboze-regexp ,server)
153 (nnoo-change-server 'nnkiboze server defs))
155 (deffoo nnkiboze-request-delete-group (group &optional force server)
156 (nnkiboze-possibly-change-group group)
159 (nnkiboze-score-file group)
160 (list (nnkiboze-nov-file-name)
161 (concat nnkiboze-directory
162 (nnheader-translate-file-chars
163 (concat group ".newsrc")))))))
165 (and (file-exists-p (car files))
166 (file-writable-p (car files))
167 (delete-file (car files)))
168 (setq files (cdr files)))))
169 (setq nnkiboze-current-group nil)
172 (nnoo-define-skeleton nnkiboze)
175 ;;; Internal functions.
177 (defun nnkiboze-possibly-change-group (group)
178 (setq nnkiboze-current-group group))
180 (defun nnkiboze-prefixed-name (group)
181 (gnus-group-prefixed-name group '(nnkiboze "")))
184 (defun nnkiboze-generate-groups ()
185 "Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups
186 Finds out what articles are to be part of the nnkiboze groups."
188 (let ((nnmail-spool-file nil)
189 (gnus-use-dribble-file nil)
190 (gnus-read-active-file t)
191 (gnus-expert-user t))
193 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
194 (newsrc (cdr gnus-newsrc-alist))
195 gnus-newsrc-hashtb info)
196 (gnus-make-hashtable-from-newsrc-alist)
197 ;; We have copied all the newsrc alist info over to local copies
198 ;; so that we can mess all we want with these lists.
199 (while (setq info (pop newsrc))
200 (when (string-match "nnkiboze" (gnus-info-group info))
201 ;; For each kiboze group, we call this function to generate
203 (nnkiboze-generate-group (gnus-info-group info))))))
205 (defun nnkiboze-score-file (group)
206 (list (expand-file-name
207 (concat (file-name-as-directory gnus-kill-files-directory)
208 (nnheader-translate-file-chars
209 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
210 "." gnus-score-file-suffix))))))
212 (defun nnkiboze-generate-group (group)
213 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
214 (newsrc-file (concat nnkiboze-directory
215 (nnheader-translate-file-chars
216 (concat group ".newsrc"))))
217 (nov-file (concat nnkiboze-directory
218 (nnheader-translate-file-chars
219 (concat group ".nov"))))
220 method nnkiboze-newsrc gname newsrc active
221 ginfo lowest glevel orig-info nov-buffer
222 ;; Bind various things to nil to make group entry faster.
224 (gnus-large-newsgroup nil)
225 (gnus-score-find-score-files-function 'nnkiboze-score-file)
226 (gnus-verbose (min gnus-verbose 3))
227 gnus-select-group-hook gnus-summary-prepare-hook
228 gnus-thread-sort-functions gnus-show-threads
229 gnus-visual gnus-suppress-duplicates num-unread)
231 (error "No such group: %s" group))
232 ;; Load the kiboze newsrc file for this group.
233 (when (file-exists-p newsrc-file)
235 (nnheader-temp-write nov-file
236 (when (file-exists-p nov-file)
237 (nnheader-insert-file-contents nov-file))
238 (setq nov-buffer (current-buffer))
239 ;; Go through the active hashtb and add new all groups that match the
243 (and (string-match nnkiboze-regexp
244 (setq gname (symbol-name group))) ; Match
245 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
246 (numberp (car (symbol-value group))) ; It is active
247 (or (> nnkiboze-level 7)
248 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
249 gname gnus-newsrc-hashtb))))
250 (>= nnkiboze-level glevel)))
251 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
252 (push (cons gname (1- (car (symbol-value group))))
255 ;; `newsrc' is set to the list of groups that possibly are
256 ;; component groups to this kiboze group. This list has elements
257 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
258 ;; number that has been kibozed in GROUP in this kiboze group.
259 (setq newsrc nnkiboze-newsrc)
261 (if (not (setq active (gnus-gethash
262 (caar newsrc) gnus-active-hashtb)))
263 ;; This group isn't active after all, so we remove it from
264 ;; the list of component groups.
265 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
266 (setq lowest (cdar newsrc))
267 ;; Ok, we have a valid component group, so we jump to it.
268 (switch-to-buffer gnus-group-buffer)
269 (gnus-group-jump-to-group (caar newsrc))
270 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
271 (setq ginfo (gnus-get-info (gnus-group-group-name))
272 orig-info (gnus-copy-sequence ginfo)
273 num-unread (car (gnus-gethash (caar newsrc)
274 gnus-newsrc-hashtb)))
277 ;; We set all list of article marks to nil. Since we operate
278 ;; on copies of the real lists, we can destroy anything we
281 (setcar (nthcdr 3 ginfo) nil))
282 ;; We set the list of read articles to be what we expect for
283 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
285 (setcar (nthcdr 2 ginfo)
286 (and (not (= lowest 1)) (cons 1 lowest))))
287 (when (and (or (not ginfo)
288 (> (length (gnus-list-of-unread-articles
293 (gnus-group-select-group nil))
294 (eq major-mode 'gnus-summary-mode)))
295 ;; We are now in the group where we want to be.
296 (setq method (gnus-find-method-for-group
297 gnus-newsgroup-name))
298 (when (eq method gnus-select-method)
300 ;; We go through the list of scored articles.
301 (while gnus-newsgroup-scored
302 (when (> (caar gnus-newsgroup-scored) lowest)
303 ;; If it has a good score, then we enter this article
304 ;; into the kiboze group.
307 (gnus-summary-article-header
308 (caar gnus-newsgroup-scored))
309 gnus-newsgroup-name))
310 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
311 ;; That's it. We exit this group.
312 (when (eq major-mode 'gnus-summary-mode)
313 (kill-buffer (current-buffer)))))
314 ;; Restore the proper info.
316 (setcdr ginfo (cdr orig-info)))
317 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
319 (setcdr (car newsrc) (car active))
320 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
321 (setq newsrc (cdr newsrc))))
322 ;; We save the kiboze newsrc for this group.
323 (nnheader-temp-write newsrc-file
324 (insert "(setq nnkiboze-newsrc '")
325 (gnus-prin1 nnkiboze-newsrc)
328 (set-buffer gnus-group-buffer)
329 (gnus-group-list-groups))
332 (defun nnkiboze-enter-nov (buffer header group)
335 (goto-char (point-max))
336 (let ((xref (mail-header-xref header))
337 (prefix (gnus-group-real-prefix group))
338 (oheader (copy-sequence header))
341 (if (zerop (forward-line -1))
343 (setq article (1+ (read (current-buffer))))
346 (mail-header-set-number oheader article)
347 (nnheader-insert-nov oheader)
348 (search-backward "\t" nil t 2)
349 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
350 (goto-char (match-beginning 0))
352 ;; The first Xref has to be the group this article
353 ;; really came for - this is the article nnkiboze
354 ;; will request when it is asked for the article.
355 (insert " " group ":"
356 (int-to-string (mail-header-number header)) " ")
357 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
358 (goto-char (1+ (match-beginning 0)))
361 (defun nnkiboze-nov-file-name ()
362 (concat (file-name-as-directory nnkiboze-directory)
363 (nnheader-translate-file-chars
364 (concat (nnkiboze-prefixed-name nnkiboze-current-group) ".nov"))))
368 ;;; nnkiboze.el ends here