1 ;;; nnkiboze.el --- select virtual news access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 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
32 (eval-when-compile (require 'cl))
33 (eval-when-compile (require 'gnus-clfns))
41 (nnoo-declare nnkiboze)
42 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
43 "nnkiboze will put its files in this directory.")
45 (defvoo nnkiboze-level 9
46 "The maximum level to be searched for articles.")
48 (defvoo nnkiboze-remove-read-articles t
49 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
51 (defvoo nnkiboze-ephemeral nil
52 "If non-nil, don't store any data anywhere.")
54 (defvoo nnkiboze-scores nil
55 "Score rules for generating the nnkiboze group.")
57 (defvoo nnkiboze-regexp nil
58 "Regexp for matching component groups.")
60 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
61 "Coding system for nnkiboze files.")
65 (defconst nnkiboze-version "nnkiboze 1.0")
67 (defvoo nnkiboze-current-group nil)
68 (defvoo nnkiboze-status-string "")
70 (defvoo nnkiboze-headers nil)
74 ;;; Interface functions.
76 (nnoo-define-basics nnkiboze)
78 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
79 (nnkiboze-possibly-change-group group)
80 (unless gnus-nov-is-evil
81 (if (stringp (car articles))
83 (let ((nov (nnkiboze-nov-file-name)))
84 (when (file-exists-p nov)
86 (set-buffer nntp-server-buffer)
88 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
89 (nnheader-insert-file-contents nov))
90 (nnheader-nov-delete-outside-range
91 (car articles) (car (last articles)))
94 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
95 (nnkiboze-possibly-change-group newsgroup)
96 (if (not (numberp article))
97 ;; This is a real kludge. It might not work at times, but it
98 ;; does no harm I think. The only alternative is to offer no
99 ;; article fetching by message-id at all.
100 (nntp-request-article article newsgroup gnus-nntp-server buffer)
101 (let* ((header (gnus-summary-article-header article))
102 (xref (mail-header-xref header))
105 (error "nnkiboze: No xref"))
106 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
107 (error "nnkiboze: Malformed xref"))
108 (setq num (string-to-int (match-string 2 xref))
109 group (match-string 1 xref))
110 (or (with-current-buffer buffer
111 (gnus-cache-request-article num group))
112 (gnus-request-article num group buffer)))))
114 (deffoo nnkiboze-request-scan (&optional group server)
115 (nnkiboze-generate-group (concat "nnkiboze:" group)))
117 (deffoo nnkiboze-request-group (group &optional server dont-check)
118 "Make GROUP the current newsgroup."
119 (nnkiboze-possibly-change-group group)
122 (let ((nov-file (nnkiboze-nov-file-name))
125 (set-buffer nntp-server-buffer)
127 (unless (file-exists-p nov-file)
128 (nnkiboze-request-scan group))
129 (if (not (file-exists-p nov-file))
130 (nnheader-report 'nnkiboze "Can't select group %s" group)
131 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
132 (nnheader-insert-file-contents nov-file))
133 (if (zerop (buffer-size))
134 (nnheader-insert "211 0 0 0 %s\n" group)
135 (goto-char (point-min))
136 (when (looking-at "[0-9]+")
137 (setq beg (read (current-buffer))))
138 (goto-char (point-max))
139 (when (re-search-backward "^[0-9]" nil t)
140 (setq end (read (current-buffer))))
141 (setq total (count-lines (point-min) (point-max)))
142 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
144 (deffoo nnkiboze-close-group (group &optional server)
145 (nnkiboze-possibly-change-group group)
146 ;; Remove NOV lines of articles that are marked as read.
147 (when (and (file-exists-p (nnkiboze-nov-file-name))
148 nnkiboze-remove-read-articles)
149 (let ((coding-system-for-write nnkiboze-file-coding-system)
150 (output-coding-system nnkiboze-file-coding-system))
151 (with-temp-file (nnkiboze-nov-file-name)
152 (let ((cur (current-buffer))
153 (nnheader-file-coding-system nnkiboze-file-coding-system))
154 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
155 (goto-char (point-min))
157 (if (not (gnus-article-read-p (read cur)))
159 (gnus-delete-line))))))
160 (setq nnkiboze-current-group nil)))
162 (deffoo nnkiboze-open-server (server &optional defs)
163 (unless (assq 'nnkiboze-regexp defs)
164 (push `(nnkiboze-regexp ,server)
166 (nnoo-change-server 'nnkiboze server defs))
168 (deffoo nnkiboze-request-delete-group (group &optional force server)
169 (nnkiboze-possibly-change-group group)
172 (nnkiboze-score-file group)
173 (list (nnkiboze-nov-file-name)
174 (nnkiboze-nov-file-name ".newsrc")))))
176 (and (file-exists-p (car files))
177 (file-writable-p (car files))
178 (delete-file (car files)))
179 (setq files (cdr files)))))
180 (setq nnkiboze-current-group nil)
183 (nnoo-define-skeleton nnkiboze)
186 ;;; Internal functions.
188 (defun nnkiboze-possibly-change-group (group)
189 (setq nnkiboze-current-group group))
191 (defun nnkiboze-prefixed-name (group)
192 (gnus-group-prefixed-name group '(nnkiboze "")))
195 (defun nnkiboze-generate-groups ()
196 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
197 Finds out what articles are to be part of the nnkiboze groups."
199 (let ((nnmail-spool-file nil)
201 (gnus-use-dribble-file nil)
202 (gnus-read-active-file t)
203 (gnus-expert-user t))
205 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
206 (newsrc (cdr gnus-newsrc-alist))
207 gnus-newsrc-hashtb info)
208 (gnus-make-hashtable-from-newsrc-alist)
209 ;; We have copied all the newsrc alist info over to local copies
210 ;; so that we can mess all we want with these lists.
211 (while (setq info (pop newsrc))
212 (when (string-match "nnkiboze" (gnus-info-group info))
213 ;; For each kiboze group, we call this function to generate
215 (nnkiboze-generate-group (gnus-info-group info) t))))
217 (set-buffer gnus-group-buffer)
218 (gnus-group-list-groups)))
220 (defun nnkiboze-score-file (group)
221 (list (expand-file-name
222 (concat (file-name-as-directory gnus-kill-files-directory)
223 (nnheader-translate-file-chars
224 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
225 "." gnus-score-file-suffix))))))
227 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
228 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
229 (newsrc-file (concat nnkiboze-directory
230 (nnheader-translate-file-chars
231 (concat group ".newsrc"))))
232 (nov-file (concat nnkiboze-directory
233 (nnheader-translate-file-chars
234 (concat group ".nov"))))
235 method nnkiboze-newsrc gname newsrc active
236 ginfo lowest glevel orig-info nov-buffer
237 ;; Bind various things to nil to make group entry faster.
239 (gnus-large-newsgroup nil)
240 (gnus-score-find-score-files-function 'nnkiboze-score-file)
241 ;; Use only nnkiboze-score-file!
242 (gnus-score-use-all-scores nil)
244 (gnus-verbose (min gnus-verbose 3))
245 gnus-select-group-hook gnus-summary-prepare-hook
246 gnus-thread-sort-functions gnus-show-threads
247 gnus-visual gnus-suppress-duplicates num-unread)
249 (error "No such group: %s" group))
250 ;; Load the kiboze newsrc file for this group.
251 (when (file-exists-p newsrc-file)
253 (let ((coding-system-for-write nnkiboze-file-coding-system)
254 (output-coding-system nnkiboze-file-coding-system))
255 (with-temp-file nov-file
256 (when (file-exists-p nov-file)
257 (nnheader-insert-file-contents nov-file))
258 (setq nov-buffer (current-buffer))
259 ;; Go through the active hashtb and add new all groups that match the
263 (and (string-match nnkiboze-regexp
264 (setq gname (symbol-name group))) ; Match
265 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
266 (numberp (car (symbol-value group))) ; It is active
267 (or (> nnkiboze-level 7)
268 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
269 gname gnus-newsrc-hashtb))))
270 (>= nnkiboze-level glevel)))
271 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
272 (push (cons gname (1- (car (symbol-value group))))
275 ;; `newsrc' is set to the list of groups that possibly are
276 ;; component groups to this kiboze group. This list has elements
277 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
278 ;; number that has been kibozed in GROUP in this kiboze group.
279 (setq newsrc nnkiboze-newsrc)
281 (if (not (setq active (gnus-gethash
282 (caar newsrc) gnus-active-hashtb)))
283 ;; This group isn't active after all, so we remove it from
284 ;; the list of component groups.
285 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
286 (setq lowest (cdar newsrc))
287 ;; Ok, we have a valid component group, so we jump to it.
288 (switch-to-buffer gnus-group-buffer)
289 (gnus-group-jump-to-group (caar newsrc))
290 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
291 (setq ginfo (gnus-get-info (gnus-group-group-name))
292 orig-info (gnus-copy-sequence ginfo)
293 num-unread (car (gnus-gethash (caar newsrc)
294 gnus-newsrc-hashtb)))
297 ;; We set all list of article marks to nil. Since we operate
298 ;; on copies of the real lists, we can destroy anything we
301 (setcar (nthcdr 3 ginfo) nil))
302 ;; We set the list of read articles to be what we expect for
303 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
305 (setcar (nthcdr 2 ginfo)
306 (and (not (= lowest 1)) (cons 1 lowest))))
307 (when (and (or (not ginfo)
308 (> (length (gnus-list-of-unread-articles
313 (gnus-group-select-group nil))
314 (eq major-mode 'gnus-summary-mode)))
315 ;; We are now in the group where we want to be.
316 (setq method (gnus-find-method-for-group
317 gnus-newsgroup-name))
318 (when (eq method gnus-select-method)
320 ;; We go through the list of scored articles.
321 (while gnus-newsgroup-scored
322 (when (> (caar gnus-newsgroup-scored) lowest)
323 ;; If it has a good score, then we enter this article
324 ;; into the kiboze group.
327 (gnus-summary-article-header
328 (caar gnus-newsgroup-scored))
329 gnus-newsgroup-name))
330 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
331 ;; That's it. We exit this group.
332 (when (eq major-mode 'gnus-summary-mode)
333 (kill-buffer (current-buffer)))))
334 ;; Restore the proper info.
336 (setcdr ginfo (cdr orig-info)))
337 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
339 (setcdr (car newsrc) (car active))
340 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
341 (setq newsrc (cdr newsrc)))))
342 ;; We save the kiboze newsrc for this group.
343 (with-temp-file newsrc-file
344 (insert "(setq nnkiboze-newsrc '")
345 (gnus-prin1 nnkiboze-newsrc)
347 (unless inhibit-list-groups
349 (set-buffer gnus-group-buffer)
350 (gnus-group-list-groups)))
353 (defun nnkiboze-enter-nov (buffer header group)
356 (goto-char (point-max))
357 (let ((prefix (gnus-group-real-prefix group))
358 (oheader (copy-sequence header))
360 (if (zerop (forward-line -1))
362 (setq article (1+ (read (current-buffer))))
365 (mail-header-set-number oheader article)
367 (insert (or (mail-header-xref oheader) ""))
368 (goto-char (point-min))
369 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
370 (goto-char (match-beginning 0))
371 (or (eobp) (forward-char 1)))
372 ;; The first Xref has to be the group this article
373 ;; really came for - this is the article nnkiboze
374 ;; will request when it is asked for the article.
375 (insert " " group ":"
376 (int-to-string (mail-header-number header)) " ")
377 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
378 (goto-char (1+ (match-beginning 0)))
380 (mail-header-set-xref oheader (buffer-string)))
381 (nnheader-insert-nov oheader))))
383 (defun nnkiboze-nov-file-name (&optional suffix)
384 (concat (file-name-as-directory nnkiboze-directory)
385 (nnheader-translate-file-chars
386 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
387 (or suffix ".nov")))))
391 ;;; nnkiboze.el ends here