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
38 (eval-when-compile (require 'cl))
40 (nnoo-declare nnkiboze)
41 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
42 "nnkiboze will put its files in this directory.")
44 (defvoo nnkiboze-level 9
45 "The maximum level to be searched for articles.")
47 (defvoo nnkiboze-remove-read-articles t
48 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
50 (defvoo nnkiboze-ephemeral nil
51 "If non-nil, don't store any data anywhere.")
53 (defvoo nnkiboze-scores nil
54 "Score rules for generating the nnkiboze group.")
56 (defvoo nnkiboze-regexp nil
57 "Regexp for matching component groups.")
59 (defvoo nnkiboze-file-coding-system mm-text-coding-system
60 "Coding system for nnkiboze files.")
64 (defconst nnkiboze-version "nnkiboze 1.0")
66 (defvoo nnkiboze-current-group nil)
67 (defvoo nnkiboze-status-string "")
69 (defvoo nnkiboze-headers nil)
73 ;;; Interface functions.
75 (nnoo-define-basics nnkiboze)
77 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
78 (nnkiboze-possibly-change-group group)
79 (unless gnus-nov-is-evil
80 (if (stringp (car articles))
82 (let ((nov (nnkiboze-nov-file-name)))
83 (when (file-exists-p nov)
85 (set-buffer nntp-server-buffer)
87 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
88 (nnheader-insert-file-contents nov))
89 (nnheader-nov-delete-outside-range
90 (car articles) (car (last articles)))
93 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
94 (nnkiboze-possibly-change-group newsgroup)
95 (if (not (numberp article))
96 ;; This is a real kludge. It might not work at times, but it
97 ;; does no harm I think. The only alternative is to offer no
98 ;; article fetching by message-id at all.
99 (nntp-request-article article newsgroup gnus-nntp-server buffer)
100 (let* ((header (gnus-summary-article-header article))
101 (xref (mail-header-xref header)))
103 (error "nnkiboze: No xref"))
104 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
105 (error "nnkiboze: Malformed xref"))
106 (gnus-request-article (string-to-int (match-string 2 xref))
107 (match-string 1 xref)
110 (deffoo nnkiboze-request-scan (&optional group server)
111 (nnkiboze-generate-group (concat "nnkiboze:" group)))
113 (deffoo nnkiboze-request-group (group &optional server dont-check)
114 "Make GROUP the current newsgroup."
115 (nnkiboze-possibly-change-group group)
118 (let ((nov-file (nnkiboze-nov-file-name))
121 (set-buffer nntp-server-buffer)
123 (unless (file-exists-p nov-file)
124 (nnkiboze-request-scan group))
125 (if (not (file-exists-p nov-file))
126 (nnheader-report 'nnkiboze "Can't select group %s" group)
127 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
128 (nnheader-insert-file-contents nov-file))
129 (if (zerop (buffer-size))
130 (nnheader-insert "211 0 0 0 %s\n" group)
131 (goto-char (point-min))
132 (when (looking-at "[0-9]+")
133 (setq beg (read (current-buffer))))
134 (goto-char (point-max))
135 (when (re-search-backward "^[0-9]" nil t)
136 (setq end (read (current-buffer))))
137 (setq total (count-lines (point-min) (point-max)))
138 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
140 (deffoo nnkiboze-close-group (group &optional server)
141 (nnkiboze-possibly-change-group group)
142 ;; Remove NOV lines of articles that are marked as read.
143 (when (and (file-exists-p (nnkiboze-nov-file-name))
144 nnkiboze-remove-read-articles)
145 (let ((coding-system-for-write nnkiboze-file-coding-system))
146 (with-temp-file (nnkiboze-nov-file-name)
147 (let ((cur (current-buffer))
148 (nnheader-file-coding-system nnkiboze-file-coding-system))
149 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
150 (goto-char (point-min))
152 (if (not (gnus-article-read-p (read cur)))
154 (gnus-delete-line))))))
155 (setq nnkiboze-current-group nil)))
157 (deffoo nnkiboze-open-server (server &optional defs)
158 (unless (assq 'nnkiboze-regexp defs)
159 (push `(nnkiboze-regexp ,server)
161 (nnoo-change-server 'nnkiboze server defs))
163 (deffoo nnkiboze-request-delete-group (group &optional force server)
164 (nnkiboze-possibly-change-group group)
167 (nnkiboze-score-file group)
168 (list (nnkiboze-nov-file-name)
169 (nnkiboze-nov-file-name ".newsrc")))))
171 (and (file-exists-p (car files))
172 (file-writable-p (car files))
173 (delete-file (car files)))
174 (setq files (cdr files)))))
175 (setq nnkiboze-current-group nil)
178 (nnoo-define-skeleton nnkiboze)
181 ;;; Internal functions.
183 (defun nnkiboze-possibly-change-group (group)
184 (setq nnkiboze-current-group group))
186 (defun nnkiboze-prefixed-name (group)
187 (gnus-group-prefixed-name group '(nnkiboze "")))
190 (defun nnkiboze-generate-groups ()
191 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
192 Finds out what articles are to be part of the nnkiboze groups."
194 (let ((nnmail-spool-file nil)
196 (gnus-use-dribble-file nil)
197 (gnus-read-active-file t)
198 (gnus-expert-user t))
200 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
201 (newsrc (cdr gnus-newsrc-alist))
202 gnus-newsrc-hashtb info)
203 (gnus-make-hashtable-from-newsrc-alist)
204 ;; We have copied all the newsrc alist info over to local copies
205 ;; so that we can mess all we want with these lists.
206 (while (setq info (pop newsrc))
207 (when (string-match "nnkiboze" (gnus-info-group info))
208 ;; For each kiboze group, we call this function to generate
210 (nnkiboze-generate-group (gnus-info-group info))))))
212 (defun nnkiboze-score-file (group)
213 (list (expand-file-name
214 (concat (file-name-as-directory gnus-kill-files-directory)
215 (nnheader-translate-file-chars
216 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
217 "." gnus-score-file-suffix))))))
219 (defun nnkiboze-generate-group (group)
220 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
221 (newsrc-file (concat nnkiboze-directory
222 (nnheader-translate-file-chars
223 (concat group ".newsrc"))))
224 (nov-file (concat nnkiboze-directory
225 (nnheader-translate-file-chars
226 (concat group ".nov"))))
227 method nnkiboze-newsrc gname newsrc active
228 ginfo lowest glevel orig-info nov-buffer
229 ;; Bind various things to nil to make group entry faster.
231 (gnus-large-newsgroup nil)
232 (gnus-score-find-score-files-function 'nnkiboze-score-file)
233 (gnus-verbose (min gnus-verbose 3))
234 gnus-select-group-hook gnus-summary-prepare-hook
235 gnus-thread-sort-functions gnus-show-threads
236 gnus-visual gnus-suppress-duplicates num-unread)
238 (error "No such group: %s" group))
239 ;; Load the kiboze newsrc file for this group.
240 (when (file-exists-p newsrc-file)
242 (let ((coding-system-for-write nnkiboze-file-coding-system))
243 (with-temp-file nov-file
244 (when (file-exists-p nov-file)
245 (insert-file-contents nov-file))
246 (setq nov-buffer (current-buffer))
247 ;; Go through the active hashtb and add new all groups that match the
251 (and (string-match nnkiboze-regexp
252 (setq gname (symbol-name group))) ; Match
253 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
254 (numberp (car (symbol-value group))) ; It is active
255 (or (> nnkiboze-level 7)
256 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
257 gname gnus-newsrc-hashtb))))
258 (>= nnkiboze-level glevel)))
259 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
260 (push (cons gname (1- (car (symbol-value group))))
263 ;; `newsrc' is set to the list of groups that possibly are
264 ;; component groups to this kiboze group. This list has elements
265 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
266 ;; number that has been kibozed in GROUP in this kiboze group.
267 (setq newsrc nnkiboze-newsrc)
269 (if (not (setq active (gnus-gethash
270 (caar newsrc) gnus-active-hashtb)))
271 ;; This group isn't active after all, so we remove it from
272 ;; the list of component groups.
273 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
274 (setq lowest (cdar newsrc))
275 ;; Ok, we have a valid component group, so we jump to it.
276 (switch-to-buffer gnus-group-buffer)
277 (gnus-group-jump-to-group (caar newsrc))
278 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
279 (setq ginfo (gnus-get-info (gnus-group-group-name))
280 orig-info (gnus-copy-sequence ginfo)
281 num-unread (car (gnus-gethash (caar newsrc)
282 gnus-newsrc-hashtb)))
285 ;; We set all list of article marks to nil. Since we operate
286 ;; on copies of the real lists, we can destroy anything we
289 (setcar (nthcdr 3 ginfo) nil))
290 ;; We set the list of read articles to be what we expect for
291 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
293 (setcar (nthcdr 2 ginfo)
294 (and (not (= lowest 1)) (cons 1 lowest))))
295 (when (and (or (not ginfo)
296 (> (length (gnus-list-of-unread-articles
301 (gnus-group-select-group nil))
302 (eq major-mode 'gnus-summary-mode)))
303 ;; We are now in the group where we want to be.
304 (setq method (gnus-find-method-for-group
305 gnus-newsgroup-name))
306 (when (eq method gnus-select-method)
308 ;; We go through the list of scored articles.
309 (while gnus-newsgroup-scored
310 (when (> (caar gnus-newsgroup-scored) lowest)
311 ;; If it has a good score, then we enter this article
312 ;; into the kiboze group.
315 (gnus-summary-article-header
316 (caar gnus-newsgroup-scored))
317 gnus-newsgroup-name))
318 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
319 ;; That's it. We exit this group.
320 (when (eq major-mode 'gnus-summary-mode)
321 (kill-buffer (current-buffer)))))
322 ;; Restore the proper info.
324 (setcdr ginfo (cdr orig-info)))
325 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
327 (setcdr (car newsrc) (car active))
328 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
329 (setq newsrc (cdr newsrc)))))
330 ;; We save the kiboze newsrc for this group.
331 (with-temp-file newsrc-file
332 (insert "(setq nnkiboze-newsrc '")
333 (gnus-prin1 nnkiboze-newsrc)
336 (set-buffer gnus-group-buffer)
337 (gnus-group-list-groups))
340 (defun nnkiboze-enter-nov (buffer header group)
343 (goto-char (point-max))
344 (let ((prefix (gnus-group-real-prefix group))
345 (oheader (copy-sequence header))
347 (if (zerop (forward-line -1))
349 (setq article (1+ (read (current-buffer))))
352 (mail-header-set-number oheader article)
354 (insert (mail-header-xref oheader))
355 (goto-char (point-min))
356 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
357 (goto-char (match-beginning 0))
359 ;; The first Xref has to be the group this article
360 ;; really came for - this is the article nnkiboze
361 ;; will request when it is asked for the article.
362 (insert " " group ":"
363 (int-to-string (mail-header-number header)) " ")
364 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
365 (goto-char (1+ (match-beginning 0)))
367 (mail-header-set-xref oheader (buffer-string)))
368 (nnheader-insert-nov oheader))))
370 (defun nnkiboze-nov-file-name (&optional suffix)
371 (concat (file-name-as-directory nnkiboze-directory)
372 (nnheader-translate-file-chars
373 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
374 (or suffix ".nov")))))
378 ;;; nnkiboze.el ends here