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))
104 (error "nnkiboze: No xref"))
105 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
106 (error "nnkiboze: Malformed xref"))
107 (setq num (string-to-int (match-string 2 xref))
108 group (match-string 1 xref))
109 (or (with-current-buffer buffer
110 (gnus-cache-request-article num group))
111 (gnus-request-article num group buffer)))))
113 (deffoo nnkiboze-request-scan (&optional group server)
114 (nnkiboze-generate-group (concat "nnkiboze:" group)))
116 (deffoo nnkiboze-request-group (group &optional server dont-check)
117 "Make GROUP the current newsgroup."
118 (nnkiboze-possibly-change-group group)
121 (let ((nov-file (nnkiboze-nov-file-name))
124 (set-buffer nntp-server-buffer)
126 (unless (file-exists-p nov-file)
127 (nnkiboze-request-scan group))
128 (if (not (file-exists-p nov-file))
129 (nnheader-report 'nnkiboze "Can't select group %s" group)
130 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
131 (nnheader-insert-file-contents nov-file))
132 (if (zerop (buffer-size))
133 (nnheader-insert "211 0 0 0 %s\n" group)
134 (goto-char (point-min))
135 (when (looking-at "[0-9]+")
136 (setq beg (read (current-buffer))))
137 (goto-char (point-max))
138 (when (re-search-backward "^[0-9]" nil t)
139 (setq end (read (current-buffer))))
140 (setq total (count-lines (point-min) (point-max)))
141 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
143 (deffoo nnkiboze-close-group (group &optional server)
144 (nnkiboze-possibly-change-group group)
145 ;; Remove NOV lines of articles that are marked as read.
146 (when (and (file-exists-p (nnkiboze-nov-file-name))
147 nnkiboze-remove-read-articles)
148 (let ((coding-system-for-write nnkiboze-file-coding-system))
149 (with-temp-file (nnkiboze-nov-file-name)
150 (let ((cur (current-buffer))
151 (nnheader-file-coding-system nnkiboze-file-coding-system))
152 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
153 (goto-char (point-min))
155 (if (not (gnus-article-read-p (read cur)))
157 (gnus-delete-line))))))
158 (setq nnkiboze-current-group nil)))
160 (deffoo nnkiboze-open-server (server &optional defs)
161 (unless (assq 'nnkiboze-regexp defs)
162 (push `(nnkiboze-regexp ,server)
164 (nnoo-change-server 'nnkiboze server defs))
166 (deffoo nnkiboze-request-delete-group (group &optional force server)
167 (nnkiboze-possibly-change-group group)
170 (nnkiboze-score-file group)
171 (list (nnkiboze-nov-file-name)
172 (nnkiboze-nov-file-name ".newsrc")))))
174 (and (file-exists-p (car files))
175 (file-writable-p (car files))
176 (delete-file (car files)))
177 (setq files (cdr files)))))
178 (setq nnkiboze-current-group nil)
181 (nnoo-define-skeleton nnkiboze)
184 ;;; Internal functions.
186 (defun nnkiboze-possibly-change-group (group)
187 (setq nnkiboze-current-group group))
189 (defun nnkiboze-prefixed-name (group)
190 (gnus-group-prefixed-name group '(nnkiboze "")))
193 (defun nnkiboze-generate-groups ()
194 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
195 Finds out what articles are to be part of the nnkiboze groups."
197 (let ((nnmail-spool-file nil)
199 (gnus-use-dribble-file nil)
200 (gnus-read-active-file t)
201 (gnus-expert-user t))
203 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
204 (newsrc (cdr gnus-newsrc-alist))
205 gnus-newsrc-hashtb info)
206 (gnus-make-hashtable-from-newsrc-alist)
207 ;; We have copied all the newsrc alist info over to local copies
208 ;; so that we can mess all we want with these lists.
209 (while (setq info (pop newsrc))
210 (when (string-match "nnkiboze" (gnus-info-group info))
211 ;; For each kiboze group, we call this function to generate
213 (nnkiboze-generate-group (gnus-info-group info) t))))
215 (set-buffer gnus-group-buffer)
216 (gnus-group-list-groups)))
218 (defun nnkiboze-score-file (group)
219 (list (expand-file-name
220 (concat (file-name-as-directory gnus-kill-files-directory)
221 (nnheader-translate-file-chars
222 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
223 "." gnus-score-file-suffix))))))
225 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
226 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
227 (newsrc-file (concat nnkiboze-directory
228 (nnheader-translate-file-chars
229 (concat group ".newsrc"))))
230 (nov-file (concat nnkiboze-directory
231 (nnheader-translate-file-chars
232 (concat group ".nov"))))
233 method nnkiboze-newsrc gname newsrc active
234 ginfo lowest glevel orig-info nov-buffer
235 ;; Bind various things to nil to make group entry faster.
237 (gnus-large-newsgroup nil)
238 (gnus-score-find-score-files-function 'nnkiboze-score-file)
239 ;; Use only nnkiboze-score-file!
240 (gnus-score-use-all-scores nil)
242 (gnus-verbose (min gnus-verbose 3))
243 gnus-select-group-hook gnus-summary-prepare-hook
244 gnus-thread-sort-functions gnus-show-threads
245 gnus-visual gnus-suppress-duplicates num-unread)
247 (error "No such group: %s" group))
248 ;; Load the kiboze newsrc file for this group.
249 (when (file-exists-p newsrc-file)
251 (let ((coding-system-for-write nnkiboze-file-coding-system))
252 (with-temp-file nov-file
253 (when (file-exists-p nov-file)
254 (insert-file-contents nov-file))
255 (setq nov-buffer (current-buffer))
256 ;; Go through the active hashtb and add new all groups that match the
260 (and (string-match nnkiboze-regexp
261 (setq gname (symbol-name group))) ; Match
262 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
263 (numberp (car (symbol-value group))) ; It is active
264 (or (> nnkiboze-level 7)
265 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
266 gname gnus-newsrc-hashtb))))
267 (>= nnkiboze-level glevel)))
268 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
269 (push (cons gname (1- (car (symbol-value group))))
272 ;; `newsrc' is set to the list of groups that possibly are
273 ;; component groups to this kiboze group. This list has elements
274 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
275 ;; number that has been kibozed in GROUP in this kiboze group.
276 (setq newsrc nnkiboze-newsrc)
278 (if (not (setq active (gnus-gethash
279 (caar newsrc) gnus-active-hashtb)))
280 ;; This group isn't active after all, so we remove it from
281 ;; the list of component groups.
282 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
283 (setq lowest (cdar newsrc))
284 ;; Ok, we have a valid component group, so we jump to it.
285 (switch-to-buffer gnus-group-buffer)
286 (gnus-group-jump-to-group (caar newsrc))
287 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
288 (setq ginfo (gnus-get-info (gnus-group-group-name))
289 orig-info (gnus-copy-sequence ginfo)
290 num-unread (car (gnus-gethash (caar newsrc)
291 gnus-newsrc-hashtb)))
294 ;; We set all list of article marks to nil. Since we operate
295 ;; on copies of the real lists, we can destroy anything we
298 (setcar (nthcdr 3 ginfo) nil))
299 ;; We set the list of read articles to be what we expect for
300 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
302 (setcar (nthcdr 2 ginfo)
303 (and (not (= lowest 1)) (cons 1 lowest))))
304 (when (and (or (not ginfo)
305 (> (length (gnus-list-of-unread-articles
310 (gnus-group-select-group nil))
311 (eq major-mode 'gnus-summary-mode)))
312 ;; We are now in the group where we want to be.
313 (setq method (gnus-find-method-for-group
314 gnus-newsgroup-name))
315 (when (eq method gnus-select-method)
317 ;; We go through the list of scored articles.
318 (while gnus-newsgroup-scored
319 (when (> (caar gnus-newsgroup-scored) lowest)
320 ;; If it has a good score, then we enter this article
321 ;; into the kiboze group.
324 (gnus-summary-article-header
325 (caar gnus-newsgroup-scored))
326 gnus-newsgroup-name))
327 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
328 ;; That's it. We exit this group.
329 (when (eq major-mode 'gnus-summary-mode)
330 (kill-buffer (current-buffer)))))
331 ;; Restore the proper info.
333 (setcdr ginfo (cdr orig-info)))
334 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
336 (setcdr (car newsrc) (car active))
337 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
338 (setq newsrc (cdr newsrc)))))
339 ;; We save the kiboze newsrc for this group.
340 (with-temp-file newsrc-file
341 (insert "(setq nnkiboze-newsrc '")
342 (gnus-prin1 nnkiboze-newsrc)
344 (unless inhibit-list-groups
346 (set-buffer gnus-group-buffer)
347 (gnus-group-list-groups)))
350 (defun nnkiboze-enter-nov (buffer header group)
353 (goto-char (point-max))
354 (let ((prefix (gnus-group-real-prefix group))
355 (oheader (copy-sequence header))
357 (if (zerop (forward-line -1))
359 (setq article (1+ (read (current-buffer))))
362 (mail-header-set-number oheader article)
364 (insert (or (mail-header-xref oheader) ""))
365 (goto-char (point-min))
366 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
367 (goto-char (match-beginning 0))
368 (or (eobp) (forward-char 1)))
369 ;; The first Xref has to be the group this article
370 ;; really came for - this is the article nnkiboze
371 ;; will request when it is asked for the article.
372 (insert " " group ":"
373 (int-to-string (mail-header-number header)) " ")
374 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
375 (goto-char (1+ (match-beginning 0)))
377 (mail-header-set-xref oheader (buffer-string)))
378 (nnheader-insert-nov oheader))))
380 (defun nnkiboze-nov-file-name (&optional suffix)
381 (concat (file-name-as-directory nnkiboze-directory)
382 (nnheader-translate-file-chars
383 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
384 (or suffix ".nov")))))
388 ;;; nnkiboze.el ends here