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) t))))
212 (set-buffer gnus-group-buffer)
213 (gnus-group-list-groups)))
215 (defun nnkiboze-score-file (group)
216 (list (expand-file-name
217 (concat (file-name-as-directory gnus-kill-files-directory)
218 (nnheader-translate-file-chars
219 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
220 "." gnus-score-file-suffix))))))
222 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
223 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
224 (newsrc-file (concat nnkiboze-directory
225 (nnheader-translate-file-chars
226 (concat group ".newsrc"))))
227 (nov-file (concat nnkiboze-directory
228 (nnheader-translate-file-chars
229 (concat group ".nov"))))
230 method nnkiboze-newsrc gname newsrc active
231 ginfo lowest glevel orig-info nov-buffer
232 ;; Bind various things to nil to make group entry faster.
234 (gnus-large-newsgroup nil)
235 (gnus-score-find-score-files-function 'nnkiboze-score-file)
236 ;; Use only nnkiboze-score-file!
237 (gnus-score-use-all-scores nil)
239 (gnus-verbose (min gnus-verbose 3))
240 gnus-select-group-hook gnus-summary-prepare-hook
241 gnus-thread-sort-functions gnus-show-threads
242 gnus-visual gnus-suppress-duplicates num-unread)
244 (error "No such group: %s" group))
245 ;; Load the kiboze newsrc file for this group.
246 (when (file-exists-p newsrc-file)
248 (let ((coding-system-for-write nnkiboze-file-coding-system))
249 (with-temp-file nov-file
250 (when (file-exists-p nov-file)
251 (insert-file-contents nov-file))
252 (setq nov-buffer (current-buffer))
253 ;; Go through the active hashtb and add new all groups that match the
257 (and (string-match nnkiboze-regexp
258 (setq gname (symbol-name group))) ; Match
259 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
260 (numberp (car (symbol-value group))) ; It is active
261 (or (> nnkiboze-level 7)
262 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
263 gname gnus-newsrc-hashtb))))
264 (>= nnkiboze-level glevel)))
265 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
266 (push (cons gname (1- (car (symbol-value group))))
269 ;; `newsrc' is set to the list of groups that possibly are
270 ;; component groups to this kiboze group. This list has elements
271 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
272 ;; number that has been kibozed in GROUP in this kiboze group.
273 (setq newsrc nnkiboze-newsrc)
275 (if (not (setq active (gnus-gethash
276 (caar newsrc) gnus-active-hashtb)))
277 ;; This group isn't active after all, so we remove it from
278 ;; the list of component groups.
279 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
280 (setq lowest (cdar newsrc))
281 ;; Ok, we have a valid component group, so we jump to it.
282 (switch-to-buffer gnus-group-buffer)
283 (gnus-group-jump-to-group (caar newsrc))
284 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
285 (setq ginfo (gnus-get-info (gnus-group-group-name))
286 orig-info (gnus-copy-sequence ginfo)
287 num-unread (car (gnus-gethash (caar newsrc)
288 gnus-newsrc-hashtb)))
291 ;; We set all list of article marks to nil. Since we operate
292 ;; on copies of the real lists, we can destroy anything we
295 (setcar (nthcdr 3 ginfo) nil))
296 ;; We set the list of read articles to be what we expect for
297 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
299 (setcar (nthcdr 2 ginfo)
300 (and (not (= lowest 1)) (cons 1 lowest))))
301 (when (and (or (not ginfo)
302 (> (length (gnus-list-of-unread-articles
307 (gnus-group-select-group nil))
308 (eq major-mode 'gnus-summary-mode)))
309 ;; We are now in the group where we want to be.
310 (setq method (gnus-find-method-for-group
311 gnus-newsgroup-name))
312 (when (eq method gnus-select-method)
314 ;; We go through the list of scored articles.
315 (while gnus-newsgroup-scored
316 (when (> (caar gnus-newsgroup-scored) lowest)
317 ;; If it has a good score, then we enter this article
318 ;; into the kiboze group.
321 (gnus-summary-article-header
322 (caar gnus-newsgroup-scored))
323 gnus-newsgroup-name))
324 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
325 ;; That's it. We exit this group.
326 (when (eq major-mode 'gnus-summary-mode)
327 (kill-buffer (current-buffer)))))
328 ;; Restore the proper info.
330 (setcdr ginfo (cdr orig-info)))
331 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
333 (setcdr (car newsrc) (car active))
334 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
335 (setq newsrc (cdr newsrc)))))
336 ;; We save the kiboze newsrc for this group.
337 (with-temp-file newsrc-file
338 (insert "(setq nnkiboze-newsrc '")
339 (gnus-prin1 nnkiboze-newsrc)
341 (unless inhibit-list-groups
343 (set-buffer gnus-group-buffer)
344 (gnus-group-list-groups)))
347 (defun nnkiboze-enter-nov (buffer header group)
350 (goto-char (point-max))
351 (let ((prefix (gnus-group-real-prefix group))
352 (oheader (copy-sequence header))
354 (if (zerop (forward-line -1))
356 (setq article (1+ (read (current-buffer))))
359 (mail-header-set-number oheader article)
361 (insert (or (mail-header-xref oheader) ""))
362 (goto-char (point-min))
363 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
364 (goto-char (match-beginning 0))
365 (or (eobp) (forward-char 1)))
366 ;; The first Xref has to be the group this article
367 ;; really came for - this is the article nnkiboze
368 ;; will request when it is asked for the article.
369 (insert " " group ":"
370 (int-to-string (mail-header-number header)) " ")
371 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
372 (goto-char (1+ (match-beginning 0)))
374 (mail-header-set-xref oheader (buffer-string)))
375 (nnheader-insert-nov oheader))))
377 (defun nnkiboze-nov-file-name (&optional suffix)
378 (concat (file-name-as-directory nnkiboze-directory)
379 (nnheader-translate-file-chars
380 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
381 (or suffix ".nov")))))
385 ;;; nnkiboze.el ends here